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

import cn.com.ctop.toutiao.modules.link.entity.BaseReportBytedanceVideoDaily;
import cn.com.ctop.toutiao.modules.link.entity.ETLReportBytedanceVideo;
import cn.com.ctop.toutiao.modules.link.mapper.ETLReportBytedanceVideoMapper;
import cn.com.ctop.toutiao.modules.link.service.IBaseReportBytedanceVideoDailyService;
import cn.com.ctop.toutiao.modules.link.service.IETLReportBytedanceVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 头条视频报表ETL
 */
@Slf4j
@Service
public class ETLReportBytedanceVideoServiceImpl extends ServiceImpl<ETLReportBytedanceVideoMapper, ETLReportBytedanceVideo> implements IETLReportBytedanceVideoService {

    @Autowired
    IBaseReportBytedanceVideoDailyService baseReportBytedanceVideoDailyService;

    @Resource
    ETLReportBytedanceVideoMapper etlReportBytedanceVideoMapper;

    @Override
    public List<BaseReportBytedanceVideoDaily> listByStatDate(String statDate) {
        QueryWrapper<BaseReportBytedanceVideoDaily>  queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("stat_datetime",statDate);
        return baseReportBytedanceVideoDailyService.list(queryWrapper);
    }

    @Override
    public void cleanETL(String statDate) {
        List<ETLReportBytedanceVideo> etlReportBytedanceVideos = etlReportBytedanceVideoMapper.queryETLData(statDate);
        if(etlReportBytedanceVideos.isEmpty()){
            log.warn("{},头条etl报表清洗异常，base数据源为空",statDate);
            return;
        }
        boolean result = this.saveBatch(etlReportBytedanceVideos);
        if(result){
            log.info("{},头条etl报表清洗完成，数量：{}",statDate,etlReportBytedanceVideos.size());
        } else{
            log.error("{},头条etl报表清洗失败,请重试",statDate);
        }
    }

    @Override
    public void cleanData(String date){
        List<ETLReportBytedanceVideo> videos = etlReportBytedanceVideoMapper.getDuplicateVideoInfo(date);
        if(null!=videos&&!videos.isEmpty()){
            videos.forEach(video->{
                etlReportBytedanceVideoMapper.deleteDuplicateVideoInfo(video);
            });
        }
    }

    @Override
    public void etlBytedanceVideoClipIdJob() {
        List <ETLReportBytedanceVideo> clipInfos = etlReportBytedanceVideoMapper.getBytedanceVideoClipInfo();
        if(null!=clipInfos&&!clipInfos.isEmpty()){
            clipInfos.forEach(clipInfo->{
                etlReportBytedanceVideoMapper.updateClipInfo(clipInfo.getClipId(),clipInfo.getClipName(),clipInfo.getMd5());
            });
        }
        etlReportBytedanceVideoMapper.updateDesignTeamLeader();
        etlReportBytedanceVideoMapper.updateTeamLeader();
    }

    @Override
    public void etlBytedanceVideoPlanInfoJob() {
        List <ETLReportBytedanceVideo> planInfos = etlReportBytedanceVideoMapper.getBytedanceVideoPlanInfo();
        if(null!=planInfos&&!planInfos.isEmpty()){
            planInfos.forEach(planInfo->{
                etlReportBytedanceVideoMapper.updatePlanInfo(planInfo.getPlanId(),planInfo.getPlanName(),planInfo.getMd5());
            });
        }
    }

    @Override
    public void etlBytedanceVideoShotInfoJob() {
        List <ETLReportBytedanceVideo> shotInfos = etlReportBytedanceVideoMapper.getBytedanceVideoShotInfo();
        if(null!=shotInfos&&!shotInfos.isEmpty()){
            shotInfos.forEach(shotInfo->{
                etlReportBytedanceVideoMapper.updateShotInfo(shotInfo.getShotId(),shotInfo.getShotName(),shotInfo.getMd5());
            });
        }
    }

}
