浏览代码

Merge remote-tracking branch 'origin/master'

zhujing 1 月之前
父节点
当前提交
ed47663ea9

+ 109 - 0
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/task/CustomerSyncTask.java

@@ -0,0 +1,109 @@
+package com.storlead.trade.task;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.storlead.framework.common.constant.CommonConstant;
+import com.storlead.trade.entity.CustomerAnalysisResultEntity;
+import com.storlead.trade.entity.CustomerEntity;
+import com.storlead.trade.service.CustomerAnalysisResultEntityService;
+import com.storlead.trade.service.CustomerTradeService;
+import lombok.extern.slf4j.Slf4j;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * 每天 4 点:同步客户表的最新数据到 CustomerAnalysisResultEntity。
+ * <p>
+ * 只写入 customer_id 和 analysis_status = -1(待分析),
+ * 增量口径:customer 表中 id 大于 customer_analysis_result 已有最大 customer_id 的客户。
+ */
+@Slf4j
+@Component
+public class CustomerSyncTask {
+
+    /** 分批落库大小 */
+    private static final int BATCH_SIZE = 1000;
+
+    @Resource
+    private CustomerAnalysisResultEntityService customerAnalysisResultService;
+
+    @Resource
+    private CustomerTradeService customerTradeService;
+
+    @Resource
+    private RedissonClient redissonClient;
+
+    /**
+     * 每天 4 点同步新增客户:
+     * 取 customer_analysis_result 中已有的最大 customer_id,把 customer 表中
+     * id 大于该值的客户(未删除)全部写入 customer_analysis_result,
+     * 只填 customer_id 与 analysis_status = -1(待分析),其余字段留空。
+     */
+    @Scheduled(cron = "0 0 4 * * ?")
+    public void dispatch() {
+        String lockKey = "customer-analysis:sync:lock";
+        RLock lock = redissonClient.getLock(lockKey);
+        boolean acquired = false;
+        try {
+            // 抢锁:等 0 秒(抢不到说明其他实例在跑,直接跳过),最长持锁 30 分钟防死锁
+            acquired = lock.tryLock(0, 30, TimeUnit.MINUTES);
+            if (!acquired) {
+                log.info("[CustomerSyncTask] 锁未获取到,当前有其他实例在执行,跳过本次");
+                return;
+            }
+            doSync();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            log.warn("[CustomerSyncTask] 获取锁被中断: {}", e.getMessage());
+        } finally {
+            if (acquired && lock.isHeldByCurrentThread()) {
+                lock.unlock();
+            }
+        }
+    }
+
+    private void doSync() {
+        // 1. 查 customer_analysis_result 中已有的最大 customer_id(无记录则从 0 开始全量同步)
+        CustomerAnalysisResultEntity maxEntity = customerAnalysisResultService.lambdaQuery()
+                .select(CustomerAnalysisResultEntity::getCustomerId)
+                .orderByDesc(CustomerAnalysisResultEntity::getCustomerId)
+                .last("LIMIT 1")
+                .one();
+        long maxCustomerId = maxEntity == null || maxEntity.getCustomerId() == null
+                ? 0L : maxEntity.getCustomerId();
+
+        // 2. 查客户表中 id 大于 maxCustomerId 的未删除客户(只取 id)
+        List<Long> newCustomerIdList = customerTradeService.list(new LambdaQueryWrapper<CustomerEntity>()
+                .select(CustomerEntity::getId)
+                .eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
+                .gt(CustomerEntity::getId, maxCustomerId))
+                .stream()
+                .map(CustomerEntity::getId)
+                .collect(Collectors.toList());
+
+        if (newCustomerIdList.isEmpty()) {
+            log.info("[CustomerSyncTask] 无新增客户,maxCustomerId={},本次同步 0 条", maxCustomerId);
+            return;
+        }
+
+        // 3. 分批写入 customer_analysis_result(只填 customer_id + analysis_status=-1)
+        int total = newCustomerIdList.size();
+        for (int i = 0; i < total; i += BATCH_SIZE) {
+            List<Long> batch = newCustomerIdList.subList(i, Math.min(i + BATCH_SIZE, total));
+            List<CustomerAnalysisResultEntity> entityList = batch.stream().map(customerId -> {
+                CustomerAnalysisResultEntity entity = new CustomerAnalysisResultEntity();
+                entity.setCustomerId(customerId);
+                entity.setAnalysisStatus(-1);
+                return entity;
+            }).collect(Collectors.toList());
+            customerAnalysisResultService.saveBatch(entityList);
+        }
+        log.info("[CustomerSyncTask] 同步完成,maxCustomerId={},新增 {} 条客户待分析记录", maxCustomerId, total);
+    }
+}