From 4e48664942bbefe70699ca43d25cbbd56ecc8daa Mon Sep 17 00:00:00 2001
From: xiebin <vip_xiaobin810@163.com>
Date: Mon, 19 Jan 2026 15:17:12 +0800
Subject: [PATCH] update-维修计划状态更新、新增定时器修改状态

---
 drone-service/drone-fw/src/main/java/org/sxkj/fw/device/service/impl/FwDeviceMaintainRecordServiceImpl.java |  137 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 127 insertions(+), 10 deletions(-)

diff --git a/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/service/impl/FwDeviceMaintainRecordServiceImpl.java b/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/service/impl/FwDeviceMaintainRecordServiceImpl.java
index 7d69355..b01cf03 100644
--- a/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/service/impl/FwDeviceMaintainRecordServiceImpl.java
+++ b/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/service/impl/FwDeviceMaintainRecordServiceImpl.java
@@ -16,16 +16,26 @@
  */
 package org.sxkj.fw.device.service.impl;
 
-import org.sxkj.fw.device.dto.FwDeviceMaintainRecordDTO;
-import org.sxkj.fw.device.entity.FwDeviceMaintainRecordEntity;
-import org.sxkj.fw.device.vo.FwDeviceMaintainRecordVO;
-import org.sxkj.fw.device.excel.FwDeviceMaintainRecordExcel;
-import org.sxkj.fw.device.mapper.FwDeviceMaintainRecordMapper;
-import org.sxkj.fw.device.service.IFwDeviceMaintainRecordService;
-import org.springframework.stereotype.Service;
 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;
 
 /**
@@ -35,21 +45,128 @@
  * @since 2026-01-08
  */
 @Service
+@AllArgsConstructor
 public class FwDeviceMaintainRecordServiceImpl extends BaseServiceImpl<FwDeviceMaintainRecordMapper, FwDeviceMaintainRecordEntity> implements IFwDeviceMaintainRecordService {
+
+	private final IFwDeviceMaintainPlanService fwDeviceMaintainPlanService;
+
+	@Override
+	public FwDeviceMaintainRecordVO selectLastOne(Long planId) {
+		return baseMapper.selectLastOne(planId);
+	}
 
 	@Override
 	public IPage<FwDeviceMaintainRecordVO> selectFwDeviceMaintainRecordPage(IPage<FwDeviceMaintainRecordVO> 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<FwDeviceMaintainRecordExcel> exportFwDeviceMaintainRecord(Wrapper<FwDeviceMaintainRecordEntity> queryWrapper) {
 		List<FwDeviceMaintainRecordExcel> fwDeviceMaintainRecordList = baseMapper.exportFwDeviceMaintainRecord(queryWrapper);
-		//fwDeviceMaintainRecordList.forEach(fwDeviceMaintainRecord -> {
-		//	fwDeviceMaintainRecord.setTypeName(DictCache.getValue(DictEnum.YES_NO, FwDeviceMaintainRecord.getType()));
-		//});
 		return fwDeviceMaintainRecordList;
 	}
 
+	// region 维修计划状态更新相关
+
+	/**
+	 * 验证维护时间是否在计划周期内
+	 */
+	private boolean validateMaintainTimeInCycle(Date maintainTime, FwDeviceMaintainPlanEntity plan) {
+		String planCycleType = plan.getPlanCycleType();
+		List<String> 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 维修计划状态更新相关
+
 }

--
Gitblit v1.9.3