吴羡
7 小时以前 2f07589b54d56085af77b50d580890cd4130b444
cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceMaintenancePlanServiceImpl.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -18,6 +19,8 @@
import com.ruoyi.device.dto.DeviceMaintenancePlanDto;
import com.ruoyi.device.mapper.DeviceMaintenancePlanDetailsMapper;
import com.ruoyi.device.mapper.DeviceMaintenancePlanMapper;
import com.ruoyi.device.mapper.DeviceMapper;
import com.ruoyi.device.pojo.Device;
import com.ruoyi.device.pojo.DeviceMaintenancePlan;
import com.ruoyi.device.pojo.DeviceMaintenancePlanDetails;
import com.ruoyi.device.service.DeviceMaintenancePlanDetailsService;
@@ -30,13 +33,16 @@
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@@ -50,6 +56,7 @@
 * @since 2024-12-16 06:10:52
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DeviceMaintenancePlanServiceImpl extends ServiceImpl<DeviceMaintenancePlanMapper, DeviceMaintenancePlan> implements DeviceMaintenancePlanService {
    @Resource
@@ -62,6 +69,8 @@
    private InformationNotificationService informationNotificationService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DeviceMapper deviceMapper;
    /**
     * 分页查询设备保养计划
@@ -288,5 +297,157 @@
        return Result.success();
    }
    /**
     * 按年份查询设备保养计划(不存在则自动创建),并自动补全已到点的设备明细
     */
    @Override
    public DeviceMaintenancePlanDto getOrCreatePlanByYear(String planYear) {
        if (StringUtils.isBlank(planYear)) {
            planYear = String.valueOf(LocalDate.now().getYear());
        }
        DeviceMaintenancePlan plan = getOrCreatePlanEntity(planYear);
        ensureMaintenanceDetails(plan);
        DeviceMaintenancePlanDto planDto = new DeviceMaintenancePlanDto();
        BeanUtils.copyProperties(plan, planDto);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        planDto.setDatePreparationStr(plan.getDatePreparation() == null ? null : plan.getDatePreparation().format(formatter));
        planDto.setAuditDateStr(plan.getAuditDate() == null ? null : plan.getAuditDate().format(formatter));
        if (StringUtils.isBlank(planDto.getCompiler()) && plan.getCompilerId() != null) {
            User compiler = userMapper.selectById(plan.getCompilerId());
            if (compiler != null) {
                planDto.setCompiler(compiler.getName());
            }
        }
        if (StringUtils.isBlank(planDto.getAudit()) && plan.getAuditId() != null) {
            User audit = userMapper.selectById(plan.getAuditId());
            if (audit != null) {
                planDto.setAudit(audit.getName());
            }
        }
        return planDto;
    }
    /**
     * 设备档案变更后,把已到点的设备同步到对应年份的设备保养计划明细
     */
    @Override
    public void syncDeviceMaintenanceDetail(Device device) {
        LocalDate remind = calcMaintenanceRemindDate(device);
        if (remind == null || remind.isAfter(LocalDate.now())) {
            return;
        }
        DeviceMaintenancePlan plan = getOrCreatePlanEntity(String.valueOf(remind.getYear()));
        fillMaintenanceDetailIfAbsent(plan, device);
    }
    /**
     * 保养记录保存后,滚动设备档案的最近/下次保养日期
     */
    @Override
    public void updateDeviceMaintenanceDate(Integer deviceId, LocalDate maintenanceDate) {
        if (deviceId == null || maintenanceDate == null) {
            return;
        }
        Device device = deviceMapper.selectById(deviceId);
        if (device == null) {
            return;
        }
        LocalDateTime recordDate = maintenanceDate.atStartOfDay();
        // 只允许向前滚动,避免补录旧记录时把日期改回去
        if (device.getLastMaintenanceDate() != null && !recordDate.isAfter(device.getLastMaintenanceDate())) {
            return;
        }
        device.setLastMaintenanceDate(recordDate);
        if (StringUtils.isNotBlank(device.getMaintenanceCycle())) {
            try {
                device.setNextMaintenanceDate(recordDate.plusMonths(Long.parseLong(device.getMaintenanceCycle().trim())));
            } catch (NumberFormatException ignored) {
            }
        }
        deviceMapper.updateById(device);
    }
    /**
     * 分页查询设备保养计划详情(含到点标红)
     */
    @Override
    public IPage<DeviceMaintenancePlanDetailsDto> pageMaintenancePlanDetail(Page page, Integer planId, String deviceName, String deviceNumber) {
        if (planId == null) {
            return new Page<>();
        }
        return deviceMaintenancePlanDetailsMapper.pageMaintenancePlanDetail(page, planId, deviceName, deviceNumber);
    }
    /**
     * 查询年份对应的计划,不存在则创建
     */
    private DeviceMaintenancePlan getOrCreatePlanEntity(String planYear) {
        DeviceMaintenancePlan plan = baseMapper.selectOne(Wrappers.<DeviceMaintenancePlan>lambdaQuery()
                .eq(DeviceMaintenancePlan::getPlanYear, planYear)
                .orderByDesc(DeviceMaintenancePlan::getMaintenancePlanId)
                .last("limit 1"));
        if (plan == null) {
            plan = new DeviceMaintenancePlan();
            plan.setPlanName(planYear + "年度设备保养计划");
            plan.setPlanYear(planYear);
            Integer userId = SecurityUtils.getUserId().intValue();
            User user = userMapper.selectById(userId);
            plan.setCompilerId(userId);
            plan.setCompiler(user == null ? null : user.getName());
            plan.setDatePreparation(LocalDateTime.now());
            baseMapper.insert(plan);
        }
        return plan;
    }
    /**
     * 扫描所有设备,把已到点(下次保养日期前1个月起,含已过期)的设备补进本年度计划
     */
    private void ensureMaintenanceDetails(DeviceMaintenancePlan plan) {
        List<Device> devices = deviceMapper.selectList(null);
        for (Device device : devices) {
            fillMaintenanceDetailIfAbsent(plan, device);
        }
    }
    /**
     * 设备提醒起点:下次保养日期前1个月(没填下次日期时用最近日期+保养周期兜底)
     */
    private LocalDate calcMaintenanceRemindDate(Device device) {
        LocalDateTime base = device.getNextMaintenanceDate();
        if (base == null && device.getLastMaintenanceDate() != null && StringUtils.isNotBlank(device.getMaintenanceCycle())) {
            try {
                base = device.getLastMaintenanceDate().plusMonths(Long.parseLong(device.getMaintenanceCycle().trim()));
            } catch (NumberFormatException ignored) {
            }
        }
        return base == null ? null : base.toLocalDate().minusMonths(1);
    }
    /**
     * 已到点且计划中尚无该设备本轮明细时自动补一条
     */
    private void fillMaintenanceDetailIfAbsent(DeviceMaintenancePlan plan, Device device) {
        LocalDate remind = calcMaintenanceRemindDate(device);
        if (remind == null || remind.isAfter(LocalDate.now())) {
            return;
        }
        if (plan.getPlanYear() != null && String.valueOf(remind.getYear()).compareTo(plan.getPlanYear()) > 0) {
            return;
        }
        Long exist = deviceMaintenancePlanDetailsMapper.selectCount(Wrappers.<DeviceMaintenancePlanDetails>lambdaQuery()
                .eq(DeviceMaintenancePlanDetails::getMaintenancePlanId, plan.getMaintenancePlanId())
                .eq(DeviceMaintenancePlanDetails::getDeviceId, device.getId())
                .eq(DeviceMaintenancePlanDetails::getMaintenanceDate, remind));
        if (exist != null && exist > 0) {
            return;
        }
        DeviceMaintenancePlanDetails detail = new DeviceMaintenancePlanDetails();
        detail.setMaintenancePlanId(plan.getMaintenancePlanId());
        detail.setDeviceId(device.getId());
        detail.setMaintenanceIntervals(device.getMaintenanceCycle());
        detail.setMaintenanceDate(remind);
        deviceMaintenancePlanDetailsMapper.insert(detail);
    }
}