feat: 新增评价树的模板替换与节点 ID 刷新

11111
winter 1 year ago
parent 18cfb50a46
commit c34e87ca0d

@ -3,6 +3,7 @@ package cn.teammodel.controller;
import cn.teammodel.common.R; import cn.teammodel.common.R;
import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.dto.Appraise.*;
import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseRecordItem;
import cn.teammodel.service.EvaluationService; import cn.teammodel.service.EvaluationService;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -12,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
/** /**
* @author winter * @author winter
@ -57,10 +59,10 @@ public class AppraiseController {
} }
@PostMapping("findVoteRecord") @PostMapping("findVoteRecord")
@ApiOperation(value = "多条件查询学生评价(投票)") @ApiOperation(value = "多条件查询当前登录老师的学生评价(投票)")
public R<String> findVoteRecord(@Valid @RequestBody FindVoteRecordDto findVoteRecordDto) { public R<List<AppraiseRecordItem>> findMyVoteRecord(@Valid @RequestBody FindVoteRecordDto findVoteRecordDto) {
evaluationService.findVoteRecord(findVoteRecordDto); List<AppraiseRecordItem> res = evaluationService.findVoteRecord(findVoteRecordDto);
return R.success("评价成功"); return R.success(res);
} }

@ -1,9 +1,9 @@
package cn.teammodel.dao; package cn.teammodel.dao;
import cn.teammodel.model.entity.appraise.AppraiseRecord; 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.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query; import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
@ -26,5 +26,18 @@ public interface AppraiseRecordRepository extends CosmosRepository<AppraiseRecor
*/ */
AppraiseRecord findAppraiseRecordByTargetIdAndAcademicYearIdAndCode(String targetId, String academicYearId, String code); AppraiseRecord findAppraiseRecordByTargetIdAndAcademicYearIdAndCode(String targetId, String academicYearId, String code);
@Query("select c.id, c.praiseCount, c.score from Student as c where c.targetId = @targetId and c.academicYearId = @academicYearId and c.code = @code") @Query("select c.id, c.praiseCount, c.score from Student as c where c.targetId = @targetId and c.academicYearId = @academicYearId and c.code = @code")
List<AppraiseRecord> findScoreAndPraise(@Param("targetId") String targetId,@Param("academicYearId") String academicYearId,@Param("code") String code); List<AppraiseRecord> 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<AppraiseRecordItem> searchNodesByCondition(String targetId, String targetType, String classId, String creatorId, String academicYearId, String code);
} }

@ -24,6 +24,12 @@ import java.util.List;
public interface AppraiseRepository extends CosmosRepository<Appraise, String> { public interface AppraiseRepository extends CosmosRepository<Appraise, String> {
Appraise findBySchoolId(String schoolId, PartitionKey partitionKey); Appraise findBySchoolId(String schoolId, PartitionKey partitionKey);
Appraise findAppraiseBySchoolIdAndPeriodIdAndCode(String schoolId, String periodId, String code); Appraise findAppraiseBySchoolIdAndPeriodIdAndCode(String schoolId, String periodId, String code);
/**
*
*/
@Query("SELECT * FROM School AS s where s.code = 'Appraise' and s.schoolId = 'template'")
List<Appraise> findTemplateTree();
@Query("SELECT value n FROM School AS s join n in s.nodes where s.code = @code and n.id = @nodeId") @Query("SELECT value n FROM School AS s join n in s.nodes where s.code = @code and n.id = @nodeId")
List<AppraiseTreeNode> findNodeById(@Param("code") String code, @Param("nodeId") String nodeId); List<AppraiseTreeNode> findNodeById(@Param("code") String code, @Param("nodeId") String nodeId);

@ -19,4 +19,6 @@ public interface SchoolRepository extends CosmosRepository<School, String> {
*/ */
@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") @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<School.Period> findPeriodById(@Param("schoolId") String schoolId, @Param("periodId") String periodId); List<School.Period> 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<School.Semester> findSemestersById(@Param("schoolId") String schoolId, @Param("periodId") String periodId);
} }

@ -11,23 +11,17 @@ import javax.validation.constraints.NotNull;
*/ */
@Data @Data
public class FindVoteRecordDto { public class FindVoteRecordDto {
@NotNull @NotNull
@ApiModelProperty(value = "班级或学生 id") @ApiModelProperty(value = "必要参数,用于获取学年")
private String periodId;
@ApiModelProperty(value = "班级或学生 id, 注意: 无参则默认获取登录老师在该学年下评价的所有记录")
private String targetId; private String targetId;
@NotNull
@ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class") @ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class")
private String targetType; private String targetType;
// 班级 id @ApiModelProperty(value = "按班级 id搜索")
/** private String classId;
* id
*/
@NotNull
@ApiModelProperty(value = "评价项唯一 id", required = true)
private String appraiseId;
@ApiModelProperty(value = "学段 id,用于拿到 semesterId", required = true)
@NotNull
private String periodId;
} }

@ -23,7 +23,7 @@ public class Appraise extends BaseItem {
*/ */
private String schoolId; private String schoolId;
/** /**
* id * id ( default, template)
*/ */
private String periodId; private String periodId;

@ -2,6 +2,7 @@ package cn.teammodel.service;
import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.dto.Appraise.*;
import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseRecordItem;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode; import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import java.util.List; import java.util.List;
@ -48,5 +49,5 @@ public interface EvaluationService {
*/ */
void vote(AppraiseVoteDto appraiseVoteDto); void vote(AppraiseVoteDto appraiseVoteDto);
void findVoteRecord(FindVoteRecordDto findVoteRecordDto); List<AppraiseRecordItem> findVoteRecord(FindVoteRecordDto findVoteRecordDto);
} }

@ -19,7 +19,6 @@ import cn.teammodel.service.EvaluationService;
import cn.teammodel.utils.RepositoryUtil; import cn.teammodel.utils.RepositoryUtil;
import cn.teammodel.utils.SchoolDateUtil; import cn.teammodel.utils.SchoolDateUtil;
import com.azure.cosmos.models.CosmosPatchOperations; 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.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -31,6 +30,8 @@ import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author winter * @author winter
@ -44,8 +45,6 @@ public class EvaluationServiceImpl implements EvaluationService {
private final static String TARGET_STUDENT = "student"; private final static String TARGET_STUDENT = "student";
private final static String TARGET_CLASS = "class"; private final static String TARGET_CLASS = "class";
@Resource @Resource
CosmosTemplate cosmosTemplate;
@Resource
private SchoolRepository schoolRepository; private SchoolRepository schoolRepository;
@Resource @Resource
private AppraiseRecordRepository appraiseRecordRepository; private AppraiseRecordRepository appraiseRecordRepository;
@ -72,10 +71,48 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override @Override
public Appraise getTree(GetEvaluateTreeDto getEvaluateTreeDto) { 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); 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<AppraiseTreeNode> nodes) {
List<AppraiseTreeNode> children = nodes.stream().filter(item -> item.getPid() != null).collect(Collectors.toList());
// 将非 root 的 nodes 通过 pid 收集成 list, 再遍历 nodes, 将其每一个 id 对应 map 中的 list 的 pid修改成新的 id 即可
Map<String, List<AppraiseTreeNode>> pidNodeMap = children.stream().collect(Collectors.groupingBy(AppraiseTreeNode::getPid));
nodes.forEach(item -> {
String newId = UUID.randomUUID().toString();
String oldId = item.getId();
List<AppraiseTreeNode> appraiseTreeNodes = pidNodeMap.get(oldId);
if (appraiseTreeNodes != null) {
appraiseTreeNodes.forEach(node -> node.setPid(newId));
}
item.setId(newId);
});
}
@Override @Override
public Appraise buildTree(Appraise appraise) { public Appraise buildTree(Appraise appraise) {
@ -208,17 +245,17 @@ public class EvaluationServiceImpl implements EvaluationService {
// 获取评价项节点 // 获取评价项节点
List<AppraiseTreeNode> nodes = appraiseRepository.findNodeById(PK.PK_APPRAISE, appraiseId); List<AppraiseTreeNode> nodes = appraiseRepository.findNodeById(PK.PK_APPRAISE, appraiseId);
AppraiseTreeNode appraiseTreeNode = RepositoryUtil.findOne(nodes); AppraiseTreeNode appraiseTreeNode = RepositoryUtil.findOne(nodes, "获取评价项失败");
// 通过 periodId 获取 semesters // 通过 periodId 获取 semesters
List<School.Period> periodById = schoolRepository.findPeriodById(schoolId, periodId); List<School.Period> periodById = schoolRepository.findPeriodById(schoolId, periodId);
School.Period period = RepositoryUtil.findOne(periodById); School.Period period = RepositoryUtil.findOne(periodById, "获取学段失败");
List<School.Semester> semesters = period.getSemesters(); List<School.Semester> semesters = period.getSemesters();
// 获取当前学年学期组合 ID // 获取当前学年学期组合 ID
String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now()); String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now());
// 查询是否存在记录,不存在则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item // 查询是否存在记录
AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndAcademicYearIdAndCode( AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndAcademicYearIdAndCode(
targetId, targetId,
academicYearId, academicYearId,
@ -233,8 +270,8 @@ public class EvaluationServiceImpl implements EvaluationService {
item.setCreatorId(loginUser.getId()); item.setCreatorId(loginUser.getId());
item.setCreateTime(LocalDateTime.now()); item.setCreateTime(LocalDateTime.now());
// 不存在则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item
if (record == null) { if (record == null) {
// 不存在, 创建新的 AppraiseRecord
List<AppraiseRecordItem> items = Collections.singletonList(item); List<AppraiseRecordItem> items = Collections.singletonList(item);
record = new AppraiseRecord(); record = new AppraiseRecord();
@ -270,8 +307,26 @@ public class EvaluationServiceImpl implements EvaluationService {
} }
@Override @Override
public void findVoteRecord(FindVoteRecordDto findVoteRecordDto) { public List<AppraiseRecordItem> 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<School.Semester> semesters = schoolRepository.findSemestersById(schoolId, periodId);
String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now());
List<AppraiseRecordItem> appraiseRecordItems = appraiseRecordRepository.searchNodesByCondition(
targetId,
targetType,
classId,
teacherId,
academicYearId,
String.format(PK.PK_APPRAISE_RECORD, schoolId)
);
return appraiseRecordItems;
} }
/** /**

@ -16,7 +16,7 @@ public class RepositoryUtil {
/** /**
* ,listlist1, * ,listlist1,
*/ */
public static <T> T findOne(List<T> list) { public static <T> T findOne(List<T> list, String errorMsg) {
if (ObjectUtils.isEmpty(list) || list.size() > 1) { if (ObjectUtils.isEmpty(list) || list.size() > 1) {
log.error("查询结果为空或数量 > 1"); log.error("查询结果为空或数量 > 1");
throw new ServiceException(ErrorCode.PARAMS_ERROR); throw new ServiceException(ErrorCode.PARAMS_ERROR);

@ -1,17 +1,14 @@
package cn.teammodel; package cn.teammodel;
import cn.teammodel.common.PK;
import cn.teammodel.dao.AppraiseRecordRepository; import cn.teammodel.dao.AppraiseRecordRepository;
import cn.teammodel.dao.AppraiseRepository; import cn.teammodel.dao.AppraiseRepository;
import cn.teammodel.dao.SchoolRepository; import cn.teammodel.dao.SchoolRepository;
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.appraise.Appraise; import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseRecord;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode; import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import cn.teammodel.service.EvaluationService; import cn.teammodel.service.EvaluationService;
import cn.teammodel.service.impl.EvaluationServiceImpl; import cn.teammodel.service.impl.EvaluationServiceImpl;
import cn.teammodel.utils.RepositoryUtil;
import com.azure.cosmos.models.PartitionKey; import com.azure.cosmos.models.PartitionKey;
import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.CosmosTemplate;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -89,12 +86,18 @@ class TeamModelExtensionApplicationTests {
//List<AppraiseTreeNode> nodeById = appraiseRepository.findNodeById("habook", "default", "Appraise", "43e23f03-288c-4012-b5f3-4d5c022739a2"); //List<AppraiseTreeNode> nodeById = appraiseRepository.findNodeById("habook", "default", "Appraise", "43e23f03-288c-4012-b5f3-4d5c022739a2");
//System.out.println(nodeById); //System.out.println(nodeById);
//System.out.println(schoolRepository.findSchoolByIdAndCode("hbcn", "Base", Semester.class)); System.out.println(schoolRepository.findSemestersById("hbcn", "be32942d-97a9-52ba-45d6-2e5b722583f5"));
List<AppraiseRecord> record = appraiseRecordRepository.findScoreAndPraise("fakeStudentId1", // 评价记录
"2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017", //List<AppraiseRecord> record = appraiseRecordRepository.findScoreAndPraise("fakeStudentId1",
String.format(PK.PK_APPRAISE_RECORD,"habook") // "2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017",
); // String.format(PK.PK_APPRAISE_RECORD,"habook")
System.out.println(RepositoryUtil.findOne(record)); //);
//System.out.println(RepositoryUtil.findOne(record));
//List<AppraiseRecordItem> 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 @Test
public void testUpdate() { public void testUpdate() {

Loading…
Cancel
Save