Procházet zdrojové kódy

修改保底视频上传方式

xueyiming před 4 měsíci
rodič
revize
4fc670069d

+ 178 - 47
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/MessageAttachmentServiceImpl.java

@@ -84,44 +84,175 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
         }
     }
 
-    @Override
+//    @Override
+//    public CommonResponse<Void> createGuaranteedMiniProgram(GuaranteedParam guaranteedParam) {
+//        if (guaranteedParam == null
+//                || StringUtils.isEmpty(guaranteedParam.getDate())
+//                || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
+//            return CommonResponse.create(500, "参数错误");
+//        }
+//        Set<Long> videoIds = new HashSet<>();
+//        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
+//            if (CollectionUtils.isEmpty(videoParam.getVideoIds()) || videoParam.getVideoIds().size() < MAX_VIDEO_NUM) {
+//                LarkRobotUtil.sendMessage("保底视频数量异常,请查看" + guaranteedParam.getDate());
+//            }
+//            if (CollectionUtils.isEmpty(videoParam.getStaffIds())) {
+//                videoParam.setStaffIds(new ArrayList<>());
+//                if (videoParam.getStaffId() != null) {
+//                    videoParam.getStaffIds().add(videoParam.getStaffId());
+//                }
+//            }
+//            if (CollectionUtils.isEmpty(videoParam.getStaffIds())) {
+//                continue;
+//            }
+//            for (Long staffId : videoParam.getStaffIds()) {
+//                for (Long videoId : videoParam.getVideoIds()) {
+//                    MessageAttachmentExample example = new MessageAttachmentExample();
+//                    example.createCriteria().andMiniprogramVideoIdEqualTo(videoId).andStaffIdEqualTo(videoParam.getStaffId());
+//                    List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
+//                    if (CollectionUtils.isEmpty(messageAttachmentList)) {
+//                        continue;
+//                    }
+//                    MessageAttachment messageAttachment = messageAttachmentList.get(0);
+//                    if (messageAttachment.getSendTime() != null
+//                            && DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 90 * MILLISECOND_DAY) {
+//                        LarkRobotUtil.sendMessage("保底视频90天内已发送,请查看videoId=" + videoId);
+//                        return CommonResponse.create(500, "保底视频90天内已发送,请查看videoId=" + videoId);
+//                    }
+//                }
+//                videoIds.addAll(videoParam.getVideoIds());
+//            }
+//        }
+//        Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
+//        Map<Long, MessageAttachment> messageAttachmentMap = new HashMap<>();
+//
+//        for (Long videoId : videoIds) {
+//            MessageAttachment messageAttachment = new MessageAttachment();
+//            VideoDetail videoDetail = coverMap.get(videoId);
+//            if (videoDetail == null || StringUtils.isEmpty(videoDetail.getCover()) || StringUtils.isEmpty(videoDetail.getTitle())) {
+//                LarkRobotUtil.sendMessage("获取视频详情异常,请查看" + videoId);
+//                throw new RuntimeException("获取视频详情异常");
+//            }
+//            messageAttachment.setMiniprogramVideoId(videoId);
+//            messageAttachment.setType(MessageAttachmentTypeEnum.MINI_PROGRAM.getType());
+//            messageAttachment.setCover(videoDetail.getCover());
+//            messageAttachment.setTitle(videoDetail.getTitle());
+//            messageAttachment.setAppid(appid);
+//            messageAttachmentMap.put(videoId, messageAttachment);
+//        }
+//        List<MessageAttachment> messageAttachmentList = new ArrayList<>();
+//        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
+//            if (CollectionUtils.isEmpty(videoParam.getStaffIds())) {
+//                videoParam.setStaffIds(new ArrayList<>());
+//                if (videoParam.getStaffId() != null) {
+//                    videoParam.getStaffIds().add(videoParam.getStaffId());
+//                }
+//            }
+//            if (CollectionUtils.isEmpty(videoParam.getStaffIds())) {
+//                continue;
+//            }
+//            for (Long staffId : videoParam.getStaffIds()) {
+//                if (staffId == 0) {
+//                    addGuaranteesVideo(guaranteedParam.getDate(), staffId, videoParam.getVideoIds());
+//                    continue;
+//                }
+//                for (Long videoId : videoParam.getVideoIds()) {
+//                    MessageAttachment messageAttachment = messageAttachmentMap.get(videoId);
+//                    if (messageAttachment == null) {
+//                        continue;
+//                    }
+//                    MessageAttachment newMessageAttachment = new MessageAttachment();
+//                    BeanUtils.copyProperties(messageAttachment, newMessageAttachment);
+//                    newMessageAttachment.setStaffId(staffId);
+//                    messageAttachmentList.add(newMessageAttachment);
+//                    addGuaranteesVideo(guaranteedParam.getDate(), staffId, videoParam.getVideoIds());
+//                }
+//            }
+//        }
+//        addMiniProgram(messageAttachmentList, null);
+//        return CommonResponse.success();
+//    }
+//
+//    private void addGuaranteesVideo(String date, Long staffId, List<Long> videoIds) {
+//        GuaranteesVideo guaranteesVideo = new GuaranteesVideo();
+//        guaranteesVideo.setDate(date);
+//        guaranteesVideo.setStaffId(staffId);
+//        guaranteesVideo.setVideoIds(JSONObject.toJSONString(videoIds));
+//        guaranteesVideoMapper.insertSelective(guaranteesVideo);
+//    }
+
     public CommonResponse<Void> createGuaranteedMiniProgram(GuaranteedParam guaranteedParam) {
-        if (guaranteedParam == null
-                || StringUtils.isEmpty(guaranteedParam.getDate())
-                || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
+        if (isInvalidGuaranteedParam(guaranteedParam)) {
             return CommonResponse.create(500, "参数错误");
         }
+
         Set<Long> videoIds = new HashSet<>();
         for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
-            if (CollectionUtils.isEmpty(videoParam.getVideoIds()) || videoParam.getVideoIds().size() < MAX_VIDEO_NUM) {
-                LarkRobotUtil.sendMessage("保底视频数量异常,请查看" + guaranteedParam.getDate());
-            }
-            for (Long videoId : videoParam.getVideoIds()) {
-                MessageAttachmentExample example = new MessageAttachmentExample();
-                example.createCriteria().andMiniprogramVideoIdEqualTo(videoId).andStaffIdEqualTo(videoParam.getStaffId());
-                List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
-                if (CollectionUtils.isEmpty(messageAttachmentList)) {
-                    continue;
-                }
-                MessageAttachment messageAttachment = messageAttachmentList.get(0);
-                if (messageAttachment.getSendTime() != null
-                        && DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 90 * MILLISECOND_DAY) {
-                    LarkRobotUtil.sendMessage("保底视频90天内已发送,请查看videoId=" + videoId);
-                    return CommonResponse.create(500, "保底视频90天内已发送,请查看videoId=" + videoId);
-                }
+            processVideoParam(videoParam, guaranteedParam.getDate(), videoIds);
+        }
+
+        Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
+        Map<Long, MessageAttachment> messageAttachmentMap = createMessageAttachmentMap(videoIds, coverMap);
+
+        List<MessageAttachment> messageAttachmentList = new ArrayList<>();
+        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
+            addMessageAttachments(videoParam, messageAttachmentMap, guaranteedParam.getDate(), messageAttachmentList);
+        }
+
+        addMiniProgram(messageAttachmentList, null);
+        return CommonResponse.success();
+    }
+
+    private boolean isInvalidGuaranteedParam(GuaranteedParam guaranteedParam) {
+        return guaranteedParam == null ||
+                StringUtils.isEmpty(guaranteedParam.getDate()) ||
+                CollectionUtils.isEmpty(guaranteedParam.getVideoParamList());
+    }
+
+
+    private void processVideoParam(VideoParam videoParam, String date, Set<Long> videoIds) {
+        if (CollectionUtils.isEmpty(videoParam.getVideoIds()) || videoParam.getVideoIds().size() < MAX_VIDEO_NUM) {
+            LarkRobotUtil.sendMessage("保底视频数量异常,请查看" + date);
+        }
+        List<Long> staffIds = Optional.ofNullable(videoParam.getStaffIds()).orElseGet(() -> {
+            List<Long> newStaffIds = new ArrayList<>();
+            if (videoParam.getStaffId() != null) {
+                newStaffIds.add(videoParam.getStaffId());
             }
+            return newStaffIds;
+        });
+        for (Long staffId : staffIds) {
+            checkVideoSendTime(videoParam, staffId);
             videoIds.addAll(videoParam.getVideoIds());
         }
-        Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
-        Map<Long, MessageAttachment> messageAttachmentMap = new HashMap<>();
+    }
 
+    private void checkVideoSendTime(VideoParam videoParam, Long staffId) {
+        for (Long videoId : videoParam.getVideoIds()) {
+            MessageAttachmentExample example = new MessageAttachmentExample();
+            example.createCriteria().andMiniprogramVideoIdEqualTo(videoId).andStaffIdEqualTo(staffId);
+            List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
+            if (CollectionUtils.isEmpty(messageAttachmentList)) {
+                continue;
+            }
+            MessageAttachment messageAttachment = messageAttachmentList.get(0);
+            if (messageAttachment.getSendTime() != null &&
+                    DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 90 * MILLISECOND_DAY) {
+                LarkRobotUtil.sendMessage("保底视频90天内已发送,请查看videoId=" + videoId);
+                throw new RuntimeException("保底视频90天内已发送,请查看videoId=" + videoId);
+            }
+        }
+    }
+
+    private Map<Long, MessageAttachment> createMessageAttachmentMap(Set<Long> videoIds, Map<Long, VideoDetail> coverMap) {
+        Map<Long, MessageAttachment> messageAttachmentMap = new HashMap<>();
         for (Long videoId : videoIds) {
-            MessageAttachment messageAttachment = new MessageAttachment();
             VideoDetail videoDetail = coverMap.get(videoId);
             if (videoDetail == null || StringUtils.isEmpty(videoDetail.getCover()) || StringUtils.isEmpty(videoDetail.getTitle())) {
                 LarkRobotUtil.sendMessage("获取视频详情异常,请查看" + videoId);
                 throw new RuntimeException("获取视频详情异常");
             }
+            MessageAttachment messageAttachment = new MessageAttachment();
             messageAttachment.setMiniprogramVideoId(videoId);
             messageAttachment.setType(MessageAttachmentTypeEnum.MINI_PROGRAM.getType());
             messageAttachment.setCover(videoDetail.getCover());
@@ -129,37 +260,37 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             messageAttachment.setAppid(appid);
             messageAttachmentMap.put(videoId, messageAttachment);
         }
-        List<MessageAttachment> messageAttachmentList = new ArrayList<>();
-        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
-            if (CollectionUtils.isEmpty(videoParam.getStaffIds())) {
-                videoParam.setStaffIds(new ArrayList<>());
-                if (videoParam.getStaffId() != null) {
-                    videoParam.getStaffIds().add(videoParam.getStaffId());
-                }
+        return messageAttachmentMap;
+    }
+
+    private void addMessageAttachments(VideoParam videoParam, Map<Long, MessageAttachment> messageAttachmentMap, String date, List<MessageAttachment> messageAttachmentList) {
+        List<Long> staffIds = Optional.ofNullable(videoParam.getStaffIds()).orElseGet(() -> {
+            List<Long> newStaffIds = new ArrayList<>();
+            if (videoParam.getStaffId() != null) {
+                newStaffIds.add(videoParam.getStaffId());
             }
-            if (CollectionUtils.isEmpty(videoParam.getStaffIds())) {
+            return newStaffIds;
+        });
+
+        for (Long staffId : staffIds) {
+            if (staffId == 0) {
+                addGuaranteesVideo(date, staffId, videoParam.getVideoIds());
                 continue;
             }
-            for (Long staffId : videoParam.getStaffIds()) {
-                if (staffId == 0) {
-                    addGuaranteesVideo(guaranteedParam.getDate(), staffId, videoParam.getVideoIds());
+
+            for (Long videoId : videoParam.getVideoIds()) {
+                MessageAttachment messageAttachment = messageAttachmentMap.get(videoId);
+                if (messageAttachment == null) {
                     continue;
                 }
-                for (Long videoId : videoParam.getVideoIds()) {
-                    MessageAttachment messageAttachment = messageAttachmentMap.get(videoId);
-                    if (messageAttachment == null) {
-                        continue;
-                    }
-                    MessageAttachment newMessageAttachment = new MessageAttachment();
-                    BeanUtils.copyProperties(messageAttachment, newMessageAttachment);
-                    newMessageAttachment.setStaffId(staffId);
-                    messageAttachmentList.add(newMessageAttachment);
-                    addGuaranteesVideo(guaranteedParam.getDate(), staffId, videoParam.getVideoIds());
-                }
+
+                MessageAttachment newMessageAttachment = new MessageAttachment();
+                BeanUtils.copyProperties(messageAttachment, newMessageAttachment);
+                newMessageAttachment.setStaffId(staffId);
+                messageAttachmentList.add(newMessageAttachment);
+                addGuaranteesVideo(date, staffId, videoParam.getVideoIds());
             }
         }
-        addMiniProgram(messageAttachmentList, null);
-        return CommonResponse.success();
     }
 
     private void addGuaranteesVideo(String date, Long staffId, List<Long> videoIds) {