瀏覽代碼

Merge branch 'dev-xym-update-user' of Server/we-com-manage into master

xueyiming 7 月之前
父節點
當前提交
8417386fe3

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

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

+ 2 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/dao/mapper/StaffWithUserMapper.java

@@ -31,4 +31,6 @@ public interface StaffWithUserMapper {
     int updateByPrimaryKey(StaffWithUser record);
 
     void insertList(@Param("list") List<StaffWithUser> list);
+
+    List<String> selectExternalUserIdByStaffId(@Param("staffId")Long staffId);
 }

+ 46 - 34
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -5,18 +5,13 @@ 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.common.enums.SourceEnum;
 import com.tzld.piaoquan.wecom.component.HttpPoolClient;
-import com.tzld.piaoquan.wecom.dao.mapper.HistoryMessageMapper;
-import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
-import com.tzld.piaoquan.wecom.dao.mapper.CorpMapper;
-import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
+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.model.vo.GuaranteedParam;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.HistoryMessageService;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
@@ -39,8 +34,6 @@ 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_MINI_PROGRAM_KEY;
-import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINI_PROGRAM_SET_KEY;
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.*;
 import static com.tzld.piaoquan.wecom.common.enums.SourceEnum.HISTORICAL_TOP;
 import static com.tzld.piaoquan.wecom.common.enums.SourceEnum.MANUAL;
@@ -77,7 +70,7 @@ public class WeComHistoryDataJob {
     private CorpMapper corpMapper;
 
     @Autowired
-    private RedisTemplate<String, Object> redisTemplate;
+    private MessageAttachmentMapper messageAttachmentMapper;
 
 
     @XxlJob("saveHistoryMessageJob")
@@ -113,14 +106,12 @@ public class WeComHistoryDataJob {
         }
         for (Corp corp : corps) {
             for (; startTime < endTime; startTime += TimeConstant.DAY) {
-                selectGroupMsgList(startTime, Math.min(startTime + TimeConstant.DAY, endTime), corp.getId());
+                selectGroupMsgList(startTime, Math.min(startTime + TimeConstant.DAY, endTime), corp.getId(), xxlJobParam.getStaffId());
             }
         }
         for (Corp corp : corps) {
             selectAlertHistoryMessage(xxlJobParam.getStartTime(), xxlJobParam.getEndTime(), corp.getId(), corp.getName());
         }
-        String key = String.format(GUARANTEED_MINI_PROGRAM_SET_KEY, DateUtil.getThatDayDateString());
-        redisTemplate.delete(key);
         return ReturnT.SUCCESS;
     }
 
@@ -165,8 +156,8 @@ public class WeComHistoryDataJob {
         LarkRobotUtil.sendMessage(stringBuilder.toString());
     }
 
-    private void selectGroupMsgList(Long startTime, Long endTime, Long corpId) {
-        delHistoryMessageList(startTime, endTime);
+    private void selectGroupMsgList(Long startTime, Long endTime, Long corpId, Long staffId) {
+        delHistoryMessageList(startTime, endTime, corpId, staffId);
         try {
             String cursor = "";
             do {
@@ -218,24 +209,33 @@ public class WeComHistoryDataJob {
                         messageAttachment.setType(MessageAttachmentTypeEnum.MINI_PROGRAM.getType());
                         messageAttachmentList.add(messageAttachment);
                     }
-                    List<String> userIdList = selectGroupMsgTask(msgId, corpId);
-                    if (CollectionUtils.isEmpty(userIdList)) {
+                    List<String> carrierIdList = selectGroupMsgTask(msgId, corpId);
+                    if (CollectionUtils.isEmpty(carrierIdList)) {
                         continue;
                     }
-                    for (String userId : userIdList) {
+                    for (String carrierId : carrierIdList) {
                         StaffExample example = new StaffExample();
-                        example.createCriteria().andCarrierIdEqualTo(userId);
+                        example.createCriteria().andCarrierIdEqualTo(carrierId);
                         List<Staff> staffList = staffMapper.selectByExample(example);
                         if (CollectionUtils.isEmpty(staffList)) {
-                            LarkRobotUtil.sendMessage("企微推送报警:userId不存在请检查 " + userId);
+                            LarkRobotUtil.sendMessage("企微推送报警:carrierId不存在请检查 " + carrierId);
                             continue;
                         }
-                        List<ExternalUser> externalUsers = selectGroupMsgSendResult(msgId, userId, corpId);
+                        //指定id以后  过滤其他id
+                        if (staffId != null) {
+                            if (!Objects.equals(staffList.get(0).getId(), staffId)) {
+                                continue;
+                            }
+                        }
+                        List<ExternalUser> externalUsers = selectGroupMsgSendResult(msgId, carrierId, corpId);
                         if (CollectionUtils.isEmpty(externalUsers)) {
                             continue;
                         }
-                        insertHistoryMessageList(staffList.get(0), externalUsers, miniprogramRecordList, createTime, corpId);
-                        messageAttachmentService.addMiniProgram(messageAttachmentList);
+                        messageAttachmentService.addMiniProgram(messageAttachmentList, createTime);
+                        insertHistoryMessageList(staffList.get(0), externalUsers, miniprogramRecordList, createTime, corpId, startTime, endTime);
+                        for (MessageAttachment messageAttachment : messageAttachmentList) {
+                            messageAttachment.setStaffId(staffList.get(0).getId());
+                        }
                     }
                 }
                 String nextCursor = jsonObject.getString("next_cursor");
@@ -250,16 +250,16 @@ public class WeComHistoryDataJob {
         }
     }
 
-    private List<ExternalUser> selectGroupMsgSendResult(String msgId, String userId, Long corpId) throws IOException {
+    private List<ExternalUser> selectGroupMsgSendResult(String msgId, String carrierId, Long corpId) throws IOException {
         List<ExternalUser> resList = new ArrayList<>();
         String cursor = "";
         do {
-            String res = getGroupMsgSendResult(msgId, userId, cursor, corpId);
+            String res = getGroupMsgSendResult(msgId, carrierId, cursor, corpId);
             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);
+                log.error("selectGroupMsgSendResult error msgId={} carrierId={} errCode={} errmsg={}", msgId, carrierId, errCode, errmsg);
                 return resList;
             }
             JSONArray sendList = jsonObject.getJSONArray("send_list");
@@ -282,13 +282,13 @@ public class WeComHistoryDataJob {
         return resList;
     }
 
-    private String getGroupMsgSendResult(String msgId, String userId, String cursor, Long corpId) throws IOException {
+    private String getGroupMsgSendResult(String msgId, String carrierId, String cursor, Long corpId) throws IOException {
         String accessToken = accessTokenService.getWeComAccessToken(corpId);
         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("userid", carrierId);
         param.put("limit", 1000);
         if (StringUtils.isNotEmpty(cursor)) {
             param.put("cursor", cursor);
@@ -336,26 +336,38 @@ public class WeComHistoryDataJob {
         return httpPoolClient.post(url, param.toJSONString());
     }
 
-    private void delHistoryMessageList(Long startTime, Long endTime) {
+    private void delHistoryMessageList(Long startTime, Long endTime, Long corpId, Long staffId) {
         HistoryMessage historyMessage = new HistoryMessage();
         historyMessage.setIsDelete(1);
         HistoryMessageExample example = new HistoryMessageExample();
-        example.createCriteria().andSendTimeBetween(new Date(startTime * 1000), new Date(endTime * 1000));
+        HistoryMessageExample.Criteria criteria =
+                example.createCriteria().andSendTimeBetween(new Date(startTime * 1000), new Date(endTime * 1000))
+                        .andCorpIdEqualTo(corpId);
+        if (staffId != null) {
+            criteria.andStaffIdEqualTo(staffId);
+        }
         historyMessageMapper.updateByExampleSelective(historyMessage, example);
 
     }
 
-    private void insertHistoryMessageList(Staff staff, List<ExternalUser> externalUsers, List<MiniprogramRecord> miniprogramRecordList, Long createTime, Long corpId) {
+    private void insertHistoryMessageList(Staff staff, List<ExternalUser> externalUsers,
+                                          List<MiniprogramRecord> miniprogramRecordList,
+                                          Long createTime, Long corpId, Long startTime, Long endTime) {
         if (CollectionUtils.isEmpty(externalUsers) || CollectionUtils.isEmpty(miniprogramRecordList)) {
             return;
         }
-        String key = String.format(GUARANTEED_MINI_PROGRAM_SET_KEY, DateUtil.getThatDayDateString());
-        Set<Long> guaranteedSet = (Set<Long>) redisTemplate.opsForValue().get(key);
-        if (CollectionUtils.isEmpty(guaranteedSet)) {
+        MessageAttachmentExample example = new MessageAttachmentExample();
+        example.createCriteria().andStaffIdEqualTo(staff.getId())
+                .andSendTimeBetween(new Date(startTime * 1000), new Date(endTime * 1000));
+        List<MessageAttachment> messageAttachments = messageAttachmentMapper.selectByExample(example);
+        if (CollectionUtils.isEmpty(messageAttachments)) {
             LarkRobotUtil.sendMessage("获取保底set失败");
             return;
         }
-        Long sendTime = externalUsers.stream().map(ExternalUser::getSendTime).filter(Objects::nonNull).findFirst().orElse(null);
+        Set<Long> guaranteedSet = messageAttachments.stream()
+                .map(MessageAttachment::getMiniprogramVideoId).collect(Collectors.toSet());
+        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());

+ 95 - 58
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

@@ -66,15 +66,15 @@ public class WeComMessageDataJob {
     @Autowired
     private CorpMapper corpMapper;
 
-
     //发送小程序标题限制字节数
     private static final int MAX_BYTES = 64;
 
     //历史优质视频可推送用户列表
-    List<PushMessage> goodHistoryPushList = new ArrayList<>();
+    Map<Long, List<PushMessage>> historicalTopMap = new HashMap<>();
 
     //保底视频列表
     Map<Long, List<Long>> guaranteedVideoMap = new HashMap<>();
+
     Map<String, String> pageMap = new HashMap<>();
 
     //初始化操作
@@ -91,11 +91,27 @@ public class WeComMessageDataJob {
             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));
+            Long staffId = Long.parseLong((String) record.get(2));
+            Double score = Double.parseDouble((String) record.get(3));
             pushMessage.setVideoId(videoId);
             pushMessage.setUserIds(userIds);
+            pushMessage.setStaffId(staffId);
+            pushMessage.setScore(score);
             list.add(pushMessage);
         }
-        goodHistoryPushList = list;
+        Map<Long, List<PushMessage>> groupedMap = list.stream()
+                .collect(Collectors.groupingBy(PushMessage::getStaffId,
+                        Collectors.mapping(pushMessage -> pushMessage,
+                                Collectors.toList())))
+                .entrySet()
+                .stream()
+                .collect(Collectors.toMap(
+                        Map.Entry::getKey,
+                        entry -> entry.getValue().stream()
+                                .sorted(Comparator.comparing(PushMessage::getScore).reversed()) // 根据 score 降序排序
+                                .collect(Collectors.toList())
+                ));
+        historicalTopMap = groupedMap;
 
         //保底视频获取
         String key = String.format(GUARANTEED_MINI_PROGRAM_KEY, DateUtil.getThatDayDateString());
@@ -111,13 +127,21 @@ public class WeComMessageDataJob {
                 LarkRobotUtil.sendMessage("保底视频获取异常,StaffId为空" + DateUtil.getThatDayDateString());
                 throw new RuntimeException();
             }
+            //默认组视频不做查询
+            if (videoParam.getStaffId() == 0L) {
+                if(videoParam.getVideoIds().size() < MAX_VIDEO_NUM){
+                    LarkRobotUtil.sendMessage("默认组视频数量不足" + DateUtil.getThatDayDateString());
+                }
+                videoMap.put(videoParam.getStaffId(), videoParam.getVideoIds());
+                continue;
+            }
             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);
+                example.createCriteria().andMiniprogramVideoIdEqualTo(videoId).andStaffIdEqualTo(videoParam.getStaffId());
                 List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
                 if (CollectionUtils.isEmpty(messageAttachmentList)) {
                     LarkRobotUtil.sendMessage("保底视频不存在,请查看videoId=" + videoId);
@@ -200,23 +224,19 @@ public class WeComMessageDataJob {
         if (guaranteedParam == null || CollectionUtils.isEmpty(guaranteedParam.getVideoParamList())) {
             return;
         }
-        List<Long> videoIdList = new ArrayList<>();
+        Set<Long> videoIdSet = new HashSet<>();
         for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
             if (CollectionUtils.isEmpty(videoParam.getVideoIds())) {
                 continue;
             }
-            videoIdList.addAll(videoParam.getVideoIds());
+            videoIdSet.addAll(videoParam.getVideoIds());
         }
-
+        List<Long> videoIdList = new ArrayList<>(videoIdSet);
         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);
-        }
+        MessageAttachment updateMessageAttachment = new MessageAttachment();
+        updateMessageAttachment.setSendTime(new Date());
+        messageAttachmentMapper.updateByExampleSelective(updateMessageAttachment, example);
         redisTemplate.delete(key);
     }
 
@@ -230,67 +250,84 @@ public class WeComMessageDataJob {
         }
         List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(example);
         if (CollectionUtils.isEmpty(staffWithUserList)) {
-            return null;
+            return Collections.emptyList();
         }
         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();
-            }
+            int n = fillHistoricalTopMessages(sendMessage, user.getId(), staffWithUser.getStaffId());
             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;
-                    }
-                }
+                // 保底数据
+                n = fillGuaranteedMessages(sendMessage, staffWithUser.getStaffId(), n);
             }
             if (n < MAX_VIDEO_NUM) {
                 LarkRobotUtil.sendMessage("组装数据失败 user=" + user);
-                throw new RuntimeException();
+                throw new RuntimeException("组装数据失败");
             }
             sendMessage.setCorpId(corpId);
             sendMessage.setStaffId(staffWithUser.getStaffId());
             sendMessage.setUserId(staffWithUser.getUserId());
             sendMessageList.add(sendMessage);
         }
+
         return sendMessageList;
     }
 
+    private int fillHistoricalTopMessages(SendMessage sendMessage, Long userId, Long staffId) {
+        List<PushMessage> pushMessages = historicalTopMap.get(staffId);
+        if (CollectionUtils.isEmpty(pushMessages)) {
+            return 0;
+        }
+        int n = 0;
+        for (PushMessage pushMessage : pushMessages) {
+            if (pushMessage.getUserIds().contains(userId)) {
+                setVideoId(sendMessage, n, pushMessage.getVideoId());
+                n++;
+                if (n >= MAX_VIDEO_NUM) {
+                    break;
+                }
+            }
+        }
+        return n;
+    }
+
+    private int fillGuaranteedMessages(SendMessage sendMessage, Long staffId, int currentCount) {
+        List<Long> guaranteedVideoIdList = guaranteedVideoMap.get(staffId);
+        if (CollectionUtils.isEmpty(guaranteedVideoIdList)) {
+            guaranteedVideoIdList = guaranteedVideoMap.get(0L);
+        }
+        if (CollectionUtils.isEmpty(guaranteedVideoIdList)) {
+            LarkRobotUtil.sendMessage("组装数据时,保底数据获取异常");
+            throw new RuntimeException("保底数据获取异常");
+        }
+        if (currentCount < MAX_VIDEO_NUM) {
+            for (Long videoId : guaranteedVideoIdList) {
+                setVideoId(sendMessage, currentCount, videoId);
+                currentCount++;
+                if (currentCount >= MAX_VIDEO_NUM) {
+                    break;
+                }
+            }
+        }
+        return currentCount;
+    }
+
+    private void setVideoId(SendMessage sendMessage, int index, Long videoId) {
+        switch (index) {
+            case 0:
+                sendMessage.setVideoId1(videoId);
+                break;
+            case 1:
+                sendMessage.setVideoId2(videoId);
+                break;
+            case 2:
+                sendMessage.setVideoId3(videoId);
+                break;
+            default:
+                break;
+        }
+    }
+
 
     @XxlJob("pushSendMessageJob")
     public ReturnT<String> pushSendMessage(String param) {

+ 11 - 3
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -16,6 +16,7 @@ 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.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -193,11 +194,18 @@ public class WeComUserDataJob {
                 }
                 List<Staff> staffList = staffMapper.selectByExample(staffExample);
                 for (Staff staff : staffList) {
-                    List<String> externalUserIds = getUserList(staff.getCarrierId(), corp.getId());
-                    if (CollectionUtils.isEmpty(externalUserIds)) {
+                    List<String> existExternalUserIds = getUserList(staff.getCarrierId(), corp.getId());
+                    List<String> allExternalUserIds = staffWithUserMapper.selectExternalUserIdByStaffId(staff.getId());
+                    allExternalUserIds.removeAll(existExternalUserIds);
+                    if (!CollectionUtils.isEmpty(allExternalUserIds)) {
+                        for (String delExternalUserId : allExternalUserIds) {
+                            userService.delStaffWithUser(delExternalUserId, staff.getCarrierId(), System.currentTimeMillis());
+                        }
+                    }
+                    if (CollectionUtils.isEmpty(existExternalUserIds)) {
                         continue;
                     }
-                    for (String externalUserId : externalUserIds) {
+                    for (String externalUserId : existExternalUserIds) {
                         userService.insertStaffWithUser(externalUserId, staff, corp.getId());
                     }
                 }

+ 4 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/model/bo/PushMessage.java

@@ -10,4 +10,8 @@ public class PushMessage {
     private Long videoId;
 
     private Set<Long> userIds;
+
+    private Long staffId;
+
+    private Double score;
 }

+ 11 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/model/po/MessageAttachment.java

@@ -5,6 +5,8 @@ import java.util.Date;
 public class MessageAttachment {
     private Long id;
 
+    private Long staffId;
+
     private Integer type;
 
     private String url;
@@ -37,6 +39,14 @@ public class MessageAttachment {
         this.id = id;
     }
 
+    public Long getStaffId() {
+        return staffId;
+    }
+
+    public void setStaffId(Long staffId) {
+        this.staffId = staffId;
+    }
+
     public Integer getType() {
         return type;
     }
@@ -140,6 +150,7 @@ public class MessageAttachment {
         sb.append(" [");
         sb.append("Hash = ").append(hashCode());
         sb.append(", id=").append(id);
+        sb.append(", staffId=").append(staffId);
         sb.append(", type=").append(type);
         sb.append(", url=").append(url);
         sb.append(", title=").append(title);

+ 60 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/model/po/MessageAttachmentExample.java

@@ -176,6 +176,66 @@ public class MessageAttachmentExample {
             return (Criteria) this;
         }
 
+        public Criteria andStaffIdIsNull() {
+            addCriterion("staff_id is null");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdIsNotNull() {
+            addCriterion("staff_id is not null");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdEqualTo(Long value) {
+            addCriterion("staff_id =", value, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdNotEqualTo(Long value) {
+            addCriterion("staff_id <>", value, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdGreaterThan(Long value) {
+            addCriterion("staff_id >", value, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdGreaterThanOrEqualTo(Long value) {
+            addCriterion("staff_id >=", value, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdLessThan(Long value) {
+            addCriterion("staff_id <", value, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdLessThanOrEqualTo(Long value) {
+            addCriterion("staff_id <=", value, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdIn(List<Long> values) {
+            addCriterion("staff_id in", values, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdNotIn(List<Long> values) {
+            addCriterion("staff_id not in", values, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdBetween(Long value1, Long value2) {
+            addCriterion("staff_id between", value1, value2, "staffId");
+            return (Criteria) this;
+        }
+
+        public Criteria andStaffIdNotBetween(Long value1, Long value2) {
+            addCriterion("staff_id not between", value1, value2, "staffId");
+            return (Criteria) this;
+        }
+
         public Criteria andTypeIsNull() {
             addCriterion("`type` is null");
             return (Criteria) this;

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

@@ -2,6 +2,7 @@ package com.tzld.piaoquan.wecom.service.Impl;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.fasterxml.jackson.databind.util.BeanUtil;
 import com.tzld.piaoquan.wecom.common.base.CommonResponse;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
 import com.tzld.piaoquan.wecom.component.HttpPoolClient;
@@ -19,6 +20,7 @@ import com.tzld.piaoquan.wecom.utils.DateUtil;
 import com.tzld.piaoquan.wecom.utils.LarkRobotUtil;
 import lombok.extern.slf4j.Slf4j;
 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.Service;
@@ -40,7 +42,6 @@ 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_MINI_PROGRAM_KEY;
-import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINI_PROGRAM_SET_KEY;
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.POST_WE_COM_MEDIA_UPLOAD;
 
 
@@ -50,6 +51,7 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
 
     @Autowired
     private HttpPoolClient httpPoolClient;
+
     @Autowired
     private AccessTokenService accessTokenService;
 
@@ -60,15 +62,19 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
     private RedisTemplate<String, Object> redisTemplate;
 
     @Override
-    public void addMiniProgram(List<MessageAttachment> messageAttachmentList) {
+    public void addMiniProgram(List<MessageAttachment> messageAttachmentList, Long createTime) {
         for (MessageAttachment messageAttachment : messageAttachmentList) {
             MessageAttachmentExample example = new MessageAttachmentExample();
             example.createCriteria()
                     .andAppidEqualTo(messageAttachment.getAppid())
                     .andTypeEqualTo(messageAttachment.getType())
-                    .andMiniprogramVideoIdEqualTo(messageAttachment.getMiniprogramVideoId());
+                    .andMiniprogramVideoIdEqualTo(messageAttachment.getMiniprogramVideoId())
+                    .andStaffIdEqualTo(messageAttachment.getStaffId());
             List<MessageAttachment> messageAttachments = messageAttachmentMapper.selectByExample(example);
             if (CollectionUtils.isEmpty(messageAttachments)) {
+                if (createTime != null) {
+                    messageAttachment.setSendTime(new Date(createTime));
+                }
                 messageAttachmentMapper.insert(messageAttachment);
                 continue;
             }
@@ -92,7 +98,8 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             videoIds.addAll(videoParam.getVideoIds());
         }
         Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
-        List<MessageAttachment> messageAttachmentList = new ArrayList<>();
+        Map<Long, MessageAttachment> messageAttachmentMap = new HashMap<>();
+
         for (Long videoId : videoIds) {
             MessageAttachment messageAttachment = new MessageAttachment();
             VideoDetail videoDetail = coverMap.get(videoId);
@@ -105,13 +112,28 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             messageAttachment.setCover(videoDetail.getCover());
             messageAttachment.setTitle(videoDetail.getTitle());
             messageAttachment.setAppid(appid);
-            messageAttachmentList.add(messageAttachment);
+            messageAttachmentMap.put(videoId, messageAttachment);
+        }
+        List<MessageAttachment> messageAttachmentList = new ArrayList<>();
+        for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
+            if (videoParam.getStaffId() == 0) {
+                continue;
+            }
+            for (Long videoId : videoParam.getVideoIds()) {
+                MessageAttachment messageAttachment = messageAttachmentMap.get(videoId);
+                if (messageAttachment == null) {
+                    continue;
+                }
+                MessageAttachment newMessageAttachment = new MessageAttachment();
+                BeanUtils.copyProperties(messageAttachment, newMessageAttachment);
+                newMessageAttachment.setStaffId(videoParam.getStaffId());
+                messageAttachmentList.add(newMessageAttachment);
+            }
         }
-        addMiniProgram(messageAttachmentList);
+        addMiniProgram(messageAttachmentList, null);
         String date = guaranteedParam.getDate();
         String key = String.format(GUARANTEED_MINI_PROGRAM_KEY, date);
         redisTemplate.opsForValue().set(key, guaranteedParam);
-        redisTemplate.opsForValue().set(String.format(GUARANTEED_MINI_PROGRAM_SET_KEY, date), videoIds);
         return CommonResponse.success();
     }
 

+ 2 - 2
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/MessageServiceImpl.java

@@ -47,12 +47,12 @@ public class MessageServiceImpl implements MessageService {
             log.info("pushWeComMessage res={}", res);
             Integer code = res.getInteger("errcode");
             if (code != 0) {
-                LarkRobotUtil.sendMessage("pushWeComMessage error" + "res:" + res.toJSONString() + "jsonObject:" + jsonObject.toJSONString());
+                LarkRobotUtil.sendMessage("pushWeComMessage error" + "res:" + res.toJSONString());
             }
             return code == 0;
         } catch (IOException e) {
+            LarkRobotUtil.sendMessage("pushWeComMessage error" + e.getMessage());
             log.error("pushWeComMessage error", e);
-            LarkRobotUtil.sendMessage("pushWeComMessage error" + "jsonObject:" + jsonObject.toJSONString() + "Exception:" + e);
         }
         return false;
     }

+ 1 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/MessageAttachmentService.java

@@ -9,7 +9,7 @@ import java.util.List;
 
 public interface MessageAttachmentService {
 
-    void addMiniProgram(List<MessageAttachment> messageAttachmentList);
+    void addMiniProgram(List<MessageAttachment> messageAttachmentList, Long createTime);
 
     CommonResponse<Void> createGuaranteedMiniProgram(GuaranteedParam guaranteedParam);
 

+ 1 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/UserService.java

@@ -10,7 +10,7 @@ public interface UserService {
 
     void insertStaffWithUser(String externalUserId, Staff staff, Long corpId);
 
-    void delStaffWithUser(String externalUserId, String staffUserId, Long deleteTime);
+    void delStaffWithUser(String externalUserId, String carrierId, Long deleteTime);
 
     void delStaffWithUser(Long userId, Long staffId, Long deleteTime);
 }

+ 22 - 7
we-com-server/src/main/resources/mapper/MessageAttachmentMapper.xml

@@ -3,6 +3,7 @@
 <mapper namespace="com.tzld.piaoquan.wecom.dao.mapper.MessageAttachmentMapper">
   <resultMap id="BaseResultMap" type="com.tzld.piaoquan.wecom.model.po.MessageAttachment">
     <id column="id" jdbcType="BIGINT" property="id" />
+    <result column="staff_id" jdbcType="BIGINT" property="staffId" />
     <result column="type" jdbcType="INTEGER" property="type" />
     <result column="url" jdbcType="VARCHAR" property="url" />
     <result column="title" jdbcType="VARCHAR" property="title" />
@@ -75,8 +76,8 @@
     </where>
   </sql>
   <sql id="Base_Column_List">
-    id, `type`, url, title, `desc`, appid, page, cover, miniprogram_video_id, is_delete, 
-    send_time, create_time, update_time
+    id, staff_id, `type`, url, title, `desc`, appid, page, cover, miniprogram_video_id, 
+    is_delete, send_time, create_time, update_time
   </sql>
   <select id="selectByExample" parameterType="com.tzld.piaoquan.wecom.model.po.MessageAttachmentExample" resultMap="BaseResultMap">
     select
@@ -112,13 +113,13 @@
     </if>
   </delete>
   <insert id="insert" parameterType="com.tzld.piaoquan.wecom.model.po.MessageAttachment">
-    insert into we_com_message_attachment (id, `type`, url, 
-      title, `desc`, appid, 
+    insert into we_com_message_attachment (id, staff_id, `type`, 
+      url, title, `desc`, appid, 
       page, cover, miniprogram_video_id, 
       send_time, create_time,
       update_time)
-    values (#{id,jdbcType=BIGINT}, #{type,jdbcType=INTEGER}, #{url,jdbcType=VARCHAR}, 
-      #{title,jdbcType=VARCHAR}, #{desc,jdbcType=VARCHAR}, #{appid,jdbcType=VARCHAR}, 
+    values (#{id,jdbcType=BIGINT}, #{staffId,jdbcType=BIGINT}, #{type,jdbcType=INTEGER}, 
+      #{url,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, #{desc,jdbcType=VARCHAR}, #{appid,jdbcType=VARCHAR}, 
       #{page,jdbcType=VARCHAR}, #{cover,jdbcType=VARCHAR}, #{miniprogramVideoId,jdbcType=BIGINT}, 
       #{sendTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP},
       #{updateTime,jdbcType=TIMESTAMP})
@@ -129,6 +130,9 @@
       <if test="id != null">
         id,
       </if>
+      <if test="staffId != null">
+        staff_id,
+      </if>
       <if test="type != null">
         `type`,
       </if>
@@ -170,6 +174,9 @@
       <if test="id != null">
         #{id,jdbcType=BIGINT},
       </if>
+      <if test="staffId != null">
+        #{staffId,jdbcType=BIGINT},
+      </if>
       <if test="type != null">
         #{type,jdbcType=INTEGER},
       </if>
@@ -220,6 +227,9 @@
       <if test="record.id != null">
         id = #{record.id,jdbcType=BIGINT},
       </if>
+      <if test="record.staffId != null">
+        staff_id = #{record.staffId,jdbcType=BIGINT},
+      </if>
       <if test="record.type != null">
         `type` = #{record.type,jdbcType=INTEGER},
       </if>
@@ -264,6 +274,7 @@
   <update id="updateByExample" parameterType="map">
     update we_com_message_attachment
     set id = #{record.id,jdbcType=BIGINT},
+      staff_id = #{record.staffId,jdbcType=BIGINT},
       `type` = #{record.type,jdbcType=INTEGER},
       url = #{record.url,jdbcType=VARCHAR},
       title = #{record.title,jdbcType=VARCHAR},
@@ -283,6 +294,9 @@
   <update id="updateByPrimaryKeySelective" parameterType="com.tzld.piaoquan.wecom.model.po.MessageAttachment">
     update we_com_message_attachment
     <set>
+      <if test="staffId != null">
+        staff_id = #{staffId,jdbcType=BIGINT},
+      </if>
       <if test="type != null">
         `type` = #{type,jdbcType=INTEGER},
       </if>
@@ -324,7 +338,8 @@
   </update>
   <update id="updateByPrimaryKey" parameterType="com.tzld.piaoquan.wecom.model.po.MessageAttachment">
     update we_com_message_attachment
-    set `type` = #{type,jdbcType=INTEGER},
+    set staff_id = #{staffId,jdbcType=BIGINT},
+      `type` = #{type,jdbcType=INTEGER},
       url = #{url,jdbcType=VARCHAR},
       title = #{title,jdbcType=VARCHAR},
       `desc` = #{desc,jdbcType=VARCHAR},

+ 9 - 0
we-com-server/src/main/resources/mapper/StaffWithUserMapper.xml

@@ -265,4 +265,13 @@
       )
     </foreach>
   </insert>
+
+  <select id="selectExternalUserIdByStaffId" parameterType="java.lang.Long" resultType="java.lang.String">
+    select t2.external_user_id
+    from we_com_staff_with_user t1
+    left join we_com_user t2
+    on t1.user_id = t2.id
+    where t1.staff_id = #{staffId,jdbcType=BIGINT}
+    and t2.external_user_id is not null
+  </select>
 </mapper>

+ 0 - 7
we-com-server/src/main/resources/mapper/UserMapper.xml

@@ -374,13 +374,6 @@
     where id = #{id,jdbcType=BIGINT}
   </update>
 
-
-    <select id="selectIdByExternalUserId3rdParty" parameterType="String" resultType="Long">
-        select id
-        from we_com_user
-        where external_user_id_3rd_party = #{externalUserId3rdParty}
-    </select>
-
     <select id="selectIdByExternalUserId" parameterType="String" resultType="Long">
         select id
         from we_com_user

+ 2 - 2
we-com-server/src/main/resources/mybatis-generator-config.xml

@@ -51,8 +51,8 @@
         </javaClientGenerator>
 
 <!--        <table tableName="we_com_alert_message" domainObjectName="AlertMessage" alias=""/>-->
-        <table tableName="we_com_history_message" domainObjectName="HistoryMessage" alias=""/>
-<!--        <table tableName="we_com_message_attachment" domainObjectName="MessageAttachment" alias=""/>-->
+<!--        <table tableName="we_com_history_message" domainObjectName="HistoryMessage" alias=""/>-->
+        <table tableName="we_com_message_attachment" domainObjectName="MessageAttachment" alias=""/>
 <!--        <table tableName="we_com_staff" domainObjectName="Staff" alias=""/>-->
 <!--        <table tableName="we_com_staff_with_user" domainObjectName="StaffWithUser" alias=""/>-->
 <!--        <table tableName="we_com_send_message" domainObjectName="SendMessage" alias=""/>-->