ソースを参照

增加定时任务

xueyiming 7 ヶ月 前
コミット
545c4972a3

+ 5 - 0
pom.xml

@@ -114,6 +114,11 @@
             <version>0.27.2-public</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.xuxueli</groupId>
+            <artifactId>xxl-job-core</artifactId>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 49 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/config/XxlJobConfig.java

@@ -0,0 +1,49 @@
+package com.tzld.piaoquan.wecom.config;
+
+import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class XxlJobConfig {
+
+
+    @Value("${xxl.job.admin.addresses}")
+    private String adminAddresses;
+
+    @Value("${xxl.job.accessToken}")
+    private String accessToken;
+
+    @Value("${xxl.job.executor.appname}")
+    private String appName;
+
+    @Value("${xxl.job.executor.address}")
+    private String address;
+
+    @Value("${xxl.job.executor.ip}")
+    private String ip;
+
+    @Value("${xxl.job.executor.port}")
+    private int port;
+
+    @Value("${xxl.job.executor.logpath}")
+    private String logPath;
+
+    @Value("${xxl.job.executor.logretentiondays}")
+    private int logRetentionDays;
+
+    @Bean
+    public XxlJobSpringExecutor xxlJobExecutor() {
+        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
+        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
+        xxlJobSpringExecutor.setAppname(appName);
+        xxlJobSpringExecutor.setAddress(address);
+        xxlJobSpringExecutor.setIp(ip);
+        xxlJobSpringExecutor.setPort(port);
+        xxlJobSpringExecutor.setAccessToken(accessToken);
+        xxlJobSpringExecutor.setLogPath(logPath);
+        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
+        return xxlJobSpringExecutor;
+    }
+}

+ 1 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/dao/mapper/SendMessageMapper.java

@@ -35,7 +35,7 @@ public interface SendMessageMapper {
 
     void insertList(@Param("list") List<SendMessage> list);
 
-    List<SendMessage> getGroupList(@Param("createTime") Date createTime, @Param("isSend")Integer isSend);
+    List<SendMessage> getGroupList(@Param("createTime") Date createTime, @Param("isSend") Integer isSend);
 
     List<String> selectExternalUserId3rdParty(@Param("record") SendMessage record);
 

+ 5 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -17,6 +17,8 @@ import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
 import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 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 org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -51,7 +53,8 @@ public class WeComHistoryDataJob {
     @Autowired
     private MessageAttachmentService messageAttachmentService;
 
-    public void selectHistoryMessageByDay() {
+    @XxlJob("saveHistoryMessageJob")
+    public ReturnT<String> selectHistoryMessageByDay(String param) {
         HistoryMessageExample example = new HistoryMessageExample();
         example.setOrderByClause("create_time desc");
         example.setPage(new Page<>(1, 1));
@@ -61,6 +64,7 @@ public class WeComHistoryDataJob {
         for (; startTime < endTime; startTime += TimeConstant.DAY) {
             selectHistoryMessage(startTime, Math.min(startTime + TimeConstant.DAY, endTime));
         }
+        return ReturnT.SUCCESS;
     }
 
     public void selectHistoryMessage(Long startTime, Long endTime) {

+ 148 - 106
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

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

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

@@ -1,309 +0,0 @@
-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();
-        List<String> test = new ArrayList<>();
-        test.add("wmgizUDQAAzefmcVLEuJLR5YcHInGcsg");
-        test.add("wmgizUDQAAYdFWt1hts2fU6P2h84fPvQ");
-        example.createCriteria().andExternalUserId3rdPartyIn(test);
-        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);
-                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.getStaffId());
-            newSendMessage.setUserId(staffWithUser.getUserId());
-            sendMessageList.add(newSendMessage);
-        }
-        return sendMessageList;
-    }
-
-
-    public void pushSendMessage() {
-        List<SendMessage> groupList = sendMessageMapper.getGroupList(DateUtil.getThatDayDate(), 0);
-        for (SendMessage sendMessage : groupList) {
-            sendMessage.setIsSend(0);
-            sendMessage.setCreateTime(DateUtil.getThatDayDate());
-            List<String> sendUserList = sendMessageMapper.selectExternalUserId3rdParty(sendMessage);
-            pushMessage(sendUserList, sendMessage);
-        }
-    }
-
-    public void 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();
-
-        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(sendUserList, 10000);
-        for (List<String> list : lists) {
-            List<JSONObject> staffEuList = new ArrayList<>();
-            JSONObject newJSONObject = new JSONObject();
-            newJSONObject.putAll(jsonObject);
-            JSONObject staff_eu = new JSONObject();
-            staff_eu.put("staff_ext_id", staff.getStaffExtId());
-            staff_eu.put("eu_ext_ids", list);
-            staffEuList.add(staff_eu);
-            newJSONObject.put("staff_eu_list", staffEuList);
-            pushList.add(newJSONObject);
-        }
-
-        if (CollectionUtils.isEmpty(pushList)) {
-            throw new RuntimeException("推送视频生成失败");
-        }
-        for (JSONObject pushJsonObject : pushList) {
-            messageService.pushMessage(pushJsonObject);
-        }
-
-        //TODO 发送成功更新发送状态
-    }
-}

+ 48 - 13
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -11,6 +11,8 @@ import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
 import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.page.Page;
+import com.xxl.job.core.biz.model.ReturnT;
+import com.xxl.job.core.handler.annotation.XxlJob;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.checkerframework.checker.units.qual.A;
@@ -43,19 +45,29 @@ public class WeComUserDataJob {
     @Autowired
     private StaffWithUserMapper staffWithUserMapper;
 
-    public void insertStaffWithUser() {
-        long endTime = 1727507085L;
-        long startTime = 1720540800L;
+    @XxlJob("insertStaffWithUserJob")
+    public ReturnT<String> insertStaffWithUser(String param) {
+        long endTime;
+        long startTime;
+        if (StringUtils.isEmpty(param)) {
+            startTime = 1720540800L;
+            endTime = System.currentTimeMillis() / 1000;
+        } else {
+            String[] split = param.split(",");
+            startTime = Long.parseLong(split[0]);
+            endTime = Long.parseLong(split[1]);
+        }
         StaffExample example = new StaffExample();
         example.createCriteria().andIdEqualTo(1L);
         List<Staff> staffList = staffMapper.selectByExample(example);
         for (Staff staff : staffList) {
             insertAllUser(startTime, endTime, staff);
         }
+        return ReturnT.SUCCESS;
     }
 
     //初始化用户使用此任务
-    public void insertAllUser(long startTime, long endTime, Staff staff) {
+    private void insertAllUser(long startTime, long endTime, Staff staff) {
         try {
             Integer total = getUserTotal(startTime, endTime, staff.getStaffExtId());
             if (total == null || total == 0) {
@@ -96,7 +108,7 @@ public class WeComUserDataJob {
                     }
                     sum++;
                 }
-                if(!CollectionUtils.isEmpty(insertStaffWithUserList)){
+                if (!CollectionUtils.isEmpty(insertStaffWithUserList)) {
                     staffWithUserMapper.insertList(insertStaffWithUserList);
                 }
                 if (jsonArray.size() < size) {
@@ -128,22 +140,33 @@ public class WeComUserDataJob {
         return httpPoolClientDefault.get(url);
     }
 
-    public void updateStaffWithUser() {
-        UserExample userExample = new UserExample();
-        userExample.setOrderByClause("create_time desc");
-        userExample.setPage(new Page<>(1, 1));
-        List<User> userList = userMapper.selectByExample(userExample);
-        long startTime = userList.get(0).getCreateTime().getTime() / 1000 - 2L * TimeConstant.HOUR;
-        long endTime = System.currentTimeMillis() / 1000;
+    @XxlJob("updateStaffWithUserJob")
+    public ReturnT<String> updateStaffWithUser(String param) {
+
+        long endTime;
+        long startTime;
+        if (StringUtils.isEmpty(param)) {
+            UserExample userExample = new UserExample();
+            userExample.setOrderByClause("create_time desc");
+            userExample.setPage(new Page<>(1, 1));
+            List<User> userList = userMapper.selectByExample(userExample);
+            startTime = userList.get(0).getCreateTime().getTime() / 1000 - 2L * TimeConstant.HOUR;
+            endTime = System.currentTimeMillis() / 1000;
+        } else {
+            String[] split = param.split(",");
+            startTime = Long.parseLong(split[0]);
+            endTime = Long.parseLong(split[1]);
+        }
         StaffExample example = new StaffExample();
         List<Staff> staffList = staffMapper.selectByExample(example);
         for (Staff staff : staffList) {
             updateUser(startTime, endTime, staff);
         }
+        return ReturnT.SUCCESS;
     }
 
 
-    public void updateUser(long startTime, long endTime, Staff staff) {
+    private void updateUser(long startTime, long endTime, Staff staff) {
         try {
             Integer total = getUpdateUserTotal(startTime, endTime, staff.getStaffExtId());
             if (total == null || total == 0) {
@@ -225,4 +248,16 @@ public class WeComUserDataJob {
     }
 
 
+    @XxlJob("testJob")
+    public ReturnT<String> test(String param) {
+        User user = new User();
+        user.setGender(0);
+        user.setName("test");
+        user.setExternalUserId("test");
+        user.setExternalUserId3rdParty("test");
+        user.setType(1);
+        userMapper.insert(user);
+        return ReturnT.SUCCESS;
+    }
+
 }

+ 13 - 0
we-com-server/src/main/resources/application.properties

@@ -25,5 +25,18 @@ spring.redis.lettuce.pool.max-wait=-1
 spring.redis.lettuce.pool.max-idle=8
 spring.redis.lettuce.pool.min-idle=0
 
+datalog=/datalog
+
+spring.application.name=we-com-server
+
+xxl.job.admin.addresses=http://xxl-job-internal.piaoquantv.com/xxl-job-admin
+xxl.job.accessToken=
+xxl.job.executor.appname=${spring.application.name}
+xxl.job.executor.address=
+xxl.job.executor.ip=
+xxl.job.executor.port=9999
+xxl.job.executor.logpath=${datalog}/xxl-job/
+xxl.job.executor.logretentiondays=30
+
 
 

+ 1 - 0
we-com-server/src/main/resources/mapper/SendMessageMapper.xml

@@ -306,6 +306,7 @@
         select staff_id, video_id_1, video_id_2, video_id_3
         from we_com_send_message
         where create_time >= #{createTime} and is_send = #{isSend}
+        group by staff_id, video_id_1, video_id_2, video_id_3
     </select>
 
     <select id="selectExternalUserId3rdParty" parameterType="com.tzld.piaoquan.wecom.model.po.SendMessage" resultType="java.lang.String">