|
@@ -1,8 +1,16 @@
|
|
|
package com.fs.company.service.impl;
|
|
package com.fs.company.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fs.common.core.domain.R;
|
|
import com.fs.common.core.domain.R;
|
|
|
|
|
+import com.fs.common.core.domain.entity.SysDictData;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
import com.fs.common.utils.StringUtils;
|
|
|
|
|
+import com.fs.common.utils.spring.SpringUtils;
|
|
|
import com.fs.company.domain.CompanyVoiceRobotic;
|
|
import com.fs.company.domain.CompanyVoiceRobotic;
|
|
|
import com.fs.company.mapper.CompanyVoiceRoboticMapper;
|
|
import com.fs.company.mapper.CompanyVoiceRoboticMapper;
|
|
|
import com.fs.company.param.EntryCustomerParam;
|
|
import com.fs.company.param.EntryCustomerParam;
|
|
@@ -10,18 +18,29 @@ import com.fs.company.service.ICompanyVoiceRoboticService;
|
|
|
import com.fs.company.service.IGeneralCustomerEntryService;
|
|
import com.fs.company.service.IGeneralCustomerEntryService;
|
|
|
import com.fs.company.util.CryptoUtil;
|
|
import com.fs.company.util.CryptoUtil;
|
|
|
import com.fs.company.util.PhoneNumberUtil;
|
|
import com.fs.company.util.PhoneNumberUtil;
|
|
|
|
|
+import com.fs.config.ai.AiHostProper;
|
|
|
import com.fs.crm.domain.CrmCustomer;
|
|
import com.fs.crm.domain.CrmCustomer;
|
|
|
|
|
+import com.fs.crm.domain.CrmCustomerPropertyTemplate;
|
|
|
|
|
+import com.fs.crm.dto.CrmCustomerAiAutoTagVo;
|
|
|
import com.fs.crm.mapper.CrmCustomerMapper;
|
|
import com.fs.crm.mapper.CrmCustomerMapper;
|
|
|
|
|
+import com.fs.crm.service.ICrmCustomerPropertyTemplateService;
|
|
|
|
|
+import com.fs.crm.utils.CrmCustomerAiTagUtil;
|
|
|
|
|
+import com.fs.crm.vo.CrmCustomerAiTagVo;
|
|
|
|
|
+import com.fs.fastgptApi.param.ChatParam;
|
|
|
|
|
+import com.fs.fastgptApi.service.ChatService;
|
|
|
|
|
+import com.fs.hisapi.util.MapUtil;
|
|
|
|
|
+import com.fs.system.mapper.SysDictDataMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.logging.log4j.core.util.UuidUtil;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.time.LocalTime;
|
|
import java.time.LocalTime;
|
|
|
-import java.util.HashMap;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.Executor;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -45,6 +64,8 @@ public class GeneralCustomerEntryServiceImpl implements IGeneralCustomerEntrySer
|
|
|
CompanyVoiceRoboticMapper companyVoiceRoboticMapper;
|
|
CompanyVoiceRoboticMapper companyVoiceRoboticMapper;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
ICompanyVoiceRoboticService companyVoiceRoboticService;
|
|
ICompanyVoiceRoboticService companyVoiceRoboticService;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private SysDictDataMapper sysDictDataMapper;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 录入客户
|
|
* 录入客户
|
|
@@ -85,6 +106,97 @@ public class GeneralCustomerEntryServiceImpl implements IGeneralCustomerEntrySer
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private static final String TRADE_TYPE = "trade_type";
|
|
|
|
|
+ @Value("${crm.customer.ai.key:mygpt-tbQfq4ejR162mGJBCTTDUH9ecP1XCVuUfaOGTipnLjb1hP8x5prg}")
|
|
|
|
|
+ private String appKey;
|
|
|
|
|
+ private List<CrmCustomerAiTagVo> getAiTags(String chatRecord) throws JsonProcessingException {
|
|
|
|
|
+ Map<String, Object> requestParam = new HashMap<>();
|
|
|
|
|
+ List<CrmCustomerPropertyTemplate> crmCustomerPropertyTemplates = SpringUtils.getBean(ICrmCustomerPropertyTemplateService.class).getBaseMapper().selectList(new LambdaQueryWrapper<CrmCustomerPropertyTemplate>());
|
|
|
|
|
+ List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType(TRADE_TYPE);
|
|
|
|
|
+ ArrayList<Map<String, String>> tags = new ArrayList<>();//标签及提示词
|
|
|
|
|
+ crmCustomerPropertyTemplates.forEach(o -> {
|
|
|
|
|
+ Map<String, String> tag = MapUtil.convertToMap(new CrmCustomerAiAutoTagVo(String.valueOf(o.getId()), o.getName(), o.getAiHint()));
|
|
|
|
|
+ tags.add(tag);
|
|
|
|
|
+ });
|
|
|
|
|
+ StringBuilder history = new StringBuilder();
|
|
|
|
|
+ List<Map<String, Object>> communication;
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ try {
|
|
|
|
|
+ communication = mapper.readValue(chatRecord,
|
|
|
|
|
+ new TypeReference<List<Map<String, Object>>>() {});
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException("数据格式错误", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ //对话内容删掉第一条(提示词)
|
|
|
|
|
+ if (!communication.isEmpty())communication.remove(0);
|
|
|
|
|
+ history.append("{");
|
|
|
|
|
+ for (Map<String, Object> o :
|
|
|
|
|
+ communication) {
|
|
|
|
|
+ String role = (String) o.get("role");
|
|
|
|
|
+ String content = (String) o.get("content");
|
|
|
|
|
+ String roleTag = "user".equals(role) ? "user" : "ai";
|
|
|
|
|
+ history.append(String.format("\"%s\":\"%s\",", roleTag, content));
|
|
|
|
|
+ }
|
|
|
|
|
+ history.deleteCharAt(history.length() - 1).append("}");
|
|
|
|
|
+ HashMap<String, String> userInfo = new HashMap<String, String>();
|
|
|
|
|
+ HashMap<String, String> aiInfo = new HashMap<>();
|
|
|
|
|
+ aiInfo.put("name", "");
|
|
|
|
|
+ aiInfo.put("sex", "");
|
|
|
|
|
+ aiInfo.put("age", "");
|
|
|
|
|
+ aiInfo.put("city", "");
|
|
|
|
|
+ aiInfo.put("habits", "");
|
|
|
|
|
+ aiInfo.put("describe", "");
|
|
|
|
|
+ userInfo.put("name", "");
|
|
|
|
|
+ userInfo.put("sex", "");
|
|
|
|
|
+ userInfo.put("age", "");
|
|
|
|
|
+ userInfo.put("city", "");
|
|
|
|
|
+ userInfo.put("habits", "");
|
|
|
|
|
+ userInfo.put("describe", "");
|
|
|
|
|
+ requestParam.put("aiInfo", aiInfo);
|
|
|
|
|
+ requestParam.put("likeRatio", "");
|
|
|
|
|
+ requestParam.put("userInfo", userInfo);
|
|
|
|
|
+ requestParam.put("history", history);
|
|
|
|
|
+ requestParam.put("tags", tags);
|
|
|
|
|
+ requestParam.put("tradeName", sysDictData.stream().map(SysDictData::getDictLabel));
|
|
|
|
|
+ requestParam.put("tradeType", sysDictData.stream().map(SysDictData::getDictValue));
|
|
|
|
|
+ requestParam.put("tagInfos", Collections.emptyList());
|
|
|
|
|
+ requestParam.put("isRepository", "");
|
|
|
|
|
+ requestParam.put("userContent", "");
|
|
|
|
|
+ requestParam.put("aiContent", "");
|
|
|
|
|
+ requestParam.put("likeRatio", "");
|
|
|
|
|
+
|
|
|
|
|
+ String requestJson = mapper.writeValueAsString(requestParam);
|
|
|
|
|
+ ChatParam param = new ChatParam();
|
|
|
|
|
+ param.setChatId(UuidUtil.getTimeBasedUuid().toString());
|
|
|
|
|
+ param.setStream(false);
|
|
|
|
|
+ param.setDetail(true);
|
|
|
|
|
+ ChatParam.Message message = new ChatParam.Message();
|
|
|
|
|
+ List<ChatParam.Message> messageList = new ArrayList<ChatParam.Message>();
|
|
|
|
|
+ message.setContent(requestJson);
|
|
|
|
|
+ message.setRole("user");
|
|
|
|
|
+ messageList.add(message);
|
|
|
|
|
+ param.setMessages(messageList);
|
|
|
|
|
+ ChatService chatService = SpringUtils.getBean(ChatService.class);
|
|
|
|
|
+ AiHostProper aiHost = SpringUtils.getBean(AiHostProper.class);
|
|
|
|
|
+ R aiResponse = chatService.initiatingTakeChat(param, aiHost.getAiApi(), appKey);
|
|
|
|
|
+ if (aiResponse == null || !Integer.valueOf(200).equals(aiResponse.get("code"))) {
|
|
|
|
|
+ throw new RuntimeException("AI响应异常: " +
|
|
|
|
|
+ (aiResponse != null ? aiResponse.get("msg") : "响应为空"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, String>> tagInfos = CrmCustomerAiTagUtil.extractTagInfos(JSONUtil.toJsonStr(aiResponse));
|
|
|
|
|
+ if (CollectionUtils.isEmpty(tagInfos)) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<CrmCustomerAiTagVo> collect = tagInfos.stream()
|
|
|
|
|
+ .map(tag -> {
|
|
|
|
|
+ return new CrmCustomerAiTagVo()
|
|
|
|
|
+ .setValue(tag.get("value")).setName(tag.get("name"));
|
|
|
|
|
+ })
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ return collect;
|
|
|
|
|
+ }
|
|
|
/**
|
|
/**
|
|
|
* 处理单条数据
|
|
* 处理单条数据
|
|
|
* @param data
|
|
* @param data
|