From cc5a3607120660eb6d74b69fecc0d09dba817098 Mon Sep 17 00:00:00 2001 From: glx <783262171@qq.com> Date: Thu, 30 Apr 2026 17:34:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=A8=E7=89=A9=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=E8=AF=84=E8=AE=BA=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../freemarker/MyBatisPlusGenerator.java | 2 + .../boot/common/enums/LogModuleEnum.java | 3 +- .../youlai/boot/common/util/HttpContext.java | 30 ++++ .../StrayAnimalNoteCommentController.java | 37 +++++ .../mini/job/CommentCountCalibrateJob.java | 40 +++++ .../MiniStrayAnimalNoteCommentLikeMapper.java | 14 ++ .../MiniStrayAnimalNoteCommentMapper.java | 19 +++ .../entity/MiniStrayAnimalNoteComment.java | 94 +++++++++++ .../MiniStrayAnimalNoteCommentLike.java | 70 ++++++++ .../form/StrayAnimalNoteCommentForm.java | 28 ++++ .../model/vo/StrayAnimalNoteCommentVO.java | 55 +++++++ .../StrayAnimalNoteCommentService.java | 13 ++ .../StrayAnimalNoteCommentServiceImpl.java | 150 ++++++++++++++++++ .../MiniStrayAnimalNoteCommentLikeMapper.xml | 9 ++ .../mini/MiniStrayAnimalNoteCommentMapper.xml | 42 +++++ 15 files changed, 605 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/youlai/boot/common/util/HttpContext.java create mode 100644 src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java create mode 100644 src/main/java/com/youlai/boot/mini/job/CommentCountCalibrateJob.java create mode 100644 src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java create mode 100644 src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java create mode 100644 src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteComment.java create mode 100644 src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteCommentLike.java create mode 100644 src/main/java/com/youlai/boot/mini/model/form/StrayAnimalNoteCommentForm.java create mode 100644 src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java create mode 100644 src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java create mode 100644 src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java create mode 100644 src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml create mode 100644 src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml diff --git a/src/main/java/com/youlai/boot/codegen/freemarker/MyBatisPlusGenerator.java b/src/main/java/com/youlai/boot/codegen/freemarker/MyBatisPlusGenerator.java index be19bc6..ae94d9b 100644 --- a/src/main/java/com/youlai/boot/codegen/freemarker/MyBatisPlusGenerator.java +++ b/src/main/java/com/youlai/boot/codegen/freemarker/MyBatisPlusGenerator.java @@ -99,6 +99,8 @@ public class MyBatisPlusGenerator { ,new TableConfig("mini_point_record", IdType.AUTO, "mini") ,new TableConfig("mini_point_rule", IdType.AUTO, "mini") ,new TableConfig("mini_sign_record", IdType.AUTO, "mini") + ,new TableConfig("mini_stray_animal_note_comment", IdType.AUTO, "mini") + ,new TableConfig("mini_stray_animal_note_comment_like", IdType.AUTO, "mini") // ,new TableConfig("mini_stray_animal", IdType.AUTO, "mini") // ,new TableConfig("mini_stray_animal", IdType.INPUT, "minitest") diff --git a/src/main/java/com/youlai/boot/common/enums/LogModuleEnum.java b/src/main/java/com/youlai/boot/common/enums/LogModuleEnum.java index 83d8dab..675db57 100644 --- a/src/main/java/com/youlai/boot/common/enums/LogModuleEnum.java +++ b/src/main/java/com/youlai/boot/common/enums/LogModuleEnum.java @@ -32,7 +32,8 @@ public enum LogModuleEnum implements IBaseEnum { POINT_ACCOUNT(100, "积分账户"), POINT_RECORD(101, "积分流水"), POINT_RULE(102, "积分规则"), - SIGN_RECORD(103, "签到记录"); + SIGN_RECORD(103, "签到记录"), + STRAY_ANIMAL_NOTE_COMMENT(104, "流浪动物笔记评论"); @EnumValue private final Integer value; diff --git a/src/main/java/com/youlai/boot/common/util/HttpContext.java b/src/main/java/com/youlai/boot/common/util/HttpContext.java new file mode 100644 index 0000000..b018f84 --- /dev/null +++ b/src/main/java/com/youlai/boot/common/util/HttpContext.java @@ -0,0 +1,30 @@ +package com.youlai.boot.common.util; + +import cn.hutool.core.util.ObjectUtil; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + + +public class HttpContext { + + + public static HttpServletResponse getResponse() throws NullPointerException { + if (ObjectUtil.isNull(RequestContextHolder.getRequestAttributes())) { + return null; + } + return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); + } + + + public static HttpServletRequest getRequest() throws NullPointerException { + if (ObjectUtil.isNull(RequestContextHolder.getRequestAttributes())) { + return null; + } + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + return request; + + } + +} diff --git a/src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java b/src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java new file mode 100644 index 0000000..077a33c --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java @@ -0,0 +1,37 @@ +package com.youlai.boot.mini.controller; + +import com.youlai.boot.common.annotation.Log; +import com.youlai.boot.common.annotation.RepeatSubmit; +import com.youlai.boot.common.enums.ActionTypeEnum; +import com.youlai.boot.common.enums.LogModuleEnum; +import com.youlai.boot.common.result.Result; +import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm; +import com.youlai.boot.mini.model.vo.StrayAnimalNoteCommentVO; +import com.youlai.boot.mini.service.StrayAnimalNoteCommentService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@Tag(name = "流浪动物笔记评论相关接口") +@RestController +@RequestMapping("/api/v1/mini/noteComment") +@RequiredArgsConstructor +public class StrayAnimalNoteCommentController { + + private final StrayAnimalNoteCommentService strayAnimalNoteCommentService; + + @Operation(summary = "笔记评论接口") + @PostMapping(value = "add") + @RepeatSubmit + @Log(module = LogModuleEnum.STRAY_ANIMAL_NOTE_COMMENT, value = ActionTypeEnum.INSERT) + public Result addNoteComment( + @Valid @RequestBody StrayAnimalNoteCommentForm formData + ) { + StrayAnimalNoteCommentVO vo = strayAnimalNoteCommentService.addNoteComment(formData); + return Result.success(vo); + } + + +} diff --git a/src/main/java/com/youlai/boot/mini/job/CommentCountCalibrateJob.java b/src/main/java/com/youlai/boot/mini/job/CommentCountCalibrateJob.java new file mode 100644 index 0000000..da932b2 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/job/CommentCountCalibrateJob.java @@ -0,0 +1,40 @@ +package com.youlai.boot.mini.job; + +import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteCommentMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +/** + * 评论计数校准定时任务 + *

+ * 定时校准评论数量 + */ +@Component +@Slf4j +@RequiredArgsConstructor +public class CommentCountCalibrateJob { + + private final MiniStrayAnimalNoteCommentMapper miniStrayAnimalNoteCommentMapper; + + // 每天凌晨 00:00:00 执行 + @Scheduled(cron = "0 0 0 * * ?") + @Transactional + public void calibrateCommentCounts() { + log.info("开始校准动物笔记评论数..."); + long startTime = System.currentTimeMillis(); + + try { + int updatedCount = miniStrayAnimalNoteCommentMapper.batchCalibrateAllCommentCounts(); + // 方案B:增量校准(数据量大时使用) + long costTime = System.currentTimeMillis() - startTime; + log.info("校准完成,共更新 {} 条笔记,耗时 {} ms", updatedCount, costTime); + } catch (Exception e) { + log.error("校准失败", e); + } + } + + +} diff --git a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java new file mode 100644 index 0000000..41a1240 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java @@ -0,0 +1,14 @@ +package com.youlai.boot.mini.mapper; + +import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteCommentLike; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* 流浪动物笔记评论点赞表 Mapper 接口 +* +* @author jwy +* @since +*/ +public interface MiniStrayAnimalNoteCommentLikeMapper extends BaseMapper { + +} diff --git a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java new file mode 100644 index 0000000..32a6184 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java @@ -0,0 +1,19 @@ +package com.youlai.boot.mini.mapper; + +import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import jakarta.validation.constraints.NotNull; + +/** +* 流浪动物笔记评论表 Mapper 接口 +* +* @author jwy +* @since +*/ +public interface MiniStrayAnimalNoteCommentMapper extends BaseMapper { + + int incrementCommentCount(Long strayAnimalNoteId); + + int batchCalibrateAllCommentCounts(); + +} diff --git a/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteComment.java b/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteComment.java new file mode 100644 index 0000000..a978ba4 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteComment.java @@ -0,0 +1,94 @@ +package com.youlai.boot.mini.model.entity; + +import com.baomidou.mybatisplus.annotation.*; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; +import com.fasterxml.jackson.annotation.JsonFormat; + +@Getter +@Setter +@ToString +@Accessors(chain = true) +@TableName("mini_stray_animal_note_comment") +@Schema(description = "流浪动物笔记评论表") +public class MiniStrayAnimalNoteComment implements Serializable { + + @TableId(value = "id", type = IdType.AUTO) + @Schema(description = "流浪动物笔记评论表主键id") + private Long id; + + + @TableField("uuid") + @Schema(description = "uuid唯一标识,前后端用这个进行数据交互") + private String uuid; + + @TableField("note_id") + @Schema(description = "笔记id") + private Long noteId; + + @TableField("mini_user_id") + @Schema(description = "评论者id") + private Long miniUserId; + + @TableField("content") + @Schema(description = "评论内容") + private String content; + + @TableField("parent_id") + @Schema(description = "父评论ID,0为一级评论") + private Long parentId; + + @TableField("root_id") + @Schema(description = "根评论ID,一级评论为自身ID,二级及以上为所属一级评论ID") + private Long rootId; + + @TableField("reply_to_user_id") + @Schema(description = "被回复的用户ID") + private Long replyToUserId; + + @TableField("like_count") + @Schema(description = "点赞数") + private Integer likeCount; + + @TableField("province") + @Schema(description = "所在省") + private String province; + + @TableField("create_time") + @Schema(description = "创建时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createTime; + + @TableField("create_timestamp") + @Schema(description = "创建时间毫秒级时间戳") + private Long createTimestamp; + + @TableField("create_by") + @Schema(description = "创建人ID") + private Long createBy; + + @TableField("update_time") + @Schema(description = "更新时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateTime; + + @TableField("update_timestamp") + @Schema(description = "更新时间毫秒级时间戳") + private Long updateTimestamp; + + @TableField("update_by") + @Schema(description = "修改人ID") + private Long updateBy; + + @TableField("is_deleted") + @Schema(description = "逻辑删除标识(0-未删除 1-已删除)") + private Boolean deleted; + + +} diff --git a/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteCommentLike.java b/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteCommentLike.java new file mode 100644 index 0000000..c8657c2 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimalNoteCommentLike.java @@ -0,0 +1,70 @@ +package com.youlai.boot.mini.model.entity; + +import com.baomidou.mybatisplus.annotation.*; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; +import com.fasterxml.jackson.annotation.JsonFormat; + +@Getter +@Setter +@ToString +@Accessors(chain = true) +@TableName("mini_stray_animal_note_comment_like") +@Schema(description = "流浪动物笔记评论点赞表") +public class MiniStrayAnimalNoteCommentLike implements Serializable { + + @TableId(value = "id", type = IdType.AUTO) + @Schema(description = "流浪动物笔记评论点赞表主键id") + private Long id; + + + @TableField("uuid") + @Schema(description = "uuid唯一标识,前后端用这个进行数据交互") + private String uuid; + + @TableField("note_comment_id") + @Schema(description = "笔记评论id") + private Long noteCommentId; + + @TableField("mini_user_id") + @Schema(description = "点赞用户id") + private Long miniUserId; + + @TableField("create_time") + @Schema(description = "创建时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createTime; + + @TableField("create_timestamp") + @Schema(description = "创建时间毫秒级时间戳") + private Long createTimestamp; + + @TableField("create_by") + @Schema(description = "创建人ID") + private Long createBy; + + @TableField("update_time") + @Schema(description = "更新时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateTime; + + @TableField("update_timestamp") + @Schema(description = "更新时间毫秒级时间戳") + private Long updateTimestamp; + + @TableField("update_by") + @Schema(description = "修改人ID") + private Long updateBy; + + @TableField("is_deleted") + @Schema(description = "逻辑删除标识(0-未删除 1-已删除)") + private Boolean deleted; + + +} diff --git a/src/main/java/com/youlai/boot/mini/model/form/StrayAnimalNoteCommentForm.java b/src/main/java/com/youlai/boot/mini/model/form/StrayAnimalNoteCommentForm.java new file mode 100644 index 0000000..9301d89 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/model/form/StrayAnimalNoteCommentForm.java @@ -0,0 +1,28 @@ +package com.youlai.boot.mini.model.form; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.validator.constraints.Length; + +@Schema(description = "动物笔记评论表单对象") +@Getter +@Setter +public class StrayAnimalNoteCommentForm { + + @NotBlank(message = "动物笔记uuid不能为空") + @Schema(type = "string", description = "动物笔记id", example = "0677d62d63ec693bf1bd6dab8a877dc1", required = true) + private String strayAnimalNoteUuId; + + @NotBlank(message = "评论内容不能为空") + @Length(max = 255, message = "评论不能超过255个字符") + @Schema(description = "评论内容", example = "老铁666") + private String content; + + @NotBlank(message = "父评论uuid不能为空") + @Schema(type = "string", description = "父评论ID,0为一级评论", example = "0677d62d63ec693bf1bd6dab8a877dc1", required = true) + private String parentUuId; + +} diff --git a/src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java b/src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java new file mode 100644 index 0000000..2e0284e --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java @@ -0,0 +1,55 @@ +package com.youlai.boot.mini.model.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +@Schema(description = "动物笔记评论VO") +public class StrayAnimalNoteCommentVO { + + @Schema(description = "动物笔记uuid") + private String noteUuId; + + @Schema(description = "动物笔记评论uuid") + private String uuid; + + @Schema(description = "评论用户uuid") + private String appUserId; + + @Schema(description = "评论用户头像") + private String avatarUrl; + + @Schema(description = "评论用户昵称") + private String nickname = "用户已注销"; + + @Schema(description = "评论内容") + private String content; + + @Schema(description = "父评论uuid,0为一级评论") + private String parentUuId; + + @Schema(description = "根评论ID,一级评论为自身ID,二级及以上为所属一级评论ID") + private String rootId; + + @Schema(description = "被回复的用户ID") + private String replyToUserId; + + @Schema(description = "被回复用户头像") + private String replyToAvatarUrl; + + @Schema(description = "被回复用户昵称") + private String replyToNickname = "用户已注销"; + + @Schema(description = "点赞数") + private Integer likeCount; + + @Schema(description = "创建时间") + private Long createdAt; + + @Schema(description = "当前用户是否点赞评论,true点赞,false未点赞") + private Boolean isLiked = false; + + @Schema(description = "所在省") + private String province; + +} diff --git a/src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java b/src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java new file mode 100644 index 0000000..2efad53 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java @@ -0,0 +1,13 @@ +package com.youlai.boot.mini.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment; +import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm; +import com.youlai.boot.mini.model.vo.StrayAnimalNoteCommentVO; +import jakarta.validation.Valid; + +public interface StrayAnimalNoteCommentService extends IService { + + StrayAnimalNoteCommentVO addNoteComment(@Valid StrayAnimalNoteCommentForm formData); + +} diff --git a/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java b/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java new file mode 100644 index 0000000..75cc0a2 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java @@ -0,0 +1,150 @@ +package com.youlai.boot.mini.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.youlai.boot.common.exception.MsgException; +import com.youlai.boot.common.util.HttpContext; +import com.youlai.boot.common.util.IPUtils; +import com.youlai.boot.framework.security.util.SecurityUtils; +import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteCommentLikeMapper; +import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteCommentMapper; +import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteMapper; +import com.youlai.boot.mini.model.entity.MiniStrayAnimalNote; +import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment; +import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm; +import com.youlai.boot.mini.model.vo.StrayAnimalNoteCommentVO; +import com.youlai.boot.mini.service.StrayAnimalNoteCommentService; +import com.youlai.boot.system.mapper.UserMapper; +import com.youlai.boot.system.model.entity.SysUser; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.UUID; + +@Slf4j +@Service +@RequiredArgsConstructor +public class StrayAnimalNoteCommentServiceImpl extends ServiceImpl implements StrayAnimalNoteCommentService { + + private final MiniStrayAnimalNoteCommentLikeMapper miniStrayAnimalNoteCommentLikeMapper; + private final MiniStrayAnimalNoteMapper miniStrayAnimalNoteMapper; + private final UserMapper userMapper; + + @Override + public StrayAnimalNoteCommentVO addNoteComment(StrayAnimalNoteCommentForm formData) { + // 1. 保存用户评论信息 + Long userId = SecurityUtils.getUserId(); + StrayAnimalNoteCommentVO vo = saveCommentInfo(formData, userId); + + //2.评论审核 +// commentAudit(animalNoteCommentVO.getId(),animalNoteCommentVO.getContent()); + + return vo; + } + + private StrayAnimalNoteCommentVO saveCommentInfo(StrayAnimalNoteCommentForm addCommentForm, Long appUserId) { + // 获取笔记信息 + LambdaQueryWrapper noteQueryWrapper = new LambdaQueryWrapper<>(); + noteQueryWrapper.eq(MiniStrayAnimalNote::getUuid, addCommentForm.getStrayAnimalNoteUuId()) + .eq(MiniStrayAnimalNote::getDeleted, 0) + .last("LIMIT 1"); + MiniStrayAnimalNote note = miniStrayAnimalNoteMapper.selectOne(noteQueryWrapper); + + // 获取父评论信息 + Long parentCommentId = null; + MiniStrayAnimalNoteComment parentComment=null; + if (!addCommentForm.getParentUuId().equals("0")){ + LambdaQueryWrapper commentQueryWrapper = new LambdaQueryWrapper<>(); + commentQueryWrapper.eq(MiniStrayAnimalNoteComment::getUuid, addCommentForm.getParentUuId()) + .eq(MiniStrayAnimalNoteComment::getDeleted, 0) + .last("LIMIT 1"); + parentComment = baseMapper.selectOne(commentQueryWrapper); + if (parentComment == null) { + throw new MsgException("父评论已消失"); + } + parentCommentId = parentComment.getId(); + } else if(addCommentForm.getParentUuId().equals("0")) { + parentCommentId = 0L; + } + + //获取用户ip位置 + String userIp = IPUtils.getIpAddr(HttpContext.getRequest()); + String ipArr = IPUtils.getRegion(userIp); + String[] parts = ipArr.split("\\|"); + String provinceShortName = parts[2].replaceAll("(省|市|自治区|壮族自治区|回族自治区|维吾尔自治区)$", ""); + + long currentTimeUnix = System.currentTimeMillis(); + MiniStrayAnimalNoteComment animalNoteComment = new MiniStrayAnimalNoteComment(); + + animalNoteComment.setNoteId(note.getId()); + animalNoteComment.setUuid(UUID.randomUUID().toString()); + animalNoteComment.setMiniUserId(appUserId); + animalNoteComment.setContent(addCommentForm.getContent()); + + animalNoteComment.setParentId(parentCommentId); + animalNoteComment.setCreateBy(appUserId); + animalNoteComment.setCreateTimestamp(currentTimeUnix); + animalNoteComment.setCreateTime(new Date(currentTimeUnix)); + animalNoteComment.setProvince(provinceShortName); + + //包装 VO 返回 + StrayAnimalNoteCommentVO animalNoteCommentVO = new StrayAnimalNoteCommentVO(); + SysUser appUser = userMapper.selectById(appUserId); + animalNoteCommentVO.setNickname(appUser.getNickname()); + animalNoteCommentVO.setAvatarUrl(appUser.getAvatar()); + + // 处理rootId + if (animalNoteComment.getParentId() == 0) { + animalNoteComment.setRootId(0L); //先设置为0L,后面处理 + // 一级评论(下面插入后设置rootId=id) + } else { + // 二级评论 + animalNoteComment.setReplyToUserId(parentComment.getMiniUserId());// 设置被回复的用户ID为父评论的用户ID + animalNoteComment.setRootId(parentComment.getRootId()); + + // 设置被回复用户信息 + SysUser replyToUser = userMapper.selectById(parentComment.getMiniUserId()); + animalNoteCommentVO.setReplyToNickname(replyToUser.getNickname()); + animalNoteCommentVO.setReplyToAvatarUrl(replyToUser.getAvatar()); + } + + baseMapper.insert(animalNoteComment); + // 如果是顶级评论 仅设置 rootId + if (animalNoteComment.getParentId() == 0) { + animalNoteComment.setRootId(animalNoteComment.getId()); + baseMapper.updateById(animalNoteComment); + } + + //复制到 VO +// BeanUtils.copyProperties(animalNoteComment, animalNoteCommentVO); + animalNoteCommentVO.setNoteUuId(note.getUuid()); + animalNoteCommentVO.setUuid(animalNoteComment.getUuid()); + animalNoteCommentVO.setAppUserId(appUser.getUuid()); + animalNoteCommentVO.setContent(animalNoteComment.getContent()); + animalNoteCommentVO.setParentUuId(addCommentForm.getParentUuId()); + + LambdaQueryWrapper newCommentQueryWrapper = new LambdaQueryWrapper<>(); + newCommentQueryWrapper.eq(MiniStrayAnimalNoteComment::getId, animalNoteComment.getRootId()) + .eq(MiniStrayAnimalNoteComment::getDeleted, 0) + .last("LIMIT 1"); + animalNoteCommentVO.setRootId(baseMapper.selectOne(newCommentQueryWrapper).getUuid()); + + SysUser replyUser = userMapper.selectById(animalNoteComment.getReplyToUserId()); + animalNoteCommentVO.setReplyToUserId(replyUser.getUuid()); + animalNoteCommentVO.setLikeCount(animalNoteComment.getLikeCount()); + animalNoteCommentVO.setCreatedAt(animalNoteComment.getCreateTimestamp()); +// animalNoteCommentVO.setIsLiked(); + + + // 更新笔记评论数 + baseMapper.incrementCommentCount(note.getId()); + + return animalNoteCommentVO; + } + +} diff --git a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml new file mode 100644 index 0000000..5d3f226 --- /dev/null +++ b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml new file mode 100644 index 0000000..dca00b1 --- /dev/null +++ b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml @@ -0,0 +1,42 @@ + + + + + + + UPDATE mini_stray_animal_note + SET comment_count = comment_count + 1 + WHERE id = #{noteId} + + + + + UPDATE mini_stray_animal_note n + SET n.comment_count = ( + SELECT COUNT(1) + FROM mini_stray_animal_note_comment c + WHERE c.note_id = n.id + AND c.flag = 0 + ) + WHERE 1 = 1 + + + + + UPDATE mini_stray_animal_note n + INNER JOIN ( + SELECT note_id, MAX(create_time) as last_comment_time + FROM mini_stray_animal_note_comment + WHERE create_time >= DATE_SUB(NOW(), INTERVAL 1 DAY) + GROUP BY note_id + ) c ON n.id = c.note_id + SET n.comment_count = ( + SELECT COUNT(1) + FROM mini_stray_animal_note_comment c2 + WHERE c2.note_id = n.id AND c2.flag = 0 + ) + + +