WeComMessageDataJob.java 20 KB

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