Selaa lähdekoodia

批量处理数据修改

xueyiming 7 kuukautta sitten
vanhempi
commit
afa1263d60

+ 25 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/component/filter/HistoryFilter.java

@@ -0,0 +1,25 @@
+package com.tzld.piaoquan.wecom.component.filter;
+
+
+import org.springframework.stereotype.Component;
+
+import java.util.HashSet;
+import java.util.Set;
+
+@Component
+public class HistoryFilter {
+
+    public static Set<Long> filterHistorySet(Set<Long>... sets) {
+        if (sets.length == 0) {
+            return new HashSet<>();
+        }
+        Set<Long> result = new HashSet<>(sets[0]);
+
+        for (int i = 1; i < sets.length; i++) {
+            result.retainAll(sets[i]);
+        }
+
+        return result;
+    }
+
+}

+ 2 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/dao/mapper/HistoryMessageMapper.java

@@ -33,4 +33,6 @@ public interface HistoryMessageMapper {
 
     int updateByPrimaryKey(HistoryMessage record);
 
+    int insertList(@Param("list") List<HistoryMessage> list);
+
 }

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

@@ -2,9 +2,10 @@ package com.tzld.piaoquan.wecom.dao.mapper;
 
 import com.tzld.piaoquan.wecom.model.po.User;
 import com.tzld.piaoquan.wecom.model.po.UserExample;
-import java.util.List;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 public interface UserMapper {
     long countByExample(UserExample example);
 
@@ -29,4 +30,6 @@ public interface UserMapper {
     int updateByPrimaryKey(User record);
 
     Long selectIdByExternalUserId3rdParty(String externalUserId3rdParty);
+
+    int insertList(@Param("list") List<User> list);
 }

+ 0 - 3
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -17,7 +17,6 @@ import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 import java.io.IOException;
-import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -31,8 +30,6 @@ public class WeComHistoryDataJob {
 
     String GET_HISTORY_DATA_URL = "https://open.weibanzhushou.com/open-api/group_msg/result";
 
-    String ACCESS_TOKEN = "b41281675156a3355c2f4d61398a24d3";
-
     private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
 
     final int size = 20;

+ 6 - 18
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
 @Component
@@ -23,8 +24,6 @@ public class WeComUserDataJob {
 
     String UPDATE_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/update/list";
 
-    String ACCESS_TOKEN = "38969e430219e6b18f88792184a37b11";
-
     private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
 
     final int size = 100;
@@ -53,19 +52,16 @@ public class WeComUserDataJob {
                 }
                 JSONObject jsonObject = JSONObject.parseObject(res);
                 JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
+                List<User> userList = new ArrayList<>();
                 for (int j = 0; j < jsonArray.size(); j++) {
                     String id = (String) jsonArray.getJSONObject(j).get("id");
                     jsonArray.getJSONObject(j).put("id", null);
                     User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
                     user.setExternalUserId3rdParty(id);
-                    //TODO 记录对象数据  info日志
-                    int insert = userMapper.insert(user);
-                    if (insert <= 0) {
-                        //TODO  异常日志
-                        continue;
-                    }
+                    userList.add(user);
                     sum++;
                 }
+                userMapper.insertList(userList);
                 if (jsonArray.size() < size) {
                     if (total > sum) {
                         //TODO 输出异常  插入数量不足
@@ -123,19 +119,11 @@ public class WeComUserDataJob {
                     List<User> list = userMapper.selectByExample(example);
                     if (CollectionUtils.isEmpty(list)) {
                         //没有用户,走插入逻辑
-                        int insert = userMapper.insert(user);
-                        if (insert <= 0) {
-                            //TODO  异常日志
-                            continue;
-                        }
+                        userMapper.insert(user);
                     } else {
                         User oldUser = list.get(0);
                         user.setId(oldUser.getId());
-                        int update = userMapper.updateByPrimaryKeySelective(user);
-                        if (update < 0) {
-                            //TODO  异常日志
-                            continue;
-                        }
+                        userMapper.updateByPrimaryKeySelective(user);
                     }
                     sum++;
                     if (jsonArray.size() < size) {

+ 1 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/HistoryMessageService.java

@@ -6,5 +6,5 @@ import java.util.List;
 
 public interface HistoryMessageService {
 
-    boolean batchInsertHistoryMessage(List<HistoryMessage> historyMessageList);
+    void batchInsertHistoryMessage(List<HistoryMessage> historyMessageList);
 }

+ 9 - 12
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/HistoryMessageServiceImpl.java

@@ -15,19 +15,16 @@ public class HistoryMessageServiceImpl implements HistoryMessageService {
     private HistoryMessageMapper historyMessageMapper;
 
     @Override
-    public boolean batchInsertHistoryMessage(List<HistoryMessage> historyMessageList) {
-        for (HistoryMessage historyMessage : historyMessageList) {
-            //处理异常  保证大部分数据处理成功
-            try {
-                int insert = historyMessageMapper.insert(historyMessage);
-                if (insert <= 0) {
-                    //TODO 记录异常日志
-                }
-            } catch (Exception e) {
-                //TODO 输出异常信息 记录插入失败信息 historyMessage
-                e.printStackTrace();
+    public void batchInsertHistoryMessage(List<HistoryMessage> historyMessageList) {
+        //处理异常  保证大部分数据处理成功
+        try {
+            int insert = historyMessageMapper.insertList(historyMessageList);
+            if (insert <= 0) {
+                //TODO 记录异常日志
             }
+        } catch (Exception e) {
+            //TODO 输出异常信息 记录插入失败信息 historyMessage
+            e.printStackTrace();
         }
-        return true;
     }
 }

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

@@ -6,20 +6,12 @@ import com.tzld.piaoquan.wecom.service.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.List;
-
 @Service
 public class UserServiceImpl implements UserService {
 
     @Autowired
     private UserMapper userMapper;
 
-    @Override
-    public boolean addUser(User user) {
-        int insert = userMapper.insert(user);
-        return insert > 0;
-    }
-
     @Override
     public Long getIdByExternalUserId3rdParty(String externalUserId3rdParty) {
         return userMapper.selectIdByExternalUserId3rdParty(externalUserId3rdParty);

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

@@ -4,7 +4,5 @@ import com.tzld.piaoquan.wecom.model.po.User;
 
 public interface UserService {
 
-    boolean addUser(User user);
-
     Long getIdByExternalUserId3rdParty(String externalUserId3rdParty);
 }

+ 22 - 0
we-com-server/src/main/resources/mapper/HistoryMessageMapper.xml

@@ -242,4 +242,26 @@
     where id = #{id,jdbcType=BIGINT}
   </update>
 
+  <insert id="insertList" parameterType="java.util.List">
+    insert into we_com_history_message
+    (
+     user_id,
+     video_id,
+     attachment_idx,
+     message_id,
+     send_time,
+     create_time
+    )
+    values
+    <foreach collection="list" item="historyMessage" separator=",">
+      (
+       #{userId,jdbcType=BIGINT},
+       #{videoId,jdbcType=BIGINT},
+       #{attachmentIdx,jdbcType=INTEGER},
+       #{messageId,jdbcType=BIGINT},
+       #{sendTime,jdbcType=TIMESTAMP},
+       #{createTime,jdbcType=TIMESTAMP})
+    </foreach>
+  </insert>
+
 </mapper>

+ 33 - 0
we-com-server/src/main/resources/mapper/UserMapper.xml

@@ -343,4 +343,37 @@
     from we_com_user
     where external_user_id_3rd_party = #{externalUserId3rdParty}
   </select>
+
+  <insert id="insertList" parameterType="java.util.List">
+    insert into we_com_user
+    (
+     external_user_id,
+     union_id,
+     external_user_id_3rd_party,
+     `type`,
+     `name`,
+     avatar,
+     gender,
+     created_at,
+     updated_at,
+     create_time,
+     update_time
+    )
+    values
+    <foreach collection="list" item="user" separator=",">
+      (
+       #{externalUserId,jdbcType=VARCHAR},
+       #{unionId,jdbcType=VARCHAR},
+       #{externalUserId3rdParty,jdbcType=VARCHAR},
+       #{type,jdbcType=INTEGER},
+       #{name,jdbcType=VARCHAR},
+       #{avatar,jdbcType=VARCHAR},
+       #{gender,jdbcType=INTEGER},
+       #{createdAt,jdbcType=BIGINT},
+       #{updatedAt,jdbcType=BIGINT},
+       #{createTime,jdbcType=TIMESTAMP},
+       #{updateTime,jdbcType=TIMESTAMP}
+      )
+    </foreach>
+  </insert>
 </mapper>