package cn.com.ty.lift.ud.schoolVideo.service.impl;

import java.util.List;

import cn.com.ty.lift.ud.config.OssConfigutatioin;
import cn.com.ty.lift.ud.dataBank.mapper.LiftBrandMapper;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
import cn.com.ty.lift.ud.schoolVideo.controller.query.SchoolVideoQuery;
import cn.com.ty.lift.ud.schoolVideo.mapper.SchoolVideoMapper;
import cn.com.ty.lift.ud.schoolVideo.mapper.entity.SchoolVideoEntity;
import cn.com.xwy.util.collection.ListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.schoolVideo.service.ISchoolVideoService;
import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * 电梯学堂 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-23
 */
@Service
@Transactional
public class SchoolVideoServiceImpl extends ServiceImpl<SchoolVideoMapper, SchoolVideoEntity>
    implements ISchoolVideoService {
    @Autowired
    private SchoolVideoMapper schoolVideoMapper;
    @Autowired
    private LiftBrandMapper liftBrandMapper;

    @Override
    public RestResponse add(SchoolVideoEntity entity) {
        entity.setBrowseNum(0);
        entity.setFavoriteNum(0);
        entity.setLikeNum(0);
        entity.setStatuz(0);
        entity.setHotFlag(0);
        if (StringUtils.isBlank(entity.getCover())){
            //如果保存的时候没有上传封面图，通过oss工具获取封面图，不过该种方式只支持H264视频编码格式
            /**
             * x-oss-process=video/snapshot 操作方式
             * t=时间ms
             * w=宽度 =0时自动计算
             * h=高度 =0时自动计算
             * m 枚举值 fast如下1s前的图
             * f=jpg png
             * ar 枚举值 auto 根据图片内容自动旋转图片
             */
            String cover = entity.getUrl() + "?x-oss-process=video/snapshot,t_1000,f_jpg,w_0,h_0,m_fast";
            entity.setCover(cover);
        }

        boolean saveFlag = save(entity);
        if (saveFlag) {
            return RestResponse.success(null, "Created successfully");
        }else {
            return RestResponse.fail("Created unsuccessfully");
        }
    }

    @Override
    public void updateByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            SchoolVideoEntity bit = new SchoolVideoEntity();
            bit.setId(id);
            bit.setStatuz(status);
            schoolVideoMapper.updateById(bit);
        }
    }

    @Override
    public RestResponse examineById(SchoolVideoEntity neDto) {
        if (neDto.getCheckFlag() == 1) {
            schoolVideoMapper.examineById(neDto.getId());
            return RestResponse.success("审批通过");
        } else {
            // 修改状态为 驳回，并加上驳回理由
            schoolVideoMapper.updateInfoById(neDto.getId(), neDto.getCheckComment());
            // todo 发送消息给该用户
            return RestResponse.fail("审批驳回");
        }
    }

    @Override
    public void updateHotFlagByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            SchoolVideoEntity bit = new SchoolVideoEntity();
            bit.setId(id);
            bit.setHotFlag(status);
            schoolVideoMapper.updateById(bit);
        }
    }

    @Override
    public void updateBrowseNum(Long id) {
        schoolVideoMapper.updateBrowseNum(id);
    }

    @Override
    public RestResponse selectByExample(SchoolVideoQuery query) {
        IPage page = query.page();
        List<SchoolVideoEntity> list = schoolVideoMapper.selectByQuery(query.condition(), query);
        if (ListUtil.isNotEmpty(list)) {
            for (SchoolVideoEntity st : list) {
                LiftBrandEntity byId = liftBrandMapper.selectById(st.getBrandId());
                if (byId != null) {
                    st.setBrandName(byId.getName());
                }
            }
        }
        int total = schoolVideoMapper.countByQuery(query.condition());
        page.setRecords(list);
        page.setTotal(total);
        return RestResponse.success(page, "Queried successfully");
    }

    @Override
    public void updateReward(Long id) {
        schoolVideoMapper.updateReward(id);
    }

}
