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


import cn.com.ctop.alarm.modules.entity.AlarmRuleOperationRecord;
import cn.com.ctop.alarm.modules.mapper.AlarmRuleOperationRecordMapper;
import cn.com.ctop.alarm.modules.service.ExternalLinkService;
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.kuaishou.modules.batch.service.IKuaiShouGroupService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
import com.alibaba.fastjson.JSONObject;
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 javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class AlarmRuleRebackJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private AlarmRuleOperationRecordMapper ruleOperationRecordMapper;
    @Autowired
    private IKuaiShouUpdateService kuaiShouUpdateService;
    @Autowired
    private IKuaiShouGroupService kuaiShouGroupService;
    @Resource
    private ExternalLinkService externalLinkService;

    static ExecutorService executorKSService = Executors.newFixedThreadPool(5);
    static ExecutorService executorTTService = Executors.newFixedThreadPool(5);

    /**
     * 每日回退修改投放时段定时任务
     * @param
     * @return
     * @throws
     * @author ZHAOXA
     */
    @XxlJob("alarmRuleRebackJob")
    public void execute() throws Exception {
        rebackKuaiShou();
        rebackByteDance();
    }

    private void rebackKuaiShou() {
        List<AlarmRuleOperationRecord> ruleAccountTemplates = ruleOperationRecordMapper.queryToBeRestoredInfo("快手",DateUtils.getLastDay(DateUtils.formatDate(new Date()), 1));
        if (!Check.isNull(ruleAccountTemplates)) {
            ruleAccountTemplates.forEach(operationRecord -> {
                CtopOauthToken token = tokenService.getTokenByAccountId(operationRecord.getAccountId());
                executorKSService.submit(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject unitJson = new JSONObject();
                        unitJson.put("advertiser_id", operationRecord.getAccountId());
                        unitJson.put("unit_id", operationRecord.getUnitId());
                        unitJson.put("schedule_time", operationRecord.getBeforeValue());
                        JSONObject jsonObject = kuaiShouUpdateService.updateUnit(token.getAccessToken(), unitJson);
                        Integer code = jsonObject.getInteger("code");
                        String message = "success";
                        if (code == 0) {
                            kuaiShouGroupService.getGroupByUnitId(token.getAccessToken(), operationRecord.getAccountId(), operationRecord.getUnitId());
                        }
                    }
                });
            });
        }
    }

    private void rebackByteDance() {
        List<AlarmRuleOperationRecord> ruleAccountTemplates = ruleOperationRecordMapper.queryToBeRestoredInfo("头条",DateUtils.getLastDay(DateUtils.formatDate(new Date()), 1));
        if (!Check.isNull(ruleAccountTemplates)) {
            ruleAccountTemplates.forEach(operationRecord -> {
                CtopOauthToken token = tokenService.getTokenByAccountId(operationRecord.getAccountId());
                executorTTService.submit(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject unitJson = new JSONObject();
                        String code = externalLinkService.editBytedancePlanCpaBidOrBudget(operationRecord.getAccountId(), operationRecord.getPlanId().toString(), null, operationRecord.getBeforeValue(), null);
                    }
                });
            });
        }
    }
}
