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.common.module.service.IRuleDataUnitKuaishouService;
import cn.com.ctop.kuaishou.modules.report.service.IRuleKuaiShouPlanService;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Component
public class RuleKuaiShouDataUnitCleanJob {
    @Autowired
    private ICtopOauthTokenService oauthTokenService;

    @Autowired
    private IRuleKuaiShouPlanService ruleKuaiShouPlanService;
    @Autowired
    private IRuleDataUnitKuaishouService ruleDataUnitKuaishouService;

    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    /**
     * 快手规则引擎组维度数据
     * @throws Exception
     */
    @XxlJob("ruleKuaiShouDataUnitCleanJob")
    public void execute() throws Exception {
        XxlJobHelper.log("规则预警组维度数据开始清洗");
        List<CtopOauthToken> tokens = oauthTokenService.queryKsValidRuleAccountsToken();
        tokens.forEach(token -> executorService.submit(() -> {
            Map<String,Object>params = new HashMap<>();
            params.put("account_id",token.getAccountId());
            ruleDataUnitKuaishouService.removeByMap(params);
            ruleKuaiShouPlanService.cleanRuleDataUnit(token.getAccountId(),token.getAccessToken(), DateUtils.date2Str(), DateUtils.date2Str(), 1);
        }));
    }

    public static void main(String[] args) {
        System.err.println(DateUtils.date2Str());
        System.err.println(DateUtils.date2Str());
    }

}
