package com.ycwl.basic.task; import cn.hutool.core.date.DateUtil; import com.ycwl.basic.biz.TemplateBiz; import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO; import com.ycwl.basic.mapper.FaceMapper; import com.ycwl.basic.mapper.TemplateMapper; import com.ycwl.basic.model.mobile.scenic.content.ContentPageVO; import com.ycwl.basic.model.pc.face.req.FaceReqQuery; import com.ycwl.basic.model.pc.face.resp.FaceRespVO; import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity; import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery; import com.ycwl.basic.model.pc.template.resp.TemplateRespVO; import com.ycwl.basic.repository.ScenicRepository; import com.ycwl.basic.repository.TemplateRepository; import com.ycwl.basic.service.pc.FaceService; import com.ycwl.basic.service.task.impl.TaskTaskServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Calendar; import java.util.Date; import java.util.List; @Slf4j @EnableScheduling @Component @Profile("prod") public class VideoTaskGenerator { @Autowired private FaceMapper faceMapper; @Autowired private TemplateBiz templateBiz; @Autowired private TaskTaskServiceImpl taskTaskService; @Autowired private TemplateMapper templateMapper; @Autowired private ScenicRepository scenicRepository; @Autowired private FaceService faceService; @Autowired private TemplateRepository templateRepository; @Scheduled(cron = "0 0 * * * *") public void generateVideoTask() { ScenicReqQuery query = new ScenicReqQuery(); query.setPageSize(1000); List scenicList = scenicRepository.list(query); if (scenicList.isEmpty()) { return; } Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); int currentHour = calendar.get(Calendar.HOUR_OF_DAY); calendar.clear(); scenicList.parallelStream().forEach(scenic -> { Long scenicId = Long.valueOf(scenic.getId()); ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenicId); if (scenicConfig == null) { log.info("当前景区{},无配置信息", scenic.getName()); return; } if (Integer.valueOf(1).equals(scenicConfig.getBookRoutine()) || Integer.valueOf(3).equals(scenicConfig.getBookRoutine())) { Integer hour = scenicConfig.getForceFinishTime(); if (hour != currentHour) { return; } // 定时逻辑 List templateList = templateRepository.getTemplateListByScenicId(scenicId); if (templateList.isEmpty()) { return; } FaceReqQuery faceReqQuery = new FaceReqQuery(); faceReqQuery.setScenicId(scenicId); faceReqQuery.setStartTime(DateUtil.beginOfDay(new Date())); faceReqQuery.setEndTime(DateUtil.endOfDay(new Date())); List list = faceMapper.list(faceReqQuery); list.forEach(face -> { faceService.matchFaceId(face.getId(), false); if (Integer.valueOf(3).equals(scenicConfig.getBookRoutine())) { // 全部生成 templateList.forEach(content -> { Long templateId = content.getId(); boolean canAutoGenerate = templateBiz.determineTemplateCanAutoGenerate(templateId, face.getId(), false); if (canAutoGenerate) { log.info("task callback: 自动生成"); taskTaskService.forceCreateTaskByFaceIdAndTempalteId(face.getId(), templateId); } else { log.info("task callback: 不自动生成"); } }); } else { Long templateId = templateList.getFirst().getId(); boolean canAutoGenerate = templateBiz.determineTemplateCanAutoGenerate(templateId, face.getId(), false); if (canAutoGenerate) { log.info("task callback: 自动生成"); taskTaskService.forceCreateTaskByFaceIdAndTempalteId(face.getId(), templateId); } else { log.info("task callback: 不自动生成"); } } try { Thread.sleep(2000L); } catch (InterruptedException e) { throw new RuntimeException(e); } }); } }); } }