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.IRuleDataTargetKuaishouService;
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.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 RuleKuaiShouDataTargetCleanJob {
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IRuleKuaiShouPlanService ruleKuaiShouPlanService;
    @Autowired
    private IRuleDataTargetKuaishouService ruleDataTargetKuaishouService;
    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    @XxlJob("ruleKuaiShouDataTargetCleanJob")
    public void execute() throws Exception {
        XxlJobHelper.log("规则预警定向维度数据开始清洗");
        List<CtopOauthToken> tokens = oauthTokenService.selectKuaiShouToken();
        tokens.forEach(token -> executorService.submit(() ->{
            Map<String,Object>params = new HashMap<>();
            params.put("account_id",token.getAccountId());
            ruleDataTargetKuaishouService.removeByMap(params);
            ruleKuaiShouPlanService.cleanRuleDataTarget(token.getAccountId(), 1);
        }));
    }

}
