From d185a3ffe706d4b0449f57b0d467fe2041b3a33f Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Wed, 20 Dec 2023 17:18:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E8=AF=84?= =?UTF-8?q?=E4=BB=B7=E6=A0=91=E7=9A=84=E5=AD=A6=E6=AE=B5id,=E7=8E=B0?= =?UTF-8?q?=E6=91=92=E5=BC=83default=E4=B8=BA=E9=BB=98=E8=AE=A4,=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E5=AD=A6=E6=AE=B5id=E9=83=BD=E6=9C=89=E8=87=AA?= =?UTF-8?q?=E5=B7=B1=E7=9A=84=E8=AF=84=E4=BB=B7=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/frontend/AppraiseController.java | 8 ++++---- .../teammodel/model/dto/Appraise/DeleteNodeDto.java | 2 +- .../model/dto/Appraise/FindVoteRecordDto.java | 2 +- .../model/dto/Appraise/GetEvaluateTreeDto.java | 5 ++++- .../teammodel/model/dto/Appraise/InsertNodeDto.java | 4 +++- .../teammodel/model/dto/Appraise/UpdateNodeDto.java | 5 ++++- .../service/impl/EvaluationServiceImpl.java | 13 ++++++++----- .../java/cn/teammodel/utils/RepositoryUtil.java | 2 +- 8 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java b/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java index 8d9ef45..91ae70a 100644 --- a/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java +++ b/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java @@ -29,26 +29,26 @@ public class AppraiseController { @PostMapping("getTrees") @ApiOperation(value = "获取评价树", notes = "获取评价树") - public R getEvaluateTree(@RequestBody GetEvaluateTreeDto getEvaluateTreeDto) { + public R getEvaluateTree(@RequestBody @Valid GetEvaluateTreeDto getEvaluateTreeDto) { Appraise appraise = evaluationService.getTree(getEvaluateTreeDto); return R.success(appraise); } @PostMapping("insertNode") @ApiOperation(value = "新增评价树的节点") - public R insertNode(@RequestBody InsertNodeDto insertNodeDto) { + public R insertNode(@RequestBody @Valid InsertNodeDto insertNodeDto) { Appraise appraise = evaluationService.insertNode(insertNodeDto); return R.success(appraise); } @PostMapping("updateNode") @ApiOperation(value = "更新评价树的节点", notes = "传递更新后的节点,而不是局部更新的值") - public R updateTree(@RequestBody UpdateNodeDto updateNodeDto) { + public R updateTree(@RequestBody @Valid UpdateNodeDto updateNodeDto) { Appraise appraise = evaluationService.updateNode(updateNodeDto); return R.success(appraise); } @PostMapping("deleteNode") @ApiOperation(value = "删除评价树的节点") - public R deleteNode(@Valid @RequestBody DeleteNodeDto deleteNodeDto) { + public R deleteNode(@RequestBody @Valid DeleteNodeDto deleteNodeDto) { Appraise appraise = evaluationService.deleteNode(deleteNodeDto); return R.success(appraise); } diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/DeleteNodeDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/DeleteNodeDto.java index 9a9b3b7..e5476a3 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/DeleteNodeDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/DeleteNodeDto.java @@ -10,7 +10,7 @@ import javax.validation.constraints.NotNull; */ @Data public class DeleteNodeDto { - // 检索需要,但是有 bug + @NotNull(message = "学段 id 不能为空") String periodId; @NotNull String id; diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java index e8bd5ec..0cd1783 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java @@ -25,7 +25,7 @@ public class FindVoteRecordDto extends PageableRequest { @ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class") private String targetType; - @ApiModelProperty(value = "按班级 id搜索") + @ApiModelProperty(value = "按班级 id 搜索") private String classId; @ApiModelProperty(value = "是否为表扬") private Boolean isPraise; diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/GetEvaluateTreeDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/GetEvaluateTreeDto.java index 7d91039..5570faa 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/GetEvaluateTreeDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/GetEvaluateTreeDto.java @@ -3,12 +3,15 @@ package cn.teammodel.model.dto.Appraise; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import javax.validation.constraints.NotNull; + /** * @author winter * @create 2023-11-22 15:21 */ @Data public class GetEvaluateTreeDto { - @ApiModelProperty("学段 id, 默认为 default") + @ApiModelProperty(value = "学段 id", required = true) + @NotNull(message = "学段 id 不能为空") String periodId; } 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 37d430a..912c6a4 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java @@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; /** * @author winter @@ -11,7 +12,8 @@ import javax.validation.constraints.NotBlank; */ @Data public class InsertNodeDto { - @ApiModelProperty(value = "不传则默认为 default 的树增加节点") + @ApiModelProperty(value = "学段 id", required = true) + @NotNull(message = "学段 id 不能为空") String periodId; @ApiModelProperty(value = "父亲节点,不传则为根节点") String pid; diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java index 832b8a3..7b87987 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java @@ -3,13 +3,16 @@ package cn.teammodel.model.dto.Appraise; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import javax.validation.constraints.NotNull; + /** * @author winter * @create 2023-11-22 16:16 */ @Data public class UpdateNodeDto { - @ApiModelProperty(value = "不传则默认为 default 的树修改节点") + @ApiModelProperty(value = "学段 id", required = true) + @NotNull(message = "学段 id 不能为空") String periodId; @ApiModelProperty(value = "评价项节点的 id") String id; diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index fb11eb8..1788200 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -60,8 +60,6 @@ public class EvaluationServiceImpl implements EvaluationService { * 从 token 中获取 schoolId */ private Appraise findAppraise(String periodId) { - // 默认学区为 default - periodId = StringUtils.isEmpty(periodId) ? "default" : periodId; User loginUser = SecurityUtil.getLoginUser(); String schoolId = loginUser.getSchoolId(); @@ -76,8 +74,6 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public Appraise getTree(GetEvaluateTreeDto getEvaluateTreeDto) { String periodId = getEvaluateTreeDto.getPeriodId(); - // 默认学区为 default - periodId = StringUtils.isEmpty(periodId) ? "default" : periodId; User loginUser = SecurityUtil.getLoginUser(); String schoolId = loginUser.getSchoolId(); Appraise appraise = appraiseRepository.findAppraiseBySchoolIdAndPeriodIdAndCode(schoolId, periodId, PK.PK_APPRAISE); @@ -279,7 +275,10 @@ public class EvaluationServiceImpl implements EvaluationService { // 获取评价项节点 List nodes = appraiseRepository.findNodeById(PK.PK_APPRAISE, appraiseId); - AppraiseTreeNode appraiseTreeNode = RepositoryUtil.findOne(nodes, "获取评价项失败"); + AppraiseTreeNode appraiseTreeNode = RepositoryUtil.findOne(nodes, "该评价项不存在"); + if (appraiseTreeNode.getPath() == null) { + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "仅能评价三级评价项"); + } // 通过 periodId 获取 semesters List periodById = schoolRepository.findPeriodById(schoolId, periodId); @@ -439,6 +438,10 @@ public class EvaluationServiceImpl implements EvaluationService { for (AppraiseRecordItem record : records) { AppraiseTreeNode appraiseNode = record.getAppraiseNode(); String[] path = appraiseNode.getPath(); + // 数据异常则跳过 + if (path == null || path.length == 0){ + continue; + } String root = path[0]; if (appraiseNode.isPraise()) { praiseDistribution.put(root, praiseDistribution.getOrDefault(root, 0) + 1); diff --git a/src/main/java/cn/teammodel/utils/RepositoryUtil.java b/src/main/java/cn/teammodel/utils/RepositoryUtil.java index 2adf3af..9bea2e9 100644 --- a/src/main/java/cn/teammodel/utils/RepositoryUtil.java +++ b/src/main/java/cn/teammodel/utils/RepositoryUtil.java @@ -19,7 +19,7 @@ public class RepositoryUtil { public static T findOne(List list, String errorMsg) { if (ObjectUtils.isEmpty(list) || list.size() > 1) { log.error("查询结果为空或数量 > 1"); - throw new ServiceException(ErrorCode.PARAMS_ERROR); + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), errorMsg); } return list.get(0); }