Przeglądaj źródła

企业微信成员事件

zoujiajian 2 lat temu
rodzic
commit
4c9ea407cd

+ 19 - 0
netflix-common/src/main/java/com/cyksj/common/util/StringUtil.java

@@ -571,4 +571,23 @@ public final class StringUtil {
 		}
 		return StrUtil.EMPTY;
 	}
+
+	public static <T> List<List<T>> averageAssign(List<T> source, int n) {
+		List<List<T>> result = new ArrayList<List<T>>();
+		int remainder = source.size() % n;  //(先计算出余数)
+		int number = source.size() / n;  //然后是商
+		int offset = 0;//偏移量
+		for (int i = 0; i < n; i++) {
+			List<T> value = null;
+			if (remainder > 0) {
+				value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
+				remainder--;
+				offset++;
+			} else {
+				value = source.subList(i * number + offset, (i + 1) * number + offset);
+			}
+			result.add(value);
+		}
+		return result;
+	}
 }

+ 68 - 0
netflix-service/src/main/java/com/cyksj/service/corp/impl/CorpEventActionServiceImpl.java

@@ -137,6 +137,8 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 
 	private final CorpCustomerAcquistionCallbackMapper corpCustomerAcquistionCallbackMapper;
 
+	private final CorpFollowMapper corpFollowMapper;
+
 	private static final String DEFAULT_MSG = "您好,欢迎加入银河录像局";
 
 	/**
@@ -593,6 +595,30 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 		return null;
 	}
 
+	@Override
+	public CorpXmlOutMessage creatOrUpdateFollowUser(CorpXmlMessage message) throws Exception {
+		//corpId
+		String corpId = message.getToUserName();
+		//成员id
+		String userId = message.getUserId();
+
+		Long wxCorpId = WeChatCorpFactory.getCorpConfigStrategy(corpId).getId();
+		CorpFollow corpFollow = corpFollowMapper.selectOne(Wrappers.lambdaQuery(CorpFollow.class).eq(CorpFollow::getWxCorpId, wxCorpId).eq(CorpFollow::getUserid, userId));
+		CorpUserInfo userInfo = wxCorpOps.getFollowUser(corpId, userId);
+		if (corpFollow == null) {
+			corpFollow = new CorpFollow();
+			BeanUtil.copyProperties(userInfo, corpFollow);
+			corpFollow.setWxCorpId(wxCorpId);
+			corpFollowMapper.insert(corpFollow);
+			//同步客户列表
+			getCorpUserInfo(corpId, corpFollow.getUserid());
+		} else {
+			BeanUtil.copyProperties(userInfo, corpFollow);
+			corpFollowMapper.updateById(corpFollow);
+		}
+		return null;
+	}
+
 	/**
 	 * 修改客户成员关系,客户标签
 	 */
@@ -1227,4 +1253,46 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 		}
 		return corpWelcomeTemplate;
 	}
+
+	public void getCorpUserInfo(String corpId, String userId) {
+		THREAD_POOL.execute(() -> {
+			List<String> externalList = null;
+			try {
+				externalList = wxCorpOps.listExternalContacts(corpId, userId);
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+			if (externalList.size() > 1000) {
+				List<List<String>> lists = StringUtil.averageAssign(externalList, 5);
+				for (List<String> list : lists) {
+					THREAD_POOL.execute(() -> {
+						for (String externalUserid : list) {
+							try {
+								log.info("同步客户数据 corpId:{},externalUserid:{}", corpId, externalUserid);
+								//尝试添加客户
+								CorpXmlMessage message = new CorpXmlMessage();
+								message.setToUserName(corpId);
+								message.setExternalUserId(externalUserid);
+								addExternalContact(message, null);
+							} catch (Exception e) {
+								e.printStackTrace();
+							}
+						}
+					});
+				}
+			} else {
+				for (String externalUserid : externalList) {
+					try {
+						//尝试添加客户
+						CorpXmlMessage message = new CorpXmlMessage();
+						message.setToUserName(corpId);
+						message.setExternalUserId(externalUserid);
+						addExternalContact(message, null);
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+				}
+			}
+		});
+	}
 }

+ 18 - 0
netflix-service/src/main/java/com/cyksj/service/corp/impl/CorpServiceImpl.java

@@ -190,6 +190,24 @@ public class CorpServiceImpl implements CorpService {
 					}
 				})
 				.end()
+				//新增成员
+				.rule().async(false).changeType("create_user")
+				.handler(new CorpMessageHandler() {
+					@Override
+					public CorpXmlOutMessage handle(CorpXmlMessage message, Map<String, Object> context, CorpService corpService) throws Exception {
+						return corpEventActionService.creatOrUpdateFollowUser(message);
+					}
+				})
+				.end()
+				//成员更新事件
+				.rule().async(false).changeType("update_user")
+				.handler(new CorpMessageHandler() {
+					@Override
+					public CorpXmlOutMessage handle(CorpXmlMessage message, Map<String, Object> context, CorpService corpService) throws Exception {
+						return corpEventActionService.creatOrUpdateFollowUser(message);
+					}
+				})
+				.end()
 		;
 
 	}

+ 2 - 0
netflix-service/src/main/java/com/cyksj/service/corp/msg/CorpEventActionService.java

@@ -33,4 +33,6 @@ public interface CorpEventActionService {
     CorpXmlOutMessage msgAuditApproved(CorpXmlMessage message, Map<String, Object> context);
 
 	CorpXmlOutMessage customerStartChat(CorpXmlMessage message, Map<String, Object> context);
+
+	CorpXmlOutMessage creatOrUpdateFollowUser(CorpXmlMessage message) throws Exception;
 }