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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.kuaishou.modules.report.service.IEtlKuaiShouAccountMaterialOverviewService;
import cn.com.ctop.kuaishou.modules.report.service.IEtlKuaiShouGroupDailyReportService;
import cn.com.ctop.kuaishou.modules.report.service.IEtlKuaiShouNewlyService;
import com.xxl.job.core.context.XxlJobHelper;
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.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class KuaishouDingPanCleanJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IEtlKuaiShouGroupDailyReportService etlKuaiShouGroupDailyReportService;
    @Autowired
    private IEtlKuaiShouAccountMaterialOverviewService accountMaterialOverviewService;

    @Autowired
    private IEtlKuaiShouNewlyService newlyService;


    static ExecutorService executorService = Executors.newFixedThreadPool(8);
    static ExecutorService materialExecutorService = Executors.newFixedThreadPool(8);

    /**
     * 快手盯盘每日上新数据
     */
    @XxlJob("cleanNewlyData")
    public void cleanNewlyData() {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String yesterdayDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        newlyService.cleanNewlyData(yesterdayDate);
        XxlJobHelper.log("快手清洗上新计划数据完成");
    }


    /**
     * 盯盘组日报数据清洗
     * @throws Exception
     */
    @XxlJob("cleanGroupReport")
    public void cleanGroupReport() throws Exception {
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if (null == tokens || tokens.isEmpty()) {
            XxlJobHelper.log("定时获取快手数据异常：未获取到可用的token");
            XxlJobHelper.handleFail();
            return;
        }
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        for (CtopOauthToken token : tokens) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    etlKuaiShouGroupDailyReportService.getGroupReport(token.getAccountId(), token.getAccessToken(), nowDate, nowDate, 1);
                }
            });
        }
    }


    /**
     * 盯盘素材概览数据
     * @throws Exception
     */
    @XxlJob("cleanAccountMaterialOverview")
    public void cleanAccountMaterialOverview() throws Exception {
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String yesterdayDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if (null == tokens || tokens.isEmpty()) {
            XxlJobHelper.log("定时获取快手数据异常：未获取到可用的token");
            XxlJobHelper.handleFail();
            return;
        }
        for (CtopOauthToken token : tokens) {
            materialExecutorService.submit(new Runnable() {
                @Override
                public void run() {
                    accountMaterialOverviewService.cleanAccountMaterialOverview(token.getAccountId(), yesterdayDate);
                }
            });
        }


    }

}
