添加代码 QueryTemplateProcessor.process

main
xueqingkun 1 year ago
parent 55985907dc
commit 890258e1fc

@ -1,6 +1,5 @@
package com.supervision.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@ -37,7 +36,7 @@ public class IrSessionParam implements Serializable {
private String paramValue;
/**
*
* @see ParamTypeConverter.typeMap
*/
private String paramType;

@ -1,6 +1,5 @@
package com.supervision.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@ -24,7 +23,7 @@ public class IrSqlParam implements Serializable {
/**
* ID
*/
private Integer knowledgeId;
private String knowledgeId;
/**
*

@ -1,24 +1,23 @@
package com.supervision.service;
import java.util.Map;
import freemarker.template.TemplateException;
import java.io.IOException;
/**
*
*/
public interface QueryTemplateProcessor {
/**
* id
* @param templateId id
* @return
*/
String query(String templateId);
/**
* id
* idid
* @param templateId id
* @param input
* @return
* @param sessionId id
* @return
* @throws TemplateException
* @throws IOException
*/
String query(String templateId, Map<String, Object> input);
String process(String templateId,String sessionId) throws TemplateException, IOException;
}

@ -9,19 +9,14 @@ import com.supervision.service.IrSessionParamService;
import com.supervision.service.IrSqlParamService;
import com.supervision.service.QueryTemplateProcessor;
import com.supervision.util.ParamTypeConverter;
import com.supervision.util.freemark.StringTemplateConfig;
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;
@ -38,79 +33,33 @@ public class QueryTemplateProcessorImpl implements QueryTemplateProcessor {
private final IrSessionParamService sessionParamService;
private final freemarker.template.Configuration databaseFreemarkerConfiguration;
private final RowSqlMapper rowSqlMapper;
@Override
public String query(String templateId) {
public String process(String templateId,String sessionId) throws TemplateException, IOException {
Assert.notEmpty(templateId, "模板ID不能为空");
// 根据模板ID查询查询sql模板和结果模板
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();
List<IrSessionParam> sessionParams = sessionParamService.lambdaQuery().eq(IrSessionParam::getSessionId, sessionId).list();
Set<String> sessionParamsNames = sessionParams.stream().map(IrSessionParam::getParamName).collect(Collectors.toSet());
paramsList.forEach(param -> Assert.isTrue(sessionParamsNames.contains(param.getId())));
paramsList.forEach(param -> Assert.isTrue(sessionParamsNames.contains(param.getParamName()), "参数["+param.getParamName()+"]未设置!"));
// 获取执行sql
// 执行查询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);
Map<String, Object> params = sessionParams.stream().collect(Collectors.toMap(IrSessionParam::getParamName, v->converter.convert(v.getParamType(),v.getParamValue()), (v1, v2) -> v1));
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();
return StringTemplateConfig.getInstance().process(knowledge.getResultTemplate(), maps);
}

@ -1,5 +1,6 @@
package com.supervision.util.freemark;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import freemarker.cache.StringTemplateLoader;
@ -10,6 +11,7 @@ import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -54,7 +56,7 @@ public class StringTemplateConfig {
return true;
}
public String process(String content, Map<String, Object> data) throws IOException, TemplateException {
public String process(String content, List<Map<String, Object>> data) throws IOException, TemplateException {
if (StrUtil.isEmpty(content)) {
return null;
}
@ -64,7 +66,14 @@ public class StringTemplateConfig {
}
Template template = this.getTemplate(name);
StringWriter out = new StringWriter();
template.process(data, out);
Map<String, Object> dataModel = new HashMap<>();
if (CollUtil.size(data) > 1){
dataModel.put("list", data);
}else {
dataModel= CollUtil.getFirst(data);
}
template.process(dataModel, out);
return out.toString();
}

@ -1,5 +1,6 @@
package com.supervision.util.mybatis;
import cn.hutool.crypto.digest.MD5;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.scripting.LanguageDriver;
@ -28,7 +29,7 @@ public class RowMapperStatementBuilder {
* @return
*/
private String generateMappedStatementId(String sql, SqlCommandType sqlCommandType) {
return sqlCommandType.toString() + "." + sql.hashCode();
return sqlCommandType.toString() + "." + MD5.create().digestHex(sql);
}
/**
@ -94,7 +95,7 @@ public class RowMapperStatementBuilder {
}
public String selectDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.SELECT);
String msId = generateMappedStatementId(sql , SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}

@ -6,7 +6,7 @@
<resultMap id="BaseResultMap" type="com.supervision.domain.IrSqlParam">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="knowledgeId" column="knowledge_id" jdbcType="INTEGER"/>
<result property="knowledgeId" column="knowledge_id" jdbcType="VARCHAR"/>
<result property="paramName" column="param_name" jdbcType="VARCHAR"/>
<result property="paramType" column="param_type" jdbcType="VARCHAR"/>
<result property="paramRequire" column="param_require" jdbcType="INTEGER"/>

@ -1,6 +1,7 @@
package com.supervision;
import com.supervision.service.IrKnowledgeService;
import com.supervision.service.QueryTemplateProcessor;
import com.supervision.util.mybatis.RowSqlMapper;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
@ -92,5 +93,16 @@ class AskApplicationTests {
}
@Autowired
private QueryTemplateProcessor queryTemplateProcessor;
@Test
void QueryTemplateProcessorProcessTest() throws IOException, TemplateException {
String process = queryTemplateProcessor.process("1770984442920292354","123");
System.out.println(process);
}
}

Loading…
Cancel
Save