/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.sxkj.fw.device.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import lombok.AllArgsConstructor; import org.springblade.core.mp.base.BaseServiceImpl; import org.springframework.stereotype.Service; import org.sxkj.common.utils.DateUtils; import org.sxkj.fw.device.dto.FwDeviceMaintainRecordDTO; import org.sxkj.fw.device.entity.FwDeviceMaintainPlanEntity; import org.sxkj.fw.device.entity.FwDeviceMaintainRecordEntity; import org.sxkj.fw.device.excel.FwDeviceMaintainRecordExcel; import org.sxkj.fw.device.mapper.FwDeviceMaintainRecordMapper; import org.sxkj.fw.device.service.IFwDeviceMaintainPlanService; import org.sxkj.fw.device.service.IFwDeviceMaintainRecordService; import org.sxkj.fw.device.vo.FwDeviceMaintainRecordVO; import java.time.DayOfWeek; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; /** * 设备维护记录表 服务实现类 * * @author lw * @since 2026-01-08 */ @Service @AllArgsConstructor public class FwDeviceMaintainRecordServiceImpl extends BaseServiceImpl implements IFwDeviceMaintainRecordService { private final IFwDeviceMaintainPlanService fwDeviceMaintainPlanService; @Override public FwDeviceMaintainRecordVO selectLastOne(Long planId) { return baseMapper.selectLastOne(planId); } @Override public IPage selectFwDeviceMaintainRecordPage(IPage page, FwDeviceMaintainRecordDTO fwDeviceMaintainRecord) { return page.setRecords(baseMapper.selectFwDeviceMaintainRecordPage(page, fwDeviceMaintainRecord)); } @Override public boolean saveOrUpdate(FwDeviceMaintainRecordEntity entity) { // 维护时间 Date maintainTime = entity.getMaintainTime(); // 关联查询维护计划,缺失则不允许保存记录 FwDeviceMaintainPlanEntity fwDeviceMaintainPlan = fwDeviceMaintainPlanService.getById(entity.getPlanId()); if (null == fwDeviceMaintainPlan) { return false; } // 验证维护时间是否符合计划周期 boolean isInCycle = validateMaintainTimeInCycle(maintainTime, fwDeviceMaintainPlan); // 在计划周期内才更新计划的维护状态 if (isInCycle) { updatePlanMaintainStatus(fwDeviceMaintainPlan, maintainTime); } return super.saveOrUpdate(entity); } @Override public List exportFwDeviceMaintainRecord(Wrapper queryWrapper) { List fwDeviceMaintainRecordList = baseMapper.exportFwDeviceMaintainRecord(queryWrapper); return fwDeviceMaintainRecordList; } // region 维修计划状态更新相关 /** * 验证维护时间是否在计划周期内 */ private boolean validateMaintainTimeInCycle(Date maintainTime, FwDeviceMaintainPlanEntity plan) { String planCycleType = plan.getPlanCycleType(); List planCycleValue = plan.getPlanCycleValue(); for (String ruleValue : planCycleValue) { boolean isValid = false; switch (planCycleType) { case "1": // 年度规则 isValid = validateAnnualCycle(maintainTime, ruleValue); break; case "2": // 月度规则 isValid = validateMonthlyCycle(maintainTime, ruleValue); break; case "3": // 周度规则 isValid = validateWeeklyCycle(maintainTime, ruleValue); break; } if (isValid) { return true; } } return false; } /** * 验证年度周期 */ private boolean validateAnnualCycle(Date maintainTime, String ruleValue) { String[] parts = ruleValue.split("月"); int ruleMonth = Integer.parseInt(parts[0]); int ruleDay = Integer.parseInt(parts[1].replace("号", "")); LocalDateTime[] cycleBounds = DateUtils.calculateAnnualCycle(new Date(), ruleMonth, ruleDay); return DateUtils.isMaintainTimeInCycle(maintainTime, cycleBounds[0], cycleBounds[1]); } /** * 验证月度周期 */ private boolean validateMonthlyCycle(Date maintainTime, String ruleValue) { int ruleDay = Integer.parseInt(ruleValue.replace("号", "")); LocalDateTime[] cycleBounds = DateUtils.calculateMonthlyCycle(new Date(), ruleDay); return DateUtils.isMaintainTimeInCycle(maintainTime, cycleBounds[0], cycleBounds[1]); } /** * 验证周度周期 */ private boolean validateWeeklyCycle(Date maintainTime, String ruleValue) { int ruleDayOfWeek = DateUtils.getDayOfWeekNumber(ruleValue); LocalDateTime[] cycleBounds = DateUtils.calculateWeeklyCycle(new Date(), ruleDayOfWeek); // String dateStart = cycleBounds[0].atZone(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); // String dateEnd = cycleBounds[1].atZone(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); // String dateMaintain = maintainTime.toInstant().atZone(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); // // log.debug("维护时间: " + dateMaintain + ", 周期开始: " + dateStart + ", 周期结束: " + dateEnd); return DateUtils.isMaintainTimeInCycle(maintainTime, cycleBounds[0], cycleBounds[1]); } /** * 更新计划维护状态 */ private void updatePlanMaintainStatus(FwDeviceMaintainPlanEntity plan, Date maintainTime) { FwDeviceMaintainPlanEntity updateEntity = new FwDeviceMaintainPlanEntity(); updateEntity.setId(plan.getId()); updateEntity.setMaintainStatus(1); // 维护状态 updateEntity.setLastMaintainTime(maintainTime); // 最后一次维护时间 fwDeviceMaintainPlanService.updateById(updateEntity); } // endregion 维修计划状态更新相关 }