From 43f0578e80af82ecae6c61b51bd0539c6b960603 Mon Sep 17 00:00:00 2001
From: puhanshu <a9236326>
Date: 星期二, 16 八月 2022 19:08:32 +0800
Subject: [PATCH] 服务范围优化

---
 springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActDiscussServiceImpl.java |  109 +++++++++++++++++++++++++++++++++---------------------
 1 files changed, 67 insertions(+), 42 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 2dbcd46..4069f73 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
@@ -204,10 +204,13 @@
                 }
                 if (currentUserRoles.isEmpty()) {
                     checkResult = false;
-                }
-                boolean result = sysAllowedRoles.stream().anyMatch(role -> currentUserRoles.contains(role));
-                if (!result) {
-                    checkResult = false;
+                } else {
+                    if (!sysAllowedRoles.contains("1")) {
+                        boolean result = sysAllowedRoles.stream().anyMatch(role -> currentUserRoles.contains(role));
+                        if (!result) {
+                            checkResult = false;
+                        }
+                    }
                 }
             }
         }
@@ -251,7 +254,7 @@
         } else {
             comActDiscussDO.setStatus(ComActDiscussDO.status.yfb);
         }
-        comActDiscussDO.setUpdateAt(new Date());
+        comActDiscussDO.setUpdateAt(nowDate);
         int insert = comActDiscussDAO.updateById(comActDiscussDO);
         if (insert > 0) {
             //如果是投票,需要把投票的选项存储
@@ -299,6 +302,7 @@
         LoginUserInfoVO loginUserInfoVO = comActDiscussDAO.selectUserByUserId(userId);
         comActDiscussVO.setUserName(loginUserInfoVO.getName());
         comActDiscussVO.setUserNickName(loginUserInfoVO.getNickName());
+        comActDiscussVO.setImageUrl(loginUserInfoVO.getImageUrl());
 
         //分页查询评论列表
         PageComActDiscussCommentDTO pageComActDiscussCommentDTO = new PageComActDiscussCommentDTO();
@@ -313,6 +317,11 @@
             }
         });
         comActDiscussVO.setDiscussCommentList(discussComments.getRecords());
+        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)));
+        }
         return R.ok(comActDiscussVO);
     }
 
@@ -393,7 +402,19 @@
      */
     @Override
     public R pageDiscussAdmin(PageComActDiscussDTO pageComActDiscussDTO) {
-        return R.ok(comActDiscussDAO.pageDiscussAdmin(new Page<>(pageComActDiscussDTO.getPageNum(), pageComActDiscussDTO.getPageSize()), pageComActDiscussDTO));
+        IPage<ComActDiscussVO> comActDiscussVOIPage=comActDiscussDAO.pageDiscussAdmin(new Page<>(pageComActDiscussDTO.getPageNum(), pageComActDiscussDTO.getPageSize()), pageComActDiscussDTO);
+        List<ComActDiscussVO> records = comActDiscussVOIPage.getRecords();
+        if (!ObjectUtils.isEmpty(records)) {
+            records.forEach(comActDiscussVO -> {
+                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)));
+                }
+            });
+        }
+        return R.ok(comActDiscussVOIPage);
     }
 
     /**
@@ -597,12 +618,14 @@
         IPage<ComActDiscussVO> iPage = comActDiscussDAO.pageDiscussApplets(page, pageComActDiscussDTO);
         List<ComActDiscussVO> records = iPage.getRecords();
         if (!ObjectUtils.isEmpty(records)) {
-            List<String> viewNumKeys = new ArrayList<>();
             records.forEach(comActDiscussVO -> {
                 Integer type = comActDiscussVO.getType();
                 Long id = comActDiscussVO.getId();
                 String viewNumKey = String.join("_", DISCUSS_VIEW_NUM_PREFIX, id.toString());
-                viewNumKeys.add(viewNumKey);
+                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));
@@ -666,17 +689,6 @@
                     comActDiscussVO.setHaveVote(haveVote > 0 ? 1 : 0);
                 }
             });
-            ValueOperations opsForValue = stringRedisTemplate.opsForValue();
-            List<String> viewNumList = opsForValue.multiGet(viewNumKeys);
-            for (ComActDiscussVO discussVO : records) {
-                try {
-                    discussVO.setViewsNum(Integer.parseInt(viewNumList.get(0)));
-                    viewNumList.remove(0);
-                } catch (IndexOutOfBoundsException e) {
-                    //数据存在异常了,跳过,继续执行正确的
-                    continue;
-                }
-            }
         }
         return R.ok(iPage);
     }
@@ -687,11 +699,13 @@
      */
     private void checkDiscussStatusIsCorrect(ComActDiscussVO comActDiscussVO) {
         //判断discuss状态是否正确
-        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);
+        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);
+            }
         }
     }
 
@@ -904,9 +918,11 @@
             comActDiscussVO.setHaveVote(comActDiscussVO1.getHaveVote());
         }
         String viewNumKey = String.join("_", DISCUSS_VIEW_NUM_PREFIX, id.toString());
-        ValueOperations opsForValue = stringRedisTemplate.opsForValue();
-        int viewNum = Integer.parseInt((String) opsForValue.get(viewNumKey));
-        comActDiscussVO.setViewsNum(viewNum);
+        if (stringRedisTemplate.hasKey(viewNumKey)) {
+            ValueOperations opsForValue = stringRedisTemplate.opsForValue();
+            int viewNum = Integer.parseInt((String) opsForValue.get(viewNumKey));
+            comActDiscussVO.setViewsNum(viewNum);
+        }
         return R.ok(comActDiscussVO);
     }
 
@@ -979,13 +995,9 @@
             comActDiscussOptionUserDO.setUserId(userId);
             comActDiscussOptionUserDO.setDiscussOptionId(optionId);
             comActDiscussOptionUserDO.setDiscussId(discussId);
-            comActDiscussOptionUserDOList.add(comActDiscussOptionUserDO);
+            comActDiscussOptionUserDAO.insert(comActDiscussOptionUserDO);
         });
-        int result = comActDiscussOptionUserDAO.batchInsert(comActDiscussOptionUserDOList);
-        if (result > 0) {
-            return R.ok();
-        }
-        return R.fail();
+        return R.ok();
     }
 
     /**
@@ -1140,7 +1152,9 @@
     @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);
@@ -1296,8 +1310,14 @@
                 //组装viewNum key
                 List<String> viewNumKeys = new ArrayList<>();
                 Iterator<String> iterator = needWriteDiscussIdSet.iterator();
+                List<String> idList = new ArrayList<>();
                 while (iterator.hasNext()) {
-                    viewNumKeys.add(String.join("_", DISCUSS_VIEW_NUM_PREFIX, iterator.next()));
+                    String next = iterator.next();
+                    String key = String.join("_", DISCUSS_VIEW_NUM_PREFIX, next);
+                    if (stringRedisTemplate.hasKey(key)) {
+                        idList.add(next);
+                        viewNumKeys.add(key);
+                    }
                 }
 
                 ValueOperations opsForValue = stringRedisTemplate.opsForValue();
@@ -1305,13 +1325,18 @@
 
                 //待处理集合,以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;
+                if (nonNull(idList) && !idList.isEmpty()) {
+                    log.error("一起议id列表:" + idList.toString());
+                    log.error("一起议浏览量:" + viewNumList.toString());
+                    for (String discussId : idList) {
+                        try {
+                            needDealMap.put(Long.parseLong(discussId), Integer.parseInt(viewNumList.get(0)));
+                            viewNumList.remove(0);
+                        } catch (IndexOutOfBoundsException e) {
+                            log.error("数据异常:" + discussId, e.getMessage());
+                            //数据存在异常了,跳过,继续执行正确的
+                            continue;
+                        }
                     }
                 }
                 int result = comActDiscussDAO.batchUpdateViewNum(needDealMap);

--
Gitblit v1.7.1