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 KuaishouMaterialsLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    @XxlJob("kuaishouMaterials")
    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 endDate = DateUtils.getNowDate("yyyy-MM-dd");
        String startDate = DateUtils.getAnotherDay("yyyy-MM-dd", endDate, -1);
        //获取全量视频素材数据
//        kuaishouInterfaceService.getVideoList(token, startDate, endDate);
        // 获取图片信息数据
        kuaishouInterfaceService.getImageList(token, DateUtils.getNowDate("yyyy-MM-dd"));
        XxlJobHelper.log("快手物料数据同步完成");
    }
}
