Selaa lähdekoodia

不自动发送消息,拉群每日上限

wangyunpeng 16 tuntia sitten
vanhempi
commit
0653e91174

+ 30 - 8
api-module/src/main/java/com/tzld/piaoquan/api/job/wecom/thirdpart/WeComCreateRoomJob.java

@@ -20,10 +20,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 import java.util.*;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -47,6 +44,12 @@ public class WeComCreateRoomJob {
     @Value("${create.room.member.max.nums:40}")
     private Integer memberMaxNums;
 
+    @Value("${create.room.auto.send.msg:false}")
+    private Boolean autoSendMsg;
+
+    @Value("${create.room.daily.max.num:50}")
+    private Integer dailyMaxNum;
+
     private final static ExecutorService pool = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.SECONDS,
             new LinkedBlockingQueue<>(1000),
             new ThreadFactoryBuilder().setNameFormat("AutoCreateRoomJob-%d").build(),
@@ -55,11 +58,24 @@ public class WeComCreateRoomJob {
     @XxlJob("autoCreateRoom")
     public ReturnT<String> autoCreateRoomJob(String param) {
         List<ThirdPartWeComStaff> activeStaffList = weComThirdPartyService.getActiveStaffList();
+        CountDownLatch cdl = new CountDownLatch(activeStaffList.size());
         for (ThirdPartWeComStaff staff : activeStaffList) {
             if (staff.getAutoCreateRoom() != 1) {
+                cdl.countDown();
                 continue;
             }
-            pool.execute(() -> createStaffRoom(staff));
+            pool.execute(() -> {
+                try {
+                    createStaffRoom(staff);
+                } finally {
+                    cdl.countDown();
+                }
+            });
+        }
+        try {
+            cdl.await();
+        } catch (InterruptedException e) {
+            log.error("autoCreateRoomJob error", e);
         }
         return ReturnT.SUCCESS;
     }
@@ -107,6 +123,11 @@ public class WeComCreateRoomJob {
             // 群成员添加存储
             saveRoomUser(room, addUserList);
         }
+        // 获取当日新创建群 达到上限 不创建
+        Long roomCount = weComThirdPartyService.getTodayCreateRoomCount(staff.getId());
+        if (roomCount >= dailyMaxNum) {
+            return;
+        }
         // 未加群客户创建新群
         if (CollectionUtils.isNotEmpty(staffUserList)) {
             createNewRoom(staff, staffUserList, maxRoomNum);
@@ -131,7 +152,8 @@ public class WeComCreateRoomJob {
                                List<ThirdPartWeComStaffUser> staffUserList,
                                Integer roomNum) {
         Iterator<ThirdPartWeComStaffUser> iterator = staffUserList.iterator();
-        while (iterator.hasNext()) {
+        // todo 暂时每次任务仅创建一个群
+        //while (iterator.hasNext()) {
             List<Long> vids = new ArrayList<>();
             List<ThirdPartWeComStaffUser> addUserList = new ArrayList<>();
             int size = staffUserList.size() > memberMaxNums ? memberMaxNums : staffUserList.size();
@@ -169,7 +191,7 @@ public class WeComCreateRoomJob {
                 List<GetRoomUserListResponse.Member> userList = weComThirdPartyService.getRoomUserList(roomUserListRequest);
                 roomDetail.setMemberCount(userList.size());
                 roomDetail.setAddUserStatus(userList.size() == memberMaxNums ? 0 : 1);
-                roomDetail.setSendStatus(userList.size() > sendMsgStatusNums ? 1 : 0);
+                roomDetail.setSendStatus(userList.size() > sendMsgStatusNums && autoSendMsg ? 1 : 0);
                 roomMapper.updateByPrimaryKeySelective(roomDetail);
                 if (!iterator.hasNext() || userList.size() == memberMaxNums) {
                     break;
@@ -186,7 +208,7 @@ public class WeComCreateRoomJob {
                 // 群成员添加存储
                 saveRoomUser(roomDetail, addUserList);
             }
-        }
+        //}
     }
 
     private void updateRoomManagement(ThirdPartWeComStaff staff, CreateRoomWxResponse createRoomWxResponse) {

+ 44 - 25
api-module/src/main/java/com/tzld/piaoquan/api/job/wecom/thirdpart/WeComUserDetailJob.java

@@ -23,10 +23,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 import java.util.*;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -57,6 +54,9 @@ public class WeComUserDetailJob {
     @Value("${create.room.member.max.nums:40}")
     private Integer memberMaxNums;
 
+    @Value("${create.room.auto.send.msg:false}")
+    private Boolean autoSendMsg;
+
     private final static ExecutorService pool = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.SECONDS,
             new LinkedBlockingQueue<>(1000),
             new ThreadFactoryBuilder().setNameFormat("SyncUserDetailJob-%d").build(),
@@ -65,29 +65,36 @@ public class WeComUserDetailJob {
     @XxlJob("syncUserDetail")
     public ReturnT<String> syncUserDetail(String param) {
         List<ThirdPartWeComStaff> activeStaffList = thirdPartyService.getActiveStaffList();
+        CountDownLatch cdl = new CountDownLatch(activeStaffList.size());
         for (ThirdPartWeComStaff staff : activeStaffList) {
             pool.execute(() -> {
-                String uuid = staff.getThirdUuid();
-                String offLineKey = "wecom:thirdpart:offline:" + uuid;
-                if (redisUtils.containsKey(offLineKey)) {
-                    return;
-                }
-                String response = apiClient.getRunClientByUuid(new UuidRequest(uuid));
-                CommonResponse<LoginInfo> commonResponse =
-                        JSONObject.parseObject(response, new TypeReference<CommonResponse<LoginInfo>>() {});
-                if (commonResponse.getErrcode() != 0) {
-                    return;
-                } else {
-                    LoginInfo loginInfo = commonResponse.getData();
-                    staff.setAvatar(loginInfo.getUser_info().getObject().getAvatar());
-                    staff.setName(loginInfo.getUser_info().getObject().getNickname());
-                    staff.setUpdateTime(new Date());
-                    staffMapper.updateByPrimaryKeySelective(staff);
+                try {
+                    String uuid = staff.getThirdUuid();
+                    String offLineKey = "wecom:thirdpart:offline:" + uuid;
+                    if (redisUtils.containsKey(offLineKey)) {
+                        return;
+                    }
+                    String response = apiClient.getRunClientByUuid(new UuidRequest(uuid));
+                    CommonResponse<LoginInfo> commonResponse =
+                            JSONObject.parseObject(response, new TypeReference<CommonResponse<LoginInfo>>() {
+                            });
+                    if (commonResponse.getErrcode() == 0) {
+                        LoginInfo loginInfo = commonResponse.getData();
+                        staff.setAvatar(loginInfo.getUser_info().getObject().getAvatar());
+                        staff.setName(loginInfo.getUser_info().getObject().getNickname());
+                        staff.setUpdateTime(new Date());
+                        staffMapper.updateByPrimaryKeySelective(staff);
+                    }
+                } finally {
+                    cdl.countDown();
                 }
-                syncRoomList(uuid, staff);
-                syncStaffUserList(uuid, staff);
             });
         }
+        try {
+            cdl.await();
+        } catch (InterruptedException e) {
+            log.error("syncUserDetail error", e);
+        }
         return ReturnT.SUCCESS;
     }
 
@@ -172,8 +179,20 @@ public class WeComUserDetailJob {
     @XxlJob("syncRoomDetail")
     public ReturnT<String> syncRoomDetail(String param) {
         List<ThirdPartWeComStaff> activeStaffList = thirdPartyService.getActiveStaffList();
+        CountDownLatch cdl = new CountDownLatch(activeStaffList.size());
         for (ThirdPartWeComStaff staff : activeStaffList) {
-            pool.execute(() -> syncRoomList(staff.getThirdUuid(), staff));
+            pool.execute(() -> {
+                try {
+                    syncRoomList(staff.getThirdUuid(), staff);
+                } finally {
+                    cdl.countDown();
+                }
+            });
+        }
+        try {
+            cdl.await();
+        } catch (InterruptedException e) {
+            log.error("syncRoomDetail error", e);
         }
         return ReturnT.SUCCESS;
     }
@@ -209,10 +228,10 @@ public class WeComUserDetailJob {
                 roomDetail.setName(roomInfo.getNickname());
                 roomDetail.setUpdateTime(new Date());
                 if (staff.getAutoCreateRoom() == 1) {
-                    if (roomDetail.getMemberCount() >= memberMaxNums){
+                    if (roomDetail.getMemberCount() >= memberMaxNums) {
                         roomDetail.setAddUserStatus(0);
                     }
-                    if (roomDetail.getMemberCount() >= sendMsgStatusNums) {
+                    if (roomDetail.getMemberCount() >= sendMsgStatusNums && autoSendMsg) {
                         roomDetail.setSendStatus(1);
                     }
                 }

+ 2 - 0
api-module/src/main/java/com/tzld/piaoquan/api/service/WeComThirdPartyService.java

@@ -42,4 +42,6 @@ public interface WeComThirdPartyService {
     List<AntiSpamRuleResponse.AntiSpamRule> queryCRMAntiSpamRule(QueryCRMAntiSpamRuleRequest request);
 
     void updateRoomSendStatus(UpdateRoomSendStatusRequest request);
+
+    Long getTodayCreateRoomCount(Long staffId);
 }

+ 10 - 0
api-module/src/main/java/com/tzld/piaoquan/api/service/impl/WeComThirdPartyServiceImpl.java

@@ -15,6 +15,7 @@ import com.tzld.piaoquan.api.service.WeComThirdPartyService;
 import com.tzld.piaoquan.growth.common.dao.mapper.CorpMapper;
 import com.tzld.piaoquan.growth.common.model.po.Corp;
 import com.tzld.piaoquan.growth.common.model.po.CorpExample;
+import com.tzld.piaoquan.growth.common.utils.DateUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -22,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 import java.util.Objects;
 
@@ -369,4 +371,12 @@ public class WeComThirdPartyServiceImpl implements WeComThirdPartyService {
         return staffList.get(0);
     }
 
+    @Override
+    public Long getTodayCreateRoomCount(Long staffId) {
+        Date todayStart = DateUtil.getTodayStartDate();
+        ThirdPartWeComRoomExample example = new ThirdPartWeComRoomExample();
+        example.createCriteria().andStaffIdEqualTo(staffId).andCreateTimeBetween(todayStart, new Date());
+        return thirdPartWeComRoomMapper.countByExample(example);
+    }
+
 }

+ 10 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/utils/DateUtil.java

@@ -149,6 +149,16 @@ public class DateUtil {
         return calendar.getTime().getTime() / 1000;
     }
 
+    public static Date getTodayStartDate() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.SECOND, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        return calendar.getTime();
+    }
+
     public static Date getDate(String dateString) {
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
         LocalDate localDate = LocalDate.parse(dateString, formatter);