From a3922d8c2de8fdab55d0b051388130f405d4d554 Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Mon, 4 Dec 2023 14:51:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=92=A4=E5=9B=9E?= =?UTF-8?q?=E8=AF=84=E4=BB=B7=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/teammodel/common/IdRequest.java | 15 +++++++ .../controller/AppraiseController.java | 6 +++ .../dao/AppraiseRecordRepository.java | 2 +- .../model/dto/Appraise/InsertNodeDto.java | 3 +- .../model/dto/Appraise/RecallVoteDto.java | 20 ++++++++++ .../teammodel/service/EvaluationService.java | 2 + .../service/impl/EvaluationServiceImpl.java | 39 ++++++++++++++++--- 7 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 src/main/java/cn/teammodel/common/IdRequest.java create mode 100644 src/main/java/cn/teammodel/model/dto/Appraise/RecallVoteDto.java diff --git a/src/main/java/cn/teammodel/common/IdRequest.java b/src/main/java/cn/teammodel/common/IdRequest.java new file mode 100644 index 0000000..ecf4d60 --- /dev/null +++ b/src/main/java/cn/teammodel/common/IdRequest.java @@ -0,0 +1,15 @@ +package cn.teammodel.common; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * @author winter + * @create 2023-12-04 9:55 + */ +@Data +public class IdRequest { + @NotBlank(message = "id 不能为空") + private String id; +} diff --git a/src/main/java/cn/teammodel/controller/AppraiseController.java b/src/main/java/cn/teammodel/controller/AppraiseController.java index 840fde7..a1d90e7 100644 --- a/src/main/java/cn/teammodel/controller/AppraiseController.java +++ b/src/main/java/cn/teammodel/controller/AppraiseController.java @@ -57,6 +57,12 @@ public class AppraiseController { evaluationService.vote(appraiseVoteDto); return R.success("评价成功"); } + @PostMapping("recallVote") + @ApiOperation(value = "撤回给某个学生评价(投票)") + public R recallVote(@RequestBody @Valid RecallVoteDto recallVoteDto) { + evaluationService.recallVote(recallVoteDto); + return R.success("撤回评价成功"); + } @PostMapping("findVoteRecord") @ApiOperation(value = "多条件查询当前登录老师的学生评价(投票)") diff --git a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java index 7bac635..d3ea54d 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/model/dto/Appraise/InsertNodeDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java index 5b33667..0b33ea5 100644 --- a/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java +++ b/src/main/java/cn/teammodel/model/dto/Appraise/InsertNodeDto.java @@ -19,6 +19,7 @@ public class InsertNodeDto { @NotBlank(message = "name 不能为空") String name; String logo; - Integer order; + Integer order = 0; + Integer score = 0; boolean isPraise; } diff --git a/src/main/java/cn/teammodel/model/dto/Appraise/RecallVoteDto.java b/src/main/java/cn/teammodel/model/dto/Appraise/RecallVoteDto.java new file mode 100644 index 0000000..2b5cf9d --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/Appraise/RecallVoteDto.java @@ -0,0 +1,20 @@ +package cn.teammodel.model.dto.Appraise; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +/** + * @author winter + * @create 2023-11-22 16:16 + */ +@Data +public class RecallVoteDto { + @NotNull + @ApiModelProperty("学生评价记录的文档id") + String recordId; + @NotNull + @ApiModelProperty("学生评价记录的具体节点id") + String nodeId; +} diff --git a/src/main/java/cn/teammodel/service/EvaluationService.java b/src/main/java/cn/teammodel/service/EvaluationService.java index b20c53e..7147309 100644 --- a/src/main/java/cn/teammodel/service/EvaluationService.java +++ b/src/main/java/cn/teammodel/service/EvaluationService.java @@ -50,4 +50,6 @@ public interface EvaluationService { void vote(AppraiseVoteDto appraiseVoteDto); List findVoteRecord(FindVoteRecordDto findVoteRecordDto); + + void recallVote(RecallVoteDto recallVoteDto); } diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index 35f277b..58f9f66 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -27,10 +27,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; /** @@ -243,7 +240,7 @@ public class EvaluationServiceImpl implements EvaluationService { String periodId; String name; String avatar = null; - // 判断评价对象是否合法 + // 分别对班级和学生的关键信息取值 if (targetType.equals(TARGET_STUDENT)) { Student student = studentRepository.findStudentByIdAndCode(targetId, String.format(PK.STUDENT, schoolId)); if (student == null) { @@ -280,8 +277,8 @@ public class EvaluationServiceImpl implements EvaluationService { // 查询是否存在记录 AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndClassIdAndAcademicYearIdAndCode( targetId, - academicYearId, classId, + academicYearId, String.format(PK.PK_APPRAISE_RECORD,schoolId) ); @@ -350,6 +347,36 @@ public class EvaluationServiceImpl implements EvaluationService { return appraiseRecordItems; } + @Override + public void recallVote(RecallVoteDto recallVoteDto) { + String recordId = recallVoteDto.getRecordId(); + String nodeId = recallVoteDto.getNodeId(); + User loginUser = SecurityUtil.getLoginUser(); + String schoolId = loginUser.getSchoolId(); + String userId = loginUser.getId(); + + Optional optional = appraiseRecordRepository.findById(recordId, PK.buildOf(PK.PK_APPRAISE_RECORD, schoolId)); + AppraiseRecord appraiseRecord = optional.orElseThrow(() -> new ServiceException("该记录不存在")); + AppraiseRecordItem record = appraiseRecord.getNodes() + .stream() + .filter(item -> nodeId.equals(item.getId())) + .findFirst().orElseThrow(() -> new ServiceException("该记录节点不存在")); + // 鉴权(不是创建老师不能撤回) + if (!userId.equals(record.getCreatorId())) { + throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您不是创建老师,不能撤回"); + } + // 删除评价项并且恢复评分 + appraiseRecord.getNodes().removeIf(item -> nodeId.equals(item.getId())); + boolean praise = record.getAppraiseNode().isPraise(); + Integer newPraiseCount = appraiseRecord.getPraiseCount() + (praise ? -1 : 1); + appraiseRecord.setPraiseCount(newPraiseCount); + int score = record.getAppraiseNode().getScore() == null ? 0 : record.getAppraiseNode().getScore(); + Integer newScore = appraiseRecord.getScore() - score; + appraiseRecord.setScore(newScore); + // 保存 + appraiseRecordRepository.save(appraiseRecord); + } + /** * 递归收集 id 的节点及 id 节点的孩子节点 (迭代器删除居然也报错) */