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

import cn.com.ctop.alarm.modules.entity.RuleAccountTemplate;
import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.HttpUtils2;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouGroup;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouStrategyService;
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.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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

    static ExecutorService executorService = null;

    /**
     * 人群分析报表每日组级数据
     * @throws Exception
     */
    @XxlJob("kuaishouDailyAudienceReportJob")
    public void execute() throws Exception {
        Thread thread = new Thread() {
            @Override
            public void run() {
                List<String> dates = new ArrayList<>();
                for (int i = 5; i < 36; i++) {
                    String dat = DateUtils.getLastDay(DateUtils.date2Str(), i);
                    dates.add(dat);
                }
                XxlJobHelper.log("-----------清洗日期：" + dates.toString());
                //  9556344、9743727、9743738、9812668、9901966、9767034
                CtopOauthToken token = tokenService.getTokenByAccountId(9812668L);
                int page = 1;
                int pageSize = 2000;
                List<KuaiShouGroup> kuaiShouGroups = kuaiShouGroupService.queryUnitInfoByAccountId(token.getAccountId());
                executorService = Executors.newFixedThreadPool(8);
                for (String date : dates) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            getAudienceReportData(kuaiShouGroups, token, date, "ageSegment", page, pageSize);
                        }
                    });
                }
            }
        };
        thread.start();
    }

    private void getAudienceReportData(List<KuaiShouGroup> kuaiShouGroups, CtopOauthToken token, String date, String type, Integer page, Integer pageSize) {
        try {
            for (KuaiShouGroup kuaiShouGroup : kuaiShouGroups) {
                kuaishouAudienceReportDailyService.crowdAnalysisReport(token, date, type, kuaiShouGroup.getCampaignId(), kuaiShouGroup.getUnitId(), page, pageSize);
                Thread.sleep(600);
            }
        } catch (Exception e) {
            XxlJobHelper.log("定时获取人群分析报表数据异常", e);
        }
    }


    String URLPREFIX = "http://api.bjhcstszkj.cn/jeecg-boot/audienceReport/getOneAudienceDataNew?accountId=";
    @XxlJob("kuaishouDailyTaoTeAudienceReportJob")
    public void getTaoTeAudienceData() throws Exception {
        List<CtopOauthToken> tokens = tokenService.getByProjectId(458L);    //淘特项目
        if (!Check.isNull(tokens)) {
            tokens.forEach(token -> {
                String url = URLPREFIX + token.getAccountId();
                HttpUtils.httpGet(url, null, null);
            });
        }
    }

    @XxlJob("kuaishouDailyTaoTeAudienceReportAggregationJob")
    public void kuaishouDailyTaoTeAudienceReportAggregationJob() throws Exception {
        //获取日期
        String date = DateUtils.addDay(DateUtils.formatDate(),-1);
        String param = XxlJobHelper.getJobParam();
        if (param==null || param.trim().length()==0) {
            param = date;
        }
        XxlJobHelper.log("人群分析报表数据开始聚合==>日期:", param);
        kuaishouAudienceReportDailyService.aggregationDataByProjectId(param,458L);
    }


}
