Sfoglia il codice sorgente

修改报警任务

xueyiming 6 mesi fa
parent
commit
2a0485e8bc

+ 65 - 19
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
 import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
 import com.tzld.piaoquan.wecom.dao.mapper.HistoryMessageMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.HistoryMessageMapper;
-import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
 import com.tzld.piaoquan.wecom.model.bo.MiniprogramRecord;
 import com.tzld.piaoquan.wecom.model.bo.MiniprogramRecord;
 import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
 import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
@@ -13,10 +12,7 @@ import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.HistoryMessageService;
 import com.tzld.piaoquan.wecom.service.HistoryMessageService;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
-import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
-import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
-import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
-import com.tzld.piaoquan.wecom.utils.MessageUtil;
+import com.tzld.piaoquan.wecom.utils.*;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import com.xxl.job.core.handler.annotation.XxlJob;
@@ -31,8 +27,6 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.Date;
 import java.util.List;
 import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
 
 
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_HISTORY_DATA_URL;
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_HISTORY_DATA_URL;
 
 
@@ -59,8 +53,6 @@ public class WeComHistoryDataJob {
     @Autowired
     @Autowired
     private MessageAttachmentService messageAttachmentService;
     private MessageAttachmentService messageAttachmentService;
 
 
-    @Autowired
-    private StaffWithUserMapper staffWithUserMapper;
 
 
     @XxlJob("saveHistoryMessageJob")
     @XxlJob("saveHistoryMessageJob")
     public ReturnT<String> selectHistoryMessageByDay(String param) {
     public ReturnT<String> selectHistoryMessageByDay(String param) {
@@ -116,7 +108,6 @@ public class WeComHistoryDataJob {
                         miniprogramRecord.setVideoId(videoId);
                         miniprogramRecord.setVideoId(videoId);
                         miniprogramRecord.setAttachmentIdx(j + 1);
                         miniprogramRecord.setAttachmentIdx(j + 1);
                         miniprogramRecordList.add(miniprogramRecord);
                         miniprogramRecordList.add(miniprogramRecord);
-
                         String cover = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("cover");
                         String cover = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("cover");
                         String appid = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("appid");
                         String appid = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("appid");
                         String title = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("title");
                         String title = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("title");
@@ -132,15 +123,6 @@ public class WeComHistoryDataJob {
                     JSONArray subMissionList = jsonArray.getJSONObject(i).getJSONArray("sub_mission_list");
                     JSONArray subMissionList = jsonArray.getJSONObject(i).getJSONArray("sub_mission_list");
                     for (int k = 0; k < subMissionList.size(); k++) {
                     for (int k = 0; k < subMissionList.size(); k++) {
                         if (!"sended".equals(subMissionList.getJSONObject(k).getString("status"))) {
                         if (!"sended".equals(subMissionList.getJSONObject(k).getString("status"))) {
-                            Integer id = jsonArray.getJSONObject(i).getInteger("id");
-                            JSONArray staffExtIdList = jsonArray.getJSONObject(i).getJSONArray("staff_id_list");
-                            List<Long> videoIds = messageAttachmentList.stream().map(MessageAttachment::getMiniprogramVideoId).collect(Collectors.toList());
-                            JSONObject error = new JSONObject();
-                            error.put("id", id);
-                            error.put("staff_id_list", staffExtIdList);
-                            error.put("videoIds", videoIds);
-                            error.put("status", subMissionList.getJSONObject(k).getString("status"));
-                            LarkRobotUtil.sendMessage(error.toJSONString());
                             continue;
                             continue;
                         }
                         }
                         List<String> externalUserList = subMissionList.getJSONObject(k).getJSONArray("external_user_list").toJavaList(String.class);
                         List<String> externalUserList = subMissionList.getJSONObject(k).getJSONArray("external_user_list").toJavaList(String.class);
@@ -210,6 +192,70 @@ public class WeComHistoryDataJob {
         }
         }
         return ReturnT.SUCCESS;
         return ReturnT.SUCCESS;
     }
     }
+
+    @XxlJob("sendMessageAlertJob")
+    public ReturnT<String> sendMessageAlert(String param) {
+        XxlJobParam xxlJobParam = new XxlJobParam();
+        if (StringUtils.isNotEmpty(param)) {
+            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
+        }
+        if (xxlJobParam.getStartTime() == null) {
+            xxlJobParam.setStartTime(TimeUtil.getTodayTimestamp() / 1000);
+        }
+        if (xxlJobParam.getEndTime() == null) {
+            xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
+        }
+        Long startTime = xxlJobParam.getStartTime();
+        Long endTime = xxlJobParam.getEndTime();
+        selectAlertHistoryMessage(startTime, endTime);
+        return ReturnT.SUCCESS;
+    }
+
+    public void selectAlertHistoryMessage(Long startTime, Long endTime) {
+        try {
+            Integer total = getHistoryDataTotal(startTime, endTime);
+            if (total == null || total == 0) {
+                return;
+            }
+            int page = total / size + 1;
+            for (int n = 0; n < page; n++) {
+                String res = getHistoryData(size, n * size, startTime, endTime);
+                log.info("selectAlertHistoryMessage size={}, n={}, startTime={}, endTime={}, res={}", size, n, startTime, endTime, res);
+                if (ObjectUtils.isEmpty(res)) {
+                    return;
+                }
+                JSONObject jsonObject = JSONObject.parseObject(res);
+                JSONArray jsonArray = jsonObject.getJSONArray("missions");
+                for (int i = 0; i < jsonArray.size(); i++) {
+                    Date sendAt = jsonArray.getJSONObject(i).getDate("send_at");
+                    JSONArray attachments = jsonArray.getJSONObject(i).getJSONObject("msg_data").getJSONArray("attachments");
+                    List<Long> videoIds = new ArrayList<>();
+                    for (int j = 0; j < attachments.size(); j++) {
+                        String indexPage = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("page");
+                        Long videoId = MessageUtil.getVideoId(indexPage);
+                        videoIds.add(videoId);
+                    }
+                    JSONArray subMissionList = jsonArray.getJSONObject(i).getJSONArray("sub_mission_list");
+                    for (int k = 0; k < subMissionList.size(); k++) {
+                        if (!"sended".equals(subMissionList.getJSONObject(k).getString("status"))) {
+                            Integer id = jsonArray.getJSONObject(i).getInteger("id");
+                            JSONArray staffExtIdList = jsonArray.getJSONObject(i).getJSONArray("staff_id_list");
+                            JSONObject error = new JSONObject();
+                            error.put("id", id);
+                            error.put("sentAt", sendAt);
+                            error.put("staff_id_list", staffExtIdList);
+                            error.put("videoIds", videoIds);
+                            error.put("status", subMissionList.getJSONObject(k).getString("status"));
+                            LarkRobotUtil.sendMessage(error.toJSONString());
+                        }
+                    }
+                }
+            }
+        } catch (IOException e) {
+            log.error("selectAlertHistoryMessage error", e);
+        }
+    }
+
 }
 }
 
 
 
 

+ 2 - 26
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

@@ -12,6 +12,7 @@ import com.tzld.piaoquan.wecom.service.MessageService;
 import com.tzld.piaoquan.wecom.utils.DateUtil;
 import com.tzld.piaoquan.wecom.utils.DateUtil;
 import com.tzld.piaoquan.wecom.utils.MessageUtil;
 import com.tzld.piaoquan.wecom.utils.MessageUtil;
 import com.tzld.piaoquan.wecom.utils.OdpsUtil;
 import com.tzld.piaoquan.wecom.utils.OdpsUtil;
+import com.tzld.piaoquan.wecom.utils.ToolUtils;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import com.xxl.job.core.handler.annotation.XxlJob;
@@ -305,7 +306,7 @@ public class WeComMessageDataJob {
             miniprogram.put("appid", messageAttachment.getAppid());
             miniprogram.put("appid", messageAttachment.getAppid());
             String title = messageAttachment.getTitle();
             String title = messageAttachment.getTitle();
             if (title.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) {
             if (title.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) {
-                title = truncateString(title, MAX_BYTES - 3) + "...";
+                title = ToolUtils.truncateString(title, MAX_BYTES - 3) + "...";
             }
             }
             miniprogram.put("title", title);
             miniprogram.put("title", title);
             miniprogram.put("cover", messageAttachment.getCover());
             miniprogram.put("cover", messageAttachment.getCover());
@@ -351,29 +352,4 @@ public class WeComMessageDataJob {
         return true;
         return true;
     }
     }
 
 
-    private String truncateString(String input, int maxBytes) {
-        if (input == null || maxBytes <= 0) {
-            return "";
-        }
-
-        byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
-        if (bytes.length <= maxBytes) {
-            return input; // 如果字节数已经在限制内,直接返回原字符串
-        }
-
-        // 截取字节数组
-        byte[] truncatedBytes = new byte[maxBytes];
-        System.arraycopy(bytes, 0, truncatedBytes, 0, maxBytes);
-
-        // 将截取的字节数组转换回字符串
-        String truncatedString = new String(truncatedBytes, StandardCharsets.UTF_8);
-
-        // 处理可能的字符截断问题
-        // 如果截取后字符串的字节数仍然大于 maxBytes,向前查找直到找到有效字符
-        while (truncatedString.getBytes(StandardCharsets.UTF_8).length > maxBytes) {
-            truncatedString = truncatedString.substring(0, truncatedString.length() - 1);
-        }
-
-        return truncatedString;
-    }
 }
 }

+ 18 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/TimeUtil.java

@@ -0,0 +1,18 @@
+package com.tzld.piaoquan.wecom.utils;
+
+import java.util.Calendar;
+
+public class TimeUtil {
+
+    //获取当天日期的时间戳  单位毫秒
+    public static long getTodayTimestamp() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.SECOND, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        return calendar.getTimeInMillis();
+    }
+
+
+}

+ 31 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/ToolUtils.java

@@ -0,0 +1,31 @@
+package com.tzld.piaoquan.wecom.utils;
+
+import java.nio.charset.StandardCharsets;
+
+public class ToolUtils {
+    public static String truncateString(String input, int maxBytes) {
+        if (input == null || maxBytes <= 0) {
+            return "";
+        }
+
+        byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
+        if (bytes.length <= maxBytes) {
+            return input; // 如果字节数已经在限制内,直接返回原字符串
+        }
+
+        // 截取字节数组
+        byte[] truncatedBytes = new byte[maxBytes];
+        System.arraycopy(bytes, 0, truncatedBytes, 0, maxBytes);
+
+        // 将截取的字节数组转换回字符串
+        String truncatedString = new String(truncatedBytes, StandardCharsets.UTF_8);
+
+        // 处理可能的字符截断问题
+        // 如果截取后字符串的字节数仍然大于 maxBytes,向前查找直到找到有效字符
+        while (truncatedString.getBytes(StandardCharsets.UTF_8).length > maxBytes) {
+            truncatedString = truncatedString.substring(0, truncatedString.length() - 1);
+        }
+
+        return truncatedString;
+    }
+}