From 962adfda22354192f98231771423593ade145e00 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Sat, 15 Jul 2023 17:21:09 +0800
Subject: [PATCH] 扫码和关注的事件

---
 src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java  |    4 
 src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java   |   36 +++++++
 src/main/java/cc/mrbird/febs/mall/entity/MallMember.java             |    7 +
 src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java      |   20 ++++
 src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java |  130 +++++++++++++++++++++++++-
 src/main/java/cc/mrbird/febs/pay/controller/AutoReplyController.java |   10 +
 src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java           |   30 +++++
 src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java    |    5 +
 src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java     |   10 ++
 src/main/java/cc/mrbird/febs/mall/service/IAgentService.java         |    4 
 10 files changed, 246 insertions(+), 10 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java b/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java
index 50797b6..afddeb0 100644
--- a/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java
+++ b/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java
@@ -122,4 +122,40 @@
         return BindingBuilder.bind(orderReturnMoneyQueue()).to(orderReturnMoneyExchange()).with(RabbitQueueEnum.ORDER_RETURN_MONEY.getRoute());
     }
 
+
+    /** 新用户注册 start**/
+    @Bean
+    public DirectExchange memberSubExchange() {
+        return new DirectExchange(RabbitQueueEnum.YWG_MEMBER_SUB.getExchange());
+    }
+
+    @Bean
+    public Queue memberSubQueue() {
+        return new Queue(RabbitQueueEnum.YWG_MEMBER_SUB.getQueue());
+    }
+
+    @Bean
+    public Binding memberSubBind() {
+        return BindingBuilder.bind(memberSubQueue()).to(memberSubExchange()).with(RabbitQueueEnum.YWG_MEMBER_SUB.getRoute());
+    }
+    /** 新用户注册 end**/
+
+
+    /** 新用户注册 start**/
+    @Bean
+    public DirectExchange memberSubScanExchange() {
+        return new DirectExchange(RabbitQueueEnum.YWG_MEMBER_SUB_SCAN.getExchange());
+    }
+
+    @Bean
+    public Queue memberSubScanQueue() {
+        return new Queue(RabbitQueueEnum.YWG_MEMBER_SUB_SCAN.getQueue());
+    }
+
+    @Bean
+    public Binding memberSubScanBind() {
+        return BindingBuilder.bind(memberSubScanQueue()).to(memberSubScanExchange()).with(RabbitQueueEnum.YWG_MEMBER_SUB_SCAN.getRoute());
+    }
+    /** 新用户注册 end**/
+
 }
diff --git a/src/main/java/cc/mrbird/febs/mall/entity/MallMember.java b/src/main/java/cc/mrbird/febs/mall/entity/MallMember.java
index 0963c19..b78d9b6 100644
--- a/src/main/java/cc/mrbird/febs/mall/entity/MallMember.java
+++ b/src/main/java/cc/mrbird/febs/mall/entity/MallMember.java
@@ -86,6 +86,13 @@
     public static final Integer ACCOUNT_STATUS_DISABLED = 2;
 
     /**
+     * 是否关注;1-关注 2-未关注
+     */
+    private Integer subStatus;
+    public static final Integer SUB_STATUS_ENABLE = 1;
+    public static final Integer SUB_STATUS_DISABLED = 2;
+
+    /**
      * 账号类型
      */
     private Integer accountType;
diff --git a/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java b/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java
index bccd530..a9a7103 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java
@@ -7,4 +7,8 @@
     void returnMoneyToAgent(Long orderId);
 
     void rankReturnMoney(Long orderId);
+
+    void addMember(String memberOpenId);
+
+    void addMemberScan(String str);
 }
diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
index eb14dd2..62a5254 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
@@ -1,23 +1,26 @@
 package cc.mrbird.febs.mall.service.impl;
 
+import cc.mrbird.febs.common.entity.FebsResponse;
 import cc.mrbird.febs.common.enumerates.AgentLevelEnum;
 import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
+import cc.mrbird.febs.common.exception.FebsException;
 import cc.mrbird.febs.common.utils.AppContants;
-import cc.mrbird.febs.mall.entity.AgentInfo;
-import cc.mrbird.febs.mall.entity.DataDictionaryCustom;
-import cc.mrbird.febs.mall.entity.MallMember;
-import cc.mrbird.febs.mall.entity.MallOrderInfo;
+import cc.mrbird.febs.common.utils.ShareCodeUtil;
+import cc.mrbird.febs.mall.entity.*;
 import cc.mrbird.febs.mall.mapper.*;
 import cc.mrbird.febs.mall.service.IAgentService;
 import cc.mrbird.febs.mall.service.IApiMallMemberService;
 import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
 import cc.mrbird.febs.mall.service.IMallAchieveService;
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.crypto.SecureUtil;
 import com.alibaba.fastjson.JSONObject;
 import jdk.nashorn.internal.ir.IfNode;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -39,6 +42,7 @@
 
     private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
     private final MallMemberMapper memberMapper;
+    private final MallMemberWalletMapper mallMemberWalletMapper;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -136,7 +140,6 @@
     /**
      * 团队业绩是否达标
      *
-     * @param mallMember
      * @param agentInfo
      * @return
      */
@@ -161,4 +164,121 @@
     public void rankReturnMoney(Long orderId) {
 
     }
+
+    @Override
+    public void addMember(String memberOpenId) {
+        MallMember mallMember = memberMapper.selectMemberByOpenId(memberOpenId);
+        //根据OPENID,查询用户信息,如果存在,直接返回
+        if (ObjectUtil.isNotEmpty(mallMember)) {
+            mallMember.setSubStatus(MallMember.SUB_STATUS_ENABLE);
+            memberMapper.updateById(mallMember);
+            return;
+        }
+        mallMember = new MallMember();
+        mallMember.setPassword(SecureUtil.md5("a123456"));
+        mallMember.setName("微信用户");
+        mallMember.setSubStatus(MallMember.SUB_STATUS_ENABLE);
+        mallMember.setReferrerId(null);
+        mallMember.setPhone(null);
+        mallMember.setAccountStatus(MallMember.ACCOUNT_STATUS_ENABLE);
+        mallMember.setAccountType(MallMember.ACCOUNT_TYPE_NORMAL);
+        mallMember.setLevel(AgentLevelEnum.ZERO_LEVEL.name());
+        mallMember.setSex("男");
+        mallMember.setBindPhone(null);
+        memberMapper.insert(mallMember);
+        //生成邀请码
+        String inviteId = ShareCodeUtil.toSerialCode(mallMember.getId());
+        mallMember.setInviteId(inviteId);
+        //推荐人和推荐人链
+        boolean flag = false;
+        String parentId = mallMember.getReferrerId();
+        if (StrUtil.isBlank(parentId)) {
+            flag = true;
+        }
+        String ids = "";
+        while (!flag) {
+            if (StrUtil.isBlank(ids)) {
+                ids += parentId;
+            } else {
+                ids += ("," + parentId);
+            }
+            MallMember parentMember = memberMapper.selectInfoByInviteId(parentId);
+            if (parentMember == null) {
+                break;
+            }
+            parentId = parentMember.getReferrerId();
+            if (StrUtil.isBlank(parentMember.getReferrerId())) {
+                flag = true;
+            }
+        }
+        if (StrUtil.isNotBlank(ids)) {
+            mallMember.setReferrerIds(ids);
+        }
+        memberMapper.updateById(mallMember);
+        //初始化钱包信息
+        MallMemberWallet wallet = new MallMemberWallet();
+        wallet.setBalance(BigDecimal.ZERO);
+        wallet.setMemberId(mallMember.getId());
+        mallMemberWalletMapper.insert(wallet);
+        return;
+    }
+
+    @Override
+    public void addMemberScan(String str) {
+
+        String[] split = StrUtil.split(str, "-");
+        String referrerId = split[0];
+        String openId = split[1];
+        MallMember mallMember = memberMapper.selectMemberByOpenId(openId);
+        if (ObjectUtil.isNotEmpty(mallMember)) {
+            return;
+        }
+        mallMember = new MallMember();
+        mallMember.setPassword(SecureUtil.md5("a123456"));
+        mallMember.setName("微信用户");
+        mallMember.setSubStatus(MallMember.SUB_STATUS_DISABLED);
+        mallMember.setReferrerId(referrerId);
+        mallMember.setPhone(null);
+        mallMember.setAccountStatus(MallMember.ACCOUNT_STATUS_ENABLE);
+        mallMember.setAccountType(MallMember.ACCOUNT_TYPE_NORMAL);
+        mallMember.setLevel(AgentLevelEnum.ZERO_LEVEL.name());
+        mallMember.setSex("男");
+        mallMember.setBindPhone(null);
+        memberMapper.insert(mallMember);
+        //生成邀请码
+        String inviteId = ShareCodeUtil.toSerialCode(mallMember.getId());
+        mallMember.setInviteId(inviteId);
+        //推荐人和推荐人链
+        boolean flag = false;
+        String parentId = mallMember.getReferrerId();
+        if (StrUtil.isBlank(parentId)) {
+            flag = true;
+        }
+        String ids = "";
+        while (!flag) {
+            if (StrUtil.isBlank(ids)) {
+                ids += parentId;
+            } else {
+                ids += ("," + parentId);
+            }
+            MallMember parentMember = memberMapper.selectInfoByInviteId(parentId);
+            if (parentMember == null) {
+                break;
+            }
+            parentId = parentMember.getReferrerId();
+            if (StrUtil.isBlank(parentMember.getReferrerId())) {
+                flag = true;
+            }
+        }
+        if (StrUtil.isNotBlank(ids)) {
+            mallMember.setReferrerIds(ids);
+        }
+        memberMapper.updateById(mallMember);
+        //初始化钱包信息
+        MallMemberWallet wallet = new MallMemberWallet();
+        wallet.setBalance(BigDecimal.ZERO);
+        wallet.setMemberId(mallMember.getId());
+        mallMemberWalletMapper.insert(wallet);
+        return;
+    }
 }
diff --git a/src/main/java/cc/mrbird/febs/pay/controller/AutoReplyController.java b/src/main/java/cc/mrbird/febs/pay/controller/AutoReplyController.java
index 69bfd27..fc7c8b6 100644
--- a/src/main/java/cc/mrbird/febs/pay/controller/AutoReplyController.java
+++ b/src/main/java/cc/mrbird/febs/pay/controller/AutoReplyController.java
@@ -3,7 +3,9 @@
 import cc.mrbird.febs.common.properties.XcxProperties;
 import cc.mrbird.febs.common.utils.SpringContextHolder;
 import cc.mrbird.febs.pay.util.WechatEventUtil;
+import cc.mrbird.febs.pay.util.WeixinServiceUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -24,6 +26,10 @@
 
     private final String wechatToken = "ywgpzfwh";
 
+
+    @Autowired
+    WechatEventUtil wechatEventUtil;
+
     /**
      * 服务器校验
      * @param req 请求
@@ -31,7 +37,7 @@
      */
     @GetMapping("/event")
     public String verification(HttpServletRequest req) {
-        return WechatEventUtil.verification(req, wechatToken);
+        return wechatEventUtil.verification(req, wechatToken);
     }
 
     /**
@@ -46,7 +52,7 @@
         String message = "success";
         PrintWriter out = resp.getWriter();
         try {
-            String messageEvent = WechatEventUtil.messageEvent(req);
+            String messageEvent = wechatEventUtil.messageEvent(req);
             if(messageEvent != null) {
                 message = messageEvent;
             }
diff --git a/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java b/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java
index 4600070..8006d27 100644
--- a/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java
+++ b/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java
@@ -1,5 +1,6 @@
 package cc.mrbird.febs.pay.util;
 
+import cc.mrbird.febs.rabbit.producter.AgentProducer;
 import org.dom4j.io.SAXReader;
 
 import javax.servlet.http.HttpServletRequest;
@@ -11,6 +12,9 @@
 import org.dom4j.Element;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
 import java.io.InputStream;
 import java.util.*;
 
@@ -21,7 +25,12 @@
  *
  * @author shenshao
  */
+@Service(value="wechatEventUtil")
 public class WechatEventUtil {
+
+
+    @Autowired
+    private AgentProducer agentProducer;
 
     private static Logger log = LoggerFactory.getLogger(WechatEventUtil.class);
 
@@ -31,7 +40,7 @@
      * @param req 请求
      * @return 校验结果
      */
-    public static String verification(HttpServletRequest req, String wechatToken) {
+    public String verification(HttpServletRequest req, String wechatToken) {
         // 接收微信服务器发送请求时传递过来的参数
         String signature = req.getParameter("signature");
         String timestamp = req.getParameter("timestamp");
@@ -57,7 +66,7 @@
      * @param req
      * @return
      */
-    public static String messageEvent(HttpServletRequest req) {
+    public String messageEvent(HttpServletRequest req) {
         String result = null;
         Map<String, String> map = xmlToMap(req);
         String fromUserName = map.get("FromUserName");
@@ -68,8 +77,23 @@
         log.info("事件处理:event:{}、msgType:{}、toUserName:{}、fromUserName:{}", eventType, msgType, toUserName, fromUserName);
         if ("event".equals(msgType)) {
             if ("subscribe".equals(eventType)) {
-                result = imgTextMsg(toUserName, fromUserName, "新用户注册", "仅限上海地区手机号用户", "图片地址", "点击后跳转的路径");
+                result = imgTextMsg(toUserName, fromUserName, "新用户注册", "欢迎光临【药王谷铺子】,点击总下角【药王谷铺子】-【商城】。", "图片地址", "点击后跳转的路径");
                 log.info("新增关注事件:toUserName{}、fromUserName{}", toUserName, fromUserName);
+                StringBuffer stringBuffer = new StringBuffer();
+                if(map.containsKey("EventKey")){
+                    /**
+                     * 扫带参数二维码
+                     */
+                    String eventKey = map.get("EventKey");
+                    stringBuffer.append(eventKey);
+                    stringBuffer.append("-");
+                    stringBuffer.append(fromUserName);
+                    agentProducer.sendMemberSubScanMsg(stringBuffer.toString());
+                }
+                /**
+                 * 普通关注事件
+                 */
+                agentProducer.sendMemberSubMsg(fromUserName);
             } else if ("unsubscribe".equals(eventType)) {
                 log.info("取消关注事件:toUserName{}、fromUserName{}", toUserName, fromUserName);
             }
diff --git a/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java b/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java
index 702f5f9..6f420fe 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java
@@ -10,4 +10,9 @@
     public static final String AGENT_AUTO_LEVEL_UP = "queue_agent_auto_level_up_qay";
     public static final String AGENT_RETURN_MONEY = "queue_agent_return_money_qay";
     public static final String ORDER_RETURN_MONEY = "queue_order_return_money_qay";
+    /**
+     * 新增关注-即用户注册
+     */
+    public static final String YWG_MEMBER_SUB = "queue_ywg_member_sub";
+    public static final String YWG_MEMBER_SUB_SCAN = "queue_ywg_member_sub_scan";
 }
diff --git a/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java b/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java
index a32d98e..474f46f 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java
@@ -72,4 +72,24 @@
             log.error("订单返利异常:", e);
         }
     }
+
+    @RabbitListener(queues = QueueConstants.YWG_MEMBER_SUB)
+    public void getMemberSubMsg(String memberOpenId) {
+        log.info("收到新增用户关注:{}", memberOpenId);
+        try {
+            agentService.addMember(memberOpenId);
+        } catch (Exception e) {
+            log.error("新增用户关注异常:", e);
+        }
+    }
+
+    @RabbitListener(queues = QueueConstants.YWG_MEMBER_SUB_SCAN)
+    public void getMemberSubScanMsg(String str) {
+        log.info("收到新增用户扫码:{}", str);
+        try {
+            agentService.addMemberScan(str);
+        } catch (Exception e) {
+            log.error("收到新增用户扫码:", e);
+        }
+    }
 }
diff --git a/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java b/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java
index 25b2216..3149d76 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java
@@ -1,10 +1,14 @@
 package cc.mrbird.febs.rabbit.enumerates;
 
+import cc.mrbird.febs.rabbit.constants.QueueConstants;
 import lombok.Getter;
 
 @Getter
 public enum RabbitQueueEnum {
 
+    YWG_MEMBER_SUB_SCAN("exchange_ywg_member_sub_scan", "route_key_ywg_member_sub_scan",QueueConstants.YWG_MEMBER_SUB_SCAN),
+    YWG_MEMBER_SUB("exchange_ywg_member_sub", "route_key_ywg_member_sub",QueueConstants.YWG_MEMBER_SUB),
+
     DEFAULT("exchange_default_qay", "route_key_default_qay", "queue_default_qay"),
 
     ORDER_CANCEL_DELAY_TTL("exchange_order_delay_ttl_qay", "route_key_order_delay_ttl_qay", "queue_order_delay_ttl_qay"),
diff --git a/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java b/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java
index 0ddb24b..f68e238 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java
@@ -76,4 +76,14 @@
         log.info("发送返利消息:{}", orderId);
         rabbitTemplate.convertAndSend(RabbitQueueEnum.AGENT_REUTRN_MONEY.getExchange(), RabbitQueueEnum.AGENT_REUTRN_MONEY.getRoute(), orderId);
     }
+
+    public void sendMemberSubMsg(String memberOpenId) {
+        log.info("新增用户关注:{}", memberOpenId);
+        rabbitTemplate.convertAndSend(RabbitQueueEnum.YWG_MEMBER_SUB.getExchange(), RabbitQueueEnum.YWG_MEMBER_SUB.getRoute(), memberOpenId);
+    }
+
+    public void sendMemberSubScanMsg(String str) {
+        log.info("新增用户扫码:{}", str);
+        rabbitTemplate.convertAndSend(RabbitQueueEnum.YWG_MEMBER_SUB_SCAN.getExchange(), RabbitQueueEnum.YWG_MEMBER_SUB_SCAN.getRoute(), str);
+    }
 }

--
Gitblit v1.9.1