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.batch.service.IKuaishouInterfaceService2;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang.StringUtils;
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;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *  快手操作记录，将前一天绑定的账户的近6个月的数据统一跑出来
 */
@Component
public class KuaishouNewAccountOperationRecordJob {
    @Autowired
    private IKuaishouInterfaceService2 kuaishouInterfaceService2;
    @Autowired
    private ICtopOauthTokenService tokenService;
    static ExecutorService executorService = Executors.newFixedThreadPool(4);
    @XxlJob("kuaishouNewAccountOperationRecord")
    public void execute(String param) throws Exception {
        Date getDate = DateUtils.addDay(new Date(), -1);
        if(StringUtils.isNotBlank(param)){
            getDate =  DateUtils.addDay(new Date(), Integer.parseInt(param));
        }

        String date = DateUtils.formatDate(getDate);
        String startDate = DateUtils.getMonthBefore("yyyy-MM-dd", date, -6);

        //1:查询当日数据
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouTokenBefore();
        if (null == tokens || tokens.size() <= 0) {
            XxlJobHelper.log("定时获取快手操作记录数据异常：未获取到可用的token");
            XxlJobHelper.handleFail();
            return;
        }
        tokens.forEach(token -> {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    month(token.getAccountId(), token.getAccessToken(), 1, startDate, date);
                    month(token.getAccountId(), token.getAccessToken(), 2, startDate, date);
                    month(token.getAccountId(), token.getAccessToken(), 3, startDate, date);
                    month(token.getAccountId(), token.getAccessToken(), 4, startDate, date);
                    month(token.getAccountId(), token.getAccessToken(), 5, startDate, date);
                    month(token.getAccountId(), token.getAccessToken(), 6, startDate, date);
                    month(token.getAccountId(), token.getAccessToken(), 7, startDate, date);
                }
            });
        });
    }


    private void month(Long token, String accessToken, Integer operationTarget, String startDate, String endDate){
        int diffMonths = DateUtils.calDiffMonth(DateUtils.parseDate(startDate, "yyyy-MM-dd"), DateUtils.parseDate(endDate, "yyyy-MM-dd"));
        if (diffMonths == 0) {
            kuaishouInterfaceService2.getOperationRecord(token, accessToken, operationTarget, startDate, endDate);
        } else {
            for (int i = 0; i <= diffMonths; i++) {
                if (i == 0) {
                    Map<String, String> dateMap = DateUtils.getMaxMinDaysOfAddMonth(startDate, "yyyy-MM-dd", i);
                    String end = dateMap.get("endDate");
                    kuaishouInterfaceService2.getOperationRecord(token, accessToken, operationTarget, startDate, end);
                    //ytedanceAsyncTaskCreateByMonth(startDate, end, token, type);
                } else if (i == diffMonths) {
                    Map<String, String> dateMap = DateUtils.getMaxMinDaysOfAddMonth(startDate, "yyyy-MM-dd", i);
                    String start = dateMap.get("startDate");
                    kuaishouInterfaceService2.getOperationRecord(token, accessToken, operationTarget, start, endDate);
                    //bytedanceAsyncTaskCreateByMonth(start, endDate, token, type);
                } else {
                    Map<String, String> dateMap = DateUtils.getMaxMinDaysOfAddMonth(startDate, "yyyy-MM-dd", i);
                    String start = dateMap.get("startDate");
                    String end = dateMap.get("endDate");
                    kuaishouInterfaceService2.getOperationRecord(token, accessToken, operationTarget, start, end);
                    //bytedanceAsyncTaskCreateByMonth(start, end, token, type);
                }
            }
        }
    }



}
