buhuazhen
3 天以前 0d7c3e7923c420ae6d3f8feff280bf4fcaac5ce7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.ruoyi.staff.task;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISysNoticeService;
import com.ruoyi.staff.pojo.StaffOnJob;
import com.ruoyi.staff.mapper.StaffOnJobMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 人员合同到期提醒定时任务
 * 检查即将到期的合同,推送通知给相关员工
 *
 * @author system
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class StaffContractReminderTask {
 
    private final StaffOnJobMapper staffOnJobMapper;
    private final SysUserMapper sysUserMapper;
    private final ISysNoticeService sysNoticeService;
 
    /**
     * 每天早上8点执行合同到期提醒
     * 提前30天、7天、1天提醒
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void contractExpirationReminder() {
        log.info("开始执行合同到期提醒任务...");
        try {
            LocalDate today = LocalDate.now();
 
            // 提前30天提醒
            sendReminder(today.plusDays(30), "30天后到期");
 
            // 提前7天提醒
            sendReminder(today.plusDays(7), "7天后到期");
 
            // 提前1天提醒
            sendReminder(today.plusDays(1), "1天后到期");
 
            log.info("合同到期提醒任务执行完成");
        } catch (Exception e) {
            log.error("合同到期提醒任务执行失败:{}", e.getMessage(), e);
        }
    }
 
    /**
     * 发送合同到期提醒
     * @param expireDate 到期日期
     * @param daysDesc 剩余天数描述
     */
    private void sendReminder(LocalDate expireDate, String daysDesc) {
        Date targetDate = Date.from(expireDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
 
        // 查询合同到期日期等于目标日期的在职员工
        List<StaffOnJob> staffList = staffOnJobMapper.selectList(Wrappers.<StaffOnJob>lambdaQuery()
                .eq(StaffOnJob::getStaffState, 1)
                .eq(StaffOnJob::getContractExpireTime, targetDate));
 
        if (CollectionUtil.isEmpty(staffList)) {
            return;
        }
 
        for (StaffOnJob staff : staffList) {
            try {
                // 通过员工编号查询系统用户
                SysUser sysUser = sysUserMapper.selectUserByUserName(staff.getStaffNo());
                if (sysUser == null) {
                    log.warn("员工 {} 未找到对应的系统用户账号", staff.getStaffName());
                    continue;
                }
 
                // 构建通知内容
                String title = "合同到期提醒";
                String message = String.format("您好,%s 的合同将于%s到期,请及时处理相关事宜。",
                        staff.getStaffName(), daysDesc);
 
                // 发送通知
                List<Long> userIds = new ArrayList<>();
                userIds.add(sysUser.getUserId());
                sysNoticeService.simpleNoticeByUser(title, message, userIds, "/staffOnJob");
 
                log.info("已向员工 {} 发送合同到期提醒", staff.getStaffName());
            } catch (Exception e) {
                log.error("向员工 {} 发送合同到期提醒失败:{}", staff.getStaffName(), e.getMessage());
            }
        }
    }
}