Ver Fonte

删除微伴助手任务

xueyiming há 6 meses atrás
pai
commit
56b59f1cea

+ 0 - 3
we-com-server/src/main/java/com/tzld/piaoquan/wecom/common/constant/RedisConstant.java

@@ -7,9 +7,6 @@ public interface RedisConstant {
 
     String WE_COM_ACCESS_TOKEN = "WE_COM_ACCESS_TOKEN";
 
-    //小程序保底视频列表key
-    String GUARANTEED_MINIPROGRAM_KEY = "guaranteed_miniprogram_list";
-
     //小程序保底视频key
     String GUARANTEED_MINI_PROGRAM_KEY = "guaranteed_mini_program_%s";
 }

+ 252 - 167
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -2,34 +2,41 @@ package com.tzld.piaoquan.wecom.job;
 
 import com.alibaba.fastjson.JSONArray;
 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.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.StaffMapper;
 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.SendDetail;
 import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
 import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.HistoryMessageService;
 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.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 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
 @Component
@@ -37,7 +44,6 @@ public class WeComHistoryDataJob {
 
     private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
 
-    final int size = 20;
 
     @Autowired
     private AccessTokenService accessTokenService;
@@ -55,13 +61,13 @@ public class WeComHistoryDataJob {
     private MessageAttachmentService messageAttachmentService;
 
     @Autowired
-    private AlertMessageMapper alertMessageMapper;
+    private StaffMapper staffMapper;
 
     @Autowired
-    private StaffMapper staffMapper;
+    private UserService userService;
 
 
-    @XxlJob("saveHistoryMessageJob")
+    @XxlJob("saveHistoryMessageJob1")
     public ReturnT<String> selectHistoryMessageByDay(String param) {
         XxlJobParam xxlJobParam = new XxlJobParam();
         if (StringUtils.isNotEmpty(param)) {
@@ -72,7 +78,12 @@ public class WeComHistoryDataJob {
             example.setOrderByClause("create_time desc");
             example.setPage(new Page<>(1, 1));
             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) {
             xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
@@ -81,217 +92,291 @@ public class WeComHistoryDataJob {
         Long endTime = xxlJobParam.getEndTime();
 
         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;
     }
 
-    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;
                 }
-                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<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++) {
+                        JSONObject miniprogram = attachments.getJSONObject(j).getJSONObject("miniprogram");
+                        if (miniprogram == null) {
+                            continue;
+                        }
                         MiniprogramRecord miniprogramRecord = new MiniprogramRecord();
                         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.setAttachmentIdx(j + 1);
                         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.setCover(cover);
-                        messageAttachment.setPage(indexPage);
+                        messageAttachment.setPage(page);
                         messageAttachment.setTitle(title);
                         messageAttachment.setMiniprogramVideoId(videoId);
-                        messageAttachment.setSendTime(sendAt);
                         messageAttachment.setType(MessageAttachmentTypeEnum.MINIPROGRAM.getType());
                         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;
                         }
-                        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;
                         }
-                        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) {
-            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());
+    }
 
 
+}
+
 
 
 

+ 0 - 378
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob1.java

@@ -1,378 +0,0 @@
-package com.tzld.piaoquan.wecom.job;
-
-import com.alibaba.fastjson.JSONArray;
-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.enums.MessageAttachmentTypeEnum;
-import com.tzld.piaoquan.wecom.dao.mapper.*;
-import com.tzld.piaoquan.wecom.model.bo.ExternalUser;
-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.po.*;
-import com.tzld.piaoquan.wecom.service.AccessTokenService;
-import com.tzld.piaoquan.wecom.service.HistoryMessageService;
-import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
-import com.tzld.piaoquan.wecom.service.UserService;
-import com.tzld.piaoquan.wecom.utils.*;
-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 lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.ObjectUtils;
-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.io.IOException;
-import java.util.*;
-
-import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
-
-@Slf4j
-@Component
-public class WeComHistoryDataJob1 {
-
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
-
-    @Autowired
-    private AccessTokenService accessTokenService;
-
-    @Autowired
-    private UserMapper userMapper;
-
-    @Autowired
-    private HistoryMessageMapper historyMessageMapper;
-
-    @Autowired
-    private HistoryMessageService historyMessageService;
-
-    @Autowired
-    private MessageAttachmentService messageAttachmentService;
-
-    @Autowired
-    private StaffMapper staffMapper;
-
-    @Autowired
-    private StaffWithUserMapper staffWithUserMapper;
-
-    @Autowired
-    private UserService userService;
-
-
-    @XxlJob("saveHistoryMessageJob1")
-    public ReturnT<String> selectHistoryMessageByDay(String param) {
-        XxlJobParam xxlJobParam = new XxlJobParam();
-        if (StringUtils.isNotEmpty(param)) {
-            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
-        }
-        if (xxlJobParam.getStartTime() == null) {
-            HistoryMessageExample example = new HistoryMessageExample();
-            example.setOrderByClause("create_time desc");
-            example.setPage(new Page<>(1, 1));
-            List<HistoryMessage> historyMessages = historyMessageMapper.selectByExample(example);
-            if (CollectionUtils.isEmpty(historyMessages)) {
-                xxlJobParam.setStartTime(1721404800L);
-            } else {
-                xxlJobParam.setStartTime(historyMessages.get(0).getCreateTime().getTime() / 1000);
-            }
-
-        }
-        if (xxlJobParam.getEndTime() == null) {
-            xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
-        }
-        Long startTime = xxlJobParam.getStartTime();
-        Long endTime = xxlJobParam.getEndTime();
-
-        for (; startTime < endTime; startTime += TimeConstant.DAY) {
-            selectGroupMsgList(startTime, Math.min(startTime + TimeConstant.DAY, endTime));
-        }
-        selectAlertHistoryMessage(xxlJobParam.getStartTime(), xxlJobParam.getEndTime());
-        return ReturnT.SUCCESS;
-    }
-
-    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;
-            }
-            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;
-                }
-                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<MessageAttachment> messageAttachmentList = new ArrayList<>();
-                    for (int j = 0; j < attachments.size(); j++) {
-                        JSONObject miniprogram = attachments.getJSONObject(j).getJSONObject("miniprogram");
-                        if (miniprogram == null) {
-                            continue;
-                        }
-                        MiniprogramRecord miniprogramRecord = new MiniprogramRecord();
-                        MessageAttachment messageAttachment = new MessageAttachment();
-                        String title = miniprogram.getString("title");
-                        String appid = miniprogram.getString("appid");
-                        String page = miniprogram.getString("page");
-                        Long videoId = MessageUtil.getVideoId(page);
-
-                        miniprogramRecord.setVideoId(videoId);
-                        miniprogramRecord.setAttachmentIdx(j + 1);
-                        miniprogramRecordList.add(miniprogramRecord);
-
-                        messageAttachment.setAppid(appid);
-                        messageAttachment.setPage(page);
-                        messageAttachment.setTitle(title);
-                        messageAttachment.setMiniprogramVideoId(videoId);
-                        messageAttachment.setType(MessageAttachmentTypeEnum.MINIPROGRAM.getType());
-                        messageAttachmentList.add(messageAttachment);
-                    }
-                    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;
-                        }
-                        List<ExternalUser> externalUsers = selectGroupMsgSendResult(msgId, userId);
-                        if (CollectionUtils.isEmpty(externalUsers)) {
-                            continue;
-                        }
-                        insertHistoryMessageList(staffList.get(0), externalUsers, miniprogramRecordList, createTime);
-                        messageAttachmentService.addMiniProgram(messageAttachmentList);
-                    }
-                }
-                String nextCursor = jsonObject.getString("next_cursor");
-                if (cursor.equals(nextCursor)) {
-                    break;
-                }
-                cursor = nextCursor;
-            } while (StringUtils.isNotEmpty(cursor));
-        } catch (IOException e) {
-            LarkRobotUtil.sendMessage("selectGroupMsgList error" + e);
-            log.error("selectGroupMsgList error", e);
-        }
-    }
-
-
-    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;
-            }
-            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);
-            }
-            String nextCursor = jsonObject.getString("next_cursor");
-            if (cursor.equals(nextCursor)) {
-                break;
-            }
-            cursor = nextCursor;
-        } while (StringUtils.isNotEmpty(cursor));
-        return resList;
-    }
-
-    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());
-    }
-
-
-    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;
-            }
-            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;
-    }
-
-    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);
-        }
-        return httpPoolClientDefault.post(url, param.toJSONString());
-    }
-
-    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;
-            }
-            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);
-                }
-            }
-        }
-        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());
-    }
-
-
-}
-
-
-
-

+ 146 - 117
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

@@ -7,33 +7,32 @@ import com.aliyun.odps.data.Record;
 import com.google.common.collect.Lists;
 import com.tzld.piaoquan.wecom.dao.mapper.*;
 import com.tzld.piaoquan.wecom.model.bo.PushMessage;
+import com.tzld.piaoquan.wecom.model.bo.VideoParam;
+import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
 import com.tzld.piaoquan.wecom.model.po.*;
+import com.tzld.piaoquan.wecom.model.vo.GuaranteedParam;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
 import com.tzld.piaoquan.wecom.service.MessageService;
-import com.tzld.piaoquan.wecom.utils.*;
+import com.tzld.piaoquan.wecom.utils.DateUtil;
+import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
+import com.tzld.piaoquan.wecom.utils.OdpsUtil;
+import com.tzld.piaoquan.wecom.utils.ToolUtils;
 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 lombok.extern.log4j.Log4j2;
 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.File;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
 import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Paths;
 import java.util.*;
 import java.util.stream.Collectors;
 
-import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINIPROGRAM_KEY;
+import static com.tzld.piaoquan.wecom.common.constant.MessageConstant.MAX_VIDEO_NUM;
+import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINI_PROGRAM_KEY;
 import static com.tzld.piaoquan.wecom.common.constant.TimeConstant.MILLISECOND_DAY;
 
 @Log4j2
@@ -64,7 +63,6 @@ public class WeComMessageDataJob {
     @Autowired
     SendMessageMapper sendMessageMapper;
 
-    private static final int MAX_VIDEO_NUM = 3;
 
     //发送小程序标题限制字节数
     private static final int MAX_BYTES = 64;
@@ -73,11 +71,7 @@ public class WeComMessageDataJob {
     List<PushMessage> goodHistoryPushList = new ArrayList<>();
 
     //保底视频列表
-    List<Long> guaranteedVideoIdList = new ArrayList<>();
-
-    //从缓存中获取的保底视频数量
-    int getGuaranteedVideoIdNum = 0;
-
+    Map<Long, List<Long>> guaranteedVideoMap = new HashMap<>();
     Map<String, String> pageMap = new HashMap<>();
 
     //初始化操作
@@ -100,49 +94,65 @@ public class WeComMessageDataJob {
         }
         goodHistoryPushList = list;
 
-        getGuaranteedVideoIdNum = 0;
         //保底视频获取
-        List<Long> videoIdList = Objects.requireNonNull(redisTemplate.opsForList().range(GUARANTEED_MINIPROGRAM_KEY, 0, -1))
-                .stream().map(o -> (Integer) o).map(String::valueOf).map(Long::parseLong).collect(Collectors.toList());
-        if (CollectionUtils.isEmpty(videoIdList)) {
-            log.error("推送消息初始化失败,保底数据为空");
-            throw new RuntimeException("保底数据为空");
+
+        String key = String.format(GUARANTEED_MINI_PROGRAM_KEY, DateUtil.getThatDayDateString());
+        GuaranteedParam guaranteedParam = (GuaranteedParam) redisTemplate.opsForValue().get(key);
+        if (guaranteedParam == null
+                || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
+            LarkRobotUtil.sendMessage("保底视频获取异常,请检查" + DateUtil.getThatDayDateString());
+            throw new RuntimeException();
         }
-        List<Long> saveVideoIds = new ArrayList<>();
-        for (Long videoId : videoIdList) {
-            getGuaranteedVideoIdNum++;
-            MessageAttachmentExample example = new MessageAttachmentExample();
-            example.createCriteria().andMiniprogramVideoIdEqualTo(videoId);
-            List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
-            if (CollectionUtils.isEmpty(messageAttachmentList)) {
-                continue;
+        Map<Long, List<Long>> videoMap = new HashMap<>();
+        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
+            if (videoParam.getStaffId() == null) {
+                LarkRobotUtil.sendMessage("保底视频获取异常,StaffId为空" + DateUtil.getThatDayDateString());
+                throw new RuntimeException();
             }
-            MessageAttachment messageAttachment = messageAttachmentList.get(0);
-            if (messageAttachment.getSendTime() != null
-                    && DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 180 * MILLISECOND_DAY) {
-                continue;
+            if (CollectionUtils.isEmpty(videoParam.getVideoIds()) || videoParam.getVideoIds().size() < MAX_VIDEO_NUM) {
+                LarkRobotUtil.sendMessage("保底视频数量异常,请查看" + guaranteedParam.getDate() + videoParam.getStaffId());
+                throw new RuntimeException();
             }
-            saveVideoIds.add(videoId);
-            if (saveVideoIds.size() >= MAX_VIDEO_NUM) {
-                break;
+            for (Long videoId : videoParam.getVideoIds()) {
+                MessageAttachmentExample example = new MessageAttachmentExample();
+                example.createCriteria().andMiniprogramVideoIdEqualTo(videoId);
+                List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
+                if (CollectionUtils.isEmpty(messageAttachmentList)) {
+                    LarkRobotUtil.sendMessage("保底视频不存在,请查看videoId=" + videoId);
+                    throw new RuntimeException();
+                }
+                MessageAttachment messageAttachment = messageAttachmentList.get(0);
+                if (messageAttachment.getSendTime() != null
+                        && DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 180 * MILLISECOND_DAY) {
+                    LarkRobotUtil.sendMessage("保底视频半年内已发送,请查看videoId=" + videoId);
+                    throw new RuntimeException();
+                }
             }
+            videoMap.put(videoParam.getStaffId(), videoParam.getVideoIds());
         }
-        if (saveVideoIds.size() < MAX_VIDEO_NUM) {
-            log.error("推送消息初始化失败,保底数据不足");
-            throw new RuntimeException("保底数据不足");
+        if (!videoMap.containsKey(0L)) {
+            LarkRobotUtil.sendMessage("保底视频没有默认组,请查看" + guaranteedParam.getDate());
+            throw new RuntimeException();
         }
-        guaranteedVideoIdList = saveVideoIds;
+        this.guaranteedVideoMap = videoMap;
     }
 
-    @XxlJob("assembleSendMessageJob")
+    @XxlJob("assembleSendMessageJob1")
     public ReturnT<String> assembleSendMessage(String param) {
+        XxlJobParam xxlJobParam = new XxlJobParam();
+        if (StringUtils.isNotEmpty(param)) {
+            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
+        }
         init();
         Long staffId = null;
-        if (StringUtils.isNotEmpty(param)) {
-            staffId = Long.parseLong(param);
+        if (xxlJobParam != null && xxlJobParam.getStaffId() != null) {
+            staffId = xxlJobParam.getStaffId();
         }
         UserExample example = new UserExample();
-        example.createCriteria().andExternalUserId3rdPartyIsNotNull();
+        example.createCriteria().andExternalUserIdIsNotNull();
+        if (xxlJobParam != null && xxlJobParam.getUserId() != null) {
+            example.createCriteria().andIdEqualTo(xxlJobParam.getUserId());
+        }
         long count = userMapper.countByExample(example);
         int page = 1;
         int pageSize = 1000;
@@ -154,23 +164,36 @@ public class WeComMessageDataJob {
                 continue;
             }
             //落库逻辑
-            List<SendMessage> allSendMessageList = new ArrayList<>();
+            List<SendMessage> allSeneMessageList = new ArrayList<>();
             for (User user : userList) {
                 List<SendMessage> sendMessageList = getSendMessage(user, staffId);
                 if (!CollectionUtils.isEmpty(sendMessageList)) {
-                    allSendMessageList.addAll(sendMessageList);
+                    allSeneMessageList.addAll(sendMessageList);
                 }
             }
-            if (!CollectionUtils.isEmpty(allSendMessageList)) {
-                sendMessageMapper.insertList(allSendMessageList);
+            if (!CollectionUtils.isEmpty(allSeneMessageList)) {
+                sendMessageMapper.insertList(allSeneMessageList);
             }
         }
         //组装好当天要发送的消息后  记录时间  删除保底数据
-        saveGuaranteedVideoIdList(guaranteedVideoIdList);
+        saveGuaranteedVideoIdList();
         return ReturnT.SUCCESS;
     }
 
-    public void saveGuaranteedVideoIdList(List<Long> videoIdList) {
+    public void saveGuaranteedVideoIdList() {
+        String key = String.format(GUARANTEED_MINI_PROGRAM_KEY, DateUtil.getThatDayDateString());
+        GuaranteedParam guaranteedParam = (GuaranteedParam) redisTemplate.opsForValue().get(key);
+        if (guaranteedParam == null || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
+            return;
+        }
+        List<Long> videoIdList = new ArrayList<>();
+        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
+            if (CollectionUtils.isEmpty(videoParam.getVideoIds())) {
+                continue;
+            }
+            videoIdList.addAll(videoParam.getVideoIds());
+        }
+
         MessageAttachmentExample example = new MessageAttachmentExample();
         example.createCriteria().andMiniprogramVideoIdIn(videoIdList);
         List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
@@ -180,78 +203,81 @@ public class WeComMessageDataJob {
             updateMessageAttachment.setSendTime(new Date());
             messageAttachmentMapper.updateByPrimaryKeySelective(updateMessageAttachment);
         }
-        log.info("getGuaranteedVideoIdNum={}", getGuaranteedVideoIdNum);
-        //移除从redis中获取的保底数据
-        for (int i = 0; i < getGuaranteedVideoIdNum; i++) {
-            redisTemplate.opsForList().leftPop(GUARANTEED_MINIPROGRAM_KEY);
-        }
+        redisTemplate.delete(key);
     }
 
     public List<SendMessage> getSendMessage(User user, Long staffId) {
         StaffWithUserExample example = new StaffWithUserExample();
         StaffWithUserExample.Criteria criteria = example.createCriteria();
         criteria.andUserIdEqualTo(user.getId());
+        criteria.andIsDeleteEqualTo(0);
         if (staffId != null) {
-            criteria.andUserIdEqualTo(staffId);
+            criteria.andStaffIdEqualTo(staffId);
         }
         List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(example);
         if (CollectionUtils.isEmpty(staffWithUserList)) {
             return null;
         }
-        int n = 0;
         List<SendMessage> sendMessageList = new ArrayList<>();
-        SendMessage sendMessage = new SendMessage();
-        for (PushMessage pushMessage : goodHistoryPushList) {
-            if (pushMessage.getUserIds().contains(user.getId())) {
-                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) {
-                    break;
+        for (StaffWithUser staffWithUser : staffWithUserList) {
+            int n = 0;
+            SendMessage sendMessage = new SendMessage();
+            for (PushMessage pushMessage : goodHistoryPushList) {
+                if (pushMessage.getUserIds().contains(user.getId())) {
+                    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) {
+                        break;
+                    }
                 }
             }
-        }
-        //保底数据
-        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++;
-                if (n >= MAX_VIDEO_NUM) {
-                    break;
+            //保底数据
+            List<Long> guaranteedVideoIdList = guaranteedVideoMap.get(staffWithUser.getStaffId());
+            if (CollectionUtils.isEmpty(guaranteedVideoIdList)) {
+                guaranteedVideoIdList = guaranteedVideoMap.get(0L);
+            }
+            if (CollectionUtils.isEmpty(guaranteedVideoIdList)) {
+                LarkRobotUtil.sendMessage("组装数据时,保底数据获取异常");
+                throw new RuntimeException();
+            }
+            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++;
+                    if (n >= MAX_VIDEO_NUM) {
+                        break;
+                    }
                 }
             }
-        }
-        if (n < MAX_VIDEO_NUM) {
-            log.error("组装数据失败 user={}", user);
-            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);
+            if (n < MAX_VIDEO_NUM) {
+                LarkRobotUtil.sendMessage("组装数据失败 user=" + user);
+                throw new RuntimeException();
+            }
+            sendMessage.setStaffId(staffWithUser.getStaffId());
+            sendMessage.setUserId(staffWithUser.getUserId());
+            sendMessageList.add(sendMessage);
         }
         return sendMessageList;
     }
 
 
-    @XxlJob("pushSendMessageJob")
+    @XxlJob("pushSendMessageJob1")
     public ReturnT<String> pushSendMessage(String param) {
         List<SendMessage> groupList = sendMessageMapper.getGroupList(DateUtil.getThatDayDate(), 0);
         if (StringUtils.isNotEmpty(param)) {
@@ -263,7 +289,7 @@ public class WeComMessageDataJob {
         for (SendMessage sendMessage : groupList) {
             sendMessage.setIsSend(0);
             sendMessage.setCreateTime(DateUtil.getThatDayDate());
-            List<String> sendUserList = sendMessageMapper.selectExternalUserId3rdParty(sendMessage);
+            List<String> sendUserList = sendMessageMapper.selectExternalUserId(sendMessage);
             boolean flag = pushMessage(sendUserList, sendMessage);
             if (flag) {
                 SendMessage updateSendMessage = new SendMessage();
@@ -282,18 +308,20 @@ public class WeComMessageDataJob {
         return ReturnT.SUCCESS;
     }
 
+
     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(staff.getRemark());
-
         JSONObject jsonObject = new JSONObject();
-        jsonObject.put("name", name);
+        jsonObject.put("chat_type", "single");
+        JSONObject text = new JSONObject();
+        String content = messageService.getMessageText();
+        text.put("content", content);
         jsonObject.put("text", text);
+        jsonObject.put("sender", staff.getCarrierId());
         JSONArray attachments = new JSONArray();
         List<Long> videoIdList = new ArrayList<>();
         videoIdList.add(sendMessage.getVideoId1());
@@ -316,9 +344,14 @@ public class WeComMessageDataJob {
                 title = ToolUtils.truncateString(title, MAX_BYTES - 3) + "...";
             }
             miniprogram.put("title", title);
-            miniprogram.put("cover", messageAttachment.getCover());
+            String picMediaId = messageAttachmentService.getPicMediaId(messageAttachment.getCover());
+            if (StringUtils.isEmpty(picMediaId)) {
+                log.error("pushMessage getPicMediaId error cover={}", messageAttachment.getCover());
+                return false;
+            }
+            miniprogram.put("pic_media_id", picMediaId);
             String page = "";
-            String key = staff.getStaffExtId() + "_" + videoId;
+            String key = staff.getCarrierId() + "_" + videoId;
             if (pageMap.containsKey(key)) {
                 page = pageMap.get(key);
             } else {
@@ -326,24 +359,20 @@ public class WeComMessageDataJob {
                 pageMap.put(key, page);
             }
             if (StringUtils.isEmpty(page)) {
-                throw new RuntimeException("获取page失败");
+                log.error("pushMessage get page error videoId={} staff={}", videoId, staff);
+                return false;
             }
             miniprogram.put("page", page);
-
             attachment.put("miniprogram", miniprogram);
             attachments.add(0, attachment);
         }
         jsonObject.put("attachments", attachments);
         List<List<String>> lists = Lists.partition(sendUserList, 10000);
         for (List<String> list : lists) {
-            List<JSONObject> staffEuList = new ArrayList<>();
+            JSONArray externalUserIds = JSONArray.parseArray(JSON.toJSONString(list));
             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);
+            newJSONObject.put("external_userid", externalUserIds);
             pushList.add(newJSONObject);
         }
         if (CollectionUtils.isEmpty(pushList)) {
@@ -351,7 +380,7 @@ public class WeComMessageDataJob {
         }
         for (JSONObject pushJsonObject : pushList) {
             log.info("pushMessage pushJsonObject={}", pushJsonObject);
-            boolean flag = messageService.pushMessage(pushJsonObject);
+            boolean flag = messageService.pushWeComMessage(pushJsonObject);
             if (!flag) {
                 return flag;
             }

+ 0 - 392
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob1.java

@@ -1,392 +0,0 @@
-package com.tzld.piaoquan.wecom.job;
-
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import com.aliyun.odps.data.Record;
-import com.google.common.collect.Lists;
-import com.tzld.piaoquan.wecom.common.base.CommonResponse;
-import com.tzld.piaoquan.wecom.dao.mapper.*;
-import com.tzld.piaoquan.wecom.model.bo.PushMessage;
-import com.tzld.piaoquan.wecom.model.bo.VideoParam;
-import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
-import com.tzld.piaoquan.wecom.model.po.*;
-import com.tzld.piaoquan.wecom.model.vo.GuaranteedParam;
-import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
-import com.tzld.piaoquan.wecom.service.MessageService;
-import com.tzld.piaoquan.wecom.utils.*;
-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 lombok.extern.log4j.Log4j2;
-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.nio.charset.StandardCharsets;
-import java.util.*;
-import java.util.stream.Collectors;
-
-import static com.tzld.piaoquan.wecom.common.constant.MessageConstant.MAX_VIDEO_NUM;
-import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINIPROGRAM_KEY;
-import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINI_PROGRAM_KEY;
-import static com.tzld.piaoquan.wecom.common.constant.TimeConstant.MILLISECOND_DAY;
-
-@Log4j2
-@Component
-public class WeComMessageDataJob1 {
-
-    @Autowired
-    private UserMapper userMapper;
-
-    @Autowired
-    private MessageAttachmentMapper messageAttachmentMapper;
-
-    @Autowired
-    private RedisTemplate<String, Object> redisTemplate;
-
-    @Autowired
-    private MessageService messageService;
-
-    @Autowired
-    private MessageAttachmentService messageAttachmentService;
-
-    @Autowired
-    private StaffWithUserMapper staffWithUserMapper;
-
-    @Autowired
-    private StaffMapper staffMapper;
-
-    @Autowired
-    SendMessageMapper sendMessageMapper;
-
-
-    //发送小程序标题限制字节数
-    private static final int MAX_BYTES = 64;
-
-    //历史优质视频可推送用户列表
-    List<PushMessage> goodHistoryPushList = new ArrayList<>();
-
-    //保底视频列表
-    Map<Long, List<Long>> guaranteedVideoMap = new HashMap<>();
-    Map<String, String> pageMap = new HashMap<>();
-
-    //初始化操作
-    void init() {
-        //历史优质视频获取
-        String sql = String.format("SELECT * FROM loghubods.history_good_video_can_push_user_list where dt = %s;",
-                DateUtil.getBeforeDayDateString());
-        List<Record> recordList = OdpsUtil.getOdpsData(sql);
-        if (CollectionUtils.isEmpty(recordList)) {
-            return;
-        }
-        List<PushMessage> list = new ArrayList<>();
-        for (Record record : recordList) {
-            PushMessage pushMessage = new PushMessage();
-            Long videoId = Long.parseLong((String) record.get(0));
-            Set<Long> userIds = new HashSet<>(JSONObject.parseArray((String) record.get(1), Long.class));
-            pushMessage.setVideoId(videoId);
-            pushMessage.setUserIds(userIds);
-            list.add(pushMessage);
-        }
-        goodHistoryPushList = list;
-
-        //保底视频获取
-
-        String key = String.format(GUARANTEED_MINI_PROGRAM_KEY, DateUtil.getThatDayDateString());
-        GuaranteedParam guaranteedParam = (GuaranteedParam) redisTemplate.opsForValue().get(key);
-        if (guaranteedParam == null
-                || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
-            LarkRobotUtil.sendMessage("保底视频获取异常,请检查" + DateUtil.getThatDayDateString());
-            throw new RuntimeException();
-        }
-        Map<Long, List<Long>> videoMap = new HashMap<>();
-        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
-            if (videoParam.getStaffId() == null) {
-                LarkRobotUtil.sendMessage("保底视频获取异常,StaffId为空" + DateUtil.getThatDayDateString());
-                throw new RuntimeException();
-            }
-            if (CollectionUtils.isEmpty(videoParam.getVideoIds()) || videoParam.getVideoIds().size() < MAX_VIDEO_NUM) {
-                LarkRobotUtil.sendMessage("保底视频数量异常,请查看" + guaranteedParam.getDate() + videoParam.getStaffId());
-                throw new RuntimeException();
-            }
-            for (Long videoId : videoParam.getVideoIds()) {
-                MessageAttachmentExample example = new MessageAttachmentExample();
-                example.createCriteria().andMiniprogramVideoIdEqualTo(videoId);
-                List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
-                if (CollectionUtils.isEmpty(messageAttachmentList)) {
-                    LarkRobotUtil.sendMessage("保底视频不存在,请查看videoId=" + videoId);
-                    throw new RuntimeException();
-                }
-                MessageAttachment messageAttachment = messageAttachmentList.get(0);
-                if (messageAttachment.getSendTime() != null
-                        && DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 180 * MILLISECOND_DAY) {
-                    LarkRobotUtil.sendMessage("保底视频半年内已发送,请查看videoId=" + videoId);
-                    throw new RuntimeException();
-                }
-            }
-            videoMap.put(videoParam.getStaffId(), videoParam.getVideoIds());
-        }
-        if (!videoMap.containsKey(0L)) {
-            LarkRobotUtil.sendMessage("保底视频没有默认组,请查看" + guaranteedParam.getDate());
-            throw new RuntimeException();
-        }
-        this.guaranteedVideoMap = videoMap;
-    }
-
-    @XxlJob("assembleSendMessageJob1")
-    public ReturnT<String> assembleSendMessage(String param) {
-        XxlJobParam xxlJobParam = new XxlJobParam();
-        if (StringUtils.isNotEmpty(param)) {
-            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
-        }
-        init();
-        Long staffId = null;
-        if (xxlJobParam != null && xxlJobParam.getStaffId() != null) {
-            staffId = xxlJobParam.getStaffId();
-        }
-        UserExample example = new UserExample();
-        example.createCriteria().andExternalUserIdIsNotNull();
-        if (xxlJobParam != null && xxlJobParam.getUserId() != null) {
-            example.createCriteria().andIdEqualTo(xxlJobParam.getUserId());
-        }
-        long count = userMapper.countByExample(example);
-        int page = 1;
-        int pageSize = 1000;
-        long totalPageSize = count / pageSize + 1;
-        for (; page <= totalPageSize; page++) {
-            example.setPage(new Page<>(page, pageSize));
-            List<User> userList = userMapper.selectByExample(example);
-            if (CollectionUtils.isEmpty(userList)) {
-                continue;
-            }
-            //落库逻辑
-            List<SendMessage> allSeneMessageList = new ArrayList<>();
-            for (User user : userList) {
-                List<SendMessage> sendMessageList = getSendMessage(user, staffId);
-                if (!CollectionUtils.isEmpty(sendMessageList)) {
-                    allSeneMessageList.addAll(sendMessageList);
-                }
-            }
-            if (!CollectionUtils.isEmpty(allSeneMessageList)) {
-                sendMessageMapper.insertList(allSeneMessageList);
-            }
-        }
-        //组装好当天要发送的消息后  记录时间  删除保底数据
-        saveGuaranteedVideoIdList();
-        return ReturnT.SUCCESS;
-    }
-
-    public void saveGuaranteedVideoIdList() {
-        String key = String.format(GUARANTEED_MINI_PROGRAM_KEY, DateUtil.getThatDayDateString());
-        GuaranteedParam guaranteedParam = (GuaranteedParam) redisTemplate.opsForValue().get(key);
-        if (guaranteedParam == null || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
-            return;
-        }
-        List<Long> videoIdList = new ArrayList<>();
-        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
-            if (CollectionUtils.isEmpty(videoParam.getVideoIds())) {
-                continue;
-            }
-            videoIdList.addAll(videoParam.getVideoIds());
-        }
-
-        MessageAttachmentExample example = new MessageAttachmentExample();
-        example.createCriteria().andMiniprogramVideoIdIn(videoIdList);
-        List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
-        for (MessageAttachment messageAttachment : messageAttachmentList) {
-            MessageAttachment updateMessageAttachment = new MessageAttachment();
-            updateMessageAttachment.setId(messageAttachment.getId());
-            updateMessageAttachment.setSendTime(new Date());
-            messageAttachmentMapper.updateByPrimaryKeySelective(updateMessageAttachment);
-        }
-        redisTemplate.delete(key);
-    }
-
-    public List<SendMessage> getSendMessage(User user, Long staffId) {
-        StaffWithUserExample example = new StaffWithUserExample();
-        StaffWithUserExample.Criteria criteria = example.createCriteria();
-        criteria.andUserIdEqualTo(user.getId());
-        criteria.andIsDeleteEqualTo(0);
-        if (staffId != null) {
-            criteria.andStaffIdEqualTo(staffId);
-        }
-        List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(example);
-        if (CollectionUtils.isEmpty(staffWithUserList)) {
-            return null;
-        }
-        List<SendMessage> sendMessageList = new ArrayList<>();
-        for (StaffWithUser staffWithUser : staffWithUserList) {
-            int n = 0;
-            SendMessage sendMessage = new SendMessage();
-            for (PushMessage pushMessage : goodHistoryPushList) {
-                if (pushMessage.getUserIds().contains(user.getId())) {
-                    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) {
-                        break;
-                    }
-                }
-            }
-            //保底数据
-            List<Long> guaranteedVideoIdList = guaranteedVideoMap.get(staffWithUser.getStaffId());
-            if (CollectionUtils.isEmpty(guaranteedVideoIdList)) {
-                guaranteedVideoIdList = guaranteedVideoMap.get(0L);
-            }
-            if (CollectionUtils.isEmpty(guaranteedVideoIdList)) {
-                LarkRobotUtil.sendMessage("组装数据时,保底数据获取异常");
-                throw new RuntimeException();
-            }
-            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++;
-                    if (n >= MAX_VIDEO_NUM) {
-                        break;
-                    }
-                }
-            }
-            if (n < MAX_VIDEO_NUM) {
-                LarkRobotUtil.sendMessage("组装数据失败 user=" + user);
-                throw new RuntimeException();
-            }
-            sendMessage.setStaffId(staffWithUser.getStaffId());
-            sendMessage.setUserId(staffWithUser.getUserId());
-            sendMessageList.add(sendMessage);
-        }
-        return sendMessageList;
-    }
-
-
-    @XxlJob("pushSendMessageJob1")
-    public ReturnT<String> pushSendMessage(String param) {
-        List<SendMessage> groupList = sendMessageMapper.getGroupList(DateUtil.getThatDayDate(), 0);
-        if (StringUtils.isNotEmpty(param)) {
-            groupList = groupList.stream().filter(e -> e.getStaffId() == Long.parseLong(param)).collect(Collectors.toList());
-        }
-        if (CollectionUtils.isEmpty(groupList)) {
-            return ReturnT.SUCCESS;
-        }
-        for (SendMessage sendMessage : groupList) {
-            sendMessage.setIsSend(0);
-            sendMessage.setCreateTime(DateUtil.getThatDayDate());
-            List<String> sendUserList = sendMessageMapper.selectExternalUserId(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 ReturnT.SUCCESS;
-    }
-
-
-    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);
-        JSONObject jsonObject = new JSONObject();
-        jsonObject.put("chat_type", "single");
-        JSONObject text = new JSONObject();
-        String content = messageService.getMessageText();
-        text.put("content", content);
-        jsonObject.put("text", text);
-        jsonObject.put("sender", staff.getCarrierId());
-        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());
-            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 = messageAttachmentService.getPicMediaId(messageAttachment.getCover());
-            if (StringUtils.isEmpty(picMediaId)) {
-                log.error("pushMessage getPicMediaId error cover={}", messageAttachment.getCover());
-                return false;
-            }
-            miniprogram.put("pic_media_id", picMediaId);
-            String page = "";
-            String key = staff.getCarrierId() + "_" + videoId;
-            if (pageMap.containsKey(key)) {
-                page = pageMap.get(key);
-            } else {
-                page = messageAttachmentService.getPage(staff, videoId);
-                pageMap.put(key, page);
-            }
-            if (StringUtils.isEmpty(page)) {
-                log.error("pushMessage get page error videoId={} staff={}", videoId, staff);
-                return false;
-            }
-            miniprogram.put("page", page);
-            attachment.put("miniprogram", miniprogram);
-            attachments.add(0, attachment);
-        }
-        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) {
-            log.info("pushMessage pushJsonObject={}", pushJsonObject);
-            boolean flag = messageService.pushWeComMessage(pushJsonObject);
-            if (!flag) {
-                return flag;
-            }
-        }
-        return true;
-    }
-
-
-}

+ 0 - 3
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComStaffDataJob.java

@@ -2,7 +2,6 @@ package com.tzld.piaoquan.wecom.job;
 
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
-import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
 import com.tzld.piaoquan.wecom.model.po.Staff;
 import com.tzld.piaoquan.wecom.model.po.StaffExample;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
@@ -12,7 +11,6 @@ import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
@@ -21,7 +19,6 @@ import java.io.IOException;
 import java.util.List;
 import java.util.stream.Collectors;
 
-import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_WE_COM_EXTERNAL_CONTACT_LIST;
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_WE_COM_FOLLOW_USER_LIST;
 
 @Slf4j

+ 124 - 228
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -2,28 +2,28 @@ package com.tzld.piaoquan.wecom.job;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
 import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
 import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
+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.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.ObjectUtils;
 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.io.IOException;
-import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
 
@@ -33,121 +33,16 @@ public class WeComUserDataJob {
 
     private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
 
-    final int size = 100;
-
+    @Autowired
+    private StaffMapper staffMapper;
     @Autowired
     private UserMapper userMapper;
-
     @Autowired
     private AccessTokenService accessTokenService;
-
-    @Autowired
-    private StaffMapper staffMapper;
-
     @Autowired
     private StaffWithUserMapper staffWithUserMapper;
-
     @Autowired
-    private WeComUserDataJob1 weComUserDataJob1;
-
-    @XxlJob("insertStaffWithUserJob")
-    public ReturnT<String> insertStaffWithUser(String param) {
-        XxlJobParam xxlJobParam = new XxlJobParam();
-        if (StringUtils.isNotEmpty(param)) {
-            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
-        }
-        if (xxlJobParam.getStartTime() == null) {
-            xxlJobParam.setStartTime(1720540800L);
-        }
-        if (xxlJobParam.getEndTime() == null) {
-            xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
-        }
-        StaffExample example = new StaffExample();
-        StaffExample.Criteria criteria = example.createCriteria();
-        if (xxlJobParam.getStaffId() != null) {
-            criteria.andIdEqualTo(xxlJobParam.getStaffId());
-        }
-        List<Staff> staffList = staffMapper.selectByExample(example);
-        for (Staff staff : staffList) {
-            insertAllUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime(), staff);
-        }
-        return ReturnT.SUCCESS;
-    }
-
-    //初始化用户使用此任务
-    private void insertAllUser(long startTime, long endTime, Staff staff) {
-        try {
-            Integer total = getUserTotal(startTime, endTime, staff.getStaffExtId());
-            if (total == null || total == 0) {
-                return;
-            }
-            int page = total / size + 1;
-            int sum = 0;
-            for (int i = 0; i < page; i++) {
-                String res = getUser(size, i * size, startTime, endTime, staff.getStaffExtId());
-                log.info("insertAllUser size={}, i={}, staffExtId = {}, startTime={}, endTime={}, res={}", size, i, staff.getStaffExtId(), startTime, endTime, res);
-                if (ObjectUtils.isEmpty(res)) {
-                    continue;
-                }
-                JSONObject jsonObject = JSONObject.parseObject(res);
-                JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
-                List<StaffWithUser> insertStaffWithUserList = new ArrayList<>();
-                for (int j = 0; j < jsonArray.size(); j++) {
-                    String externalUserId3rdParty = (String) jsonArray.getJSONObject(j).get("id");
-                    Long userId = userMapper.selectIdByExternalUserId3rdParty(externalUserId3rdParty);
-                    if (userId == null) {
-                        jsonArray.getJSONObject(j).put("id", null);
-                        User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
-                        user.setExternalUserId3rdParty(externalUserId3rdParty);
-                        userMapper.insert(user);
-                        userId = user.getId();
-                    }
-                    if (userId == null) {
-                        System.out.println("插入数据异常:" + jsonArray.getJSONObject(j));
-                        continue;
-                    }
-                    StaffWithUserExample staffWithUserExample = new StaffWithUserExample();
-                    staffWithUserExample.createCriteria().andStaffIdEqualTo(staff.getId()).andUserIdEqualTo(userId);
-                    List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample);
-                    if (CollectionUtils.isEmpty(staffWithUserList)) {
-                        StaffWithUser staffWithUser = new StaffWithUser();
-                        staffWithUser.setUserId(userId);
-                        staffWithUser.setStaffId(staff.getId());
-                        insertStaffWithUserList.add(staffWithUser);
-                    }
-                    sum++;
-                }
-                if (!CollectionUtils.isEmpty(insertStaffWithUserList)) {
-                    staffWithUserMapper.insertList(insertStaffWithUserList);
-                }
-                if (jsonArray.size() < size) {
-                    break;
-                }
-            }
-            if (total > sum) {
-                log.error("insertAllUser插入数量不足 total = {}, sum={}", total, sum);
-            }
-        } catch (Exception e) {
-            log.error("insertAllUser error", e);
-        }
-    }
-
-    private Integer getUserTotal(Long startTime, Long endTime, String staffId) throws IOException {
-        String res = getUser(1, 0, startTime, endTime, staffId);
-        JSONObject jsonObject = JSONObject.parseObject(res);
-        return jsonObject.getInteger("total");
-    }
-
-    private String getUser(Integer limit, Integer offset, Long startTime, Long endTime, String staffExtId) throws IOException {
-        String accessToken = accessTokenService.getAccessToken();
-        String url = GET_USER_URL
-                + "?access_token=" + accessToken
-                + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime;
-        if (StringUtils.isNotEmpty(staffExtId)) {
-            url = url + "&staff_id=" + staffExtId;
-        }
-        return httpPoolClientDefault.get(url);
-    }
+    private UserService userService;
 
     @XxlJob("updateStaffWithUserJob")
     public ReturnT<String> updateStaffWithUser(String param) {
@@ -155,16 +50,6 @@ public class WeComUserDataJob {
         if (StringUtils.isNotEmpty(param)) {
             xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
         }
-        if (xxlJobParam.getStartTime() == null) {
-            UserExample userExample = new UserExample();
-            userExample.setOrderByClause("create_time desc");
-            userExample.setPage(new Page<>(1, 1));
-            List<User> userList = userMapper.selectByExample(userExample);
-            xxlJobParam.setStartTime(userList.get(0).getCreateTime().getTime() / 1000 - 2L * TimeConstant.HOUR);
-        }
-        if (xxlJobParam.getEndTime() == null) {
-            xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
-        }
         StaffExample example = new StaffExample();
         StaffExample.Criteria criteria = example.createCriteria();
         if (xxlJobParam.getStaffId() != null) {
@@ -172,46 +57,52 @@ public class WeComUserDataJob {
         }
         List<Staff> staffList = staffMapper.selectByExample(example);
         for (Staff staff : staffList) {
-            updateUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime(), staff);
+            updateUserList(staff);
         }
         return ReturnT.SUCCESS;
     }
 
 
-    private void updateUser(long startTime, long endTime, Staff staff) {
+    private void updateUserList(Staff staff) {
         try {
-            Integer total = getUpdateUserTotal(startTime, endTime, staff.getStaffExtId());
-            if (total == null || total == 0) {
-                return;
-            }
-            int page = total / size + 1;
-            int sum = 0;
-            for (int i = 0; i < page; i++) {
-                String res = getUpdateUser(size, i * size, startTime, endTime, staff.getStaffExtId());
-                log.info("updateUser size={}, i={}, staffExtId = {}, startTime={}, endTime={}, res={}",
-                        size, i, staff.getStaffExtId(), startTime, endTime, res);
-                if (ObjectUtils.isEmpty(res)) {
-                    continue;
-                }
+            String cursor = "";
+            do {
+                String res = getUserDetailList(staff.getCarrierId(), cursor);
+                log.info("updateUserList res={} cursor={}", res, cursor);
                 JSONObject jsonObject = JSONObject.parseObject(res);
-                JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
-                for (int j = 0; j < jsonArray.size(); j++) {
-                    String id = (String) jsonArray.getJSONObject(j).get("id");
-                    jsonArray.getJSONObject(j).put("id", null);
-                    User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
-                    user.setExternalUserId3rdParty(id);
+                Integer errCode = jsonObject.getInteger("errcode");
+                if (errCode != 0) {
+                    log.error("updateUserList error carrierId={} cursor={}", staff.getCarrierId(), cursor);
+                    return;
+                }
+                JSONArray externalContactList = jsonObject.getJSONArray("external_contact_list");
+                for (int i = 0; i < externalContactList.size(); i++) {
+                    JSONObject externalContact = externalContactList.getJSONObject(i).getJSONObject("external_contact");
+                    JSONObject followInfo = externalContactList.getJSONObject(i).getJSONObject("follow_info");
+                    Long createAt = followInfo.getLong("createtime");
+                    String externalUserId = externalContact.getString("external_userid");
+                    String name = externalContact.getString("name");
+                    String unionId = externalContact.getString("unionid");
+                    String avatar = externalContact.getString("avatar");
+                    Integer type = externalContact.getInteger("type");
+                    Integer gender = externalContact.getInteger("gender");
                     UserExample example = new UserExample();
-                    example.createCriteria().andExternalUserId3rdPartyEqualTo(user.getExternalUserId3rdParty());
-                    List<User> list = userMapper.selectByExample(example);
+                    example.createCriteria().andExternalUserIdEqualTo(externalUserId);
+                    List<User> userList = userMapper.selectByExample(example);
+                    User user = new User();
+                    user.setExternalUserId(externalUserId);
+                    user.setName(name);
+                    user.setType(type);
+                    user.setUnionId(unionId);
+                    user.setGender(gender);
+                    user.setAvatar(avatar);
+                    user.setCreatedAt(createAt);
                     Long userId;
-                    if (CollectionUtils.isEmpty(list)) {
-                        //没有用户,走插入逻辑
-                        String externalUserId = weComUserDataJob1.getExternalUserId(user.getExternalUserId3rdParty());
-                        user.setExternalUserId(externalUserId);
+                    if (CollectionUtils.isEmpty(userList)) {
                         userMapper.insert(user);
                         userId = user.getId();
                     } else {
-                        User oldUser = list.get(0);
+                        User oldUser = userList.get(0);
                         user.setId(oldUser.getId());
                         userMapper.updateByPrimaryKeySelective(user);
                         userId = oldUser.getId();
@@ -228,115 +119,120 @@ public class WeComUserDataJob {
                         staffWithUser.setUserId(userId);
                         staffWithUserMapper.insert(staffWithUser);
                     }
-                    sum++;
                 }
-                if (jsonArray.size() < size) {
+                String nextCursor = jsonObject.getString("next_cursor");
+                if (cursor.equals(nextCursor)) {
                     break;
                 }
-            }
-
-            if (total > sum) {
-                log.error("updateUser插入数量不足 total = {}, sum={}", total, sum);
-            }
+                cursor = nextCursor;
+            } while (StringUtils.isNotEmpty(cursor));
         } catch (IOException e) {
+            LarkRobotUtil.sendMessage("updateUser error" + e);
             log.error("updateUser error", e);
         }
     }
 
-    private Integer getUpdateUserTotal(Long startTime, Long endTime, String staffExtId) throws IOException {
-        String res = getUpdateUser(1, 0, startTime, endTime, staffExtId);
-        JSONObject jsonObject = JSONObject.parseObject(res);
-        return jsonObject.getInteger("total");
+    private String getUserDetailList(String userId, String cursor) throws IOException {
+        String accessToken = accessTokenService.getWeComAccessToken();
+        String url = POST_WE_COM_GET_BY_USER
+                + "?access_token=" + accessToken;
+        JSONObject param = new JSONObject();
+        JSONArray userIdList = new JSONArray();
+        userIdList.add(userId);
+        param.put("userid_list", userIdList);
+        param.put("limit", 100);
+        if (StringUtils.isNotEmpty(cursor)) {
+            param.put("cursor", cursor);
+        }
+        return httpPoolClientDefault.post(url, param.toJSONString());
     }
 
-    private String getUpdateUser(Integer limit, Integer offset, Long startTime, Long endTime, String staffExtId) throws IOException {
-        String accessToken = accessTokenService.getAccessToken();
-        String url = UPDATE_USER_URL
-                + "?access_token=" + accessToken
-                + "&limit=" + limit + "&offset=" + offset + "&start_update_time=" + startTime + "&end_update_time=" + endTime
-                + "&source=external_user";
-        if (StringUtils.isNotEmpty(staffExtId)) {
-            url = url + "&staff_id=" + staffExtId;
+    @XxlJob("insertStaffWithUserJob")
+    public ReturnT<String> insertStaffWithUserJob(String param) {
+        try {
+            StaffExample example = new StaffExample();
+            List<Staff> staffList = staffMapper.selectByExample(example);
+            for (Staff staff : staffList) {
+                List<String> externalUserIds = getUserList(staff.getCarrierId());
+                if (CollectionUtils.isEmpty(externalUserIds)) {
+                    continue;
+                }
+                for (String externalUserId : externalUserIds) {
+                    userService.insertStaffWithUser(externalUserId, staff);
+                }
+            }
+        } catch (Exception e) {
+            LarkRobotUtil.sendMessage("insertStaffWithUserJob error" + e);
+            log.error("insertStaffWithUserJob error", e);
         }
-        return httpPoolClientDefault.get(url);
-    }
 
+        return ReturnT.SUCCESS;
 
-    @XxlJob("deleteUserJob")
-    public ReturnT<String> deleteUserJob(String param) {
-        XxlJobParam xxlJobParam = new XxlJobParam();
-        if (StringUtils.isNotEmpty(param)) {
-            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
-        }
-        if (xxlJobParam.getStartTime() == null) {
-            UserExample userExample = new UserExample();
-            userExample.createCriteria().andIsDeleteEqualTo(1);
-            userExample.setOrderByClause("deleted_at desc");
-            userExample.setPage(new Page<>(1, 1));
-            List<User> userList = userMapper.selectByExample(userExample);
-            if (CollectionUtils.isEmpty(userList)) {
-                xxlJobParam.setStartTime(1720540800L);
-            } else {
-                xxlJobParam.setStartTime(userList.get(0).getDeletedAt());
-            }
-        }
-        if (xxlJobParam.getEndTime() == null) {
-            xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
+    }
+
+    public List<String> getUserList(String userId) throws IOException {
+        String weComAccessToken = accessTokenService.getWeComAccessToken();
+        String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_LIST + "?access_token=%s&userid=%s", weComAccessToken, userId);
+        String res = httpPoolClientDefault.get(url);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        Integer errcode = jsonObject.getInteger("errcode");
+        if (errcode == 0) {
+            return jsonObject.getJSONArray("external_userid").stream().map(String::valueOf).collect(Collectors.toList());
         }
-        deleteUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime());
-        return ReturnT.SUCCESS;
+        return null;
     }
 
-    //查询删除用户并更新
-    private void deleteUser(long startTime, long endTime) {
+
+    @XxlJob("setExternalUserIdJob")
+    public ReturnT<String> setExternalUserId(String param) {
         try {
-            Integer total = getDeleteUserTotal(startTime, endTime);
-            if (total == null || total == 0) {
-                return;
+            UserExample example = new UserExample();
+            if (StringUtils.isNotEmpty(param)) {
+                example.createCriteria().andIdGreaterThanOrEqualTo(Long.parseLong(param));
             }
-            int page = total / size + 1;
-            int sum = 0;
-            for (int i = 0; i < page; i++) {
-                String res = getDeleteUser(size, i * size, startTime, endTime);
-                log.info("deleteUser size={}, i={}, startTime={}, endTime={}, res={}", size, i, startTime, endTime, res);
-                if (ObjectUtils.isEmpty(res)) {
+            long count = userMapper.countByExample(example);
+            int page = 1;
+            int pageSize = 1000;
+            long totalPageSize = count / pageSize + 1;
+            for (; page <= totalPageSize; page++) {
+                example.setPage(new Page<>(page, pageSize));
+                List<User> userList = userMapper.selectByExample(example);
+                if (CollectionUtils.isEmpty(userList)) {
                     continue;
                 }
-                JSONObject jsonObject = JSONObject.parseObject(res);
-                JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
-                for (int j = 0; j < jsonArray.size(); j++) {
-                    JSONObject staffRelation = jsonArray.getJSONObject(j).getJSONObject("staff_relation");
-                    Long deletedAt = staffRelation.getLong("deleted_at");
-                    String externalUserId3rdParty = staffRelation.getString("external_user_ext_id");
-                    Long userId = userMapper.selectIdByExternalUserId3rdParty(externalUserId3rdParty);
+                for (User user : userList) {
+                    String externalUserId3rdParty = user.getExternalUserId3rdParty();
+                    String externalUserId = getExternalUserId(externalUserId3rdParty);
+                    if (StringUtils.isEmpty(externalUserId)) {
+                        continue;
+                    }
                     User updateUser = new User();
-                    updateUser.setId(userId);
-                    updateUser.setIsDelete(1);
-                    updateUser.setDeletedAt(deletedAt);
+                    updateUser.setId(user.getId());
+                    updateUser.setExternalUserId(externalUserId);
                     userMapper.updateByPrimaryKeySelective(updateUser);
-                    sum++;
                 }
             }
-            if (total > sum) {
-                log.error("deleteUser数量不足 total = {}, sum={}", total, sum);
-            }
         } catch (Exception e) {
-            log.error("deleteUser error", e);
+            log.error("setExternalUserId error", e);
         }
+
+        return ReturnT.SUCCESS;
     }
 
-    private Integer getDeleteUserTotal(Long startTime, Long endTime) throws IOException {
-        String res = getDeleteUser(1, 0, startTime, endTime);
+    public String getExternalUserId(String externalUserId3rdParty) throws IOException {
+        String weComAccessToken = accessTokenService.getWeComAccessToken();
+        String url = String.format(POST_WE_COM_EXTERNAL_USER_ID + "?access_token=%s", weComAccessToken);
+        JSONObject param = new JSONObject();
+        param.put("external_userid", externalUserId3rdParty);
+        param.put("source_agentid", 1000009);
+        String res = httpPoolClientDefault.post(url, param.toJSONString());
         JSONObject jsonObject = JSONObject.parseObject(res);
-        return jsonObject.getInteger("total_count");
+        Integer errcode = jsonObject.getInteger("errcode");
+        if (errcode == 0) {
+            return jsonObject.getString("external_userid");
+        }
+        return null;
     }
 
-    private String getDeleteUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
-        String accessToken = accessTokenService.getAccessToken();
-        String url = GET_DELETE_USER_URL
-                + "?access_token=" + accessToken
-                + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime;
-        return httpPoolClientDefault.get(url);
-    }
 
 }

+ 0 - 239
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob1.java

@@ -1,239 +0,0 @@
-package com.tzld.piaoquan.wecom.job;
-
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
-import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
-import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
-import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
-import com.tzld.piaoquan.wecom.model.po.*;
-import com.tzld.piaoquan.wecom.service.AccessTokenService;
-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.page.Page;
-import com.xxl.job.core.biz.model.ReturnT;
-import com.xxl.job.core.handler.annotation.XxlJob;
-import lombok.extern.slf4j.Slf4j;
-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.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
-
-@Slf4j
-@Component
-public class WeComUserDataJob1 {
-
-    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
-
-    @Autowired
-    private StaffMapper staffMapper;
-    @Autowired
-    private UserMapper userMapper;
-    @Autowired
-    private AccessTokenService accessTokenService;
-    @Autowired
-    private StaffWithUserMapper staffWithUserMapper;
-    @Autowired
-    private UserService userService;
-
-    @XxlJob("updateStaffWithUserJob1")
-    public ReturnT<String> updateStaffWithUser(String param) {
-        XxlJobParam xxlJobParam = new XxlJobParam();
-        if (StringUtils.isNotEmpty(param)) {
-            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
-        }
-        StaffExample example = new StaffExample();
-        StaffExample.Criteria criteria = example.createCriteria();
-        if (xxlJobParam.getStaffId() != null) {
-            criteria.andIdEqualTo(xxlJobParam.getStaffId());
-        }
-        List<Staff> staffList = staffMapper.selectByExample(example);
-        for (Staff staff : staffList) {
-            updateUserList(staff);
-        }
-        return ReturnT.SUCCESS;
-    }
-
-
-    private void updateUserList(Staff staff) {
-        try {
-            String cursor = "";
-            do {
-                String res = getUserDetailList(staff.getCarrierId(), cursor);
-                log.info("updateUserList res={} cursor={}", res, cursor);
-                JSONObject jsonObject = JSONObject.parseObject(res);
-                Integer errCode = jsonObject.getInteger("errcode");
-                if (errCode != 0) {
-                    log.error("updateUserList error carrierId={} cursor={}", staff.getCarrierId(), cursor);
-                    return;
-                }
-                JSONArray externalContactList = jsonObject.getJSONArray("external_contact_list");
-                for (int i = 0; i < externalContactList.size(); i++) {
-                    JSONObject externalContact = externalContactList.getJSONObject(i).getJSONObject("external_contact");
-                    JSONObject followInfo = externalContactList.getJSONObject(i).getJSONObject("follow_info");
-                    Long createAt = followInfo.getLong("createtime");
-                    String externalUserId = externalContact.getString("external_userid");
-                    String name = externalContact.getString("name");
-                    String unionId = externalContact.getString("unionid");
-                    String avatar = externalContact.getString("avatar");
-                    Integer type = externalContact.getInteger("type");
-                    Integer gender = externalContact.getInteger("gender");
-                    UserExample example = new UserExample();
-                    example.createCriteria().andExternalUserIdEqualTo(externalUserId);
-                    List<User> userList = userMapper.selectByExample(example);
-                    User user = new User();
-                    user.setExternalUserId(externalUserId);
-                    user.setName(name);
-                    user.setType(type);
-                    user.setUnionId(unionId);
-                    user.setGender(gender);
-                    user.setAvatar(avatar);
-                    user.setCreatedAt(createAt);
-                    Long userId;
-                    if (CollectionUtils.isEmpty(userList)) {
-                        userMapper.insert(user);
-                        userId = user.getId();
-                    } else {
-                        User oldUser = userList.get(0);
-                        user.setId(oldUser.getId());
-                        userMapper.updateByPrimaryKeySelective(user);
-                        userId = oldUser.getId();
-                    }
-                    if (userId == null) {
-                        continue;
-                    }
-                    StaffWithUserExample staffWithUserExample = new StaffWithUserExample();
-                    staffWithUserExample.createCriteria().andStaffIdEqualTo(staff.getId()).andUserIdEqualTo(userId);
-                    List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample);
-                    if (CollectionUtils.isEmpty(staffWithUserList)) {
-                        StaffWithUser staffWithUser = new StaffWithUser();
-                        staffWithUser.setStaffId(staff.getId());
-                        staffWithUser.setUserId(userId);
-                        staffWithUserMapper.insert(staffWithUser);
-                    }
-                }
-                String nextCursor = jsonObject.getString("next_cursor");
-                if (cursor.equals(nextCursor)) {
-                    break;
-                }
-                cursor = nextCursor;
-            } while (StringUtils.isNotEmpty(cursor));
-        } catch (IOException e) {
-            LarkRobotUtil.sendMessage("updateUser error" + e);
-            log.error("updateUser error", e);
-        }
-    }
-
-    private String getUserDetailList(String userId, String cursor) throws IOException {
-        String accessToken = accessTokenService.getWeComAccessToken();
-        String url = POST_WE_COM_GET_BY_USER
-                + "?access_token=" + accessToken;
-        JSONObject param = new JSONObject();
-        JSONArray userIdList = new JSONArray();
-        userIdList.add(userId);
-        param.put("userid_list", userIdList);
-        param.put("limit", 100);
-        if (StringUtils.isNotEmpty(cursor)) {
-            param.put("cursor", cursor);
-        }
-        return httpPoolClientDefault.post(url, param.toJSONString());
-    }
-
-
-    @XxlJob("setExternalUserIdJob")
-    public ReturnT<String> setExternalUserId(String param) {
-        try {
-            UserExample example = new UserExample();
-            if (StringUtils.isNotEmpty(param)) {
-                example.createCriteria().andIdGreaterThanOrEqualTo(Long.parseLong(param));
-            }
-            long count = userMapper.countByExample(example);
-            int page = 1;
-            int pageSize = 1000;
-            long totalPageSize = count / pageSize + 1;
-            for (; page <= totalPageSize; page++) {
-                example.setPage(new Page<>(page, pageSize));
-                List<User> userList = userMapper.selectByExample(example);
-                if (CollectionUtils.isEmpty(userList)) {
-                    continue;
-                }
-                for (User user : userList) {
-                    String externalUserId3rdParty = user.getExternalUserId3rdParty();
-                    String externalUserId = getExternalUserId(externalUserId3rdParty);
-                    if (StringUtils.isEmpty(externalUserId)) {
-                        continue;
-                    }
-                    User updateUser = new User();
-                    updateUser.setId(user.getId());
-                    updateUser.setExternalUserId(externalUserId);
-                    userMapper.updateByPrimaryKeySelective(updateUser);
-                }
-            }
-        } catch (Exception e) {
-            log.error("setExternalUserId error", e);
-        }
-
-        return ReturnT.SUCCESS;
-    }
-
-    public String getExternalUserId(String externalUserId3rdParty) throws IOException {
-        String weComAccessToken = accessTokenService.getWeComAccessToken();
-        String url = String.format(POST_WE_COM_EXTERNAL_USER_ID + "?access_token=%s", weComAccessToken);
-        JSONObject param = new JSONObject();
-        param.put("external_userid", externalUserId3rdParty);
-        param.put("source_agentid", 1000009);
-        String res = httpPoolClientDefault.post(url, param.toJSONString());
-        JSONObject jsonObject = JSONObject.parseObject(res);
-        Integer errcode = jsonObject.getInteger("errcode");
-        if (errcode == 0) {
-            return jsonObject.getString("external_userid");
-        }
-        return null;
-    }
-
-    @XxlJob("insertStaffWithUserJob1")
-    public ReturnT<String> insertStaffWithUserJob(String param) {
-        try {
-            StaffExample example = new StaffExample();
-            List<Staff> staffList = staffMapper.selectByExample(example);
-            for (Staff staff : staffList) {
-                List<String> externalUserIds = getUserList(staff.getCarrierId());
-                if (CollectionUtils.isEmpty(externalUserIds)) {
-                    continue;
-                }
-                for (String externalUserId : externalUserIds) {
-                    userService.insertStaffWithUser(externalUserId, staff);
-                }
-            }
-        } catch (Exception e) {
-            LarkRobotUtil.sendMessage("insertStaffWithUserJob error" + e);
-            log.error("insertStaffWithUserJob error", e);
-        }
-
-        return ReturnT.SUCCESS;
-
-    }
-
-    public List<String> getUserList(String userId) throws IOException {
-        String weComAccessToken = accessTokenService.getWeComAccessToken();
-        String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_LIST + "?access_token=%s&userid=%s", weComAccessToken, userId);
-        String res = httpPoolClientDefault.get(url);
-        JSONObject jsonObject = JSONObject.parseObject(res);
-        Integer errcode = jsonObject.getInteger("errcode");
-        if (errcode == 0) {
-            return jsonObject.getJSONArray("external_userid").stream().map(String::valueOf).collect(Collectors.toList());
-        }
-        return null;
-    }
-
-
-}

+ 0 - 4
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/MessageAttachmentServiceImpl.java

@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.common.base.CommonResponse;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
-import com.tzld.piaoquan.wecom.common.exception.CustomizeException;
 import com.tzld.piaoquan.wecom.dao.mapper.MessageAttachmentMapper;
 import com.tzld.piaoquan.wecom.model.bo.AdPutFlowParam;
 import com.tzld.piaoquan.wecom.model.bo.VideoDetail;
@@ -33,7 +32,6 @@ import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.nio.file.Files;
-import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
@@ -42,10 +40,8 @@ import static com.tzld.piaoquan.wecom.common.constant.MessageConstant.MAX_VIDEO_
 import static com.tzld.piaoquan.wecom.common.constant.MessageConstant.appid;
 import static com.tzld.piaoquan.wecom.common.constant.OtherServerURL.POST_ADD_TENCENT;
 import static com.tzld.piaoquan.wecom.common.constant.OtherServerURL.POST_VIDEO_DETAIL_URL;
-import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINIPROGRAM_KEY;
 import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINI_PROGRAM_KEY;
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.POST_WE_COM_MEDIA_UPLOAD;
-import static com.tzld.piaoquan.wecom.common.enums.ExceptionCodeEnum.PARAMS_ERROR;
 
 
 @Slf4j