wangyunpeng vor 2 Wochen
Ursprung
Commit
0a34acef21

+ 76 - 58
api-module/src/main/java/com/tzld/piaoquan/api/job/wecom/thirdpart/WeComSendMsgJob.java

@@ -107,70 +107,88 @@ public class WeComSendMsgJob {
             }
             Map<String, List<ThirdPartWeComRoomConfigTask>> roomConfigTaskMap = roomConfigTaskList.stream()
                     .filter(o -> o.getSwitchFlag() == 1).collect(Collectors.groupingBy(ThirdPartWeComRoomConfigTask::getConfigId));
-            for (ThirdPartWeComRoom room : roomList) {
-                if (room.getSendStatus() != 1) {
+            sendStaffRoomMsg(staff, time, roomList, roomConfigMap, roomConfigTaskMap);
+        }
+        return ReturnT.SUCCESS;
+    }
+
+    private void sendStaffRoomMsg(ThirdPartWeComStaff staff,
+                                  String time,
+                                  List<ThirdPartWeComRoom> roomList,
+                                  Map<Long, ThirdPartWeComRoomConfig> roomConfigMap,
+                                  Map<String, List<ThirdPartWeComRoomConfigTask>> roomConfigTaskMap) {
+        for (ThirdPartWeComRoom room : roomList) {
+            if (room.getSendStatus() != 1) {
+                continue;
+            }
+            pool.execute(() -> {
+                ThirdPartWeComRoomConfig roomConfig = roomConfigMap.get(room.getId());
+                if (roomConfig == null) {
+                    return;
+                }
+                List<ThirdPartWeComRoomConfigTask> taskList = roomConfigTaskMap.getOrDefault(roomConfig.getId(), new ArrayList<>());
+                if (taskList.isEmpty()) {
+                    return;
+                }
+                List<String> configTaskIds = taskList.stream().map(ThirdPartWeComRoomConfigTask::getId).collect(Collectors.toList());
+                List<ThirdPartWeComRoomConfigTaskContent> taskContentList = weComThirdPartyRoomService.getRoomConfigTaskContents(configTaskIds);
+                if (CollectionUtils.isEmpty(taskContentList)) {
+                    return;
+                }
+                Map<String, List<ThirdPartWeComRoomConfigTaskContent>> taskContentMap = taskContentList.stream()
+                        .collect(Collectors.groupingBy(ThirdPartWeComRoomConfigTaskContent::getTaskId));
+                ThirdPartWeComStaff pushStaff = getSendStaff(roomConfig);
+                if (Objects.isNull(pushStaff)) {
+                    LarkRobotUtil.sendWeComThirdPartMessage(
+                            "【账号发送群消息,推送账号获取失败通知】\n" +
+                                    "账号名称:" + staff.getName() + "\n" +
+                                    "账号VID:" + staff.getThirdStaffId() + "\n" +
+                                    "群名称:" + room.getName());
+                    return;
+                }
+                sendRoomConfigTaskMsg(staff, pushStaff, time, room, roomConfig, taskList, taskContentMap);
+            });
+        }
+    }
+
+    private void sendRoomConfigTaskMsg(ThirdPartWeComStaff staff,
+                                       ThirdPartWeComStaff pushStaff,
+                                       String time,
+                                       ThirdPartWeComRoom room,
+                                       ThirdPartWeComRoomConfig roomConfig,
+                                       List<ThirdPartWeComRoomConfigTask> taskList,
+                                       Map<String, List<ThirdPartWeComRoomConfigTaskContent>> taskContentMap) {
+        for (ThirdPartWeComRoomConfigTask configTask : taskList) {
+            List<String> timeList = getRoomSendTime(room, roomConfig, configTask);
+            if (timeList.contains(time)) {
+                // 选取视频
+                List<ThirdPartWeComRoomConfigTaskContent> contentList = taskContentMap.get(configTask.getId());
+                if (Objects.isNull(contentList) || contentList.isEmpty()) {
                     continue;
                 }
-                pool.execute(() -> {
-                    ThirdPartWeComRoomConfig roomConfig = roomConfigMap.get(room.getId());
-                    if (roomConfig == null) {
-                        return;
-                    }
-                    List<ThirdPartWeComRoomConfigTask> taskList = roomConfigTaskMap.getOrDefault(roomConfig.getId(), new ArrayList<>());
-                    if (taskList.isEmpty()) {
-                        return;
-                    }
-                    List<String> configTaskIds = taskList.stream().map(ThirdPartWeComRoomConfigTask::getId).collect(Collectors.toList());
-                    List<ThirdPartWeComRoomConfigTaskContent> taskContentList = weComThirdPartyRoomService.getRoomConfigTaskContents(configTaskIds);
-                    if (CollectionUtils.isEmpty(taskContentList)) {
-                        return;
-                    }
-                    Map<String, List<ThirdPartWeComRoomConfigTaskContent>> taskContentMap = taskContentList.stream()
-                            .collect(Collectors.groupingBy(ThirdPartWeComRoomConfigTaskContent::getTaskId));
-                    ThirdPartWeComStaff pushStaff = getSendStaff(roomConfig);
-                    if (Objects.isNull(pushStaff)) {
-                        LarkRobotUtil.sendWeComThirdPartMessage(
-                                "【账号发送群消息,推送账号获取失败通知】\n" +
-                                        "账号名称:" + staff.getName() + "\n" +
-                                        "账号VID:" + staff.getThirdStaffId() + "\n" +
-                                        "群名称:" + room.getName());
-                        return;
-                    }
-                    for (ThirdPartWeComRoomConfigTask configTask : taskList) {
-                        List<String> timeList = getRoomSendTime(room, roomConfig, configTask);
-                        if (timeList.contains(time)) {
-                            // 选取视频
-                            List<ThirdPartWeComRoomConfigTaskContent> contentList = taskContentMap.get(configTask.getId());
-                            if (Objects.isNull(contentList) || contentList.isEmpty()) {
-                                continue;
-                            }
-                            contentList = contentList.stream().sorted(Comparator.comparing(ThirdPartWeComRoomConfigTaskContent::getSeq)).collect(Collectors.toList());
-                            for (ThirdPartWeComRoomConfigTaskContent configTaskContent : contentList) {
-                                if (ConfigTaskContentTypeEnum.TEXT.getVal() == configTaskContent.getType()) {
-                                    // build发送体
-                                    SendTextMsgRequest request = buildSendTextMsgRequest(configTaskContent.getContent(), pushStaff, room);
-                                    // 发送消息
-                                    CommonResponse<SendTextMsgResponse> response = weComThirdPartyService.sendTextMsg(pushStaff, room, request);
-                                    // 存储消息
-                                    saveTextWeComMsg(pushStaff.getId(), configTaskContent.getContent(), request, response);
-                                } else if (ConfigTaskContentTypeEnum.MINI_PROGRAM.getVal() == configTaskContent.getType()) {
-                                    List<CgiReplyBucketData> cgiReplyBucketDataList = getCgiReplyBucketData(room.getThirdRoomId(), staff, roomConfig, configTaskContent);
-                                    for (CgiReplyBucketData cgiReplyBucketData : cgiReplyBucketDataList) {
-                                        // build发送体
-                                        SendAppMsgRequest request = buildSendAppMsgRequest(cgiReplyBucketData, pushStaff, room);
-                                        // 发送消息
-                                        CommonResponse<SendAppMsgResponse> response = weComThirdPartyService.sendAppMsg(pushStaff, room, request);
-                                        // 存储消息
-                                        saveAppWeComMsg(pushStaff.getId(), cgiReplyBucketData.getMiniVideoId(), request, response);
-                                    }
-                                }
-                            }
+                contentList = contentList.stream().sorted(Comparator.comparing(ThirdPartWeComRoomConfigTaskContent::getSeq)).collect(Collectors.toList());
+                for (ThirdPartWeComRoomConfigTaskContent configTaskContent : contentList) {
+                    if (ConfigTaskContentTypeEnum.TEXT.getVal() == configTaskContent.getType()) {
+                        // build发送体
+                        SendTextMsgRequest request = buildSendTextMsgRequest(configTaskContent.getContent(), pushStaff, room);
+                        // 发送消息
+                        CommonResponse<SendTextMsgResponse> response = weComThirdPartyService.sendTextMsg(pushStaff, room, request);
+                        // 存储消息
+                        saveTextWeComMsg(pushStaff.getId(), configTaskContent.getContent(), request, response);
+                    } else if (ConfigTaskContentTypeEnum.MINI_PROGRAM.getVal() == configTaskContent.getType()) {
+                        List<CgiReplyBucketData> cgiReplyBucketDataList = getCgiReplyBucketData(room.getThirdRoomId(), staff, roomConfig, configTaskContent);
+                        for (CgiReplyBucketData cgiReplyBucketData : cgiReplyBucketDataList) {
+                            // build发送体
+                            SendAppMsgRequest request = buildSendAppMsgRequest(cgiReplyBucketData, pushStaff, room);
+                            // 发送消息
+                            CommonResponse<SendAppMsgResponse> response = weComThirdPartyService.sendAppMsg(pushStaff, room, request);
+                            // 存储消息
+                            saveAppWeComMsg(pushStaff.getId(), cgiReplyBucketData.getMiniVideoId(), request, response);
                         }
                     }
-                });
+                }
             }
         }
-        return ReturnT.SUCCESS;
     }
 
     private ThirdPartWeComStaff getSendStaff(ThirdPartWeComRoomConfig roomConfig) {

+ 7 - 0
api-module/src/main/java/com/tzld/piaoquan/api/job/wecom/thirdpart/WeComUserDetailJob.java

@@ -96,6 +96,11 @@ public class WeComUserDetailJob {
     }
 
     public void syncStaffUserDetail(ThirdPartWeComStaff staff) {
+        // redis锁
+        String lockKey = "wecom:thirdpart:userDetail:lock:" + staff.getThirdUuid();
+        if (!redisUtils.tryLock(lockKey, "1", 5 * 60)) {
+            return;
+        }
         String uuid = staff.getThirdUuid();
         String offLineKey = "wecom:thirdpart:offline:" + uuid;
         if (redisUtils.containsKey(offLineKey)) {
@@ -117,6 +122,8 @@ public class WeComUserDetailJob {
         }
         syncRoomList(staff);
         syncStaffUserList(staff);
+        // 清除redis锁
+        redisUtils.del(lockKey);
     }
 
     private void syncStaffUserList(ThirdPartWeComStaff staff) {

+ 42 - 30
api-module/src/main/java/com/tzld/piaoquan/api/job/wecom/thirdpart/WeComVideoJob.java

@@ -78,42 +78,55 @@ public class WeComVideoJob {
                 saveList.add(item);
             }
             // 获取视频封面
-            for (List<ThirdPartWeComVideo> partition : Lists.partition(saveList, 20)) {
-                List<Long> videoIds = partition.stream().map(ThirdPartWeComVideo::getVideoId).distinct().collect(Collectors.toList());
-                Map<Long, VideoDetail> coverMap = messageAttachmentService.getVideoDetail(new HashSet<>(videoIds));
-                for (ThirdPartWeComVideo item : partition) {
-                    VideoDetail detail = coverMap.get(item.getVideoId());
-                    if (Objects.nonNull(detail)) {
-                        String cover = detail.getCover().substring(0, detail.getCover().indexOf("/watermark"));
-                        item.setCover(cover);
-                        if (detail.getAuditStatus() != 5) {
-                            item.setStatus(0);
-                        }
+            setVideoCover(saveList);
+            // save
+            batchSaveVideo(dt, saveList, now);
+        }
+        // 更新封面
+        updateVideoCover();
+        return ReturnT.SUCCESS;
+    }
+
+    private void setVideoCover(List<ThirdPartWeComVideo> videoList) {
+        for (List<ThirdPartWeComVideo> partition : Lists.partition(videoList, 20)) {
+            List<Long> videoIds = partition.stream().map(ThirdPartWeComVideo::getVideoId).distinct().collect(Collectors.toList());
+            Map<Long, VideoDetail> coverMap = messageAttachmentService.getVideoDetail(new HashSet<>(videoIds));
+            for (ThirdPartWeComVideo item : partition) {
+                VideoDetail detail = coverMap.get(item.getVideoId());
+                if (Objects.nonNull(detail)) {
+                    String cover = detail.getCover().substring(0, detail.getCover().indexOf("/watermark"));
+                    item.setCover(cover);
+                    if (detail.getAuditStatus() != 5) {
+                        item.setStatus(0);
                     }
                 }
             }
-            // save
-            if (CollectionUtils.isNotEmpty(saveList)) {
-                List<ThirdPartWeComVideo> videoList = getVideoList(dt);
-                List<ContentPlatformIllegalVideo> illegalVideoList = getAllIllegalVideoList();
-                List<Long> illegalVideoIds = illegalVideoList.stream().map(ContentPlatformIllegalVideo::getVideoId).collect(Collectors.toList());
-                if (CollectionUtils.isNotEmpty(illegalVideoIds)) {
-                    saveList = saveList.stream().filter(item -> !illegalVideoIds.contains(item.getVideoId())).collect(Collectors.toList());
-                }
-                if (CollectionUtils.isNotEmpty(videoList)) {
-                    List<Long> tempExistVideoIds = videoList.stream().map(ThirdPartWeComVideo::getVideoId).collect(Collectors.toList());
-                    List<Long> videoIdList = saveList.stream().map(ThirdPartWeComVideo::getVideoId).collect(Collectors.toList());
-                    videoMapperExt.updateOtherVideoStatus(dt, videoIdList, now);
-                    saveList = saveList.stream().filter(item -> !tempExistVideoIds.contains(item.getVideoId())).collect(Collectors.toList());
-                    if (CollectionUtils.isNotEmpty(saveList)) {
-                        videoMapperExt.batchInsertThirdPartWeComVideo(saveList);
-                    }
-                } else {
+        }
+    }
+
+    private void batchSaveVideo(String dt, List<ThirdPartWeComVideo> saveList, Long now) {
+        if (CollectionUtils.isNotEmpty(saveList)) {
+            List<ThirdPartWeComVideo> videoList = getVideoList(dt);
+            List<ContentPlatformIllegalVideo> illegalVideoList = getAllIllegalVideoList();
+            List<Long> illegalVideoIds = illegalVideoList.stream().map(ContentPlatformIllegalVideo::getVideoId).collect(Collectors.toList());
+            if (CollectionUtils.isNotEmpty(illegalVideoIds)) {
+                saveList = saveList.stream().filter(item -> !illegalVideoIds.contains(item.getVideoId())).collect(Collectors.toList());
+            }
+            if (CollectionUtils.isNotEmpty(videoList)) {
+                List<Long> tempExistVideoIds = videoList.stream().map(ThirdPartWeComVideo::getVideoId).collect(Collectors.toList());
+                List<Long> videoIdList = saveList.stream().map(ThirdPartWeComVideo::getVideoId).collect(Collectors.toList());
+                videoMapperExt.updateOtherVideoStatus(dt, videoIdList, now);
+                saveList = saveList.stream().filter(item -> !tempExistVideoIds.contains(item.getVideoId())).collect(Collectors.toList());
+                if (CollectionUtils.isNotEmpty(saveList)) {
                     videoMapperExt.batchInsertThirdPartWeComVideo(saveList);
                 }
+            } else {
+                videoMapperExt.batchInsertThirdPartWeComVideo(saveList);
             }
         }
-        // 更新封面
+    }
+
+    private void updateVideoCover() {
         List<ThirdPartWeComVideo> nullCoverVideoList = getVideoNullCover();
         if (CollectionUtils.isNotEmpty(nullCoverVideoList)) {
             for (List<ThirdPartWeComVideo> partition : Lists.partition(nullCoverVideoList, 20)) {
@@ -138,7 +151,6 @@ public class WeComVideoJob {
                 }
             }
         }
-        return ReturnT.SUCCESS;
     }
 
     private List<ContentPlatformIllegalVideo> getAllIllegalVideoList() {