WeComMomentDataJob.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.tzld.piaoquan.offline.job;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
  5. import com.tzld.piaoquan.growth.common.dao.mapper.MomentSendMessageMapper;
  6. import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
  7. import com.tzld.piaoquan.growth.common.dao.mapper.UserWithTagMapper;
  8. import com.tzld.piaoquan.growth.common.model.po.MessageAttachment;
  9. import com.tzld.piaoquan.growth.common.model.po.MomentSendMessage;
  10. import com.tzld.piaoquan.growth.common.model.po.MomentSendMessageExample;
  11. import com.tzld.piaoquan.growth.common.model.po.Staff;
  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.DateUtil;
  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.util.List;
  22. import java.util.Objects;
  23. @Component
  24. public class WeComMomentDataJob {
  25. @Autowired
  26. private StaffMapper staffMapper;
  27. @Autowired
  28. private MessageService messageService;
  29. @Autowired
  30. private MessageAttachmentService messageAttachmentService;
  31. @Autowired
  32. UserWithTagMapper userWithTagMapper;
  33. @Autowired
  34. private MomentSendMessageMapper momentSendMessageMapper;
  35. @XxlJob("sendMomentMessageJob")
  36. public ReturnT<String> sendMomentMessage(String param) {
  37. String thatDayDateString = DateUtil.getThatDayDateString();
  38. MomentSendMessageExample example = new MomentSendMessageExample();
  39. example.createCriteria().andPreSendDateEqualTo(thatDayDateString).andIsSendEqualTo(0);
  40. List<MomentSendMessage> momentSendMessages = momentSendMessageMapper.selectByExample(example);
  41. for (MomentSendMessage momentSendMessage : momentSendMessages) {
  42. Staff staff = staffMapper.selectByPrimaryKey(momentSendMessage.getStaffId());
  43. JSONObject jsonObject = new JSONObject();
  44. JSONObject text = new JSONObject();
  45. if (StringUtils.isNotEmpty(momentSendMessage.getContent())) {
  46. text.put("content", momentSendMessage.getContent());
  47. jsonObject.put("text", text);
  48. }
  49. String attachmentIds = momentSendMessage.getAttachmentIds();
  50. JSONArray attachments = new JSONArray();
  51. if (StringUtils.isNotEmpty(attachmentIds)) {
  52. List<Long> attachmentIdList = JSONArray.parseArray(attachmentIds, Long.class);
  53. if (!CollectionUtils.isEmpty(attachmentIdList)) {
  54. List<MessageAttachment> messageAttachments = messageAttachmentService.getMessageAttachment(attachmentIdList);
  55. if (!CollectionUtils.isEmpty(messageAttachments)) {
  56. for (MessageAttachment messageAttachment : messageAttachments) {
  57. Integer type = messageAttachment.getType();
  58. if (Objects.equals(MessageAttachmentTypeEnum.LINK.getCode(), type)) {
  59. JSONObject linkAttachment = new JSONObject();
  60. JSONObject link = new JSONObject();
  61. link.put("title", messageAttachment.getTitle());
  62. link.put("media_id", messageAttachment.getMediaId());
  63. link.put("url", messageAttachment.getUrl());
  64. linkAttachment.put("msgtype", "link");
  65. linkAttachment.put("link", link);
  66. attachments.add(linkAttachment);
  67. }
  68. if (Objects.equals(MessageAttachmentTypeEnum.IMAGE.getCode(), type)) {
  69. JSONObject imageAttachment = new JSONObject();
  70. JSONObject image = new JSONObject();
  71. image.put("media_id", messageAttachment.getMediaId());
  72. imageAttachment.put("msgtype", "image");
  73. imageAttachment.put("image", image);
  74. attachments.add(imageAttachment);
  75. }
  76. if (Objects.equals(MessageAttachmentTypeEnum.VIDEO.getCode(), type)) {
  77. JSONObject videoAttachment = new JSONObject();
  78. JSONObject image = new JSONObject();
  79. image.put("media_id", messageAttachment.getMediaId());
  80. videoAttachment.put("msgtype", "video");
  81. videoAttachment.put("video", image);
  82. attachments.add(videoAttachment);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. if (!attachments.isEmpty()) {
  89. jsonObject.put("attachments", attachments);
  90. }
  91. JSONObject visibleRange = new JSONObject();
  92. JSONObject senderList = new JSONObject();
  93. JSONArray userList = new JSONArray();
  94. userList.add(staff.getCarrierId());
  95. senderList.put("user_list", userList);
  96. visibleRange.put("sender_list", senderList);
  97. jsonObject.put("visible_range", visibleRange);
  98. boolean flag = messageService.pushMomentMessage(jsonObject, staff.getCorpId());
  99. if (flag) {
  100. momentSendMessage.setIsSend(1);
  101. momentSendMessageMapper.updateByPrimaryKeySelective(momentSendMessage);
  102. }
  103. }
  104. return ReturnT.SUCCESS;
  105. }
  106. }