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 attachmentIds, String content) { List 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 sendSpecialPushMessage(String param) { List groupList = specialSendMessageMapper.getGroupList(); for (SpecialSendMessage specialSendMessage : groupList) { List 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 sendUserList, SpecialSendMessage specialSendMessage) { List pushList = new ArrayList<>(); StaffExample staffExample = new StaffExample(); staffExample.createCriteria().andIdEqualTo(specialSendMessage.getStaffId()); List 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 attachmentIdList = JSONArray.parseArray(attachmentIds, Long.class); if (!CollectionUtils.isEmpty(attachmentIdList)) { List 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> lists = Lists.partition(sendUserList, 10000); for (List 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; } }