添加代码 QueryTemplateProcessor.process

main
xueqingkun 1 year ago
parent 55985907dc
commit 890258e1fc

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

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

@ -1,24 +1,23 @@
package com.supervision.service; package com.supervision.service;
import java.util.Map; import freemarker.template.TemplateException;
import java.io.IOException;
/** /**
* *
*/ */
public interface QueryTemplateProcessor { public interface QueryTemplateProcessor {
/**
* id
* @param templateId id
* @return
*/
String query(String templateId);
/** /**
* id * idid
* @param templateId id * @param templateId id
* @param input * @param sessionId id
* @return * @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.IrSqlParamService;
import com.supervision.service.QueryTemplateProcessor; import com.supervision.service.QueryTemplateProcessor;
import com.supervision.util.ParamTypeConverter; import com.supervision.util.ParamTypeConverter;
import com.supervision.util.freemark.StringTemplateConfig;
import com.supervision.util.mybatis.RowSqlMapper; import com.supervision.util.mybatis.RowSqlMapper;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.swing.tree.RowMapper;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -38,79 +33,33 @@ public class QueryTemplateProcessorImpl implements QueryTemplateProcessor {
private final IrSessionParamService sessionParamService; private final IrSessionParamService sessionParamService;
private final freemarker.template.Configuration databaseFreemarkerConfiguration;
private final RowSqlMapper rowSqlMapper; private final RowSqlMapper rowSqlMapper;
@Override @Override
public String query(String templateId) { public String process(String templateId,String sessionId) throws TemplateException, IOException {
Assert.notEmpty(templateId, "模板ID不能为空"); Assert.notEmpty(templateId, "模板ID不能为空");
// 根据模板ID查询查询sql模板和结果模板
IrKnowledge knowledge = knowledgeService.getById(templateId); IrKnowledge knowledge = knowledgeService.getById(templateId);
Assert.notNull(knowledge, "知识不存在!"); Assert.notNull(knowledge, "知识不存在!");
Assert.notEmpty(knowledge.getSqlTemplate(), "模板内容不能为空"); Assert.notEmpty(knowledge.getSqlTemplate(), "模板内容不能为空");
Assert.notEmpty(knowledge.getResultTemplate(), "结果模板不能为空"); Assert.notEmpty(knowledge.getResultTemplate(), "结果模板不能为空");
// 查询必填的参数 // 查询必填的参数
List<IrSqlParam> paramsList = sqlParamService.lambdaQuery().eq(IrSqlParam::getKnowledgeId, templateId).eq(IrSqlParam::getParamRequire, 1).list(); 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()); 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(); 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)); Map<String, Object> params = sessionParams.stream().collect(Collectors.toMap(IrSessionParam::getParamName, v->converter.convert(v.getParamType(),v.getParamValue()), (v1, v2) -> v1));
String query = query(templateId, params);
List<Map<String, Object>> maps = rowSqlMapper.selectList(knowledge.getSqlTemplate(), params); List<Map<String, Object>> maps = rowSqlMapper.selectList(knowledge.getSqlTemplate(), params);
// 组装返回结果 // 组装返回结果
return StringTemplateConfig.getInstance().process(knowledge.getResultTemplate(), maps);
//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();
} }

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

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

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

@ -1,6 +1,7 @@
package com.supervision; package com.supervision;
import com.supervision.service.IrKnowledgeService; import com.supervision.service.IrKnowledgeService;
import com.supervision.service.QueryTemplateProcessor;
import com.supervision.util.mybatis.RowSqlMapper; import com.supervision.util.mybatis.RowSqlMapper;
import freemarker.cache.StringTemplateLoader; import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration; 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