|
@@ -0,0 +1,207 @@
|
|
|
|
|
+package com.storlead.trade.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.storlead.framework.common.constant.CommonConstant;
|
|
|
|
|
+import com.storlead.framework.common.constant.DSConstants;
|
|
|
|
|
+import com.storlead.framework.common.result.Result;
|
|
|
|
|
+import com.storlead.trade.dto.CustomerAnalysisSaveDTO;
|
|
|
|
|
+import com.storlead.trade.entity.CustomerAnalysisRecordEntity;
|
|
|
|
|
+import com.storlead.trade.entity.CustomerAnalysisResultEntity;
|
|
|
|
|
+import com.storlead.trade.service.CustomerAnalysisRecordEntityService;
|
|
|
|
|
+import com.storlead.trade.service.CustomerAnalysisResultEntityService;
|
|
|
|
|
+import com.storlead.trade.service.CustomerAnalysisSaveService;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 对齐 Python save_data_api.py 的 /analysis/update 逻辑。
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@DS(DSConstants.DATASOURCE_TRADE)
|
|
|
|
|
+public class CustomerAnalysisSaveServiceImpl implements CustomerAnalysisSaveService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private CustomerAnalysisResultEntityService resultService;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private CustomerAnalysisRecordEntityService recordService;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public Result<Object> updateOrInsert(CustomerAnalysisSaveDTO dto) {
|
|
|
|
|
+ if (dto == null || dto.getCustomerId() == null) {
|
|
|
|
|
+ return Result.error("Missing required field: customer_id");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!hasAnyUpdatableField(dto)) {
|
|
|
|
|
+ return Result.error("No valid fields to update");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String coreTags = serializeForMysql(dto.getCoreTags());
|
|
|
|
|
+ String featureSnapshot = serializeForMysql(dto.getFeatureSnapshot());
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 1. 先写流水(对齐 Python 每次调用都 insert record)
|
|
|
|
|
+ CustomerAnalysisRecordEntity record = new CustomerAnalysisRecordEntity();
|
|
|
|
|
+ record.setCustomerId(dto.getCustomerId());
|
|
|
|
|
+ record.setAnalysisScene(dto.getAnalysisScene());
|
|
|
|
|
+ record.setAnalysisStatus(dto.getAnalysisStatus());
|
|
|
|
|
+ record.setPotentialScore(dto.getPotentialScore());
|
|
|
|
|
+ record.setCustomerLevel(dto.getCustomerLevel());
|
|
|
|
|
+ record.setConfidence(dto.getConfidence());
|
|
|
|
|
+ record.setPriority(dto.getPriority());
|
|
|
|
|
+ record.setCoreTags(coreTags);
|
|
|
|
|
+ record.setReasonSummary(dto.getReasonSummary());
|
|
|
|
|
+ record.setFeatureSnapshot(featureSnapshot);
|
|
|
|
|
+ record.setModelName(dto.getModelName());
|
|
|
|
|
+ record.setModelVersion(dto.getModelVersion());
|
|
|
|
|
+ record.setPromptVersion(dto.getPromptVersion());
|
|
|
|
|
+ record.setIsDelete(CommonConstant.DEL_FLAG_0);
|
|
|
|
|
+ recordService.save(record);
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 按 customer_id 查结果表
|
|
|
|
|
+ CustomerAnalysisResultEntity existing = resultService.getOne(
|
|
|
|
|
+ new LambdaQueryWrapper<CustomerAnalysisResultEntity>()
|
|
|
|
|
+ .eq(CustomerAnalysisResultEntity::getCustomerId, dto.getCustomerId())
|
|
|
|
|
+ .eq(CustomerAnalysisResultEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
|
|
|
|
|
+ .orderByDesc(CustomerAnalysisResultEntity::getId)
|
|
|
|
|
+ .last("limit 1"),
|
|
|
|
|
+ false);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> resp = new HashMap<>(4);
|
|
|
|
|
+ if (existing != null) {
|
|
|
|
|
+ LambdaUpdateWrapper<CustomerAnalysisResultEntity> uw = new LambdaUpdateWrapper<>();
|
|
|
|
|
+ uw.eq(CustomerAnalysisResultEntity::getId, existing.getId());
|
|
|
|
|
+ applyUpdateFields(uw, dto, coreTags, featureSnapshot);
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getAnalysisTime, new Date());
|
|
|
|
|
+ boolean ok = resultService.update(uw);
|
|
|
|
|
+ resp.put("action", "updated");
|
|
|
|
|
+ resp.put("affected", ok ? 1 : 0);
|
|
|
|
|
+ resp.put("id", existing.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ CustomerAnalysisResultEntity insert = new CustomerAnalysisResultEntity();
|
|
|
|
|
+ insert.setCustomerId(dto.getCustomerId());
|
|
|
|
|
+ applyInsertFields(insert, dto, coreTags, featureSnapshot);
|
|
|
|
|
+ insert.setAnalysisTime(new Date());
|
|
|
|
|
+ insert.setIsDelete(CommonConstant.DEL_FLAG_0);
|
|
|
|
|
+ resultService.save(insert);
|
|
|
|
|
+ resp.put("action", "inserted");
|
|
|
|
|
+ resp.put("affected", 1);
|
|
|
|
|
+ resp.put("id", insert.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ return Result.result(resp);
|
|
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
|
|
+ log.warn("customer analysis upsert duplicate, customerId={}", dto.getCustomerId(), e);
|
|
|
|
|
+ return Result.error("Duplicate entry: " + e.getMessage());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("customer analysis upsert error, customerId={}", dto.getCustomerId(), e);
|
|
|
|
|
+ throw e;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasAnyUpdatableField(CustomerAnalysisSaveDTO dto) {
|
|
|
|
|
+ return StringUtils.hasText(dto.getAnalysisScene())
|
|
|
|
|
+ || dto.getAnalysisStatus() != null
|
|
|
|
|
+ || dto.getPotentialScore() != null
|
|
|
|
|
+ || StringUtils.hasText(dto.getCustomerLevel())
|
|
|
|
|
+ || dto.getConfidence() != null
|
|
|
|
|
+ || dto.getPriority() != null
|
|
|
|
|
+ || dto.getCoreTags() != null
|
|
|
|
|
+ || StringUtils.hasText(dto.getReasonSummary())
|
|
|
|
|
+ || dto.getFeatureSnapshot() != null
|
|
|
|
|
+ || StringUtils.hasText(dto.getModelName())
|
|
|
|
|
+ || StringUtils.hasText(dto.getModelVersion())
|
|
|
|
|
+ || StringUtils.hasText(dto.getPromptVersion());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void applyUpdateFields(LambdaUpdateWrapper<CustomerAnalysisResultEntity> uw,
|
|
|
|
|
+ CustomerAnalysisSaveDTO dto,
|
|
|
|
|
+ String coreTags,
|
|
|
|
|
+ String featureSnapshot) {
|
|
|
|
|
+ if (StringUtils.hasText(dto.getAnalysisScene())) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getAnalysisScene, dto.getAnalysisScene());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getAnalysisStatus() != null) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getAnalysisStatus, dto.getAnalysisStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getPotentialScore() != null) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getPotentialScore, dto.getPotentialScore());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.hasText(dto.getCustomerLevel())) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getCustomerLevel, dto.getCustomerLevel());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getConfidence() != null) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getConfidence, dto.getConfidence());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getPriority() != null) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getPriority, dto.getPriority());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getCoreTags() != null) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getCoreTags, coreTags);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.hasText(dto.getReasonSummary())) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getReasonSummary, dto.getReasonSummary());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getFeatureSnapshot() != null) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getFeatureSnapshot, featureSnapshot);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.hasText(dto.getModelName())) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getModelName, dto.getModelName());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.hasText(dto.getModelVersion())) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getModelVersion, dto.getModelVersion());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.hasText(dto.getPromptVersion())) {
|
|
|
|
|
+ uw.set(CustomerAnalysisResultEntity::getPromptVersion, dto.getPromptVersion());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void applyInsertFields(CustomerAnalysisResultEntity entity,
|
|
|
|
|
+ CustomerAnalysisSaveDTO dto,
|
|
|
|
|
+ String coreTags,
|
|
|
|
|
+ String featureSnapshot) {
|
|
|
|
|
+ entity.setAnalysisScene(dto.getAnalysisScene());
|
|
|
|
|
+ entity.setAnalysisStatus(dto.getAnalysisStatus());
|
|
|
|
|
+ entity.setPotentialScore(dto.getPotentialScore());
|
|
|
|
|
+ entity.setCustomerLevel(dto.getCustomerLevel());
|
|
|
|
|
+ entity.setConfidence(dto.getConfidence());
|
|
|
|
|
+ entity.setPriority(dto.getPriority());
|
|
|
|
|
+ entity.setCoreTags(coreTags);
|
|
|
|
|
+ entity.setReasonSummary(dto.getReasonSummary());
|
|
|
|
|
+ entity.setFeatureSnapshot(featureSnapshot);
|
|
|
|
|
+ entity.setModelName(dto.getModelName());
|
|
|
|
|
+ entity.setModelVersion(dto.getModelVersion());
|
|
|
|
|
+ entity.setPromptVersion(dto.getPromptVersion());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 对齐 Python serialize_for_mysql:dict/list → JSON 字符串 */
|
|
|
|
|
+ private String serializeForMysql(Object value) {
|
|
|
|
|
+ if (value == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (value instanceof String) {
|
|
|
|
|
+ return (String) value;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (value instanceof Map || value instanceof Iterable || value.getClass().isArray()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return objectMapper.writeValueAsString(value);
|
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
|
+ throw new IllegalArgumentException("字段无法序列化为 JSON: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return String.valueOf(value);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|