Bladeren bron

[update] message:1.调整给订阅用户发消息的逻辑

kelei 6 maanden geleden
bovenliggende
commit
ff130651fe

+ 4 - 0
admin/src/main/java/com/flyer/foster/controller/app/AppSubscribeController.java

@@ -26,6 +26,10 @@ public class AppSubscribeController {
     @Autowired
     private IAppSubscribeService iAppSubscribeService;
 
+    /**
+     * 新增用户订阅记录
+     * @return
+     */
     @PostMapping("")
     public R addSubscribe() {
         return R.ok().result(iAppSubscribeService.addSubscribe());

+ 23 - 1
admin/src/main/java/com/flyer/foster/controller/app/AppUserController.java

@@ -1,7 +1,12 @@
 package com.flyer.foster.controller.app;
 
+import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
 import com.flyer.foster.consts.WechatConst;
+import com.flyer.foster.dto.app.AppSubscribeRespDTO;
 import com.flyer.foster.dto.app.AppUserQueryDTO;
+import com.flyer.foster.entity.AppSubscribe;
+import com.flyer.foster.entity.AppUser;
+import com.flyer.foster.service.IAppSubscribeService;
 import com.flyer.foster.service.IAppUserService;
 import com.flyer.foster.util.WeChatApiUtil;
 import com.flyer.foster.util.WxAccessToken;
@@ -11,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -31,6 +37,8 @@ public class AppUserController {
     @Autowired
     private WeChatApiUtil weChatApiUtil;
 
+    @Autowired
+    private IAppSubscribeService iAppSubscribeService;
 
     @RequestMapping("/login")
     public R login(@RequestBody AppUserQueryDTO dto) {
@@ -54,12 +62,26 @@ public class AppUserController {
 
     /**
      * 给订阅用户发送消息
+     *
      * @return
      */
     @PostMapping("/send-sub-msg")
     public R sendSubscribeMessage() {
         String accessToken = weChatApiUtil.getAccessToken(WechatConst.APP_ID, WechatConst.APP_SECRET);
-        weChatApiUtil.subscribeMessage(accessToken, "oiUkI7dJILmw43YavQ6SD9BuiDsU", WechatConst.SUBSCRIBE_TEMPLATE1);
+
+        // 获取已经订阅用户信息
+        List<AppSubscribeRespDTO> subscribeUserInfoList = iAppSubscribeService.getSubscribeUserInfo();
+        for (AppSubscribeRespDTO respDTO : subscribeUserInfoList) {
+            int count = respDTO.getCount();
+            // 订阅次数-1
+            iAppSubscribeService
+                    .lambdaUpdate()
+                    .set(AppSubscribe::getCount, --count)
+                    .eq(AppSubscribe::getAppUserId, respDTO.getAppUserId())
+                    .update();
+            // 给订阅用户发消息
+            weChatApiUtil.subscribeMessage(accessToken, respDTO.getOpenId(), WechatConst.SUBSCRIBE_TEMPLATE1);
+        }
         return R.ok();
     }
 }

+ 2 - 0
admin/src/main/java/com/flyer/foster/dto/app/AppSubscribeRespDTO.java

@@ -13,4 +13,6 @@ public class AppSubscribeRespDTO {
     private Integer appUserId;
 
     private String openId;
+
+    private Integer count;
 }

+ 10 - 1
admin/src/main/java/com/flyer/foster/schedule/SendSubscribeMsgSchedule.java

@@ -2,10 +2,12 @@ package com.flyer.foster.schedule;
 
 import com.flyer.foster.consts.WechatConst;
 import com.flyer.foster.dto.app.AppSubscribeRespDTO;
+import com.flyer.foster.entity.AppSubscribe;
 import com.flyer.foster.service.IAppSubscribeService;
 import com.flyer.foster.util.WeChatApiUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
@@ -28,7 +30,7 @@ public class SendSubscribeMsgSchedule {
     /**
      * 每天上午9点、晚上9点发送订阅消息
      */
-//    @Scheduled(cron = "0 0 9,21 * * ?")
+    @Scheduled(cron = "0 0 9,21 * * ?")
     public void execute() {
         log.info("begin SendSubscribeMsgSchedule--->execute");
         String accessToken = weChatApiUtil.getAccessToken(WechatConst.APP_ID, WechatConst.APP_SECRET);
@@ -36,6 +38,13 @@ public class SendSubscribeMsgSchedule {
         // 获取已经订阅用户信息
         List<AppSubscribeRespDTO> subscribeUserInfoList = iAppSubscribeService.getSubscribeUserInfo();
         for (AppSubscribeRespDTO respDTO : subscribeUserInfoList) {
+            int count = respDTO.getCount();
+            // 订阅次数-1
+            iAppSubscribeService
+                    .lambdaUpdate()
+                    .set(AppSubscribe::getCount, --count)
+                    .eq(AppSubscribe::getAppUserId, respDTO.getAppUserId())
+                    .update();
             // 给订阅用户发消息
             weChatApiUtil.subscribeMessage(accessToken, respDTO.getOpenId(), WechatConst.SUBSCRIBE_TEMPLATE1);
         }

+ 2 - 0
admin/src/main/resources/mapper/AppSubscribeMapper.xml

@@ -4,8 +4,10 @@
 
     <select id="getSubscribeUserInfo" resultType="com.flyer.foster.dto.app.AppSubscribeRespDTO">
         select a.app_user_id as appUserId,
+               a.count       as count,
                b.open_id     as openId
         from tb_app_subscribe a
                  inner join tb_app_user b on a.app_user_id = b.id
+        where a.count > 0
     </select>
 </mapper>