| | |
| | | package com.panzhihua.service_community.service.impl; |
| | | |
| | | import static java.util.Objects.isNull; |
| | | import static java.util.Objects.nonNull; |
| | | import static org.apache.commons.lang3.StringUtils.isBlank; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.panzhihua.common.constants.Constants; |
| | | import com.panzhihua.common.exceptions.ServiceException; |
| | | import com.panzhihua.common.model.dtos.community.discuss.ComActDiscussDetailDTO; |
| | | import com.panzhihua.common.model.dtos.community.discuss.ComActDiscussPublishResultDTO; |
| | | import com.panzhihua.common.model.vos.community.*; |
| | | import com.panzhihua.common.utlis.DateUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.data.redis.connection.StringRedisConnection; |
| | | import org.springframework.data.redis.core.RedisCallback; |
| | | import org.springframework.data.redis.core.SetOperations; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.data.redis.core.ValueOperations; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.panzhihua.common.model.dtos.community.*; |
| | | import com.panzhihua.common.constants.Constants; |
| | | import com.panzhihua.common.exceptions.ServiceException; |
| | | import com.panzhihua.common.model.dtos.community.ComActDiscussCommentDTO; |
| | | import com.panzhihua.common.model.dtos.community.ComActDiscussCommentUserDTO; |
| | | import com.panzhihua.common.model.dtos.community.ComActDiscussDTO; |
| | | import com.panzhihua.common.model.dtos.community.ComActDiscussOptionDTO; |
| | | import com.panzhihua.common.model.dtos.community.ComActDiscussOptionUserDTO; |
| | | import com.panzhihua.common.model.dtos.community.ComActDiscussUserDTO; |
| | | import com.panzhihua.common.model.dtos.community.PageComActDiscussCommentDTO; |
| | | import com.panzhihua.common.model.dtos.community.PageComActDiscussDTO; |
| | | import com.panzhihua.common.model.dtos.community.bigscreen.work.ScreenDiscussListDTO; |
| | | import com.panzhihua.common.model.dtos.community.discuss.ComActDiscussDetailDTO; |
| | | import com.panzhihua.common.model.dtos.community.discuss.ComActDiscussPublishResultDTO; |
| | | import com.panzhihua.common.model.vos.LoginUserInfoVO; |
| | | import com.panzhihua.common.model.vos.R; |
| | | import com.panzhihua.common.model.vos.community.ComActDiscussCommentVO; |
| | | import com.panzhihua.common.model.vos.community.ComActDiscussOptionUserVO; |
| | | import com.panzhihua.common.model.vos.community.ComActDiscussOptionVO; |
| | | import com.panzhihua.common.model.vos.community.ComActDiscussVO; |
| | | import com.panzhihua.common.model.vos.community.ComActVO; |
| | | import com.panzhihua.common.model.vos.community.SysConfVO; |
| | | import com.panzhihua.common.model.vos.community.screen.work.DiscussListVO; |
| | | import com.panzhihua.service_community.dao.*; |
| | | import com.panzhihua.service_community.model.dos.*; |
| | | import com.panzhihua.common.service.user.UserService; |
| | | import com.panzhihua.common.utlis.DateUtils; |
| | | import com.panzhihua.service_community.dao.ComActDiscussCommentDAO; |
| | | import com.panzhihua.service_community.dao.ComActDiscussCommentUserDAO; |
| | | import com.panzhihua.service_community.dao.ComActDiscussDAO; |
| | | import com.panzhihua.service_community.dao.ComActDiscussOptionDAO; |
| | | import com.panzhihua.service_community.dao.ComActDiscussOptionUserDAO; |
| | | import com.panzhihua.service_community.dao.ComActDiscussUserDAO; |
| | | import com.panzhihua.service_community.dao.SysConfMapper; |
| | | import com.panzhihua.service_community.model.dos.ComActDiscussCommentDO; |
| | | import com.panzhihua.service_community.model.dos.ComActDiscussCommentUserDO; |
| | | import com.panzhihua.service_community.model.dos.ComActDiscussDO; |
| | | import com.panzhihua.service_community.model.dos.ComActDiscussOptionDO; |
| | | import com.panzhihua.service_community.model.dos.ComActDiscussOptionUserDO; |
| | | import com.panzhihua.service_community.model.dos.ComActDiscussUserDO; |
| | | import com.panzhihua.service_community.model.dos.SysConfDO; |
| | | import com.panzhihua.service_community.service.ComActDiscussOptionService; |
| | | import com.panzhihua.service_community.service.ComActDiscussService; |
| | | import com.panzhihua.service_community.service.SysConfService; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | |
| | | * @create: 2021-01-22 14:51 |
| | | **/ |
| | | @Service |
| | | @Slf4j |
| | | public class ComActDiscussServiceImpl implements ComActDiscussService { |
| | | |
| | | private static final String DISCUSS_VIEW_NUM_PREFIX = "DISCUSS_VIEW_NUM"; |
| | | private static final String INCR_VIEW_DISCUSS_IDS_KEY = "INCR_VIEW_DISCUSS_IDS"; |
| | | private static final long DISCUSS_EXPIRE_TIME = 2678400L; |
| | | |
| | | @Resource |
| | | private ComActDiscussCommentDAO comActDiscussCommentDAO; |
| | | @Resource |
| | |
| | | private ComActDiscussOptionService comActDiscussOptionService; |
| | | @Resource |
| | | private SysConfMapper sysConfDao; |
| | | @Resource |
| | | private SysConfService sysConfService; |
| | | @Resource |
| | | private UserService userService; |
| | | @Resource |
| | | private StringRedisTemplate stringRedisTemplate; |
| | | |
| | | /** |
| | | * 议事投票-新增 |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R addDiscuss(ComActDiscussDTO comActDiscussDTO) { |
| | | if (!checkCurrentUserDiscussIdentity(comActDiscussDTO)) { |
| | | return R.fail("权限验证失败,请确认是否有发布权限"); |
| | | } |
| | | ComActDiscussDO comActDiscussDO = new ComActDiscussDO(); |
| | | BeanUtils.copyProperties(comActDiscussDTO, comActDiscussDO); |
| | | Integer type = comActDiscussDTO.getType(); |
| | |
| | | } else { |
| | | comActDiscussDO.setStatus(ComActDiscussDO.status.yfb); |
| | | } |
| | | //查询社区地址 |
| | | ComActVO comActVO = comActDiscussDAO.selectCommunity(comActDiscussDTO.getCommunityId()); |
| | | String address = comActDiscussDO.getAddress(); |
| | | if (ObjectUtils.isEmpty(address)) { |
| | | //查询社区地址 |
| | | ComActVO comActVO = comActDiscussDAO.selectCommunity(comActDiscussDTO.getCommunityId()); |
| | | comActDiscussDO.setAddress(comActVO.getAddress()); |
| | | } |
| | | int insert = comActDiscussDAO.insert(comActDiscussDO); |
| | |
| | | return R.ok(); |
| | | } |
| | | return R.fail(); |
| | | } |
| | | |
| | | /** |
| | | * 小程序用户验证是否拥有发布权限 |
| | | * @param comActDiscussDTO |
| | | * @return |
| | | */ |
| | | private boolean checkCurrentUserDiscussIdentity(ComActDiscussDTO comActDiscussDTO) { |
| | | //小程序用户验证是否拥有发布权限 |
| | | boolean checkResult = true; |
| | | Boolean isApplets = comActDiscussDTO.getIsApplets(); |
| | | if (nonNull(isApplets) && isApplets) { |
| | | String discussIdentityPrefix = "DISCUSS_IDENTITY_"; |
| | | Long communityId = comActDiscussDTO.getCommunityId(); |
| | | R sysConfValue = sysConfService.getSysConfValue(discussIdentityPrefix + communityId, communityId); |
| | | String discussIdentityConfig = (String) sysConfValue.getData(); |
| | | if (isBlank(discussIdentityConfig)) { |
| | | checkResult = false; |
| | | } else { |
| | | List<String> currentUserRoles = new ArrayList<>(); |
| | | List<String> sysAllowedRoles = Arrays.asList(discussIdentityConfig.split(",")); |
| | | LoginUserInfoVO loginUserInfo = comActDiscussDTO.getLoginUserInfo(); |
| | | if (loginUserInfo.getIsPartymember() == 1) { |
| | | currentUserRoles.add("2"); |
| | | } |
| | | if (loginUserInfo.getIsVolunteer().intValue() == 1) { |
| | | currentUserRoles.add("3"); |
| | | } |
| | | R isTeamResult = userService.checkCurrentUserIsTeam(loginUserInfo.getPhone(), loginUserInfo.getCommunityId()); |
| | | if (R.isOk(isTeamResult) && (Boolean) isTeamResult.getData()) { |
| | | currentUserRoles.add("4"); |
| | | } |
| | | if (currentUserRoles.isEmpty()) { |
| | | checkResult = false; |
| | | } else { |
| | | if (!sysAllowedRoles.contains("1")) { |
| | | boolean result = sysAllowedRoles.stream().anyMatch(role -> currentUserRoles.contains(role)); |
| | | if (!result) { |
| | | checkResult = false; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return checkResult; |
| | | } |
| | | |
| | | /** |
| | |
| | | } else { |
| | | comActDiscussDO.setStatus(ComActDiscussDO.status.yfb); |
| | | } |
| | | comActDiscussDO.setUpdateAt(new Date()); |
| | | comActDiscussDO.setUpdateAt(nowDate); |
| | | int insert = comActDiscussDAO.updateById(comActDiscussDO); |
| | | if (insert > 0) { |
| | | //如果是投票,需要把投票的选项存储 |
| | |
| | | if (ObjectUtils.isEmpty(comActDiscussDO)) { |
| | | return R.fail("议事投票不存在"); |
| | | } |
| | | Long currentUser = comActDiscussCommentDTO.getUserId(); |
| | | //判断当前用户是否参与过评论 |
| | | int currentUserCommentCount = comActDiscussCommentDAO.selectCount(new QueryWrapper<ComActDiscussCommentDO>() |
| | | .lambda().eq(ComActDiscussCommentDO::getDiscussId, discussId).eq(ComActDiscussCommentDO::getUserId, currentUser)); |
| | | |
| | | ComActDiscussCommentDO comActDiscussCommentDO = new ComActDiscussCommentDO(); |
| | | BeanUtils.copyProperties(comActDiscussCommentDTO, comActDiscussCommentDO); |
| | | Long userId = comActDiscussCommentDTO.getUserId(); |
| | | Long userId1 = comActDiscussDO.getUserId(); |
| | | if (userId.equals(userId1)) { |
| | | Long discussUser = comActDiscussDO.getUserId(); |
| | | if (currentUser.equals(discussUser)) { |
| | | comActDiscussCommentDO.setIsAuthor(1); |
| | | } |
| | | int insert = comActDiscussCommentDAO.insert(comActDiscussCommentDO); |
| | | if (insert > 0) { |
| | | comActDiscussDAO.addCommentCount(discussId, 1); |
| | | // comActDiscussDAO.addCommentCount(discussId, 1); |
| | | //评论成功,评论总数+1 |
| | | //若首次参与评论,参与讨论人数+1 |
| | | if (currentUserCommentCount > 0) { |
| | | comActDiscussDAO.incrCommentAndJoinNumById(discussId, false); |
| | | } else { |
| | | comActDiscussDAO.incrCommentAndJoinNumById(discussId, true); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | return R.fail(); |
| | |
| | | LoginUserInfoVO loginUserInfoVO = comActDiscussDAO.selectUserByUserId(comActDiscussCommentDO.getUserId()); |
| | | comActDiscussCommentVO.setUserName(loginUserInfoVO.getName()); |
| | | comActDiscussCommentVO.setPhone(loginUserInfoVO.getPhone()); |
| | | comActDiscussCommentVO.setImageUrl(loginUserInfoVO.getImageUrl()); |
| | | |
| | | //查询该评论下所有回复内容 |
| | | IPage<ComActDiscussCommentVO> discussCommentList = comActDiscussCommentDAO.getDiscussCommentList(new Page(pageComActDiscussCommentDTO.getPageNum(), pageComActDiscussCommentDTO.getPageSize()), pageComActDiscussCommentDTO.getId()); |
| | |
| | | if (parentId.intValue() != 0) { |
| | | return R.fail("二级评论不能回复"); |
| | | } |
| | | Long userId = comActDiscussDO.getUserId(); |
| | | Long userId1 = comActDiscussCommentDTO.getUserId(); |
| | | Long currentUser = comActDiscussCommentDTO.getUserId(); |
| | | //判断当前用户是否参与过评论/回复 |
| | | int currentUserCommentCount = comActDiscussCommentDAO.selectCount(new QueryWrapper<ComActDiscussCommentDO>() |
| | | .lambda().eq(ComActDiscussCommentDO::getDiscussId, discussId).eq(ComActDiscussCommentDO::getUserId, currentUser)); |
| | | Long discussUser = comActDiscussDO.getUserId(); |
| | | ComActDiscussCommentDO comActDiscussCommentDO1 = new ComActDiscussCommentDO(); |
| | | comActDiscussCommentDO1.setDiscussId(discussId); |
| | | comActDiscussCommentDO1.setComment(comActDiscussCommentDTO.getComment()); |
| | | comActDiscussCommentDO1.setUserId(comActDiscussCommentDTO.getUserId()); |
| | | comActDiscussCommentDO1.setParentId(id); |
| | | if (userId.equals(userId1)) { |
| | | if (discussUser.equals(currentUser)) { |
| | | comActDiscussCommentDO1.setIsAuthor(1); |
| | | } |
| | | int insert = comActDiscussCommentDAO.insert(comActDiscussCommentDO1); |
| | | if (insert > 0) { |
| | | comActDiscussDAO.addCommentCount(discussId, 1); |
| | | // comActDiscussDAO.addCommentCount(discussId, 1); |
| | | //回复成功,评论总数+1 |
| | | //若首次参与评论,参与讨论人数+1 |
| | | if (currentUserCommentCount > 0) { |
| | | comActDiscussDAO.incrCommentAndJoinNumById(discussId, false); |
| | | } else { |
| | | comActDiscussDAO.incrCommentAndJoinNumById(discussId, true); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | return R.fail(); |
| | |
| | | records.forEach(comActDiscussVO -> { |
| | | Integer type = comActDiscussVO.getType(); |
| | | Long id = comActDiscussVO.getId(); |
| | | String viewNumKey = String.join("_", DISCUSS_VIEW_NUM_PREFIX, id.toString()); |
| | | if (stringRedisTemplate.hasKey(viewNumKey)) { |
| | | ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue(); |
| | | comActDiscussVO.setViewsNum(Integer.parseInt(opsForValue.get(viewNumKey))); |
| | | } |
| | | if (type.equals(2)) { |
| | | List<ComActDiscussOptionDO> comActDiscussOptionDOS = comActDiscussOptionDAO.selectList( |
| | | new QueryWrapper<ComActDiscussOptionDO>().lambda().eq(ComActDiscussOptionDO::getDiscussId, id)); |
| | | List<ComActDiscussOptionVO> comActDiscussOptionVOS = new ArrayList<>(); |
| | | List<Integer> votes = new ArrayList<>(); |
| | | comActDiscussOptionDOS.forEach(comActDiscussOptionDO -> { |
| | | Long discussOptionDOId = comActDiscussOptionDO.getId(); |
| | | Integer selectCount = |
| | |
| | | // 判断选项是否已投票 |
| | | int haveVote = |
| | | comActDiscussOptionUserDAO.selectHaveVotes(comActDiscussOptionDO.getId(), loginUserId); |
| | | votes.add(haveVote); |
| | | comActDiscussOptionVO.setHaveVote(haveVote > 0 ? 1 : 0); |
| | | } |
| | | comActDiscussOptionVOS.add(comActDiscussOptionVO); |
| | | }); |
| | | comActDiscussOptionVOS.sort(Comparator.comparing(ComActDiscussOptionVO::getNum).reversed()); |
| | | fillThePercentAndSort(comActDiscussOptionVOS); |
| | | checkDiscussStatusIsCorrect(comActDiscussVO); |
| | | comActDiscussVO.setComActDiscussOptionVOS(comActDiscussOptionVOS); |
| | | //填充剩余可投票数 |
| | | if (loginUserId != null && !loginUserId.equals(0L)) { |
| | | if (comActDiscussVO.getIsRepeat().intValue() == 1) { |
| | | //可重复投票 |
| | | int votedVotesInToday = comActDiscussOptionUserDAO.selectVotedVotesInToday(id, loginUserId); |
| | | int remainingVotes = comActDiscussVO.getCount().intValue() - votedVotesInToday; |
| | | comActDiscussVO.setRemainingVotes(remainingVotes >= 0 ? remainingVotes : 0); |
| | | } else { |
| | | //不可重复投票 |
| | | int alreadyVotedVotes = votes.stream().mapToInt(Integer::intValue).sum(); |
| | | int remainingVotes = comActDiscussVO.getCount().intValue() - alreadyVotedVotes; |
| | | comActDiscussVO.setRemainingVotes(remainingVotes >= 0 ? remainingVotes : 0); |
| | | } |
| | | } |
| | | //填充投票记录列表 |
| | | List<ComActDiscussOptionUserVO> comActDiscussOptionUserVOList = comActDiscussOptionUserDAO |
| | | .selectVotedRecords(comActDiscussVO.getId()); |
| | | comActDiscussVO.setComActDiscussOptionUserVOList(comActDiscussOptionUserVOList); |
| | | //填充已参与投票人数 |
| | | int votedPersonNum = comActDiscussOptionUserDAO.selectVotedPersonNum(comActDiscussVO.getId()); |
| | | comActDiscussVO.setVotedPersonNum(votedPersonNum); |
| | | } |
| | | |
| | | if (loginUserId != null) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 判断discuss状态是否正确 |
| | | * @param comActDiscussVO |
| | | */ |
| | | private void checkDiscussStatusIsCorrect(ComActDiscussVO comActDiscussVO) { |
| | | //判断discuss状态是否正确 |
| | | if(comActDiscussVO.getEndTime() != null){ |
| | | boolean isIncorrectStatus = comActDiscussVO.getEndTime().before(new Date()) && |
| | | (comActDiscussVO.getStatus().intValue() == 1 || comActDiscussVO.getStatus().intValue() == 2); |
| | | if (isIncorrectStatus) { |
| | | comActDiscussVO.setStatus(3); |
| | | comActDiscussDAO.updateStatusById(comActDiscussVO.getId(), 3); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 议事投票-点赞、取消点赞 |
| | | * |
| | | * @param comActDiscussUserDTO 操作类型 |
| | |
| | | int num = 0; |
| | | ComActDiscussUserDO comActDiscussUserDO = new ComActDiscussUserDO(); |
| | | BeanUtils.copyProperties(comActDiscussUserDTO, comActDiscussUserDO); |
| | | if (type.intValue() == 1) { |
| | | boolean isIncr = type.intValue() == 1; |
| | | if (isIncr) { |
| | | num = comActDiscussUserDAO.insert(comActDiscussUserDO); |
| | | } else { |
| | | num = comActDiscussUserDAO.delete(new QueryWrapper<ComActDiscussUserDO>().lambda() |
| | |
| | | .eq(ComActDiscussUserDO::getDiscussId, comActDiscussUserDTO.getDiscussId())); |
| | | } |
| | | if (num > 0) { |
| | | //点赞成功 总数+1 || 取消点赞成功 总数-1 |
| | | comActDiscussDAO.incrOrDecrFabulousNumByType(comActDiscussUserDTO.getDiscussId(), isIncr); |
| | | return R.ok(); |
| | | } |
| | | return R.fail(); |
| | |
| | | } |
| | | ComActDiscussVO comActDiscussVO = new ComActDiscussVO(); |
| | | BeanUtils.copyProperties(comActDiscussDO, comActDiscussVO); |
| | | Integer integer = comActDiscussCommentDAO.selectCount( |
| | | new QueryWrapper<ComActDiscussCommentDO>().lambda().eq(ComActDiscussCommentDO::getDiscussId, id)); |
| | | comActDiscussVO.setCommentNum(integer); |
| | | Integer integer1 = comActDiscussUserDAO |
| | | .selectCount(new QueryWrapper<ComActDiscussUserDO>().lambda().eq(ComActDiscussUserDO::getDiscussId, id)); |
| | | comActDiscussVO.setSignNum(integer1); |
| | | comActDiscussVO.setSignNum(comActDiscussDO.getFabulousNum()); |
| | | Integer type = comActDiscussVO.getType(); |
| | | if (type.equals(2)) { |
| | | List<ComActDiscussOptionDO> comActDiscussOptionDOS = comActDiscussOptionDAO.selectList( |
| | | new QueryWrapper<ComActDiscussOptionDO>().lambda().eq(ComActDiscussOptionDO::getDiscussId, id)); |
| | | List<ComActDiscussOptionVO> comActDiscussOptionVOS = new ArrayList<>(); |
| | | ArrayList<Integer> votes = new ArrayList<>(); |
| | | comActDiscussOptionDOS.forEach(comActDiscussOptionDO -> { |
| | | Long discussOptionDOId = comActDiscussOptionDO.getId(); |
| | | Integer selectCount = |
| | |
| | | // 判断选项是否已投票 |
| | | int haveVote = |
| | | comActDiscussOptionUserDAO.selectHaveVotes(comActDiscussOptionDO.getId(), loginUserId); |
| | | votes.add(haveVote); |
| | | comActDiscussOptionVO.setHaveVote(haveVote > 0 ? 1 : 0); |
| | | } |
| | | comActDiscussOptionVOS.add(comActDiscussOptionVO); |
| | | }); |
| | | comActDiscussOptionVOS.sort(Comparator.comparing(ComActDiscussOptionVO::getNum).reversed()); |
| | | fillThePercentAndSort(comActDiscussOptionVOS); |
| | | comActDiscussVO.setComActDiscussOptionVOS(comActDiscussOptionVOS); |
| | | //填充剩余可投票数 |
| | | if (loginUserId != null && !loginUserId.equals(0L)) { |
| | | if (comActDiscussVO.getIsRepeat().intValue() == 1) { |
| | | //可重复投票 |
| | | int votedVotesInToday = comActDiscussOptionUserDAO.selectVotedVotesInToday(id, loginUserId); |
| | | int remainingVotes = comActDiscussVO.getCount().intValue() - votedVotesInToday; |
| | | comActDiscussVO.setRemainingVotes(remainingVotes >= 0 ? remainingVotes : 0); |
| | | } else { |
| | | //不可重复投票 |
| | | int alreadyVotedVotes = votes.stream().mapToInt(Integer::intValue).sum(); |
| | | int remainingVotes = comActDiscussVO.getCount().intValue() - alreadyVotedVotes; |
| | | comActDiscussVO.setRemainingVotes(remainingVotes >= 0 ? remainingVotes : 0); |
| | | } |
| | | } |
| | | checkDiscussStatusIsCorrect(comActDiscussVO); |
| | | //填充投票记录列表 |
| | | List<ComActDiscussOptionUserVO> comActDiscussOptionUserVOList = comActDiscussOptionUserDAO |
| | | .selectVotedRecords(comActDiscussVO.getId()); |
| | | comActDiscussVO.setComActDiscussOptionUserVOList(comActDiscussOptionUserVOList); |
| | | //填充已参与投票人数 |
| | | int votedPersonNum = comActDiscussOptionUserDAO.selectVotedPersonNum(comActDiscussVO.getId()); |
| | | comActDiscussVO.setVotedPersonNum(votedPersonNum); |
| | | } |
| | | Long userId = comActDiscussDO.getUserId(); |
| | | LoginUserInfoVO loginUserInfoVO = comActDiscussDAO.selectUserByUserId(userId); |
| | |
| | | comActDiscussVO.setHaveSign(comActDiscussVO1.getHaveSign()); |
| | | comActDiscussVO.setHaveVote(comActDiscussVO1.getHaveVote()); |
| | | } |
| | | |
| | | String viewNumKey = String.join("_", DISCUSS_VIEW_NUM_PREFIX, id.toString()); |
| | | if (stringRedisTemplate.hasKey(viewNumKey)) { |
| | | ValueOperations opsForValue = stringRedisTemplate.opsForValue(); |
| | | int viewNum = Integer.parseInt((String) opsForValue.get(viewNumKey)); |
| | | comActDiscussVO.setViewsNum(viewNum); |
| | | } |
| | | return R.ok(comActDiscussVO); |
| | | } |
| | | |
| | | /** |
| | | * 计算投票占比以及排序 |
| | | * @param comActDiscussOptionVOS |
| | | */ |
| | | private void fillThePercentAndSort(List<ComActDiscussOptionVO> comActDiscussOptionVOS) { |
| | | //填充总票数 |
| | | int allSum = comActDiscussOptionVOS.stream().mapToInt(ComActDiscussOptionVO::getNum).sum(); |
| | | comActDiscussOptionVOS.forEach(option -> { |
| | | if (allSum == 0) { |
| | | option.setPercent(new BigDecimal(0)); |
| | | } else { |
| | | //占比 |
| | | BigDecimal percent = BigDecimal.valueOf(option.getNum()).divide(BigDecimal.valueOf(allSum), 2, RoundingMode.HALF_UP) |
| | | .multiply(BigDecimal.valueOf(100)).setScale(0); |
| | | option.setPercent(percent); |
| | | } |
| | | }); |
| | | comActDiscussOptionVOS.sort(Comparator.comparing(ComActDiscussOptionVO::getNum).reversed()); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public R addDiscussOptionUser(ComActDiscussOptionUserDTO comActDiscussOptionUserDTO) { |
| | | Long discussOptionId = comActDiscussOptionUserDTO.getDiscussOptionId(); |
| | | List<Long> discussOptionIds = comActDiscussOptionUserDTO.getDiscussOptionIds(); |
| | | Long discussOptionId = discussOptionIds.get(0); |
| | | Long userId = comActDiscussOptionUserDTO.getUserId(); |
| | | ComActDiscussOptionDO comActDiscussOptionDO = comActDiscussOptionDAO.selectById(discussOptionId); |
| | | if (ObjectUtils.isEmpty(comActDiscussOptionDO)) { |
| | |
| | | // QueryWrapper<ComActDiscussOptionDO>().lambda().eq(ComActDiscussOptionDO::getDiscussId, discussId)); |
| | | // List<Long> longList=comActDiscussOptionDOS.stream().map(comActDiscussOptionDO1 -> |
| | | // comActDiscussOptionDO1.getId()).collect(Collectors.toList()); |
| | | Integer integer = comActDiscussOptionUserDAO.selectCount( |
| | | new QueryWrapper<ComActDiscussOptionUserDO>().lambda().eq(ComActDiscussOptionUserDO::getUserId, userId) |
| | | .eq(ComActDiscussOptionUserDO::getDiscussOptionId, discussOptionId)); |
| | | if (integer > 0) { |
| | | return R.fail("不要重复投票"); |
| | | Long discussId = comActDiscussOptionDO.getDiscussId(); |
| | | ComActDiscussDO comActDiscussDO = comActDiscussDAO.selectById(comActDiscussOptionDO.getDiscussId()); |
| | | if (isNull(comActDiscussOptionDO)) { |
| | | return R.fail("投票主题不存在"); |
| | | } |
| | | ComActDiscussOptionUserDO comActDiscussOptionUserDO = new ComActDiscussOptionUserDO(); |
| | | comActDiscussOptionUserDO.setUserId(userId); |
| | | comActDiscussOptionUserDO.setDiscussOptionId(discussOptionId); |
| | | int insert = comActDiscussOptionUserDAO.insert(comActDiscussOptionUserDO); |
| | | if (insert > 0) { |
| | | boolean isRepeat = comActDiscussDO.getIsRepeat().intValue() == 1; |
| | | int usableVoteVotes = comActDiscussDO.getCount().intValue(); |
| | | int currentVoteNum = discussOptionIds.size(); |
| | | if (isRepeat) { |
| | | //可重复投票 |
| | | int votedVotesInToday = comActDiscussOptionUserDAO.selectVotedVotesInToday(discussId, userId); |
| | | if (usableVoteVotes <= votedVotesInToday || currentVoteNum > usableVoteVotes - votedVotesInToday) { |
| | | return R.fail("票数不足"); |
| | | } |
| | | } else { |
| | | //不可重复投票 |
| | | int alreadyVotedVotes = comActDiscussOptionUserDAO.selectCount( |
| | | new QueryWrapper<ComActDiscussOptionUserDO>().lambda().eq(ComActDiscussOptionUserDO::getUserId, userId) |
| | | .eq(ComActDiscussOptionUserDO::getDiscussId, discussId)); |
| | | if (usableVoteVotes <= alreadyVotedVotes || currentVoteNum > usableVoteVotes - alreadyVotedVotes) { |
| | | return R.fail("票数不足"); |
| | | } |
| | | } |
| | | List<ComActDiscussOptionUserDO> comActDiscussOptionUserDOList = new ArrayList<>(); |
| | | discussOptionIds.forEach(optionId -> { |
| | | ComActDiscussOptionUserDO comActDiscussOptionUserDO = new ComActDiscussOptionUserDO(); |
| | | comActDiscussOptionUserDO.setUserId(userId); |
| | | comActDiscussOptionUserDO.setDiscussOptionId(optionId); |
| | | comActDiscussOptionUserDO.setDiscussId(discussId); |
| | | comActDiscussOptionUserDOList.add(comActDiscussOptionUserDO); |
| | | }); |
| | | int result = comActDiscussOptionUserDAO.batchInsert(comActDiscussOptionUserDOList); |
| | | if (result > 0) { |
| | | return R.ok(); |
| | | } |
| | | return R.fail(); |
| | |
| | | @Override |
| | | public R discussJurisdictionGet(Long communityId, Long userId) { |
| | | List<SysConfDO> confDOList = sysConfDao.selectList(new LambdaQueryWrapper<SysConfDO>() |
| | | .eq(SysConfDO::getCommunityId, communityId).orderByDesc(SysConfDO::getCreateAt)); |
| | | .eq(SysConfDO::getCommunityId, communityId) |
| | | .eq(SysConfDO::getCode,Constants.DISCUSS_IDENTITY_KEY + communityId) |
| | | .orderByDesc(SysConfDO::getCreateAt)); |
| | | if (confDOList == null || confDOList.size() == 0) { |
| | | SysConfDO sysConfDO = new SysConfDO(); |
| | | sysConfDO.setCode(Constants.DISCUSS_IDENTITY_KEY + communityId); |
| | |
| | | }); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 一起议增加浏览量 |
| | | * @param discussId 一起议主键id |
| | | */ |
| | | @Override |
| | | public void increaseViewNum(Long discussId) { |
| | | String incrKey = String.join("_", DISCUSS_VIEW_NUM_PREFIX, discussId.toString()); |
| | | Boolean isExist = stringRedisTemplate.hasKey(incrKey); |
| | | ValueOperations opsForValue = stringRedisTemplate.opsForValue(); |
| | | if (isExist) { |
| | | opsForValue.increment(incrKey); |
| | | } else { |
| | | ComActDiscussDO comActDiscussDO = comActDiscussDAO.selectById(discussId); |
| | | if (nonNull(comActDiscussDO)) { |
| | | stringRedisTemplate.executePipelined((RedisCallback<Object>) redisConnection -> { |
| | | StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection; |
| | | Integer nowViewNum = Math.addExact(comActDiscussDO.getViewsNum().intValue(), 1); |
| | | stringRedisConnection.set(incrKey, nowViewNum.toString()); |
| | | stringRedisConnection.expire(incrKey, DISCUSS_EXPIRE_TIME); |
| | | stringRedisConnection.sAdd(INCR_VIEW_DISCUSS_IDS_KEY, discussId.toString()); |
| | | stringRedisConnection.expire(INCR_VIEW_DISCUSS_IDS_KEY, DISCUSS_EXPIRE_TIME); |
| | | return null; |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 公布/编辑一起议投票结果 |
| | | * @param comActDiscussDTO |
| | | * @return 请求结果 |
| | | */ |
| | | @Override |
| | | public R editDiscussResult(ComActDiscussDTO comActDiscussDTO) { |
| | | if (isNull(comActDiscussDTO.getId()) || isNull(comActDiscussDTO.getPublishResult())) { |
| | | return R.fail("缺少参数!"); |
| | | } |
| | | ComActDiscussDO comActDiscussDO = comActDiscussDAO.selectById(comActDiscussDTO.getId()); |
| | | if (isNull(comActDiscussDO)) { |
| | | return R.fail("当前一起议主题不存在!"); |
| | | } |
| | | if (!comActDiscussDTO.getUserId().equals(comActDiscussDO.getUserId())) { |
| | | return R.fail("只能公布或编辑自己发布的主题!"); |
| | | } |
| | | comActDiscussDO.setPublishResult(comActDiscussDTO.getPublishResult()); |
| | | comActDiscussDO.setStatus(4); |
| | | int result = comActDiscussDAO.updateById(comActDiscussDO); |
| | | if (result > 0) { |
| | | return R.ok(); |
| | | } |
| | | return R.fail("网络错误,请重试!"); |
| | | } |
| | | |
| | | /** |
| | | * 一起议获取是否有发布权 |
| | | * @param comActDiscussDTO |
| | | * @return |
| | | */ |
| | | @Override |
| | | public R getDiscussPermissions(ComActDiscussDTO comActDiscussDTO) { |
| | | return R.ok(checkCurrentUserDiscussIdentity(comActDiscussDTO)); |
| | | } |
| | | |
| | | /** |
| | | * 将redis中浏览量写入表中 |
| | | * @return 执行结果 |
| | | */ |
| | | @Override |
| | | public R writeDiscussViewNumToTable() { |
| | | try { |
| | | if (stringRedisTemplate.hasKey(INCR_VIEW_DISCUSS_IDS_KEY)) { |
| | | SetOperations opsForSet = stringRedisTemplate.opsForSet(); |
| | | Set<String> needWriteDiscussIdSet = opsForSet.members(INCR_VIEW_DISCUSS_IDS_KEY); |
| | | |
| | | //组装viewNum key |
| | | List<String> viewNumKeys = new ArrayList<>(); |
| | | Iterator<String> iterator = needWriteDiscussIdSet.iterator(); |
| | | while (iterator.hasNext()) { |
| | | viewNumKeys.add(String.join("_", DISCUSS_VIEW_NUM_PREFIX, iterator.next())); |
| | | } |
| | | |
| | | ValueOperations opsForValue = stringRedisTemplate.opsForValue(); |
| | | List<String> viewNumList = opsForValue.multiGet(viewNumKeys); |
| | | |
| | | //待处理集合,以key-value/discussId-viewNum 存放 |
| | | HashMap<Long, Integer> needDealMap = new HashMap<>(); |
| | | for (String discussId : needWriteDiscussIdSet) { |
| | | try { |
| | | needDealMap.put(Long.parseLong(discussId), Integer.parseInt(viewNumList.get(0))); |
| | | viewNumList.remove(0); |
| | | } catch (IndexOutOfBoundsException e) { |
| | | //数据存在异常了,跳过,继续执行正确的 |
| | | continue; |
| | | } |
| | | } |
| | | int result = comActDiscussDAO.batchUpdateViewNum(needDealMap); |
| | | if (result <= 0) { |
| | | return R.fail("处理失败"); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("写入浏览量失败【{}】", e.getMessage()); |
| | | return R.fail("处理失败"); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | } |