|
|
|
@ -9,8 +9,8 @@ import cn.teammodel.model.dto.DeleteNodeDto;
|
|
|
|
|
import cn.teammodel.model.dto.GetEvaluateTreeDto;
|
|
|
|
|
import cn.teammodel.model.dto.InsertNodeDto;
|
|
|
|
|
import cn.teammodel.model.dto.UpdateNodeDto;
|
|
|
|
|
import cn.teammodel.model.entity.Evaluation;
|
|
|
|
|
import cn.teammodel.model.entity.EvaluationTreeNode;
|
|
|
|
|
import cn.teammodel.model.entity.Appraise;
|
|
|
|
|
import cn.teammodel.model.entity.AppraiseTreeNode;
|
|
|
|
|
import cn.teammodel.model.entity.User;
|
|
|
|
|
import cn.teammodel.security.utils.SecurityUtils;
|
|
|
|
|
import cn.teammodel.service.EvaluationService;
|
|
|
|
@ -40,56 +40,56 @@ public class EvaluationServiceImpl implements EvaluationService {
|
|
|
|
|
* 通用的获取 evaluation 的方法: 判断参数,判断数据是否为空 <br/>
|
|
|
|
|
* 从 token 中获取 schoolId
|
|
|
|
|
*/
|
|
|
|
|
private Evaluation findEvaluation(String periodId) {
|
|
|
|
|
private Appraise findEvaluation(String periodId) {
|
|
|
|
|
periodId = StringUtils.isEmpty(periodId) ? "default" : periodId;
|
|
|
|
|
User loginUser = SecurityUtils.getLoginUser();
|
|
|
|
|
String schoolId = loginUser.getSchoolId();
|
|
|
|
|
|
|
|
|
|
// 拿到要新增节点的原始数据
|
|
|
|
|
Evaluation evaluation = evaluationRepository.findBySchoolIdAndPeriodId(schoolId, periodId, PK.PK_EVALUATION);
|
|
|
|
|
if (evaluation == null) {
|
|
|
|
|
Appraise appraise = evaluationRepository.findBySchoolIdAndPeriodId(schoolId, periodId, PK.PK_EVALUATION);
|
|
|
|
|
if (appraise == null) {
|
|
|
|
|
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "学校评价数据不存在");
|
|
|
|
|
}
|
|
|
|
|
return evaluation;
|
|
|
|
|
return appraise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Evaluation getTree(GetEvaluateTreeDto getEvaluateTreeDto) {
|
|
|
|
|
Evaluation evaluation = findEvaluation(getEvaluateTreeDto.getPeriodId());
|
|
|
|
|
public Appraise getTree(GetEvaluateTreeDto getEvaluateTreeDto) {
|
|
|
|
|
Appraise appraise = findEvaluation(getEvaluateTreeDto.getPeriodId());
|
|
|
|
|
|
|
|
|
|
return this.buildTree(evaluation);
|
|
|
|
|
return this.buildTree(appraise);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Evaluation buildTree(Evaluation evaluation) {
|
|
|
|
|
if (evaluation == null) {
|
|
|
|
|
public Appraise buildTree(Appraise appraise) {
|
|
|
|
|
if (appraise == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<EvaluationTreeNode> nodes = evaluation.getNodes();
|
|
|
|
|
List<EvaluationTreeNode> parents = new ArrayList<>();
|
|
|
|
|
List<AppraiseTreeNode> nodes = appraise.getNodes();
|
|
|
|
|
List<AppraiseTreeNode> parents = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// pid 为 null 或者 "" 则为 parents
|
|
|
|
|
for (EvaluationTreeNode node : nodes) {
|
|
|
|
|
for (AppraiseTreeNode node : nodes) {
|
|
|
|
|
if (StringUtils.isBlank(node.getPid())) {
|
|
|
|
|
parents.add(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 迭代构建孩子节点
|
|
|
|
|
for (EvaluationTreeNode parent : parents) {
|
|
|
|
|
for (AppraiseTreeNode parent : parents) {
|
|
|
|
|
buildChildren(parent, nodes);
|
|
|
|
|
}
|
|
|
|
|
evaluation.setNodes(parents);
|
|
|
|
|
return evaluation;
|
|
|
|
|
appraise.setNodes(parents);
|
|
|
|
|
return appraise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void flattenTree(List<EvaluationTreeNode> trees, List<EvaluationTreeNode> nodes) {
|
|
|
|
|
public void flattenTree(List<AppraiseTreeNode> trees, List<AppraiseTreeNode> nodes) {
|
|
|
|
|
// 递归结束条件
|
|
|
|
|
if (ObjectUtils.isEmpty(trees)) return;
|
|
|
|
|
|
|
|
|
|
// 这里的每个 tree 都是组装好的树,区别于单个节点
|
|
|
|
|
for (EvaluationTreeNode tree : trees) {
|
|
|
|
|
for (AppraiseTreeNode tree : trees) {
|
|
|
|
|
// 递归
|
|
|
|
|
flattenTree(tree.getChildren(), nodes);
|
|
|
|
|
// 回溯时删除 children,添加进列表
|
|
|
|
@ -99,13 +99,13 @@ public class EvaluationServiceImpl implements EvaluationService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Evaluation insertNode(InsertNodeDto insertNodeDto) {
|
|
|
|
|
Evaluation evaluation = findEvaluation(insertNodeDto.getPeriodId());
|
|
|
|
|
public Appraise insertNode(InsertNodeDto insertNodeDto) {
|
|
|
|
|
Appraise appraise = findEvaluation(insertNodeDto.getPeriodId());
|
|
|
|
|
User loginUser = SecurityUtils.getLoginUser();
|
|
|
|
|
|
|
|
|
|
List<EvaluationTreeNode> originNodes = evaluation.getNodes();
|
|
|
|
|
List<AppraiseTreeNode> originNodes = appraise.getNodes();
|
|
|
|
|
// 拷贝数据到新节点
|
|
|
|
|
EvaluationTreeNode newNode = new EvaluationTreeNode();
|
|
|
|
|
AppraiseTreeNode newNode = new AppraiseTreeNode();
|
|
|
|
|
BeanUtils.copyProperties(insertNodeDto, newNode);
|
|
|
|
|
|
|
|
|
|
String newNodePid = newNode.getPid();
|
|
|
|
@ -123,17 +123,17 @@ public class EvaluationServiceImpl implements EvaluationService {
|
|
|
|
|
newNode.setCreateTime(LocalDateTime.now());
|
|
|
|
|
|
|
|
|
|
originNodes.add(newNode);
|
|
|
|
|
return buildTree(evaluationRepository.save(evaluation));
|
|
|
|
|
return buildTree(evaluationRepository.save(appraise));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Evaluation updateNode(UpdateNodeDto updateNodeDto) {
|
|
|
|
|
public Appraise updateNode(UpdateNodeDto updateNodeDto) {
|
|
|
|
|
String updateNodeId = updateNodeDto.getId();
|
|
|
|
|
|
|
|
|
|
Evaluation evaluation = findEvaluation(updateNodeDto.getPeriodId());
|
|
|
|
|
List<EvaluationTreeNode> originNodes = evaluation.getNodes();
|
|
|
|
|
Appraise appraise = findEvaluation(updateNodeDto.getPeriodId());
|
|
|
|
|
List<AppraiseTreeNode> originNodes = appraise.getNodes();
|
|
|
|
|
// 每个节点都有 id, 直接校验是否合法
|
|
|
|
|
EvaluationTreeNode updateNode = originNodes.stream()
|
|
|
|
|
AppraiseTreeNode updateNode = originNodes.stream()
|
|
|
|
|
.filter(item -> updateNodeId.equals(item.getId()))
|
|
|
|
|
.findFirst()
|
|
|
|
|
.orElseThrow(() -> new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "更新节点不存在"));
|
|
|
|
@ -144,18 +144,18 @@ public class EvaluationServiceImpl implements EvaluationService {
|
|
|
|
|
updateNode.setPraise(updateNodeDto.isPraise());
|
|
|
|
|
// todo: 为新节点赋值必须参数 (id, creator), 可不可以添加默认值 order ?
|
|
|
|
|
|
|
|
|
|
return buildTree(evaluationRepository.save(evaluation));
|
|
|
|
|
return buildTree(evaluationRepository.save(appraise));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Evaluation deleteNode(DeleteNodeDto deleteNodeDto) {
|
|
|
|
|
public Appraise deleteNode(DeleteNodeDto deleteNodeDto) {
|
|
|
|
|
// 删除指定节点,可能是(一级,二级,三级),设计一个通用的
|
|
|
|
|
Evaluation evaluation = findEvaluation(deleteNodeDto.getPeriodId());
|
|
|
|
|
List<EvaluationTreeNode> nodes = evaluation.getNodes();
|
|
|
|
|
List<EvaluationTreeNode> nodesToDelete = new ArrayList<>();
|
|
|
|
|
Appraise appraise = findEvaluation(deleteNodeDto.getPeriodId());
|
|
|
|
|
List<AppraiseTreeNode> nodes = appraise.getNodes();
|
|
|
|
|
List<AppraiseTreeNode> nodesToDelete = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// 迭代器安全删除
|
|
|
|
|
for (EvaluationTreeNode node : nodes) {
|
|
|
|
|
for (AppraiseTreeNode node : nodes) {
|
|
|
|
|
if (node.getId().equals(deleteNodeDto.getId())) {
|
|
|
|
|
// 删除当前节点
|
|
|
|
|
nodesToDelete.add(node);
|
|
|
|
@ -165,14 +165,14 @@ public class EvaluationServiceImpl implements EvaluationService {
|
|
|
|
|
}
|
|
|
|
|
nodes.removeAll(nodesToDelete);
|
|
|
|
|
|
|
|
|
|
return buildTree(evaluationRepository.save(evaluation));
|
|
|
|
|
return buildTree(evaluationRepository.save(appraise));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 递归收集 id 的节点及 id 节点的孩子节点 (迭代器删除居然也报错)
|
|
|
|
|
*/
|
|
|
|
|
private void collectNodesToDelete(String id, List<EvaluationTreeNode> nodes, List<EvaluationTreeNode> nodesToDelete) {
|
|
|
|
|
for (EvaluationTreeNode node : nodes) {
|
|
|
|
|
private void collectNodesToDelete(String id, List<AppraiseTreeNode> nodes, List<AppraiseTreeNode> nodesToDelete) {
|
|
|
|
|
for (AppraiseTreeNode node : nodes) {
|
|
|
|
|
if (id.equals(node.getPid())) {
|
|
|
|
|
// 收集
|
|
|
|
|
nodesToDelete.add(node);
|
|
|
|
@ -186,12 +186,12 @@ public class EvaluationServiceImpl implements EvaluationService {
|
|
|
|
|
/**
|
|
|
|
|
* 递归的构建父亲节点的孩子,以及孩子的孩子 (理论支持无极树,但应该考虑是否增加递归深度)
|
|
|
|
|
*/
|
|
|
|
|
private void buildChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) {
|
|
|
|
|
List<EvaluationTreeNode> children = getChildren(parent, nodes);
|
|
|
|
|
private void buildChildren(AppraiseTreeNode parent, List<AppraiseTreeNode> nodes) {
|
|
|
|
|
List<AppraiseTreeNode> children = getChildren(parent, nodes);
|
|
|
|
|
if (children.size() == 0) return;
|
|
|
|
|
|
|
|
|
|
parent.setChildren(children);
|
|
|
|
|
for (EvaluationTreeNode child : children) {
|
|
|
|
|
for (AppraiseTreeNode child : children) {
|
|
|
|
|
// 如果子节点还有孩子的话,就继续递归构建
|
|
|
|
|
if (getChildren(child, nodes).size() > 0) {
|
|
|
|
|
buildChildren(child, nodes);
|
|
|
|
@ -204,9 +204,9 @@ public class EvaluationServiceImpl implements EvaluationService {
|
|
|
|
|
/**
|
|
|
|
|
* 获取节点的孩子节点列表
|
|
|
|
|
*/
|
|
|
|
|
private List<EvaluationTreeNode> getChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) {
|
|
|
|
|
List<EvaluationTreeNode> children = new ArrayList<>();
|
|
|
|
|
for (EvaluationTreeNode node : nodes) {
|
|
|
|
|
private List<AppraiseTreeNode> getChildren(AppraiseTreeNode parent, List<AppraiseTreeNode> nodes) {
|
|
|
|
|
List<AppraiseTreeNode> children = new ArrayList<>();
|
|
|
|
|
for (AppraiseTreeNode node : nodes) {
|
|
|
|
|
if (StringUtils.isNotBlank(node.getPid()) && node.getPid().equals(parent.getId())) {
|
|
|
|
|
children.add(node);
|
|
|
|
|
}
|
|
|
|
|