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.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.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class KuaishouOperationRecordJob {
    @Autowired
    private IKuaishouInterfaceService2 kuaishouInterfaceService2;
    @Autowired
    private ICtopOauthTokenService tokenService;

    static ExecutorService executorService = Executors.newFixedThreadPool(4);

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

}
