From edc79455ccc995f1e6dbf12b1d6685178900c261 Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Mon, 27 Nov 2023 11:38:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=BD=E5=8F=96=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E7=9A=84=E5=AE=9E=E4=BD=93=E7=B1=BB=20BaseItem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/cn/teammodel/common/PK.java | 23 +++++++++++- .../cn/teammodel/model/entity/Appraise.java | 15 ++------ .../model/entity/AppraiseRecord.java | 36 +++++++++++++++++++ .../cn/teammodel/model/entity/BaseItem.java | 24 +++++++++++++ .../service/impl/EvaluationServiceImpl.java | 13 +++---- .../java/cn/teammodel/TestWithoutSpring.java | 3 +- 7 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 src/main/java/cn/teammodel/model/entity/AppraiseRecord.java create mode 100644 src/main/java/cn/teammodel/model/entity/BaseItem.java diff --git a/README.md b/README.md index 4c9118f..6dc219a 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,4 @@ ### 教育评价项 > 学生每学期所有的评价项都在一个项中,按学校进行分区 -分区键: `AppraiseItem-{学校id}` \ No newline at end of file +分区键: `AppraiseRecord-{学校id}` \ No newline at end of file diff --git a/src/main/java/cn/teammodel/common/PK.java b/src/main/java/cn/teammodel/common/PK.java index ce92243..9b0df58 100644 --- a/src/main/java/cn/teammodel/common/PK.java +++ b/src/main/java/cn/teammodel/common/PK.java @@ -11,6 +11,27 @@ public interface PK { /** * 评价树 */ - PartitionKey PK_EVALUATION = new PartitionKey("Appraise"); + String PK_APPRAISE = "Appraise"; + /** + * 学生的评价记录 + * 参数数量: 1 , 拼接: SchoolId + */ + String PK_APPRAISE_RECORD = "AppraiseRecord-%s"; + + /** + * 构建分区键 + */ + static PartitionKey of(String pk) { + return new PartitionKey(pk); + } + + /** + * 需要传参构建分区键
+ * 注意: 参数的个数不会在编译时被校验,需自己注意 + */ + static PartitionKey buildOf(String pk, String... args) { + String pkStr = String.format(pk, args); + return new PartitionKey(pkStr); + } } diff --git a/src/main/java/cn/teammodel/model/entity/Appraise.java b/src/main/java/cn/teammodel/model/entity/Appraise.java index 9cb977c..85ef5ba 100644 --- a/src/main/java/cn/teammodel/model/entity/Appraise.java +++ b/src/main/java/cn/teammodel/model/entity/Appraise.java @@ -1,11 +1,9 @@ package cn.teammodel.model.entity; import com.azure.spring.data.cosmos.core.mapping.Container; -import com.azure.spring.data.cosmos.core.mapping.GeneratedValue; -import com.azure.spring.data.cosmos.core.mapping.PartitionKey; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; -import org.springframework.data.annotation.Id; +import lombok.EqualsAndHashCode; import java.util.List; @@ -14,18 +12,11 @@ import java.util.List; * @author winter * @create 2023-11-20 11:04 */ +@EqualsAndHashCode(callSuper = true) @Container(containerName = "School") @Data @JsonInclude(JsonInclude.Include.NON_NULL) -public class Appraise { - @Id - @GeneratedValue - private String id; - /** - * 分区键: evaluation - */ - @PartitionKey - private String code; +public class Appraise extends BaseItem{ /** * 学校 Id */ diff --git a/src/main/java/cn/teammodel/model/entity/AppraiseRecord.java b/src/main/java/cn/teammodel/model/entity/AppraiseRecord.java new file mode 100644 index 0000000..c5b8afe --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/AppraiseRecord.java @@ -0,0 +1,36 @@ +package cn.teammodel.model.entity; + +import com.azure.spring.data.cosmos.core.mapping.Container; +import com.azure.spring.data.cosmos.core.mapping.GeneratedValue; +import com.azure.spring.data.cosmos.core.mapping.PartitionKey; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; +import org.springframework.data.annotation.Id; + +/** + * @author winter + * @create 2023-11-27 11:02 + */ +@Container(containerName = "Student") +@Data +@JsonInclude(JsonInclude.Include.NON_NULL) +public class AppraiseRecord { + @Id + @GeneratedValue + private String id; + /** + * 分区键: AppraiseRecord-{schoolId} + */ + @PartitionKey + private String code; + /** + * 学校 Id + */ + private String schoolId; + /** + * 学段 id + */ + private String periodId; + + +} diff --git a/src/main/java/cn/teammodel/model/entity/BaseItem.java b/src/main/java/cn/teammodel/model/entity/BaseItem.java new file mode 100644 index 0000000..10ae0e6 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/BaseItem.java @@ -0,0 +1,24 @@ +package cn.teammodel.model.entity; + +import com.azure.spring.data.cosmos.core.mapping.GeneratedValue; +import com.azure.spring.data.cosmos.core.mapping.PartitionKey; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; +import org.springframework.data.annotation.Id; + +/** + * @author winter + * @create 2023-11-27 11:05 + */ +@Data +@JsonInclude(JsonInclude.Include.NON_NULL) +public class BaseItem { + @Id + @GeneratedValue + private String id; +/** +* 分区键 +*/ + @PartitionKey + private String code; +} diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index e0d02a3..ce13739 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -40,13 +40,14 @@ public class EvaluationServiceImpl implements EvaluationService { * 通用的获取 evaluation 的方法: 判断参数,判断数据是否为空
* 从 token 中获取 schoolId */ - private Appraise findEvaluation(String periodId) { + private Appraise findAppraise(String periodId) { + // 默认学区为 default periodId = StringUtils.isEmpty(periodId) ? "default" : periodId; User loginUser = SecurityUtils.getLoginUser(); String schoolId = loginUser.getSchoolId(); // 拿到要新增节点的原始数据 - Appraise appraise = evaluationRepository.findBySchoolIdAndPeriodId(schoolId, periodId, PK.PK_EVALUATION); + Appraise appraise = evaluationRepository.findBySchoolIdAndPeriodId(schoolId, periodId, PK.of(PK.PK_APPRAISE)); if (appraise == null) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "学校评价数据不存在"); } @@ -55,7 +56,7 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public Appraise getTree(GetEvaluateTreeDto getEvaluateTreeDto) { - Appraise appraise = findEvaluation(getEvaluateTreeDto.getPeriodId()); + Appraise appraise = findAppraise(getEvaluateTreeDto.getPeriodId()); return this.buildTree(appraise); } @@ -100,7 +101,7 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public Appraise insertNode(InsertNodeDto insertNodeDto) { - Appraise appraise = findEvaluation(insertNodeDto.getPeriodId()); + Appraise appraise = findAppraise(insertNodeDto.getPeriodId()); User loginUser = SecurityUtils.getLoginUser(); List originNodes = appraise.getNodes(); @@ -130,7 +131,7 @@ public class EvaluationServiceImpl implements EvaluationService { public Appraise updateNode(UpdateNodeDto updateNodeDto) { String updateNodeId = updateNodeDto.getId(); - Appraise appraise = findEvaluation(updateNodeDto.getPeriodId()); + Appraise appraise = findAppraise(updateNodeDto.getPeriodId()); List originNodes = appraise.getNodes(); // 每个节点都有 id, 直接校验是否合法 AppraiseTreeNode updateNode = originNodes.stream() @@ -150,7 +151,7 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public Appraise deleteNode(DeleteNodeDto deleteNodeDto) { // 删除指定节点,可能是(一级,二级,三级),设计一个通用的 - Appraise appraise = findEvaluation(deleteNodeDto.getPeriodId()); + Appraise appraise = findAppraise(deleteNodeDto.getPeriodId()); List nodes = appraise.getNodes(); List nodesToDelete = new ArrayList<>(); diff --git a/src/test/java/cn/teammodel/TestWithoutSpring.java b/src/test/java/cn/teammodel/TestWithoutSpring.java index 83b270b..1216416 100644 --- a/src/test/java/cn/teammodel/TestWithoutSpring.java +++ b/src/test/java/cn/teammodel/TestWithoutSpring.java @@ -11,7 +11,6 @@ import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -137,6 +136,6 @@ public class TestWithoutSpring { @Test public void testLocalDateTime() { - System.out.println(LocalDateTime.now()); + } }