Просмотр исходного кода

修复一键艾灸重启后,穴位仍然找旧穴位的bug

jiapu 3 месяцев назад
Родитель
Сommit
48dd03d665

+ 150 - 19
code/backend/src/main/java/com/aijiuyi/admin/common/config/PlanDataInitializer.java

@@ -1,7 +1,9 @@
 package com.aijiuyi.admin.common.config;
 
+import com.aijiuyi.admin.entity.Acupoint;
 import com.aijiuyi.admin.entity.Plan;
 import com.aijiuyi.admin.entity.PlanStep;
+import com.aijiuyi.admin.mapper.AcupointMapper;
 import com.aijiuyi.admin.mapper.PlanMapper;
 import com.aijiuyi.admin.mapper.PlanStepMapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -9,6 +11,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
 
 import java.math.BigDecimal;
 import java.util.ArrayList;
@@ -27,10 +30,14 @@ public class PlanDataInitializer implements CommandLineRunner {
     @Autowired
     private PlanStepMapper planStepMapper;
 
+    @Autowired
+    private AcupointMapper acupointMapper;
+
     @Override
     public void run(String... args) {
         List<PlanSeed> seeds = buildSeeds();
         for (PlanSeed seed : seeds) {
+            boolean created = false;
             Plan plan = planMapper.selectOne(new LambdaQueryWrapper<Plan>()
                     .eq(Plan::getPlanCode, seed.planCode)
                     .last("limit 1"));
@@ -46,32 +53,156 @@ public class PlanDataInitializer implements CommandLineRunner {
                 plan.setUseCount(0);
                 fillPlan(plan, seed);
                 planMapper.insert(plan);
+                created = true;
             } else {
-                fillPlan(plan, seed);
-                planMapper.updateById(plan);
+                if (fillMissingPlanFields(plan, seed)) {
+                    planMapper.updateById(plan);
+                }
             }
 
-            planStepMapper.delete(new LambdaUpdateWrapper<PlanStep>()
-                    .eq(PlanStep::getPlanId, plan.getId()));
-
-            for (int i = 0; i < seed.steps.size(); i++) {
-                StepSeed stepSeed = seed.steps.get(i);
-                PlanStep step = new PlanStep();
-                step.setPlanId(plan.getId());
-                step.setStepOrder(i + 1);
-                step.setAcupointId(stepSeed.acupointId);
-                step.setAcupointName(stepSeed.acupointName);
-                step.setSide(stepSeed.side);
-                step.setTemperature(stepSeed.temperature);
-                step.setDuration(stepSeed.duration);
-                step.setTechniqueId(stepSeed.techniqueId);
-                step.setTechniqueName(stepSeed.techniqueName);
-                step.setRemark(stepSeed.remark);
-                planStepMapper.insert(step);
+            if (created || hasNoActiveSteps(plan.getId())) {
+                replaceStepsFromSeed(plan.getId(), seed);
+            } else {
+                repairDeletedAcupointReferences(plan.getId());
             }
         }
     }
 
+    private boolean hasNoActiveSteps(Long planId) {
+        Long count = planStepMapper.selectCount(new LambdaQueryWrapper<PlanStep>()
+                .eq(PlanStep::getPlanId, planId));
+        return count == null || count == 0;
+    }
+
+    private void replaceStepsFromSeed(Long planId, PlanSeed seed) {
+        planStepMapper.delete(new LambdaUpdateWrapper<PlanStep>()
+                .eq(PlanStep::getPlanId, planId));
+
+        for (int i = 0; i < seed.steps.size(); i++) {
+            StepSeed stepSeed = seed.steps.get(i);
+            Acupoint acupoint = resolveActiveAcupoint(stepSeed.acupointId, stepSeed.acupointName);
+            PlanStep step = new PlanStep();
+            step.setPlanId(planId);
+            step.setStepOrder(i + 1);
+            step.setAcupointId(acupoint != null ? acupoint.getId() : stepSeed.acupointId);
+            step.setAcupointName(acupoint != null ? acupoint.getName() : stepSeed.acupointName);
+            step.setSide(stepSeed.side);
+            step.setTemperature(stepSeed.temperature);
+            step.setDuration(stepSeed.duration);
+            step.setTechniqueId(stepSeed.techniqueId);
+            step.setTechniqueName(stepSeed.techniqueName);
+            step.setRemark(stepSeed.remark);
+            planStepMapper.insert(step);
+        }
+    }
+
+    private void repairDeletedAcupointReferences(Long planId) {
+        List<PlanStep> steps = planStepMapper.selectList(new LambdaQueryWrapper<PlanStep>()
+                .eq(PlanStep::getPlanId, planId));
+        for (PlanStep step : steps) {
+            Acupoint acupoint = findActiveById(step.getAcupointId());
+            if (acupoint != null) {
+                continue;
+            }
+            Acupoint replacement = resolveActiveAcupoint(null, step.getAcupointName());
+            if (replacement == null) {
+                continue;
+            }
+            PlanStep update = new PlanStep();
+            update.setId(step.getId());
+            update.setAcupointId(replacement.getId());
+            update.setAcupointName(replacement.getName());
+            planStepMapper.updateById(update);
+        }
+    }
+
+    private Acupoint resolveActiveAcupoint(Long acupointId, String acupointName) {
+        if (StringUtils.hasText(acupointName)) {
+            Acupoint byName = findActiveByName(acupointName);
+            if (byName != null) {
+                return byName;
+            }
+        }
+        return findActiveById(acupointId);
+    }
+
+    private Acupoint findActiveByName(String acupointName) {
+        String name = acupointName.trim();
+        Acupoint exact = selectActiveByName(name);
+        if (exact != null) {
+            return exact;
+        }
+        if (name.endsWith("\u7a74") && name.length() > 1) {
+            return selectActiveByName(name.substring(0, name.length() - 1));
+        }
+        return selectActiveByName(name + "\u7a74");
+    }
+
+    private Acupoint selectActiveByName(String acupointName) {
+        return acupointMapper.selectOne(new LambdaQueryWrapper<Acupoint>()
+                .eq(Acupoint::getName, acupointName)
+                .eq(Acupoint::getStatus, 1)
+                .orderByDesc(Acupoint::getUpdateTime)
+                .orderByDesc(Acupoint::getId)
+                .last("limit 1"));
+    }
+
+    private Acupoint findActiveById(Long acupointId) {
+        if (acupointId == null) {
+            return null;
+        }
+        Acupoint acupoint = acupointMapper.selectById(acupointId);
+        if (acupoint == null || !Integer.valueOf(1).equals(acupoint.getStatus())) {
+            return null;
+        }
+        return acupoint;
+    }
+
+    private boolean fillMissingPlanFields(Plan plan, PlanSeed seed) {
+        boolean changed = false;
+        if (!StringUtils.hasText(plan.getName())) {
+            plan.setName(seed.name);
+            changed = true;
+        }
+        if (plan.getModeType() == null) {
+            plan.setModeType(seed.modeType);
+            changed = true;
+        }
+        if (!StringUtils.hasText(plan.getEffectType()) && StringUtils.hasText(seed.effectType)) {
+            plan.setEffectType(seed.effectType);
+            changed = true;
+        }
+        if (plan.getApplicableGender() == null) {
+            plan.setApplicableGender(0);
+            changed = true;
+        }
+        if (plan.getAgeMin() == null) {
+            plan.setAgeMin(1);
+            changed = true;
+        }
+        if (plan.getAgeMax() == null) {
+            plan.setAgeMax(120);
+            changed = true;
+        }
+        if (plan.getAuthorType() == null) {
+            plan.setAuthorType(1);
+            changed = true;
+        }
+        if (!StringUtils.hasText(plan.getAuthorName())) {
+            plan.setAuthorName("ADMIN");
+            changed = true;
+        }
+        if (plan.getStatus() == null) {
+            plan.setStatus(1);
+            changed = true;
+        }
+        if (plan.getUseCount() == null) {
+            plan.setUseCount(0);
+            changed = true;
+        }
+        return changed;
+    }
+
     private void fillPlan(Plan plan, PlanSeed seed) {
         plan.setName(seed.name);
         plan.setModeType(seed.modeType);

+ 19 - 0
code/backend/src/main/java/com/aijiuyi/admin/service/impl/PlanServiceImpl.java

@@ -6,10 +6,12 @@ import com.aijiuyi.admin.common.exception.BusinessException;
 import com.aijiuyi.admin.common.util.LogUtil;
 import com.aijiuyi.admin.controller.dto.PlanQueryDTO;
 import com.aijiuyi.admin.controller.dto.PlanSaveDTO;
+import com.aijiuyi.admin.entity.Acupoint;
 import com.aijiuyi.admin.entity.AppUser;
 import com.aijiuyi.admin.entity.Plan;
 import com.aijiuyi.admin.entity.PlanStep;
 import com.aijiuyi.admin.entity.UserPlan;
+import com.aijiuyi.admin.mapper.AcupointMapper;
 import com.aijiuyi.admin.mapper.PlanMapper;
 import com.aijiuyi.admin.mapper.PlanStepMapper;
 import com.aijiuyi.admin.mapper.UserPlanMapper;
@@ -46,6 +48,9 @@ public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements Pl
     @Autowired
     private PlanStepMapper planStepMapper;
 
+    @Autowired
+    private AcupointMapper acupointMapper;
+
     @Autowired
     private UserPlanMapper userPlanMapper;
 
@@ -419,13 +424,27 @@ public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements Pl
     private void saveSteps(Long planId, List<PlanStep> steps) {
         for (int i = 0; i < steps.size(); i++) {
             PlanStep step = steps.get(i);
+            Acupoint acupoint = getActiveAcupoint(step.getAcupointId());
             step.setId(null);
             step.setPlanId(planId);
             step.setStepOrder(i + 1);
+            step.setAcupointId(acupoint.getId());
+            step.setAcupointName(acupoint.getName());
             planStepMapper.insert(step);
         }
     }
 
+    private Acupoint getActiveAcupoint(Long acupointId) {
+        if (acupointId == null) {
+            throw new BusinessException(ResultCode.ACUPOINT_NOT_FOUND);
+        }
+        Acupoint acupoint = acupointMapper.selectById(acupointId);
+        if (acupoint == null || !Integer.valueOf(1).equals(acupoint.getStatus())) {
+            throw new BusinessException(ResultCode.ACUPOINT_NOT_FOUND);
+        }
+        return acupoint;
+    }
+
     private void hideMasterUserPlans(Plan plan) {
         if (plan == null || !Integer.valueOf(MODE_TYPE_MASTER).equals(plan.getModeType())) {
             return;