From 18cfb50a46f8a29fbc18c0b011d4c4e4a2246dde Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Thu, 30 Nov 2023 14:44:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=20validation=20?= =?UTF-8?q?=E4=BE=9D=E8=B5=96,=E4=BF=AE=E5=A4=8D=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E4=B8=8D=E7=94=9F=E6=95=88=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 +++ src/main/java/cn/teammodel/common/R.java | 2 +- .../controller/AppraiseController.java | 15 +++++---- .../dao/AppraiseRecordRepository.java | 6 ++++ .../model/dto/Appraise/AppraiseVoteDto.java | 7 +++- .../model/dto/Appraise/FindVoteRecordDto.java | 33 +++++++++++++++++++ .../model/entity/appraise/AppraiseRecord.java | 4 +++ .../entity/appraise/AppraiseRecordItem.java | 2 +- .../teammodel/service/EvaluationService.java | 2 ++ .../service/impl/EvaluationServiceImpl.java | 13 +++++++- .../cn/teammodel/utils/RepositoryUtil.java | 3 ++ .../TeamModelExtensionApplicationTests.java | 4 ++- 12 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java diff --git a/pom.xml b/pom.xml index bede74a..9e20422 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,11 @@ spring-cloud-azure-starter-data-cosmos + + org.springframework.boot + spring-boot-starter-validation + + cn.hutool hutool-all diff --git a/src/main/java/cn/teammodel/common/R.java b/src/main/java/cn/teammodel/common/R.java index 3fb1faf..d6e1fe6 100644 --- a/src/main/java/cn/teammodel/common/R.java +++ b/src/main/java/cn/teammodel/common/R.java @@ -35,7 +35,7 @@ public class R implements Serializable { } public static R error(String msg) { - return new R<>(ErrorCode.SYSTEM_ERROR); + return new R<>(ErrorCode.SYSTEM_ERROR.getCode(), null, msg); } public static R error(Integer code, String msg) { diff --git a/src/main/java/cn/teammodel/controller/AppraiseController.java b/src/main/java/cn/teammodel/controller/AppraiseController.java index 6f65228..91c3d3e 100644 --- a/src/main/java/cn/teammodel/controller/AppraiseController.java +++ b/src/main/java/cn/teammodel/controller/AppraiseController.java @@ -1,11 +1,7 @@ package cn.teammodel.controller; import cn.teammodel.common.R; -import cn.teammodel.model.dto.Appraise.AppraiseVoteDto; -import cn.teammodel.model.dto.Appraise.DeleteNodeDto; -import cn.teammodel.model.dto.Appraise.GetEvaluateTreeDto; -import cn.teammodel.model.dto.Appraise.InsertNodeDto; -import cn.teammodel.model.dto.Appraise.UpdateNodeDto; +import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.service.EvaluationService; import io.swagger.annotations.ApiOperation; @@ -55,10 +51,17 @@ public class AppraiseController { @PostMapping("vote") @ApiOperation(value = "给某个学生评价(投票)") - public R vote(@Valid @RequestBody AppraiseVoteDto appraiseVoteDto) { + public R vote(@RequestBody @Valid AppraiseVoteDto appraiseVoteDto) { evaluationService.vote(appraiseVoteDto); return R.success("评价成功"); } + @PostMapping("findVoteRecord") + @ApiOperation(value = "多条件查询学生评价(投票)") + public R findVoteRecord(@Valid @RequestBody FindVoteRecordDto findVoteRecordDto) { + evaluationService.findVoteRecord(findVoteRecordDto); + return R.success("评价成功"); + } + } diff --git a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java index 6685f7e..5f41437 100644 --- a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java +++ b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java @@ -2,8 +2,12 @@ package cn.teammodel.dao; import cn.teammodel.model.entity.appraise.AppraiseRecord; import com.azure.spring.data.cosmos.repository.CosmosRepository; +import com.azure.spring.data.cosmos.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; + /** * @author winter * @create 2023-11-20 14:39
@@ -21,4 +25,6 @@ public interface AppraiseRecordRepository extends CosmosRepository findScoreAndPraise(@Param("targetId") String targetId,@Param("academicYearId") String academicYearId,@Param("code") String code); } diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java index 21fc369..310d7ae 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java @@ -3,6 +3,7 @@ package cn.teammodel.model.dto.Appraise; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; /** @@ -12,9 +13,13 @@ import javax.validation.constraints.NotNull; @Data public class AppraiseVoteDto { @NotNull - @ApiModelProperty(value = "评价对象为班级 或 学生") + @ApiModelProperty(value = "评价对象 Id") private String targetId; + @ApiModelProperty(value = "评价对象所处班级 Id") + @NotBlank + private String classId; + @NotNull @ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class") private String targetType; diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java new file mode 100644 index 0000000..80c79b6 --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java @@ -0,0 +1,33 @@ +package cn.teammodel.model.dto.Appraise; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +/** + * @author winter + * @create 2023-11-28 16:16 + */ +@Data +public class FindVoteRecordDto { + @NotNull + @ApiModelProperty(value = "班级或学生 id") + private String targetId; + + @NotNull + @ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class") + private String targetType; + + // 班级 id + /** + * 可以当作唯一 id + */ + @NotNull + @ApiModelProperty(value = "评价项唯一 id", required = true) + private String appraiseId; + + @ApiModelProperty(value = "学段 id,用于拿到 semesterId", required = true) + @NotNull + private String periodId; +} diff --git a/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java b/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java index d3d2d49..473ffc4 100644 --- a/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java +++ b/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java @@ -24,6 +24,10 @@ public class AppraiseRecord extends BaseItem { */ private String periodId; /** + * 班级 id + */ + private String classId; + /** * 学年(组合 id: 学年-semesterId -> 2023-{semesterId}) */ private String academicYearId; diff --git a/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecordItem.java b/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecordItem.java index ea9eea6..bed9652 100644 --- a/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecordItem.java +++ b/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecordItem.java @@ -16,7 +16,7 @@ import java.time.LocalDateTime; @JsonInclude(JsonInclude.Include.NON_NULL) public class AppraiseRecordItem { private String id; - private String appraiseNodeId; + private AppraiseTreeNode appraiseNode; /** * 用包装类,这样为 null 时就不会持久化 */ diff --git a/src/main/java/cn/teammodel/service/EvaluationService.java b/src/main/java/cn/teammodel/service/EvaluationService.java index 37650f4..08b6060 100644 --- a/src/main/java/cn/teammodel/service/EvaluationService.java +++ b/src/main/java/cn/teammodel/service/EvaluationService.java @@ -47,4 +47,6 @@ public interface EvaluationService { * 给学生评价 */ void vote(AppraiseVoteDto appraiseVoteDto); + + void findVoteRecord(FindVoteRecordDto findVoteRecordDto); } diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index c7bac1f..af45089 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -181,6 +181,9 @@ public class EvaluationServiceImpl implements EvaluationService { this.collectNodesToDelete(node.getId(), nodes, nodesToDelete); } } + if (ObjectUtils.isEmpty(nodesToDelete)) { + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "删除节点不存在"); + } nodes.removeAll(nodesToDelete); return buildTree(appraiseRepository.save(appraise)); @@ -189,6 +192,7 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public void vote(AppraiseVoteDto appraiseVoteDto) { String targetId = appraiseVoteDto.getTargetId(); + String classId = appraiseVoteDto.getClassId(); boolean spread = appraiseVoteDto.isSpread(); String targetType = appraiseVoteDto.getTargetType(); String appraiseId = appraiseVoteDto.getAppraiseId(); @@ -197,6 +201,7 @@ public class EvaluationServiceImpl implements EvaluationService { User loginUser = SecurityUtil.getLoginUser(); String schoolId = loginUser.getSchoolId(); + // 判断评价对象是否合法 if (!targetType.equals(TARGET_STUDENT) && !targetType.equals(TARGET_CLASS)) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "不受支持的评价对象"); } @@ -223,7 +228,7 @@ public class EvaluationServiceImpl implements EvaluationService { // 初始化新的评价节点 AppraiseRecordItem item = new AppraiseRecordItem(); item.setId(UUID.randomUUID().toString()); - item.setAppraiseNodeId(appraiseId); + item.setAppraiseNode(appraiseTreeNode); item.setCreator(loginUser.getName()); item.setCreatorId(loginUser.getId()); item.setCreateTime(LocalDateTime.now()); @@ -239,6 +244,7 @@ public class EvaluationServiceImpl implements EvaluationService { record.setTargetType(TARGET_CLASS); } record.setTargetId(targetId); + record.setClassId(classId); record.setAcademicYearId(academicYearId); record.setPraiseCount(appraiseTreeNode.isPraise() ? 1 : -1); record.setScore(ObjectUtils.isEmpty(appraiseTreeNode.getScore()) ? 0 : appraiseTreeNode.getScore()); @@ -263,6 +269,11 @@ public class EvaluationServiceImpl implements EvaluationService { } } + @Override + public void findVoteRecord(FindVoteRecordDto findVoteRecordDto) { + + } + /** * 递归收集 id 的节点及 id 节点的孩子节点 (迭代器删除居然也报错) */ diff --git a/src/main/java/cn/teammodel/utils/RepositoryUtil.java b/src/main/java/cn/teammodel/utils/RepositoryUtil.java index 6edfde2..b6db971 100644 --- a/src/main/java/cn/teammodel/utils/RepositoryUtil.java +++ b/src/main/java/cn/teammodel/utils/RepositoryUtil.java @@ -2,6 +2,7 @@ package cn.teammodel.utils; import cn.teammodel.common.ErrorCode; import cn.teammodel.config.exception.ServiceException; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import java.util.List; @@ -10,12 +11,14 @@ import java.util.List; * @author winter * @create 2023-11-29 15:41 */ +@Slf4j public class RepositoryUtil { /** * 获取唯一一个结果,如果list为空或者list长度大于1,抛出参数错误异常 */ public static T findOne(List list) { if (ObjectUtils.isEmpty(list) || list.size() > 1) { + log.error("查询结果为空或数量 > 1"); throw new ServiceException(ErrorCode.PARAMS_ERROR); } return list.get(0); diff --git a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java index a2951a9..e20230b 100644 --- a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java +++ b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java @@ -11,6 +11,7 @@ import cn.teammodel.model.entity.appraise.AppraiseRecord; import cn.teammodel.model.entity.appraise.AppraiseTreeNode; import cn.teammodel.service.EvaluationService; import cn.teammodel.service.impl.EvaluationServiceImpl; +import cn.teammodel.utils.RepositoryUtil; import com.azure.cosmos.models.PartitionKey; import com.azure.spring.data.cosmos.core.CosmosTemplate; import org.junit.jupiter.api.Test; @@ -89,10 +90,11 @@ class TeamModelExtensionApplicationTests { //System.out.println(nodeById); //System.out.println(schoolRepository.findSchoolByIdAndCode("hbcn", "Base", Semester.class)); - AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByStudentIdAndAcademicYearIdAndCode("fakeStudentId1", + List record = appraiseRecordRepository.findScoreAndPraise("fakeStudentId1", "2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017", String.format(PK.PK_APPRAISE_RECORD,"habook") ); + System.out.println(RepositoryUtil.findOne(record)); } @Test public void testUpdate() {