修改环境配置

This commit is contained in:
liujiangtao
2020-07-25 12:39:47 +08:00
parent b5b78b4fff
commit 9e73f6c52b
49 changed files with 2746 additions and 83 deletions

View File

@ -0,0 +1,150 @@
package com.muster.logic.BO;
/**
* Description
* <p>
* </p>
* DATE 2020-07-04.
*
* @author 刘江涛.
*/
public class DaoContext {
public DaoContext() {
}
public DaoContext(final String appId, final String bizId, final String uhId, final int userId, final int token) {
this.appId = appId;
this.bizId = bizId;
this.uhId = uhId;
this.userId = userId;
this.token = token;
}
/**
* 系统级设定appid
*/
private String appId = "10001";
/**
* 系统级设定bizid 原则上 fx_bizid>0 时,将无权注册企业
*/
private String bizId = "0";
/**
* 用户操作的终端硬件参数
*/
private String uhId = "test";
/**
* 用户id
*/
private int userId = 1;
/**
* 用户token
*/
private int token = 1;
/**
* Gets appId.
*
* @return the appId
*/
public String getAppId() {
return appId;
}
/**
* Sets appId.
*
* @param appId the appId
*/
public void setAppId(final String appId) {
this.appId = appId;
}
/**
* Gets bizId.
*
* @return the bizId
*/
public String getBizId() {
return bizId;
}
/**
* Sets bizId.
*
* @param bizId the bizId
*/
public void setBizId(final String bizId) {
this.bizId = bizId;
}
/**
* Gets uhId.
*
* @return the uhId
*/
public String getUhId() {
return uhId;
}
/**
* Sets uhId.
*
* @param uhId the uhId
*/
public void setUhId(final String uhId) {
this.uhId = uhId;
}
/**
* Gets userId.
*
* @return the userId
*/
public int getUserId() {
return userId;
}
/**
* Sets userId.
*
* @param userId the userId
*/
public void setUserId(final int userId) {
this.userId = userId;
}
/**
* Gets token.
*
* @return the token
*/
public int getToken() {
return token;
}
/**
* Sets token.
*
* @param token the token
*/
public void setToken(final int token) {
this.token = token;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("DaoContext{");
sb.append("appId='").append(appId).append('\'');
sb.append(", bizId='").append(bizId).append('\'');
sb.append(", uhId='").append(uhId).append('\'');
sb.append(", userId=").append(userId);
sb.append(", token=").append(token);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,9 @@
/**
* Description
* <p>
* </p>
* DATE 2020-07-04.
*
* @author 刘江涛.
*/
package com.muster.logic.BO;

View File

@ -0,0 +1,39 @@
package com.muster.logic;
import com.muster.common.config.MusterConfig;
import com.muster.logic.BO.DaoContext;
import org.slf4j.MDC;
import java.util.Map;
/**
* Description
* <p>
* </p>
* DATE 2020-07-05.
*
* @author 刘江涛.
*/
public class DaoContextUtils {
public static DaoContext createContext() {
String hid = MDC.get("hid");
int userId = Integer.valueOf(MDC.get("userId"));
int token = Integer.valueOf(MDC.get("token"));
String appId = MusterConfig.getAppId();
String bizId = MusterConfig.getBizId();
return new DaoContext(appId, bizId, hid, userId, token);
}
public static DaoContext createContext(final Map<String, Object> params) {
String hid = MDC.get("hid");
int userId = Integer.valueOf(MDC.get("userId"));
int token = Integer.valueOf(MDC.get("token"));
String appId = String.valueOf(params.getOrDefault("appId", MusterConfig.getAppId()));
String bizId = String.valueOf(params.getOrDefault("bizId", MusterConfig.getBizId()));
params.remove("appId");
params.remove("bizId");
return new DaoContext(appId, bizId, hid, userId, token);
}
}

View File

@ -0,0 +1,23 @@
package com.muster.logic;
import com.muster.logic.model.ProcedureResult;
import java.util.Map;
/**
* Description
* <p>
* </p>
* DATE 2020-07-04.
*
* @author 刘江涛.
*/
public interface DbLogicService {
/**
* @param procedureName 存储过程名称,从 ProcedureNameConstants 获取
* @param params 存储过程参数
* @return 返回结果集
*/
ProcedureResult exec(final String procedureName, final Map<String, Object> params);
}

View File

@ -0,0 +1,163 @@
package com.muster.logic;
import com.muster.logic.BO.DaoContext;
import com.muster.logic.model.ProcedureResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Description
* <p>
* </p>
* DATE 2020-07-04.
*
* @author 刘江涛.
*/
@Service
public class DbLogicServiceImpl implements DbLogicService {
Logger log = LoggerFactory.getLogger(DbLogicServiceImpl.class);
private DataSource dataSource;
private final static int OUPUT_INDEX = 6;
@Autowired
DbLogicServiceImpl(@Qualifier("masterDataSource") DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public ProcedureResult exec(final String procedureName, final Map<String, Object> params) {
ProcedureResult result = new ProcedureResult();
DaoContext context = DaoContextUtils.createContext(params);
String sql = this.createProcedure(procedureName, params.size());
try (Connection conn = dataSource.getConnection();
CallableStatement cs = conn.prepareCall(sql)) {
// 设置上下文参数
this.setContext(cs, context);
// 设置业务参数
this.setParams(cs, params);
cs.registerOutParameter(6, Types.INTEGER);
cs.execute();
int res = cs.getInt(OUPUT_INDEX);
result.setRes(res);
if (log.isDebugEnabled()) {
log.debug("{}, {}, {}, {}", procedureName, context.toString(), params, res);
}
List<Map<String, Object>> list = this.getResult(cs);
result.setResult(list);
} catch (SQLException e) {
log.error("{}, {}, {}", procedureName, context.toString(), params, e);
}
return result;
}
private List<Map<String, Object>> getResult(final CallableStatement cs) {
List<Map<String, Object>> list = new ArrayList<>();
try (ResultSet rs = cs.getResultSet();) {
if (Objects.isNull(rs)) {
return list;
}
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<>();
for (int i = 1; i <= columnCount; i++) {
String name = rsmd.getColumnName(i);
String type = rsmd.getColumnClassName(i);
Object value = null;
if ("java.lang.String".equals(type)) {
value = rs.getString(i);
} else if ("java.lang.Integer".equals(type)) {
value = rs.getInt(i);
} else if ("java.sql.Date".equals(type)) {
value = rs.getDate(i);
} else if ("java.math.BigDecimal".equals(type)) {
value = rs.getBigDecimal(i);
} else if ("java.lang.Boolean".equals(type)) {
value = rs.getBoolean(i);
} else if ("java.lang.Long".equals(type)) {
value = rs.getLong(i);
} else if ("java.sql.Timestamp".equals(type)) {
value = rs.getTimestamp(i);
}
map.put(name, value);
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
private void setParams(final CallableStatement cs, final Map<String, Object> params) {
int index = 7;
List<String> keys = params.keySet().stream().sorted().collect(Collectors.toList());
for (int i = 0; i < keys.size(); i++) {
setParam(cs, params, keys.get(i), index);
index++;
}
}
private void setParam(final CallableStatement cs, final Map<String, Object> params, final String key,
final int index) {
Object v = params.get(key);
try {
if (v instanceof String) {
cs.setString(index, v.toString());
} else if (v instanceof Integer) {
cs.setInt(index, Integer.valueOf(v.toString()));
} else if (v instanceof Double) {
cs.setDouble(index, Double.valueOf(v.toString()));
}
} catch (SQLException e) {
}
}
private void setContext(final CallableStatement cs, final DaoContext context) throws SQLException {
cs.setString(1, context.getAppId());
cs.setString(2, context.getBizId());
cs.setString(3, context.getUhId());
cs.setInt(4, context.getUserId());
cs.setInt(5, context.getToken());
}
private String createProcedure(final String name, final int size) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < size; i++) {
str.append(" ,?");
}
return String.format(procedureTemplate, name, str.toString());
}
private final static String procedureTemplate = "call %s(?, ?, ?, ?, ?, ?%s)";
}

View File

@ -0,0 +1,64 @@
package com.muster.logic.model;
import java.util.List;
import java.util.Map;
/**
* Description
* <p>
* </p>
* DATE 2020-07-04.
*
* @author 刘江涛.
*/
public class ProcedureResult {
private int res;
private List<Map<String, Object>> result;
/**
* Gets res.
*
* @return the res
*/
public int getRes() {
return res;
}
/**
* Sets res.
*
* @param res the res
*/
public void setRes(final int res) {
this.res = res;
}
/**
* Gets result.
*
* @return the result
*/
public List<Map<String, Object>> getResult() {
return result;
}
/**
* Sets result.
*
* @param result the result
*/
public void setResult(final List<Map<String, Object>> result) {
this.result = result;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ProcedureResult{");
sb.append("res=").append(res);
sb.append(", result=").append(result);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,9 @@
/**
* Description
* <p>
* </p>
* DATE 2020-07-04.
*
* @author 刘江涛.
*/
package com.muster.logic.model;

View File

@ -0,0 +1,9 @@
/**
* Description
* <p>
* </p>
* DATE 2020-07-04.
*
* @author 刘江涛.
*/
package com.muster.logic;