From c34e87ca0d1bbede1e06e63fca97452b1d0400c5 Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Thu, 30 Nov 2023 17:53:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E8=AF=84=E4=BB=B7?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=A8=A1=E6=9D=BF=E6=9B=BF=E6=8D=A2=E4=B8=8E?= =?UTF-8?q?=E8=8A=82=E7=82=B9=20ID=20=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AppraiseController.java | 10 ++- .../dao/AppraiseRecordRepository.java | 17 ++++- .../cn/teammodel/dao/AppraiseRepository.java | 6 ++ .../cn/teammodel/dao/SchoolRepository.java | 2 + .../model/dto/Appraise/FindVoteRecordDto.java | 20 ++--- .../model/entity/appraise/Appraise.java | 2 +- .../teammodel/service/EvaluationService.java | 3 +- .../service/impl/EvaluationServiceImpl.java | 73 ++++++++++++++++--- .../cn/teammodel/utils/RepositoryUtil.java | 2 +- .../TeamModelExtensionApplicationTests.java | 21 +++--- 10 files changed, 116 insertions(+), 40 deletions(-) diff --git a/src/main/java/cn/teammodel/controller/AppraiseController.java b/src/main/java/cn/teammodel/controller/AppraiseController.java index 91c3d3e..840fde7 100644 --- a/src/main/java/cn/teammodel/controller/AppraiseController.java +++ b/src/main/java/cn/teammodel/controller/AppraiseController.java @@ -3,6 +3,7 @@ package cn.teammodel.controller; import cn.teammodel.common.R; import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.entity.appraise.Appraise; +import cn.teammodel.model.entity.appraise.AppraiseRecordItem; import cn.teammodel.service.EvaluationService; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.PostMapping; @@ -12,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.validation.Valid; +import java.util.List; /** * @author winter @@ -57,10 +59,10 @@ public class AppraiseController { } @PostMapping("findVoteRecord") - @ApiOperation(value = "多条件查询学生评价(投票)") - public R findVoteRecord(@Valid @RequestBody FindVoteRecordDto findVoteRecordDto) { - evaluationService.findVoteRecord(findVoteRecordDto); - return R.success("评价成功"); + @ApiOperation(value = "多条件查询当前登录老师的学生评价(投票)") + public R> findMyVoteRecord(@Valid @RequestBody FindVoteRecordDto findVoteRecordDto) { + List res = evaluationService.findVoteRecord(findVoteRecordDto); + return R.success(res); } diff --git a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java index 5f41437..d5a3c97 100644 --- a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java +++ b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java @@ -1,9 +1,9 @@ package cn.teammodel.dao; import cn.teammodel.model.entity.appraise.AppraiseRecord; +import cn.teammodel.model.entity.appraise.AppraiseRecordItem; import com.azure.spring.data.cosmos.repository.CosmosRepository; import com.azure.spring.data.cosmos.repository.Query; -import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -26,5 +26,18 @@ public interface AppraiseRecordRepository extends CosmosRepository findScoreAndPraise(@Param("targetId") String targetId,@Param("academicYearId") String academicYearId,@Param("code") String code); + List findScoreAndPraise(String targetId,String academicYearId,String code); + + /** + * 条件查询 + */ + @Query("select value n from Student as c join n in c.nodes where " + + "(IS_NULL(@targetId) or c.targetId = @targetId) and " + + "(IS_NULL(@targetType) or c.targetType = @targetType) and " + + "(IS_NULL(@creatorId) or n.creatorId = @creatorId) and " + + "(IS_NULL(@academicYearId) or c.academicYearId = @academicYearId) and " + + "c.code = @code") + List searchNodesByCondition(String targetId, String targetType, String classId, String creatorId, String academicYearId, String code); + + } diff --git a/src/main/java/cn/teammodel/dao/AppraiseRepository.java b/src/main/java/cn/teammodel/dao/AppraiseRepository.java index fa2cd47..f2aa592 100644 --- a/src/main/java/cn/teammodel/dao/AppraiseRepository.java +++ b/src/main/java/cn/teammodel/dao/AppraiseRepository.java @@ -24,6 +24,12 @@ import java.util.List; public interface AppraiseRepository extends CosmosRepository { Appraise findBySchoolId(String schoolId, PartitionKey partitionKey); Appraise findAppraiseBySchoolIdAndPeriodIdAndCode(String schoolId, String periodId, String code); + + /** + * 获取模板树 + */ + @Query("SELECT * FROM School AS s where s.code = 'Appraise' and s.schoolId = 'template'") + List findTemplateTree(); @Query("SELECT value n FROM School AS s join n in s.nodes where s.code = @code and n.id = @nodeId") List findNodeById(@Param("code") String code, @Param("nodeId") String nodeId); diff --git a/src/main/java/cn/teammodel/dao/SchoolRepository.java b/src/main/java/cn/teammodel/dao/SchoolRepository.java index 4f6f066..571957c 100644 --- a/src/main/java/cn/teammodel/dao/SchoolRepository.java +++ b/src/main/java/cn/teammodel/dao/SchoolRepository.java @@ -19,4 +19,6 @@ public interface SchoolRepository extends CosmosRepository { */ @Query("select value p from School as s join p in s.period where s.id = @schoolId and s.code = 'Base' and p.id = @periodId") List findPeriodById(@Param("schoolId") String schoolId, @Param("periodId") String periodId); + @Query("select value p.semesters from School as s join p in s.period where s.id = @schoolId and s.code = 'Base' and p.id = @periodId") + List findSemestersById(@Param("schoolId") String schoolId, @Param("periodId") String periodId); } diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java index 80c79b6..b8c86eb 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/FindVoteRecordDto.java @@ -11,23 +11,17 @@ import javax.validation.constraints.NotNull; */ @Data public class FindVoteRecordDto { + @NotNull - @ApiModelProperty(value = "班级或学生 id") + @ApiModelProperty(value = "必要参数,用于获取学年") + private String periodId; + + @ApiModelProperty(value = "班级或学生 id, 注意: 无参则默认获取登录老师在该学年下评价的所有记录") private String targetId; - @NotNull @ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class") private String targetType; - // 班级 id - /** - * 可以当作唯一 id - */ - @NotNull - @ApiModelProperty(value = "评价项唯一 id", required = true) - private String appraiseId; - - @ApiModelProperty(value = "学段 id,用于拿到 semesterId", required = true) - @NotNull - private String periodId; + @ApiModelProperty(value = "按班级 id搜索") + private String classId; } diff --git a/src/main/java/cn/teammodel/model/entity/appraise/Appraise.java b/src/main/java/cn/teammodel/model/entity/appraise/Appraise.java index e57452e..5396291 100644 --- a/src/main/java/cn/teammodel/model/entity/appraise/Appraise.java +++ b/src/main/java/cn/teammodel/model/entity/appraise/Appraise.java @@ -23,7 +23,7 @@ public class Appraise extends BaseItem { */ private String schoolId; /** - * 学段 id + * 学段 id (默认 default, 模板为 template) */ private String periodId; diff --git a/src/main/java/cn/teammodel/service/EvaluationService.java b/src/main/java/cn/teammodel/service/EvaluationService.java index 08b6060..b20c53e 100644 --- a/src/main/java/cn/teammodel/service/EvaluationService.java +++ b/src/main/java/cn/teammodel/service/EvaluationService.java @@ -2,6 +2,7 @@ package cn.teammodel.service; import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.entity.appraise.Appraise; +import cn.teammodel.model.entity.appraise.AppraiseRecordItem; import cn.teammodel.model.entity.appraise.AppraiseTreeNode; import java.util.List; @@ -48,5 +49,5 @@ public interface EvaluationService { */ void vote(AppraiseVoteDto appraiseVoteDto); - void findVoteRecord(FindVoteRecordDto findVoteRecordDto); + List findVoteRecord(FindVoteRecordDto findVoteRecordDto); } diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index af45089..f83b27b 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -19,7 +19,6 @@ import cn.teammodel.service.EvaluationService; import cn.teammodel.utils.RepositoryUtil; import cn.teammodel.utils.SchoolDateUtil; import com.azure.cosmos.models.CosmosPatchOperations; -import com.azure.spring.data.cosmos.core.CosmosTemplate; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; @@ -31,6 +30,8 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * @author winter @@ -44,8 +45,6 @@ public class EvaluationServiceImpl implements EvaluationService { private final static String TARGET_STUDENT = "student"; private final static String TARGET_CLASS = "class"; @Resource - CosmosTemplate cosmosTemplate; - @Resource private SchoolRepository schoolRepository; @Resource private AppraiseRecordRepository appraiseRecordRepository; @@ -72,11 +71,49 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public Appraise getTree(GetEvaluateTreeDto getEvaluateTreeDto) { - Appraise appraise = findAppraise(getEvaluateTreeDto.getPeriodId()); + String periodId = getEvaluateTreeDto.getPeriodId(); + // 默认学区为 default + periodId = StringUtils.isEmpty(periodId) ? "default" : periodId; + User loginUser = SecurityUtil.getLoginUser(); + String schoolId = loginUser.getSchoolId(); + Appraise appraise = appraiseRepository.findAppraiseBySchoolIdAndPeriodIdAndCode(schoolId, periodId, PK.PK_APPRAISE); + // todo: 是否要对学段进行鉴权 + if (appraise != null) { + return this.buildTree(appraise); + } + // 如果传参 period 所在没有 tree 的话,则复制一份模板 tree 给他 (处理节点 id) + appraise = RepositoryUtil.findOne(appraiseRepository.findTemplateTree(), "获取模板评价树失败"); + if (appraise == null) { + throw new ServiceException(); + } + refreshAppraiseTree(appraise.getNodes()); + appraise.setPeriodId(periodId); + appraise.setSchoolId(schoolId); + appraise.setId(null); + appraise = appraiseRepository.save(appraise); return this.buildTree(appraise); } + /** + * 将树的节点的 id 进行替换 + */ + private void refreshAppraiseTree(List nodes) { + List children = nodes.stream().filter(item -> item.getPid() != null).collect(Collectors.toList()); + // 将非 root 的 nodes 通过 pid 收集成 list, 再遍历 nodes, 将其每一个 id 对应 map 中的 list 的 pid修改成新的 id 即可 + Map> pidNodeMap = children.stream().collect(Collectors.groupingBy(AppraiseTreeNode::getPid)); + nodes.forEach(item -> { + String newId = UUID.randomUUID().toString(); + String oldId = item.getId(); + List appraiseTreeNodes = pidNodeMap.get(oldId); + if (appraiseTreeNodes != null) { + appraiseTreeNodes.forEach(node -> node.setPid(newId)); + } + item.setId(newId); + }); + + } + @Override public Appraise buildTree(Appraise appraise) { if (appraise == null) { @@ -208,17 +245,17 @@ public class EvaluationServiceImpl implements EvaluationService { // 获取评价项节点 List nodes = appraiseRepository.findNodeById(PK.PK_APPRAISE, appraiseId); - AppraiseTreeNode appraiseTreeNode = RepositoryUtil.findOne(nodes); + AppraiseTreeNode appraiseTreeNode = RepositoryUtil.findOne(nodes, "获取评价项失败"); // 通过 periodId 获取 semesters List periodById = schoolRepository.findPeriodById(schoolId, periodId); - School.Period period = RepositoryUtil.findOne(periodById); + School.Period period = RepositoryUtil.findOne(periodById, "获取学段失败"); List semesters = period.getSemesters(); // 获取当前学年学期组合 ID String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now()); - // 查询是否存在记录,不存在则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item + // 查询是否存在记录 AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndAcademicYearIdAndCode( targetId, academicYearId, @@ -233,8 +270,8 @@ public class EvaluationServiceImpl implements EvaluationService { item.setCreatorId(loginUser.getId()); item.setCreateTime(LocalDateTime.now()); + // 不存在则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item if (record == null) { - // 不存在, 创建新的 AppraiseRecord List items = Collections.singletonList(item); record = new AppraiseRecord(); @@ -270,8 +307,26 @@ public class EvaluationServiceImpl implements EvaluationService { } @Override - public void findVoteRecord(FindVoteRecordDto findVoteRecordDto) { + public List findVoteRecord(FindVoteRecordDto findVoteRecordDto) { + String periodId = findVoteRecordDto.getTargetId(); + String targetId = findVoteRecordDto.getTargetId(); + String targetType = findVoteRecordDto.getTargetType(); + String classId = findVoteRecordDto.getClassId(); + User loginUser = SecurityUtil.getLoginUser(); + String teacherId = loginUser.getId(); + String schoolId = loginUser.getSchoolId(); + List semesters = schoolRepository.findSemestersById(schoolId, periodId); + String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now()); + List appraiseRecordItems = appraiseRecordRepository.searchNodesByCondition( + targetId, + targetType, + classId, + teacherId, + academicYearId, + String.format(PK.PK_APPRAISE_RECORD, schoolId) + ); + return appraiseRecordItems; } /** diff --git a/src/main/java/cn/teammodel/utils/RepositoryUtil.java b/src/main/java/cn/teammodel/utils/RepositoryUtil.java index b6db971..2adf3af 100644 --- a/src/main/java/cn/teammodel/utils/RepositoryUtil.java +++ b/src/main/java/cn/teammodel/utils/RepositoryUtil.java @@ -16,7 +16,7 @@ public class RepositoryUtil { /** * 获取唯一一个结果,如果list为空或者list长度大于1,抛出参数错误异常 */ - public static T findOne(List list) { + public static T findOne(List list, String errorMsg) { if (ObjectUtils.isEmpty(list) || list.size() > 1) { log.error("查询结果为空或数量 > 1"); throw new ServiceException(ErrorCode.PARAMS_ERROR); diff --git a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java index e20230b..55d3466 100644 --- a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java +++ b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java @@ -1,17 +1,14 @@ package cn.teammodel; -import cn.teammodel.common.PK; import cn.teammodel.dao.AppraiseRecordRepository; import cn.teammodel.dao.AppraiseRepository; import cn.teammodel.dao.SchoolRepository; import cn.teammodel.dao.StudentRepository; import cn.teammodel.manager.DingAlertNotifier; import cn.teammodel.model.entity.appraise.Appraise; -import cn.teammodel.model.entity.appraise.AppraiseRecord; import cn.teammodel.model.entity.appraise.AppraiseTreeNode; import cn.teammodel.service.EvaluationService; import cn.teammodel.service.impl.EvaluationServiceImpl; -import cn.teammodel.utils.RepositoryUtil; import com.azure.cosmos.models.PartitionKey; import com.azure.spring.data.cosmos.core.CosmosTemplate; import org.junit.jupiter.api.Test; @@ -89,12 +86,18 @@ class TeamModelExtensionApplicationTests { //List nodeById = appraiseRepository.findNodeById("habook", "default", "Appraise", "43e23f03-288c-4012-b5f3-4d5c022739a2"); //System.out.println(nodeById); - //System.out.println(schoolRepository.findSchoolByIdAndCode("hbcn", "Base", Semester.class)); - List record = appraiseRecordRepository.findScoreAndPraise("fakeStudentId1", - "2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017", - String.format(PK.PK_APPRAISE_RECORD,"habook") - ); - System.out.println(RepositoryUtil.findOne(record)); + System.out.println(schoolRepository.findSemestersById("hbcn", "be32942d-97a9-52ba-45d6-2e5b722583f5")); + // 评价记录 + //List record = appraiseRecordRepository.findScoreAndPraise("fakeStudentId1", + // "2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017", + // String.format(PK.PK_APPRAISE_RECORD,"habook") + //); + //System.out.println(RepositoryUtil.findOne(record)); + //List record = appraiseRecordRepository.searchNodesByCondition(null, + // null, null,"1595321354","2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017", + // String.format(PK.PK_APPRAISE_RECORD,"habook") + //); + //System.out.println(record); } @Test public void testUpdate() {