refactor: 重构学生评价树的新增/更新节点,自构建 path

11111
winter 12 months ago
parent 70ffad27a3
commit 8bc0da460b

@ -20,7 +20,6 @@ public class InsertNodeDto {
@ApiModelProperty(value = "父亲节点,不传则为根节点", required = true) @ApiModelProperty(value = "父亲节点,不传则为根节点", required = true)
@NotBlank(message = "name 不能为空") @NotBlank(message = "name 不能为空")
String name; String name;
String[] path;
String logo; String logo;
Integer order = 0; Integer order = 0;
Integer score = 0; Integer score = 0;

@ -170,7 +170,7 @@ public class DutyServiceImpl implements DutyService {
.findFirst() .findFirst()
.orElseThrow(() -> new ServiceException("待更新的节点不存在")); .orElseThrow(() -> new ServiceException("待更新的节点不存在"));
String originNodeName = nodeToUpdate.getName(); String originNodeName = nodeToUpdate.getName();
// 更新当前节点 // 通用更新节点字段
nodeToUpdate.setName(newNodeName); nodeToUpdate.setName(newNodeName);
nodeToUpdate.setOrder(updateDutyNodeDto.getOrder()); nodeToUpdate.setOrder(updateDutyNodeDto.getOrder());

@ -7,7 +7,6 @@ import cn.teammodel.common.FiveEducations;
import cn.teammodel.common.IdRequest; import cn.teammodel.common.IdRequest;
import cn.teammodel.common.PK; import cn.teammodel.common.PK;
import cn.teammodel.config.exception.ServiceException; import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.repository.*;
import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.dto.Appraise.*;
import cn.teammodel.model.entity.User; import cn.teammodel.model.entity.User;
import cn.teammodel.model.entity.appraise.*; 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.entity.school.Student;
import cn.teammodel.model.vo.appraise.AppraiseRecordVo; import cn.teammodel.model.vo.appraise.AppraiseRecordVo;
import cn.teammodel.model.vo.appraise.StudentReportVo; import cn.teammodel.model.vo.appraise.StudentReportVo;
import cn.teammodel.repository.*;
import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.EvaluationService; import cn.teammodel.service.EvaluationService;
import cn.teammodel.utils.RepositoryUtil; 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 com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -165,24 +164,57 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override @Override
public Appraise insertNode(InsertNodeDto insertNodeDto) { 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(); User loginUser = SecurityUtil.getLoginUser();
List<AppraiseTreeNode> originNodes = appraise.getNodes(); List<AppraiseTreeNode> originNodes = appraise.getNodes();
// 拷贝数据到新节点
AppraiseTreeNode newNode = new AppraiseTreeNode();
BeanUtils.copyProperties(insertNodeDto, newNode);
// todo: build path AppraiseTreeNode newNode = new AppraiseTreeNode();
String newNodePid = newNode.getPid();
// 根节点直接添加 // 根节点直接添加
if (StringUtils.isNotEmpty(newNodePid)) { if (StringUtils.isNotEmpty(pid)) {
boolean invalid = originNodes.stream().noneMatch(item -> newNodePid.equals(item.getId())); boolean invalid = originNodes.stream().noneMatch(item -> pid.equals(item.getId()));
if (invalid) { if (invalid) {
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "父节点不存在"); 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.setId(UUID.randomUUID().toString());
newNode.setName(name);
newNode.setOrder(order);
newNode.setCreatorId(loginUser.getId()); newNode.setCreatorId(loginUser.getId());
newNode.setCreator(loginUser.getName()); newNode.setCreator(loginUser.getName());
newNode.setCreateTime(Instant.now().toEpochMilli()); newNode.setCreateTime(Instant.now().toEpochMilli());
@ -194,6 +226,7 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override @Override
public Appraise updateNode(UpdateNodeDto updateNodeDto) { public Appraise updateNode(UpdateNodeDto updateNodeDto) {
String updateNodeId = updateNodeDto.getId(); String updateNodeId = updateNodeDto.getId();
String newNodeName = updateNodeDto.getName();
Appraise appraise = findAppraise(updateNodeDto.getPeriodId()); Appraise appraise = findAppraise(updateNodeDto.getPeriodId());
List<AppraiseTreeNode> originNodes = appraise.getNodes(); List<AppraiseTreeNode> originNodes = appraise.getNodes();
@ -202,12 +235,30 @@ public class EvaluationServiceImpl implements EvaluationService {
.filter(item -> updateNodeId.equals(item.getId())) .filter(item -> updateNodeId.equals(item.getId()))
.findFirst() .findFirst()
.orElseThrow(() -> new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "更新节点不存在")); .orElseThrow(() -> new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "更新节点不存在"));
// todo: 如果是一级,二级需同步更新三级节点的 path String originNodeName = updateNode.getName();
updateNode.setName(updateNodeDto.getName()); updateNode.setName(newNodeName);
updateNode.setPath(updateNodeDto.getPath());
updateNode.setLogo(updateNodeDto.getLogo());
updateNode.setOrder(updateNodeDto.getOrder()); updateNode.setOrder(updateNodeDto.getOrder());
// 一级/二级
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.setPraise(updateNodeDto.isPraise());
updateNode.getPath()[2] = newNodeName;
}
return buildTree(appraiseRepository.save(appraise)); return buildTree(appraiseRepository.save(appraise));
} }

Loading…
Cancel
Save