RemindUtil.java 3.86 KB
Newer Older
邓实川 committed
1
package cn.timer.api.utils.schedule;
2 3 4 5 6

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

7 8
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
9 10
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
邓实川 committed
11
import org.springframework.scheduling.annotation.SchedulingConfigurer;
邓实川 committed
12 13
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
14 15 16 17 18 19 20 21
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.timer.api.bean.htzz.HtzzAdminZzda;
import cn.timer.api.bean.htzz.HtzzAssoHtgx;
邓实川 committed
22
import cn.timer.api.utils.aliyun.AliyunSMS;
23 24 25 26 27 28 29 30

/**
 * 记录当前时间循环输出 遍历合同提醒关系表,每天固定时间(8点)查询是否提醒
 * 
 * @author Administrator
 *
 */
@Component
31
@Lazy
邓实川 committed
32
public class RemindUtil implements SchedulingConfigurer {
33 34 35 36 37 38 39 40 41 42 43 44
	
	@Autowired
	private AliyunSMS sms;
	
	@Value("${config-8timer.remind.one}")
	private int one;
	
	@Value("${config-8timer.remind.two}")
	private int two;
	
	@Value("${config-8timer.remind.three}")
	private int three;
邓实川 committed
45 46 47 48 49 50

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	static String className = new Exception().getStackTrace()[0].getClassName();
	static String methodName = null;

	// 数据库动态更改定时配置
邓实川 committed
51 52 53
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.addTriggerTask(() -> {
邓实川 committed
54 55
			// 任务逻辑
			methodName = reportCurrentTime();
邓实川 committed
56
		}, triggerContext -> {
邓实川 committed
57 58
			CronTrigger cronTrigger = new CronTrigger(CronUtil.getCron(className, methodName)); // cron配置
			return cronTrigger.nextExecutionTime(triggerContext); // 下次执行任务的时间
邓实川 committed
59 60
		});
	}
61 62

	/**
63
	 * 间隔时间提醒 30min/次
64 65 66 67 68 69 70 71 72
	 */
	@Scheduled(fixedRate = 30 * 60 * 1000)
	private void now() {
		System.err.println("当前时间:" + dateFormat.format(new Date()));
	}

	/**
	 * 每天固定时间提醒
	 */
邓实川 committed
73
//	@Scheduled(cron = "0 0 8 * * ?") // 每天8点扫一下看有没要提醒的,有就发一个
74
	public  String reportCurrentTime() {
75

76 77 78 79 80 81 82
		List<HtzzAssoHtgx> htgxs = HtzzAssoHtgx.builder().build().selectAll();
		for (HtzzAssoHtgx htgx : htgxs) {
			QueryWrapper<HtzzAdminZzda> q = new QueryWrapper<HtzzAdminZzda>();
			q.select("id", "zjmc", "txkg_type", "yxdqr").eq("id", htgx.getHtid()).eq("is_delete", 0).eq("txkg_type", 0);
			HtzzAdminZzda zzda = HtzzAdminZzda.builder().build().selectOne(q);
			if (zzda != null) {
				String phone = htgx.getPhone(); // 员工手机
83
				String name = htgx.getName(); // 员工姓名
84 85 86 87 88 89
				String htname = zzda.getZjmc(); // 合同名称
				Date now = new Date(); // 当前时间
				Date dqsj = zzda.getYxdqr(); // 到期时间
				String time = DateUtil.formatDate(dqsj); // 到期時間格式
				Long betweenDay = DateUtil.between(dqsj, now, DateUnit.DAY); // 时间差天数
				Long sjc = dqsj.getTime() - now.getTime(); // 时间差数值
90
				System.err.println(name + " 的 " + htname + "还有: " + betweenDay + " 天到期");
91 92
				if (sjc > 0) {
					if (betweenDay <= 1) {
93
						sms.remind(name, htname, time, phone); // 少于1天短信提醒
94 95 96 97 98 99
					} else if (betweenDay == one) {
						sms.remind(name, htname, time, phone); // 短信提醒1
					} else if (betweenDay == two) {
						sms.remind(name, htname, time, phone); // 短信提醒2
					} else if (betweenDay == three) {
						sms.remind(name, htname, time, phone); // 短信提醒3
100 101 102 103 104 105 106
					}
				} else {
					zzda.setTxkgType(1); // 关闭提醒
					zzda.updateById();
				}
			}
		}
邓实川 committed
107
		return new Exception().getStackTrace()[0].getMethodName();
108 109
	}

yuquan.zhu committed
110
}