buhuazhen
3 天以前 0d7c3e7923c420ae6d3f8feff280bf4fcaac5ce7
src/main/java/com/ruoyi/device/service/impl/MaintenanceTaskJob.java
@@ -1,9 +1,17 @@
package com.ruoyi.device.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.device.pojo.DeviceMaintenance;
import com.ruoyi.device.pojo.MaintenanceTask;
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.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@@ -16,16 +24,28 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.ArrayList;
@Component
@DisallowConcurrentExecution // 禁止并发执行同一个Job
@RequiredArgsConstructor
@DisallowConcurrentExecution
@Slf4j
public class MaintenanceTaskJob implements Job, Serializable {
    private static final long serialVersionUID = 1L; // 必须定义序列化ID
    private static final long serialVersionUID = 1L;
    private final DeviceMaintenanceServiceImpl deviceMaintenanceService;
    @Autowired
    private DeviceMaintenanceServiceImpl deviceMaintenanceService;
    private final JdbcTemplate jdbcTemplate;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private StaffOnJobMapper staffOnJobMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
@@ -50,6 +70,9 @@
            // 2. 创建并保存巡检任务记录 - 这就是您提供的代码应该放的位置
            DeviceMaintenance deviceMaintenance = createInspectionTask(timingTask);
            deviceMaintenanceService.save(deviceMaintenance);
            // 发送保养提醒通知
            sendMaintenanceReminder(timingTask);
            // 3. 更新定时任务的执行时间
            if (!tasks.isEmpty()) {
@@ -91,6 +114,7 @@
        inspectionTask.setMaintenanceTaskId(timingTask.getId());
        inspectionTask.setDeviceLedgerId(timingTask.getTaskId());
        inspectionTask.setMaintenancePlanTime(LocalDateTime.now());
        inspectionTask.setMaintenanceActuallyName(timingTask.getMaintenancePerson());
        inspectionTask.setFrequencyType(timingTask.getFrequencyType());
        inspectionTask.setFrequencyDetail(timingTask.getFrequencyDetail());
        inspectionTask.setTenantId(timingTask.getTenantId());
@@ -240,4 +264,51 @@
        return days;
    }
    /**
     * 发送设备保养提醒通知
     * @param timingTask 保养任务
     */
    private void sendMaintenanceReminder(MaintenanceTask timingTask) {
        try {
            if (timingTask.getMaintenancePerson() == null || timingTask.getMaintenancePerson().isEmpty()) {
                log.warn("保养任务 {} 未指定保养人,跳过推送", timingTask.getTaskName());
                return;
            }
            // 通过保养人姓名查询员工档案
            List<StaffOnJob> staffList = staffOnJobMapper.selectList(Wrappers.<StaffOnJob>lambdaQuery()
                    .eq(StaffOnJob::getStaffName, timingTask.getMaintenancePerson())
                    .eq(StaffOnJob::getStaffState, 1)
                    .last("LIMIT 1"));
            if (staffList.isEmpty()) {
                log.warn("未找到保养人 {} 的员工档案", timingTask.getMaintenancePerson());
                return;
            }
            StaffOnJob staff = staffList.get(0);
            // 通过员工编号查询系统用户
            SysUser sysUser = sysUserMapper.selectUserByUserName(staff.getStaffNo());
            if (sysUser == null) {
                log.warn("保养人 {} 未找到对应的系统用户账号", staff.getStaffName());
                return;
            }
            // 构建通知内容
            String title = "设备保养提醒";
            String message = String.format("您好,设备【%s】需要进行保养,请及时处理。",
                    timingTask.getTaskName());
            // 发送通知
            List<Long> userIds = new ArrayList<>();
            userIds.add(sysUser.getUserId());
            sysNoticeService.simpleNoticeByUser(title, message, userIds, "/deviceMaintenance");
            log.info("已向保养人 {} 发送设备保养提醒", timingTask.getMaintenancePerson());
        } catch (Exception e) {
            log.error("发送设备保养提醒失败:{}", e.getMessage(), e);
        }
    }
}