123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package com.tzld.piaoquan.offline.job;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.google.common.collect.Lists;
- import com.tzld.piaoquan.growth.common.common.constant.MessageConstant;
- import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
- import com.tzld.piaoquan.growth.common.dao.mapper.SpecialSendMessageMapper;
- import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
- import com.tzld.piaoquan.growth.common.dao.mapper.WeComUserMapper;
- import com.tzld.piaoquan.growth.common.model.po.*;
- import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
- import com.tzld.piaoquan.growth.common.service.MessageService;
- import com.tzld.piaoquan.growth.common.utils.ToolUtils;
- import com.xxl.job.core.biz.model.ReturnT;
- import com.xxl.job.core.handler.annotation.XxlJob;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.util.CollectionUtils;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Objects;
- @Component
- public class WeComSpecialDataJob {
- @Autowired
- private WeComUserMapper weComUserMapper;
- @Autowired
- private StaffMapper staffMapper;
- @Autowired
- private SpecialSendMessageMapper specialSendMessageMapper;
- @Autowired
- private MessageService messageService;
- @Autowired
- private MessageAttachmentService messageAttachmentService;
- //发送小程序标题限制字节数
- private static final int MAX_BYTES = 64;
- public void specialAssembleSendMessage(Long staffId, Integer pageNum, Integer pageSize,
- byte groupMsgDisabled, List<Long> attachmentIds, String content) {
- List<WeComUser> weComUserList = weComUserMapper.selectUserList(staffId, groupMsgDisabled, (pageNum - 1) * pageSize, pageSize);
- //落库逻辑
- for (WeComUser weComUser : weComUserList) {
- SpecialSendMessage specialSendMessage = new SpecialSendMessage();
- specialSendMessage.setStaffId(staffId);
- specialSendMessage.setUserId(weComUser.getId());
- specialSendMessage.setGroup(pageNum);
- specialSendMessage.setAttachmentIds(JSONObject.toJSONString(attachmentIds));
- specialSendMessage.setContent(content);
- specialSendMessageMapper.insertSelective(specialSendMessage);
- }
- }
- @XxlJob("sendSpecialPushMessageJob")
- public ReturnT<String> sendSpecialPushMessage(String param) {
- List<SpecialSendMessage> groupList = specialSendMessageMapper.getGroupList();
- for (SpecialSendMessage specialSendMessage : groupList) {
- List<String> externalUserIds = specialSendMessageMapper.selectExternalUserId(specialSendMessage.getStaffId(), specialSendMessage.getGroup());
- boolean flag = specialPushMessage(externalUserIds, specialSendMessage);
- if (flag) {
- SpecialSendMessage updateSpecialSendMessage = new SpecialSendMessage();
- updateSpecialSendMessage.setIsSend(1);
- SpecialSendMessageExample example = new SpecialSendMessageExample();
- example.createCriteria()
- .andStaffIdEqualTo(specialSendMessage.getStaffId())
- .andGroupEqualTo(specialSendMessage.getGroup())
- .andContentEqualTo(specialSendMessage.getContent())
- .andAttachmentIdsEqualTo(specialSendMessage.getAttachmentIds());
- specialSendMessageMapper.updateByExampleSelective(updateSpecialSendMessage, example);
- }
- }
- return ReturnT.SUCCESS;
- }
- private boolean specialPushMessage(List<String> sendUserList, SpecialSendMessage specialSendMessage) {
- List<JSONObject> pushList = new ArrayList<>();
- StaffExample staffExample = new StaffExample();
- staffExample.createCriteria().andIdEqualTo(specialSendMessage.getStaffId());
- List<Staff> staffList = staffMapper.selectByExample(staffExample);
- Staff staff = staffList.get(0);
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("chat_type", "single");
- JSONObject text = new JSONObject();
- if (StringUtils.isNotEmpty(specialSendMessage.getContent())) {
- text.put("content", specialSendMessage.getContent());
- } else {
- text.put("content", messageService.getMessageText());
- }
- jsonObject.put("text", text);
- jsonObject.put("sender", staff.getCarrierId());
- String attachmentIds = specialSendMessage.getAttachmentIds();
- JSONArray attachments = new JSONArray();
- if (StringUtils.isNotEmpty(attachmentIds)) {
- List<Long> attachmentIdList = JSONArray.parseArray(attachmentIds, Long.class);
- if (!CollectionUtils.isEmpty(attachmentIdList)) {
- List<MessageAttachment> messageAttachments = messageAttachmentService.getMessageAttachment(attachmentIdList);
- if (!CollectionUtils.isEmpty(messageAttachments)) {
- for (MessageAttachment messageAttachment : messageAttachments) {
- Integer type = messageAttachment.getType();
- if (Objects.equals(MessageAttachmentTypeEnum.MINI_PROGRAM.getCode(), type)) {
- JSONObject miniprogramAttachment = new JSONObject();
- JSONObject miniprogram = new JSONObject();
- miniprogram.put("appid", MessageConstant.appid);
- String title = messageAttachment.getTitle();
- if (title.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) {
- title = ToolUtils.truncateString(title, MAX_BYTES - 3) + "...";
- }
- miniprogram.put("title", title);
- String picMediaId = messageAttachment.getMediaId();
- if (StringUtils.isEmpty(picMediaId)) {
- return false;
- }
- miniprogram.put("pic_media_id", picMediaId);
- String page = messageAttachment.getPage();
- if (StringUtils.isEmpty(page)) {
- return false;
- }
- miniprogram.put("page", page);
- miniprogramAttachment.put("msgtype", "miniprogram");
- miniprogramAttachment.put("miniprogram", miniprogram);
- attachments.add(miniprogramAttachment);
- }
- if (Objects.equals(MessageAttachmentTypeEnum.LINK.getCode(), type)) {
- JSONObject linkAttachment = new JSONObject();
- JSONObject link = new JSONObject();
- link.put("title", messageAttachment.getTitle());
- link.put("picurl", messageAttachment.getPicUrl());
- link.put("desc", messageAttachment.getDesc());
- link.put("url", messageAttachment.getUrl());
- linkAttachment.put("msgtype", "link");
- linkAttachment.put("link", link);
- attachments.add(linkAttachment);
- }
- if (Objects.equals(MessageAttachmentTypeEnum.IMAGE.getCode(), type)) {
- JSONObject imageAttachment = new JSONObject();
- JSONObject image = new JSONObject();
- image.put("media_id", messageAttachment.getMediaId());
- imageAttachment.put("msgtype", "image");
- imageAttachment.put("image", image);
- attachments.add(imageAttachment);
- }
- }
- }
- }
- }
- if (!attachments.isEmpty()) {
- jsonObject.put("attachments", attachments);
- }
- List<List<String>> lists = Lists.partition(sendUserList, 10000);
- for (List<String> list : lists) {
- JSONArray externalUserIds = JSONArray.parseArray(JSON.toJSONString(list));
- JSONObject newJSONObject = new JSONObject();
- newJSONObject.putAll(jsonObject);
- newJSONObject.put("external_userid", externalUserIds);
- pushList.add(newJSONObject);
- }
- if (CollectionUtils.isEmpty(pushList)) {
- return false;
- }
- for (JSONObject pushJsonObject : pushList) {
- boolean flag = messageService.pushWeComMessage(pushJsonObject, 1L);
- if (!flag) {
- return flag;
- }
- }
- return true;
- }
- }
|