WeComSpecialDataJob.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.tzld.piaoquan.offline.job;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.google.common.collect.Lists;
  6. import com.tzld.piaoquan.growth.common.common.constant.MessageConstant;
  7. import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
  8. import com.tzld.piaoquan.growth.common.dao.mapper.SpecialSendMessageMapper;
  9. import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
  10. import com.tzld.piaoquan.growth.common.dao.mapper.WeComUserMapper;
  11. import com.tzld.piaoquan.growth.common.model.po.*;
  12. import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
  13. import com.tzld.piaoquan.growth.common.service.MessageService;
  14. import com.tzld.piaoquan.growth.common.utils.ToolUtils;
  15. import com.xxl.job.core.biz.model.ReturnT;
  16. import com.xxl.job.core.handler.annotation.XxlJob;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Component;
  20. import org.springframework.util.CollectionUtils;
  21. import java.nio.charset.StandardCharsets;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Objects;
  25. @Component
  26. public class WeComSpecialDataJob {
  27. @Autowired
  28. private WeComUserMapper weComUserMapper;
  29. @Autowired
  30. private StaffMapper staffMapper;
  31. @Autowired
  32. private SpecialSendMessageMapper specialSendMessageMapper;
  33. @Autowired
  34. private MessageService messageService;
  35. @Autowired
  36. private MessageAttachmentService messageAttachmentService;
  37. //发送小程序标题限制字节数
  38. private static final int MAX_BYTES = 64;
  39. public void specialAssembleSendMessage(Long staffId, Integer pageNum, Integer pageSize,
  40. byte groupMsgDisabled, List<Long> attachmentIds, String content) {
  41. List<WeComUser> weComUserList = weComUserMapper.selectUserList(staffId, groupMsgDisabled, (pageNum - 1) * pageSize, pageSize);
  42. //落库逻辑
  43. for (WeComUser weComUser : weComUserList) {
  44. SpecialSendMessage specialSendMessage = new SpecialSendMessage();
  45. specialSendMessage.setStaffId(staffId);
  46. specialSendMessage.setUserId(weComUser.getId());
  47. specialSendMessage.setGroup(pageNum);
  48. specialSendMessage.setAttachmentIds(JSONObject.toJSONString(attachmentIds));
  49. specialSendMessage.setContent(content);
  50. specialSendMessageMapper.insertSelective(specialSendMessage);
  51. }
  52. }
  53. @XxlJob("sendSpecialPushMessageJob")
  54. public ReturnT<String> sendSpecialPushMessage(String param) {
  55. List<SpecialSendMessage> groupList = specialSendMessageMapper.getGroupList();
  56. for (SpecialSendMessage specialSendMessage : groupList) {
  57. List<String> externalUserIds = specialSendMessageMapper.selectExternalUserId(specialSendMessage.getStaffId(), specialSendMessage.getGroup());
  58. boolean flag = specialPushMessage(externalUserIds, specialSendMessage);
  59. if (flag) {
  60. SpecialSendMessage updateSpecialSendMessage = new SpecialSendMessage();
  61. updateSpecialSendMessage.setIsSend(1);
  62. SpecialSendMessageExample example = new SpecialSendMessageExample();
  63. example.createCriteria()
  64. .andStaffIdEqualTo(specialSendMessage.getStaffId())
  65. .andGroupEqualTo(specialSendMessage.getGroup())
  66. .andContentEqualTo(specialSendMessage.getContent())
  67. .andAttachmentIdsEqualTo(specialSendMessage.getAttachmentIds());
  68. specialSendMessageMapper.updateByExampleSelective(updateSpecialSendMessage, example);
  69. }
  70. }
  71. return ReturnT.SUCCESS;
  72. }
  73. private boolean specialPushMessage(List<String> sendUserList, SpecialSendMessage specialSendMessage) {
  74. List<JSONObject> pushList = new ArrayList<>();
  75. StaffExample staffExample = new StaffExample();
  76. staffExample.createCriteria().andIdEqualTo(specialSendMessage.getStaffId());
  77. List<Staff> staffList = staffMapper.selectByExample(staffExample);
  78. Staff staff = staffList.get(0);
  79. JSONObject jsonObject = new JSONObject();
  80. jsonObject.put("chat_type", "single");
  81. JSONObject text = new JSONObject();
  82. if (StringUtils.isNotEmpty(specialSendMessage.getContent())) {
  83. text.put("content", specialSendMessage.getContent());
  84. } else {
  85. text.put("content", messageService.getMessageText());
  86. }
  87. jsonObject.put("text", text);
  88. jsonObject.put("sender", staff.getCarrierId());
  89. String attachmentIds = specialSendMessage.getAttachmentIds();
  90. JSONArray attachments = new JSONArray();
  91. if (StringUtils.isNotEmpty(attachmentIds)) {
  92. List<Long> attachmentIdList = JSONArray.parseArray(attachmentIds, Long.class);
  93. if (!CollectionUtils.isEmpty(attachmentIdList)) {
  94. List<MessageAttachment> messageAttachments = messageAttachmentService.getMessageAttachment(attachmentIdList);
  95. if (!CollectionUtils.isEmpty(messageAttachments)) {
  96. for (MessageAttachment messageAttachment : messageAttachments) {
  97. Integer type = messageAttachment.getType();
  98. if (Objects.equals(MessageAttachmentTypeEnum.MINI_PROGRAM.getCode(), type)) {
  99. JSONObject miniprogramAttachment = new JSONObject();
  100. JSONObject miniprogram = new JSONObject();
  101. miniprogram.put("appid", MessageConstant.appid);
  102. String title = messageAttachment.getTitle();
  103. if (title.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) {
  104. title = ToolUtils.truncateString(title, MAX_BYTES - 3) + "...";
  105. }
  106. miniprogram.put("title", title);
  107. String picMediaId = messageAttachment.getMediaId();
  108. if (StringUtils.isEmpty(picMediaId)) {
  109. return false;
  110. }
  111. miniprogram.put("pic_media_id", picMediaId);
  112. String page = messageAttachment.getPage();
  113. if (StringUtils.isEmpty(page)) {
  114. return false;
  115. }
  116. miniprogram.put("page", page);
  117. miniprogramAttachment.put("msgtype", "miniprogram");
  118. miniprogramAttachment.put("miniprogram", miniprogram);
  119. attachments.add(miniprogramAttachment);
  120. }
  121. if (Objects.equals(MessageAttachmentTypeEnum.LINK.getCode(), type)) {
  122. JSONObject linkAttachment = new JSONObject();
  123. JSONObject link = new JSONObject();
  124. link.put("title", messageAttachment.getTitle());
  125. link.put("picurl", messageAttachment.getPicUrl());
  126. link.put("desc", messageAttachment.getDesc());
  127. link.put("url", messageAttachment.getUrl());
  128. linkAttachment.put("msgtype", "link");
  129. linkAttachment.put("link", link);
  130. attachments.add(linkAttachment);
  131. }
  132. if (Objects.equals(MessageAttachmentTypeEnum.IMAGE.getCode(), type)) {
  133. JSONObject imageAttachment = new JSONObject();
  134. JSONObject image = new JSONObject();
  135. image.put("media_id", messageAttachment.getMediaId());
  136. imageAttachment.put("msgtype", "image");
  137. imageAttachment.put("image", image);
  138. attachments.add(imageAttachment);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. if (!attachments.isEmpty()) {
  145. jsonObject.put("attachments", attachments);
  146. }
  147. List<List<String>> lists = Lists.partition(sendUserList, 10000);
  148. for (List<String> list : lists) {
  149. JSONArray externalUserIds = JSONArray.parseArray(JSON.toJSONString(list));
  150. JSONObject newJSONObject = new JSONObject();
  151. newJSONObject.putAll(jsonObject);
  152. newJSONObject.put("external_userid", externalUserIds);
  153. pushList.add(newJSONObject);
  154. }
  155. if (CollectionUtils.isEmpty(pushList)) {
  156. return false;
  157. }
  158. for (JSONObject pushJsonObject : pushList) {
  159. boolean flag = messageService.pushWeComMessage(pushJsonObject, 1L);
  160. if (!flag) {
  161. return flag;
  162. }
  163. }
  164. return true;
  165. }
  166. }