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.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouGroupTemplate;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class KuaishouCreateCreativeJob {


    @Autowired
    private IKuaiShouGroupTemplateService groupTemplateService;
    @Autowired
    private ICtopOauthTokenService tokenService;

    @XxlJob("kuaihouCreateCreative")
    public void execute() {
        QueryWrapper<KuaiShouGroupTemplate> templateQueryWrapper = new QueryWrapper<>();
        templateQueryWrapper.eq("task_status", 2);
        templateQueryWrapper.eq("creative_status", 0);
        templateQueryWrapper.orderByAsc("create_time");
        templateQueryWrapper.last("limit 20");
        List<KuaiShouGroupTemplate> list = groupTemplateService.list(templateQueryWrapper);
        if (Check.isNull(list)) {
            return;
        }
        for (KuaiShouGroupTemplate template : list) {
            CtopOauthToken token = tokenService.getTokenByAccountId(template.getAccountId());
            if (Check.isNull(token)) {
                continue;
            }
            groupTemplateService.createCreative(token.getAccessToken(), template.getUnitId(), template);
        }
    }
}
