Commit 0d2c4a9c authored by 万成波's avatar 万成波

优化代码

parent 0230113b
......@@ -8,9 +8,9 @@ import com.tangguo.common.core.domain.entity.SysMenu;
import com.tangguo.common.core.domain.entity.SysUser;
import com.tangguo.common.core.domain.model.LoginBody;
import com.tangguo.common.core.domain.model.LoginUser;
import com.tangguo.common.core.domain.model.WxcpCodeLogin;
import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.utils.SecurityUtils;
import com.tangguo.common.core.domain.model.WxcpCodeLogin;
import com.tangguo.framework.config.ServerConfig;
import com.tangguo.framework.web.service.SysLoginService;
import com.tangguo.framework.web.service.SysPermissionService;
......@@ -28,7 +28,9 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* 登录验证
......
package com.tangguo;
import com.tangguo.common.constant.ActiveMQConstant;
import com.tangguo.common.domain.PointsDetail;
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
import me.chanjar.weixin.cp.api.WxCpService;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
import javax.jms.*;
/**
*
......
......@@ -196,8 +196,10 @@ public class MBbsMomentController {
*/
@DeleteMapping("/comment/delete/{commentId}")
public AjaxResult deleteComment(@PathVariable Long commentId) {
this.momentService.userDeleteComment(commentId);
return AjaxResult.success();
synchronized (String.valueOf(commentId).intern()) {
this.momentService.userDeleteComment(commentId);
return AjaxResult.success();
}
}
......
......@@ -31,4 +31,22 @@ public interface BbsMomentMapper extends BaseMapper<BbsMoment> {
BbsMomentListVO selectMomentDetail(@Param("momentId") Long momentId, @Param("userName") String userName);
int incrMomentLikeCount(@Param("momentId") Long momentId, @Param("count") int count);
int decrMomentLikeCount(@Param("momentId") Long momentId, @Param("count") int count);
int incrMomentCommentCount(@Param("momentId") Long momentId, @Param("count") int count);
int decrMomentCommentCount(@Param("momentId") Long momentId, @Param("count") int count);
int incrMomentVoteCount(@Param("momentId") Long momentId, @Param("count") int count);
int decrMomentVoteCount(@Param("momentId") Long momentId, @Param("count") int count);
}
......@@ -11,7 +11,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
......
......@@ -43,9 +43,6 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Resource
private ApplicationEventPublisher eventPublisher;
@Resource
private BbsMomentMapper bbsMomentMapper;
@Resource
private IBbsMomentAttachmentService attachmentsService;
......@@ -245,32 +242,27 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
throw new ServiceException("点赞失败,未查询到当前动态数据。");
}
Long dbMomentId = dbMoment.getId();
String username = SecurityUtils.getUsername();
long likeStatus = this.likeService.getUserMomentLikeCount(dbMoment.getId(), username);
int likeCount = dbMoment.getLikeCount();
long likeStatus = this.likeService.getUserMomentLikeCount(dbMomentId, username);
if (LikeStatus.LIKE.getStatus() == bo.getLikeStatus()) {
if (likeStatus > 0) {
throw new ServiceException("点赞失败,已对当前动态点赞。");
} else {
this.likeService.addMomentLike(dbMoment.getId(), username);
likeCount++;
this.likeService.addMomentLike(dbMomentId, username);
this.baseMapper.incrMomentLikeCount(dbMomentId, 1);
}
} else {
}
else {
if (likeStatus < 1) {
throw new ServiceException("取消失败,没有对当前动态点赞。");
} else {
this.likeService.deleteMomentLike(dbMoment.getId(), username);
likeCount--;
this.likeService.deleteMomentLike(dbMomentId, username);
this.baseMapper.decrMomentLikeCount(dbMomentId, 1);
}
}
// 更新动态点赞人数
BbsMoment updMoment = new BbsMoment();
updMoment.setId(dbMoment.getId());
updMoment.setLikeCount(likeCount);
this.updateById(updMoment);
// 发布用户操作事件
OperateEvent operateEvent = new OperateEvent(username, OperateType.DZDT);
this.eventPublisher.publishEvent(operateEvent);
......@@ -293,20 +285,21 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
throw new ServiceException("投票失败,当前动态未开启投票。");
}
Long dbMomentId = dbMoment.getId();
String username = SecurityUtils.getUsername();
long voteCount = this.voteService.getUserMomentVoteCount(dbMoment.getId(), username);
long voteCount = this.voteService.getUserMomentVoteCount(dbMomentId, username);
if (voteCount > 0) {
throw new ServiceException("投票失败,已对当前动态投票。");
}
String optionCode = bo.getOptionCode();
BbsMomentVoteOption dbOption = this.voteOptionService.getVoteOption(dbMoment.getId(), optionCode);
BbsMomentVoteOption dbOption = this.voteOptionService.getVoteOption(dbMomentId, optionCode);
if (Objects.isNull(dbOption)) {
throw new ServiceException("投票失败,未查询到当前投票选项数据。");
}
// 添加投票记录
this.voteService.addMomentVoteCount(dbMoment.getId(), username, optionCode);
this.voteService.addMomentVoteCount(dbMomentId, username, optionCode);
// 更新投票选项投票人数
BbsMomentVoteOption updOption = new BbsMomentVoteOption();
......@@ -315,10 +308,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
this.voteOptionService.updateById(updOption);
// 更新动态投票人数
BbsMoment updMoment = new BbsMoment();
updMoment.setId(dbMoment.getId());
updMoment.setVoteCount(dbMoment.getVoteCount() + 1);
this.updateById(updMoment);
this.baseMapper.incrMomentVoteCount(dbMomentId, 1);
// 发布用户操作事件
OperateEvent operateEvent = new OperateEvent(username, OperateType.TPDT);
......@@ -360,9 +350,10 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
}
// 添加动态评论
Long dbMomentId = dbMoment.getId();
SysUser dbUser = SecurityUtils.getLoginUser().getUser();
BbsMomentComment newComment = new BbsMomentComment();
newComment.setMomentId(dbMoment.getId());
newComment.setMomentId(dbMomentId);
newComment.setUserName(dbUser.getUserName());
newComment.setNickName(dbUser.getNickName());
newComment.setContent(content);
......@@ -383,10 +374,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
this.commentService.updateById(updComment);
// 更新动态评论人数
BbsMoment updMoment = new BbsMoment();
updMoment.setId(dbMoment.getId());
updMoment.setCommentCount(dbMoment.getCommentCount() + 1);
this.updateById(updMoment);
this.baseMapper.incrMomentCommentCount(dbMomentId, 1);
// 发布用户操作事件
OperateEvent operateEvent = new OperateEvent(dbUser.getUserName(), OperateType.TPDT);
......@@ -412,10 +400,10 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
throw new ServiceException("删除失败,没有对当前动态评论的操作权限。");
}
BbsMomentComment updComment = new BbsMomentComment();
updComment.setId(dbComment.getId());
updComment.setStatus(CommentStatus.SC.getStatus());
this.commentService.updateById(updComment);
this.commentService.removeById(dbComment.getId());
// 更新动态评论人数
this.baseMapper.incrMomentCommentCount(dbComment.getMomentId(), 1);
}
......
......@@ -22,7 +22,6 @@ import com.tangguo.service.IBbsTopicService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
......
......@@ -3,8 +3,8 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tangguo.mapper.BbsMomentMapper">
<select id="selectBbsMomentList" resultType="com.tangguo.domain.BbsMoment">
<select id="selectBbsMomentList" resultType="com.tangguo.domain.BbsMoment">
SELECT
m.*,
uv.nick_name,
......@@ -156,5 +156,35 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
ORDER BY
m.create_time DESC
</select>
<update id="incrMomentLikeCount">
UPDATE bbs_moment SET like_count = like_count + #{count} WHERE id = #{momentId}
</update>
<update id="decrMomentLikeCount">
UPDATE bbs_moment SET like_count = like_count - #{count} WHERE id = #{momentId}
</update>
<update id="incrMomentCommentCount">
UPDATE bbs_moment SET comment_count = comment_count + #{count} WHERE id = #{momentId}
</update>
<update id="decrMomentCommentCount">
UPDATE bbs_moment SET comment_count = comment_count - #{count} WHERE id = #{momentId}
</update>
<update id="incrMomentVoteCount">
UPDATE bbs_moment SET vote_count = vote_count + #{count} WHERE id = #{momentId}
</update>
<update id="decrMomentVoteCount">
UPDATE bbs_moment SET vote_count = vote_count - #{count} WHERE id = #{momentId}
</update>
</mapper>
package com.tangguo.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tangguo.common.annotation.Log;
import com.tangguo.common.core.controller.BaseController;
import com.tangguo.common.core.domain.AjaxResult;
......
......@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.domain.PointsDetail;
import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.utils.SecurityUtils;
import com.tangguo.domain.BbsGrade;
import com.tangguo.domain.BbsUserPoints;
import com.tangguo.domain.BbsUserPointsDetail;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment