From 8bc0da460b2ca7708a1dd0b0293b9b318faf2cbd Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Tue, 16 Jan 2024 11:49:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E5=AD=A6?= =?UTF-8?q?=E7=94=9F=E8=AF=84=E4=BB=B7=E6=A0=91=E7=9A=84=E6=96=B0=E5=A2=9E?= =?UTF-8?q?/=E6=9B=B4=E6=96=B0=E8=8A=82=E7=82=B9,=E8=87=AA=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/dto/Appraise/InsertNodeDto.java | 1 - .../service/impl/DutyServiceImpl.java | 2 +- .../service/impl/EvaluationServiceImpl.java | 83 +++++++++++++++---- 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java index 258345b..1b1b94b 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java @@ -20,7 +20,6 @@ public class InsertNodeDto { @ApiModelProperty(value = "父亲节点,不传则为根节点", required = true) @NotBlank(message = "name 不能为空") String name; - String[] path; String logo; Integer order = 0; Integer score = 0; diff --git a/src/main/java/cn/teammodel/service/impl/DutyServiceImpl.java b/src/main/java/cn/teammodel/service/impl/DutyServiceImpl.java index 1799a15..384e14d 100644 --- a/src/main/java/cn/teammodel/service/impl/DutyServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/DutyServiceImpl.java @@ -170,7 +170,7 @@ public class DutyServiceImpl implements DutyService { .findFirst() .orElseThrow(() -> new ServiceException("待更新的节点不存在")); String originNodeName = nodeToUpdate.getName(); - // 更新当前节点 + // 通用更新节点字段 nodeToUpdate.setName(newNodeName); nodeToUpdate.setOrder(updateDutyNodeDto.getOrder()); diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index cabc2d6..27d245a 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -7,7 +7,6 @@ import cn.teammodel.common.FiveEducations; import cn.teammodel.common.IdRequest; import cn.teammodel.common.PK; import cn.teammodel.config.exception.ServiceException; -import cn.teammodel.repository.*; import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.entity.User; import cn.teammodel.model.entity.appraise.*; @@ -16,6 +15,7 @@ import cn.teammodel.model.entity.school.School; import cn.teammodel.model.entity.school.Student; import cn.teammodel.model.vo.appraise.AppraiseRecordVo; import cn.teammodel.model.vo.appraise.StudentReportVo; +import cn.teammodel.repository.*; import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.service.EvaluationService; import cn.teammodel.utils.RepositoryUtil; @@ -24,7 +24,6 @@ import com.azure.cosmos.models.CosmosPatchOperations; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.BeanUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; @@ -165,24 +164,57 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public Appraise insertNode(InsertNodeDto insertNodeDto) { - Appraise appraise = findAppraise(insertNodeDto.getPeriodId()); + String periodId = insertNodeDto.getPeriodId(); + String pid = insertNodeDto.getPid(); + String name = insertNodeDto.getName(); + String logo = insertNodeDto.getLogo(); + Integer order = insertNodeDto.getOrder(); + Integer score = insertNodeDto.getScore(); + Boolean isPraise = insertNodeDto.getIsPraise(); + + Appraise appraise = findAppraise(periodId); User loginUser = SecurityUtil.getLoginUser(); - List originNodes = appraise.getNodes(); - // 拷贝数据到新节点 - AppraiseTreeNode newNode = new AppraiseTreeNode(); - BeanUtils.copyProperties(insertNodeDto, newNode); - // todo: build path - String newNodePid = newNode.getPid(); + AppraiseTreeNode newNode = new AppraiseTreeNode(); // 根节点直接添加 - if (StringUtils.isNotEmpty(newNodePid)) { - boolean invalid = originNodes.stream().noneMatch(item -> newNodePid.equals(item.getId())); + if (StringUtils.isNotEmpty(pid)) { + boolean invalid = originNodes.stream().noneMatch(item -> pid.equals(item.getId())); if (invalid) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "父节点不存在"); } } + if (StringUtils.isNotEmpty(pid)) { + AppraiseTreeNode parent = originNodes.stream().filter(x -> pid.equals(x.getId())) + .findFirst() + .orElseThrow(() -> new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "pid 无效")); + String level2 = parent.getName(); + + // 新节点是三级节点(二级节点不用特殊处理 + String ppid = parent.getPid(); + if (StringUtils.isNotEmpty(ppid)) { + String[] path = new String[3]; + AppraiseTreeNode root = originNodes.stream().filter(x -> ppid.equals(x.getId())) + .findFirst() + .orElseThrow(() -> new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "值周树结构错误")); + // 层数不为三级,抛出异常 + if (StringUtils.isNotEmpty(root.getPid())) { + throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "值周树结构错误"); + } + String level1 = root.getName(); + path[0] = level1; + path[1] = level2; + path[2] = name; + newNode.setPath(path); + newNode.setPraise(isPraise); + newNode.setLogo(logo); + newNode.setScore(score); + } + } + newNode.setId(UUID.randomUUID().toString()); + newNode.setName(name); + newNode.setOrder(order); newNode.setCreatorId(loginUser.getId()); newNode.setCreator(loginUser.getName()); newNode.setCreateTime(Instant.now().toEpochMilli()); @@ -194,6 +226,7 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public Appraise updateNode(UpdateNodeDto updateNodeDto) { String updateNodeId = updateNodeDto.getId(); + String newNodeName = updateNodeDto.getName(); Appraise appraise = findAppraise(updateNodeDto.getPeriodId()); List originNodes = appraise.getNodes(); @@ -202,12 +235,30 @@ public class EvaluationServiceImpl implements EvaluationService { .filter(item -> updateNodeId.equals(item.getId())) .findFirst() .orElseThrow(() -> new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "更新节点不存在")); - // todo: 如果是一级,二级需同步更新三级节点的 path - updateNode.setName(updateNodeDto.getName()); - updateNode.setPath(updateNodeDto.getPath()); - updateNode.setLogo(updateNodeDto.getLogo()); + String originNodeName = updateNode.getName(); + updateNode.setName(newNodeName); updateNode.setOrder(updateNodeDto.getOrder()); - updateNode.setPraise(updateNodeDto.isPraise()); + + // 一级/二级 + if (updateNode.getPath() == null || updateNode.getPath().length == 0) { + int index = StringUtils.isEmpty(updateNode.getPid()) ? 0 : 1; + originNodes.forEach(x -> { + // 修改三级节点的 path + if (x.getPath() == null) { + return; + } + String[] path = x.getPath(); + if (originNodeName.equals(path[index])) { + path[index] = newNodeName; + x.setPath(path); + } + }); + } else { + // 三级 + updateNode.setLogo(updateNodeDto.getLogo()); + updateNode.setPraise(updateNodeDto.isPraise()); + updateNode.getPath()[2] = newNodeName; + } return buildTree(appraiseRepository.save(appraise)); }