From c422763f048e1658a38adfa16c8c51f27f4e59f5 Mon Sep 17 00:00:00 2001
From: liwei <1598061435@qq.com>
Date: Wed, 12 Feb 2025 10:48:12 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E7=95=99=E8=A8=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../entity/MemLeaveMessage.java               |  7 ++++
 .../impl/MemLeaveMessageServiceImpl.java      | 41 +++++++++++++++----
 2 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/rcisoft/business/memLeaveMessage/entity/MemLeaveMessage.java b/src/main/java/org/rcisoft/business/memLeaveMessage/entity/MemLeaveMessage.java
index ef6427b..f2457f9 100644
--- a/src/main/java/org/rcisoft/business/memLeaveMessage/entity/MemLeaveMessage.java
+++ b/src/main/java/org/rcisoft/business/memLeaveMessage/entity/MemLeaveMessage.java
@@ -88,5 +88,12 @@ public class MemLeaveMessage extends CyIdIncreNotDataEntity<MemLeaveMessage> {
       */
      @TableField(exist = false)
      private String isPay;
+
+     /**
+      * 会员wxOpenid
+      */
+     @TableField(exist = false)
+     private String wxOpenid;
+
 }
 
diff --git a/src/main/java/org/rcisoft/business/memLeaveMessage/service/impl/MemLeaveMessageServiceImpl.java b/src/main/java/org/rcisoft/business/memLeaveMessage/service/impl/MemLeaveMessageServiceImpl.java
index f941e77..67a5c32 100644
--- a/src/main/java/org/rcisoft/business/memLeaveMessage/service/impl/MemLeaveMessageServiceImpl.java
+++ b/src/main/java/org/rcisoft/business/memLeaveMessage/service/impl/MemLeaveMessageServiceImpl.java
@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.extern.slf4j.Slf4j;
 import org.rcisoft.business.memInfo.bean.MemberInfoRedisBean;
+import org.rcisoft.business.memInfo.dao.MemInfoRepository;
+import org.rcisoft.business.memInfo.entity.MemInfo;
 import org.rcisoft.business.memLeaveMessage.bean.MemLeaveMessageRedisBean;
 import org.rcisoft.business.memLeaveMessage.dao.MemLeaveMessageRepository;
 import org.rcisoft.business.memLeaveMessage.entity.*;
@@ -51,6 +53,8 @@ public class MemLeaveMessageServiceImpl extends ServiceImpl<MemLeaveMessageRepos
     @Autowired
     private CyRedisServiceImpl cyRedisServiceImpl;
 
+    @Autowired
+    private MemInfoRepository memInfoRepository;
     /**
      * 留言
      * @param memLeaveMessage
@@ -61,16 +65,30 @@ public class MemLeaveMessageServiceImpl extends ServiceImpl<MemLeaveMessageRepos
     public CyPersistModel leaveMessage(MemLeaveMessage memLeaveMessage) {
         String userId = CyUserUtil.getAuthenBusinessId();
         //获取留言每天限制条数
-        List<DictData> articleConfig = dictionaryService.selectByTypes("leave_message_config");
-        DictData dictData = articleConfig.stream().filter(item -> item.getDictLabel().equals("leave_message_limit_count")).findFirst().orElse(null);
-        Integer leaveMessageLimitCount = Integer.valueOf(dictData.getDictValue());
-        //判断留言次数是否大于等于leaveMessageLimitCount
+        List<DictData> leaveMessageConfig = dictionaryService.selectByTypes("leave_message_config");
+        DictData dictData1 = leaveMessageConfig.stream().filter(item -> item.getDictLabel().equals("leave_message_limit_count")).findFirst().orElse(null);
+        DictData dictData2 = leaveMessageConfig.stream().filter(item -> item.getDictLabel().equals("pay_leave_message_limit_count")).findFirst().orElse(null);
+        Integer leaveMessageLimitCount = Integer.valueOf(dictData1.getDictValue());
+        Integer payLeaveMessageLimitCount = Integer.valueOf(dictData2.getDictValue());
+        //判断redis中的留言次数是否大于等于leaveMessageLimitCount
         Object redisLeaveMessageCount = cyRedisServiceImpl.hget(MemLeaveMessageRedisBean.USER_LEAVE_MESSAGE + userId, String.valueOf(memLeaveMessage.getTargetId()));
-        if (redisLeaveMessageCount != null && Integer.parseInt(redisLeaveMessageCount.toString()) >= leaveMessageLimitCount){
+        //超出留言次数的条件:
+        //1:redis中的存储数量>=字典限制数量
+        //2:用户不用金币进行留言
+        //3:超出金币进行留言的限制数量
+        if (redisLeaveMessageCount != null && Integer.parseInt(redisLeaveMessageCount.toString()) >= leaveMessageLimitCount && memLeaveMessage.getIsPay().equals("0")){
             //超出留言次数
-            throw new CyServiceException("留言次数超出限制");
+            if (Integer.parseInt(redisLeaveMessageCount.toString()) >= payLeaveMessageLimitCount){
+                //超出金币进行留言的数量限制
+                throw new CyServiceException(1002,"今日留言次数超出限制");
+            } else {
+                throw new CyServiceException(1001,"留言次数超出限制");
+            }
         } else {
-            //未超出留言次数
+            if (Integer.parseInt(redisLeaveMessageCount.toString()) >= payLeaveMessageLimitCount){
+                //超出金币进行留言的数量限制
+                throw new CyServiceException(1002,"今日留言次数超出限制");
+            }
             //先插入到留言表
             memLeaveMessage.setCreateBy(Integer.valueOf(userId));
             memLeaveMessage.setUpdateBy(Integer.valueOf(userId));
@@ -113,6 +131,15 @@ public class MemLeaveMessageServiceImpl extends ServiceImpl<MemLeaveMessageRepos
                     cyRedisServiceImpl.hset(MemLeaveMessageRedisBean.USER_LEAVE_MESSAGE + userId, String.valueOf(memLeaveMessage.getTargetId()), 1L);
                 }
             }
+            //金币支付进行留言
+            if (memLeaveMessage.getIsPay().equals("1")){
+                //扣除该用户的金币数量
+                DictData dictData3 = leaveMessageConfig.stream().filter(item -> item.getDictLabel().equals("pay_count")).findFirst().orElse(null);
+                Integer payCount = Integer.valueOf(dictData3.getDictValue());
+                MemInfo memInfo = memInfoRepository.selectByOpenId(memLeaveMessage.getWxOpenid());
+                memInfo.setGoldCoinsCount(memInfo.getGoldCoinsCount() - payCount);
+                memInfoRepository.updateById(memInfo);
+            }
             return new CyPersistModel(line);
         }
     }
-- 
2.22.0