diff --git a/src/main/java/cn/teammodel/controller/HelloController.java b/src/main/java/cn/teammodel/controller/HelloController.java index 0337a44..e0cac8a 100644 --- a/src/main/java/cn/teammodel/controller/HelloController.java +++ b/src/main/java/cn/teammodel/controller/HelloController.java @@ -1,26 +1,32 @@ package cn.teammodel.controller; import cn.teammodel.common.R; +import cn.teammodel.dao.EvaluationTreeRepository; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import javax.annotation.Resource; + @RestController @RequestMapping("/") public class HelloController { + @Resource + private EvaluationTreeRepository evaluationTreeRepository; + @GetMapping("hello") @PreAuthorize("@ss.hasRole('admin')") - public R hello() { + public R hello() { System.out.println(SecurityContextHolder.getContext().getAuthentication()); + return new R(200, "success","hello world"); } @GetMapping("public/free") @PreAuthorize("permitAll()") - public R free() { - System.out.println(SecurityContextHolder.getContext().getAuthentication()); + public R free() { return new R(200, "success","hello world"); } } \ No newline at end of file diff --git a/src/main/java/cn/teammodel/dao/EvaluationTreeRepository.java b/src/main/java/cn/teammodel/dao/EvaluationTreeRepository.java new file mode 100644 index 0000000..3a96a5d --- /dev/null +++ b/src/main/java/cn/teammodel/dao/EvaluationTreeRepository.java @@ -0,0 +1,22 @@ +package cn.teammodel.dao; + +import cn.teammodel.model.entity.EvaluationTree; +import com.azure.spring.data.cosmos.repository.CosmosRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Repository; + +/** + * @author winter + * @create 2023-11-20 14:39
+ * Page findByLastname(String lastname, Pageable pageable);
+ * Slice findByLastname(String lastname, Pageable pageable);
+ * Window findTop10ByLastname(String lastname, ScrollPosition position, Sort sort);
+ * List findByLastname(String lastname, Sort sort);
+ * List findByLastname(String lastname, Sort sort, Limit limit);
+ * List findByLastname(String lastname, Pageable pageable);
+ */ +@Repository +public interface EvaluationTreeRepository extends CosmosRepository { + Page findById(String lastname, Pageable pageable); +} diff --git a/src/main/java/cn/teammodel/dao/StudentRepository.java b/src/main/java/cn/teammodel/dao/StudentRepository.java new file mode 100644 index 0000000..9531269 --- /dev/null +++ b/src/main/java/cn/teammodel/dao/StudentRepository.java @@ -0,0 +1,11 @@ +package cn.teammodel.dao; + +import cn.teammodel.model.entity.Student; +import com.azure.spring.data.cosmos.repository.CosmosRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface StudentRepository extends CosmosRepository { + + //Flux findById(String id); +} \ No newline at end of file diff --git a/src/main/java/cn/teammodel/model/entity/EvaluationTree.java b/src/main/java/cn/teammodel/model/entity/EvaluationTree.java new file mode 100644 index 0000000..bd196e7 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/EvaluationTree.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 lombok.Data; +import lombok.ToString; +import org.springframework.data.annotation.Id; + +import java.util.List; + +/** + * 评价列表 + * @author winter + * @create 2023-11-20 11:04 + */ +@Container(containerName = "School") +@Data +@ToString +public class EvaluationTree { + @Id + @GeneratedValue + String id; + /** + * 分区键: evaluation + */ + @PartitionKey + String code; + /** + * 学段 id + */ + String campusId; + + List nodes; + +} diff --git a/src/main/java/cn/teammodel/model/entity/EvaluationTreeNode.java b/src/main/java/cn/teammodel/model/entity/EvaluationTreeNode.java new file mode 100644 index 0000000..94e5e75 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/EvaluationTreeNode.java @@ -0,0 +1,16 @@ +package cn.teammodel.model.entity; + +import lombok.Data; +import org.springframework.data.annotation.Transient; + +import java.util.List; + +@Data +public class EvaluationTreeNode { + String id; + String pid; + String name; + Integer score; + @Transient + List children; +} diff --git a/src/main/java/cn/teammodel/model/entity/Student.java b/src/main/java/cn/teammodel/model/entity/Student.java new file mode 100644 index 0000000..1d87e34 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/Student.java @@ -0,0 +1,47 @@ +package cn.teammodel.model.entity; + +import com.azure.spring.data.cosmos.core.mapping.Container; +import com.azure.spring.data.cosmos.core.mapping.PartitionKey; +import lombok.Data; +import org.springframework.data.annotation.Id; + +import java.util.List; + + +@Data +@Container(containerName = "Student") +public class Student { + @Id + private String id; + @PartitionKey + private String code; + private String mail; + private String mobile; + private String country; + private String name; + private String picture; + private String schoolId; + private String pw; + private String salt; + private Integer year; + private String no; + private String irs; + private String classId; + private String groupId; + private String groupName; + private String periodId; + private String gender; + private Integer graduate; + private List loginInfos; + private Integer createTime; + private List guardians; + private String pk; + private Integer ttl; + private String _rid; + private String _self; + private String _etag; + private String _attachments; + private Integer _ts; + +} + diff --git a/src/main/java/cn/teammodel/service/EvaluationTreeService.java b/src/main/java/cn/teammodel/service/EvaluationTreeService.java new file mode 100644 index 0000000..8caf3bd --- /dev/null +++ b/src/main/java/cn/teammodel/service/EvaluationTreeService.java @@ -0,0 +1,13 @@ +package cn.teammodel.service; + +import cn.teammodel.model.entity.EvaluationTreeNode; + +import java.util.List; + +/** + * @author winter + * @create 2023-11-20 17:46 + */ +public interface EvaluationTreeService { + List buildTree(List nodes); +} diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationTreeServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationTreeServiceImpl.java new file mode 100644 index 0000000..ce46cd5 --- /dev/null +++ b/src/main/java/cn/teammodel/service/impl/EvaluationTreeServiceImpl.java @@ -0,0 +1,59 @@ +package cn.teammodel.service.impl; + +import cn.teammodel.model.entity.EvaluationTreeNode; +import cn.teammodel.service.EvaluationTreeService; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author winter + * @create 2023-11-20 17:47 + */ +public class EvaluationTreeServiceImpl implements EvaluationTreeService { + + @Override + public List buildTree(List nodes) { + List parents = new ArrayList<>(); + // pid 为 null 或者 "" 则为 parents + for (EvaluationTreeNode node : nodes) { + if (StringUtils.isBlank(node.getPid())) { + parents.add(node); + } + } + + // 迭代构建孩子节点 + for (EvaluationTreeNode parent : parents) { + buildChildren(parent, nodes); + } + return parents; + } + + /** + * 递归的构建父亲节点的孩子,以及孩子的孩子 (理论无极树,但应该考虑是否增加递归深度) + */ + private void buildChildren(EvaluationTreeNode parent, List nodes) { + List children = getChildren(parent, nodes); + if (children.size() > 0) return; + + parent.setChildren(children); + for (EvaluationTreeNode child : children) { + // 如果子节点还有孩子的话,就继续递归构建 + if (getChildren(child, nodes).size() > 0) { + buildChildren(child, nodes); + } + } + + } + + private List getChildren(EvaluationTreeNode parent, List nodes) { + List children = new ArrayList<>(); + for (EvaluationTreeNode node : nodes) { + if (node.getPid().equals(parent.getId())) { + children.add(node); + } + } + return children; + } +} diff --git a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java index 9064105..8a3ad41 100644 --- a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java +++ b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java @@ -1,19 +1,77 @@ package cn.teammodel; +import cn.teammodel.dao.EvaluationTreeRepository; +import cn.teammodel.dao.StudentRepository; import cn.teammodel.manager.DingAlertNotifier; +import cn.teammodel.model.entity.EvaluationTree; +import cn.teammodel.model.entity.EvaluationTreeNode; +import cn.teammodel.model.entity.Student; +import com.azure.cosmos.models.PartitionKey; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + @SpringBootTest class TeamModelExtensionApplicationTests { @Autowired private DingAlertNotifier notifier; + @Autowired + StudentRepository studentRepository; + @Autowired + private EvaluationTreeRepository evaluationTreeRepository; + @Test void contextLoads() { notifier.send("告警: 测试消息推送封装模块"); } + @Test + public void testCrud() { + EvaluationTree evaluationTree = new EvaluationTree(); + evaluationTree.setCode("evaluation"); + evaluationTree.setCampusId("default"); + List nodes = new ArrayList<>(); + + // 1 + String rootId = UUID.randomUUID().toString(); + EvaluationTreeNode node = new EvaluationTreeNode(); + node.setId(rootId); + node.setPid(null); + node.setName("root"); + node.setScore(0); + // 2 + EvaluationTreeNode node1 = new EvaluationTreeNode(); + String subId = UUID.randomUUID().toString(); + node.setId(subId); + node.setPid(rootId); + node.setName("child-1"); + node.setScore(0); + // 3 + EvaluationTreeNode node2 = new EvaluationTreeNode(); + node.setId(UUID.randomUUID().toString()); + node.setPid(subId); + node.setName("child-2"); + node.setScore(0); + nodes.add(node); + nodes.add(node1); + nodes.add(node2); + evaluationTree.setNodes(nodes); + + EvaluationTree saved = evaluationTreeRepository.save(evaluationTree); + System.out.println(saved); + } + + @Test + public void testSelect() { + Optional s = studentRepository.findById("24913", new PartitionKey("Base-hbcn")); + System.out.println(s.orElse(null)); + } + }