refactor: 完善添加评价记录的逻辑(如果一个student确是不同的班级)

11111
winter 1 year ago
parent 18d42a36e6
commit 6c47e0c59e

@ -18,6 +18,11 @@ public interface PK {
*/ */
String PK_APPRAISE_RECORD = "AppraiseRecord-%s"; String PK_APPRAISE_RECORD = "AppraiseRecord-%s";
String COMMON_BASE = "Base"; String COMMON_BASE = "Base";
/**
* , id
*/
String STUDENT = "Base-%s";
String CLASS = "Class-%s";
/** /**
* *

@ -24,7 +24,7 @@ public interface AppraiseRecordRepository extends CosmosRepository<AppraiseRecor
/** /**
* *
*/ */
AppraiseRecord findAppraiseRecordByTargetIdAndAcademicYearIdAndCode(String targetId, String academicYearId, String code); AppraiseRecord findAppraiseRecordByTargetIdAndClassIdAndAcademicYearIdAndCode(String targetId, String academicYearId, String classId , 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(String targetId,String academicYearId,String code); List<AppraiseRecord> findScoreAndPraise(String targetId,String academicYearId,String code);

@ -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, String> {
ClassInfo findClassByIdAndCode(String targetId, String format);
}

@ -1,11 +1,10 @@
package cn.teammodel.dao; 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 com.azure.spring.data.cosmos.repository.CosmosRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface StudentRepository extends CosmosRepository<Student, String> { public interface StudentRepository extends CosmosRepository<Student, String> {
Student findStudentByIdAndCode(String id, String code);
//Flux<Student> findById(String id);
} }

@ -3,7 +3,6 @@ package cn.teammodel.model.dto.Appraise;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
/** /**
@ -16,10 +15,6 @@ public class AppraiseVoteDto {
@ApiModelProperty(value = "评价对象 Id") @ApiModelProperty(value = "评价对象 Id")
private String targetId; private String targetId;
@ApiModelProperty(value = "评价对象所处班级 Id")
@NotBlank
private String classId;
@NotNull @NotNull
@ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class") @ApiModelProperty(value = "评价对象类型:", allowableValues = "student ,class")
private String targetType; private String targetType;
@ -32,8 +27,4 @@ public class AppraiseVoteDto {
@NotNull @NotNull
@ApiModelProperty(value = "评价项唯一 id", required = true) @ApiModelProperty(value = "评价项唯一 id", required = true)
private String appraiseId; private String appraiseId;
@ApiModelProperty(value = "学段 id,用于拿到 semesterId", required = true)
@NotNull
private String periodId;
} }

@ -11,6 +11,7 @@ import lombok.Data;
public class UpdateNodeDto { public class UpdateNodeDto {
@ApiModelProperty(value = "不传则默认为 default 的树修改节点") @ApiModelProperty(value = "不传则默认为 default 的树修改节点")
String periodId; String periodId;
@ApiModelProperty(value = "评价项节点的 id")
String id; String id;
String name; String name;
String logo; String logo;

@ -32,11 +32,19 @@ public class AppraiseRecord extends BaseItem {
*/ */
private String academicYearId; private String academicYearId;
/** /**
* id * /class id
*/ */
private String targetId; private String targetId;
private String targetType; private String targetType;
/** /**
* /class
*/
private String name;
/**
* /class
*/
private String avatar;
/**
* *
*/ */
private Integer praiseCount; private Integer praiseCount;

@ -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;
}
}

@ -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.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey; import com.azure.spring.data.cosmos.core.mapping.PartitionKey;
@ -33,7 +33,7 @@ public class Student {
private String gender; private String gender;
private Integer graduate; private Integer graduate;
private List<String> loginInfos; private List<String> loginInfos;
private Integer createTime; private Long createTime;
private List<String> guardians; private List<String> guardians;
private String pk; private String pk;
private Integer ttl; private Integer ttl;

@ -4,16 +4,16 @@ import cn.hutool.core.lang.UUID;
import cn.teammodel.common.ErrorCode; import cn.teammodel.common.ErrorCode;
import cn.teammodel.common.PK; import cn.teammodel.common.PK;
import cn.teammodel.config.exception.ServiceException; import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.dao.AppraiseRecordRepository; import cn.teammodel.dao.*;
import cn.teammodel.dao.AppraiseRepository;
import cn.teammodel.dao.SchoolRepository;
import cn.teammodel.model.dto.Appraise.*; import cn.teammodel.model.dto.Appraise.*;
import cn.teammodel.model.entity.User; import cn.teammodel.model.entity.User;
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.AppraiseRecord;
import cn.teammodel.model.entity.appraise.AppraiseRecordItem; import cn.teammodel.model.entity.appraise.AppraiseRecordItem;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode; 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.School;
import cn.teammodel.model.entity.school.Student;
import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.EvaluationService; import cn.teammodel.service.EvaluationService;
import cn.teammodel.utils.RepositoryUtil; 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_STUDENT = "student";
private final static String TARGET_CLASS = "class"; private final static String TARGET_CLASS = "class";
@Resource @Resource
private ClassRepository classRepository;
@Resource
private StudentRepository studentRepository;
@Resource
private SchoolRepository schoolRepository; private SchoolRepository schoolRepository;
@Resource @Resource
private AppraiseRecordRepository appraiseRecordRepository; private AppraiseRecordRepository appraiseRecordRepository;
@ -109,7 +113,9 @@ public class EvaluationServiceImpl implements EvaluationService {
if (appraiseTreeNodes != null) { if (appraiseTreeNodes != null) {
appraiseTreeNodes.forEach(node -> node.setPid(newId)); appraiseTreeNodes.forEach(node -> node.setPid(newId));
} }
// 处理每个节点属性
item.setId(newId); item.setId(newId);
item.setCreator("template");
}); });
} }
@ -228,17 +234,34 @@ public class EvaluationServiceImpl implements EvaluationService {
@Override @Override
public void vote(AppraiseVoteDto appraiseVoteDto) { public void vote(AppraiseVoteDto appraiseVoteDto) {
String targetId = appraiseVoteDto.getTargetId(); String targetId = appraiseVoteDto.getTargetId();
String classId = appraiseVoteDto.getClassId();
boolean spread = appraiseVoteDto.isSpread(); boolean spread = appraiseVoteDto.isSpread();
String targetType = appraiseVoteDto.getTargetType(); String targetType = appraiseVoteDto.getTargetType();
String appraiseId = appraiseVoteDto.getAppraiseId(); String appraiseId = appraiseVoteDto.getAppraiseId();
// 获取 school 中的 semesters
String periodId = appraiseVoteDto.getPeriodId();
User loginUser = SecurityUtil.getLoginUser(); User loginUser = SecurityUtil.getLoginUser();
String schoolId = loginUser.getSchoolId(); 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(), "不受支持的评价对象"); throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "不受支持的评价对象");
} }
@ -255,9 +278,10 @@ public class EvaluationServiceImpl implements EvaluationService {
String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now()); String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now());
// 查询是否存在记录 // 查询是否存在记录
AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndAcademicYearIdAndCode( AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndClassIdAndAcademicYearIdAndCode(
targetId, targetId,
academicYearId, academicYearId,
classId,
String.format(PK.PK_APPRAISE_RECORD,schoolId) String.format(PK.PK_APPRAISE_RECORD,schoolId)
); );
@ -269,18 +293,16 @@ public class EvaluationServiceImpl implements EvaluationService {
item.setCreatorId(loginUser.getId()); item.setCreatorId(loginUser.getId());
item.setCreateTime(LocalDateTime.now()); item.setCreateTime(LocalDateTime.now());
// 不存在则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item // 不存在或者学生的班级不一样则创建一条新的,存在则处理一下分值后再向其 nodes 中插入一条 item
if (record == null) { if (record == null || !classId.equals(record.getClassId())) {
List<AppraiseRecordItem> items = Collections.singletonList(item); List<AppraiseRecordItem> items = Collections.singletonList(item);
record = new AppraiseRecord(); record = new AppraiseRecord();
if (targetType.equals(TARGET_STUDENT)) {
record.setTargetType(TARGET_STUDENT);
} else {
record.setTargetType(TARGET_CLASS);
}
record.setTargetId(targetId); record.setTargetId(targetId);
record.setTargetType(targetType);
record.setClassId(classId); record.setClassId(classId);
record.setName(name);
record.setAvatar(avatar);
record.setAcademicYearId(academicYearId); record.setAcademicYearId(academicYearId);
record.setPraiseCount(appraiseTreeNode.isPraise() ? 1 : -1); record.setPraiseCount(appraiseTreeNode.isPraise() ? 1 : -1);
record.setScore(ObjectUtils.isEmpty(appraiseTreeNode.getScore()) ? 0 : appraiseTreeNode.getScore()); record.setScore(ObjectUtils.isEmpty(appraiseTreeNode.getScore()) ? 0 : appraiseTreeNode.getScore());

Loading…
Cancel
Save