Skip to content
代码片段 群组 项目
提交 da45c2cd 编辑于 作者: 张宝童's avatar 张宝童
浏览文件

[fix] 重写 HlsRuleEngineServiceImpl, 页面规则布局定义选择lov-dataSourceId引起的NPE

上级 d347ee60
分支 master
No related tags found
无相关合并请求
package com.hand.hls.ruleengine.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.hand.hap.core.IRequest;
import com.hand.hap.intergration.beans.HapJDBCSqlSessionFactory;
import com.hand.hap.system.service.impl.BaseServiceImpl;
import com.hand.hls.fnd.mapper.HlsDbDataSourceMapper;
import com.hand.hls.ruleengine.dto.HlsRuleEngine;
import com.hand.hls.ruleengine.dto.RuleEngineType;
import com.hand.hls.ruleengine.service.IHlsRuleEngineService;
import com.hand.hls.utils.SqlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hand.hls.ruleengine.mapper.HlsRuleEngineMapper;
import com.hand.hls.ruleengine.mapper.RuleEngineTypeMapper;
import com.hand.hls.fnd.components.Datasource2Json;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Transactional
public class HlsRuleEngineServiceImpl extends BaseServiceImpl<HlsRuleEngine> implements IHlsRuleEngineService {
@Autowired
private HlsRuleEngineMapper hlsRuleEngineMapper;
@Autowired
private HapJDBCSqlSessionFactory hapSqlSessionFactory;
@Autowired
private RuleEngineTypeMapper ruleEngineTypeMapper;
@Autowired
private HlsDbDataSourceMapper hlsDbDataSourceMapper;
@Autowired
private Datasource2Json datasource2Json;
private String parseSql(String sql, IRequest iRequest, Map param) {
String oldStr = "\\u007B\\u0024" + "session\\u002E" + "userId" + "\\}";
String oldStr1 = "\\u007B\\u0024" + "session\\u002E" + "companyId" + "\\}";
String newSql = sql.replaceAll(oldStr, Long.toString(iRequest.getUserId()))
.replaceAll(oldStr1, Long.toString(iRequest.getCompanyId()));//userId和companyId先区分处理
String result = paramReplace(newSql, param);
return result;
}
@Override
public List<Map<String, Object>> getHlsRuleEngine(IRequest iRequest, Map param) {
Long ruleEngineId = null;
Long dataSourceId = null;
if (param.get("ruleEngineId") != null) {
ruleEngineId = Long.valueOf(param.get("ruleEngineId").toString());
}
/*if(param.get("dataSourceId") != null){
dataSourceId = Long.valueOf(param.get("dataSourceId").toString());
}*/
param.put("companyId",iRequest.getCompanyId());
HlsRuleEngine hlsRuleEngine = hlsRuleEngineMapper.selectByPrimaryKey(ruleEngineId);
// Long dataSourceId = hlsRuleEngine.getDataSourceId();
// 更改为从数据源获取
String engineType = hlsRuleEngine.getRuleEngineType();
RuleEngineType ruleEngineType = ruleEngineTypeMapper.selectByPrimaryKey(engineType);
//更改为hls_rule_engine_type --> hls_db_data_source -->sql
String resultSql = hlsDbDataSourceMapper.selectByPrimaryKey(ruleEngineType.getDataSourceId()).getSqlContext();
// List<Map<String, Object>> resultList = hapSqlSessionFactory.getJdbcTemplateObject().queryForList();
List<Map<String, Object>> resultList = hapSqlSessionFactory.getJdbcTemplateObject().query(parseSql(SqlUtils.autoTranslate(resultSql), iRequest, param), new RowMapper<Map<String, Object>>() {
@Override
public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
HashMap<String, Object> result = new HashMap<>();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String columnLabel = metaData.getColumnLabel(i);
Object value = rs.getObject(i);
result.put(columnLabel.toLowerCase(Locale.CHINA), value);
}
return result;
}
});
// LOV查询条件 , 前台传过来NAME和CODE
JSONObject paras = (JSONObject) JSONObject.parseObject(param.get("_request_data").toString()).get("parameter");
if (paras.get("name") != null) {
resultList = resultList.stream()
.filter(item -> item.get("name").toString().indexOf(paras.get("name").toString()) != -1)
.collect(Collectors.toList());
}
if (paras.get("code") != null) {
resultList = resultList.stream()
.filter(item -> item.get("code").toString().indexOf(paras.get("code").toString()) != -1)
.collect(Collectors.toList());
}
return resultList;
}
@Override
public HlsRuleEngine queryByType(String type) {
return hlsRuleEngineMapper.queryByType(type);
}
@Override
public List<HlsRuleEngine> selectInfo(IRequest requestContext, HlsRuleEngine dto, int page, int pageSize) {
PageHelper.startPage(page, pageSize);
return hlsRuleEngineMapper.selectInfo(dto);
}
/**
* map为参数集合,格式固定为{$parameter.xxx}
*
* @param paramsql
* @param param
* @return
*/
private static String paramReplace(String paramsql, Map param) {
StringBuilder sql = new StringBuilder(paramsql);
String paramName; //定义参数名
String paramValue; //定义参数值
while (sql.lastIndexOf("${") > 0) {
paramName = sql.subSequence(sql.lastIndexOf("${") + 2, sql.lastIndexOf("}")).toString();//获取标示中的参数名
if (param.get(paramName) != null) {
//区分参数类型
if (param.get(paramName) instanceof Long || param.get(paramName) instanceof Integer) {
paramValue = param.get(paramName).toString();//从HashMap中将参数取出
} else {
paramValue = "\'" + param.get(paramName) + "\'";
}
} else {
paramValue = "null"; //若没有得到该参数的值,则在sql上替换为"null"这样使得sql执行结果为空避免报错
}
sql.replace(sql.lastIndexOf("${"), sql.lastIndexOf("}") + 2, paramValue);
}
return sql.toString();
}
}
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册