Jelajahi Sumber

Merge branch 'dev-xym-addTag' of Server/growth-manager into master

xueyiming 3 bulan lalu
induk
melakukan
b98f79f192

+ 29 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/WeComStaffServiceImpl.java

@@ -0,0 +1,29 @@
+package com.tzld.piaoquan.growth.common.service.Impl;
+
+import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
+import com.tzld.piaoquan.growth.common.model.po.Staff;
+import com.tzld.piaoquan.growth.common.model.po.StaffExample;
+import com.tzld.piaoquan.growth.common.service.WeComStaffService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.List;
+
+@Service
+public class WeComStaffServiceImpl implements WeComStaffService {
+
+    @Autowired
+    private StaffMapper staffMapper;
+
+    @Override
+    public Long getStaffIdByCarrierId(String carrierId) {
+        StaffExample example = new StaffExample();
+        example.createCriteria().andCarrierIdEqualTo(carrierId);
+        List<Staff> staffs = staffMapper.selectByExample(example);
+        if (CollectionUtils.isEmpty(staffs)) {
+            return null;
+        }
+        return staffs.get(0).getId();
+    }
+}

+ 6 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/WeComStaffService.java

@@ -0,0 +1,6 @@
+package com.tzld.piaoquan.growth.common.service;
+
+public interface WeComStaffService {
+
+    Long getStaffIdByCarrierId(String carrierId);
+}

+ 36 - 20
offline-module/src/main/java/com/tzld/piaoquan/offline/job/WeComUserDataJob.java

@@ -7,6 +7,7 @@ import com.tzld.piaoquan.growth.common.dao.mapper.*;
 import com.tzld.piaoquan.growth.common.model.bo.XxlJobParam;
 import com.tzld.piaoquan.growth.common.model.po.*;
 import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
+import com.tzld.piaoquan.growth.common.service.WeComStaffService;
 import com.tzld.piaoquan.growth.common.service.WeComUserService;
 import com.tzld.piaoquan.growth.common.utils.DateUtil;
 import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
@@ -19,6 +20,7 @@ import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -60,6 +62,9 @@ public class WeComUserDataJob {
     @Autowired
     private UserWithTagMapper userWithTagMapper;
 
+    @Autowired
+    private WeComStaffService weComStaffService;
+
     @XxlJob("updateStaffWithUserJob")
     public ReturnT<String> updateStaffWithUser(String param) {
         XxlJobParam xxlJobParam = new XxlJobParam();
@@ -348,34 +353,45 @@ public class WeComUserDataJob {
                     for (String externalUserId : existExternalUserIds) {
                         JSONObject userDetail = weComUserService.getUserDetail(externalUserId, corp.getId());
                         JSONObject externalContact = userDetail.getJSONObject("external_contact");
-                        JSONObject followInfo = userDetail.getJSONObject("follow_info");
-                        Long createAt = followInfo.getLong("createtime");
-                        JSONArray tagIds = followInfo.getJSONArray("tag_id");
-                        if (!tagIds.isEmpty()) {
-                            List<String> tagIdList = tagIds.toJavaList(String.class);
-                            for (String tagId : tagIdList) {
-                                TagExample example = new TagExample();
-                                example.createCriteria().andTagIdEqualTo(tagId);
-                                List<Tag> tags = tagMapper.selectByExample(example);
-                                if (!CollectionUtils.isEmpty(tags)) {
-                                    Long userId = weComUserMapper.selectIdByExternalUserId(externalUserId);
-                                    if (userId == null) {
-                                        userId = addUser(externalContact, corp.getId(), externalUserId, createAt, staff.getId());
+                        JSONArray followUserList = userDetail.getJSONArray("follow_user");
+                        Long createAt = null;
+                        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"));
+                            }
+                            JSONArray tagIds = followUser.getJSONArray("tags");
+                            if (!CollectionUtils.isEmpty(tagIds)) {
+                                List<String> tagIdList = new ArrayList<>();
+                                for (int j = 0; j < tagIds.size(); j++) {
+                                    JSONObject jsonObject = tagIds.getJSONObject(j);
+                                    tagIdList.add(jsonObject.getString("tag_id"));
+                                }
+                                for (String tagId : tagIdList) {
+                                    TagExample example = new TagExample();
+                                    example.createCriteria().andTagIdEqualTo(tagId);
+                                    List<Tag> tags = tagMapper.selectByExample(example);
+                                    if (!CollectionUtils.isEmpty(tags)) {
+                                        Long userId = weComUserMapper.selectIdByExternalUserId(externalUserId);
+                                        if (userId == null) {
+                                            userId = addUser(externalContact, corp.getId(), externalUserId, createAt, staff.getId());
+                                        }
+                                        Tag tag = tags.get(0);
+                                        UserWithTag userWithTag = new UserWithTag();
+                                        userWithTag.setUserId(userId);
+                                        userWithTag.setTagId(tag.getId());
+                                        addUserWithTag(userWithTag);
                                     }
-                                    Tag tag = tags.get(0);
-                                    UserWithTag userWithTag = new UserWithTag();
-                                    userWithTag.setUserId(userId);
-                                    userWithTag.setTagId(tag.getId());
-                                    addUserWithTag(userWithTag);
                                 }
                             }
                         }
-
                     }
                 }
             }
         } catch (Exception e) {
-            LarkRobotUtil.sendMessage("insertStaffWithUserJob error" + e);
+//            LarkRobotUtil.sendMessage("insertStaffWithUserJob error" + e);
             log.error("insertStaffWithUserJob error", e);
         }