WeComSpecialDataJob.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.common.enums.PreSpecialStatusEnum;
  9. import com.tzld.piaoquan.growth.common.dao.mapper.PreSpecialSendMessageMapper;
  10. import com.tzld.piaoquan.growth.common.dao.mapper.SpecialSendMessageMapper;
  11. import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
  12. import com.tzld.piaoquan.growth.common.dao.mapper.WeComUserMapper;
  13. import com.tzld.piaoquan.growth.common.model.po.*;
  14. import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
  15. import com.tzld.piaoquan.growth.common.service.MessageService;
  16. import com.tzld.piaoquan.growth.common.service.WeComUserService;
  17. import com.tzld.piaoquan.growth.common.utils.DateUtil;
  18. import com.tzld.piaoquan.growth.common.utils.ToolUtils;
  19. import com.xxl.job.core.biz.model.ReturnT;
  20. import com.xxl.job.core.handler.annotation.XxlJob;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Component;
  24. import org.springframework.util.CollectionUtils;
  25. import java.nio.charset.StandardCharsets;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.Objects;
  29. import java.util.stream.Collectors;
  30. @Component
  31. public class WeComSpecialDataJob {
  32. @Autowired
  33. private WeComUserMapper weComUserMapper;
  34. @Autowired
  35. private StaffMapper staffMapper;
  36. @Autowired
  37. private SpecialSendMessageMapper specialSendMessageMapper;
  38. @Autowired
  39. private MessageService messageService;
  40. @Autowired
  41. private MessageAttachmentService messageAttachmentService;
  42. @Autowired
  43. private WeComUserService weComUserService;
  44. @Autowired
  45. private PreSpecialSendMessageMapper preSpecialSendMessageMapper;
  46. //发送小程序标题限制字节数
  47. private static final int MAX_BYTES = 64;
  48. @XxlJob("sendSpecialPushMessageJob")
  49. public ReturnT<String> getAndAddSpecialAssembleSendMessage(String param) {
  50. String thatDayDateString = DateUtil.getThatDayDateString();
  51. PreSpecialSendMessageExample example = new PreSpecialSendMessageExample();
  52. example.createCriteria().andPreSendDateEqualTo(thatDayDateString).andIsSendEqualTo(0);
  53. List<PreSpecialSendMessage> preSpecialSendMessages = preSpecialSendMessageMapper.selectByExample(example);
  54. List<PreSpecialSendMessage> defaultPreSpecialSendMessage = preSpecialSendMessages.stream()
  55. .filter(e -> Objects.equals(e.getStatus(), PreSpecialStatusEnum.DEFAULT.getStatus())).collect(Collectors.toList());
  56. if (!CollectionUtils.isEmpty(defaultPreSpecialSendMessage)) {
  57. for (PreSpecialSendMessage preSpecialSendMessage : defaultPreSpecialSendMessage) {
  58. defaultSpecialAssembleSendMessage(preSpecialSendMessage.getId(), preSpecialSendMessage.getStaffId(),
  59. preSpecialSendMessage.getPageNum(), preSpecialSendMessage.getPageSize(),
  60. preSpecialSendMessage.getGroupMsgDisabled().byteValue(), preSpecialSendMessage.getAttachmentIds(),
  61. preSpecialSendMessage.getContent(), preSpecialSendMessage.getPreSendDate());
  62. }
  63. }
  64. List<PreSpecialSendMessage> tagPreSpecialSendMessage = preSpecialSendMessages.stream()
  65. .filter(e -> Objects.equals(e.getStatus(), PreSpecialStatusEnum.TAG.getStatus())).collect(Collectors.toList());
  66. if (!CollectionUtils.isEmpty(tagPreSpecialSendMessage)) {
  67. for (PreSpecialSendMessage preSpecialSendMessage : tagPreSpecialSendMessage) {
  68. tagSpecialAssembleSendMessage(preSpecialSendMessage.getId(), preSpecialSendMessage.getStaffId(), preSpecialSendMessage.getAttachmentIds(),
  69. preSpecialSendMessage.getContent(), preSpecialSendMessage.getPreSendDate(), preSpecialSendMessage.getTagId(), preSpecialSendMessage.getGender());
  70. }
  71. }
  72. List<PreSpecialSendMessage> genderPreSpecialSendMessage = preSpecialSendMessages.stream()
  73. .filter(e -> Objects.equals(e.getStatus(), PreSpecialStatusEnum.GENDER.getStatus())).collect(Collectors.toList());
  74. if (!CollectionUtils.isEmpty(genderPreSpecialSendMessage)) {
  75. for (PreSpecialSendMessage preSpecialSendMessage : genderPreSpecialSendMessage) {
  76. genderSpecialAssembleSendMessage(preSpecialSendMessage.getId(), preSpecialSendMessage.getStaffId(),
  77. preSpecialSendMessage.getGroupMsgDisabled().byteValue(), preSpecialSendMessage.getAttachmentIds(),
  78. preSpecialSendMessage.getContent(), preSpecialSendMessage.getPreSendDate(), preSpecialSendMessage.getGender());
  79. }
  80. }
  81. //发送拼装好的消息
  82. sendSpecialPushMessage();
  83. for (PreSpecialSendMessage preSpecialSendMessage : preSpecialSendMessages) {
  84. PreSpecialSendMessage updatePreSpecialSendMessage = new PreSpecialSendMessage();
  85. updatePreSpecialSendMessage.setId(preSpecialSendMessage.getId());
  86. updatePreSpecialSendMessage.setIsSend(1);
  87. preSpecialSendMessageMapper.updateByPrimaryKeySelective(updatePreSpecialSendMessage);
  88. }
  89. return ReturnT.SUCCESS;
  90. }
  91. public void defaultSpecialAssembleSendMessage(Long preId, Long staffId, Integer pageNum, Integer pageSize,
  92. byte groupMsgDisabled, String attachmentIds, String content, String date) {
  93. List<Long> filterTagId = weComUserService.getFilterTagId();
  94. List<WeComUser> weComUserList = weComUserMapper.selectUserList(staffId, groupMsgDisabled, filterTagId, (pageNum - 1) * pageSize, pageSize);
  95. //落库逻辑
  96. for (WeComUser weComUser : weComUserList) {
  97. SpecialSendMessage specialSendMessage = new SpecialSendMessage();
  98. specialSendMessage.setStaffId(staffId);
  99. specialSendMessage.setUserId(weComUser.getId());
  100. specialSendMessage.setGroup(preId.intValue());
  101. specialSendMessage.setAttachmentIds(attachmentIds);
  102. if (content.contains("${name}")) {
  103. specialSendMessage.setContent(content.replace("${name}", weComUser.getName()));
  104. } else {
  105. specialSendMessage.setContent(content);
  106. }
  107. specialSendMessage.setPreSendDate(date);
  108. specialSendMessageMapper.insertSelective(specialSendMessage);
  109. }
  110. }
  111. public void tagSpecialAssembleSendMessage(Long preId, Long staffId, String attachmentIds, String content, String date, Long tagId, Integer gender) {
  112. List<WeComUser> weComUsers = weComUserMapper.selectByTagUserList(tagId);
  113. if (CollectionUtils.isEmpty(weComUsers)) {
  114. return;
  115. }
  116. for (WeComUser weComUser : weComUsers) {
  117. if (gender != null && !gender.equals(weComUser.getGender())) {
  118. continue;
  119. }
  120. List<Staff> staffs = weComUserService.getStaffByUserId(weComUser.getId());
  121. if (CollectionUtils.isEmpty(staffs)) {
  122. continue;
  123. }
  124. for (Staff staff : staffs) {
  125. if (staffId != null && !Objects.equals(staff.getId(), staffId)) {
  126. continue;
  127. }
  128. SpecialSendMessage specialSendMessage = new SpecialSendMessage();
  129. specialSendMessage.setStaffId(staff.getId());
  130. specialSendMessage.setUserId(weComUser.getId());
  131. specialSendMessage.setGroup(preId.intValue());
  132. specialSendMessage.setAttachmentIds(attachmentIds);
  133. if (content.contains("${name}")) {
  134. content = content.replace("${name}", weComUser.getName());
  135. }
  136. specialSendMessage.setContent(content);
  137. specialSendMessage.setPreSendDate(date);
  138. specialSendMessageMapper.insertSelective(specialSendMessage);
  139. }
  140. }
  141. }
  142. public void genderSpecialAssembleSendMessage(Long preId, Long staffId, byte groupMsgDisabled, String attachmentIds,
  143. String content, String date, Integer gender) {
  144. List<Long> filterTagId = weComUserService.getFilterTagId();
  145. List<WeComUser> weComUserList = weComUserMapper.selectByGenderUserList(staffId, groupMsgDisabled, filterTagId, gender);
  146. //落库逻辑
  147. for (WeComUser weComUser : weComUserList) {
  148. SpecialSendMessage specialSendMessage = new SpecialSendMessage();
  149. specialSendMessage.setStaffId(staffId);
  150. specialSendMessage.setUserId(weComUser.getId());
  151. specialSendMessage.setGroup(preId.intValue());
  152. specialSendMessage.setAttachmentIds(attachmentIds);
  153. if (content.contains("${name}")) {
  154. content = content.replace("${name}", weComUser.getName());
  155. }
  156. specialSendMessage.setContent(content);
  157. specialSendMessage.setPreSendDate(date);
  158. specialSendMessageMapper.insertSelective(specialSendMessage);
  159. }
  160. }
  161. public void sendSpecialPushMessage() {
  162. String thatDayDateString = DateUtil.getThatDayDateString();
  163. List<SpecialSendMessage> groupList = specialSendMessageMapper.getGroupList(thatDayDateString);
  164. for (SpecialSendMessage specialSendMessage : groupList) {
  165. List<String> externalUserIds = specialSendMessageMapper.selectExternalUserId(specialSendMessage.getStaffId(),
  166. specialSendMessage.getGroup(), thatDayDateString);
  167. boolean flag = specialPushMessage(externalUserIds, specialSendMessage);
  168. if (flag) {
  169. SpecialSendMessage updateSpecialSendMessage = new SpecialSendMessage();
  170. updateSpecialSendMessage.setIsSend(1);
  171. SpecialSendMessageExample example = new SpecialSendMessageExample();
  172. SpecialSendMessageExample.Criteria criteria = example.createCriteria()
  173. .andStaffIdEqualTo(specialSendMessage.getStaffId())
  174. .andGroupEqualTo(specialSendMessage.getGroup())
  175. .andContentEqualTo(specialSendMessage.getContent())
  176. .andPreSendDateEqualTo(thatDayDateString);
  177. if (StringUtils.isNotEmpty(specialSendMessage.getAttachmentIds())) {
  178. criteria.andAttachmentIdsEqualTo(specialSendMessage.getAttachmentIds());
  179. }
  180. specialSendMessageMapper.updateByExampleSelective(updateSpecialSendMessage, example);
  181. }
  182. }
  183. }
  184. private boolean specialPushMessage(List<String> sendUserList, SpecialSendMessage specialSendMessage) {
  185. List<JSONObject> pushList = new ArrayList<>();
  186. StaffExample staffExample = new StaffExample();
  187. staffExample.createCriteria().andIdEqualTo(specialSendMessage.getStaffId());
  188. List<Staff> staffList = staffMapper.selectByExample(staffExample);
  189. Staff staff = staffList.get(0);
  190. JSONObject jsonObject = new JSONObject();
  191. jsonObject.put("chat_type", "single");
  192. JSONObject text = new JSONObject();
  193. if (StringUtils.isNotEmpty(specialSendMessage.getContent())) {
  194. text.put("content", specialSendMessage.getContent());
  195. } else {
  196. text.put("content", messageService.getMessageText(staff.getId()));
  197. }
  198. jsonObject.put("text", text);
  199. jsonObject.put("sender", staff.getCarrierId());
  200. String attachmentIds = specialSendMessage.getAttachmentIds();
  201. JSONArray attachments = new JSONArray();
  202. if (StringUtils.isNotEmpty(attachmentIds)) {
  203. List<Long> attachmentIdList = JSONArray.parseArray(attachmentIds, Long.class);
  204. if (!CollectionUtils.isEmpty(attachmentIdList)) {
  205. List<MessageAttachment> messageAttachments = messageAttachmentService.getMessageAttachment(attachmentIdList);
  206. if (!CollectionUtils.isEmpty(messageAttachments)) {
  207. for (MessageAttachment messageAttachment : messageAttachments) {
  208. Integer type = messageAttachment.getType();
  209. if (Objects.equals(MessageAttachmentTypeEnum.MINI_PROGRAM.getCode(), type)) {
  210. JSONObject miniprogramAttachment = new JSONObject();
  211. JSONObject miniprogram = new JSONObject();
  212. if (StringUtils.isNotEmpty(messageAttachment.getAppid())) {
  213. miniprogram.put("appid", messageAttachment.getAppid());
  214. } else {
  215. miniprogram.put("appid", MessageConstant.appid);
  216. }
  217. String title = messageAttachment.getTitle();
  218. if (title.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) {
  219. title = ToolUtils.truncateString(title, MAX_BYTES - 3) + "...";
  220. }
  221. miniprogram.put("title", title);
  222. String picMediaId = messageAttachment.getMediaId();
  223. if (StringUtils.isEmpty(picMediaId)) {
  224. return false;
  225. }
  226. miniprogram.put("pic_media_id", picMediaId);
  227. String page = messageAttachment.getPage();
  228. if (StringUtils.isEmpty(page)) {
  229. return false;
  230. }
  231. miniprogram.put("page", page);
  232. miniprogramAttachment.put("msgtype", "miniprogram");
  233. miniprogramAttachment.put("miniprogram", miniprogram);
  234. attachments.add(miniprogramAttachment);
  235. }
  236. if (Objects.equals(MessageAttachmentTypeEnum.LINK.getCode(), type)) {
  237. JSONObject linkAttachment = new JSONObject();
  238. JSONObject link = new JSONObject();
  239. link.put("title", messageAttachment.getTitle());
  240. link.put("picurl", messageAttachment.getPicUrl());
  241. link.put("desc", messageAttachment.getDesc());
  242. link.put("url", messageAttachment.getUrl());
  243. linkAttachment.put("msgtype", "link");
  244. linkAttachment.put("link", link);
  245. attachments.add(linkAttachment);
  246. }
  247. if (Objects.equals(MessageAttachmentTypeEnum.IMAGE.getCode(), type)) {
  248. JSONObject imageAttachment = new JSONObject();
  249. JSONObject image = new JSONObject();
  250. image.put("media_id", messageAttachment.getMediaId());
  251. imageAttachment.put("msgtype", "image");
  252. imageAttachment.put("image", image);
  253. attachments.add(imageAttachment);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. if (!attachments.isEmpty()) {
  260. jsonObject.put("attachments", attachments);
  261. }
  262. List<List<String>> lists = Lists.partition(sendUserList, 10000);
  263. for (List<String> list : lists) {
  264. JSONArray externalUserIds = JSONArray.parseArray(JSON.toJSONString(list));
  265. JSONObject newJSONObject = new JSONObject();
  266. newJSONObject.putAll(jsonObject);
  267. newJSONObject.put("external_userid", externalUserIds);
  268. pushList.add(newJSONObject);
  269. }
  270. if (CollectionUtils.isEmpty(pushList)) {
  271. return false;
  272. }
  273. for (JSONObject pushJsonObject : pushList) {
  274. boolean flag = messageService.pushWeComMessage(pushJsonObject, 1L);
  275. if (!flag) {
  276. return flag;
  277. }
  278. }
  279. return true;
  280. }
  281. }