xueyiming 8 mesiacov pred
rodič
commit
51fb1eeea7

+ 20 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/controller/WeComController.java

@@ -2,10 +2,12 @@ package com.tzld.piaoquan.wecom.controller;
 
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.common.constant.WeComServerConstant;
+import com.tzld.piaoquan.wecom.service.UserService;
 import com.tzld.piaoquan.wecom.utils.wecom.AesException;
 import com.tzld.piaoquan.wecom.utils.wecom.WXBizMsgCrypt;
 import com.tzld.piaoquan.wecom.utils.wecom.WxUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.*;
@@ -23,8 +25,9 @@ import java.util.Map;
 @RequestMapping("/wecom/server")
 public class WeComController {
 
+
     @Autowired
-    RedisTemplate<String, Object> redisTemplate;
+    private UserService userService;
 
     @GetMapping("/verify")
     public void verifyGet(HttpServletRequest request, HttpServletResponse response) {
@@ -103,6 +106,22 @@ public class WeComController {
 
             Map suiteMap = WxUtil.transferXmlToMap(suiteXml);
             log.info("suiteMap = {}", JSONObject.toJSONString(suiteMap));
+            String changeType = (String) suiteMap.get("ChangeType");
+            if (StringUtils.isNotEmpty(changeType) && changeType.equals("add_external_contact")) {
+                String userId = (String) suiteMap.get("UserID");
+                String externalUserId = (String) suiteMap.get("ExternalUserID");
+                log.info("addStaffWithUser userId={} externalUserId={}", userId, externalUserId);
+//                userService.addStaffWithUser(externalUserId, userId);
+            }
+
+            if (StringUtils.isNotEmpty(changeType) && changeType.equals("del_follow_user")) {
+                String userId = (String) suiteMap.get("UserID");
+                String externalUserId = (String) suiteMap.get("ExternalUserID");
+                log.info("delStaffWithUser userId={} externalUserId={}", userId, externalUserId);
+//                userService.addStaffWithUser(externalUserId, userId);
+            }
+
+
         } catch (Exception e) {
             log.error("verifyPost error", e);
         }

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

@@ -34,7 +34,7 @@ public interface UserMapper {
 
     Long selectIdByExternalUserId3rdParty(String externalUserId3rdParty);
 
-    Long selectIdByExternalUserId(String externalUserId3rdParty);
+    Long selectIdByExternalUserId(String externalUserId);
 
     void insertList(@Param("list") List<User> list);
 }

+ 4 - 91
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob1.java

@@ -8,6 +8,7 @@ import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
 import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
 import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
+import com.tzld.piaoquan.wecom.service.UserService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
 import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.page.Page;
@@ -40,6 +41,8 @@ public class WeComUserDataJob1 {
     private AccessTokenService accessTokenService;
     @Autowired
     private StaffWithUserMapper staffWithUserMapper;
+    @Autowired
+    private UserService userService;
 
     @XxlJob("updateStaffWithUserJob1")
     public ReturnT<String> updateStaffWithUser(String param) {
@@ -200,85 +203,7 @@ public class WeComUserDataJob1 {
                 continue;
             }
             for (String externalUserId : externalUserIds) {
-                UserExample userExample = new UserExample();
-                userExample.createCriteria().andExternalUserIdEqualTo(externalUserId);
-                List<User> userList = userMapper.selectByExample(userExample);
-                if (CollectionUtils.isEmpty(userList)) {
-                    JSONObject userDetail = getUserDetail(externalUserId);
-                    if (userDetail == null) {
-                        continue;
-                    }
-                    JSONObject externalContact = userDetail.getJSONObject("external_contact");
-                    JSONArray followUserList = userDetail.getJSONArray("follow_user");
-                    Long createAt = null;
-                    List<String> staffUserIdList = new ArrayList<>();
-                    for (int i = 0; i < followUserList.size(); i++) {
-                        JSONObject followUser = followUserList.getJSONObject(i);
-                        if (createAt == null) {
-                            createAt = followUser.getLong("createtime");
-                        } else {
-                            createAt = Math.min(createAt, followUser.getLong("createtime"));
-                        }
-                        String userId = followUser.getString("userid");
-                        if (StringUtils.isNotEmpty(userId)) {
-                            staffUserIdList.add(userId);
-                        }
-                    }
-                    String name = externalContact.getString("name");
-                    String unionId = externalContact.getString("unionid");
-                    String avatar = externalContact.getString("avatar");
-                    Integer type = externalContact.getInteger("type");
-                    Integer gender = externalContact.getInteger("gender");
-                    User user = new User();
-                    user.setExternalUserId(externalUserId);
-                    user.setCreatedAt(createAt);
-                    user.setName(name);
-                    user.setUnionId(unionId);
-                    user.setAvatar(avatar);
-                    user.setType(type);
-                    user.setGender(gender);
-                    userMapper.insert(user);
-                    Long userId = user.getId();
-                    if (userId == null) {
-                        log.error("insertStaffWithUserJob insert user error user={}", user);
-                        continue;
-                    }
-                    if (CollectionUtils.isEmpty(staffUserIdList)) {
-                        continue;
-                    }
-                    for (String staffUserId : staffUserIdList) {
-                        StaffExample staffExample = new StaffExample();
-                        staffExample.createCriteria().andCarrierIdEqualTo(staffUserId);
-                        List<Staff> staffList1 = staffMapper.selectByExample(staffExample);
-                        if (CollectionUtils.isEmpty(staffList1)) {
-                            continue;
-                        }
-                        Staff staff1 = staffList1.get(0);
-                        Long staffId = staff1.getId();
-                        StaffWithUser staffWithUser = new StaffWithUser();
-                        staffWithUser.setStaffId(staffId);
-                        staffWithUser.setUserId(userId);
-                        staffWithUserMapper.insert(staffWithUser);
-                    }
-                } else {
-                    User user = userList.get(0);
-                    StaffWithUserExample staffWithUserExample = new StaffWithUserExample();
-                    staffWithUserExample.createCriteria().andUserIdEqualTo(user.getId()).andStaffIdEqualTo(staff.getId());
-                    List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample);
-                    if (CollectionUtils.isEmpty(staffWithUserList)) {
-                        StaffWithUser staffWithUser = new StaffWithUser();
-                        staffWithUser.setStaffId(staff.getId());
-                        staffWithUser.setUserId(user.getId());
-                        staffWithUserMapper.insert(staffWithUser);
-                    } else {
-                        StaffWithUser staffWithUser = staffWithUserList.get(0);
-                        if (staffWithUser.getIsDelete() == 1) {
-                            staffWithUser.setIsDelete(0);
-                            staffWithUserMapper.updateByPrimaryKeySelective(staffWithUser);
-                        }
-                    }
-                }
-
+                userService.insertStaffWithUser(externalUserId, staff);
             }
 
         }
@@ -299,17 +224,5 @@ public class WeComUserDataJob1 {
         return null;
     }
 
-    public JSONObject getUserDetail(String externalUserId) throws IOException {
-        String weComAccessToken = accessTokenService.getWeComAccessToken();
-        String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_GET + "?access_token=%s&external_userid=%s", weComAccessToken, externalUserId);
-        String res = httpPoolClientDefault.get(url);
-        JSONObject jsonObject = JSONObject.parseObject(res);
-        Integer errcode = jsonObject.getInteger("errcode");
-        if (errcode == 0) {
-            return jsonObject;
-        }
-        return null;
-    }
-
 
 }

+ 155 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/UserServiceImpl.java

@@ -0,0 +1,155 @@
+package com.tzld.piaoquan.wecom.service.Impl;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
+import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
+import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
+import com.tzld.piaoquan.wecom.model.po.*;
+import com.tzld.piaoquan.wecom.service.AccessTokenService;
+import com.tzld.piaoquan.wecom.service.UserService;
+import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
+import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_WE_COM_EXTERNAL_CONTACT_GET;
+
+@Slf4j
+@Service
+public class UserServiceImpl implements UserService {
+    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
+
+    @Autowired
+    private AccessTokenService accessTokenService;
+
+    @Autowired
+    private UserMapper userMapper;
+
+    @Autowired
+    private StaffWithUserMapper staffWithUserMapper;
+
+    @Autowired
+    private StaffMapper staffMapper;
+
+    public void addStaffWithUser(String externalUserId, String staffUserId) {
+        if (StringUtils.isEmpty(externalUserId) || StringUtils.isEmpty(staffUserId)) {
+            return;
+        }
+        StaffExample example = new StaffExample();
+        example.createCriteria().andCarrierIdEqualTo(staffUserId);
+        List<Staff> staffList = staffMapper.selectByExample(example);
+        if (CollectionUtils.isEmpty(staffList)) {
+            log.error("addUserWithStaff staff empty staffUserId={}", staffUserId);
+            return;
+        }
+        Staff staff = staffList.get(0);
+//        insertStaffWithUser(externalUserId, staff);
+    }
+
+
+    public void insertStaffWithUser(String externalUserId, Staff staff) {
+        try {
+            UserExample userExample = new UserExample();
+            userExample.createCriteria().andExternalUserIdEqualTo(externalUserId);
+            List<User> userList = userMapper.selectByExample(userExample);
+            if (CollectionUtils.isEmpty(userList)) {
+                JSONObject userDetail = getUserDetail(externalUserId);
+                if (userDetail == null) {
+                    return;
+                }
+                JSONObject externalContact = userDetail.getJSONObject("external_contact");
+                JSONArray followUserList = userDetail.getJSONArray("follow_user");
+                Long createAt = null;
+                List<String> staffUserIdList = new ArrayList<>();
+                for (int i = 0; i < followUserList.size(); i++) {
+                    JSONObject followUser = followUserList.getJSONObject(i);
+                    if (createAt == null) {
+                        createAt = followUser.getLong("createtime");
+                    } else {
+                        createAt = Math.min(createAt, followUser.getLong("createtime"));
+                    }
+                    String userId = followUser.getString("userid");
+                    if (StringUtils.isNotEmpty(userId)) {
+                        staffUserIdList.add(userId);
+                    }
+                }
+                String name = externalContact.getString("name");
+                String unionId = externalContact.getString("unionid");
+                String avatar = externalContact.getString("avatar");
+                Integer type = externalContact.getInteger("type");
+                Integer gender = externalContact.getInteger("gender");
+                User user = new User();
+                user.setExternalUserId(externalUserId);
+                user.setCreatedAt(createAt);
+                user.setName(name);
+                user.setUnionId(unionId);
+                user.setAvatar(avatar);
+                user.setType(type);
+                user.setGender(gender);
+                userMapper.insert(user);
+                Long userId = user.getId();
+                if (userId == null) {
+                    log.error("insertStaffWithUserJob insert user error user={}", user);
+                    return;
+                }
+                if (CollectionUtils.isEmpty(staffUserIdList)) {
+                    return;
+                }
+                for (String staffUserId : staffUserIdList) {
+                    StaffExample staffExample = new StaffExample();
+                    staffExample.createCriteria().andCarrierIdEqualTo(staffUserId);
+                    List<Staff> staffList1 = staffMapper.selectByExample(staffExample);
+                    if (CollectionUtils.isEmpty(staffList1)) {
+                        continue;
+                    }
+                    Staff staff1 = staffList1.get(0);
+                    Long staffId = staff1.getId();
+                    StaffWithUser staffWithUser = new StaffWithUser();
+                    staffWithUser.setStaffId(staffId);
+                    staffWithUser.setUserId(userId);
+                    staffWithUserMapper.insert(staffWithUser);
+                }
+            } else {
+                User user = userList.get(0);
+                StaffWithUserExample staffWithUserExample = new StaffWithUserExample();
+                staffWithUserExample.createCriteria().andUserIdEqualTo(user.getId()).andStaffIdEqualTo(staff.getId());
+                List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample);
+                if (CollectionUtils.isEmpty(staffWithUserList)) {
+                    StaffWithUser staffWithUser = new StaffWithUser();
+                    staffWithUser.setStaffId(staff.getId());
+                    staffWithUser.setUserId(user.getId());
+                    staffWithUserMapper.insert(staffWithUser);
+                } else {
+                    StaffWithUser staffWithUser = staffWithUserList.get(0);
+                    if (staffWithUser.getIsDelete() == 1) {
+                        staffWithUser.setIsDelete(0);
+                        staffWithUserMapper.updateByPrimaryKeySelective(staffWithUser);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("insertUser error", e);
+        }
+    }
+
+
+    public JSONObject getUserDetail(String externalUserId) throws IOException {
+        String weComAccessToken = accessTokenService.getWeComAccessToken();
+        String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_GET + "?access_token=%s&external_userid=%s", weComAccessToken, externalUserId);
+        String res = httpPoolClientDefault.get(url);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        Integer errcode = jsonObject.getInteger("errcode");
+        if (errcode == 0) {
+            return jsonObject;
+        }
+        return null;
+    }
+}

+ 12 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/UserService.java

@@ -0,0 +1,12 @@
+package com.tzld.piaoquan.wecom.service;
+
+import com.tzld.piaoquan.wecom.model.po.Staff;
+
+
+public interface UserService {
+
+
+    void addStaffWithUser(String externalUserId, String staffUserId);
+
+    void insertStaffWithUser(String externalUserId, Staff staff);
+}