|
@@ -0,0 +1,199 @@
|
|
|
+package com.tzld.piaoquan.risk.control.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.tzld.piaoquan.risk.control.config.QywxConfig;
|
|
|
+import com.tzld.piaoquan.risk.control.model.po.UserBase;
|
|
|
+import com.tzld.piaoquan.risk.control.model.qywx.*;
|
|
|
+import com.tzld.piaoquan.risk.control.util.HttpClientUtil;
|
|
|
+import com.tzld.piaoquan.risk.control.util.HttpPoolClient;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class RiskUserHandleService {
|
|
|
+ @Autowired
|
|
|
+ private QywxConfig qywxConfig; // 注入配置类
|
|
|
+ @Autowired
|
|
|
+ private QywxUserDataService qwUserService;
|
|
|
+ @Autowired
|
|
|
+ private RiskUserOperateService riskUserOperateService;
|
|
|
+ @Value("${qywx.corpid}")
|
|
|
+ private String corpid;
|
|
|
+ @Value("${qywx.scorpid}")
|
|
|
+ private String scorpid;
|
|
|
+ private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(10000, 10000, 2000, 5000, 5, 10000);
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(RiskUserHandleService.class);
|
|
|
+
|
|
|
+
|
|
|
+ public void handleRiskUser(RiskUserInfo riskUserInfo){
|
|
|
+ log.info("handleRiskUser, riskUserInfo: {}", riskUserInfo);
|
|
|
+ //根据名称找到人
|
|
|
+ List<UserBase> staffList = findStaffByName(riskUserInfo);
|
|
|
+ if (staffList.isEmpty()) return;
|
|
|
+ //根据群名匹配到:人-群:哪个员工哪个群
|
|
|
+ Map<String, List<RoomListResponse.RoomInfo>> toBeOperate = matchUserAndRoom(staffList, riskUserInfo);
|
|
|
+ if (toBeOperate.isEmpty()) return;
|
|
|
+ log.info("handleRiskUser, toBeOperate: {}", toBeOperate);
|
|
|
+ //TODO:检查过滤规则是否配置
|
|
|
+ //找到待踢的人,找到终止
|
|
|
+ for (Map.Entry<String, List<RoomListResponse.RoomInfo>> entry : toBeOperate.entrySet()) {
|
|
|
+ String staffVid = entry.getKey(); // Key: staff vid内部员工
|
|
|
+ UserBase staff = findStaffByVid(staffList, staffVid);
|
|
|
+ List<RoomListResponse.RoomInfo> roomInfoList = entry.getValue(); // Value: List of RoomInfo
|
|
|
+ // Process each room in the list
|
|
|
+ long externalVid = externalId2Vid(staff, riskUserInfo);
|
|
|
+ if (externalVid == -1) {
|
|
|
+ log.info("handleRiskUser, externalId2Vid error, staff: {}, riskUserInfo: {}", staff, riskUserInfo);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (RoomListResponse.RoomInfo roomInfo : roomInfoList) {
|
|
|
+ boolean success = riskUserOperateService.checkAndKickExternalUser(staff, riskUserInfo, externalVid,Long.parseLong(roomInfo.getRoomId()));
|
|
|
+ if (success) {
|
|
|
+ log.info("handleRiskUser to be kick user, vid: {},name: {}", externalVid,riskUserInfo.getExternalNickname());
|
|
|
+ return;
|
|
|
+ //kick(staff, Long.parseLong(roomInfo.getRoomId()), externalVid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserBase findStaffByVid(List<UserBase> staffList, String vid) {
|
|
|
+ for (UserBase userBase : staffList) {
|
|
|
+ if (userBase.getVid().equals(vid)) {
|
|
|
+ return userBase;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, List<RoomListResponse.RoomInfo>> matchUserAndRoom(List<UserBase> staffList, RiskUserInfo userInfo) {
|
|
|
+ Map<String, List<RoomListResponse.RoomInfo>> toBeOperate = new HashMap<>();
|
|
|
+ if (staffList != null && !staffList.isEmpty()) {
|
|
|
+ log.info("handleRiskUser hits, userInfo: {}", userInfo);
|
|
|
+ for (UserBase userBase : staffList) {
|
|
|
+ //获取匹配到每个人的群信息
|
|
|
+ List<RoomListResponse.RoomInfo> chatRoomList = getChatList(userBase);
|
|
|
+ if (chatRoomList != null && !chatRoomList.isEmpty()) {
|
|
|
+ log.info("handleRiskUserList hits, userInfo: {}", userBase);
|
|
|
+ for (RoomListResponse.RoomInfo roomInfo : chatRoomList) {
|
|
|
+ List<RoomListResponse.RoomInfo> matched = findRoomByChatRoomName(chatRoomList, userInfo);
|
|
|
+ if (matched != null && !matched.isEmpty()) {
|
|
|
+ log.info("handleRiskUserList hits, userInfo: {}", userBase);
|
|
|
+ toBeOperate.put(userBase.getVid(), matched);
|
|
|
+ } else {
|
|
|
+ log.info("handleRiskUserList, roomInfo: {}", roomInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return toBeOperate;
|
|
|
+ }
|
|
|
+
|
|
|
+ //每个人有几个匹配到的群
|
|
|
+ public List<RoomListResponse.RoomInfo> findRoomByChatRoomName(List<RoomListResponse.RoomInfo> roomInfoList, RiskUserInfo riskInfo) {
|
|
|
+ List<RoomListResponse.RoomInfo> matchedRoomList = new ArrayList<>();
|
|
|
+ for (RoomListResponse.RoomInfo roomInfo : roomInfoList) {
|
|
|
+ if (riskInfo != null && riskInfo.getGroupName().equals(roomInfo.getNickname())) {
|
|
|
+ matchedRoomList.add(roomInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return matchedRoomList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<RoomListResponse.RoomInfo> getChatList(UserBase userBase) {
|
|
|
+ List<RoomListResponse.RoomInfo> chatRoomList = new ArrayList<>();
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("uuid", userBase.getUuid());
|
|
|
+ requestBody.put("limit", 100);
|
|
|
+ requestBody.put("star_index", 0);
|
|
|
+ LOGGER.info("getChatList, userBase: {}", userBase);
|
|
|
+ String params = JSON.toJSONString(requestBody);
|
|
|
+ Optional<String> response = httpPoolClientDefault.postJson(qywxConfig.getDomain() + qywxConfig.getPath("get-chatList"), params);
|
|
|
+ QwCommonResModel<RoomListResponse> roomList = null;
|
|
|
+ if (response.isPresent()) {
|
|
|
+ roomList = QwCommonResModel.parseResponse(response.get(), RoomListResponse.class);
|
|
|
+ }
|
|
|
+ if (roomList != null && roomList.getErrcode() == 0) {
|
|
|
+ if (roomList.getData().getRoomList() != null && !roomList.getData().getRoomList().isEmpty()) {
|
|
|
+ chatRoomList = roomList.getData().getRoomList();
|
|
|
+ log.info("handleRiskUserList hits, userInfo: {}", userBase);
|
|
|
+ } else {
|
|
|
+ log.info("handleRiskUserList not hits, userInfo: {}", userBase);
|
|
|
+ }
|
|
|
+ log.info("handleRiskUserList, roomList size: {}", roomList.getData().getRoomList().size());
|
|
|
+ }
|
|
|
+ return chatRoomList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<UserBase> findStaffByName(RiskUserInfo userInfo) {
|
|
|
+ List<UserBase> userBaseList = new ArrayList<>();
|
|
|
+ if (userInfo != null ) {
|
|
|
+ List<RiskUserInfo.Admin> adminList = userInfo.getAdminList();
|
|
|
+ if (adminList != null && !adminList.isEmpty()) {
|
|
|
+ for (RiskUserInfo.Admin admin : adminList) {
|
|
|
+ String name = admin.getNickname();
|
|
|
+ if (name != null && !name.isEmpty()) {
|
|
|
+ userBaseList.addAll(qwUserService.getUserByNickName(name));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!userBaseList.isEmpty()) {
|
|
|
+ log.info("handleRiskUserList hits, userInfo: {}", userInfo);
|
|
|
+ }
|
|
|
+ return userBaseList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<RoomMemberListInfo.Member> getRoomMemberList(UserBase staff,long roomId) {
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("uuid", staff.getUuid());
|
|
|
+ requestBody.put("roomId", roomId);
|
|
|
+ LOGGER.info("getRoomMemberList, staff: {}", staff);
|
|
|
+ String params = JSON.toJSONString(requestBody);
|
|
|
+ Optional<String> response = httpPoolClientDefault.postJson(qywxConfig.getDomain() + qywxConfig.getPath("get-roomMembers"), params);
|
|
|
+ List<RoomMemberListInfo.Member> memberList = new ArrayList<>();
|
|
|
+ if (response.isPresent()) {
|
|
|
+ QwCommonResModel<RoomMemberListInfo> memberInfo = QwCommonResModel.parseResponse(response.get(), RoomMemberListInfo.class);
|
|
|
+ if (memberInfo.getErrcode() == 0) {
|
|
|
+ memberList = memberInfo.getData().getMemberList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return memberList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private long externalId2Vid(UserBase staff,RiskUserInfo riskinfo) {
|
|
|
+ log.info("externalId2Vid, staff: {}, riskinfo: {}", staff, riskinfo);
|
|
|
+ Map<String, Object> requestBody = new HashMap<>();
|
|
|
+ requestBody.put("uuid", staff.getUuid());
|
|
|
+ requestBody.put("corpid", corpid);
|
|
|
+ requestBody.put("scorpid", scorpid);
|
|
|
+ requestBody.put("openid",Arrays.asList(riskinfo.getExternalId()));
|
|
|
+ String params = JSON.toJSONString(requestBody);
|
|
|
+ Optional<String> response = httpPoolClientDefault.postJson(qywxConfig.getDomain() + qywxConfig.getPath("userId2Vid"), params);
|
|
|
+ log.info("externalId2Vid, response: {}", response);
|
|
|
+ if (response.isPresent()) {
|
|
|
+ QwCommonResModel<List<OpenUserInfo>> result = JSON.parseObject(
|
|
|
+ response.get(),
|
|
|
+ new TypeReference<QwCommonResModel<List<OpenUserInfo>>>() {}.getType()
|
|
|
+ );
|
|
|
+ if (result.getErrcode() == 0 && result.getData() != null && !result.getData().isEmpty()) {
|
|
|
+ return result.getData().get(0).getUserId();
|
|
|
+ } else {
|
|
|
+ log.error("externalId2Vid error, response: {}", response.get());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.error("externalId2Vid error, no response");
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|