Explorar el Código

Merge remote-tracking branch 'origin/master'

18118724055@163.com hace 3 semanas
padre
commit
5868b39abb

+ 1 - 1
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/entity/MarketEmailsEntity.java

@@ -35,7 +35,7 @@ public class MarketEmailsEntity extends SysBaseField {
     @TableField("email_id")
     private Long emailId;
 
-    @ApiModelProperty(value = "发送状态:-3取消 -2草稿 -1定时 0发送中 1成功 2失败;收信记录一般为1")
+    @ApiModelProperty(value = "发送状态:-4退回 -3取消 -2草稿 -1定时 0发送中 1成功 2失败;收信记录一般为1")
     @TableField("status")
     private Integer status;
 

+ 46 - 16
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/service/impl/MarketEmailsServiceImpl.java

@@ -293,6 +293,13 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
                         }
                     }
                 }
+                // 回填客户 AI 分析结果(一对一关联 customer_analysis_result,取最新一条)
+                Map<Long, CustomerAnalysisResultEntity> analysisMap = getLatestAnalysisMap(customerIdSet);
+                for (MarketEmailsOpenVO v : voList) {
+                    if (v.getCustomerId() != null) {
+                        v.setCustomerAnalysisResult(analysisMap.get(v.getCustomerId()));
+                    }
+                }
             } catch (Exception ex) {
                 log.warn("查询客户名称失败,customerIds={} err={}", customerIdSet, ex.getMessage());
             }
@@ -707,22 +714,8 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
                             if (c.getId() != null) customerMap.put(c.getId(), c);
                         }
                     }
-                    // 回填客户等级 / 核心标签(从 CustomerAnalysisResultEntity)
-                    // 一个 customer 可能有多个分析结果,取最新一条(id 最大)
-                    List<CustomerAnalysisResultEntity> analysisList =
-                            customerAnalysisResultEntityService.list(
-                                    new LambdaQueryWrapper<CustomerAnalysisResultEntity>()
-                                            .in(CustomerAnalysisResultEntity::getCustomerId, customerIdSet)
-                                            .eq(CustomerAnalysisResultEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
-                                            .orderByDesc(CustomerAnalysisResultEntity::getId));
-                    Map<Long, CustomerAnalysisResultEntity> analysisMap = new HashMap<>();
-                    if (!CollectionUtils.isEmpty(analysisList)) {
-                        for (CustomerAnalysisResultEntity a : analysisList) {
-                            if (a.getCustomerId() != null && !analysisMap.containsKey(a.getCustomerId())) {
-                                analysisMap.put(a.getCustomerId(), a);
-                            }
-                        }
-                    }
+                    // 回填客户等级 / 核心标签(从 CustomerAnalysisResultEntity,一对一关联,取最新一条)
+                    Map<Long, CustomerAnalysisResultEntity> analysisMap = getLatestAnalysisMap(customerIdSet);
                     for (MarketEmailsReplyVO v : voList) {
                         CustomerEntity c = customerMap.get(v.getCustomerId());
                         if (c != null) {
@@ -854,6 +847,13 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
                             v.setCustomerName(c.getCustomerName());
                         }
                     }
+                    // 回填客户 AI 分析结果(一对一关联 customer_analysis_result,取最新一条)
+                    Map<Long, CustomerAnalysisResultEntity> analysisMap = getLatestAnalysisMap(customerIdSet);
+                    for (MarketEmailsProtectVO v : voList) {
+                        if (v.getCustomerId() != null) {
+                            v.setCustomerAnalysisResult(analysisMap.get(v.getCustomerId()));
+                        }
+                    }
                 } catch (Exception ex) {
                     log.warn("回填客户名失败,customerIds={} err={}", customerIdSet, ex.getMessage());
                 }
@@ -862,6 +862,36 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
         return Result.ok(voList);
     }
 
+    /**
+     * 批量查询客户最新的 AI 分析结果(一对一关联 customer_analysis_result)。
+     * <p>
+     * 客户与 CustomerAnalysisResultEntity 理论上是一对一,但 DB 上可能因重算产生多条记录;
+     * 这里按 id DESC 取最新一条,避免重复计数。
+     *
+     * @param customerIdSet 客户 id 集合(可为空)
+     * @return key = customerId,value = 最新一条 CustomerAnalysisResultEntity(无命中则为空 map)
+     */
+    private Map<Long, CustomerAnalysisResultEntity> getLatestAnalysisMap(Set<Long> customerIdSet) {
+        Map<Long, CustomerAnalysisResultEntity> analysisMap = new HashMap<>();
+        if (CollectionUtils.isEmpty(customerIdSet)) {
+            return analysisMap;
+        }
+        List<CustomerAnalysisResultEntity> analysisList =
+                customerAnalysisResultEntityService.list(
+                        new LambdaQueryWrapper<CustomerAnalysisResultEntity>()
+                                .in(CustomerAnalysisResultEntity::getCustomerId, customerIdSet)
+                                .eq(CustomerAnalysisResultEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
+                                .orderByDesc(CustomerAnalysisResultEntity::getId));
+        if (!CollectionUtils.isEmpty(analysisList)) {
+            for (CustomerAnalysisResultEntity a : analysisList) {
+                if (a.getCustomerId() != null && !analysisMap.containsKey(a.getCustomerId())) {
+                    analysisMap.put(a.getCustomerId(), a);
+                }
+            }
+        }
+        return analysisMap;
+    }
+
     /**
      * 移除保护:按邮件主键 id 把 has_protect 置 0(保留 has_suspend)
      */

+ 16 - 9
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/task/CustomerSyncTask.java

@@ -70,25 +70,32 @@ public class CustomerSyncTask {
 
     private void doSync() {
         // 1. 查 customer_analysis_result 中已有的最大 customer_id(无记录则从 0 开始全量同步)
-        CustomerAnalysisResultEntity maxEntity = customerAnalysisResultService.lambdaQuery()
+//        CustomerAnalysisResultEntity maxEntity = customerAnalysisResultService.lambdaQuery()
+//                .select(CustomerAnalysisResultEntity::getCustomerId)
+//                .orderByDesc(CustomerAnalysisResultEntity::getCustomerId)
+//                .last("LIMIT 1")
+//                .one();
+//        long maxCustomerId = maxEntity == null || maxEntity.getCustomerId() == null
+//                ? 0L : maxEntity.getCustomerId();
+        List<Long> customerIdList = customerAnalysisResultService.list(new LambdaQueryWrapper<CustomerAnalysisResultEntity>()
                 .select(CustomerAnalysisResultEntity::getCustomerId)
-                .orderByDesc(CustomerAnalysisResultEntity::getCustomerId)
-                .last("LIMIT 1")
-                .one();
-        long maxCustomerId = maxEntity == null || maxEntity.getCustomerId() == null
-                ? 0L : maxEntity.getCustomerId();
+                .eq(CustomerAnalysisResultEntity::getIsDelete, CommonConstant.DEL_FLAG_0))
+                .stream()
+                .map(CustomerAnalysisResultEntity::getCustomerId)
+                .collect(Collectors.toList());
+
 
         // 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))
+                .ne(CustomerEntity::getId, customerIdList))
                 .stream()
                 .map(CustomerEntity::getId)
                 .collect(Collectors.toList());
 
         if (newCustomerIdList.isEmpty()) {
-            log.info("[CustomerSyncTask] 无新增客户,maxCustomerId={},本次同步 0 条", maxCustomerId);
+            log.info("[CustomerSyncTask] 无新增客户");
             return;
         }
 
@@ -104,6 +111,6 @@ public class CustomerSyncTask {
             }).collect(Collectors.toList());
             customerAnalysisResultService.saveBatch(entityList);
         }
-        log.info("[CustomerSyncTask] 同步完成,maxCustomerId={},新增 {} 条客户待分析记录", maxCustomerId, total);
+        log.info("[CustomerSyncTask] 同步完成,新增 {} 条客户待分析记录",  total);
     }
 }

+ 4 - 0
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/vo/MarketEmailsOpenVO.java

@@ -3,6 +3,7 @@ package com.storlead.trade.vo;
 import com.alibaba.fastjson.annotation.JSONField;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.fasterxml.jackson.annotation.JsonFormat;
+import com.storlead.trade.entity.CustomerAnalysisResultEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -32,6 +33,9 @@ public class MarketEmailsOpenVO implements Serializable {
     @ApiModelProperty(value = "客户名称")
     private String customerName;
 
+    @ApiModelProperty(value = "客户AI分析结果(一对一关联 customer_analysis_result,取最新一条)")
+    private CustomerAnalysisResultEntity customerAnalysisResult;
+
     @ApiModelProperty(value = "打开次数(来自 market_emails.open_count 字段)")
     private Integer openCount;
 

+ 4 - 0
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/vo/MarketEmailsProtectVO.java

@@ -3,6 +3,7 @@ package com.storlead.trade.vo;
 import com.alibaba.fastjson.annotation.JSONField;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.fasterxml.jackson.annotation.JsonFormat;
+import com.storlead.trade.entity.CustomerAnalysisResultEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -37,6 +38,9 @@ public class MarketEmailsProtectVO implements Serializable {
     @ApiModelProperty(value = "客户名称")
     private String customerName;
 
+    @ApiModelProperty(value = "客户AI分析结果(一对一关联 customer_analysis_result,取最新一条)")
+    private CustomerAnalysisResultEntity customerAnalysisResult;
+
     @ApiModelProperty(value = "保护原因")
     private String protectReason;