package cn.com.ctop.job.kuaishou.handler;

import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import cn.com.ctop.kuaishou.modules.material.service.IEtlKuaishouVideoInfoService;
import cn.com.ctop.kuaishou.modules.report.service.IEtlKuaishouAccountMaterialReportDailyService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouVideoEtlInfoService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class KuaishouEtlVideoJob {
    @Autowired
    private IKuaishouVideoEtlInfoService kuaishouVideoEtlInfoService;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;
    @Autowired
    private IEtlKuaishouAccountMaterialReportDailyService materialReportDailyService;
    @Autowired
    private IEtlKuaishouVideoInfoService videoEtlInfoService;
    @Autowired
    private IEtlKuaishouAccountMaterialReportDailyService etlKuaishouAccountMaterialReportDailyService;

    /**
     * 快手爆款有效视频数据清洗
     *
     * @throws Exception
     */
    @XxlJob("kuaishouEtlVideoJob")
    public void execute() throws Exception {
        Date startDate = DateUtils.addDay(new Date(), -2);
        for (int i = 0; i <= 2; i++) {
            Date getDate = DateUtils.addDay(startDate, i);
            kuaishouVideoEtlInfoService.etlKuaishouVideoInfo(getDate);
        }
    }

    /**
     * 快手视频基础信息表清洗
     *
     * @throws Exception
     */
    @XxlJob("kuaishouEtlVideoBaseInfoJob")
    public void kuaishouEtlVideoBaseInfoJob() throws Exception {
        String statDate = DateUtils.getStartTime(DateUtils.addDay(new Date(), -1));
        videoGetService.etlVideoBaseInfoByStaDate(statDate);
    }


    @XxlJob("kuaishouEtlVideoBaseInfo")
    public void kuaishouEtlVideoBaseInfo() throws Exception {
        String statDate = DateUtils.getStartTime(DateUtils.addDay(new Date(), -7));
        videoGetService.etlVideoBaseInfoByStaDate(statDate);
    }


    /**
     * 清洗快手视频归属信息表
     */
    @XxlJob("etlFinalKuaishouVideoInfo")
    public void etlKuaishouVideoInfo() {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        videoEtlInfoService.etlKuaishouVideoInfo(anotherDay);

    }

    @XxlJob("cleanKuaishouMaterialReport")
    public void cleanKuaishouMaterialReport() {
        for (int i = 1; i <= 2; i++) {
            String date = DateUtils.formatDate(DateUtils.addDay(new Date(), -i));
            log.info("开始,{}号素材报表清洗", date);
            List<Long> accountList = etlKuaishouAccountMaterialReportDailyService.getAccountListByDate(date);
            for (int j = 0; j < accountList.size(); j++) {
                Long accountId = accountList.get(j);
                etlKuaishouAccountMaterialReportDailyService.cleanVideoInfo(accountId, date);
            }


        }
    }


    /**
     * 快手清洗素材账户报表信息
     */
    @XxlJob("etlKuaishouAccountMaterialReportInfo")
    public void etlKuaishouAccountMaterialReportInfo() {
        for (int i = 1; i <= 2; i++) {
            materialReportDailyService.etlKuaishouAccountMaterialReportInfo(DateUtils.formatDate(DateUtils.addDay(new Date(), -i)));
        }
    }

    /**
     * 清洗快手视频报表公司相关数据
     */
    @XxlJob("etlKuaishouReportCompanyInfo")
    public void etlKuaishouReportCompanyInfo() {
        materialReportDailyService.etlKuaishouReportCompanyInfo();
    }

    /**
     * 清洗快手视频开始投放时间
     */
    //TODO 待删除
    @XxlJob("etlKuaishouVideoReportStateDate")
    public void etlKuaishouVideoReportStateDate() {
        materialReportDailyService.etlKuaishouVideoReportStateDate();
    }

    /**
     * 清洗快手视频类型(真人/剪辑)
     */
    @XxlJob("etlKuaishouVideoType")
    public void etlKuaishouVideoType() {
        materialReportDailyService.etlKuaishouVideoType();
    }


}
