WeComMessageDataJob.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. package com.tzld.piaoquan.wecom.job;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.aliyun.odps.data.Record;
  6. import com.google.common.collect.Lists;
  7. import com.tzld.piaoquan.wecom.common.constant.MessageConstant;
  8. import com.tzld.piaoquan.wecom.dao.mapper.*;
  9. import com.tzld.piaoquan.wecom.model.bo.PushMessage;
  10. import com.tzld.piaoquan.wecom.model.bo.VideoCombination;
  11. import com.tzld.piaoquan.wecom.model.bo.VideoParam;
  12. import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
  13. import com.tzld.piaoquan.wecom.model.po.*;
  14. import com.tzld.piaoquan.wecom.model.vo.GuaranteedParam;
  15. import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
  16. import com.tzld.piaoquan.wecom.service.MessageService;
  17. import com.tzld.piaoquan.wecom.utils.DateUtil;
  18. import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
  19. import com.tzld.piaoquan.wecom.utils.OdpsUtil;
  20. import com.tzld.piaoquan.wecom.utils.ToolUtils;
  21. import com.tzld.piaoquan.wecom.utils.page.Page;
  22. import com.xxl.job.core.biz.model.ReturnT;
  23. import com.xxl.job.core.handler.annotation.XxlJob;
  24. import lombok.extern.log4j.Log4j2;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.data.redis.core.RedisTemplate;
  28. import org.springframework.stereotype.Component;
  29. import org.springframework.util.CollectionUtils;
  30. import java.nio.charset.StandardCharsets;
  31. import java.util.*;
  32. import java.util.stream.Collectors;
  33. import static com.tzld.piaoquan.wecom.common.constant.MessageConstant.MAX_VIDEO_NUM;
  34. import static com.tzld.piaoquan.wecom.common.constant.TimeConstant.MILLISECOND_DAY;
  35. @Log4j2
  36. @Component
  37. public class WeComMessageDataJob {
  38. @Autowired
  39. private UserMapper userMapper;
  40. @Autowired
  41. private MessageAttachmentMapper messageAttachmentMapper;
  42. @Autowired
  43. private RedisTemplate<String, Object> redisTemplate;
  44. @Autowired
  45. private MessageService messageService;
  46. @Autowired
  47. private MessageAttachmentService messageAttachmentService;
  48. @Autowired
  49. private StaffWithUserMapper staffWithUserMapper;
  50. @Autowired
  51. private StaffMapper staffMapper;
  52. @Autowired
  53. private SendMessageMapper sendMessageMapper;
  54. @Autowired
  55. private CorpMapper corpMapper;
  56. //发送小程序标题限制字节数
  57. private static final int MAX_BYTES = 64;
  58. //历史优质视频可推送用户列表
  59. Map<Long, List<PushMessage>> historicalTopMap = new HashMap<>();
  60. //保底视频列表
  61. Map<Long, VideoCombination> guaranteedVideoMap = new HashMap<>();
  62. Map<String, String> pageMap = new HashMap<>();
  63. //初始化操作
  64. private void init(List<Long> staffIds) {
  65. //历史优质视频获取
  66. String sql = String.format("SELECT * FROM loghubods.history_good_video_can_push_user_list where dt = %s;",
  67. DateUtil.getBeforeDayDateString());
  68. List<Record> recordList = OdpsUtil.getOdpsData(sql);
  69. if (CollectionUtils.isEmpty(recordList)) {
  70. LarkRobotUtil.sendMessage("历史优质视频为空");
  71. } else {
  72. List<PushMessage> list = new ArrayList<>();
  73. for (Record record : recordList) {
  74. PushMessage pushMessage = new PushMessage();
  75. Long videoId = Long.parseLong((String) record.get(0));
  76. Set<Long> userIds = new HashSet<>(JSONObject.parseArray((String) record.get(1), Long.class));
  77. Long staffId = Long.parseLong((String) record.get(2));
  78. Double score = Double.parseDouble((String) record.get(3));
  79. pushMessage.setVideoId(videoId);
  80. pushMessage.setUserIds(userIds);
  81. pushMessage.setStaffId(staffId);
  82. pushMessage.setScore(score);
  83. list.add(pushMessage);
  84. }
  85. historicalTopMap = list.stream()
  86. .collect(Collectors.groupingBy(PushMessage::getStaffId,
  87. Collectors.mapping(pushMessage -> pushMessage,
  88. Collectors.toList())))
  89. .entrySet()
  90. .stream()
  91. .collect(Collectors.toMap(
  92. Map.Entry::getKey,
  93. entry -> entry.getValue().stream()
  94. .sorted(Comparator.comparing(PushMessage::getScore).reversed()) // 根据 score 降序排序
  95. .collect(Collectors.toList())
  96. ));
  97. }
  98. //保底视频获取
  99. GuaranteedParam guaranteedParam = messageAttachmentService.getGuaranteedVideo(DateUtil.getThatDayDateString());
  100. if (guaranteedParam == null
  101. || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
  102. LarkRobotUtil.sendMessage("保底视频获取异常,请检查" + DateUtil.getThatDayDateString());
  103. throw new RuntimeException();
  104. }
  105. Map<Long, VideoCombination> videoMap = new HashMap<>();
  106. for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
  107. if (videoParam.getStaffId() == null) {
  108. LarkRobotUtil.sendMessage("保底视频获取异常,StaffId为空" + DateUtil.getThatDayDateString());
  109. continue;
  110. }
  111. if (videoParam.getStaffId() != 0) {
  112. if (!CollectionUtils.isEmpty(staffIds) && !staffIds.contains(videoParam.getStaffId())) {
  113. continue;
  114. }
  115. }
  116. //默认组视频不做查询
  117. if (videoParam.getStaffId() == 0L) {
  118. if (videoParam.getVideoIds().size() < MAX_VIDEO_NUM) {
  119. LarkRobotUtil.sendMessage("默认组视频数量不足" + DateUtil.getThatDayDateString());
  120. }
  121. videoMap.put(videoParam.getStaffId(), combination(videoParam.getVideoIds()));
  122. continue;
  123. }
  124. if (CollectionUtils.isEmpty(videoParam.getVideoIds()) || videoParam.getVideoIds().size() < MAX_VIDEO_NUM) {
  125. LarkRobotUtil.sendMessage("保底视频数量异常,请查看" + guaranteedParam.getDate() + videoParam.getStaffId());
  126. throw new RuntimeException();
  127. }
  128. for (Long videoId : videoParam.getVideoIds()) {
  129. MessageAttachmentExample example = new MessageAttachmentExample();
  130. example.createCriteria().andMiniprogramVideoIdEqualTo(videoId).andStaffIdEqualTo(videoParam.getStaffId());
  131. List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
  132. if (CollectionUtils.isEmpty(messageAttachmentList)) {
  133. LarkRobotUtil.sendMessage("保底视频不存在,请查看videoId=" + videoId);
  134. continue;
  135. }
  136. MessageAttachment messageAttachment = messageAttachmentList.get(0);
  137. if (messageAttachment.getSendTime() != null
  138. && DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 90 * MILLISECOND_DAY) {
  139. LarkRobotUtil.sendMessage("保底视频90天内已发送,请查看videoId=" + videoId);
  140. }
  141. }
  142. //重新组合视频id
  143. videoMap.put(videoParam.getStaffId(), combination(videoParam.getVideoIds()));
  144. }
  145. if (!videoMap.containsKey(0L)) {
  146. LarkRobotUtil.sendMessage("保底视频没有默认组,请查看" + guaranteedParam.getDate());
  147. throw new RuntimeException();
  148. }
  149. log.info("保底数据获取,videoMap={}", videoMap);
  150. this.guaranteedVideoMap = videoMap;
  151. }
  152. private VideoCombination combination(List<Long> videoIds) {
  153. VideoCombination videoCombination = new VideoCombination();
  154. videoCombination.setThreeVideoList(combinedVideoList(videoIds, 3));
  155. videoCombination.setThreeVideoIndex(0);
  156. videoCombination.setTwoVideoList(combinedVideoList(videoIds, 2));
  157. videoCombination.setTwoVideoIndex(0);
  158. videoCombination.setOneVideoList(combinedVideoList(videoIds, 1));
  159. videoCombination.setOneVideoIndex(0);
  160. return videoCombination;
  161. }
  162. private List<List<Long>> combinedVideoList(List<Long> videoIds, int count) {
  163. List<List<Long>> res = new ArrayList<>();
  164. // 此处假设视频的打开率与上下文无关,只保证每个视频在每个位置都有发送
  165. for (int i = 0; i < videoIds.size(); i++) {
  166. List<Long> list = new ArrayList<>();
  167. for (int j = 0; j < count; j++) {
  168. int index = (i + j) % videoIds.size();
  169. list.add(videoIds.get(index));
  170. }
  171. res.add(list);
  172. }
  173. return res;
  174. }
  175. @XxlJob("assembleSendMessageJob")
  176. public ReturnT<String> assembleSendMessage(String param) {
  177. XxlJobParam xxlJobParam = new XxlJobParam();
  178. if (StringUtils.isNotEmpty(param)) {
  179. xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
  180. }
  181. CorpExample corpExample = new CorpExample();
  182. if (xxlJobParam.getCorpId() != null) {
  183. corpExample.createCriteria().andIdEqualTo(xxlJobParam.getCorpId());
  184. }
  185. List<Corp> corps = corpMapper.selectByExample(corpExample);
  186. if (CollectionUtils.isEmpty(corps)) {
  187. return ReturnT.SUCCESS;
  188. }
  189. List<Long> staffIds = new ArrayList<>();
  190. if (xxlJobParam.getStaffId() != null) {
  191. staffIds.add(xxlJobParam.getStaffId());
  192. }
  193. if (!CollectionUtils.isEmpty(xxlJobParam.getStaffIds())) {
  194. staffIds.addAll(xxlJobParam.getStaffIds());
  195. }
  196. init(staffIds);
  197. for (Corp corp : corps) {
  198. UserExample userExample = new UserExample();
  199. userExample.createCriteria().andExternalUserIdIsNotNull().andCorpIdEqualTo(corp.getId());
  200. if (xxlJobParam.getUserId() != null) {
  201. userExample.createCriteria().andIdEqualTo(xxlJobParam.getUserId());
  202. }
  203. long count = userMapper.countByExample(userExample);
  204. int page = 1;
  205. int pageSize = 1000;
  206. long totalPageSize = count / pageSize + 1;
  207. for (; page <= totalPageSize; page++) {
  208. userExample.setPage(new Page<>(page, pageSize));
  209. List<User> userList = userMapper.selectByExample(userExample);
  210. if (CollectionUtils.isEmpty(userList)) {
  211. continue;
  212. }
  213. //落库逻辑
  214. List<SendMessage> allSeneMessageList = new ArrayList<>();
  215. for (User user : userList) {
  216. List<SendMessage> sendMessageList = getSendMessage(user, staffIds, corp.getId());
  217. if (CollectionUtils.isEmpty(sendMessageList)) {
  218. continue;
  219. }
  220. allSeneMessageList.addAll(sendMessageList);
  221. }
  222. if (CollectionUtils.isEmpty(allSeneMessageList)) {
  223. continue;
  224. }
  225. sendMessageMapper.insertList(allSeneMessageList);
  226. }
  227. }
  228. //组装好当天要发送的消息后 记录时间
  229. saveGuaranteedVideoIdList(staffIds);
  230. return ReturnT.SUCCESS;
  231. }
  232. private void saveGuaranteedVideoIdList(List<Long> staffIds) {
  233. GuaranteedParam guaranteedParam = messageAttachmentService.getGuaranteedVideo(DateUtil.getThatDayDateString());
  234. if (guaranteedParam == null || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
  235. return;
  236. }
  237. if (CollectionUtils.isEmpty(staffIds)) {
  238. Set<Long> videoIdSet = new HashSet<>();
  239. for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
  240. if (CollectionUtils.isEmpty(videoParam.getVideoIds())) {
  241. continue;
  242. }
  243. videoIdSet.addAll(videoParam.getVideoIds());
  244. }
  245. List<Long> videoIdList = new ArrayList<>(videoIdSet);
  246. MessageAttachmentExample example = new MessageAttachmentExample();
  247. example.createCriteria().andMiniprogramVideoIdIn(videoIdList).andSendTimeIsNull();
  248. MessageAttachment updateMessageAttachment = new MessageAttachment();
  249. updateMessageAttachment.setSendTime(new Date());
  250. messageAttachmentMapper.updateByExampleSelective(updateMessageAttachment, example);
  251. } else {
  252. for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
  253. if (!CollectionUtils.isEmpty(staffIds) && staffIds.contains(videoParam.getStaffId())) {
  254. if (CollectionUtils.isEmpty(videoParam.getVideoIds())) {
  255. continue;
  256. }
  257. MessageAttachmentExample example = new MessageAttachmentExample();
  258. example.createCriteria().andMiniprogramVideoIdIn(videoParam.getVideoIds()).andStaffIdEqualTo(videoParam.getStaffId());
  259. MessageAttachment updateMessageAttachment = new MessageAttachment();
  260. updateMessageAttachment.setSendTime(new Date());
  261. messageAttachmentMapper.updateByExampleSelective(updateMessageAttachment, example);
  262. }
  263. }
  264. }
  265. }
  266. private List<SendMessage> getSendMessage(User user, List<Long> staffIds, Long corpId) {
  267. StaffWithUserExample example = new StaffWithUserExample();
  268. StaffWithUserExample.Criteria criteria = example.createCriteria();
  269. criteria.andUserIdEqualTo(user.getId());
  270. criteria.andIsDeleteEqualTo(0);
  271. if (!CollectionUtils.isEmpty(staffIds)) {
  272. criteria.andStaffIdIn(staffIds);
  273. }
  274. List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(example);
  275. if (CollectionUtils.isEmpty(staffWithUserList)) {
  276. return Collections.emptyList();
  277. }
  278. List<SendMessage> sendMessageList = new ArrayList<>();
  279. for (StaffWithUser staffWithUser : staffWithUserList) {
  280. SendMessage sendMessage = new SendMessage();
  281. int n = fillHistoricalTopMessages(sendMessage, user.getId(), staffWithUser.getStaffId());
  282. if (n < MAX_VIDEO_NUM) {
  283. // 保底数据
  284. n = fillGuaranteedMessages(sendMessage, staffWithUser.getStaffId(), n);
  285. }
  286. if (n < MAX_VIDEO_NUM) {
  287. LarkRobotUtil.sendMessage("组装数据失败 user=" + user);
  288. throw new RuntimeException("组装数据失败");
  289. }
  290. sendMessage.setCorpId(corpId);
  291. sendMessage.setStaffId(staffWithUser.getStaffId());
  292. sendMessage.setUserId(staffWithUser.getUserId());
  293. sendMessageList.add(sendMessage);
  294. }
  295. return sendMessageList;
  296. }
  297. private int fillHistoricalTopMessages(SendMessage sendMessage, Long userId, Long staffId) {
  298. List<PushMessage> pushMessages = historicalTopMap.get(staffId);
  299. if (CollectionUtils.isEmpty(pushMessages)) {
  300. return 0;
  301. }
  302. int n = 0;
  303. for (PushMessage pushMessage : pushMessages) {
  304. if (pushMessage.getUserIds().contains(userId)) {
  305. setVideoId(sendMessage, n, pushMessage.getVideoId());
  306. n++;
  307. if (n >= MAX_VIDEO_NUM) {
  308. break;
  309. }
  310. }
  311. }
  312. return n;
  313. }
  314. private int fillGuaranteedMessages(SendMessage sendMessage, Long staffId, int currentCount) {
  315. VideoCombination videoCombination = guaranteedVideoMap.get(staffId);
  316. if (videoCombination == null) {
  317. videoCombination = guaranteedVideoMap.get(0L);
  318. }
  319. if (videoCombination == null) {
  320. log.error("保底数据获取失败,staffId={}, guaranteedVideoMap={}", staffId, guaranteedVideoMap);
  321. LarkRobotUtil.sendMessage("组装数据时,保底数据获取异常");
  322. throw new RuntimeException("保底数据获取异常");
  323. }
  324. if (currentCount < MAX_VIDEO_NUM) {
  325. switch (currentCount) {
  326. case 0:
  327. currentCount = setVideoId(sendMessage, videoCombination.getThreeVideoList().get(videoCombination.getThreeVideoIndex()),
  328. currentCount);
  329. videoCombination.setThreeVideoIndex(
  330. (videoCombination.getThreeVideoIndex() + 1) % videoCombination.getThreeVideoList().size());
  331. break;
  332. case 1:
  333. currentCount = setVideoId(sendMessage, videoCombination.getTwoVideoList().get(videoCombination.getTwoVideoIndex()),
  334. currentCount);
  335. videoCombination.setTwoVideoIndex(
  336. (videoCombination.getTwoVideoIndex() + 1) % videoCombination.getTwoVideoList().size());
  337. break;
  338. case 2:
  339. currentCount = setVideoId(sendMessage, videoCombination.getOneVideoList().get(videoCombination.getOneVideoIndex()),
  340. currentCount);
  341. videoCombination.setOneVideoIndex(
  342. (videoCombination.getOneVideoIndex() + 1) % videoCombination.getOneVideoList().size());
  343. break;
  344. default:
  345. break;
  346. }
  347. }
  348. return currentCount;
  349. }
  350. private int setVideoId(SendMessage sendMessage, List<Long> videoIds, Integer currentCount) {
  351. for (Long videoId : videoIds) {
  352. setVideoId(sendMessage, currentCount, videoId);
  353. currentCount++;
  354. if (currentCount >= MAX_VIDEO_NUM) {
  355. break;
  356. }
  357. }
  358. return currentCount;
  359. }
  360. private void setVideoId(SendMessage sendMessage, int index, Long videoId) {
  361. switch (index) {
  362. case 0:
  363. sendMessage.setVideoId1(videoId);
  364. break;
  365. case 1:
  366. sendMessage.setVideoId2(videoId);
  367. break;
  368. case 2:
  369. sendMessage.setVideoId3(videoId);
  370. break;
  371. default:
  372. break;
  373. }
  374. }
  375. @XxlJob("pushSendMessageJob")
  376. public ReturnT<String> pushSendMessage(String param) {
  377. XxlJobParam xxlJobParam = new XxlJobParam();
  378. if (StringUtils.isNotEmpty(param)) {
  379. xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
  380. }
  381. List<SendMessage> groupList = sendMessageMapper.getGroupList(DateUtil.getThatDayDate(), 0);
  382. if (xxlJobParam.getCorpId() != null) {
  383. Long corpId = xxlJobParam.getCorpId();
  384. groupList = groupList.stream().filter(e -> Objects.equals(e.getCorpId(), corpId)).collect(Collectors.toList());
  385. }
  386. if (CollectionUtils.isEmpty(groupList)) {
  387. return ReturnT.SUCCESS;
  388. }
  389. if (xxlJobParam.getStaffId() != null) {
  390. Long staffId = xxlJobParam.getStaffId();
  391. groupList = groupList.stream().filter(e -> Objects.equals(e.getStaffId(), staffId)).collect(Collectors.toList());
  392. }
  393. if (CollectionUtils.isEmpty(groupList)) {
  394. return ReturnT.SUCCESS;
  395. }
  396. for (SendMessage sendMessage : groupList) {
  397. pushAndUpdateMessage(sendMessage);
  398. }
  399. Map<Long, List<SendMessage>> groupedByStaffId = groupList.stream().collect(Collectors.groupingBy(SendMessage::getStaffId));
  400. for (Map.Entry<Long, List<SendMessage>> entry : groupedByStaffId.entrySet()) {
  401. SendMessageExample example = new SendMessageExample();
  402. example.createCriteria().andStaffIdEqualTo(entry.getKey()).andIsSendEqualTo(0)
  403. .andCreateTimeGreaterThan(DateUtil.getThatDayDate());
  404. long l = sendMessageMapper.countByExample(example);
  405. //增加重试
  406. if (l > 0) {
  407. List<SendMessage> retryGroupList = sendMessageMapper.getGroupList(DateUtil.getThatDayDate(), 0);
  408. retryGroupList = retryGroupList.stream().filter(e -> Objects.equals(e.getStaffId(), entry.getKey()))
  409. .collect(Collectors.toList());
  410. for (SendMessage sendMessage : retryGroupList) {
  411. pushAndUpdateMessage(sendMessage);
  412. }
  413. }
  414. }
  415. SendMessageExample example = new SendMessageExample();
  416. example.createCriteria().andIsSendEqualTo(0).andCreateTimeGreaterThan(DateUtil.getThatDayDate());
  417. long l = sendMessageMapper.countByExample(example);
  418. if (l > 0) {
  419. LarkRobotUtil.sendMessage("存在发送失败消息,请检查@薛一鸣");
  420. }
  421. return ReturnT.SUCCESS;
  422. }
  423. private void pushAndUpdateMessage(SendMessage sendMessage) {
  424. sendMessage.setIsSend(0);
  425. sendMessage.setCreateTime(DateUtil.getThatDayDate());
  426. List<String> sendUserList = sendMessageMapper.selectExternalUserId(sendMessage);
  427. boolean flag = pushMessage(sendUserList, sendMessage);
  428. if (flag) {
  429. SendMessage updateSendMessage = new SendMessage();
  430. updateSendMessage.setIsSend(1);
  431. SendMessageExample example = new SendMessageExample();
  432. example.createCriteria()
  433. .andVideoId1EqualTo(sendMessage.getVideoId1())
  434. .andVideoId2EqualTo(sendMessage.getVideoId2())
  435. .andVideoId3EqualTo(sendMessage.getVideoId3())
  436. .andStaffIdEqualTo(sendMessage.getStaffId())
  437. .andCreateTimeGreaterThan(DateUtil.getThatDayDate());
  438. sendMessageMapper.updateByExampleSelective(updateSendMessage, example);
  439. }
  440. }
  441. private boolean pushMessage(List<String> sendUserList, SendMessage sendMessage) {
  442. List<JSONObject> pushList = new ArrayList<>();
  443. StaffExample staffExample = new StaffExample();
  444. staffExample.createCriteria().andIdEqualTo(sendMessage.getStaffId());
  445. List<Staff> staffList = staffMapper.selectByExample(staffExample);
  446. Staff staff = staffList.get(0);
  447. JSONObject jsonObject = new JSONObject();
  448. jsonObject.put("chat_type", "single");
  449. JSONObject text = new JSONObject();
  450. String content = messageService.getMessageText();
  451. text.put("content", content);
  452. jsonObject.put("text", text);
  453. jsonObject.put("sender", staff.getCarrierId());
  454. JSONArray attachments = new JSONArray();
  455. List<Long> videoIdList = new ArrayList<>();
  456. videoIdList.add(sendMessage.getVideoId1());
  457. videoIdList.add(sendMessage.getVideoId2());
  458. videoIdList.add(sendMessage.getVideoId3());
  459. for (Long videoId : videoIdList) {
  460. JSONObject attachment = new JSONObject();
  461. attachment.put("msgtype", "miniprogram");
  462. MessageAttachmentExample example = new MessageAttachmentExample();
  463. example.createCriteria().andMiniprogramVideoIdEqualTo(videoId);
  464. List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
  465. if (CollectionUtils.isEmpty(messageAttachmentList)) {
  466. log.error("附件信息查询异常");
  467. return false;
  468. }
  469. MessageAttachment messageAttachment = messageAttachmentList.get(0);
  470. JSONObject miniprogram = new JSONObject();
  471. miniprogram.put("appid", MessageConstant.appid);
  472. String title = messageAttachment.getTitle();
  473. if (title.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) {
  474. title = ToolUtils.truncateString(title, MAX_BYTES - 3) + "...";
  475. }
  476. miniprogram.put("title", title);
  477. String picMediaId = messageAttachmentService.getPicMediaId(messageAttachment.getCover(), sendMessage.getCorpId());
  478. if (StringUtils.isEmpty(picMediaId)) {
  479. log.error("pushMessage getPicMediaId error cover={}", messageAttachment.getCover());
  480. return false;
  481. }
  482. miniprogram.put("pic_media_id", picMediaId);
  483. String page = "";
  484. String key = staff.getCarrierId() + "_" + videoId;
  485. if (pageMap.containsKey(key)) {
  486. page = pageMap.get(key);
  487. } else {
  488. page = messageAttachmentService.getPage(staff, videoId);
  489. pageMap.put(key, page);
  490. }
  491. if (StringUtils.isEmpty(page)) {
  492. log.error("pushMessage get page error videoId={} staff={}", videoId, staff);
  493. return false;
  494. }
  495. miniprogram.put("page", page);
  496. attachment.put("miniprogram", miniprogram);
  497. attachments.add(0, attachment);
  498. }
  499. jsonObject.put("attachments", attachments);
  500. List<List<String>> lists = Lists.partition(sendUserList, 10000);
  501. for (List<String> list : lists) {
  502. JSONArray externalUserIds = JSONArray.parseArray(JSON.toJSONString(list));
  503. JSONObject newJSONObject = new JSONObject();
  504. newJSONObject.putAll(jsonObject);
  505. newJSONObject.put("external_userid", externalUserIds);
  506. pushList.add(newJSONObject);
  507. }
  508. if (CollectionUtils.isEmpty(pushList)) {
  509. return false;
  510. }
  511. for (JSONObject pushJsonObject : pushList) {
  512. log.info("pushMessage pushJsonObject={}", pushJsonObject);
  513. boolean flag = messageService.pushWeComMessage(pushJsonObject, sendMessage.getCorpId());
  514. if (!flag) {
  515. return flag;
  516. }
  517. }
  518. return true;
  519. }
  520. @XxlJob("existGuaranteesJob")
  521. public ReturnT<String> existGuarantees(String param) {
  522. //保底视频获取
  523. GuaranteedParam guaranteedParam = messageAttachmentService.getGuaranteedVideo(DateUtil.getNextDayDateString());
  524. if (guaranteedParam == null
  525. || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
  526. LarkRobotUtil.sendMessage("@薛一鸣 保底视频异常,请检查" + DateUtil.getNextDayDateString());
  527. }
  528. return ReturnT.SUCCESS;
  529. }
  530. }