refactor: 抽取公共的实体类 BaseItem

11111
winter 1 year ago
parent 610c6d4e81
commit edc79455cc

@ -17,4 +17,4 @@
### 教育评价项 ### 教育评价项
> 学生每学期所有的评价项都在一个项中,按学校进行分区 > 学生每学期所有的评价项都在一个项中,按学校进行分区
分区键: `AppraiseItem-{学校id}` 分区键: `AppraiseRecord-{学校id}`

@ -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);
}
/**
* <br/>
* : ,
*/
static PartitionKey buildOf(String pk, String... args) {
String pkStr = String.format(pk, args);
return new PartitionKey(pkStr);
}
} }

@ -1,11 +1,9 @@
package cn.teammodel.model.entity; package cn.teammodel.model.entity;
import com.azure.spring.data.cosmos.core.mapping.Container; 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 com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data; import lombok.Data;
import org.springframework.data.annotation.Id; import lombok.EqualsAndHashCode;
import java.util.List; import java.util.List;
@ -14,18 +12,11 @@ import java.util.List;
* @author winter * @author winter
* @create 2023-11-20 11:04 * @create 2023-11-20 11:04
*/ */
@EqualsAndHashCode(callSuper = true)
@Container(containerName = "School") @Container(containerName = "School")
@Data @Data
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
public class Appraise { public class Appraise extends BaseItem{
@Id
@GeneratedValue
private String id;
/**
* : evaluation
*/
@PartitionKey
private String code;
/** /**
* Id * Id
*/ */

@ -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;
}

@ -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;
}

@ -40,13 +40,14 @@ public class EvaluationServiceImpl implements EvaluationService {
* evaluation : , <br/> * evaluation : , <br/>
* token schoolId * token schoolId
*/ */
private Appraise findEvaluation(String periodId) { private Appraise findAppraise(String periodId) {
// 默认学区为 default
periodId = StringUtils.isEmpty(periodId) ? "default" : periodId; periodId = StringUtils.isEmpty(periodId) ? "default" : periodId;
User loginUser = SecurityUtils.getLoginUser(); User loginUser = SecurityUtils.getLoginUser();
String schoolId = loginUser.getSchoolId(); 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) { if (appraise == null) {
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "学校评价数据不存在"); throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "学校评价数据不存在");
} }
@ -55,7 +56,7 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override @Override
public Appraise getTree(GetEvaluateTreeDto getEvaluateTreeDto) { public Appraise getTree(GetEvaluateTreeDto getEvaluateTreeDto) {
Appraise appraise = findEvaluation(getEvaluateTreeDto.getPeriodId()); Appraise appraise = findAppraise(getEvaluateTreeDto.getPeriodId());
return this.buildTree(appraise); return this.buildTree(appraise);
} }
@ -100,7 +101,7 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override @Override
public Appraise insertNode(InsertNodeDto insertNodeDto) { public Appraise insertNode(InsertNodeDto insertNodeDto) {
Appraise appraise = findEvaluation(insertNodeDto.getPeriodId()); Appraise appraise = findAppraise(insertNodeDto.getPeriodId());
User loginUser = SecurityUtils.getLoginUser(); User loginUser = SecurityUtils.getLoginUser();
List<AppraiseTreeNode> originNodes = appraise.getNodes(); List<AppraiseTreeNode> originNodes = appraise.getNodes();
@ -130,7 +131,7 @@ public class EvaluationServiceImpl implements EvaluationService {
public Appraise updateNode(UpdateNodeDto updateNodeDto) { public Appraise updateNode(UpdateNodeDto updateNodeDto) {
String updateNodeId = updateNodeDto.getId(); String updateNodeId = updateNodeDto.getId();
Appraise appraise = findEvaluation(updateNodeDto.getPeriodId()); Appraise appraise = findAppraise(updateNodeDto.getPeriodId());
List<AppraiseTreeNode> originNodes = appraise.getNodes(); List<AppraiseTreeNode> originNodes = appraise.getNodes();
// 每个节点都有 id, 直接校验是否合法 // 每个节点都有 id, 直接校验是否合法
AppraiseTreeNode updateNode = originNodes.stream() AppraiseTreeNode updateNode = originNodes.stream()
@ -150,7 +151,7 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override @Override
public Appraise deleteNode(DeleteNodeDto deleteNodeDto) { public Appraise deleteNode(DeleteNodeDto deleteNodeDto) {
// 删除指定节点,可能是(一级,二级,三级),设计一个通用的 // 删除指定节点,可能是(一级,二级,三级),设计一个通用的
Appraise appraise = findEvaluation(deleteNodeDto.getPeriodId()); Appraise appraise = findAppraise(deleteNodeDto.getPeriodId());
List<AppraiseTreeNode> nodes = appraise.getNodes(); List<AppraiseTreeNode> nodes = appraise.getNodes();
List<AppraiseTreeNode> nodesToDelete = new ArrayList<>(); List<AppraiseTreeNode> nodesToDelete = new ArrayList<>();

@ -11,7 +11,6 @@ import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -137,6 +136,6 @@ public class TestWithoutSpring {
@Test @Test
public void testLocalDateTime() { public void testLocalDateTime() {
System.out.println(LocalDateTime.now());
} }
} }

Loading…
Cancel
Save