Browse Source

增加待发送消息落库

xueyiming 8 months ago
parent
commit
2249901a4d

+ 2 - 11
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

@@ -4,10 +4,7 @@ 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.dao.mapper.MessageAttachmentMapper;
-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.dao.mapper.*;
 import com.tzld.piaoquan.wecom.model.bo.PushMessage;
 import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
@@ -118,17 +115,12 @@ public class WeComMessageDataJob {
     }
 
     public void assemble() {
-        List<String> staffExtIdList = new ArrayList<>();
-        staffExtIdList.add("wogizUDQAA1oa8qD5jh_qvVkfNbyohUw");
         init();
         Map<String, List<String>> res = new HashMap<>();
         UserExample example = new UserExample();
-        List<String> list = new ArrayList<>();
-        list.add("wmgizUDQAAhUFId3OLvMROvFfnBeVpCg");
-        example.createCriteria().andExternalUserId3rdPartyIn(list);
         long count = userMapper.countByExample(example);
         int page = 1;
-        int pageSize = 10;
+        int pageSize = 1000;
         long totalPageSize = count / pageSize + 1;
         totalPageSize = 1L;
         for (; page <= totalPageSize; page++) {
@@ -152,7 +144,6 @@ public class WeComMessageDataJob {
             }
         }
         pushMessage(res);
-
         saveGuaranteedVideoIdList(guaranteedVideoIdList);
     }
 

+ 309 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob2.java

@@ -0,0 +1,309 @@
+package com.tzld.piaoquan.wecom.job;
+
+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.dao.mapper.*;
+import com.tzld.piaoquan.wecom.model.bo.PushMessage;
+import com.tzld.piaoquan.wecom.model.po.*;
+import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
+import com.tzld.piaoquan.wecom.service.MessageService;
+import com.tzld.piaoquan.wecom.utils.DateUtil;
+import com.tzld.piaoquan.wecom.utils.MessageUtil;
+import com.tzld.piaoquan.wecom.utils.OdpsUtil;
+import com.tzld.piaoquan.wecom.utils.page.Page;
+import 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.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.TimeConstant.MILLISECOND_DAY;
+
+@Component
+public class WeComMessageDataJob2 {
+
+    @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_VIDEO_NUM = 3;
+
+    //历史优质视频可推送用户列表
+    List<PushMessage> goodHistoryPushList = new ArrayList<>();
+
+    //保底视频列表
+    List<Long> guaranteedVideoIdList = new ArrayList<>();
+
+    //从缓存中获取的保底视频数量
+    int getGuaranteedVideoIdNum = 0;
+
+    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;
+
+        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)) {
+            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;
+            }
+            MessageAttachment messageAttachment = messageAttachmentList.get(0);
+            if (messageAttachment.getSendTime() != null
+                    && DateUtil.dateDifference(new Date(), messageAttachment.getSendTime()) < 180 * MILLISECOND_DAY) {
+                continue;
+            }
+            saveVideoIds.add(videoId);
+            if (saveVideoIds.size() >= MAX_VIDEO_NUM) {
+                break;
+            }
+        }
+        if (saveVideoIds.size() < MAX_VIDEO_NUM) {
+            throw new RuntimeException("保底数据不足");
+        }
+        guaranteedVideoIdList = saveVideoIds;
+    }
+
+    public void assemble() {
+        init();
+        Map<String, List<String>> res = new HashMap<>();
+        UserExample example = new UserExample();
+        long count = userMapper.countByExample(example);
+        int page = 1;
+        int pageSize = 1000;
+        long totalPageSize = count / pageSize + 1;
+        totalPageSize = 1L;
+        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);
+                if (!CollectionUtils.isEmpty(sendMessageList)) {
+                    allSeneMessageList.addAll(sendMessageList);
+                }
+            }
+            if (!CollectionUtils.isEmpty(allSeneMessageList)) {
+                sendMessageMapper.insertList(allSeneMessageList);
+            }
+        }
+        pushSendMessage();
+        saveGuaranteedVideoIdList(guaranteedVideoIdList);
+    }
+
+    public void saveGuaranteedVideoIdList(List<Long> videoIdList) {
+        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);
+        }
+        //移除从redis中获取的保底数据
+        for (int i = 0; i < getGuaranteedVideoIdNum; i++) {
+            redisTemplate.opsForList().leftPop(GUARANTEED_MINIPROGRAM_KEY);
+        }
+    }
+
+    public List<SendMessage> getSendMessage(User user) {
+        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;
+                }
+            }
+        }
+        //保底数据
+        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) {
+            throw new RuntimeException("保底数据异常");
+        }
+        StaffWithUserExample example = new StaffWithUserExample();
+        example.createCriteria().andUserIdEqualTo(user.getId());
+        List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(example);
+        if (CollectionUtils.isEmpty(staffWithUserList)) {
+            return null;
+        }
+        for (StaffWithUser staffWithUser : staffWithUserList) {
+            SendMessage newSendMessage = new SendMessage();
+            BeanUtils.copyProperties(sendMessage, newSendMessage);
+            newSendMessage.setStaffId(staffWithUser.getId());
+            sendMessageList.add(newSendMessage);
+        }
+        return sendMessageList;
+    }
+
+
+    public void pushSendMessage() {
+        List<SendMessage> groupList = sendMessageMapper.getGroupList(DateUtil.getThatDayDate(), 0);
+        for (SendMessage sendMessage : groupList) {
+            SendMessageExample example = new SendMessageExample();
+            example.createCriteria().andStaffIdEqualTo(sendMessage.getStaffId())
+                    .andVideoId1EqualTo(sendMessage.getVideoId1())
+                    .andVideoId2EqualTo(sendMessage.getVideoId2())
+                    .andVideoId3EqualTo(sendMessage.getVideoId3())
+                    .andCreateTimeGreaterThan(DateUtil.getThatDayDate())
+                    .andIsSendEqualTo(0);
+            List<SendMessage> sendMessages = sendMessageMapper.selectByExample(example);
+        }
+    }
+
+    public void pushMessage(List<String> userIds, 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();
+
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("name", name);
+        jsonObject.put("text", text);
+        JSONArray attachments = new JSONArray();
+        List<Long> videoIdList = new ArrayList<>();
+        videoIdList.add(sendMessage.getVideoId1());
+        videoIdList.add(sendMessage.getVideoId2());
+        videoIdList.add(sendMessage.getVideoId3());
+        for (Long videoId : videoIdList) {
+            JSONObject attachment = new JSONObject();
+            attachment.put("msgtype", "miniprogram");
+            MessageAttachmentExample example = new MessageAttachmentExample();
+            example.createCriteria().andMiniprogramVideoIdEqualTo(videoId);
+            List<MessageAttachment> messageAttachmentList = messageAttachmentMapper.selectByExample(example);
+            if (CollectionUtils.isEmpty(messageAttachmentList)) {
+                throw new RuntimeException("附件信息查询异常");
+            }
+            MessageAttachment messageAttachment = messageAttachmentList.get(0);
+            JSONObject miniprogram = new JSONObject();
+            miniprogram.put("appid", messageAttachment.getAppid());
+            miniprogram.put("title", messageAttachment.getTitle());
+            miniprogram.put("cover", messageAttachment.getCover());
+            String page = "";
+            String key = staff.getStaffExtId() + "_" + videoId;
+            if (pageMap.containsKey(key)) {
+                page = pageMap.get(key);
+            } else {
+                page = messageAttachmentService.getPage(staff, videoId);
+                pageMap.put(key, page);
+            }
+            if (StringUtils.isEmpty(page)) {
+                throw new RuntimeException("获取page失败");
+            }
+            miniprogram.put("page", page);
+
+            attachment.put("miniprogram", miniprogram);
+            attachments.add(attachment);
+        }
+        jsonObject.put("attachments", attachments);
+        List<List<String>> lists = Lists.partition(userIds, 10000);
+        for (List<String> list : lists) {
+            List<JSONObject> staffEuList = new ArrayList<>();
+            JSONObject newJSONObject = new JSONObject();
+            newJSONObject.putAll(jsonObject);
+            JSONObject staff_eu = new JSONObject();
+            staff_eu.put("staff_ext_id", staff.getStaffExtId());
+            staff_eu.put("eu_ext_ids", list);
+            staffEuList.add(staff_eu);
+            newJSONObject.put("staff_eu_list", staffEuList);
+            pushList.add(newJSONObject);
+        }
+
+        if (CollectionUtils.isEmpty(pushList)) {
+            throw new RuntimeException("推送视频生成失败");
+        }
+        for (JSONObject pushJsonObject : pushList) {
+            messageService.pushMessage(pushJsonObject);
+        }
+
+        //TODO 发送成功更新发送状态
+    }
+}

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

@@ -0,0 +1,114 @@
+package com.tzld.piaoquan.wecom.model.po;
+
+import java.util.Date;
+
+public class SendMessage {
+    private Long id;
+
+    private Long staffId;
+
+    private Long userId;
+
+    private Long videoId1;
+
+    private Long videoId2;
+
+    private Long videoId3;
+
+    private Integer isSend;
+
+    private Date createTime;
+
+    private Date updateTime;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Long getStaffId() {
+        return staffId;
+    }
+
+    public void setStaffId(Long staffId) {
+        this.staffId = staffId;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public Long getVideoId1() {
+        return videoId1;
+    }
+
+    public void setVideoId1(Long videoId1) {
+        this.videoId1 = videoId1;
+    }
+
+    public Long getVideoId2() {
+        return videoId2;
+    }
+
+    public void setVideoId2(Long videoId2) {
+        this.videoId2 = videoId2;
+    }
+
+    public Long getVideoId3() {
+        return videoId3;
+    }
+
+    public void setVideoId3(Long videoId3) {
+        this.videoId3 = videoId3;
+    }
+
+    public Integer getIsSend() {
+        return isSend;
+    }
+
+    public void setIsSend(Integer isSend) {
+        this.isSend = isSend;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+    public Date getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Date updateTime) {
+        this.updateTime = updateTime;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append(getClass().getSimpleName());
+        sb.append(" [");
+        sb.append("Hash = ").append(hashCode());
+        sb.append(", id=").append(id);
+        sb.append(", staffId=").append(staffId);
+        sb.append(", userId=").append(userId);
+        sb.append(", videoId1=").append(videoId1);
+        sb.append(", videoId2=").append(videoId2);
+        sb.append(", videoId3=").append(videoId3);
+        sb.append(", isSend=").append(isSend);
+        sb.append(", createTime=").append(createTime);
+        sb.append(", updateTime=").append(updateTime);
+        sb.append("]");
+        return sb.toString();
+    }
+}

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

@@ -53,8 +53,9 @@
 <!--        <table tableName="we_com_user" domainObjectName="User" 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" domainObjectName="Staff" alias=""/>-->
 <!--        <table tableName="we_com_staff_with_user" domainObjectName="StaffWithUser" alias=""/>-->
+        <table tableName="we_com_send_message" domainObjectName="SendMessage" alias=""/>
 
 
     </context>