package com.xxl.job.executor.jobhandler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.batch.service.IKuaishouInterfaceService;
import com.xxl.job.executor.common.entity.CtopOauthToken;
import com.xxl.job.executor.common.service.ICtopOauthTokenService;
import com.xxl.job.executor.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class KuaishouSuZaoLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    @XxlJob("kuaishouSuZao")
    public void execute() {
        String param = XxlJobHelper.getJobParam();
        if (param==null || param.trim().length()==0) {
            XxlJobHelper.log("param["+ param +"] invalid.");
            XxlJobHelper.handleFail();
            return;
        }
        CtopOauthToken token = tokenService.getOauthTokenByAccountId(param);
        if (null == token) {
            XxlJobHelper.log("获取头条token数据异常：账户{}token不存在",param);
            XxlJobHelper.handleFail();
            return;
        }
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String endDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, 1);
        String startDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        kuaishouInterfaceService.getSuZaoList(token.getAccessToken(), token.getAccountId(), 1, startDate, endDate);
        XxlJobHelper.log("快手素造数据同步完成");
    }


}
