Browse Source

app消息通知发送之前存入消息表,增加admin的fegin,增加统一发送app消息通知接口(新&&旧)

sucheng 1 year ago
parent
commit
a2249a2dc4

+ 24 - 0
bs-common/src/main/java/com/rongwei/safecommon/fegin/CXAdminFeginClient.java

@@ -0,0 +1,24 @@
+package com.rongwei.safecommon.fegin;
+
+import com.rongwei.rwcommon.base.R;
+import com.rongwei.rwcommon.vo.CriteriaQuery;
+import com.rongwei.rwcommon.vo.MailDo;
+import com.rongwei.rwcommon.vo.generalsql.BatchSaveVo;
+import com.rongwei.rwcommonentity.commonservers.vo.SysNotifyAnnounceVo;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.http.MediaType;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
+import java.util.Map;
+
+@FeignClient(value = "rw-admin",fallback = CXAdminFeginClientError.class,contextId = "adminFeign")
+public interface CXAdminFeginClient {
+    @PostMapping("sys/generalCRUD/getDataBySql")
+    R getDataBySql(@RequestBody CriteriaQuery query);
+
+    @PostMapping("sys/generalCRUD/batchSaveList")
+    R batchSaveList(@RequestBody BatchSaveVo batchSaveVo);
+}

+ 33 - 0
bs-common/src/main/java/com/rongwei/safecommon/fegin/CXAdminFeginClientError.java

@@ -0,0 +1,33 @@
+package com.rongwei.safecommon.fegin;
+
+import com.rongwei.rwcommon.base.R;
+import com.rongwei.rwcommon.vo.CriteriaQuery;
+import com.rongwei.rwcommon.vo.generalsql.BatchSaveVo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * Hysitx class
+ *
+ * @author XH
+ * @date 2023/05/30
+ */
+@Component
+public class CXAdminFeginClientError implements CXAdminFeginClient {
+    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
+
+    @Override
+    public R getDataBySql(CriteriaQuery query) {
+        log.error("执行自定义sql失败:{}", query);
+        return R.error();
+    }
+
+    @Override
+    public R batchSaveList(BatchSaveVo batchSaveVo) {
+        log.error("批量保存失败:{}", batchSaveVo);
+        return R.error();
+    }
+
+
+}

+ 1 - 0
bs-common/src/main/java/com/rongwei/safecommon/fegin/CXCommonFeginClient.java

@@ -1,6 +1,7 @@
 package com.rongwei.safecommon.fegin;
 
 import com.rongwei.rwcommon.base.R;
+import com.rongwei.rwcommon.vo.CriteriaQuery;
 import com.rongwei.rwcommon.vo.MailDo;
 import com.rongwei.rwcommonentity.commonservers.vo.SysNotifyAnnounceVo;
 import org.springframework.cloud.openfeign.FeignClient;

+ 47 - 16
bs-common/src/main/java/com/rongwei/safecommon/utils/CXCommonUtils.java

@@ -1,5 +1,6 @@
 package com.rongwei.safecommon.utils;
 
+import cn.hutool.core.date.DateUtil;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson.JSONObject;
@@ -10,8 +11,12 @@ import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommon.utils.SecurityUtil;
 import com.rongwei.rwcommon.utils.StringUtils;
+import com.rongwei.rwcommon.vo.CriteriaQuery;
+import com.rongwei.rwcommon.vo.generalsql.BatchSaveVo;
+import com.rongwei.rwcommon.vo.generalsql.GeneralInsertVo;
 import com.rongwei.rwcommonentity.commonservers.vo.SysNotifyAnnounceVo;
 import com.rongwei.rwcommonentity.commonservers.vo.SysSerialVo;
+import com.rongwei.safecommon.fegin.CXAdminFeginClient;
 import com.rongwei.safecommon.fegin.CXCommonFeginClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -22,12 +27,10 @@ import org.springframework.web.context.request.ServletRequestAttributes;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.time.LocalDate;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import static com.rongwei.safecommon.utils.SaveConstans.AQ_ORG_CODE_MAP;
@@ -46,6 +49,8 @@ public class CXCommonUtils {
     private RedisService autoRedisService;
     @Autowired
     private CXCommonFeginClient autoCommonFeginClient;
+    @Resource
+    private CXAdminFeginClient autoAdminFeginClient;
     @Autowired
     private SysSerialNumberService autoSysSerialNumberService;
 
@@ -54,14 +59,15 @@ public class CXCommonUtils {
     private static RedisService redisService;
 
     private static CXCommonFeginClient commonFeginClient;
+    private static CXAdminFeginClient adminFeginClient;
     public static final String DEFAULT_NOTIFY_STATUS = "3";
 
 
-
     @PostConstruct
     public void info() {
         redisService = autoRedisService;
         commonFeginClient = autoCommonFeginClient;
+        adminFeginClient = autoAdminFeginClient;
         sysSerialNumberService = autoSysSerialNumberService;
     }
 
@@ -144,11 +150,37 @@ public class CXCommonUtils {
     public static void sendNotify(String title, String content, String remark,
                                   List<String> recipientIds, String roption,
                                   String notifyStatus) {
-        sendNotify(title, content, remark, recipientIds, roption, notifyStatus,true);
+        sendNotify(title, content, remark, recipientIds, roption, notifyStatus, true);
     }
 
 
-    public static void pushApp(String title, String content, List<String> recipientIds){
+    public static void pushApp(String title, String content, List<String> recipientIds) {
+        if(recipientIds.size() <= 0){
+            return;
+        }
+        BatchSaveVo batchSaveVo = new BatchSaveVo();
+        LinkedList<GeneralInsertVo> generalInserts = new LinkedList<>();
+        for (String recipientId : recipientIds) {
+            String uuid = SecurityUtil.getUUID();
+
+            Map<String, Object> map = new HashMap<>();
+            map.put("ID",uuid);
+            map.put("DELETED","0");
+            map.put("CREATEDATE",DateUtil.now());
+            map.put("TITLE", title);
+            map.put("CONTENT", content);
+            map.put("USERID", recipientId);
+
+            GeneralInsertVo generalInsertVo = new GeneralInsertVo();
+            generalInsertVo.setTablename("app_push_message");
+            generalInsertVo.setInsertcolumns(map);
+            generalInsertVo.setAutoGenId(false);
+            generalInsertVo.setIdCol("ID");
+            generalInserts.add(generalInsertVo);
+        }
+        batchSaveVo.setGeneralInserts(generalInserts);
+        adminFeginClient.batchSaveList(batchSaveVo);
+
         //推送websocket消息给app
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("title", title);
@@ -158,7 +190,6 @@ public class CXCommonUtils {
     }
 
 
-
     /**
      * 发送消息提醒
      *
@@ -171,9 +202,9 @@ public class CXCommonUtils {
      * @param notifyStatus  消息提醒类型
      * @param pushWebsocket 是否向webSocket推送消息
      */
-    public static void sendNotify(String id,String title, String content, String remark,
+    public static void sendNotify(String id, String title, String content, String remark,
                                   List<String> recipientIds, String roption,
-                                  String notifyStatus,boolean pushWebsocket) {
+                                  String notifyStatus, boolean pushWebsocket) {
         if (recipientIds == null || recipientIds.isEmpty()) {
             log.debug("接收人ID为空");
             return;
@@ -184,7 +215,7 @@ public class CXCommonUtils {
         sysNotifyAnnounceVo.setNotifytype(notifyStatus);
         sysNotifyAnnounceVo.setNotifystatus(DEFAULT_NOTIFY_STATUS);
         sysNotifyAnnounceVo.setNotifytitle(title);
-        sysNotifyAnnounceVo.setNotifycontent(content.replace("null"," "));
+        sysNotifyAnnounceVo.setNotifycontent(content.replace("null", " "));
         sysNotifyAnnounceVo.setRemark(remark);
         // 特殊处理
         if (StringUtils.isNotBlank(roption)) {
@@ -194,9 +225,9 @@ public class CXCommonUtils {
         log.debug("开始通过fegin发送消息通知: {}", sysNotifyAnnounceVo);
         commonFeginClient.sendNotify(sysNotifyAnnounceVo);
         // 通过webSocket推送数据
-        if(pushWebsocket){
+        if (pushWebsocket) {
             //防止推送用户过多,导致调用接口报错,实行分流
-            Lists.partition(recipientIds, 10).forEach(userIds->{
+            Lists.partition(recipientIds, 10).forEach(userIds -> {
                 pushApp(title, content, userIds);
             });
         }
@@ -262,6 +293,7 @@ public class CXCommonUtils {
 
     /**
      * 计算时间和当前时间相差几天
+     *
      * @param startDate
      * @return
      */
@@ -281,13 +313,12 @@ public class CXCommonUtils {
      * @return
      * @date 2024/1/29 17:12
      * @author shangmi
-     *
      */
 
-    public static void deleteSendNotify(String notifyType, String roption){
+    public static void deleteSendNotify(String notifyType, String roption) {
         JSONArray data = JSONUtil.parseArray(commonFeginClient.list(new HashMap<String, Object>() {{
             this.put("NOTIFYTYPE", notifyType);
-            if (roption!=null){
+            if (roption != null) {
                 this.put("ROPTION", roption);
             }
         }}).getData());

+ 16 - 0
cx-safe-check/cx-save-check-entity/src/main/java/com/rongwe/scentity/vo/PushMessageToAppRequestVo.java

@@ -0,0 +1,16 @@
+package com.rongwe.scentity.vo;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author :sc
+ * @since :2024/2/5
+ */
+@Data
+public class PushMessageToAppRequestVo {
+    private String title;
+    private String content;
+    private List<String> userIds;
+}

+ 57 - 0
cx-safe-check/cx-save-check-server/src/main/java/com/rongwei/savecheck/controller/AppPushMessageController.java

@@ -0,0 +1,57 @@
+package com.rongwei.savecheck.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.google.common.collect.Lists;
+import com.rongwe.scentity.vo.PushMessageToAppRequestVo;
+import com.rongwei.safecommon.utils.CXCommonUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * @author :sc
+ * @since :2024/2/5
+ */
+@RestController
+@RequestMapping("app/pushMessage")
+@Slf4j
+public class AppPushMessageController {
+    /**
+     * 旧版批量替换沿用版本post,url传参
+     * @param message
+     */
+    @PostMapping("/old/pushToApp")
+    public void pushToWeb(@RequestParam String message){
+        try {
+            JSONObject jsonObject = JSONObject.parseObject(message);
+            String title = jsonObject.getString("title");
+            String content = jsonObject.getString("content");
+            List<String> userIdList = JSON.parseArray(jsonObject.getJSONArray("userIds").toJSONString(), String.class);
+            Lists.partition(userIdList, 10).forEach(userIds -> CXCommonUtils.pushApp(title, content, userIds));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+
+    /**
+     * 新版可用此方式,post实体类传参
+     */
+    @PostMapping("/new/pushToApp")
+    public void pushToWeb(@RequestBody PushMessageToAppRequestVo model){
+        try {
+            String title = model.getTitle();
+            String content = model.getContent();
+            List<String> userIdList = model.getUserIds();
+            Lists.partition(userIdList, 10).forEach(userIds -> CXCommonUtils.pushApp(title, content, userIds));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+
+
+}