package cn.com.ctop.job.kuaishou.handler;

import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAccountTargetTemplate;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAccountTargetTemplateService;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouNewCreateCampaign;
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.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class KuaishouAiAdsNewMaterialCreativityJob {

    @Autowired
    IAiKuaishouNewCreateCampaign aiKuaishouNewCreateCampaign;
    static ExecutorService executorService = Executors.newFixedThreadPool(5);
    static CountDownLatch countDownLatch = null;

    @Autowired
    private IAiKuaishouAccountTargetTemplateService aiKuaishouAccountTargetTemplateService;
    @XxlJob("kuaishouAiAdsNewMaterialCreativityJob")
    public void kuaishouAiAdsNewMaterialCreativityJob() {
        List<AiKuaishouAccountTargetTemplate> accountTargetTemplates = aiKuaishouAccountTargetTemplateService.getAllEffectTemplate();
        if(null==accountTargetTemplates||accountTargetTemplates.isEmpty()){
            return;
        }
        accountTargetTemplates.forEach(targetTemplate -> aiKuaishouNewCreateCampaign.kuaishouAiAdsNewMaterialCreativity(targetTemplate));
    }


    @XxlJob("kuaishouAiAddNewMaterialCreativityJob")
    public void kuaishouAiAddNewMaterialCreativityJob() {
        List<AiKuaishouAccountTargetTemplate> accountTargetTemplates = aiKuaishouAccountTargetTemplateService.getAllEffectTemplate();
        if(null==accountTargetTemplates||accountTargetTemplates.isEmpty()){
            return;
        }
        countDownLatch = new CountDownLatch(accountTargetTemplates.size());
        accountTargetTemplates.forEach(targetTemplate -> executorService.submit(()->{
            try {
                aiKuaishouNewCreateCampaign.kuaishouAiAddNewMaterialCreativity(targetTemplate, 10);
                XxlJobHelper.log("账户上新结束=>accountId:{}",targetTemplate.getAccountId());
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                countDownLatch.countDown();
            }
        }));
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        XxlJobHelper.log("全部账户上新结束");
    }

    @XxlJob("kuaishouAiAdsHotMaterialCreativityJob")
    public void kuaishouAiAdsHotMaterialCreativityJob() {
        List<AiKuaishouAccountTargetTemplate> accountTargetTemplates = aiKuaishouAccountTargetTemplateService.getAllEffectTemplate();
        if(null==accountTargetTemplates||accountTargetTemplates.isEmpty()){
            return;
        }
        accountTargetTemplates.forEach(targetTemplate -> aiKuaishouNewCreateCampaign.kuaishouAiAdsHotMaterialCreativity(targetTemplate));
    }
}
