Przeglądaj źródła

自动获取保底视频

wangyunpeng 3 tygodni temu
rodzic
commit
9de04dcc4a

+ 88 - 0
offline-module/src/main/java/com/tzld/piaoquan/offline/job/WeComMessageDataJob.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.odps.data.Record;
 import com.google.common.collect.Lists;
+import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
 import com.tzld.piaoquan.growth.common.common.constant.MessageConstant;
 import com.tzld.piaoquan.growth.common.dao.mapper.*;
 import com.tzld.piaoquan.growth.common.model.bo.*;
@@ -17,6 +18,7 @@ import com.tzld.piaoquan.growth.common.utils.*;
 import com.tzld.piaoquan.growth.common.utils.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
+import com.xxl.job.core.log.XxlJobLogger;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -596,4 +598,90 @@ public class WeComMessageDataJob {
     }
 
 
+    @XxlJob("addMessageAttachmentJob")
+    public ReturnT<String> addMessageAttachment(String param) {
+        String dt = DateUtil.getBeforeDayDateString(-1, "yyyy-MM-dd");
+        List<Long> videoIds = getTopThousandVideoIds();
+        if (CollectionUtils.isEmpty(videoIds)) {
+            XxlJobLogger.log("保底视频查询异常");
+            LarkRobotUtil.sendMessage("addMessageAttachmentJob 保底视频添加任务异常,请检查" + dt);
+            return ReturnT.FAIL;
+        }
+
+        List<Long> currentVideoIds = new ArrayList<>();
+        int index = 0;
+
+        while (true) {
+            while (currentVideoIds.size() < MAX_VIDEO_NUM && index < videoIds.size()) {
+                currentVideoIds.add(videoIds.get(index++));
+            }
+            if (currentVideoIds.size() < MAX_VIDEO_NUM) {
+                XxlJobLogger.log("保底视频可用数量不足,无法凑齐3个有效视频");
+                LarkRobotUtil.sendMessage("addMessageAttachmentJob 保底视频可用数量不足,无法凑齐3个有效视频,请检查" + dt);
+                return ReturnT.FAIL;
+            }
+            GuaranteedParam guaranteedParam = new GuaranteedParam();
+            guaranteedParam.setDate(dt);
+            guaranteedParam.setVideoParamList(buildVideoParamList(new ArrayList<>(currentVideoIds)));
+            CommonResponse<Void> commonResponse = messageAttachmentService.createGuaranteedMiniProgram(guaranteedParam);
+            if (commonResponse.isSuccess() && commonResponse.getCode() == 0) {
+                return ReturnT.SUCCESS;
+            }
+
+            String msg = commonResponse.getMsg();
+            if (StringUtils.isNotEmpty(msg) && msg.contains("保底视频30天内已发送,请查看videoId=")) {
+                String errorVideoId = msg.substring(msg.indexOf("videoId=") + 8);
+                try {
+                    Long removeId = Long.parseLong(errorVideoId);
+                    currentVideoIds.remove(removeId);
+                } catch (Exception e) {
+                    XxlJobLogger.log("保底视频异常,解析videoId失败: " + errorVideoId);
+                }
+            } else {
+                XxlJobLogger.log("创建保底视频失败: " + msg);
+            }
+            if (currentVideoIds.size() == MAX_VIDEO_NUM) {
+                currentVideoIds.clear();
+            }
+        }
+    }
+
+    private List<Long> getTopThousandVideoIds() {
+        int releaseDays = 1;
+        String dt = DateUtil.getBeforeDayDateString(releaseDays, "yyyyMMdd");
+        int retryTimes = 3;
+        List<Long> videoIds = new ArrayList<>();
+        do {
+            String sql = String.format("SELECT videoid FROM loghubods.lastday_return WHERE dt=%s limit 2000;", dt);
+            List<Record> dataList = OdpsUtil.getOdpsData(sql);
+            if (!CollectionUtils.isEmpty(dataList)) {
+                for (Record record : dataList) {
+                    Long videoId = Long.parseLong((String) record.get(0));
+                    videoIds.add(videoId);
+                }
+                return videoIds;
+            }
+            retryTimes--;
+            releaseDays++;
+            dt = DateUtil.getBeforeDayDateString(releaseDays, "yyyyMMdd");
+        } while (retryTimes > 0);
+        return videoIds;
+    }
+
+    private List<VideoParam> buildVideoParamList(List<Long> videoIds) {
+        List<VideoParam> videoParamList = new ArrayList<>();
+        VideoParam videoParam = new VideoParam();
+        videoParam.setStaffIds(Arrays.asList(0L, 3L, 7L, 9L, 10L, 12L, 16L, 17L, 100L, 103L, 104L, 106L, 107L, 108L, 109L, 110L));
+        videoParam.setVideoIds(videoIds);
+        videoParamList.add(videoParam);
+        VideoParam videoParam2 = new VideoParam();
+        videoParam2.setStaffIds(Arrays.asList(5L, 15L));
+        videoParam2.setVideoIds(videoIds);
+        videoParamList.add(videoParam2);
+        VideoParam videoParam3 = new VideoParam();
+        videoParam3.setStaffIds(Arrays.asList(8L, 11L, 13L, 14L));
+        videoParam3.setVideoIds(videoIds);
+        videoParamList.add(videoParam3);
+        return videoParamList;
+    }
 }