feat: 新增获取评价项树的接口

11111
winter 1 year ago
parent 46b88742d7
commit 1be6ca7e02

@ -49,6 +49,12 @@
<artifactId>spring-cloud-azure-starter-data-cosmos</artifactId> <artifactId>spring-cloud-azure-starter-data-cosmos</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>

@ -0,0 +1,13 @@
package cn.teammodel.common;
import com.azure.cosmos.models.PartitionKey;
/**
*
* @author winter
* @create 2023-11-22 15:31
*/
public interface PK {
PartitionKey PK_SCHOOL = new PartitionKey("School");
}

@ -0,0 +1,30 @@
package cn.teammodel.controller;
import cn.teammodel.common.R;
import cn.teammodel.model.dto.GetEvaluateTreeDto;
import cn.teammodel.model.entity.EvaluationTreeNode;
import cn.teammodel.service.EvaluationService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @author winter
* @create 2023-11-22 15:10
*/
@RestController
@RequestMapping("/public/evaluate")
public class EvaluationController {
@Resource
private EvaluationService evaluationService;
@PostMapping("getTrees")
public R<List<EvaluationTreeNode>> getEvaluateTree(@RequestBody GetEvaluateTreeDto getEvaluateTreeDto) {
List<EvaluationTreeNode> tree = evaluationService.getTree(getEvaluateTreeDto);
return R.success(tree);
}
}

@ -1,7 +1,7 @@
package cn.teammodel.controller; package cn.teammodel.controller;
import cn.teammodel.common.R; import cn.teammodel.common.R;
import cn.teammodel.dao.EvaluationTreeRepository; import cn.teammodel.dao.EvaluationRepository;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -15,7 +15,7 @@ import javax.annotation.Resource;
public class HelloController { public class HelloController {
@Resource @Resource
private EvaluationTreeRepository evaluationTreeRepository; private EvaluationRepository evaluationRepository;
@GetMapping("hello") @GetMapping("hello")
@PreAuthorize("@ss.hasRole('admin')") @PreAuthorize("@ss.hasRole('admin')")

@ -1,9 +1,8 @@
package cn.teammodel.dao; package cn.teammodel.dao;
import cn.teammodel.model.entity.EvaluationTree; import cn.teammodel.model.entity.Evaluation;
import com.azure.cosmos.models.PartitionKey;
import com.azure.spring.data.cosmos.repository.CosmosRepository; 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; import org.springframework.stereotype.Repository;
/** /**
@ -17,6 +16,9 @@ import org.springframework.stereotype.Repository;
* List<User> findByLastname(String lastname, Pageable pageable);<br/> * List<User> findByLastname(String lastname, Pageable pageable);<br/>
*/ */
@Repository @Repository
public interface EvaluationTreeRepository extends CosmosRepository<EvaluationTree, String> { public interface EvaluationRepository extends CosmosRepository<Evaluation, String> {
Page<EvaluationTree> findById(String lastname, Pageable pageable); Evaluation findBySchoolId(String schoolId, PartitionKey partitionKey);
Evaluation findBySchoolIdAndPeriodId(String schoolId,String periodId, PartitionKey partitionKey);
} }

@ -0,0 +1,14 @@
package cn.teammodel.model.dto;
import lombok.Data;
/**
* @author winter
* @create 2023-11-22 15:21
*/
@Data
public class GetEvaluateTreeDto {
// todo: 校验非空
String schoolId;
String periodId;
}

@ -17,20 +17,24 @@ import java.util.List;
@Container(containerName = "School") @Container(containerName = "School")
@Data @Data
@ToString @ToString
public class EvaluationTree { public class Evaluation {
@Id @Id
@GeneratedValue @GeneratedValue
String id; private String id;
/** /**
* : evaluation * : evaluation
*/ */
@PartitionKey @PartitionKey
String code; private String code;
/**
* Id
*/
private String schoolId;
/** /**
* id * id
*/ */
String campusId; private String periodId;
List<EvaluationTreeNode> nodes; private List<EvaluationTreeNode> nodes;
} }

@ -1,7 +1,7 @@
package cn.teammodel.model.entity; package cn.teammodel.model.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import org.springframework.data.annotation.Transient;
import java.util.List; import java.util.List;
@ -10,7 +10,20 @@ public class EvaluationTreeNode {
String id; String id;
String pid; String pid;
String name; String name;
String logo;
String creator;
/**
*
*/
String order;
/**
*
*/
Integer score; Integer score;
@Transient /**
* ? true
*/
boolean isPraise = true;
@JsonIgnore
List<EvaluationTreeNode> children; List<EvaluationTreeNode> children;
} }

@ -0,0 +1,30 @@
package cn.teammodel.service;
import cn.teammodel.model.dto.GetEvaluateTreeDto;
import cn.teammodel.model.entity.EvaluationTreeNode;
import java.util.List;
/**
* @author winter
* @create 2023-11-20 17:46
*/
public interface EvaluationService {
List<EvaluationTreeNode> getTree(GetEvaluateTreeDto getEvaluateTreeDto);
/**
*
*/
List<EvaluationTreeNode> buildTree(List<EvaluationTreeNode> nodes);
/**
*
* @param trees:
* @param nodes:
* @return: void
* @author: winter
* @date: 2023/11/21 16:40
* @description:
*/
void flattenTree(List<EvaluationTreeNode> trees, List<EvaluationTreeNode> nodes);
}

@ -1,13 +0,0 @@
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);
}

@ -1,9 +1,16 @@
package cn.teammodel.service.impl; package cn.teammodel.service.impl;
import cn.teammodel.common.PK;
import cn.teammodel.dao.EvaluationRepository;
import cn.teammodel.model.dto.GetEvaluateTreeDto;
import cn.teammodel.model.entity.Evaluation;
import cn.teammodel.model.entity.EvaluationTreeNode; import cn.teammodel.model.entity.EvaluationTreeNode;
import cn.teammodel.service.EvaluationTreeService; import cn.teammodel.service.EvaluationService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -11,7 +18,23 @@ import java.util.List;
* @author winter * @author winter
* @create 2023-11-20 17:47 * @create 2023-11-20 17:47
*/ */
public class EvaluationTreeServiceImpl implements EvaluationTreeService { @Service
public class EvaluationServiceImpl implements EvaluationService {
@Resource
private EvaluationRepository evaluationRepository;
@Override
public List<EvaluationTreeNode> getTree(GetEvaluateTreeDto getEvaluateTreeDto) {
String schoolId = getEvaluateTreeDto.getSchoolId();
String periodId = getEvaluateTreeDto.getPeriodId();
periodId = StringUtils.isEmpty(periodId) ? "default" : periodId;
Evaluation evaluation = evaluationRepository.findBySchoolIdAndPeriodId(schoolId, periodId, PK.PK_SCHOOL);
List<EvaluationTreeNode> nodes = evaluation.getNodes();
List<EvaluationTreeNode> trees = this.buildTree(nodes);
evaluation.setNodes(trees);
return trees;
}
@Override @Override
public List<EvaluationTreeNode> buildTree(List<EvaluationTreeNode> nodes) { public List<EvaluationTreeNode> buildTree(List<EvaluationTreeNode> nodes) {
@ -22,7 +45,6 @@ public class EvaluationTreeServiceImpl implements EvaluationTreeService {
parents.add(node); parents.add(node);
} }
} }
// 迭代构建孩子节点 // 迭代构建孩子节点
for (EvaluationTreeNode parent : parents) { for (EvaluationTreeNode parent : parents) {
buildChildren(parent, nodes); buildChildren(parent, nodes);
@ -30,6 +52,21 @@ public class EvaluationTreeServiceImpl implements EvaluationTreeService {
return parents; return parents;
} }
@Override
public void flattenTree(List<EvaluationTreeNode> trees, List<EvaluationTreeNode> nodes) {
// 递归结束条件
if (ObjectUtils.isEmpty(trees)) return;
// 这里的每个 tree 都是组装好的树,区别于单个节点
for (EvaluationTreeNode tree : trees) {
// 递归
flattenTree(tree.getChildren(), nodes);
// 回溯时删除 children,添加进列表
tree.setChildren(null);
nodes.add(tree);
}
}
/** /**
* , (,) * , (,)
*/ */

@ -4,7 +4,7 @@ spring:
cosmos: cosmos:
endpoint: https://cdhabookdep-free.documents.azure.cn:443 endpoint: https://cdhabookdep-free.documents.azure.cn:443
database: TEAMModelOS database: TEAMModelOS
key: v07dMthUjNk8AbwI8698AXHPbXBaaADgtgdYf4sFGXohei6aD2doq6XV45sLMGpDtDDpENAnlGkEUBu9RaAhpg== key: JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A==
security: security:
oauth2: oauth2:

@ -1,11 +1,13 @@
package cn.teammodel; package cn.teammodel;
import cn.teammodel.dao.EvaluationTreeRepository; import cn.hutool.json.JSONUtil;
import cn.teammodel.dao.EvaluationRepository;
import cn.teammodel.dao.StudentRepository; import cn.teammodel.dao.StudentRepository;
import cn.teammodel.manager.DingAlertNotifier; import cn.teammodel.manager.DingAlertNotifier;
import cn.teammodel.model.entity.EvaluationTree; import cn.teammodel.model.entity.Evaluation;
import cn.teammodel.model.entity.EvaluationTreeNode; import cn.teammodel.model.entity.EvaluationTreeNode;
import cn.teammodel.model.entity.Student; import cn.teammodel.service.EvaluationService;
import cn.teammodel.service.impl.EvaluationServiceImpl;
import com.azure.cosmos.models.PartitionKey; import com.azure.cosmos.models.PartitionKey;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -13,7 +15,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@SpringBootTest @SpringBootTest
@ -25,7 +26,7 @@ class TeamModelExtensionApplicationTests {
@Autowired @Autowired
StudentRepository studentRepository; StudentRepository studentRepository;
@Autowired @Autowired
private EvaluationTreeRepository evaluationTreeRepository; private EvaluationRepository evaluationRepository;
@Test @Test
void contextLoads() { void contextLoads() {
@ -34,9 +35,9 @@ class TeamModelExtensionApplicationTests {
@Test @Test
public void testCrud() { public void testCrud() {
EvaluationTree evaluationTree = new EvaluationTree(); Evaluation evaluation = new Evaluation();
evaluationTree.setCode("evaluation"); evaluation.setCode("evaluation");
evaluationTree.setCampusId("default"); evaluation.setPeriodId("default");
List<EvaluationTreeNode> nodes = new ArrayList<>(); List<EvaluationTreeNode> nodes = new ArrayList<>();
// 1 // 1
@ -62,16 +63,25 @@ class TeamModelExtensionApplicationTests {
nodes.add(node); nodes.add(node);
nodes.add(node1); nodes.add(node1);
nodes.add(node2); nodes.add(node2);
evaluationTree.setNodes(nodes); evaluation.setNodes(nodes);
EvaluationTree saved = evaluationTreeRepository.save(evaluationTree); Evaluation saved = evaluationRepository.save(evaluation);
System.out.println(saved); System.out.println(saved);
} }
@Test @Test
public void testSelect() { public void testSelect() {
Optional<Student> s = studentRepository.findById("24913", new PartitionKey("Base-hbcn")); Evaluation saved = evaluationRepository.findBySchoolId("hbcn", new PartitionKey("evaluation"));
System.out.println(s.orElse(null)); EvaluationService service = new EvaluationServiceImpl();
System.out.println(JSONUtil.parse(service.buildTree(saved.getNodes())).toStringPretty());
}
@Test
public void testUpdate() {
//EvaluationTree saved = evaluationTreeRepository.findBySchoolId("hbcn", new PartitionKey("evaluation"));
Evaluation saved = evaluationRepository.findBySchoolId("hbcn", new PartitionKey("evaluation"));
EvaluationService service = new EvaluationServiceImpl();
System.out.println(JSONUtil.parse(service.buildTree(saved.getNodes())).toStringPretty());
evaluationRepository.save(saved);
} }
} }

@ -1,10 +1,9 @@
package cn.teammodel; package cn.teammodel;
import cn.teammodel.common.R; import cn.hutool.json.JSONUtil;
import cn.teammodel.model.entity.EvaluationTree; import cn.teammodel.model.entity.Evaluation;
import cn.teammodel.model.entity.EvaluationTreeNode; import cn.teammodel.model.entity.EvaluationTreeNode;
import cn.teammodel.model.entity.User; import cn.teammodel.service.impl.EvaluationServiceImpl;
import cn.teammodel.service.impl.EvaluationTreeServiceImpl;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.request.OapiRobotSendRequest;
@ -88,12 +87,18 @@ public class TestWithoutSpring {
} }
@Test @Test
public void testResult() { public void testResult() {
EvaluationTreeServiceImpl service = new EvaluationTreeServiceImpl(); EvaluationServiceImpl service = new EvaluationServiceImpl();
EvaluationTree evaluationTree = new EvaluationTree(); Evaluation evaluation = new Evaluation();
evaluationTree.setCode("evaluation"); evaluation.setCode("evaluation");
evaluationTree.setCampusId("default"); evaluation.setPeriodId("default");
List<EvaluationTreeNode> nodes = new ArrayList<>(); List<EvaluationTreeNode> nodes = new ArrayList<>();
String root2Id = UUID.randomUUID().toString();
EvaluationTreeNode root1 = new EvaluationTreeNode();
root1.setId(root2Id);
root1.setPid(null);
root1.setName("root1");
root1.setScore(0);
// 1 // 1
String rootId = UUID.randomUUID().toString(); String rootId = UUID.randomUUID().toString();
EvaluationTreeNode node = new EvaluationTreeNode(); EvaluationTreeNode node = new EvaluationTreeNode();
@ -101,6 +106,8 @@ public class TestWithoutSpring {
node.setPid(null); node.setPid(null);
node.setName("root"); node.setName("root");
node.setScore(0); node.setScore(0);
// 2 // 2
EvaluationTreeNode node1 = new EvaluationTreeNode(); EvaluationTreeNode node1 = new EvaluationTreeNode();
String subId = UUID.randomUUID().toString(); String subId = UUID.randomUUID().toString();
@ -115,11 +122,16 @@ public class TestWithoutSpring {
node2.setName("child-2"); node2.setName("child-2");
node2.setScore(0); node2.setScore(0);
nodes.add(node); nodes.add(node);
nodes.add(root1);
nodes.add(node1); nodes.add(node1);
nodes.add(node2); nodes.add(node2);
evaluationTree.setNodes(nodes); evaluation.setNodes(nodes);
List<EvaluationTreeNode> tree = service.buildTree(nodes); List<EvaluationTreeNode> tree = service.buildTree(nodes);
System.out.println(tree); System.out.println(JSONUtil.parse(service.buildTree(tree)).toStringPretty());
List<EvaluationTreeNode> nodeList = new ArrayList<>();
service.flattenTree(tree, nodeList);
System.out.println(JSONUtil.parse(nodeList).toStringPretty());
} }
} }

Loading…
Cancel
Save