Merge remote-tracking branch 'origin/main'
commit
f5c2120c47
@ -0,0 +1,15 @@
|
||||
package com.supervision.service;
|
||||
|
||||
/**
|
||||
* 对话服务
|
||||
*
|
||||
*/
|
||||
public interface TalkService {
|
||||
|
||||
/**
|
||||
* 回答问题
|
||||
* @param questionId 问题id
|
||||
* @return 回答内容
|
||||
*/
|
||||
String answer(String questionId);
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package com.supervision.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.supervision.domain.IrKnowledge;
|
||||
import com.supervision.domain.IrSessionParam;
|
||||
import com.supervision.domain.IrSqlParam;
|
||||
import com.supervision.service.IrKnowledgeService;
|
||||
import com.supervision.service.IrSessionParamService;
|
||||
import com.supervision.service.IrSqlParamService;
|
||||
import com.supervision.service.QueryTemplateProcessor;
|
||||
import com.supervision.util.ParamTypeConverter;
|
||||
import com.supervision.util.mybatis.RowSqlMapper;
|
||||
import freemarker.cache.StringTemplateLoader;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.swing.tree.RowMapper;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class QueryTemplateProcessorImpl implements QueryTemplateProcessor {
|
||||
|
||||
private final IrKnowledgeService knowledgeService;
|
||||
|
||||
private final IrSqlParamService sqlParamService;
|
||||
|
||||
private final IrSessionParamService sessionParamService;
|
||||
|
||||
private final freemarker.template.Configuration databaseFreemarkerConfiguration;
|
||||
|
||||
private final RowSqlMapper rowSqlMapper;
|
||||
@Override
|
||||
public String query(String templateId) {
|
||||
|
||||
Assert.notEmpty(templateId, "模板ID不能为空");
|
||||
|
||||
IrKnowledge knowledge = knowledgeService.getById(templateId);
|
||||
Assert.notNull(knowledge, "知识不存在!");
|
||||
Assert.notEmpty(knowledge.getSqlTemplate(), "模板内容不能为空");
|
||||
Assert.notEmpty(knowledge.getResultTemplate(), "结果模板不能为空");
|
||||
// 查询必填的参数
|
||||
List<IrSqlParam> paramsList = sqlParamService.lambdaQuery().eq(IrSqlParam::getKnowledgeId, templateId).eq(IrSqlParam::getParamRequire, 1).list();
|
||||
|
||||
// 校验必填参数
|
||||
List<IrSessionParam> sessionParams = sessionParamService.lambdaQuery().eq(IrSessionParam::getSessionId, templateId).list();
|
||||
Set<String> sessionParamsNames = sessionParams.stream().map(IrSessionParam::getParamName).collect(Collectors.toSet());
|
||||
paramsList.forEach(param -> Assert.isTrue(sessionParamsNames.contains(param.getId())));
|
||||
|
||||
|
||||
// 获取执行sql
|
||||
ParamTypeConverter converter = ParamTypeConverter.getInstance();
|
||||
Map<String, Object> params = sessionParams.stream().collect(Collectors.toMap(IrSessionParam::getParamName, v->converter.convert(v.getParamValue(),v.getParamType()), (v1, v2) -> v1));
|
||||
String query = query(templateId, params);
|
||||
|
||||
List<Map<String, Object>> maps = rowSqlMapper.selectList(knowledge.getSqlTemplate(), params);
|
||||
|
||||
|
||||
// 组装返回结果
|
||||
|
||||
//templateProcess(maps, knowledge.getResultTemplate())
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String query(String templateId, Map<String, Object> input) {
|
||||
|
||||
Template template = null;
|
||||
try {
|
||||
template = databaseFreemarkerConfiguration.getTemplate(templateId);
|
||||
} catch (IOException e) {
|
||||
log.error("加载模板失败:模板id:{}",templateId,e);
|
||||
return null;
|
||||
}
|
||||
//databaseFreemarkerConfiguration.gett
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
template.process(input, writer);
|
||||
} catch (TemplateException | IOException e) {
|
||||
log.error("模板执行失败",e);
|
||||
return null;
|
||||
}finally {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
log.error("关闭流失败", e);
|
||||
}
|
||||
}
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
private String templateProcess(String templateId,String context, Map<String, Object> input) throws IOException, TemplateException {
|
||||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_30);
|
||||
|
||||
StringTemplateLoader stringLoader = new StringTemplateLoader();
|
||||
stringLoader.putTemplate(templateId, context);
|
||||
cfg.setTemplateLoader(stringLoader);
|
||||
Template template = cfg.getTemplate(templateId);
|
||||
StringWriter output = new StringWriter();
|
||||
template.process(input, output);
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue