|
@@ -2,34 +2,41 @@ package com.tzld.piaoquan.wecom.job;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.google.common.collect.Lists;
|
|
import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
|
|
import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
|
|
import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
|
|
import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
|
|
-import com.tzld.piaoquan.wecom.dao.mapper.AlertMessageMapper;
|
|
|
|
import com.tzld.piaoquan.wecom.dao.mapper.HistoryMessageMapper;
|
|
import com.tzld.piaoquan.wecom.dao.mapper.HistoryMessageMapper;
|
|
import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
|
|
import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
|
|
import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
|
|
import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
|
|
|
|
+import com.tzld.piaoquan.wecom.model.bo.ExternalUser;
|
|
import com.tzld.piaoquan.wecom.model.bo.MiniprogramRecord;
|
|
import com.tzld.piaoquan.wecom.model.bo.MiniprogramRecord;
|
|
|
|
+import com.tzld.piaoquan.wecom.model.bo.SendDetail;
|
|
import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
|
|
import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
|
|
import com.tzld.piaoquan.wecom.model.po.*;
|
|
import com.tzld.piaoquan.wecom.model.po.*;
|
|
import com.tzld.piaoquan.wecom.service.AccessTokenService;
|
|
import com.tzld.piaoquan.wecom.service.AccessTokenService;
|
|
import com.tzld.piaoquan.wecom.service.HistoryMessageService;
|
|
import com.tzld.piaoquan.wecom.service.HistoryMessageService;
|
|
import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
|
|
import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
|
|
-import com.tzld.piaoquan.wecom.utils.*;
|
|
|
|
|
|
+import com.tzld.piaoquan.wecom.service.UserService;
|
|
|
|
+import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
|
|
|
|
+import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
|
|
|
|
+import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
|
|
|
|
+import com.tzld.piaoquan.wecom.utils.MessageUtil;
|
|
import com.tzld.piaoquan.wecom.utils.page.Page;
|
|
import com.tzld.piaoquan.wecom.utils.page.Page;
|
|
import com.xxl.job.core.biz.model.ReturnT;
|
|
import com.xxl.job.core.biz.model.ReturnT;
|
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
-import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
-import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_HISTORY_DATA_URL;
|
|
|
|
|
|
+import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Component
|
|
@Component
|
|
@@ -37,7 +44,6 @@ public class WeComHistoryDataJob {
|
|
|
|
|
|
private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
|
|
private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
|
|
|
|
|
|
- final int size = 20;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private AccessTokenService accessTokenService;
|
|
private AccessTokenService accessTokenService;
|
|
@@ -55,13 +61,13 @@ public class WeComHistoryDataJob {
|
|
private MessageAttachmentService messageAttachmentService;
|
|
private MessageAttachmentService messageAttachmentService;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private AlertMessageMapper alertMessageMapper;
|
|
|
|
|
|
+ private StaffMapper staffMapper;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private StaffMapper staffMapper;
|
|
|
|
|
|
+ private UserService userService;
|
|
|
|
|
|
|
|
|
|
- @XxlJob("saveHistoryMessageJob")
|
|
|
|
|
|
+ @XxlJob("saveHistoryMessageJob1")
|
|
public ReturnT<String> selectHistoryMessageByDay(String param) {
|
|
public ReturnT<String> selectHistoryMessageByDay(String param) {
|
|
XxlJobParam xxlJobParam = new XxlJobParam();
|
|
XxlJobParam xxlJobParam = new XxlJobParam();
|
|
if (StringUtils.isNotEmpty(param)) {
|
|
if (StringUtils.isNotEmpty(param)) {
|
|
@@ -72,7 +78,12 @@ public class WeComHistoryDataJob {
|
|
example.setOrderByClause("create_time desc");
|
|
example.setOrderByClause("create_time desc");
|
|
example.setPage(new Page<>(1, 1));
|
|
example.setPage(new Page<>(1, 1));
|
|
List<HistoryMessage> historyMessages = historyMessageMapper.selectByExample(example);
|
|
List<HistoryMessage> historyMessages = historyMessageMapper.selectByExample(example);
|
|
- xxlJobParam.setStartTime(historyMessages.get(0).getCreateTime().getTime() / 1000);
|
|
|
|
|
|
+ if (CollectionUtils.isEmpty(historyMessages)) {
|
|
|
|
+ xxlJobParam.setStartTime(1721404800L);
|
|
|
|
+ } else {
|
|
|
|
+ xxlJobParam.setStartTime(historyMessages.get(0).getCreateTime().getTime() / 1000);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
if (xxlJobParam.getEndTime() == null) {
|
|
if (xxlJobParam.getEndTime() == null) {
|
|
xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
@@ -81,217 +92,291 @@ public class WeComHistoryDataJob {
|
|
Long endTime = xxlJobParam.getEndTime();
|
|
Long endTime = xxlJobParam.getEndTime();
|
|
|
|
|
|
for (; startTime < endTime; startTime += TimeConstant.DAY) {
|
|
for (; startTime < endTime; startTime += TimeConstant.DAY) {
|
|
- selectHistoryMessage(startTime, Math.min(startTime + TimeConstant.DAY, endTime));
|
|
|
|
|
|
+ selectGroupMsgList(startTime, Math.min(startTime + TimeConstant.DAY, endTime));
|
|
}
|
|
}
|
|
|
|
+ selectAlertHistoryMessage(xxlJobParam.getStartTime(), xxlJobParam.getEndTime());
|
|
return ReturnT.SUCCESS;
|
|
return ReturnT.SUCCESS;
|
|
}
|
|
}
|
|
|
|
|
|
- public void selectHistoryMessage(Long startTime, Long endTime) {
|
|
|
|
- try {
|
|
|
|
- Integer total = getHistoryDataTotal(startTime, endTime);
|
|
|
|
- if (total == null || total == 0) {
|
|
|
|
- return;
|
|
|
|
|
|
+ public void selectAlertHistoryMessage(Long startTime, Long endTime) {
|
|
|
|
+
|
|
|
|
+ StaffExample staffExample = new StaffExample();
|
|
|
|
+ List<Staff> staffList = staffMapper.selectByExample(staffExample);
|
|
|
|
+ ArrayList<Integer> statusList = Lists.newArrayList(0, 1, 2, 3);
|
|
|
|
+ List<SendDetail> sendDetailList = new ArrayList<>();
|
|
|
|
+ for (Staff staff : staffList) {
|
|
|
|
+ SendDetail sendDetail = new SendDetail();
|
|
|
|
+ sendDetail.setRemark(staff.getRemark());
|
|
|
|
+ List<Long> countList = new ArrayList<>();
|
|
|
|
+ for (Integer status : statusList) {
|
|
|
|
+ HistoryMessageExample example = new HistoryMessageExample();
|
|
|
|
+ example.createCriteria().andSendTimeBetween(new Date(startTime * 1000), new Date(endTime * 1000))
|
|
|
|
+ .andStaffIdEqualTo(staff.getId())
|
|
|
|
+ .andStatusEqualTo(status)
|
|
|
|
+ .andIsDeleteEqualTo(0);
|
|
|
|
+ long l = historyMessageMapper.countByExample(example);
|
|
|
|
+ countList.add(l);
|
|
|
|
+ }
|
|
|
|
+ sendDetail.setSendCountList(countList);
|
|
|
|
+ sendDetailList.add(sendDetail);
|
|
|
|
+ }
|
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
+ for (SendDetail sendDetail : sendDetailList) {
|
|
|
|
+ List<Long> sendCountList = sendDetail.getSendCountList();
|
|
|
|
+ if (CollectionUtils.isEmpty(sendCountList)) {
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- int page = total / size + 1;
|
|
|
|
- for (int n = 0; n < page; n++) {
|
|
|
|
- String res = getHistoryData(size, n * size, startTime, endTime);
|
|
|
|
- log.info("selectHistoryMessage size={}, n={}, startTime={}, endTime={}, res={}", size, n, startTime, endTime, res);
|
|
|
|
- if (ObjectUtils.isEmpty(res)) {
|
|
|
|
|
|
+ stringBuilder.append(sendDetail.getRemark());
|
|
|
|
+ long count = sendCountList.get(0) + sendCountList.get(1) + sendCountList.get(2) + sendCountList.get(3);
|
|
|
|
+ stringBuilder.append("总发送数量:").append(count).append(" ");
|
|
|
|
+ stringBuilder.append("未发送数量:").append(sendCountList.get(0)).append(" ");
|
|
|
|
+ stringBuilder.append("已发送数量:").append(sendCountList.get(1)).append(" ");
|
|
|
|
+ stringBuilder.append("不是好友发送失败数量:").append(sendCountList.get(2)).append(" ");
|
|
|
|
+ stringBuilder.append("已经收到其他群发消息失败发送数量:").append(sendCountList.get(3)).append(" ");
|
|
|
|
+ stringBuilder.append("\n");
|
|
|
|
+ }
|
|
|
|
+ LarkRobotUtil.sendMessage(stringBuilder.toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void selectGroupMsgList(Long startTime, Long endTime) {
|
|
|
|
+ delHistoryMessageList(startTime, endTime);
|
|
|
|
+ try {
|
|
|
|
+ String cursor = "";
|
|
|
|
+ do {
|
|
|
|
+ String res = getGroupMsgList(startTime, endTime, cursor);
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
|
+ Integer errCode = jsonObject.getInteger("errcode");
|
|
|
|
+ if (errCode != 0) {
|
|
|
|
+ LarkRobotUtil.sendMessage("selectGroupMsgList error startTime = " + startTime + "endTime = " + endTime);
|
|
|
|
+ log.error("selectGroupMsgList error startTime={}, endTime={}", startTime, endTime);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
|
- JSONArray jsonArray = jsonObject.getJSONArray("missions");
|
|
|
|
- for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
|
- List<String> allExternalUserList = new ArrayList<>();
|
|
|
|
|
|
+ JSONArray groupMsgList = jsonObject.getJSONArray("group_msg_list");
|
|
|
|
+ if (CollectionUtils.isEmpty(groupMsgList)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ for (int i = 0; i < groupMsgList.size(); i++) {
|
|
|
|
+ JSONObject groupMsg = groupMsgList.getJSONObject(i);
|
|
|
|
+ JSONArray attachments = groupMsg.getJSONArray("attachments");
|
|
|
|
+ String msgId = groupMsg.getString("msgid");
|
|
|
|
+ Long createTime = null;
|
|
|
|
+ if (groupMsg.getLong("create_time") != null) {
|
|
|
|
+ createTime = groupMsg.getLong("create_time") * 1000;
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isEmpty(attachments)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
List<MiniprogramRecord> miniprogramRecordList = new ArrayList<>();
|
|
List<MiniprogramRecord> miniprogramRecordList = new ArrayList<>();
|
|
List<MessageAttachment> messageAttachmentList = new ArrayList<>();
|
|
List<MessageAttachment> messageAttachmentList = new ArrayList<>();
|
|
- Date sendAt = jsonArray.getJSONObject(i).getDate("send_at");
|
|
|
|
- JSONArray attachments = jsonArray.getJSONObject(i).getJSONObject("msg_data").getJSONArray("attachments");
|
|
|
|
for (int j = 0; j < attachments.size(); j++) {
|
|
for (int j = 0; j < attachments.size(); j++) {
|
|
|
|
+ JSONObject miniprogram = attachments.getJSONObject(j).getJSONObject("miniprogram");
|
|
|
|
+ if (miniprogram == null) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
MiniprogramRecord miniprogramRecord = new MiniprogramRecord();
|
|
MiniprogramRecord miniprogramRecord = new MiniprogramRecord();
|
|
MessageAttachment messageAttachment = new MessageAttachment();
|
|
MessageAttachment messageAttachment = new MessageAttachment();
|
|
- String indexPage = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("page");
|
|
|
|
- Long videoId = MessageUtil.getVideoId(indexPage);
|
|
|
|
|
|
+ String title = miniprogram.getString("title");
|
|
|
|
+ String appid = miniprogram.getString("appid");
|
|
|
|
+ String page = miniprogram.getString("page");
|
|
|
|
+ Long videoId = MessageUtil.getVideoId(page);
|
|
|
|
+
|
|
miniprogramRecord.setVideoId(videoId);
|
|
miniprogramRecord.setVideoId(videoId);
|
|
miniprogramRecord.setAttachmentIdx(j + 1);
|
|
miniprogramRecord.setAttachmentIdx(j + 1);
|
|
miniprogramRecordList.add(miniprogramRecord);
|
|
miniprogramRecordList.add(miniprogramRecord);
|
|
- String cover = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("cover");
|
|
|
|
- String appid = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("appid");
|
|
|
|
- String title = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("title");
|
|
|
|
|
|
+
|
|
messageAttachment.setAppid(appid);
|
|
messageAttachment.setAppid(appid);
|
|
- messageAttachment.setCover(cover);
|
|
|
|
- messageAttachment.setPage(indexPage);
|
|
|
|
|
|
+ messageAttachment.setPage(page);
|
|
messageAttachment.setTitle(title);
|
|
messageAttachment.setTitle(title);
|
|
messageAttachment.setMiniprogramVideoId(videoId);
|
|
messageAttachment.setMiniprogramVideoId(videoId);
|
|
- messageAttachment.setSendTime(sendAt);
|
|
|
|
messageAttachment.setType(MessageAttachmentTypeEnum.MINIPROGRAM.getType());
|
|
messageAttachment.setType(MessageAttachmentTypeEnum.MINIPROGRAM.getType());
|
|
messageAttachmentList.add(messageAttachment);
|
|
messageAttachmentList.add(messageAttachment);
|
|
}
|
|
}
|
|
- JSONArray subMissionList = jsonArray.getJSONObject(i).getJSONArray("sub_mission_list");
|
|
|
|
- for (int k = 0; k < subMissionList.size(); k++) {
|
|
|
|
- if (!"sended".equals(subMissionList.getJSONObject(k).getString("status"))) {
|
|
|
|
|
|
+ List<String> userIdList = selectGroupMsgTask(msgId);
|
|
|
|
+ if (CollectionUtils.isEmpty(userIdList)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ for (String userId : userIdList) {
|
|
|
|
+ StaffExample example = new StaffExample();
|
|
|
|
+ example.createCriteria().andCarrierIdEqualTo(userId);
|
|
|
|
+ List<Staff> staffList = staffMapper.selectByExample(example);
|
|
|
|
+ if (CollectionUtils.isEmpty(staffList)) {
|
|
|
|
+ LarkRobotUtil.sendMessage("企微推送报警:userId不存在请检查 " + userId);
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- List<String> externalUserList = subMissionList.getJSONObject(k).getJSONArray("external_user_list").toJavaList(String.class);
|
|
|
|
- if (CollectionUtils.isEmpty(externalUserList)) {
|
|
|
|
|
|
+ List<ExternalUser> externalUsers = selectGroupMsgSendResult(msgId, userId);
|
|
|
|
+ if (CollectionUtils.isEmpty(externalUsers)) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- allExternalUserList.addAll(externalUserList);
|
|
|
|
|
|
+ insertHistoryMessageList(staffList.get(0), externalUsers, miniprogramRecordList, createTime);
|
|
|
|
+ messageAttachmentService.addMiniProgram(messageAttachmentList);
|
|
}
|
|
}
|
|
- insertHistoryMessageList(allExternalUserList, miniprogramRecordList, sendAt);
|
|
|
|
- messageAttachmentService.addMiniProgram(messageAttachmentList);
|
|
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ String nextCursor = jsonObject.getString("next_cursor");
|
|
|
|
+ if (cursor.equals(nextCursor)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ cursor = nextCursor;
|
|
|
|
+ } while (StringUtils.isNotEmpty(cursor));
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- log.error("selectHistoryMessage error", e);
|
|
|
|
|
|
+ LarkRobotUtil.sendMessage("selectGroupMsgList error" + e);
|
|
|
|
+ log.error("selectGroupMsgList error", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private void insertHistoryMessageList(List<String> allExternalUserList, List<MiniprogramRecord> miniprogramRecordList, Date sendTime) {
|
|
|
|
- List<HistoryMessage> historyMessageList = new ArrayList<>();
|
|
|
|
- for (String externalUserId3rdParty : allExternalUserList) {
|
|
|
|
- Long userId = userMapper.selectIdByExternalUserId3rdParty(externalUserId3rdParty);
|
|
|
|
- if (userId == null) {
|
|
|
|
- continue;
|
|
|
|
|
|
+ private List<ExternalUser> selectGroupMsgSendResult(String msgId, String userId) throws IOException {
|
|
|
|
+ List<ExternalUser> resList = new ArrayList<>();
|
|
|
|
+ String cursor = "";
|
|
|
|
+ do {
|
|
|
|
+ String res = getGroupMsgSendResult(msgId, userId, cursor);
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
|
+ Integer errCode = jsonObject.getInteger("errcode");
|
|
|
|
+ if (errCode != 0) {
|
|
|
|
+ String errmsg = jsonObject.getString("errmsg");
|
|
|
|
+ log.error("selectGroupMsgSendResult error msgId={} userId={} errCode={} errmsg={}", msgId, userId, errCode, errmsg);
|
|
|
|
+ return resList;
|
|
}
|
|
}
|
|
- for (MiniprogramRecord miniprogramRecord : miniprogramRecordList) {
|
|
|
|
- HistoryMessage historyMessage = new HistoryMessage();
|
|
|
|
- historyMessage.setSendTime(sendTime);
|
|
|
|
- historyMessage.setAttachmentIdx(miniprogramRecord.getAttachmentIdx());
|
|
|
|
- historyMessage.setVideoId(miniprogramRecord.getVideoId());
|
|
|
|
- historyMessage.setUserId(userId);
|
|
|
|
- historyMessageList.add(historyMessage);
|
|
|
|
|
|
+ JSONArray sendList = jsonObject.getJSONArray("send_list");
|
|
|
|
+ for (int i = 0; i < sendList.size(); i++) {
|
|
|
|
+ JSONObject send = sendList.getJSONObject(i);
|
|
|
|
+ ExternalUser externalUser = new ExternalUser();
|
|
|
|
+ externalUser.setExternalUserId(send.getString("external_userid"));
|
|
|
|
+ externalUser.setStatus(send.getInteger("status"));
|
|
|
|
+ if (send.getLong("send_time") != null) {
|
|
|
|
+ externalUser.setSendTime(send.getLong("send_time") * 1000);
|
|
|
|
+ }
|
|
|
|
+ resList.add(externalUser);
|
|
}
|
|
}
|
|
- }
|
|
|
|
- historyMessageService.batchInsertHistoryMessage(historyMessageList);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public Integer getHistoryDataTotal(Long startTime, Long endTime) throws IOException {
|
|
|
|
- String res = getHistoryData(1, 0, startTime, endTime);
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
|
- return jsonObject.getInteger("total_count");
|
|
|
|
|
|
+ String nextCursor = jsonObject.getString("next_cursor");
|
|
|
|
+ if (cursor.equals(nextCursor)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ cursor = nextCursor;
|
|
|
|
+ } while (StringUtils.isNotEmpty(cursor));
|
|
|
|
+ return resList;
|
|
}
|
|
}
|
|
|
|
|
|
- public String getHistoryData(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
|
|
|
|
- String accessToken = accessTokenService.getAccessToken();
|
|
|
|
- String url = GET_HISTORY_DATA_URL
|
|
|
|
- + "?access_token=" + accessToken
|
|
|
|
- + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime;
|
|
|
|
- return httpPoolClientDefault.get(url);
|
|
|
|
|
|
+ private String getGroupMsgSendResult(String msgId, String userId, String cursor) throws IOException {
|
|
|
|
+ String accessToken = accessTokenService.getWeComAccessToken();
|
|
|
|
+ String url = POST_WE_COM_GROUP_MSG_SEND_RESULT
|
|
|
|
+ + "?access_token=" + accessToken;
|
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
|
+ param.put("msgid", msgId);
|
|
|
|
+ param.put("userid", userId);
|
|
|
|
+ param.put("limit", 1000);
|
|
|
|
+ if (StringUtils.isNotEmpty(cursor)) {
|
|
|
|
+ param.put("cursor", cursor);
|
|
|
|
+ }
|
|
|
|
+ return httpPoolClientDefault.post(url, param.toJSONString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- @XxlJob("deleteHistoryMessageJob")
|
|
|
|
- public ReturnT<String> deleteHistoryMessage(String param) {
|
|
|
|
- UserExample userExample = new UserExample();
|
|
|
|
- userExample.createCriteria().andIsDeleteEqualTo(1);
|
|
|
|
- List<User> userList = userMapper.selectByExample(userExample);
|
|
|
|
- for (User user : userList) {
|
|
|
|
- HistoryMessageExample example = new HistoryMessageExample();
|
|
|
|
- example.createCriteria().andUserIdEqualTo(user.getId());
|
|
|
|
- List<HistoryMessage> historyMessages = historyMessageMapper.selectByExample(example);
|
|
|
|
- for (HistoryMessage historyMessage : historyMessages) {
|
|
|
|
- if (historyMessage.getSendTime().getTime() > user.getDeletedAt() * 1000) {
|
|
|
|
- historyMessageMapper.deleteByPrimaryKey(historyMessage.getId());
|
|
|
|
- }
|
|
|
|
|
|
+ private List<String> selectGroupMsgTask(String msgId) throws IOException {
|
|
|
|
+ List<String> resList = new ArrayList<>();
|
|
|
|
+ String cursor = "";
|
|
|
|
+ do {
|
|
|
|
+ String res = getGroupMsgTask(msgId, cursor);
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
|
+ Integer errCode = jsonObject.getInteger("errcode");
|
|
|
|
+ if (errCode != 0) {
|
|
|
|
+ String errmsg = jsonObject.getString("errmsg");
|
|
|
|
+ log.error("selectGroupMsgTask error msgId={} errCode={} errmsg={}", msgId, errCode, errmsg);
|
|
|
|
+ return resList;
|
|
}
|
|
}
|
|
- }
|
|
|
|
- return ReturnT.SUCCESS;
|
|
|
|
|
|
+ JSONArray taskList = jsonObject.getJSONArray("task_list");
|
|
|
|
+ for (int i = 0; i < taskList.size(); i++) {
|
|
|
|
+ JSONObject task = taskList.getJSONObject(i);
|
|
|
|
+ String userId = task.getString("userid");
|
|
|
|
+ resList.add(userId);
|
|
|
|
+ }
|
|
|
|
+ String nextCursor = jsonObject.getString("next_cursor");
|
|
|
|
+ if (cursor.equals(nextCursor)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ cursor = nextCursor;
|
|
|
|
+ } while (StringUtils.isNotEmpty(cursor));
|
|
|
|
+ return resList;
|
|
}
|
|
}
|
|
|
|
|
|
- @XxlJob("sendMessageAlertJob")
|
|
|
|
- public ReturnT<String> sendMessageAlert(String param) {
|
|
|
|
- XxlJobParam xxlJobParam = new XxlJobParam();
|
|
|
|
- if (StringUtils.isNotEmpty(param)) {
|
|
|
|
- xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
|
|
|
|
|
|
+ private String getGroupMsgTask(String msgId, String cursor) throws IOException {
|
|
|
|
+ String accessToken = accessTokenService.getWeComAccessToken();
|
|
|
|
+ String url = POST_WE_COM_GROUP_MSG_TASK
|
|
|
|
+ + "?access_token=" + accessToken;
|
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
|
+ param.put("msgid", msgId);
|
|
|
|
+ param.put("limit", 1000);
|
|
|
|
+ if (StringUtils.isNotEmpty(cursor)) {
|
|
|
|
+ param.put("cursor", cursor);
|
|
}
|
|
}
|
|
- if (xxlJobParam.getStartTime() == null) {
|
|
|
|
- xxlJobParam.setStartTime(TimeUtil.getTodayTimestamp() / 1000);
|
|
|
|
- }
|
|
|
|
- if (xxlJobParam.getEndTime() == null) {
|
|
|
|
- xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
|
|
- }
|
|
|
|
- Long startTime = xxlJobParam.getStartTime();
|
|
|
|
- Long endTime = xxlJobParam.getEndTime();
|
|
|
|
- selectAlertHistoryMessage(startTime, endTime);
|
|
|
|
- return ReturnT.SUCCESS;
|
|
|
|
|
|
+ return httpPoolClientDefault.post(url, param.toJSONString());
|
|
}
|
|
}
|
|
|
|
|
|
- public void selectAlertHistoryMessage(Long startTime, Long endTime) {
|
|
|
|
- try {
|
|
|
|
- Integer total = getHistoryDataTotal(startTime, endTime);
|
|
|
|
- if (total == null || total == 0) {
|
|
|
|
- return;
|
|
|
|
|
|
+ private void delHistoryMessageList(Long startTime, Long endTime) {
|
|
|
|
+ HistoryMessage historyMessage = new HistoryMessage();
|
|
|
|
+ historyMessage.setIsDelete(1);
|
|
|
|
+ HistoryMessageExample example = new HistoryMessageExample();
|
|
|
|
+ example.createCriteria().andSendTimeBetween(new Date(startTime * 1000), new Date(endTime * 1000));
|
|
|
|
+ historyMessageMapper.updateByExample(historyMessage, example);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void insertHistoryMessageList(Staff staff, List<ExternalUser> externalUsers, List<MiniprogramRecord> miniprogramRecordList, Long createTime) {
|
|
|
|
+ if (CollectionUtils.isEmpty(externalUsers) || CollectionUtils.isEmpty(miniprogramRecordList)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Long sendTime = externalUsers.stream().map(ExternalUser::getSendTime).filter(Objects::nonNull).findFirst().orElse(null);
|
|
|
|
+ List<HistoryMessage> historyMessageList = new ArrayList<>();
|
|
|
|
+ for (ExternalUser externalUser : externalUsers) {
|
|
|
|
+ Long userId = userMapper.selectIdByExternalUserId(externalUser.getExternalUserId());
|
|
|
|
+ if (userId == null) {
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- Set<String> filterStatus = new HashSet<String>() {{
|
|
|
|
- add("sended");
|
|
|
|
- add("waiting_confirm");
|
|
|
|
- }};
|
|
|
|
- List<AlertMessage> alertMessageList = new ArrayList<>();
|
|
|
|
- int page = total / size + 1;
|
|
|
|
- for (int n = 0; n < page; n++) {
|
|
|
|
- String res = getHistoryData(size, n * size, startTime, endTime);
|
|
|
|
- log.info("selectAlertHistoryMessage size={}, n={}, startTime={}, endTime={}, res={}", size, n, startTime, endTime, res);
|
|
|
|
- if (ObjectUtils.isEmpty(res)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
|
- JSONArray jsonArray = jsonObject.getJSONArray("missions");
|
|
|
|
- for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
|
- JSONArray attachments = jsonArray.getJSONObject(i).getJSONObject("msg_data").getJSONArray("attachments");
|
|
|
|
- List<Long> videoIds = new ArrayList<>();
|
|
|
|
- for (int j = 0; j < attachments.size(); j++) {
|
|
|
|
- String indexPage = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("page");
|
|
|
|
- Long videoId = MessageUtil.getVideoId(indexPage);
|
|
|
|
- videoIds.add(videoId);
|
|
|
|
- }
|
|
|
|
- JSONArray subMissionList = jsonArray.getJSONObject(i).getJSONArray("sub_mission_list");
|
|
|
|
- for (int k = 0; k < subMissionList.size(); k++) {
|
|
|
|
- if (!filterStatus.contains(subMissionList.getJSONObject(k).getString("status"))) {
|
|
|
|
- Long messageId = jsonArray.getJSONObject(i).getLong("id");
|
|
|
|
- AlertMessageExample example = new AlertMessageExample();
|
|
|
|
- example.createCriteria().andMessageIdEqualTo(messageId);
|
|
|
|
- List<AlertMessage> alertMessages = alertMessageMapper.selectByExample(example);
|
|
|
|
- if (!CollectionUtils.isEmpty(alertMessages)) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- Long sendAt = jsonArray.getJSONObject(i).getLong("send_at");
|
|
|
|
- AlertMessage alertMessage = new AlertMessage();
|
|
|
|
- alertMessage.setMessageId(messageId);
|
|
|
|
- alertMessage.setVideoIds(videoIds.toString());
|
|
|
|
- alertMessage.setSendTime(DateUtil.getDateString(sendAt));
|
|
|
|
- alertMessage.setStatus(subMissionList.getJSONObject(k).getString("status"));
|
|
|
|
- JSONArray staffIdList = jsonArray.getJSONObject(i).getJSONArray("staff_id_list");
|
|
|
|
- for (int s = 0; s < staffIdList.size(); s++) {
|
|
|
|
- AlertMessage newAlertMessage = new AlertMessage();
|
|
|
|
- BeanUtils.copyProperties(alertMessage, newAlertMessage);
|
|
|
|
- StaffExample staffExample = new StaffExample();
|
|
|
|
- staffExample.createCriteria().andStaffExtIdEqualTo(staffIdList.getString(s));
|
|
|
|
- List<Staff> staffList = staffMapper.selectByExample(staffExample);
|
|
|
|
- if (CollectionUtils.isEmpty(staffList)) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- newAlertMessage.setStaffName(staffList.get(0).getRemark());
|
|
|
|
- alertMessageList.add(newAlertMessage);
|
|
|
|
- alertMessageMapper.insert(newAlertMessage);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ for (MiniprogramRecord miniprogramRecord : miniprogramRecordList) {
|
|
|
|
+ Integer status = externalUser.getStatus();
|
|
|
|
+ HistoryMessage historyMessage = new HistoryMessage();
|
|
|
|
+ if (externalUser.getSendTime() != null) {
|
|
|
|
+ historyMessage.setSendTime(new Date(externalUser.getSendTime()));
|
|
|
|
+ } else {
|
|
|
|
+ if (sendTime != null) {
|
|
|
|
+ historyMessage.setSendTime(new Date(sendTime));
|
|
|
|
+ } else if (createTime != null) {
|
|
|
|
+ historyMessage.setSendTime(new Date(createTime));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ historyMessage.setAttachmentIdx(miniprogramRecord.getAttachmentIdx());
|
|
|
|
+ historyMessage.setVideoId(miniprogramRecord.getVideoId());
|
|
|
|
+ historyMessage.setUserId(userId);
|
|
|
|
+ historyMessage.setStaffId(staff.getId());
|
|
|
|
+ historyMessage.setStatus(status);
|
|
|
|
+ historyMessageList.add(historyMessage);
|
|
|
|
+ if (status == 2) {
|
|
|
|
+ userService.delStaffWithUser(userId, staff.getId(), sendTime);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- if (!CollectionUtils.isEmpty(alertMessageList)) {
|
|
|
|
- LarkRobotUtil.sendMessage(JSONArray.toJSONString(alertMessageList));
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- log.error("selectAlertHistoryMessage error", e);
|
|
|
|
}
|
|
}
|
|
|
|
+ historyMessageService.batchInsertHistoryMessage(historyMessageList);
|
|
}
|
|
}
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
+ private String getGroupMsgList(Long startTime, Long endTime, String cursor) throws IOException {
|
|
|
|
+ String accessToken = accessTokenService.getWeComAccessToken();
|
|
|
|
+ String url = POST_WE_COM_GROUP_MSG_LIST_URL
|
|
|
|
+ + "?access_token=" + accessToken;
|
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
|
+ param.put("chat_type", "single");
|
|
|
|
+ param.put("start_time", startTime);
|
|
|
|
+ param.put("end_time", endTime);
|
|
|
|
+ param.put("limit", 100);
|
|
|
|
+ if (StringUtils.isNotEmpty(cursor)) {
|
|
|
|
+ param.put("cursor", cursor);
|
|
|
|
+ }
|
|
|
|
+ return httpPoolClientDefault.post(url, param.toJSONString());
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|