diff --git a/src/main/java/cn/teammodel/common/PK.java b/src/main/java/cn/teammodel/common/PK.java index 4fcb4ec..69eec63 100644 --- a/src/main/java/cn/teammodel/common/PK.java +++ b/src/main/java/cn/teammodel/common/PK.java @@ -18,6 +18,11 @@ public interface PK { */ String PK_APPRAISE_RECORD = "AppraiseRecord-%s"; String COMMON_BASE = "Base"; + /** + * 学生,拼接学校 id + */ + String STUDENT = "Base-%s"; + String CLASS = "Class-%s"; /** * 构建分区键 diff --git a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java index 3819e50..7bac635 100644 --- a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java +++ b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java @@ -24,7 +24,7 @@ public interface AppraiseRecordRepository extends CosmosRepository findScoreAndPraise(String targetId,String academicYearId,String code); diff --git a/src/main/java/cn/teammodel/dao/ClassRepository.java b/src/main/java/cn/teammodel/dao/ClassRepository.java new file mode 100644 index 0000000..7b1a2bc --- /dev/null +++ b/src/main/java/cn/teammodel/dao/ClassRepository.java @@ -0,0 +1,15 @@ +package cn.teammodel.dao; + +import cn.teammodel.model.entity.school.ClassInfo; +import com.azure.spring.data.cosmos.repository.CosmosRepository; +import org.springframework.stereotype.Repository; + +/** + * @author winter + * @create 2023-12-01 16:37 + */ +@Repository +public interface ClassRepository extends CosmosRepository { + + ClassInfo findClassByIdAndCode(String targetId, String format); +} diff --git a/src/main/java/cn/teammodel/dao/StudentRepository.java b/src/main/java/cn/teammodel/dao/StudentRepository.java index 9531269..7d03de1 100644 --- a/src/main/java/cn/teammodel/dao/StudentRepository.java +++ b/src/main/java/cn/teammodel/dao/StudentRepository.java @@ -1,11 +1,10 @@ package cn.teammodel.dao; -import cn.teammodel.model.entity.Student; +import cn.teammodel.model.entity.school.Student; import com.azure.spring.data.cosmos.repository.CosmosRepository; import org.springframework.stereotype.Repository; @Repository public interface StudentRepository extends CosmosRepository { - - //Flux findById(String id); + Student findStudentByIdAndCode(String id, String code); } \ No newline at end of file diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java index 310d7ae..d4dc6c1 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/AppraiseVoteDto.java @@ -3,7 +3,6 @@ package cn.teammodel.model.dto.Appraise; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; /** @@ -16,10 +15,6 @@ public class AppraiseVoteDto { @ApiModelProperty(value = "评价对象 Id") private String targetId; - @ApiModelProperty(value = "评价对象所处班级 Id") - @NotBlank - private String classId; - @NotNull @ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class") private String targetType; @@ -32,8 +27,4 @@ public class AppraiseVoteDto { @NotNull @ApiModelProperty(value = "评价项唯一 id", required = true) private String appraiseId; - - @ApiModelProperty(value = "学段 id,用于拿到 semesterId", required = true) - @NotNull - private String periodId; } diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java index e86fd17..cf5073a 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/UpdateNodeDto.java @@ -11,6 +11,7 @@ import lombok.Data; public class UpdateNodeDto { @ApiModelProperty(value = "不传则默认为 default 的树修改节点") String periodId; + @ApiModelProperty(value = "评价项节点的 id") String id; String name; String logo; diff --git a/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java b/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java index 473ffc4..95e8a77 100644 --- a/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java +++ b/src/main/java/cn/teammodel/model/entity/appraise/AppraiseRecord.java @@ -32,11 +32,19 @@ public class AppraiseRecord extends BaseItem { */ private String academicYearId; /** - * 学生 id + * 学生/class id */ private String targetId; private String targetType; /** + * 学生/class 名字 + */ + private String name; + /** + * 学生/class 头像 + */ + private String avatar; + /** * 表扬次数 */ private Integer praiseCount; diff --git a/src/main/java/cn/teammodel/model/entity/school/ClassInfo.java b/src/main/java/cn/teammodel/model/entity/school/ClassInfo.java new file mode 100644 index 0000000..277ced8 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/school/ClassInfo.java @@ -0,0 +1,30 @@ +package cn.teammodel.model.entity.school; + +import cn.teammodel.model.entity.BaseItem; +import com.azure.spring.data.cosmos.core.mapping.Container; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Container(containerName = "School") +@Data +@JsonInclude(JsonInclude.Include.NON_NULL) +public class ClassInfo extends BaseItem { + private String no; + private String name; + private Teacher teacher; + private String periodId; + private Integer year; + private String room; + private String school; + private Integer graduate; + private String pk; + private Integer ttl; + + @Data + public static class Teacher { + private String id; + private String name; + } +} \ No newline at end of file diff --git a/src/main/java/cn/teammodel/model/entity/Student.java b/src/main/java/cn/teammodel/model/entity/school/Student.java similarity index 93% rename from src/main/java/cn/teammodel/model/entity/Student.java rename to src/main/java/cn/teammodel/model/entity/school/Student.java index 1d87e34..1ce7c6a 100644 --- a/src/main/java/cn/teammodel/model/entity/Student.java +++ b/src/main/java/cn/teammodel/model/entity/school/Student.java @@ -1,4 +1,4 @@ -package cn.teammodel.model.entity; +package cn.teammodel.model.entity.school; import com.azure.spring.data.cosmos.core.mapping.Container; import com.azure.spring.data.cosmos.core.mapping.PartitionKey; @@ -33,7 +33,7 @@ public class Student { private String gender; private Integer graduate; private List loginInfos; - private Integer createTime; + private Long createTime; private List guardians; private String pk; private Integer ttl; diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index 2da578a..35f277b 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -4,16 +4,16 @@ import cn.hutool.core.lang.UUID; import cn.teammodel.common.ErrorCode; import cn.teammodel.common.PK; import cn.teammodel.config.exception.ServiceException; -import cn.teammodel.dao.AppraiseRecordRepository; -import cn.teammodel.dao.AppraiseRepository; -import cn.teammodel.dao.SchoolRepository; +import cn.teammodel.dao.*; import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.entity.User; import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.model.entity.appraise.AppraiseRecord; import cn.teammodel.model.entity.appraise.AppraiseRecordItem; import cn.teammodel.model.entity.appraise.AppraiseTreeNode; +import cn.teammodel.model.entity.school.ClassInfo; import cn.teammodel.model.entity.school.School; +import cn.teammodel.model.entity.school.Student; import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.service.EvaluationService; import cn.teammodel.utils.RepositoryUtil; @@ -45,6 +45,10 @@ public class EvaluationServiceImpl implements EvaluationService { private final static String TARGET_STUDENT = "student"; private final static String TARGET_CLASS = "class"; @Resource + private ClassRepository classRepository; + @Resource + private StudentRepository studentRepository; + @Resource private SchoolRepository schoolRepository; @Resource private AppraiseRecordRepository appraiseRecordRepository; @@ -109,7 +113,9 @@ public class EvaluationServiceImpl implements EvaluationService { if (appraiseTreeNodes != null) { appraiseTreeNodes.forEach(node -> node.setPid(newId)); } + // 处理每个节点属性 item.setId(newId); + item.setCreator("template"); }); } @@ -228,17 +234,34 @@ public class EvaluationServiceImpl implements EvaluationService { @Override public void vote(AppraiseVoteDto appraiseVoteDto) { String targetId = appraiseVoteDto.getTargetId(); - String classId = appraiseVoteDto.getClassId(); boolean spread = appraiseVoteDto.isSpread(); String targetType = appraiseVoteDto.getTargetType(); String appraiseId = appraiseVoteDto.getAppraiseId(); - // 获取 school 中的 semesters - String periodId = appraiseVoteDto.getPeriodId(); User loginUser = SecurityUtil.getLoginUser(); String schoolId = loginUser.getSchoolId(); - + String classId; + String periodId; + String name; + String avatar = null; // 判断评价对象是否合法 - if (!targetType.equals(TARGET_STUDENT) && !targetType.equals(TARGET_CLASS)) { + if (targetType.equals(TARGET_STUDENT)) { + Student student = studentRepository.findStudentByIdAndCode(targetId, String.format(PK.STUDENT, schoolId)); + if (student == null) { + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "该学生不存在"); + } + classId = student.getClassId(); + periodId = student.getPeriodId(); + name = student.getName(); + avatar = student.getPicture(); + } else if (targetType.equals(TARGET_CLASS)){ + ClassInfo classInfo = classRepository.findClassByIdAndCode(targetId, String.format(PK.CLASS, schoolId)); + if (classInfo == null) { + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "班级不存在"); + } + classId = targetId; + periodId = classInfo.getPeriodId(); + name = classInfo.getName(); + } else { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "不受支持的评价对象"); } @@ -255,9 +278,10 @@ public class EvaluationServiceImpl implements EvaluationService { String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now()); // 查询是否存在记录 - AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndAcademicYearIdAndCode( + AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndClassIdAndAcademicYearIdAndCode( targetId, academicYearId, + classId, String.format(PK.PK_APPRAISE_RECORD,schoolId) ); @@ -269,18 +293,16 @@ public class EvaluationServiceImpl implements EvaluationService { item.setCreatorId(loginUser.getId()); item.setCreateTime(LocalDateTime.now()); - // 不存在则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item - if (record == null) { + // 不存在或者学生的班级不一样则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item + if (record == null || !classId.equals(record.getClassId())) { List items = Collections.singletonList(item); record = new AppraiseRecord(); - if (targetType.equals(TARGET_STUDENT)) { - record.setTargetType(TARGET_STUDENT); - } else { - record.setTargetType(TARGET_CLASS); - } record.setTargetId(targetId); + record.setTargetType(targetType); record.setClassId(classId); + record.setName(name); + record.setAvatar(avatar); record.setAcademicYearId(academicYearId); record.setPraiseCount(appraiseTreeNode.isPraise() ? 1 : -1); record.setScore(ObjectUtils.isEmpty(appraiseTreeNode.getScore()) ? 0 : appraiseTreeNode.getScore());