EduSendMsgJob.java 4.1 KB
package com.skua.modules.edu.job;

import com.skua.core.util.DateUtils;
import com.skua.modules.edu.entity.Paper;
import com.skua.modules.edu.service.IPaperService;
import com.skua.modules.system.service.ISysUserService;
import com.skua.modules.system.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 发送消息任务
 * (2)、考试开始时间前1天推送一次
 * (3)、考试结束时间前1天推送一次(如果还未完成的话)
 */
@Slf4j
@Component
public class EduSendMsgJob implements Job {

	@Autowired
	private ISysUserService userService;
	@Autowired
	private IPaperService paperService;
	@Autowired
	private SmsService  smsService;
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info(String.format(" Jeecg-Boot 发送消息任务 SendMsgJob !  时间:" + DateUtils.getTimestamp()));

		// 获取当前日期
		LocalDate today = LocalDate.now();
		// 获取昨天的日期
		LocalDate yesterday = today.minusDays(1);
		LocalDate tomorrowDay = today.plusDays(1);

		// 创建一个DateTimeFormatter对象,指定格式为yyyy-MM-dd
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		// 使用formatter格式化LocalDate对象
		String yesterDate = yesterday.format(formatter);
		String tomorrowDate = tomorrowDay.format(formatter);

		String messageTitle = null;
		String messageBody = null;
		String userIds = null;

		String msgCategory = "12";
		List<Paper> paperList = paperService.queryExamUserIds(yesterDate,null);
		if(paperList	!= null && !paperList.isEmpty()){
			for(Paper paper : paperList){
				messageTitle = "考试任务:<"+paper.getPaperTitle() +">明天开始,请参加考试";
				//messageBody = "明天考试任务:"+paper.getPaperTitle() +",请参加考试";
				messageBody =  "考试任务:<"+paper.getPaperTitle() +">明天开始,考试时间:"+paper.getStartTime() +"至"+paper.getEndTime()+",请准时参加考试";
				userIds = getNoExamUserIds(paper.getExamUserIds(),paper.getUserIds());
				if(StringUtils.isNotEmpty(userIds)){
					//pushMsgNoProcess(messageTitle,messageBody,userIds);
					smsService.pushMsgNoProcess( messageTitle , messageBody  , userIds, "Paper", msgCategory,"H");
				}

			}
		}
		paperList = paperService.queryExamUserIds(null,tomorrowDate);
		if(paperList	!= null && !paperList.isEmpty()){
			for(Paper paper : paperList){
				messageTitle = "考试任务:<"+paper.getPaperTitle() +">明天结束,请尽快考试";
				//messageBody = "考试:"+paper.getPaperTitle() +"明天结束,请尽快参加考试";
				messageBody =  "考试任务:<"+paper.getPaperTitle() +">明天结束,考试时间:"+paper.getStartTime() +"至"+paper.getEndTime()+",请准时参加考试";
				userIds = getNoExamUserIds(paper.getExamUserIds(),paper.getUserIds());
				if(StringUtils.isNotEmpty(userIds)){
					//pushMsgNoProcess(messageTitle,messageBody,userIds);
					smsService.pushMsgNoProcess( messageTitle , messageBody  , userIds, "Paper", msgCategory,"H");
				}
			}
		}
	}

	/***
	 * 获取第一个字符串列表中存在而第二个字符串列表中不存在的元素
	 * @param string1
	 * @param string2
	 * @return
	 */
	private String  getNoExamUserIds(String string1 , String string2){
// 分割字符串
		String[] array1 = string1.split(",");
		String[] array2 = string2.split(",");

		// 将array2转换为Set集合
		Set<String> set2 = new HashSet<>(Arrays.asList(array2));

		// 获取在array1中但不在set2中的元素
		String result = Arrays.stream(array1)
				.filter(element -> !set2.contains(element))
				.collect(Collectors.joining(","));
		return result;
	}
}