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.IKuaishouAudienceReportDailyService;
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;

/**
 * 快手广告主分天数据
 *
 * @author syh
 */
@Component
public class KuaishouDailyAccountAudienceReportJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouAudienceReportDailyService kuaishouAudienceReportDailyService;

    static ExecutorService executorService = Executors.newFixedThreadPool(5);

    /**
     * 人群分析报表账户级每日数据
     * @throws Exception
     */
    @XxlJob("kuaishouAccountAudienceReportJob")
    public void execute() throws Exception {
        //查询两天前数据  策略
        String date = DateUtils.getLastDay(DateUtils.date2Str(), 2);
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if (null == tokens || tokens.isEmpty()) {
            XxlJobHelper.log("定时获取人群分析报表数据异常：未获取到可用的token");
            XxlJobHelper.handleFail();
            return;
        }
        int page = 1;
        int pageSize = 2000;
        tokens.forEach(token -> executorService.submit(() -> {
            kuaishouAudienceReportDailyService.crowdAnalysisReport(token, date, "province", null, null, page, pageSize);
            kuaishouAudienceReportDailyService.crowdAnalysisReport(token, date, "city", null, null, page, pageSize);
            kuaishouAudienceReportDailyService.crowdAnalysisReport(token, date, "gender", null, null, page, pageSize);
            kuaishouAudienceReportDailyService.crowdAnalysisReport(token, date, "ageSegment", null, null, page, pageSize);
            kuaishouAudienceReportDailyService.crowdAnalysisReport(token, date, "clientId", null, null, page, pageSize);
            kuaishouAudienceReportDailyService.crowdAnalysisReport(token, date, "businessInterestTags", null, null, page, pageSize);
        }));
    }
}
