15 changed files with 605 additions and 1 deletions
@ -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; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -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; |
||||
|
|
||||
|
/** |
||||
|
* 评论计数校准定时任务 |
||||
|
* <p> |
||||
|
* 定时校准评论数量 |
||||
|
*/ |
||||
|
@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); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -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<MiniStrayAnimalNoteCommentLike> { |
||||
|
|
||||
|
} |
||||
@ -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<MiniStrayAnimalNoteComment> { |
||||
|
|
||||
|
int incrementCommentCount(Long strayAnimalNoteId); |
||||
|
|
||||
|
int batchCalibrateAllCommentCounts(); |
||||
|
|
||||
|
} |
||||
@ -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; |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -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; |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -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; |
||||
|
|
||||
|
} |
||||
@ -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; |
||||
|
|
||||
|
} |
||||
@ -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<MiniStrayAnimalNoteComment> { |
||||
|
|
||||
|
StrayAnimalNoteCommentVO addNoteComment(@Valid StrayAnimalNoteCommentForm formData); |
||||
|
|
||||
|
} |
||||
@ -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<MiniStrayAnimalNoteCommentMapper, MiniStrayAnimalNoteComment> 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<MiniStrayAnimalNote> 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<MiniStrayAnimalNoteComment> 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<MiniStrayAnimalNoteComment> 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; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<!DOCTYPE mapper |
||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
|
||||
|
<mapper namespace="com.youlai.boot.mini.mapper.MiniStrayAnimalNoteCommentLikeMapper"> |
||||
|
|
||||
|
|
||||
|
</mapper> |
||||
@ -0,0 +1,42 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<!DOCTYPE mapper |
||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
|
||||
|
<mapper namespace="com.youlai.boot.mini.mapper.MiniStrayAnimalNoteCommentMapper"> |
||||
|
|
||||
|
<update id="incrementCommentCount"> |
||||
|
UPDATE mini_stray_animal_note |
||||
|
SET comment_count = comment_count + 1 |
||||
|
WHERE id = #{noteId} |
||||
|
</update> |
||||
|
|
||||
|
<!-- 一次性校准所有笔记的评论数 --> |
||||
|
<update id="batchCalibrateAllCommentCounts"> |
||||
|
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> |
||||
|
|
||||
|
<!-- 只校准最近有变动的笔记(24小时内有过评论的) --> |
||||
|
<update id="batchCalibrateRecentNotes"> |
||||
|
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 |
||||
|
) |
||||
|
</update> |
||||
|
|
||||
|
</mapper> |
||||
Loading…
Reference in new issue