package cn.com.ctop.toutiao.modules.material.service.impl;

import cn.com.ctop.toutiao.modules.material.entity.BytedanceEffectVideoInfo;
import cn.com.ctop.toutiao.modules.material.mapper.BytedanceEffectVideoInfoMapper;
import cn.com.ctop.toutiao.modules.material.service.IBytedanceEffectVideoInfoService;
import cn.com.ctop.toutiao.modules.material.vo.BytedanceVideoVo;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceReportMaterialDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BytedanceEffectVideoInfoServiceImpl extends ServiceImpl<BytedanceEffectVideoInfoMapper, BytedanceEffectVideoInfo> implements IBytedanceEffectVideoInfoService {
    @Autowired
    private IBytedanceReportMaterialDailyService bytedanceReportMaterialDailyService;
    @Autowired
    private BytedanceEffectVideoInfoMapper effectVideoInfoMapper;
    @Override
    public void loadEffectVideo(String date) {
        //1:查询素材报表
        List<BytedanceVideoVo> vos = bytedanceReportMaterialDailyService.getVideoVoByDate(date);
        if(null == vos||vos.isEmpty()){
            XxlJobHelper.log("当日头条素材数据报表为空,date:{}",date);
            return;
        }
        for (BytedanceVideoVo vo:vos) {
            BytedanceEffectVideoInfo effectVideoInfo = new BytedanceEffectVideoInfo();
            effectVideoInfo.setCode(vo.getCode());
            effectVideoInfo.setUrl(vo.getUrl());
            effectVideoInfo.setInitialCostDate(vo.getInitialCostDate());
            effectVideoInfo.setCreateTime(new Date());
            effectVideoInfo.setUpdateTime(new Date());
            this.save(effectVideoInfo);
        }
        //1:计算最后消耗时间
        effectVideoInfoMapper.updateFinalCostDate();
        //2:计算最终消耗总和
        effectVideoInfoMapper.updateTotalCost();
        //3:计算两周消耗
        effectVideoInfoMapper.updateTwoWeekCost();
        //4:同步修改拍摄id
        effectVideoInfoMapper.updateShot();
        //5:同步修改剪辑id
        effectVideoInfoMapper.updateClip();
        //6:同步修改策划id
        effectVideoInfoMapper.updatePlan();
        //7:同步修改平面id
        effectVideoInfoMapper.updatePlane();
        //8:负责人信息
        effectVideoInfoMapper.updateManager();
    }

    @Override
    public BytedanceEffectVideoInfo getVideoByCode(String code) {
        QueryWrapper<BytedanceEffectVideoInfo>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code",code);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
