parent
a16e8f8de8
commit
676f6fc46b
@ -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<String> 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<String> free() {
|
||||
return new R(200, "success","hello world");
|
||||
}
|
||||
}
|
@ -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 <br/>
|
||||
* Page<User> findByLastname(String lastname, Pageable pageable); <br/>
|
||||
* Slice<User> findByLastname(String lastname, Pageable pageable);<br/>
|
||||
* Window<User> findTop10ByLastname(String lastname, ScrollPosition position, Sort sort);<br/>
|
||||
* List<User> findByLastname(String lastname, Sort sort);<br/>
|
||||
* List<User> findByLastname(String lastname, Sort sort, Limit limit);<br/>
|
||||
* List<User> findByLastname(String lastname, Pageable pageable);<br/>
|
||||
*/
|
||||
@Repository
|
||||
public interface EvaluationTreeRepository extends CosmosRepository<EvaluationTree, String> {
|
||||
Page<EvaluationTree> findById(String lastname, Pageable pageable);
|
||||
}
|
@ -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<Student, String> {
|
||||
|
||||
//Flux<Student> findById(String 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 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<EvaluationTreeNode> nodes;
|
||||
|
||||
}
|
@ -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<EvaluationTreeNode> children;
|
||||
}
|
@ -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<String> loginInfos;
|
||||
private Integer createTime;
|
||||
private List<String> guardians;
|
||||
private String pk;
|
||||
private Integer ttl;
|
||||
private String _rid;
|
||||
private String _self;
|
||||
private String _etag;
|
||||
private String _attachments;
|
||||
private Integer _ts;
|
||||
|
||||
}
|
||||
|
@ -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<EvaluationTreeNode> buildTree(List<EvaluationTreeNode> nodes);
|
||||
}
|
@ -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<EvaluationTreeNode> buildTree(List<EvaluationTreeNode> nodes) {
|
||||
List<EvaluationTreeNode> 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<EvaluationTreeNode> nodes) {
|
||||
List<EvaluationTreeNode> 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<EvaluationTreeNode> getChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) {
|
||||
List<EvaluationTreeNode> children = new ArrayList<>();
|
||||
for (EvaluationTreeNode node : nodes) {
|
||||
if (node.getPid().equals(parent.getId())) {
|
||||
children.add(node);
|
||||
}
|
||||
}
|
||||
return children;
|
||||
}
|
||||
}
|
@ -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<EvaluationTreeNode> 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<Student> s = studentRepository.findById("24913", new PartitionKey("Base-hbcn"));
|
||||
System.out.println(s.orElse(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue