2024-05-23 22:31:10 +08:00

172 lines
10 KiB
Java

package com.zhgd.xmgl.task;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.xmgl.modules.exam.entity.*;
import com.zhgd.xmgl.modules.exam.service.*;
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.service.IProjectService;
import com.zhgd.xmgl.modules.worker.entity.WorkerInfo;
import com.zhgd.xmgl.modules.worker.service.IWorkerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Component
public class ExamTask {
@Autowired
private IProjectService projectService;
@Autowired
private IExamPaperService examPaperService;
@Autowired
private IExamRecordService examRecordService;
@Autowired
private IExamCourseTaskService examCourseTaskService;
@Autowired
private IExamCourseService examCourseService;
@Autowired
private IExamCourseRecordService examCourseRecordService;
@Autowired
private IExamNoticeConfigService examConfigService;
@Autowired
private IExamTrainService examTrainService;
@Autowired
private IExamTrainRecordService examTrainRecordService;
@Autowired
private IExamNoticeService examNoticeService;
@Autowired
private IWorkerInfoService workerInfoService;
@Scheduled(cron = "0 0/1 * * * ?")
public void examNotice() {
List<Project> list = projectService.list();
List<ExamNotice> noticeList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
ExamNoticeConfig examNoticeConfig = examConfigService.getOne(Wrappers.<ExamNoticeConfig>lambdaQuery()
.eq(ExamNoticeConfig::getProjectSn, list.get(i).getProjectSn())
.eq(ExamNoticeConfig::getEnable, 1));
if (examNoticeConfig != null) {
if (examNoticeConfig.getExamNum() != null && examNoticeConfig.getExamType() != null) {
List<ExamTrain> examTrainList = examTrainService.list(Wrappers.<ExamTrain>lambdaQuery().eq(ExamTrain::getProjectSn, list.get(i).getProjectSn()));
for (int j = 0; j < examTrainList.size(); j++) {
if (DateUtil.compare(examTrainList.get(j).getTrainBeginTime(), getTime(examNoticeConfig.getExamType(), examNoticeConfig.getExamNum())) < 0) {
List<ExamTrainRecord> examTrainRecordList = examTrainRecordService.list(Wrappers.<ExamTrainRecord>lambdaQuery().eq(ExamTrainRecord::getTrainId, examTrainList.get(j).getId()));
for (int i1 = 0; i1 < examTrainRecordList.size(); i1++) {
if (examTrainRecordList.get(i1).getScore() == null) {
ExamNotice exist = examNoticeService.getOne(Wrappers.<ExamNotice>lambdaQuery().eq(ExamNotice::getWorkerId, examTrainRecordList.get(i1).getWorkerId())
.eq(ExamNotice::getRecordId, examTrainRecordList.get(i1).getId())
.eq(ExamNotice::getContent, "参加安全教育培训"));
if (exist == null) {
WorkerInfo workerInfo = workerInfoService.getById(examTrainRecordList.get(i1).getWorkerId());
ExamNotice examNotice = new ExamNotice();
examNotice.setWorkerId(examTrainRecordList.get(i1).getWorkerId().toString());
examNotice.setWorkerName(examTrainRecordList.get(i1).getWorkerName());
examNotice.setType(1);
examNotice.setContent("参加安全教育培训");
examNotice.setProjectSn(list.get(i).getProjectSn());
examNotice.setEnterpriseId(workerInfo.getEnterpriseId());
noticeList.add(examNotice);
}
}
}
List<ExamCourseRecord> examCourseRecords = examCourseRecordService.list(Wrappers.<ExamCourseRecord>lambdaQuery()
.eq(ExamCourseRecord::getTrainId, examTrainList.get(j).getId()));
for (int i2 = 0; i2 < examCourseRecords.size(); i2++) {
if (examCourseRecords.get(i2).getNumber() == null || examCourseRecords.get(i2).getNumber() == 0) {
ExamNotice exist = examNoticeService.getOne(Wrappers.<ExamNotice>lambdaQuery().eq(ExamNotice::getWorkerId, examCourseRecords.get(i2).getWorkerId())
.eq(ExamNotice::getRecordId, examCourseRecords.get(i2).getId())
.eq(ExamNotice::getContent, "参加安全教育课程学习"));
if (exist == null) {
WorkerInfo workerInfo = workerInfoService.getById(examCourseRecords.get(i2).getWorkerId());
ExamNotice examNotice = new ExamNotice();
examNotice.setWorkerId(examCourseRecords.get(i2).getWorkerId().toString());
examNotice.setWorkerName(examCourseRecords.get(i2).getWorkerName());
examNotice.setType(1);
examNotice.setContent("参加安全教育课程学习");
examNotice.setProjectSn(list.get(i).getProjectSn());
examNotice.setEnterpriseId(workerInfo.getEnterpriseId());
examNotice.setRecordId(examCourseRecords.get(i2).getId());
noticeList.add(examNotice);
}
}
}
}
}
}
// if (examNoticeConfig.getExamNum() != null && examNoticeConfig.getExamType() != null) {
// List<ExamPaper> examPapers = examPaperService.list(Wrappers.<ExamPaper>lambdaQuery().eq(ExamPaper::getProjectSn, list.get(i)));
// for (int j = 0; j < examPapers.size(); j++) {
// if (DateUtil.compare(new Date(), getTime(examNoticeConfig.getExamType(), examNoticeConfig.getExamNum())) < 0) {
// List<ExamRecord> examRecords = examRecordService.list(Wrappers.<ExamRecord>lambdaQuery().eq(ExamRecord::getExamId, examPapers.get(j)));
// for (int i1 = 0; i1 < examRecords.size(); i1++) {
// if (examRecords.get(i1).getScore() == null) {
// ExamNotice examNotice = new ExamNotice();
// examNotice.setWorkerId(examRecords.get(i1).getUserId());
// examNotice.setWorkerName(examRecords.get(i1).getUserName());
// examNotice.setType(1);
// examNotice.setContent("参加安全教育课程考试");
// examNotice.setProjectSn(list.get(i).getProjectSn());
// noticeList.add(examNotice);
// }
// }
// }
// }
// }
// if (examNoticeConfig.getCourseNum() != null && examNoticeConfig.getCourseType() != null) {
// List<ExamCourse> examCourseList = examCourseService.list(Wrappers.<ExamCourse>lambdaQuery().eq(ExamCourse::getProjectSn, list.get(i)));
// for (int j = 0; j < examCourseList.size(); j++) {
// if (DateUtil.compare(new Date(), getTime(examNoticeConfig.getCourseType(), examNoticeConfig.getCourseNum())) < 0) {
// List<ExamCourseRecord> examCourseRecords = examCourseRecordService.list(Wrappers.<ExamCourseRecord>lambdaQuery().eq(ExamCourseRecord::getCourseId, examCourseList.get(j)));
//// List<ExamCourseTask> examCourseTasks = examCourseTaskService.list(Wrappers.<ExamCourseTask>lambdaQuery()
//// .in(ExamCourseTask::getRecordId, examCourseRecords.stream().map(r -> r.getId()).collect(Collectors.toList())));
// for (int i1 = 0; i1 < examCourseRecords.size(); i1++) {
// if (examCourseRecords.get(i).getNumber() == null || examCourseRecords.get(i).getNumber() == 0) {
// ExamNotice examNotice = new ExamNotice();
// examNotice.setWorkerId(examCourseRecords.get(i1).getWorkerId().toString());
// examNotice.setWorkerName(examCourseRecords.get(i1).getWorkerName());
// examNotice.setType(1);
// examNotice.setContent("参加安全教育课程学习");
// examNotice.setProjectSn(list.get(i).getProjectSn());
// noticeList.add(examNotice);
// }
// }
// }
// }
// }
}
}
examNoticeService.saveBatch(noticeList);
}
private DateTime getTime(int type, int num){
DateTime dateTime = null;
if (type == 1) {
dateTime = DateUtil.offsetHour(new Date(), num);
} else if (type == 2) {
dateTime = DateUtil.offsetDay(new Date(), num);
}
return dateTime;
}
}