|
@@ -1,11 +1,18 @@
|
|
|
package com.fs.his.service.impl;
|
|
package com.fs.his.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.fs.common.core.domain.R;
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
import com.fs.common.utils.DateUtils;
|
|
|
|
|
+import com.fs.fastgptApi.config.FastGptApiConfig;
|
|
|
|
|
+import com.fs.fastgptApi.util.HttpUtil;
|
|
|
|
|
+import com.fs.his.config.TestConfig;
|
|
|
import com.fs.his.domain.FsTestTemp;
|
|
import com.fs.his.domain.FsTestTemp;
|
|
|
import com.fs.his.domain.FsTestTempItem;
|
|
import com.fs.his.domain.FsTestTempItem;
|
|
|
import com.fs.his.mapper.FsTestTempItemMapper;
|
|
import com.fs.his.mapper.FsTestTempItemMapper;
|
|
@@ -14,12 +21,17 @@ import com.fs.his.param.FsTestReportParam;
|
|
|
import com.fs.his.utils.TestReportUtil;
|
|
import com.fs.his.utils.TestReportUtil;
|
|
|
import com.fs.his.vo.FsTestReportListVO;
|
|
import com.fs.his.vo.FsTestReportListVO;
|
|
|
import com.fs.his.vo.FsTestReportVO;
|
|
import com.fs.his.vo.FsTestReportVO;
|
|
|
|
|
+import com.fs.system.service.ISysConfigService;
|
|
|
import com.qiniu.util.Json;
|
|
import com.qiniu.util.Json;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.fs.his.mapper.FsTestReportMapper;
|
|
import com.fs.his.mapper.FsTestReportMapper;
|
|
|
import com.fs.his.domain.FsTestReport;
|
|
import com.fs.his.domain.FsTestReport;
|
|
|
import com.fs.his.service.IFsTestReportService;
|
|
import com.fs.his.service.IFsTestReportService;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.fs.his.domain.FsTestReportAiLog;
|
|
|
|
|
+import com.fs.his.mapper.FsTestReportAiLogMapper;
|
|
|
/**
|
|
/**
|
|
|
* 测试报告Service业务层处理
|
|
* 测试报告Service业务层处理
|
|
|
*
|
|
*
|
|
@@ -28,6 +40,7 @@ import com.fs.his.service.IFsTestReportService;
|
|
|
*/
|
|
*/
|
|
|
@SuppressWarnings("all")
|
|
@SuppressWarnings("all")
|
|
|
@Service
|
|
@Service
|
|
|
|
|
+@Slf4j
|
|
|
public class FsTestReportServiceImpl implements IFsTestReportService
|
|
public class FsTestReportServiceImpl implements IFsTestReportService
|
|
|
{
|
|
{
|
|
|
@Autowired
|
|
@Autowired
|
|
@@ -37,6 +50,10 @@ public class FsTestReportServiceImpl implements IFsTestReportService
|
|
|
private FsTestTempItemMapper fsTestTempItemMapper;
|
|
private FsTestTempItemMapper fsTestTempItemMapper;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private FsTestTempMapper fsTestTempMapper;
|
|
private FsTestTempMapper fsTestTempMapper;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private FsTestReportAiLogMapper fsTestReportAiLogMapper;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ISysConfigService configService;
|
|
|
/**
|
|
/**
|
|
|
* 查询测试报告
|
|
* 查询测试报告
|
|
|
*
|
|
*
|
|
@@ -172,10 +189,18 @@ public class FsTestReportServiceImpl implements IFsTestReportService
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
testReport.setCreateTime(DateUtils.getNowDate());
|
|
testReport.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
+
|
|
|
|
|
+ // AI润色测一测答案
|
|
|
|
|
+ Map<String, Object> aiResult = aiOptimize(fsTestTemp.getName(), itemType, maps, itemTypeJSonValue);
|
|
|
|
|
+ itemTypeJSonValue = (String) aiResult.get("content");
|
|
|
testReport.setConditioningPlanJson(itemTypeJSonValue);
|
|
testReport.setConditioningPlanJson(itemTypeJSonValue);
|
|
|
testReport.setTestResult(itemType);
|
|
testReport.setTestResult(itemType);
|
|
|
testReport.setScore(Long.parseLong(allScore+""));
|
|
testReport.setScore(Long.parseLong(allScore+""));
|
|
|
if(fsTestReportMapper.insertFsTestReport(testReport)>0){
|
|
if(fsTestReportMapper.insertFsTestReport(testReport)>0){
|
|
|
|
|
+ if ((Boolean) aiResult.get("isAi")) {
|
|
|
|
|
+ saveAiLog(aiResult, testReport.getReportId(), testReport.getUserId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
testReportUtil.getTestReportImageUrlByReportId(testReport.getReportId());
|
|
testReportUtil.getTestReportImageUrlByReportId(testReport.getReportId());
|
|
|
return R.ok().put("reportId",testReport.getReportId());
|
|
return R.ok().put("reportId",testReport.getReportId());
|
|
|
}
|
|
}
|
|
@@ -256,10 +281,18 @@ public class FsTestReportServiceImpl implements IFsTestReportService
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
testReport.setCreateTime(DateUtils.getNowDate());
|
|
testReport.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
+
|
|
|
|
|
+ // AI润色测一测答案
|
|
|
|
|
+ Map<String, Object> aiResult = aiOptimize(fsTestTemp.getName(), tempType, maps, itemTypeJSonValue);
|
|
|
|
|
+ itemTypeJSonValue = (String) aiResult.get("content");
|
|
|
testReport.setConditioningPlanJson(itemTypeJSonValue);
|
|
testReport.setConditioningPlanJson(itemTypeJSonValue);
|
|
|
testReport.setTestResult(tempType);
|
|
testReport.setTestResult(tempType);
|
|
|
testReport.setScore( Math.round(score));
|
|
testReport.setScore( Math.round(score));
|
|
|
if(fsTestReportMapper.insertFsTestReport(testReport)>0){
|
|
if(fsTestReportMapper.insertFsTestReport(testReport)>0){
|
|
|
|
|
+ if ((Boolean) aiResult.get("isAi")) {
|
|
|
|
|
+ saveAiLog(aiResult, testReport.getReportId(), testReport.getUserId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
testReportUtil.getTestReportImageUrlByReportId(testReport.getReportId());
|
|
testReportUtil.getTestReportImageUrlByReportId(testReport.getReportId());
|
|
|
return R.ok().put("reportId",testReport.getReportId());
|
|
return R.ok().put("reportId",testReport.getReportId());
|
|
|
}
|
|
}
|
|
@@ -271,6 +304,114 @@ public class FsTestReportServiceImpl implements IFsTestReportService
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * AI润色测一测答案
|
|
|
|
|
+ */
|
|
|
|
|
+ private Map<String, Object> aiOptimize(String testName, String result, List<Map> params, String conditioningPlanJsonStr) {
|
|
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ String chatId = "TS" + UUID.randomUUID().toString();
|
|
|
|
|
+ resultMap.put("chatId", chatId);
|
|
|
|
|
+ resultMap.put("content", conditioningPlanJsonStr);
|
|
|
|
|
+ resultMap.put("isAi", false);
|
|
|
|
|
+ resultMap.put("inputTokens", 0);
|
|
|
|
|
+ resultMap.put("outputTokens", 0);
|
|
|
|
|
+ resultMap.put("query", "");
|
|
|
|
|
+ resultMap.put("reasoningText", "");
|
|
|
|
|
+ resultMap.put("modelName", "");
|
|
|
|
|
+ resultMap.put("costTime", 0D);
|
|
|
|
|
+
|
|
|
|
|
+ String json = configService.selectConfigByKey("test.config");
|
|
|
|
|
+ TestConfig testConfig = JSONUtil.toBean(json, TestConfig.class);
|
|
|
|
|
+ if (testConfig.getEnable() == null || !testConfig.getEnable()) {
|
|
|
|
|
+ return resultMap;
|
|
|
|
|
+ }
|
|
|
|
|
+ String appKey = testConfig.getAppKey();
|
|
|
|
|
+ String url = testConfig.getUrl();
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONObject requestJson = new JSONObject();
|
|
|
|
|
+ requestJson.put("stream", false);
|
|
|
|
|
+ requestJson.put("detail", true);
|
|
|
|
|
+ requestJson.put("chatId", chatId);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject content = new JSONObject();
|
|
|
|
|
+ content.put("testName", testName);
|
|
|
|
|
+ content.put("result", result);
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> filteredParams = new ArrayList<>();
|
|
|
|
|
+ for (Map paramMap : params) {
|
|
|
|
|
+ Map<String, Object> filteredMap = new HashMap<>();
|
|
|
|
|
+ filteredMap.put("option", paramMap.get("option"));
|
|
|
|
|
+ filteredMap.put("title", paramMap.get("title"));
|
|
|
|
|
+ filteredParams.add(filteredMap);
|
|
|
|
|
+ }
|
|
|
|
|
+ content.put("params", filteredParams);
|
|
|
|
|
+ content.put("conditioningPlanJson", conditioningPlanJsonStr);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject message = new JSONObject();
|
|
|
|
|
+ message.put("role", "user");
|
|
|
|
|
+ message.put("content", JSON.toJSONString(content));
|
|
|
|
|
+
|
|
|
|
|
+ JSONArray messages = new JSONArray();
|
|
|
|
|
+ messages.add(message);
|
|
|
|
|
+ requestJson.put("messages", messages);
|
|
|
|
|
+
|
|
|
|
|
+ String response = HttpUtil.sendPost(requestJson, url + FastGptApiConfig.completionsUrl, appKey);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("FastGPT Request: {}", requestJson);
|
|
|
|
|
+ log.info("FastGPT Response: {}", response);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(response);
|
|
|
|
|
+
|
|
|
|
|
+ if (jsonObject.containsKey("responseData") && ((JSONArray)jsonObject.get("responseData")).size() > 0) {
|
|
|
|
|
+ JSONArray responseData = jsonObject.getJSONArray("responseData");
|
|
|
|
|
+ responseData.stream().map(o -> (JSONObject) o).filter(o -> "chatNode".equals(o.getString("moduleType"))).findAny().ifPresent(o -> {
|
|
|
|
|
+ resultMap.put("costTime", o.getDoubleValue("runningTime"));
|
|
|
|
|
+ resultMap.put("inputTokens", o.getIntValue("inputTokens"));
|
|
|
|
|
+ resultMap.put("outputTokens", o.getIntValue("outputTokens"));
|
|
|
|
|
+ resultMap.put("query", o.getString("query"));
|
|
|
|
|
+ resultMap.put("reasoningText", o.getString("reasoningText"));
|
|
|
|
|
+ resultMap.put("modelName", o.getString("model"));
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (jsonObject.containsKey("choices") && jsonObject.getJSONArray("choices").size() > 0) {
|
|
|
|
|
+ JSONObject choice = (JSONObject) jsonObject.getJSONArray("choices").get(0);
|
|
|
|
|
+ if (choice.containsKey("message")) {
|
|
|
|
|
+ JSONObject messageObj = (JSONObject) choice.get("message");
|
|
|
|
|
+ resultMap.put("content", messageObj.getString("content"));
|
|
|
|
|
+ resultMap.put("isAi", true);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("AI优化失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return resultMap;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存ai回复
|
|
|
|
|
+ */
|
|
|
|
|
+ private void saveAiLog(Map<String, Object> aiResult, Long reportId, Long userId) {
|
|
|
|
|
+ FsTestReportAiLog aiLog = new FsTestReportAiLog();
|
|
|
|
|
+ aiLog.setReportId(reportId);
|
|
|
|
|
+ aiLog.setUserId(userId);
|
|
|
|
|
+ aiLog.setChatId((String) aiResult.get("chatId"));
|
|
|
|
|
+ aiLog.setModelName((String) aiResult.get("modelName"));
|
|
|
|
|
+ aiLog.setQueryContent((String) aiResult.get("query"));
|
|
|
|
|
+ aiLog.setReasoningText((String) aiResult.get("reasoningText"));
|
|
|
|
|
+ aiLog.setAiContent((String) aiResult.get("content"));
|
|
|
|
|
+ aiLog.setInputTokens((Integer) aiResult.get("inputTokens"));
|
|
|
|
|
+ aiLog.setOutputTokens((Integer) aiResult.get("outputTokens"));
|
|
|
|
|
+ aiLog.setTotalTokens((Integer) aiResult.get("inputTokens") + (Integer) aiResult.get("outputTokens"));
|
|
|
|
|
+ aiLog.setCostTime((Double) aiResult.get("costTime"));
|
|
|
|
|
+ aiLog.setCreateTime(LocalDateTime.now());
|
|
|
|
|
+ fsTestReportAiLogMapper.insert(aiLog);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 修改测试报告
|
|
* 修改测试报告
|
|
|
*
|
|
*
|
|
@@ -320,7 +461,17 @@ public class FsTestReportServiceImpl implements IFsTestReportService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<FsTestReportAiLog> selectAiLogByReportId(Long reportId) {
|
|
|
|
|
+ LambdaQueryWrapper<FsTestReportAiLog> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(FsTestReportAiLog::getReportId, reportId)
|
|
|
|
|
+ .orderByDesc(FsTestReportAiLog::getCreateTime);
|
|
|
|
|
+ return fsTestReportAiLogMapper.selectList(wrapper);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Long selectTokenTotal(FsTestReportParam param) {
|
|
|
|
|
+ return fsTestReportMapper.selectTokenTotal(param);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|
|
|
|
|
+}
|