package org.jeecg.modules.ctop.job;

import cn.com.ctop.common.module.entity.MaterialCutFrameTask;
import cn.com.ctop.common.module.service.IMaterialCutFrameService;
import cn.com.ctop.common.module.service.IMaterialCutFrameTaskService;
import cn.com.ctop.common.module.utils.Check;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.Executors;

import static org.jeecg.modules.ctop.job.KuaishouDailyGroupReportLoadJob.executorService;

/**
 * 获取抽帧视频
 */
@Slf4j
public class CutFrameJob implements Job {
    @Autowired
    private IMaterialCutFrameService materialCutFrameService;
    @Autowired
    private IMaterialCutFrameTaskService taskService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Thread thread = new Thread() {
            @Override
            public void run() {

                log.info("开始抽帧定时任务");
                QueryWrapper<MaterialCutFrameTask> taskQueryWrapper = new QueryWrapper<>();
                taskQueryWrapper.eq("job_status", 0);
                List<MaterialCutFrameTask> list = taskService.list(taskQueryWrapper);

                if (Check.isNull(list)) {
                    return;
                }
                executorService = Executors.newFixedThreadPool(5);
                list.forEach(cutFrameTask -> {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            //获取广告计划信息数据
                            materialCutFrameService.loadCutFrame(cutFrameTask.getJobId(), cutFrameTask.getVideoSignature());
                        }
                    });
                });
            }
        };
        thread.start();


    }
}
