huminmin
2026-06-29 b16eec9b38aedda5f5e7e60f749f9b2dbba2a84e
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
package com.ruoyi.safe.specialequipment.task;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.approve.pojo.NotificationManagement;
import com.ruoyi.approve.service.NotificationManagementService;
import com.ruoyi.safe.specialequipment.mapper.SpecialEquipmentLedgerMapper;
import com.ruoyi.safe.specialequipment.pojo.SpecialEquipmentLedger;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Component
@RequiredArgsConstructor
public class SpecialEquipmentReminderTask {
    private final SpecialEquipmentLedgerMapper ledgerMapper;
    private final NotificationManagementService notificationManagementService;
    private static final int REMIND_DAYS = 30;
 
    @Scheduled(cron = "0 0 8 * * ?")
    public void remindExpire() {
        LocalDate now = LocalDate.now();
        LocalDate threshold = now.plusDays(REMIND_DAYS);
        List<SpecialEquipmentLedger> list = ledgerMapper.selectList(Wrappers.<SpecialEquipmentLedger>lambdaQuery()
                .and(wrapper -> wrapper
                        .isNotNull(SpecialEquipmentLedger::getNextInspectionDate)
                        .le(SpecialEquipmentLedger::getNextInspectionDate, threshold)
                        .or()
                        .isNotNull(SpecialEquipmentLedger::getNextMaintenanceDate)
                        .le(SpecialEquipmentLedger::getNextMaintenanceDate, threshold)));
        for (SpecialEquipmentLedger ledger : list) {
            syncReminder(ledger, now, threshold, true);
            syncReminder(ledger, now, threshold, false);
        }
    }
 
    private void syncReminder(SpecialEquipmentLedger ledger, LocalDate now, LocalDate threshold, boolean inspection) {
        LocalDate dueDate = inspection ? ledger.getNextInspectionDate() : ledger.getNextMaintenanceDate();
        if (dueDate == null || dueDate.isAfter(threshold)) {
            return;
        }
        long daysLeft = ChronoUnit.DAYS.between(now, dueDate);
        String reminderType = inspection ? "检验到期提醒" : "维保到期提醒";
        String title = ledger.getEquipmentName() + "-" + reminderType;
        String content = String.format("设备【%s】(%s) %s,%s,请及时安排处理。",
                ledger.getEquipmentName(),
                Optional.ofNullable(ledger.getEquipmentNo()).orElse(""),
                inspection ? "检验即将到期" : "维保即将到期",
                daysLeft < 0 ? ("已逾期 " + Math.abs(daysLeft) + " 天") : ("距离到期还有 " + daysLeft + " 天"));
        NotificationManagement notification = notificationManagementService.lambdaQuery()
                .eq(NotificationManagement::getTitle, title)
                .eq(NotificationManagement::getType, reminderType)
                .oneOpt()
                .orElseGet(NotificationManagement::new);
        notification.setTitle(title);
        notification.setType(reminderType);
        notification.setPriority(daysLeft <= 7 ? "高" : "中");
        notification.setStatus("1");
        notification.setExpireDate(dueDate);
        notification.setContent(content);
        notification.setDepartments(new ArrayList<>());
        notification.setSyncMethods(new ArrayList<>());
        if (notification.getId() == null) {
            notificationManagementService.save(notification);
        } else {
            notificationManagementService.updateById(notification);
        }
    }
}