|
|
@@ -8,11 +8,13 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import com.ruoyi.project.reliability.domain.TRelCompo;
|
|
|
import com.ruoyi.project.reliability.domain.TRelDevice;
|
|
|
+import com.ruoyi.project.reliability.domain.TRelMaintMemo;
|
|
|
import com.ruoyi.project.reliability.domain.TRelMaintPlan;
|
|
|
import com.ruoyi.project.reliability.domain.TRelMaintRecord;
|
|
|
import com.ruoyi.project.reliability.service.IMaintPlanGeneratorService;
|
|
|
import com.ruoyi.project.reliability.service.ITRelCompoService;
|
|
|
import com.ruoyi.project.reliability.service.ITRelDeviceService;
|
|
|
+import com.ruoyi.project.reliability.service.ITRelMaintMemoService;
|
|
|
import com.ruoyi.project.reliability.service.ITRelMaintPlanService;
|
|
|
import com.ruoyi.project.reliability.service.ITRelMaintRecordService;
|
|
|
import com.ruoyi.project.reliability.utils.MaintFrequencyUtils;
|
|
|
@@ -51,11 +53,17 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
@Autowired
|
|
|
private ITRelMaintRecordService maintRecordService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ITRelMaintMemoService maintMemoService;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public int generateAllMaintPlans(int monthsAhead, int mergeThresholdDays) {
|
|
|
logger.info("开始生成所有设备的维修计划,提前{}个月,合并阈值{}天", monthsAhead, mergeThresholdDays);
|
|
|
|
|
|
+ // 为避免重复生成:先清理历史“计划中/未开始(9)”的旧计划及其下的旧记录(逻辑删除)
|
|
|
+ cleanupOldPlannedPlans(null);
|
|
|
+
|
|
|
// 获取所有设备
|
|
|
List<TRelDevice> devices = deviceService.selectTRelDeviceList(new TRelDevice());
|
|
|
if (devices == null || devices.isEmpty()) {
|
|
|
@@ -80,6 +88,9 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+ // 按设备生成时同样需要先清理该设备历史“计划中/未开始(9)”的旧计划与记录,避免重复堆积
|
|
|
+ cleanupOldPlannedPlans(devTag);
|
|
|
+
|
|
|
// 1. 获取设备信息
|
|
|
TRelDevice deviceQuery = new TRelDevice();
|
|
|
deviceQuery.setDevTag(devTag);
|
|
|
@@ -110,6 +121,19 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
// Key: 年月(如 "2025-06"), Value: 该月需要维护的部件任务列表
|
|
|
Map<String, List<CompoMaintTask>> monthlyTasks = new TreeMap<>();
|
|
|
|
|
|
+ // 备忘录纳入规则:若某部件存在备忘录(t_rel_maint_memo),也需要强制纳入本次生成
|
|
|
+ // 将备忘录按 compoId 分组,后续在遍历部件时追加“备忘录任务”到 monthlyTasks
|
|
|
+ List<TRelMaintMemo> memoList = getMemoListForDevice(devTag);
|
|
|
+ Map<String, List<TRelMaintMemo>> memoMap = new HashMap<>();
|
|
|
+ if (memoList != null && !memoList.isEmpty()) {
|
|
|
+ for (TRelMaintMemo memo : memoList) {
|
|
|
+ if (memo == null || memo.getCompoId() == null || memo.getCompoId().trim().isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ memoMap.computeIfAbsent(memo.getCompoId().trim(), k -> new ArrayList<>()).add(memo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
for (TRelCompo compo : compoList) {
|
|
|
CompoMaintTask task = calculateOptimalTask(compo, now, endDate, mergeThresholdDays);
|
|
|
if (task != null) {
|
|
|
@@ -117,6 +141,21 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
String monthKey = getMonthKey(task.maintDate);
|
|
|
monthlyTasks.computeIfAbsent(monthKey, k -> new ArrayList<>()).add(task);
|
|
|
}
|
|
|
+
|
|
|
+ if (compo != null && compo.getCompoId() != null) {
|
|
|
+ List<TRelMaintMemo> compoMemoList = memoMap.get(compo.getCompoId().toString());
|
|
|
+ if (compoMemoList != null && !compoMemoList.isEmpty()) {
|
|
|
+ // 将同一部件的备忘录逐条转换为“备忘录任务”并纳入生成
|
|
|
+ // 日期优先使用 memoTime;若 memoTime 早于当前日期,则按当前日期落计划
|
|
|
+ for (TRelMaintMemo memo : compoMemoList) {
|
|
|
+ CompoMaintTask memoTask = buildTaskFromMemo(compo, memo, now, endDate);
|
|
|
+ if (memoTask != null) {
|
|
|
+ String monthKey = getMonthKey(memoTask.maintDate);
|
|
|
+ monthlyTasks.computeIfAbsent(monthKey, k -> new ArrayList<>()).add(memoTask);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (monthlyTasks.isEmpty()) {
|
|
|
@@ -142,7 +181,18 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
// 不设置计划结束时间
|
|
|
plan.setApprovalStatus(STATUS_PLANNED); // 9-未开始
|
|
|
plan.setCompletionStatus(STATUS_PLANNED); // 9-计划中
|
|
|
- plan.setRemarks("自动生成的" + monthKey + "维修计划");
|
|
|
+
|
|
|
+ // 若该月计划中包含“备忘录任务”,在计划备注中追加来源标识,便于客户识别
|
|
|
+ boolean hasMemoTask = false;
|
|
|
+ if (tasks != null && !tasks.isEmpty()) {
|
|
|
+ for (CompoMaintTask t : tasks) {
|
|
|
+ if (t != null && t.fromMemo) {
|
|
|
+ hasMemoTask = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ plan.setRemarks(hasMemoTask ? ("自动生成的" + monthKey + "维修计划(从备忘录添加)") : ("自动生成的" + monthKey + "维修计划"));
|
|
|
|
|
|
maintPlanService.insertTRelMaintPlan(plan);
|
|
|
Long planId = plan.getPlanId();
|
|
|
@@ -160,6 +210,98 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
return planCount;
|
|
|
}
|
|
|
|
|
|
+ private int cleanupOldPlannedPlans(String devTag) {
|
|
|
+ // 清理范围:只清理“计划中/未开始(9)”的自动计划数据
|
|
|
+ // 原则:先删记录再删计划,避免残留关联数据
|
|
|
+ TRelMaintPlan planQuery = new TRelMaintPlan();
|
|
|
+ planQuery.setApprovalStatus(STATUS_PLANNED);
|
|
|
+ planQuery.setCompletionStatus(STATUS_PLANNED);
|
|
|
+ if (devTag != null && !devTag.trim().isEmpty()) {
|
|
|
+ planQuery.setDevTag(devTag);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<TRelMaintPlan> oldPlanList = maintPlanService.selectTRelMaintPlanList(planQuery);
|
|
|
+ if (oldPlanList == null || oldPlanList.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (TRelMaintPlan oldPlan : oldPlanList) {
|
|
|
+ if (oldPlan == null || oldPlan.getPlanId() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Long planId = oldPlan.getPlanId();
|
|
|
+
|
|
|
+ List<TRelMaintRecord> recordList = maintRecordService.selectTRelMaintRecordByPlanId(planId);
|
|
|
+ if (recordList != null && !recordList.isEmpty()) {
|
|
|
+ for (TRelMaintRecord record : recordList) {
|
|
|
+ if (record != null && record.getRecordId() != null) {
|
|
|
+ maintRecordService.deleteTRelMaintRecordById(record.getRecordId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ maintPlanService.deleteTRelMaintPlanById(planId);
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("已清理旧的计划中数据,devTag={}, planCount={}", devTag, oldPlanList.size());
|
|
|
+ return oldPlanList.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<TRelMaintMemo> getMemoListForDevice(String devTag) {
|
|
|
+ // 备忘录按设备查询(devTag),再在内存中按 compoId 分组
|
|
|
+ if (devTag == null || devTag.trim().isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ TRelMaintMemo memoQuery = new TRelMaintMemo();
|
|
|
+ memoQuery.setDevTag(devTag);
|
|
|
+ return maintMemoService.selectTRelMaintMemoList(memoQuery);
|
|
|
+ }
|
|
|
+
|
|
|
+ private CompoMaintTask buildTaskFromMemo(TRelCompo compo, TRelMaintMemo memo, Date now, Date endDate) {
|
|
|
+ // 将备忘录转成可参与生成的任务:使用 memoTime 作为任务日期(为空则使用当前日期)
|
|
|
+ if (compo == null || memo == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Date memoTime = memo.getMemoTime();
|
|
|
+ if (memoTime != null && memoTime.after(endDate)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String maintType = memo.getMaintType();
|
|
|
+ if (maintType == null || maintType.trim().isEmpty()) {
|
|
|
+ // 兜底:防止客户未填维修类型导致生成记录 maintType 为空
|
|
|
+ maintType = "2";
|
|
|
+ }
|
|
|
+ String maintContent = memo.getMaintContent();
|
|
|
+ if (maintContent == null || maintContent.trim().isEmpty()) {
|
|
|
+ maintContent = memo.getMemoReason();
|
|
|
+ }
|
|
|
+ String responsible = memo.getResponsible();
|
|
|
+ if (responsible == null || responsible.trim().isEmpty()) {
|
|
|
+ if ("1".equals(maintType)) {
|
|
|
+ responsible = compo.getInspector();
|
|
|
+ } else {
|
|
|
+ responsible = compo.getFixer();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Date taskDate = memoTime != null ? memoTime : now;
|
|
|
+ // 若备忘录时间早于当前日期,为避免计划开始时间落在过去,这里按当前日期落计划
|
|
|
+ if (taskDate.before(now)) {
|
|
|
+ taskDate = now;
|
|
|
+ }
|
|
|
+
|
|
|
+ CompoMaintTask task = new CompoMaintTask();
|
|
|
+ task.compo = compo;
|
|
|
+ task.maintType = maintType;
|
|
|
+ task.maintDate = taskDate;
|
|
|
+ task.maintContent = maintContent;
|
|
|
+ task.responsible = responsible;
|
|
|
+ task.fromMemo = true;
|
|
|
+ return task;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 计算部件的最优维护任务
|
|
|
*
|
|
|
@@ -373,6 +515,11 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
record.setResponsible(task.responsible);
|
|
|
record.setRecordStatus(RECORD_STATUS_PLANNED); // 9-计划中
|
|
|
|
|
|
+ if (task.fromMemo) {
|
|
|
+ // 记录备注用于在列表/导出中识别该条记录来源于备忘录
|
|
|
+ record.setRemarks("从备忘录添加");
|
|
|
+ }
|
|
|
+
|
|
|
// 根据维修类型设置检查人
|
|
|
if ("1".equals(task.maintType)) {
|
|
|
record.setInspector(task.compo.getInspector());
|
|
|
@@ -391,5 +538,6 @@ public class MaintPlanGeneratorServiceImpl implements IMaintPlanGeneratorService
|
|
|
Date maintDate;
|
|
|
String maintContent;
|
|
|
String responsible;
|
|
|
+ boolean fromMemo;
|
|
|
}
|
|
|
}
|