Browse Source

社群群发random sendTime

wangyunpeng 1 tuần trước cách đây
mục cha
commit
c143feb4b9

+ 67 - 6
api-module/src/main/java/com/tzld/piaoquan/api/job/wecom/thirdpart/WeComSendMsgJob.java

@@ -24,18 +24,17 @@ import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
 import com.tzld.piaoquan.growth.common.utils.DateUtil;
 import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
 import com.tzld.piaoquan.growth.common.utils.MessageUtil;
+import com.tzld.piaoquan.growth.common.utils.RedisUtils;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
@@ -59,6 +58,9 @@ public class WeComSendMsgJob {
     @Autowired
     ThirdPartWeComMsgMapper msgMapper;
 
+    @Autowired
+    RedisUtils redisUtils;
+
     @Value("${send.room.msg.video.min.score:3}")
     private Double videoMinScore;
     @Value("${send.room.msg.duplicate.days:7}")
@@ -73,6 +75,8 @@ public class WeComSendMsgJob {
     private Map<Long, String> staffPutSceneConfig;
     @ApolloJsonValue("${send.room.msg.auto.open.staff:[]}")
     private List<Long> autoOpenStaffList;
+    @Value("${send.room.msg.random.time:20}")
+    private Integer randomTimeRange;
 
     private final static ExecutorService pool = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.SECONDS,
             new LinkedBlockingQueue<>(),
@@ -90,7 +94,7 @@ public class WeComSendMsgJob {
                     continue;
                 }
                 pool.execute(() -> {
-                    List<String> timeList = JSONObject.parseArray(room.getSendTime()).toJavaList(String.class);
+                    List<String> timeList = getRoomSendTime(room);
                     if (timeList.contains(time)) {
                         // 选取视频
                         List<CgiReplyBucketData> cgiReplyBucketDataList = getCgiReplyBucketData(room.getThirdRoomId(), staff);
@@ -109,6 +113,14 @@ public class WeComSendMsgJob {
         return ReturnT.SUCCESS;
     }
 
+    private List<String> getRoomSendTime(ThirdPartWeComRoom room) {
+        String timeStr = redisUtils.get("wecom:room:send:time:" + room.getThirdRoomId());
+        if (StringUtils.isBlank(timeStr)) {
+            return JSONObject.parseArray(room.getSendTime()).toJavaList(String.class);
+        }
+        return JSONObject.parseArray(timeStr).toJavaList(String.class);
+    }
+
     private SendAppMsgRequest buildSendAppMsgRequest(CgiReplyBucketData cgiReplyBucketData,
                                                      ThirdPartWeComStaff staff,
                                                      ThirdPartWeComRoom room) {
@@ -280,7 +292,6 @@ public class WeComSendMsgJob {
         msgMapper.insertSelective(msg);
     }
 
-
     @XxlJob("autoOpenSendStatusJob")
     public ReturnT<String> autoOpenSendStatusJob(String param) {
         List<ThirdPartWeComStaff> activeStaffList = weComThirdPartyService.getActiveStaffList();
@@ -301,4 +312,54 @@ public class WeComSendMsgJob {
         }
         return ReturnT.SUCCESS;
     }
+
+    @XxlJob("randomRoomSendTimeJob")
+    public ReturnT<String> randomRoomSendTimeJob(String param) {
+        List<ThirdPartWeComStaff> activeStaffList = weComThirdPartyService.getActiveStaffList();
+        String now = DateUtil.getCurrentDateStr("HH:mm");
+        for (ThirdPartWeComStaff staff : activeStaffList) {
+            List<ThirdPartWeComRoom> roomList = weComThirdPartyService.getStaffRoomList(staff.getId());
+            for (ThirdPartWeComRoom room : roomList) {
+                if (room.getSendStatus() != 1) {
+                    continue;
+                }
+                String key = "wecom:room:send:time:" + room.getThirdRoomId();
+                if (redisUtils.containsKey(key)) {
+                    continue;
+                }
+                List<String> timeList = JSONObject.parseArray(room.getSendTime()).toJavaList(String.class);
+                // 判断timeList中在now之后的时间,随机增加或减少20以内的分钟数
+                List<String> newTimeList = new ArrayList<>();
+                for (String time : timeList) {
+                    // time与now比较,判断是否在now之后
+                    int hour = Integer.parseInt(time.substring(0, 2));
+                    int minute = Integer.parseInt(time.substring(3, 5));
+                    int nowHour = Integer.parseInt(now.substring(0, 2));
+                    int nowMinute = Integer.parseInt(now.substring(3, 5));
+                    if (hour < nowHour || (hour == nowHour && minute <= nowMinute)) {
+                        newTimeList.add(time);
+                        continue;
+                    }
+                    // 增加或减少20以内的分钟数 -20~20
+                    int randomMinute = new Random().nextInt(randomTimeRange * 2) - randomTimeRange;
+                    minute += randomMinute;
+                    if (minute < 0) {
+                        hour--;
+                        minute += 60;
+                    } else if (minute >= 60) {
+                        hour++;
+                        minute -= 60;
+                    }
+                    // 确保时间在now之后
+                    if (hour < nowHour || (hour == nowHour && minute <= nowMinute)) {
+                        newTimeList.add(time);
+                        continue;
+                    }
+                    newTimeList.add(String.format("%02d:%02d", hour, minute));
+                }
+                redisUtils.set(key, JSONObject.toJSONString(newTimeList), 18 * 60 * 60);
+            }
+        }
+        return ReturnT.SUCCESS;
+    }
 }

+ 5 - 0
api-module/src/test/java/com/tzld/piaoquan/api/WeComThirdPartTest.java

@@ -61,6 +61,11 @@ public class WeComThirdPartTest {
         weComSendMsgJob.autoOpenSendStatusJob("");
     }
 
+    @Test
+    public void randomRoomSendTimeJob() {
+        weComSendMsgJob.randomRoomSendTimeJob("");
+    }
+
     @Test
     public void autoCreateRoomJob() {
         weComCreateRoomJob.autoCreateRoomJob("");