Commit efd33ffe authored by 万成波's avatar 万成波

动态投票

parent d06f4cc4
package com.tangguo.controller.mobile; package com.tangguo.controller.mobile;
import cn.hutool.core.lang.Opt;
import com.tangguo.common.core.domain.AjaxResult; import com.tangguo.common.core.domain.AjaxResult;
import com.tangguo.common.mauth.MobileAuth; import com.tangguo.common.mauth.MobileAuth;
import com.tangguo.common.mauth.MobileTokenHelper; import com.tangguo.common.mauth.MobileTokenHelper;
...@@ -8,6 +7,7 @@ import com.tangguo.common.utils.PageUtils; ...@@ -8,6 +7,7 @@ import com.tangguo.common.utils.PageUtils;
import com.tangguo.common.utils.ValidateOperations; import com.tangguo.common.utils.ValidateOperations;
import com.tangguo.domain.bo.CreateMomentBO; import com.tangguo.domain.bo.CreateMomentBO;
import com.tangguo.domain.bo.LikeMomentBO; import com.tangguo.domain.bo.LikeMomentBO;
import com.tangguo.domain.bo.VoteMomentBO;
import com.tangguo.domain.vo.BbsMomentListVO; import com.tangguo.domain.vo.BbsMomentListVO;
import com.tangguo.service.IBbsMomentService; import com.tangguo.service.IBbsMomentService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -66,7 +66,7 @@ public class MBbsMomentController { ...@@ -66,7 +66,7 @@ public class MBbsMomentController {
*/ */
@MobileAuth @MobileAuth
@DeleteMapping("/delete/{momentId}") @DeleteMapping("/delete/{momentId}")
public AjaxResult createMoment(@PathVariable Long momentId) { public AjaxResult deleteMoment(@PathVariable Long momentId) {
this.momentService.userDeleteMoment(momentId); this.momentService.userDeleteMoment(momentId);
return AjaxResult.success(); return AjaxResult.success();
} }
...@@ -83,8 +83,25 @@ public class MBbsMomentController { ...@@ -83,8 +83,25 @@ public class MBbsMomentController {
public AjaxResult likeMoment(@RequestBody LikeMomentBO bo) { public AjaxResult likeMoment(@RequestBody LikeMomentBO bo) {
ValidateOperations.generalValidate(bo); ValidateOperations.generalValidate(bo);
synchronized (String.valueOf(bo.getMomentId()).intern()) { synchronized (String.valueOf(bo.getMomentId()).intern()) {
int likeCount = this.momentService.likeMoment(bo); this.momentService.userLikeMoment(bo);
return AjaxResult.success(likeCount); return AjaxResult.success();
}
}
/**
* 投票动态
*
* @param bo 动态
* @return 投票结果
*/
@MobileAuth
@PostMapping("/vote")
public AjaxResult voteMoment(@RequestBody VoteMomentBO bo) {
ValidateOperations.generalValidate(bo);
synchronized (String.valueOf(bo.getMomentId()).intern()) {
this.momentService.userVoteMoment(bo);
return AjaxResult.success();
} }
} }
......
...@@ -4,6 +4,7 @@ import lombok.Data; ...@@ -4,6 +4,7 @@ import lombok.Data;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
/** /**
......
package com.tangguo.domain.bo;
import lombok.Data;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
*
*
* @author 谈笑
* @createTime 2025-09-04 15:28:48 星期四
*/
@Data
public class VoteMomentBO {
/**
* 动态Id
*/
@NotNull(message = "动态Id不能为空")
private Long momentId;
/**
* 投票选项编码
*/
@NotNull(message = "投票选项编码不能为空")
private String optionCode;
}
...@@ -18,7 +18,7 @@ public interface IBbsMomentLikeService extends IService<BbsMomentLike> { ...@@ -18,7 +18,7 @@ public interface IBbsMomentLikeService extends IService<BbsMomentLike> {
* @param userName 用户名 * @param userName 用户名
* @return 点赞记录 * @return 点赞记录
*/ */
long getMomentLikeCount(Long momentId, String userName); long getUserMomentLikeCount(Long momentId, String userName);
/** /**
......
...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.tangguo.domain.BbsMoment; import com.tangguo.domain.BbsMoment;
import com.tangguo.domain.bo.CreateMomentBO; import com.tangguo.domain.bo.CreateMomentBO;
import com.tangguo.domain.bo.LikeMomentBO; import com.tangguo.domain.bo.LikeMomentBO;
import com.tangguo.domain.bo.VoteMomentBO;
import com.tangguo.domain.vo.BbsMomentListVO; import com.tangguo.domain.vo.BbsMomentListVO;
import com.tangguo.domain.vo.BbsUserMomentCountVO; import com.tangguo.domain.vo.BbsUserMomentCountVO;
...@@ -83,6 +84,14 @@ public interface IBbsMomentService extends IService<BbsMoment> { ...@@ -83,6 +84,14 @@ public interface IBbsMomentService extends IService<BbsMoment> {
* *
* @param bo 动态 * @param bo 动态
*/ */
int likeMoment(LikeMomentBO bo); void userLikeMoment(LikeMomentBO bo);
/**
* 投票动态
*
* @param bo 动态
*/
void userVoteMoment(VoteMomentBO bo);
} }
...@@ -50,4 +50,14 @@ public interface IBbsMomentVoteOptionService extends IService<BbsMomentVoteOptio ...@@ -50,4 +50,14 @@ public interface IBbsMomentVoteOptionService extends IService<BbsMomentVoteOptio
*/ */
void deleteVoteOptions(Long momentId); void deleteVoteOptions(Long momentId);
/**
* 查询投票选项
*
* @param momentId 动态Id
* @param optionCode 投票选项编码
* @return 投票选项
*/
BbsMomentVoteOption getVoteOption(Long momentId, String optionCode);
} }
...@@ -11,4 +11,23 @@ import com.tangguo.domain.BbsMomentVote; ...@@ -11,4 +11,23 @@ import com.tangguo.domain.BbsMomentVote;
*/ */
public interface IBbsMomentVoteService extends IService<BbsMomentVote> { public interface IBbsMomentVoteService extends IService<BbsMomentVote> {
/**
* 查询用户投票记录数量
*
* @param momentId 动态Id
* @param userName 用户名
* @return 点赞记录
*/
long getUserMomentVoteCount(Long momentId, String userName);
/**
* 添加点赞记录
*
* @param momentId 动态Id
* @param userName 用户名
* @param optionCode 投票选项编码
*/
void addMomentVoteCount(Long momentId, String userName, String optionCode);
} }
...@@ -30,7 +30,7 @@ public class BbsMomentLikeServiceImpl extends ServiceImpl<BbsMomentLikeMapper, B ...@@ -30,7 +30,7 @@ public class BbsMomentLikeServiceImpl extends ServiceImpl<BbsMomentLikeMapper, B
* @return 点赞记录 * @return 点赞记录
*/ */
@Override @Override
public long getMomentLikeCount(Long momentId, String userName) { public long getUserMomentLikeCount(Long momentId, String userName) {
return this.count( return this.count(
Wrappers.lambdaQuery(BbsMomentLike.class) Wrappers.lambdaQuery(BbsMomentLike.class)
.eq(BbsMomentLike::getMomentId, momentId) .eq(BbsMomentLike::getMomentId, momentId)
......
...@@ -9,6 +9,7 @@ import com.tangguo.common.mauth.MobileTokenHelper; ...@@ -9,6 +9,7 @@ import com.tangguo.common.mauth.MobileTokenHelper;
import com.tangguo.domain.*; import com.tangguo.domain.*;
import com.tangguo.domain.bo.CreateMomentBO; import com.tangguo.domain.bo.CreateMomentBO;
import com.tangguo.domain.bo.LikeMomentBO; import com.tangguo.domain.bo.LikeMomentBO;
import com.tangguo.domain.bo.VoteMomentBO;
import com.tangguo.domain.vo.*; import com.tangguo.domain.vo.*;
import com.tangguo.enums.EnableStatus; import com.tangguo.enums.EnableStatus;
import com.tangguo.enums.LikeStatus; import com.tangguo.enums.LikeStatus;
...@@ -54,6 +55,9 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -54,6 +55,9 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Resource @Resource
private IBbsMomentLikeService likeService; private IBbsMomentLikeService likeService;
@Resource
private IBbsMomentVoteService voteService;
/** /**
* 查询动态列表 * 查询动态列表
...@@ -176,14 +180,14 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -176,14 +180,14 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
*/ */
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public int likeMoment(LikeMomentBO bo) { public void userLikeMoment(LikeMomentBO bo) {
BbsMoment dbMoment = this.getById(bo.getMomentId()); BbsMoment dbMoment = this.getById(bo.getMomentId());
if (Objects.isNull(dbMoment)) { if (Objects.isNull(dbMoment)) {
throw new ServiceException("点赞失败,未查询到当前动态数据。"); throw new ServiceException("点赞失败,未查询到当前动态数据。");
} }
String userName = MobileTokenHelper.getUsername(); String userName = MobileTokenHelper.getUsername();
long likeStatus = this.likeService.getMomentLikeCount(dbMoment.getId(), userName); long likeStatus = this.likeService.getUserMomentLikeCount(dbMoment.getId(), userName);
int likeCount = dbMoment.getLikeCount(); int likeCount = dbMoment.getLikeCount();
if (LikeStatus.LIKE.getStatus() == bo.getLikeStatus()) { if (LikeStatus.LIKE.getStatus() == bo.getLikeStatus()) {
...@@ -202,14 +206,60 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -202,14 +206,60 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
} }
} }
// 更新动态点赞人数
BbsMoment updMoment = new BbsMoment(); BbsMoment updMoment = new BbsMoment();
updMoment.setId(dbMoment.getId()); updMoment.setId(dbMoment.getId());
updMoment.setLikeCount(likeCount); updMoment.setLikeCount(likeCount);
this.updateById(updMoment); this.updateById(updMoment);
return likeCount;
} }
/**
* 投票动态
*
* @param bo 动态
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void userVoteMoment(VoteMomentBO bo) {
BbsMoment dbMoment = this.getById(bo.getMomentId());
if (Objects.isNull(dbMoment)) {
throw new ServiceException("投票失败,未查询到当前动态数据。");
}
if (EnableStatus.QY.getStatus() != dbMoment.getIsEnableVote()) {
throw new ServiceException("投票失败,当前动态未开启投票。");
}
String username = MobileTokenHelper.getUsername();
long voteCount = this.voteService.getUserMomentVoteCount(dbMoment.getId(), username);
if (voteCount > 0) {
throw new ServiceException("投票失败,已对当前动态投票。");
}
String optionCode = bo.getOptionCode();
BbsMomentVoteOption dbOption = this.voteOptionService.getVoteOption(dbMoment.getId(), optionCode);
if (Objects.isNull(dbOption)) {
throw new ServiceException("投票失败,未查询到当前投票选项数据。");
}
// 添加投票记录
this.voteService.addMomentVoteCount(dbMoment.getId(), username, optionCode);
// 更新投票选项投票人数
BbsMomentVoteOption updOption = new BbsMomentVoteOption();
updOption.setId(dbOption.getId());
updOption.setVoteCount(dbOption.getVoteCount() + 1);
this.voteOptionService.updateById(updOption);
// 更新动态投票人数
BbsMoment updMoment = new BbsMoment();
updMoment.setId(dbMoment.getId());
updMoment.setVoteCount(updOption.getVoteCount());
this.updateById(updMoment);
}
/** /**
* 构建动态实体 * 构建动态实体
* *
......
...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; ...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.domain.BbsMoment; import com.tangguo.domain.BbsMoment;
import com.tangguo.domain.BbsMomentAttachment; import com.tangguo.domain.BbsMomentAttachment;
import com.tangguo.domain.BbsMomentLike;
import com.tangguo.domain.BbsMomentVoteOption; import com.tangguo.domain.BbsMomentVoteOption;
import com.tangguo.domain.bo.CreateMomentBO; import com.tangguo.domain.bo.CreateMomentBO;
import com.tangguo.domain.vo.BbsMomentListVO; import com.tangguo.domain.vo.BbsMomentListVO;
...@@ -108,4 +109,21 @@ public class BbsMomentVoteOptionServiceImpl extends ServiceImpl<BbsMomentVoteOpt ...@@ -108,4 +109,21 @@ public class BbsMomentVoteOptionServiceImpl extends ServiceImpl<BbsMomentVoteOpt
); );
} }
/**
* 查询投票选项
*
* @param momentId 动态Id
* @param optionCode 投票选项编码
* @return 投票选项
*/
@Override
public BbsMomentVoteOption getVoteOption(Long momentId, String optionCode) {
return this.getOne(
Wrappers.lambdaQuery(BbsMomentVoteOption.class)
.eq(BbsMomentVoteOption::getMomentId, momentId)
.eq(BbsMomentVoteOption::getCode, optionCode)
);
}
} }
package com.tangguo.service.impl; package com.tangguo.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.domain.BbsMomentVote; import com.tangguo.domain.BbsMomentVote;
import com.tangguo.mapper.BbsMomentVoteMapper; import com.tangguo.mapper.BbsMomentVoteMapper;
...@@ -20,4 +21,38 @@ public class BbsMomentVoteServiceImpl extends ServiceImpl<BbsMomentVoteMapper, B ...@@ -20,4 +21,38 @@ public class BbsMomentVoteServiceImpl extends ServiceImpl<BbsMomentVoteMapper, B
@Resource @Resource
private BbsMomentVoteMapper bbsMomentVoteMapper; private BbsMomentVoteMapper bbsMomentVoteMapper;
/**
* 查询用户投票记录数量
*
* @param momentId 动态Id
* @param userName 用户名
* @return 点赞记录
*/
@Override
public long getUserMomentVoteCount(Long momentId, String userName) {
return this.count(
Wrappers.lambdaQuery(BbsMomentVote.class)
.eq(BbsMomentVote::getMomentId, momentId)
.eq(BbsMomentVote::getUserName, userName)
);
}
/**
* 添加点赞记录
*
* @param momentId 动态Id
* @param userName 用户名
* @param optionCode 投票选项编码
*/
@Override
public void addMomentVoteCount(Long momentId, String userName, String optionCode) {
BbsMomentVote newVote = new BbsMomentVote();
newVote.setMomentId(momentId);
newVote.setUserName(userName);
newVote.setOptionCode(optionCode);
this.save(newVote);
}
} }
...@@ -40,14 +40,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -40,14 +40,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectUserMoments" resultType="com.tangguo.domain.vo.BbsMomentListVO"> <select id="selectUserMoments" resultType="com.tangguo.domain.vo.BbsMomentListVO">
SELECT SELECT
m.*, m.*,
IF(l.id IS NOT NULL AND l.like_status = 1, 1, 0) AS is_like, IF(l.id IS NOT NULL, 1, 0) AS is_like,
IF(v.id IS NOT NULL, 1, 0) AS is_vote IF(v.id IS NOT NULL, 1, 0) AS is_vote
FROM FROM
bbs_moment m bbs_moment m
LEFT JOIN LEFT JOIN
( (
SELECT SELECT
id, moment_id, like_status id, moment_id
FROM FROM
bbs_moment_like bbs_moment_like
WHERE WHERE
...@@ -79,7 +79,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -79,7 +79,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
SELECT SELECT
m.*, m.*,
uv.nike_name, uv.nike_name,
IF(l.id IS NOT NULL AND l.like_status = 1, 1, 0) AS is_like, IF(l.id IS NOT NULL, 1, 0) AS is_like,
IF(v.id IS NOT NULL, 1, 0) AS is_vote, IF(v.id IS NOT NULL, 1, 0) AS is_vote,
IF(m.user_name = #{userName}, 1, 0) AS is_self IF(m.user_name = #{userName}, 1, 0) AS is_self
FROM FROM
...@@ -89,7 +89,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -89,7 +89,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
LEFT JOIN LEFT JOIN
( (
SELECT SELECT
id, moment_id, like_status id, moment_id
FROM FROM
bbs_moment_like bbs_moment_like
WHERE WHERE
......
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