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 list = ledgerMapper.selectList(Wrappers.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); } } }