|
@@ -13,13 +13,15 @@ import com.tzld.piaoquan.wecom.utils.DateUtil;
|
|
|
import com.tzld.piaoquan.wecom.utils.MessageUtil;
|
|
|
import com.tzld.piaoquan.wecom.utils.OdpsUtil;
|
|
|
import com.tzld.piaoquan.wecom.utils.page.Page;
|
|
|
+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.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import java.io.PrintStream;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -50,6 +52,9 @@ public class WeComMessageDataJob {
|
|
|
@Autowired
|
|
|
private StaffMapper staffMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ SendMessageMapper sendMessageMapper;
|
|
|
+
|
|
|
private static final int MAX_VIDEO_NUM = 3;
|
|
|
|
|
|
//历史优质视频可推送用户列表
|
|
@@ -114,7 +119,8 @@ public class WeComMessageDataJob {
|
|
|
guaranteedVideoIdList = saveVideoIds;
|
|
|
}
|
|
|
|
|
|
- public void assemble() {
|
|
|
+ @XxlJob("assembleSendMessageJob")
|
|
|
+ public ReturnT<String> assembleSendMessage(String param) {
|
|
|
init();
|
|
|
Map<String, List<String>> res = new HashMap<>();
|
|
|
UserExample example = new UserExample();
|
|
@@ -128,22 +134,21 @@ public class WeComMessageDataJob {
|
|
|
if (CollectionUtils.isEmpty(userList)) {
|
|
|
continue;
|
|
|
}
|
|
|
+ //落库逻辑
|
|
|
+ List<SendMessage> allSeneMessageList = new ArrayList<>();
|
|
|
for (User user : userList) {
|
|
|
- List<String> staffPushKeys = getStaffPushKey(user);
|
|
|
- if (CollectionUtils.isEmpty(staffPushKeys)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- for (String staffPushKey : staffPushKeys) {
|
|
|
- if (res.containsKey(staffPushKey)) {
|
|
|
- res.get(staffPushKey).add(user.getExternalUserId3rdParty());
|
|
|
- } else {
|
|
|
- res.put(staffPushKey, Lists.newArrayList(user.getExternalUserId3rdParty()));
|
|
|
- }
|
|
|
+ List<SendMessage> sendMessageList = getSendMessage(user);
|
|
|
+ if (!CollectionUtils.isEmpty(sendMessageList)) {
|
|
|
+ allSeneMessageList.addAll(sendMessageList);
|
|
|
}
|
|
|
}
|
|
|
+ if (!CollectionUtils.isEmpty(allSeneMessageList)) {
|
|
|
+ sendMessageMapper.insertList(allSeneMessageList);
|
|
|
+ }
|
|
|
}
|
|
|
- pushMessage(res);
|
|
|
+ //组装好当天要发送的消息后 记录时间 删除保底数据
|
|
|
saveGuaranteedVideoIdList(guaranteedVideoIdList);
|
|
|
+ return ReturnT.SUCCESS;
|
|
|
}
|
|
|
|
|
|
public void saveGuaranteedVideoIdList(List<Long> videoIdList) {
|
|
@@ -162,123 +167,160 @@ public class WeComMessageDataJob {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public List<String> getStaffPushKey(User user) {
|
|
|
- StaffWithUserExample example = new StaffWithUserExample();
|
|
|
- example.createCriteria().andUserIdEqualTo(user.getId());
|
|
|
- List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(example);
|
|
|
- if (CollectionUtils.isEmpty(staffWithUserList)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- String pushKey = getPushKey(user);
|
|
|
- List<String> staffPushKeys = new ArrayList<>();
|
|
|
- for (StaffWithUser staffWithUser : staffWithUserList) {
|
|
|
- staffPushKeys.add(staffWithUser.getId() + "&" + pushKey);
|
|
|
- }
|
|
|
- return staffPushKeys;
|
|
|
- }
|
|
|
-
|
|
|
- public String getPushKey(User user) {
|
|
|
+ public List<SendMessage> getSendMessage(User user) {
|
|
|
int n = 0;
|
|
|
- StringBuilder key = new StringBuilder();
|
|
|
+ List<SendMessage> sendMessageList = new ArrayList<>();
|
|
|
+ SendMessage sendMessage = new SendMessage();
|
|
|
for (PushMessage pushMessage : goodHistoryPushList) {
|
|
|
if (pushMessage.getUserIds().contains(user.getId())) {
|
|
|
- if (key.length() == 0) {
|
|
|
- key.append(pushMessage.getVideoId());
|
|
|
- n++;
|
|
|
- } else {
|
|
|
- key.append("_");
|
|
|
- key.append(pushMessage.getVideoId());
|
|
|
+ if (n == 0) {
|
|
|
+ sendMessage.setVideoId1(pushMessage.getVideoId());
|
|
|
+ }
|
|
|
+ if (n == 1) {
|
|
|
+ sendMessage.setVideoId2(pushMessage.getVideoId());
|
|
|
+ }
|
|
|
+ if (n == 2) {
|
|
|
+ sendMessage.setVideoId3(pushMessage.getVideoId());
|
|
|
}
|
|
|
n++;
|
|
|
- if (n > MAX_VIDEO_NUM) {
|
|
|
- return key.toString();
|
|
|
+ if (n >= MAX_VIDEO_NUM) {
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- for (Long videoId : guaranteedVideoIdList) {
|
|
|
- if (key.length() == 0) {
|
|
|
- key.append(videoId);
|
|
|
+ //保底数据
|
|
|
+ if (n < MAX_VIDEO_NUM) {
|
|
|
+ for (Long videoId : guaranteedVideoIdList) {
|
|
|
+ if (n == 0) {
|
|
|
+ sendMessage.setVideoId1(videoId);
|
|
|
+ }
|
|
|
+ if (n == 1) {
|
|
|
+ sendMessage.setVideoId2(videoId);
|
|
|
+ }
|
|
|
+ if (n == 2) {
|
|
|
+ sendMessage.setVideoId3(videoId);
|
|
|
+ }
|
|
|
n++;
|
|
|
- } else {
|
|
|
- key.append("_");
|
|
|
- key.append(videoId);
|
|
|
+ if (n >= MAX_VIDEO_NUM) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- n++;
|
|
|
- if (n > MAX_VIDEO_NUM) {
|
|
|
- return key.toString();
|
|
|
+ }
|
|
|
+ if (n < MAX_VIDEO_NUM) {
|
|
|
+ throw new RuntimeException("保底数据异常");
|
|
|
+ }
|
|
|
+ StaffWithUserExample example = new StaffWithUserExample();
|
|
|
+ example.createCriteria().andUserIdEqualTo(user.getId());
|
|
|
+ List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(example);
|
|
|
+ if (CollectionUtils.isEmpty(staffWithUserList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (StaffWithUser staffWithUser : staffWithUserList) {
|
|
|
+ SendMessage newSendMessage = new SendMessage();
|
|
|
+ BeanUtils.copyProperties(sendMessage, newSendMessage);
|
|
|
+ newSendMessage.setStaffId(staffWithUser.getStaffId());
|
|
|
+ newSendMessage.setUserId(staffWithUser.getUserId());
|
|
|
+ sendMessageList.add(newSendMessage);
|
|
|
+ }
|
|
|
+ return sendMessageList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @XxlJob("pushSendMessageJob")
|
|
|
+ public ReturnT<String> pushSendMessage(String param) {
|
|
|
+ List<SendMessage> groupList = sendMessageMapper.getGroupList(DateUtil.getThatDayDate(), 0);
|
|
|
+ for (SendMessage sendMessage : groupList) {
|
|
|
+ sendMessage.setIsSend(0);
|
|
|
+ sendMessage.setCreateTime(DateUtil.getThatDayDate());
|
|
|
+ List<String> sendUserList = sendMessageMapper.selectExternalUserId3rdParty(sendMessage);
|
|
|
+ boolean flag = pushMessage(sendUserList, sendMessage);
|
|
|
+ if (flag) {
|
|
|
+ SendMessage updateSendMessage = new SendMessage();
|
|
|
+ updateSendMessage.setIsSend(1);
|
|
|
+ SendMessageExample example = new SendMessageExample();
|
|
|
+ example.createCriteria()
|
|
|
+ .andVideoId1EqualTo(sendMessage.getVideoId1())
|
|
|
+ .andVideoId2EqualTo(sendMessage.getVideoId2())
|
|
|
+ .andVideoId3EqualTo(sendMessage.getVideoId3())
|
|
|
+ .andStaffIdEqualTo(sendMessage.getStaffId())
|
|
|
+ .andCreateTimeGreaterThan(DateUtil.getThatDayDate());
|
|
|
+ sendMessageMapper.updateByExampleSelective(updateSendMessage, example);
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
- return key.toString();
|
|
|
+ return ReturnT.SUCCESS;
|
|
|
}
|
|
|
|
|
|
- public void pushMessage(Map<String, List<String>> map) {
|
|
|
+ public boolean pushMessage(List<String> sendUserList, SendMessage sendMessage) {
|
|
|
List<JSONObject> pushList = new ArrayList<>();
|
|
|
+ StaffExample staffExample = new StaffExample();
|
|
|
+ staffExample.createCriteria().andIdEqualTo(sendMessage.getStaffId());
|
|
|
+ List<Staff> staffList = staffMapper.selectByExample(staffExample);
|
|
|
+ Staff staff = staffList.get(0);
|
|
|
String text = messageService.getMessageText();
|
|
|
String name = MessageUtil.getName();
|
|
|
- for (Map.Entry<String, List<String>> entry : map.entrySet()) {
|
|
|
- String[] split = entry.getKey().split("&");
|
|
|
- Long staffId = Long.parseLong(split[0]);
|
|
|
- StaffExample staffExample = new StaffExample();
|
|
|
- staffExample.createCriteria().andIdEqualTo(staffId);
|
|
|
- List<Staff> staffList = staffMapper.selectByExample(staffExample);
|
|
|
- Staff staff = staffList.get(0);
|
|
|
- String pushKey = split[1];
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("name", name);
|
|
|
- jsonObject.put("text", text);
|
|
|
- JSONArray attachments = new JSONArray();
|
|
|
- List<Long> videoIdList = Arrays.stream(pushKey.split("_")).map(Long::parseLong).collect(Collectors.toList());
|
|
|
- for (Long videoId : videoIdList) {
|
|
|
- JSONObject attachment = new JSONObject();
|
|
|
- attachment.put("msgtype", "miniprogram");
|
|
|
- MessageAttachmentExample example = new MessageAttachmentExample();
|
|
|
- example.createCriteria().andMiniprogramVideoIdEqualTo(videoId);
|
|
|
- List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
|
|
|
- if (CollectionUtils.isEmpty(messageAttachmentList)) {
|
|
|
- throw new RuntimeException("附件信息查询异常");
|
|
|
- }
|
|
|
- MessageAttachment messageAttachment = messageAttachmentList.get(0);
|
|
|
- JSONObject miniprogram = new JSONObject();
|
|
|
- miniprogram.put("appid", messageAttachment.getAppid());
|
|
|
- miniprogram.put("title", messageAttachment.getTitle());
|
|
|
|
|
|
- String page = "";
|
|
|
- String key = staff.getStaffExtId() + "_" + videoId;
|
|
|
- if (pageMap.containsKey(key)) {
|
|
|
- page = pageMap.get(key);
|
|
|
- } else {
|
|
|
- page = messageAttachmentService.getPage(staff, videoId);
|
|
|
- pageMap.put(key, page);
|
|
|
- }
|
|
|
- if (StringUtils.isEmpty(page)) {
|
|
|
- throw new RuntimeException("获取page失败");
|
|
|
- }
|
|
|
- miniprogram.put("page", page);
|
|
|
- miniprogram.put("cover", messageAttachment.getCover());
|
|
|
- attachment.put("miniprogram", miniprogram);
|
|
|
- attachments.add(attachment);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("name", name);
|
|
|
+ jsonObject.put("text", text);
|
|
|
+ JSONArray attachments = new JSONArray();
|
|
|
+ List<Long> videoIdList = new ArrayList<>();
|
|
|
+ videoIdList.add(sendMessage.getVideoId1());
|
|
|
+ videoIdList.add(sendMessage.getVideoId2());
|
|
|
+ videoIdList.add(sendMessage.getVideoId3());
|
|
|
+ for (Long videoId : videoIdList) {
|
|
|
+ JSONObject attachment = new JSONObject();
|
|
|
+ attachment.put("msgtype", "miniprogram");
|
|
|
+ MessageAttachmentExample example = new MessageAttachmentExample();
|
|
|
+ example.createCriteria().andMiniprogramVideoIdEqualTo(videoId);
|
|
|
+ List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
|
|
|
+ if (CollectionUtils.isEmpty(messageAttachmentList)) {
|
|
|
+ throw new RuntimeException("附件信息查询异常");
|
|
|
+ }
|
|
|
+ MessageAttachment messageAttachment = messageAttachmentList.get(0);
|
|
|
+ JSONObject miniprogram = new JSONObject();
|
|
|
+ miniprogram.put("appid", messageAttachment.getAppid());
|
|
|
+ miniprogram.put("title", messageAttachment.getTitle());
|
|
|
+ miniprogram.put("cover", messageAttachment.getCover());
|
|
|
+ String page = "";
|
|
|
+ String key = staff.getStaffExtId() + "_" + videoId;
|
|
|
+ if (pageMap.containsKey(key)) {
|
|
|
+ page = pageMap.get(key);
|
|
|
+ } else {
|
|
|
+ page = messageAttachmentService.getPage(staff, videoId);
|
|
|
+ pageMap.put(key, page);
|
|
|
}
|
|
|
- jsonObject.put("attachments", attachments);
|
|
|
- List<List<String>> lists = Lists.partition(entry.getValue(), 10000);
|
|
|
- for (List<String> list : lists) {
|
|
|
- List<JSONObject> staffEuList = new ArrayList<>();
|
|
|
- JSONObject newJSONObject = new JSONObject();
|
|
|
- newJSONObject.putAll(jsonObject);
|
|
|
- JSONObject staff_eu = new JSONObject();
|
|
|
- staff_eu.put("staff_ext_id", staff.getStaffExtId());
|
|
|
- staff_eu.put("eu_ext_ids", list);
|
|
|
- staffEuList.add(staff_eu);
|
|
|
- newJSONObject.put("staff_eu_list", staffEuList);
|
|
|
- pushList.add(newJSONObject);
|
|
|
+ if (StringUtils.isEmpty(page)) {
|
|
|
+ throw new RuntimeException("获取page失败");
|
|
|
}
|
|
|
+ miniprogram.put("page", page);
|
|
|
+
|
|
|
+ attachment.put("miniprogram", miniprogram);
|
|
|
+ attachments.add(attachment);
|
|
|
+ }
|
|
|
+ jsonObject.put("attachments", attachments);
|
|
|
+ List<List<String>> lists = Lists.partition(sendUserList, 10000);
|
|
|
+ for (List<String> list : lists) {
|
|
|
+ List<JSONObject> staffEuList = new ArrayList<>();
|
|
|
+ JSONObject newJSONObject = new JSONObject();
|
|
|
+ newJSONObject.putAll(jsonObject);
|
|
|
+ JSONObject staff_eu = new JSONObject();
|
|
|
+ staff_eu.put("staff_ext_id", staff.getStaffExtId());
|
|
|
+ staff_eu.put("eu_ext_ids", list);
|
|
|
+ staffEuList.add(staff_eu);
|
|
|
+ newJSONObject.put("staff_eu_list", staffEuList);
|
|
|
+ pushList.add(newJSONObject);
|
|
|
}
|
|
|
+
|
|
|
if (CollectionUtils.isEmpty(pushList)) {
|
|
|
throw new RuntimeException("推送视频生成失败");
|
|
|
}
|
|
|
-
|
|
|
- System.out.println(pushList);
|
|
|
- for (JSONObject jsonObject : pushList) {
|
|
|
- messageService.pushMessage(jsonObject);
|
|
|
+ for (JSONObject pushJsonObject : pushList) {
|
|
|
+ boolean flag = messageService.pushMessage(pushJsonObject);
|
|
|
+ if (!flag) {
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|