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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.toutiao.modules.link.service.IETLReportBytedanceVideoService;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceVideoEtlInfoService;
import cn.com.ctop.toutiao.modules.report.service.IEtlBytedanceReportVideoDailyService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class BytedanceEtlVideoJob {

    static ExecutorService executorService = Executors.newFixedThreadPool(5);


    @Autowired
    private IBytedanceVideoEtlInfoService bytedanceVideoEtlInfoService;

    @Autowired
    private IEtlBytedanceReportVideoDailyService etlBytedanceReportVideoDailyService;

    @Autowired
    private IUserAllocationService userAllocationService;

    @Autowired
    private ICtopOauthTokenService tokenService;


    @XxlJob("bytedanceEtlVideoJob")
    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);
            bytedanceVideoEtlInfoService.etlBytedanceVideoInfo(getDate);
        }
    }

    //TODO 待废弃
    @XxlJob("bytedanceEtlVideoProjectJob")
    public void bytedanceEtlVideoProjectJob() throws Exception {
        Date startDate = DateUtils.addDay(new Date(), -2);
        for (int i = 0; i <= 2; i++) {
            Date getDate = DateUtils.addDay(startDate, i);
            bytedanceVideoEtlInfoService.etlBytedanceVideoInfoByProjectId(getDate);
        }
    }


    /**
     * 头条视频报表url异常 -修改
     *
     * @throws Exception
     */
    @XxlJob("updBytedanceVideoJob")
    public void updBytedanceVideoJob() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(new Date());
        List<UserAllocation> userAllocationList = userAllocationService.list();
        for (UserAllocation userAllocation : userAllocationList) {
            etlBytedanceReportVideoDailyService.updBytedanceVideo(userAllocation.getAccountId(), userAllocation.getProjectId(), timeStr);
        }
    }

    @Autowired
    private IETLReportBytedanceVideoService etlReportBytedanceVideoService;

    /**
     * 清洗头条剪辑
     *
     * @throws Exception
     */
    @XxlJob("etlBytedanceVideoClipInfoJob")
    public void etlBytedanceVideoClipInfoJob() throws Exception {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String date = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -2);
        etlReportBytedanceVideoService.etlBytedanceVideoClipIdJob(date);
    }

    /**
     * 清洗头条视频报表编导数据
     *
     * @throws Exception
     */
    @XxlJob("etlBytedanceVideoPlanInfoJob")
    public void etlBytedanceVideoPlanInfoJob() throws Exception {

        etlReportBytedanceVideoService.etlBytedanceVideoPlanInfoJob();
    }


    /**
     * 清洗头条拍摄
     *
     * @throws Exception
     */
    @XxlJob("etlBytedanceVideoShotInfoJob")
    public void etlBytedanceVideoShotInfoJob() throws Exception {
        etlReportBytedanceVideoService.etlBytedanceVideoShotInfoJob();
    }

    /**
     * 头条视频报表数据任务
     *
     * @throws Exception
     */
    @XxlJob("formatBytedance")
    public void etlBytedanceVideoJob() throws Exception {

        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        String startDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -2);
        //查询所有账户-头条
        List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
        tokens.forEach(token -> executorService.submit(() -> {
            etlBytedanceReportVideoDailyService.etlBytedanceVideo(token.getAccountId(), startDate);
            etlBytedanceReportVideoDailyService.etlBytedanceVideo(token.getAccountId(), endDate);
        }));
    }

}
