瀏覽代碼

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

xueyiming 2 月之前
父節點
當前提交
6f2d869aac

+ 67 - 3
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/WeComUserServiceImpl.java

@@ -408,6 +408,9 @@ public class WeComUserServiceImpl implements WeComUserService {
     }
 
     private boolean addWeComTag(Tag tag, WeComUser weComUser) {
+        if (tag == null || weComUser == null) {
+            return false;
+        }
         try {
             String accessToken = weComAccessTokenService.getWeComAccessToken(weComUser.getCorpId());
             JSONArray jsonArray = new JSONArray();
@@ -436,10 +439,17 @@ public class WeComUserServiceImpl implements WeComUserService {
     public boolean addUserWithTag(UserWithTag userWithTag) {
         try {
             UserWithTagExample userWithTagExample = new UserWithTagExample();
-            userWithTagExample.createCriteria().andUserIdEqualTo(userWithTag.getUserId()).andTagIdEqualTo(userWithTag.getTagId());
-            long l = userWithTagMapper.countByExample(userWithTagExample);
-            if (l == 0) {
+            userWithTagExample.createCriteria().andUserIdEqualTo(userWithTag.getUserId())
+                    .andTagIdEqualTo(userWithTag.getTagId());
+            List<UserWithTag> userWithTags = userWithTagMapper.selectByExample(userWithTagExample);
+            if (CollectionUtils.isEmpty(userWithTags)) {
                 userWithTagMapper.insertSelective(userWithTag);
+            } else {
+                UserWithTag userWithTag1 = userWithTags.get(0);
+                if (userWithTag1.getIsDelete() == 1) {
+                    userWithTag1.setIsDelete(0);
+                    userWithTagMapper.updateByPrimaryKeySelective(userWithTag1);
+                }
             }
             return true;
         } catch (Exception e) {
@@ -447,4 +457,58 @@ public class WeComUserServiceImpl implements WeComUserService {
         }
         return false;
     }
+
+    public boolean delAllUserTag(Long userId) {
+        try {
+            UserWithTagExample userWithTagExample = new UserWithTagExample();
+            userWithTagExample.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(1);
+            UserWithTag record = new UserWithTag();
+            record.setIsDelete(1);
+            userWithTagMapper.updateByExampleSelective(record, userWithTagExample);
+            return true;
+        } catch (Exception e) {
+            log.error("addUserWithTag error", e);
+        }
+        return false;
+    }
+
+    public void updateUserWithTag(Long userId, List<String> tagIdList) {
+        try {
+            TagExample example = new TagExample();
+            example.createCriteria().andTagIdIn(tagIdList).andIsDeleteEqualTo(0);
+            List<Tag> tags = tagMapper.selectByExample(example);
+            List<Long> tagIds = tags.stream().map(Tag::getId).collect(Collectors.toList());
+            UserWithTagExample userWithTagExample = new UserWithTagExample();
+            userWithTagExample.createCriteria().andUserIdEqualTo(userId).andIsDeleteEqualTo(0);
+            List<UserWithTag> userWithTags = userWithTagMapper.selectByExample(userWithTagExample);
+            List<Long> userTagIds = userWithTags.stream().map(UserWithTag::getTagId).collect(Collectors.toList());
+            userTagIds.removeAll(tagIds);
+            if (!CollectionUtils.isEmpty(userTagIds)) {
+                List<UserWithTag> collect = userWithTags.stream()
+                        .filter(f -> userTagIds.contains(f.getTagId())).collect(Collectors.toList());
+                for (UserWithTag userWithTag : collect) {
+                    if (userWithTag.getStatus() == 2) {
+                        WeComUser weComUser = weComUserMapper.selectByPrimaryKey(userWithTag.getUserId());
+                        Tag tag = tagMapper.selectByPrimaryKey(userWithTag.getTagId());
+                        addWeComTag(tag, weComUser);
+                    } else {
+                        userWithTag.setIsDelete(1);
+                        userWithTagMapper.updateByPrimaryKeySelective(userWithTag);
+                    }
+                }
+            }
+            List<Long> hasUserTagIds = userWithTags.stream().map(UserWithTag::getTagId).collect(Collectors.toList());
+            tagIds.removeAll(hasUserTagIds);
+            if (!CollectionUtils.isEmpty(tagIds)) {
+                for (Long tagId : tagIds) {
+                    UserWithTag userWithTag = new UserWithTag();
+                    userWithTag.setTagId(tagId);
+                    userWithTag.setUserId(userId);
+                    addUserWithTag(userWithTag);
+                }
+            }
+        } catch (Exception e) {
+            log.error("updateUserWithTag error", e);
+        }
+    }
 }

+ 4 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/WeComUserService.java

@@ -39,4 +39,8 @@ public interface WeComUserService {
     CommonResponse<Void> addUserTag(UserTagParam userTagParam);
 
     boolean addUserWithTag(UserWithTag userWithTag);
+
+    boolean delAllUserTag(Long userId);
+
+    void updateUserWithTag(Long userId, List<String> tagIdList);
 }

+ 19 - 16
offline-module/src/main/java/com/tzld/piaoquan/offline/job/WeComUserDataJob.java

@@ -257,7 +257,7 @@ public class WeComUserDataJob {
             date = param;
         }
         for (Staff staff : staffList) {
-            Map<String, Integer> userCountRes = getUserList(staff, 1L, date);
+            Map<String, Integer> userCountRes = getUserList(staff, date);
             if (userCountRes == null || userCountRes.get("count") == 0) {
                 continue;
             }
@@ -283,14 +283,14 @@ public class WeComUserDataJob {
     }
 
 
-    private Map<String, Integer> getUserList(Staff staff, Long corpId, String date) {
+    private Map<String, Integer> getUserList(Staff staff, String date) {
         Map<String, Integer> result = new HashMap<>();
         int count = 0;
         int newCount = 0;
         try {
             String cursor = "";
             do {
-                String res = getUserDetailList(staff.getCarrierId(), cursor, corpId);
+                String res = getUserDetailList(staff.getCarrierId(), cursor, staff.getCorpId());
                 log.info("getUserList res={} cursor={}", res, cursor);
                 JSONObject jsonObject = JSONObject.parseObject(res);
                 Integer errCode = jsonObject.getInteger("errcode");
@@ -310,22 +310,25 @@ public class WeComUserDataJob {
                     }
                     count++;
                     Long userId = weComUserMapper.selectIdByExternalUserId(externalUserId);
-                    userId = addAndUpdateUser(externalContact, corpId, externalUserId, createAt, staff.getId(), userId);
+                    userId = addAndUpdateUser(externalContact, staff.getCorpId(), externalUserId, createAt, staff.getId(), userId);
                     JSONArray tagIds = followInfo.getJSONArray("tag_id");
                     if (!CollectionUtils.isEmpty(tagIds)) {
                         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)) {
-                                Tag tag = tags.get(0);
-                                UserWithTag userWithTag = new UserWithTag();
-                                userWithTag.setUserId(userId);
-                                userWithTag.setTagId(tag.getId());
-                                weComUserService.addUserWithTag(userWithTag);
-                            }
-                        }
+                        weComUserService.updateUserWithTag(userId, tagIdList);
+//                        for (String tagId : tagIdList) {
+//                            TagExample example = new TagExample();
+//                            example.createCriteria().andTagIdEqualTo(tagId);
+//                            List<Tag> tags = tagMapper.selectByExample(example);
+//                            if (!CollectionUtils.isEmpty(tags)) {
+//                                Tag tag = tags.get(0);
+//                                UserWithTag userWithTag = new UserWithTag();
+//                                userWithTag.setUserId(userId);
+//                                userWithTag.setTagId(tag.getId());
+//                                weComUserService.addUserWithTag(userWithTag);
+//                            }
+//                        }
+                    } else {
+                        weComUserService.delAllUserTag(userId);
                     }
                 }
                 String nextCursor = jsonObject.getString("next_cursor");