From 2e6d04aeffb672dbe0afd48d1427fbe34374b7e5 Mon Sep 17 00:00:00 2001 From: "zhouj1203@hotmail.com" Date: Thu, 29 Aug 2024 16:57:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=84=E8=AF=AD=E4=B8=80=E6=9D=A1=E9=BE=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/CommonController.java | 24 ++++++++-- .../admin/service/CommonService.java | 5 +++ .../admin/service/impl/CommonServiceImpl.java | 44 +++++++++++++++++++ .../model/dto/admin/common/CommentDto.java | 14 ++++++ .../model/entity/common/Comment.java | 7 ++- .../repository/CommentRepository.java | 19 ++++++++ 6 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 src/main/java/cn/teammodel/model/dto/admin/common/CommentDto.java create mode 100644 src/main/java/cn/teammodel/repository/CommentRepository.java diff --git a/src/main/java/cn/teammodel/controller/admin/controller/CommonController.java b/src/main/java/cn/teammodel/controller/admin/controller/CommonController.java index 672aa1c..4806d52 100644 --- a/src/main/java/cn/teammodel/controller/admin/controller/CommonController.java +++ b/src/main/java/cn/teammodel/controller/admin/controller/CommonController.java @@ -1,11 +1,10 @@ package cn.teammodel.controller.admin.controller; import cn.teammodel.common.R; -import cn.teammodel.controller.admin.service.ArtService; import cn.teammodel.controller.admin.service.CommonService; -import cn.teammodel.model.dto.admin.art.ArtFindDto; +import cn.teammodel.model.dto.admin.common.CommentDto; import cn.teammodel.model.dto.admin.common.GCDto; -import cn.teammodel.model.vo.admin.ArtElementsVo; +import cn.teammodel.model.entity.common.Comment; import cn.teammodel.model.vo.admin.GradeAndClassVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -15,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; -import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import java.util.List; @@ -32,4 +30,22 @@ public class CommonController { List res = commonService.getGradeAndClass(gcDto); return R.success(res); } + @PostMapping("save") + @ApiOperation("保存评语") + public R saveOrUpdateComment(@Valid @RequestBody Comment comment) { + Comment res = commonService.saveOrUpdateComment(comment); + return R.success(res); + } + @PostMapping("delete") + @ApiOperation("删除评语") + public R> deleteComment(@Valid @RequestBody CommentDto comment) { + List res = commonService.deleteComment(comment); + return R.success(res); + } + @PostMapping("find") + @ApiOperation("根据ID查询评语评语") + public R> findComment(@Valid @RequestBody Comment comment) { + List res = commonService.getCommentById(comment); + return R.success(res); + } } diff --git a/src/main/java/cn/teammodel/controller/admin/service/CommonService.java b/src/main/java/cn/teammodel/controller/admin/service/CommonService.java index 5322efa..cc06987 100644 --- a/src/main/java/cn/teammodel/controller/admin/service/CommonService.java +++ b/src/main/java/cn/teammodel/controller/admin/service/CommonService.java @@ -1,10 +1,15 @@ package cn.teammodel.controller.admin.service; +import cn.teammodel.model.dto.admin.common.CommentDto; import cn.teammodel.model.dto.admin.common.GCDto; +import cn.teammodel.model.entity.common.Comment; import cn.teammodel.model.vo.admin.GradeAndClassVo; import org.springframework.stereotype.Service; import java.util.List; public interface CommonService { List getGradeAndClass(GCDto gcDto); + Comment saveOrUpdateComment(Comment comment); + List deleteComment(CommentDto comment); + List getCommentById(Comment comment); } diff --git a/src/main/java/cn/teammodel/controller/admin/service/impl/CommonServiceImpl.java b/src/main/java/cn/teammodel/controller/admin/service/impl/CommonServiceImpl.java index 9b170ec..71eef44 100644 --- a/src/main/java/cn/teammodel/controller/admin/service/impl/CommonServiceImpl.java +++ b/src/main/java/cn/teammodel/controller/admin/service/impl/CommonServiceImpl.java @@ -1,19 +1,26 @@ package cn.teammodel.controller.admin.service.impl; import cn.teammodel.controller.admin.service.CommonService; +import cn.teammodel.model.dto.admin.common.CommentDto; import cn.teammodel.model.dto.admin.common.GCDto; +import cn.teammodel.model.entity.common.Comment; import cn.teammodel.model.entity.school.ClassInfo; import cn.teammodel.model.entity.school.School; import cn.teammodel.model.vo.admin.GradeAndClassVo; import cn.teammodel.repository.ClassRepository; +import cn.teammodel.repository.CommentRepository; import cn.teammodel.repository.SchoolRepository; import cn.teammodel.utils.MonthToNumberConverter; +import com.azure.cosmos.models.CosmosPatchOperations; +import com.azure.cosmos.models.PartitionKey; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.time.Instant; import java.time.LocalDate; import java.time.Month; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.stream.Stream; @@ -25,6 +32,8 @@ public class CommonServiceImpl implements CommonService { private SchoolRepository schoolRepository; @Resource private ClassRepository classRepository; + @Resource + private CommentRepository commentRepository; @Override public List getGradeAndClass(GCDto gcDto) { @@ -99,4 +108,39 @@ public class CommonServiceImpl implements CommonService { throw new RuntimeException("获取当前学校当前学段年级班级信息失败"); } } + + @Override + public Comment saveOrUpdateComment(Comment comment) { + + List comments = commentRepository.findById(comment.getId(),comment.getSchool()); + Comment existingComment = comments.stream().findFirst().orElse(null); + if (existingComment != null) { + // 如果评论对象存在,则进行更新操作 + // 这里你可以根据需要更新评论对象的属性 + existingComment.setContent(comment.getContent()); + existingComment.setUpdateTime(Instant.now().toEpochMilli()); + + commentRepository.deleteById(existingComment.getId(),new PartitionKey(existingComment.getSchool())); + // 最后保存更新后的评论对象 + return commentRepository.save(existingComment); + } else { + // 如果评论对象不存在,则创建新的记录 + comment.setCode("Comment-"+ comment.getSchool()); + comment.setCreateTime(Instant.now().toEpochMilli()); + return commentRepository.save(comment); + } + } + + @Override + public List deleteComment(CommentDto comment) { + for (String id : comment.getIds()) { + commentRepository.deleteById(id,new PartitionKey(comment.getCode())); + } + return comment.getIds(); + } + + @Override + public List getCommentById(Comment comment) { + return commentRepository.findById(comment.getId(),comment.getSchool()); + } } diff --git a/src/main/java/cn/teammodel/model/dto/admin/common/CommentDto.java b/src/main/java/cn/teammodel/model/dto/admin/common/CommentDto.java new file mode 100644 index 0000000..c25cfba --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/admin/common/CommentDto.java @@ -0,0 +1,14 @@ +package cn.teammodel.model.dto.admin.common; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.List; + +@Data +public class CommentDto { + @ApiModelProperty("需要删除的id集合") + private List ids; + @ApiModelProperty("完成的编码:如Comment-hbcn") + private String code; +} diff --git a/src/main/java/cn/teammodel/model/entity/common/Comment.java b/src/main/java/cn/teammodel/model/entity/common/Comment.java index edd4885..20e76e8 100644 --- a/src/main/java/cn/teammodel/model/entity/common/Comment.java +++ b/src/main/java/cn/teammodel/model/entity/common/Comment.java @@ -12,18 +12,21 @@ import lombok.EqualsAndHashCode; @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class Comment extends BaseItem { + @ApiModelProperty("活动内容") private String content; private String creatorId; private String creatorName; + @ApiModelProperty("学校编码") private String school; private String studentId; + @ApiModelProperty("活动ID") private String activityId; @ApiModelProperty("活动类型") private String activityType; @ApiModelProperty("评论类型") private String contentType; - private String pk = "comment"; - //private int ttl = -1; + private String pk = "Comment"; + private int ttl = -1; private Long createTime; private Long updateTime; diff --git a/src/main/java/cn/teammodel/repository/CommentRepository.java b/src/main/java/cn/teammodel/repository/CommentRepository.java new file mode 100644 index 0000000..5f345a4 --- /dev/null +++ b/src/main/java/cn/teammodel/repository/CommentRepository.java @@ -0,0 +1,19 @@ +package cn.teammodel.repository; + +import cn.teammodel.model.entity.common.Comment; +import cn.teammodel.model.entity.school.School; +import com.azure.cosmos.models.CosmosPatchOperations; +import com.azure.cosmos.models.PartitionKey; +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; + +@Repository +public interface CommentRepository extends CosmosRepository { + @Query("select * from Comment as s where s.id = @id and s.pk = 'Comment' and s.school = @school ") + List findById(@Param("id") String id, @Param("school") String school); + +}