|
|
@@ -1,25 +1,45 @@
|
|
|
package com.fs.crm.service.impl;
|
|
|
|
|
|
-import java.util.List;
|
|
|
-import com.fs.common.utils.DateUtils;
|
|
|
+import cn.hutool.core.lang.TypeReference;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import com.fs.crm.mapper.CrmCustomerAnalyzeMapper;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
+import com.fs.common.core.domain.entity.SysDictData;
|
|
|
+import com.fs.common.utils.DateUtils;
|
|
|
import com.fs.crm.domain.CrmCustomerAnalyze;
|
|
|
+import com.fs.crm.mapper.CrmCustomerAnalyzeMapper;
|
|
|
import com.fs.crm.service.ICrmCustomerAnalyzeService;
|
|
|
+import com.fs.crm.utils.CrmCustomerAiTagUtil;
|
|
|
+import com.fs.system.mapper.SysDictDataMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 客户聊天记录分析Service业务层处理
|
|
|
- *
|
|
|
+ *
|
|
|
* @author fs
|
|
|
* @date 2026-03-24
|
|
|
*/
|
|
|
@Service
|
|
|
public class CrmCustomerAnalyzeServiceImpl extends ServiceImpl<CrmCustomerAnalyzeMapper, CrmCustomerAnalyze> implements ICrmCustomerAnalyzeService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysDictDataMapper sysDictDataMapper;
|
|
|
/**
|
|
|
* 查询客户聊天记录分析
|
|
|
- *
|
|
|
+ *
|
|
|
* @param id 客户聊天记录分析主键
|
|
|
* @return 客户聊天记录分析
|
|
|
*/
|
|
|
@@ -31,7 +51,7 @@ public class CrmCustomerAnalyzeServiceImpl extends ServiceImpl<CrmCustomerAnalyz
|
|
|
|
|
|
/**
|
|
|
* 查询客户聊天记录分析列表
|
|
|
- *
|
|
|
+ *
|
|
|
* @param crmCustomerAnalyze 客户聊天记录分析
|
|
|
* @return 客户聊天记录分析
|
|
|
*/
|
|
|
@@ -43,7 +63,7 @@ public class CrmCustomerAnalyzeServiceImpl extends ServiceImpl<CrmCustomerAnalyz
|
|
|
|
|
|
/**
|
|
|
* 新增客户聊天记录分析
|
|
|
- *
|
|
|
+ *
|
|
|
* @param crmCustomerAnalyze 客户聊天记录分析
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@@ -56,7 +76,7 @@ public class CrmCustomerAnalyzeServiceImpl extends ServiceImpl<CrmCustomerAnalyz
|
|
|
|
|
|
/**
|
|
|
* 修改客户聊天记录分析
|
|
|
- *
|
|
|
+ *
|
|
|
* @param crmCustomerAnalyze 客户聊天记录分析
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@@ -68,7 +88,7 @@ public class CrmCustomerAnalyzeServiceImpl extends ServiceImpl<CrmCustomerAnalyz
|
|
|
|
|
|
/**
|
|
|
* 批量删除客户聊天记录分析
|
|
|
- *
|
|
|
+ *
|
|
|
* @param ids 需要删除的客户聊天记录分析主键
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@@ -80,7 +100,7 @@ public class CrmCustomerAnalyzeServiceImpl extends ServiceImpl<CrmCustomerAnalyz
|
|
|
|
|
|
/**
|
|
|
* 删除客户聊天记录分析信息
|
|
|
- *
|
|
|
+ *
|
|
|
* @param id 客户聊天记录分析主键
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@@ -89,4 +109,98 @@ public class CrmCustomerAnalyzeServiceImpl extends ServiceImpl<CrmCustomerAnalyz
|
|
|
{
|
|
|
return baseMapper.deleteCrmCustomerAnalyzeById(id);
|
|
|
}
|
|
|
+ private static final String AI_PORTRAIT = "crm_ai_portrait";
|
|
|
+ private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
+ //ai获取客户画像
|
|
|
+ @Override
|
|
|
+ @Async
|
|
|
+ public void aiGeneratedCustomerPortrait(String customerId, String dataJson,String logId) {
|
|
|
+ Map<String, Object> stringObjectMap = buildRequestParam(Long.valueOf(customerId), dataJson);
|
|
|
+ R aiResponse = CrmCustomerAiTagUtil.callAiService(stringObjectMap, Long.valueOf(logId));
|
|
|
+
|
|
|
+ JSONObject root = JSON.parseObject(JSONUtil.toJsonStr(aiResponse));
|
|
|
+
|
|
|
+// 获取 data.responseData
|
|
|
+ JSONArray responseData = root.getJSONObject("data").getJSONArray("responseData");
|
|
|
+
|
|
|
+ JSONObject userInfo = null;
|
|
|
+
|
|
|
+// 遍历 responseData
|
|
|
+ for (int i = 0; i < responseData.size(); i++) {
|
|
|
+ JSONObject node = responseData.getJSONObject(i);
|
|
|
+ JSONArray historyPreview = node.getJSONArray("historyPreview");
|
|
|
+
|
|
|
+ if (historyPreview != null) {
|
|
|
+ for (int j = 0; j < historyPreview.size(); j++) {
|
|
|
+ JSONObject historyItem = historyPreview.getJSONObject(j);
|
|
|
+
|
|
|
+ // 找到 obj 为 "AI" 的项
|
|
|
+ if ("AI".equals(historyItem.getString("obj"))) {
|
|
|
+ String valueStr = historyItem.getString("value");
|
|
|
+ JSONObject valueObj = JSON.parseObject(valueStr);
|
|
|
+ userInfo = valueObj.getJSONObject("userInfo");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (userInfo != null) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (userInfo != null) {
|
|
|
+ baseMapper.updateCustomerPortrait(Long.valueOf(customerId),userInfo.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private Map<String, Object> buildRequestParam(Long customerId,
|
|
|
+ String communication) {
|
|
|
+ Map<String, Object> requestParam = new HashMap<>();
|
|
|
+
|
|
|
+ // 获取各类数据
|
|
|
+ HashMap<String, Object> history = new HashMap<>();
|
|
|
+ history.put("history", communication);
|
|
|
+ Map<String, Object> userInfo = getUserInfo(customerId);
|
|
|
+ String likeRatio ="";
|
|
|
+ if (!userInfo.isEmpty()){
|
|
|
+ likeRatio = (String) userInfo.remove("likeRatio");
|
|
|
+ }
|
|
|
+ // 合并数据
|
|
|
+ requestParam.putAll(history);
|
|
|
+ requestParam.putAll(userInfo);
|
|
|
+
|
|
|
+ // 设置其他参数
|
|
|
+ requestParam.put("tagInfos", Collections.emptyList());
|
|
|
+ requestParam.put("isRepository", "");
|
|
|
+ requestParam.put("userContent", "");
|
|
|
+ requestParam.put("aiContent", "");
|
|
|
+ requestParam.put("likeRatio", likeRatio);
|
|
|
+ requestParam.put("modelType","客户画像");
|
|
|
+
|
|
|
+ return requestParam;
|
|
|
+ }
|
|
|
+ private Map<String, Object> getUserInfo(Long customerId) {
|
|
|
+ CrmCustomerAnalyze crmCustomerAnalyze = baseMapper.selectLatestOne(customerId);
|
|
|
+ if (ObjectUtil.isEmpty(crmCustomerAnalyze))throw new RuntimeException("客户信息不存在");
|
|
|
+ HashMap<String, String> userInfo = new HashMap<String, String>();
|
|
|
+// userInfo.put("name", crmCustomerAnalyze.getCustomerName()==null?"" : crmCustomerAnalyze.getCustomerName());
|
|
|
+ List<SysDictData> portraits = sysDictDataMapper.selectDictDataByType(AI_PORTRAIT);
|
|
|
+ List<String> dictValue = portraits.stream().map(SysDictData::getDictValue).collect(Collectors.toList());
|
|
|
+ Map<String, String> portraitMap = portraits.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
|
|
|
+
|
|
|
+ if (crmCustomerAnalyze.getCustomerPortraitJson() != null){
|
|
|
+ Map<String, String> portraitList = JSON.parseObject(
|
|
|
+ crmCustomerAnalyze.getCustomerPortraitJson(),
|
|
|
+ new TypeReference<Map<String, String>>() {}
|
|
|
+ );
|
|
|
+ portraitList.forEach((k, v)->{
|
|
|
+ if(ObjectUtil.isNotEmpty(portraitMap.get(k)))userInfo.put(portraitMap.get(k),v);
|
|
|
+ });
|
|
|
+
|
|
|
+ }else {
|
|
|
+ dictValue.forEach(o->{
|
|
|
+ userInfo.put(o, "");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ HashMap<String, Object> result = new HashMap<>();
|
|
|
+ result.put("userInfo", userInfo);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|