Forráskód Böngészése

批量减少分组群发剩余次数

wangyunpeng 1 hete
szülő
commit
dae9058a8f

+ 8 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/dao/mapper/crawler/ArticleUseGroupMapper.java

@@ -38,4 +38,12 @@ public interface ArticleUseGroupMapper {
 
     Integer selectFansBeforePublishCount(@Param("ghId") String ghId);
 
+    /**
+     * 批量减少剩余次数
+     * @param gzhId 公众号ID
+     * @param openIdList openId列表
+     * @return 更新条数
+     */
+    int batchDecreaseRemainingCount(@Param("gzhId") String gzhId, @Param("openIdList") List<String> openIdList);
+
 }

+ 6 - 13
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/local/impl/CoreServiceImpl.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.google.common.collect.Lists;
 import com.tzld.piaoquan.longarticle.common.constants.TimeConstant;
 import com.tzld.piaoquan.longarticle.common.enums.*;
 import com.tzld.piaoquan.longarticle.dao.mapper.crawler.ArticleUseGroupMapper;
@@ -1059,19 +1060,11 @@ public class CoreServiceImpl implements CoreService {
                                     .andUserGroupIdEqualTo(Integer.parseInt(userGroupId));
                             List<GroupSendOpenId> groupSendOpenIds = groupSendOpenIdMapper.selectByExample(groupSendOpenIdExample);
                             if (!CollectionUtils.isEmpty(groupSendOpenIds)) {
-                                for (GroupSendOpenId groupSendOpenId : groupSendOpenIds) {
-                                    ArticleUseGroupExample articleUseGroupExample = new ArticleUseGroupExample();
-                                    articleUseGroupExample.createCriteria().andGzhIdEqualTo(groupSendOpenId.getGhId())
-                                            .andOpenIdEqualTo(groupSendOpenId.getOpenId());
-                                    List<ArticleUseGroup> articleUseGroups = articleUseGroupMapper.selectByExample(articleUseGroupExample);
-                                    if (!CollectionUtils.isEmpty(articleUseGroups)) {
-                                        for (ArticleUseGroup articleUseGroup : articleUseGroups) {
-                                            ArticleUseGroup updateArticleUseGroup = new ArticleUseGroup();
-                                            updateArticleUseGroup.setId(articleUseGroup.getId());
-                                            updateArticleUseGroup.setRemainingCount(articleUseGroup.getRemainingCount() - 1);
-                                            articleUseGroupMapper.updateByPrimaryKeySelective(updateArticleUseGroup);
-                                        }
-                                    }
+                                // 提取所有 openId
+                                List<String> openIdList = groupSendOpenIds.stream().map(GroupSendOpenId::getOpenId).distinct().collect(Collectors.toList());
+                                // 批量更新,一次性减少所有匹配记录的 remaining_count
+                                for (List<String> partition : Lists.partition(openIdList, 1000)) {
+                                    articleUseGroupMapper.batchDecreaseRemainingCount(ghId, partition);
                                 }
                             }
                         }

+ 12 - 0
long-article-server/src/main/resources/mapper/crawler/ArticleUseGroupMapper.xml

@@ -259,4 +259,16 @@
     where is_delete = 0 and gzh_id = #{ghId,jdbcType=VARCHAR}
       and remaining_count > 0
   </select>
+
+  <update id="batchDecreaseRemainingCount">
+    update article_user_group
+    set remaining_count = remaining_count - 1
+    where gzh_id = #{gzhId,jdbcType=VARCHAR}
+      and open_id in
+      <foreach collection="openIdList" item="openId" open="(" separator="," close=")">
+        #{openId,jdbcType=VARCHAR}
+      </foreach>
+      and is_delete = 0
+      and remaining_count > 0
+  </update>
 </mapper>

+ 20 - 0
long-article-server/src/test/java/com/tzld/piaoquan/longarticle/CoreServiceJobTest.java

@@ -0,0 +1,20 @@
+package com.tzld.piaoquan.longarticle;
+
+import com.tzld.piaoquan.longarticle.service.local.CoreService;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest(classes = LongArticleServerApplication.class)
+@Slf4j
+public class CoreServiceJobTest {
+
+    @Autowired
+    CoreService coreService;
+
+    @Test
+    public void getPushStatus() {
+        coreService.getPushStatus();
+    }
+}