From 15d3711a5321e9a5c38b66320ca6bff5b96a3b50 Mon Sep 17 00:00:00 2001
From: huanghongfa <huanghongfa123456>
Date: 星期六, 18 九月 2021 16:00:43 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/test' into test

---
 springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActDiscussServiceImpl.java |  108 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 76 insertions(+), 32 deletions(-)

diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActDiscussServiceImpl.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActDiscussServiceImpl.java
index fef0c1e..8db80c5 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActDiscussServiceImpl.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActDiscussServiceImpl.java
@@ -188,26 +188,27 @@
             String discussIdentityConfig = (String) sysConfValue.getData();
             if (isBlank(discussIdentityConfig)) {
                 checkResult = false;
-            }
-            List<String> currentUserRoles = new ArrayList<>();
-            List<String> sysAllowedRoles = Arrays.asList(discussIdentityConfig.split(","));
-            LoginUserInfoVO loginUserInfo = comActDiscussDTO.getLoginUserInfo();
-            if (loginUserInfo.getIsPartymember().intValue() == 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;
-            }
-            boolean result = sysAllowedRoles.stream().anyMatch(role -> currentUserRoles.contains(role));
-            if (!result) {
-                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;
+                }
+                boolean result = sysAllowedRoles.stream().anyMatch(role -> currentUserRoles.contains(role));
+                if (!result) {
+                    checkResult = false;
+                }
             }
         }
         return checkResult;
@@ -599,10 +600,16 @@
             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 =
@@ -616,6 +623,7 @@
                             // 判断选项是否已投票
                             int haveVote =
                                     comActDiscussOptionUserDAO.selectHaveVotes(comActDiscussOptionDO.getId(), loginUserId);
+                            votes.add(haveVote);
                             comActDiscussOptionVO.setHaveVote(haveVote > 0 ? 1 : 0);
                         }
                         comActDiscussOptionVOS.add(comActDiscussOptionVO);
@@ -623,6 +631,20 @@
                     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());
@@ -872,7 +894,12 @@
             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);
     }
 
@@ -904,7 +931,8 @@
      */
     @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)) {
@@ -922,27 +950,32 @@
         }
         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) {
-                return R.fail("当天投票次数已用完");
+            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) {
-                return R.fail("投票次数已用完");
+            if (usableVoteVotes <= alreadyVotedVotes || currentVoteNum > usableVoteVotes - alreadyVotedVotes) {
+                return R.fail("票数不足");
             }
         }
-        ComActDiscussOptionUserDO comActDiscussOptionUserDO = new ComActDiscussOptionUserDO();
-        comActDiscussOptionUserDO.setUserId(userId);
-        comActDiscussOptionUserDO.setDiscussOptionId(discussOptionId);
-        comActDiscussOptionUserDO.setDiscussId(discussId);
-        int insert = comActDiscussOptionUserDAO.insert(comActDiscussOptionUserDO);
-        if (insert > 0) {
+        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();
@@ -1226,6 +1259,7 @@
             return R.fail("只能公布或编辑自己发布的主题!");
         }
         comActDiscussDO.setPublishResult(comActDiscussDTO.getPublishResult());
+        comActDiscussDO.setStatus(4);
         int result = comActDiscussDAO.updateById(comActDiscussDO);
         if (result > 0) {
             return R.ok();
@@ -1234,6 +1268,16 @@
     }
 
     /**
+     * 一起议获取是否有发布权
+     * @param comActDiscussDTO
+     * @return
+     */
+    @Override
+    public R getDiscussPermissions(ComActDiscussDTO comActDiscussDTO) {
+        return R.ok(checkCurrentUserDiscussIdentity(comActDiscussDTO));
+    }
+
+    /**
      * 将redis中浏览量写入表中
      * @return 执行结果
      */

--
Gitblit v1.7.1