|
|
@@ -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);
|