refactor(ims): 重构高压IMS数据库访问逻辑
This commit is contained in:
parent
742a08be8e
commit
70f72c23a8
|
|
@ -8,9 +8,8 @@ import nc.bs.logging.Log;
|
||||||
import nc.bs.pub.pa.PreAlertObject;
|
import nc.bs.pub.pa.PreAlertObject;
|
||||||
import nc.bs.pub.taskcenter.BgWorkingContext;
|
import nc.bs.pub.taskcenter.BgWorkingContext;
|
||||||
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
|
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
|
||||||
|
import nc.bs.uapbd.util.GyImsDbUtil;
|
||||||
import nc.bs.uapbd.util.MyHelper;
|
import nc.bs.uapbd.util.MyHelper;
|
||||||
import nc.jdbc.framework.SQLParameter;
|
|
||||||
import nc.jdbc.framework.processor.MapListProcessor;
|
|
||||||
import nc.jdbc.framework.processor.MapProcessor;
|
import nc.jdbc.framework.processor.MapProcessor;
|
||||||
import nc.vo.ic.m45.entity.PurchaseInVO;
|
import nc.vo.ic.m45.entity.PurchaseInVO;
|
||||||
import nc.vo.pub.BusinessException;
|
import nc.vo.pub.BusinessException;
|
||||||
|
|
@ -32,28 +31,14 @@ import java.util.Map;
|
||||||
public class ErpIaI2billFromGyImsPlugin implements IBackgroundWorkPlugin {
|
public class ErpIaI2billFromGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
private static final String LOG_INFO_NAME = "gyimslog";
|
private static final String LOG_INFO_NAME = "gyimslog";
|
||||||
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
||||||
private SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
|
private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
private SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
private final SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
// IMS数据源
|
|
||||||
public BaseDAO imsDao;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取IMS数据源
|
|
||||||
*/
|
|
||||||
public BaseDAO getImsDao() {
|
|
||||||
if (imsDao == null) {
|
|
||||||
imsDao = new BaseDAO("gyims");
|
|
||||||
// 禁用时间戳ts
|
|
||||||
imsDao.setAddTimeStamp(false);
|
|
||||||
}
|
|
||||||
return imsDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreAlertObject executeTask(BgWorkingContext bgWorkingContext) throws BusinessException {
|
public PreAlertObject executeTask(BgWorkingContext bgWorkingContext) throws BusinessException {
|
||||||
// 查询高压ims中间表需要同步的数据
|
// 查询高压ims中间表需要同步的数据
|
||||||
String sql = "SELECT * FROM BIPInputMainTab a WHERE a.status = 'C'";
|
String sql = "SELECT * FROM BIPInputMainTab a WHERE a.status = 'C'";
|
||||||
List<Map<String, Object>> remain = (List<Map<String, Object>>) getImsDao().executeQuery(sql, new MapListProcessor());
|
List<Map<String, Object>> remain = GyImsDbUtil.query(sql);
|
||||||
if (remain == null || remain.isEmpty()) {
|
if (remain == null || remain.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -64,7 +49,7 @@ public class ErpIaI2billFromGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
String status = map.get("status") + "";
|
String status = map.get("status") + "";
|
||||||
try {
|
try {
|
||||||
String bodySql = " SELECT *" + " from BIPInputDetailTab a " + " where a.cgeneralhid ='" + cgeneralhid + "'";
|
String bodySql = " SELECT *" + " from BIPInputDetailTab a " + " where a.cgeneralhid ='" + cgeneralhid + "'";
|
||||||
List<Map<String, Object>> bodyList = (List<Map<String, Object>>) getImsDao().executeQuery(bodySql, new MapListProcessor());
|
List<Map<String, Object>> bodyList = GyImsDbUtil.query(bodySql);
|
||||||
if (bodyList == null || bodyList.isEmpty()) {
|
if (bodyList == null || bodyList.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -160,14 +145,12 @@ public class ErpIaI2billFromGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
updateSql = updateSql.replace("[err_msg]", "新增返回空");
|
updateSql = updateSql.replace("[err_msg]", "新增返回空");
|
||||||
}
|
}
|
||||||
updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid);
|
updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid);
|
||||||
getImsDao().executeUpdate(updateSql);
|
GyImsDbUtil.update(updateSql);
|
||||||
} catch (BusinessException e) {
|
} catch (BusinessException e) {
|
||||||
logger.error("ErpIaI2billFromGyImsPlugin Error: " + e.getMessage(), e);
|
logger.error("ErpIaI2billFromGyImsPlugin Error: " + e.getMessage(), e);
|
||||||
String updateSql = "update BIPInputMainTab set err_msg = ? where cgeneralhid = ?";
|
String updateSql = "update BIPInputMainTab set err_msg = ? where cgeneralhid = ?";
|
||||||
SQLParameter parameter = new SQLParameter();
|
Object[] params = new Object[]{e.getMessage(), cgeneralhid};
|
||||||
parameter.addParam(e.getMessage());
|
int rows = GyImsDbUtil.update(updateSql, params);
|
||||||
parameter.addParam(cgeneralhid);
|
|
||||||
int rows = getImsDao().executeUpdate(updateSql, parameter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,11 @@ import nc.bs.logging.Log;
|
||||||
import nc.bs.pub.pa.PreAlertObject;
|
import nc.bs.pub.pa.PreAlertObject;
|
||||||
import nc.bs.pub.taskcenter.BgWorkingContext;
|
import nc.bs.pub.taskcenter.BgWorkingContext;
|
||||||
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
|
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
|
||||||
|
import nc.bs.uapbd.util.GyImsDbUtil;
|
||||||
import nc.bs.uapbd.util.MyHelper;
|
import nc.bs.uapbd.util.MyHelper;
|
||||||
import nc.itf.mmpac.pickm.IPickmQueryService;
|
import nc.itf.mmpac.pickm.IPickmQueryService;
|
||||||
import nc.itf.uap.pf.busiflow.PfButtonClickContext;
|
import nc.itf.uap.pf.busiflow.PfButtonClickContext;
|
||||||
import nc.jdbc.framework.SQLParameter;
|
|
||||||
import nc.jdbc.framework.processor.ColumnProcessor;
|
import nc.jdbc.framework.processor.ColumnProcessor;
|
||||||
import nc.jdbc.framework.processor.MapListProcessor;
|
|
||||||
import nc.pubitf.ic.m4d.api.IMaterialOutMaintainAPI;
|
import nc.pubitf.ic.m4d.api.IMaterialOutMaintainAPI;
|
||||||
import nc.util.mmf.busi.service.PFPubService;
|
import nc.util.mmf.busi.service.PFPubService;
|
||||||
import nc.util.mmf.framework.base.MMValueCheck;
|
import nc.util.mmf.framework.base.MMValueCheck;
|
||||||
|
|
@ -45,39 +44,25 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
private static final String LOG_INFO_NAME = "gyimslog";
|
private static final String LOG_INFO_NAME = "gyimslog";
|
||||||
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
||||||
|
|
||||||
private SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
|
private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
private SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
private final SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
// BIP数据源
|
// BIP数据源
|
||||||
public static final BaseDAO baseDAO = new BaseDAO();
|
public static final BaseDAO baseDAO = new BaseDAO();
|
||||||
// IMS数据源
|
|
||||||
public BaseDAO imsDao;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取IMS数据源
|
|
||||||
*/
|
|
||||||
public BaseDAO getImsDao() {
|
|
||||||
if (imsDao == null) {
|
|
||||||
imsDao = new BaseDAO("gyims");
|
|
||||||
// 禁用时间戳ts
|
|
||||||
imsDao.setAddTimeStamp(false);
|
|
||||||
}
|
|
||||||
return imsDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
|
public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
|
||||||
logger.error("---start----高压IMS-材料出库-任务开始运行--");
|
logger.error("---start----高压IMS-材料出库-任务开始运行--");
|
||||||
IPickmQueryService queryService = NCLocator.getInstance().lookup(IPickmQueryService.class);
|
IPickmQueryService queryService = NCLocator.getInstance().lookup(IPickmQueryService.class);
|
||||||
// IMS 同步状态:BIP:C-创建;U-修改D-删除 IMS:2-MES单据接收完成
|
// IMS 同步状态:BIP:C-创建;U-修改D-删除 IMS:2-MES单据接收完成
|
||||||
String selSql = "select * from BIPOutMainTab where status = 'C'";
|
String selSql = "select * from BIPOutMainTab where status = 'C1'";
|
||||||
List<Map<String, Object>> mainList = (List<Map<String, Object>>) getImsDao().executeQuery(selSql, new MapListProcessor());
|
List<Map<String, Object>> mainList = GyImsDbUtil.query(selSql);
|
||||||
for (Map<String, Object> mainMap : mainList) {
|
for (Map<String, Object> mainMap : mainList) {
|
||||||
String cgeneralhid = mainMap.get("cgeneralhid") + "";
|
String cgeneralhid = mainMap.get("cgeneralhid") + "";
|
||||||
// 同步状态:IMS:C-创建;U-修改; 4-BIP审核出库MES接收确认 BIP:2-BIP审核出库
|
// 同步状态:IMS:C-创建;U-修改; 4-BIP审核出库MES接收确认 BIP:2-BIP审核出库
|
||||||
String status = mainMap.get("status") + "";
|
String status = mainMap.get("status") + "";
|
||||||
try {
|
try {
|
||||||
String pkSql = "select * from BIPOutDetailTab where cgeneralhid = '" + cgeneralhid + "'";
|
String pkSql = "select * from BIPOutDetailTab where cgeneralhid = '" + cgeneralhid + "'";
|
||||||
List<Map<String, Object>> detailList = (List<Map<String, Object>>) getImsDao().executeQuery(pkSql, new MapListProcessor());
|
List<Map<String, Object>> detailList = GyImsDbUtil.query(pkSql);
|
||||||
Map<String, Map<String, Object>> detailIdMap = new HashMap<>();
|
Map<String, Map<String, Object>> detailIdMap = new HashMap<>();
|
||||||
for (Map<String, Object> detailMap : detailList) {
|
for (Map<String, Object> detailMap : detailList) {
|
||||||
detailIdMap.put(detailMap.get("vbdef17") + "", detailMap);
|
detailIdMap.put(detailMap.get("vbdef17") + "", detailMap);
|
||||||
|
|
@ -166,14 +151,12 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
updateSql = updateSql.replace("[err_msg]", "新增返回空");
|
updateSql = updateSql.replace("[err_msg]", "新增返回空");
|
||||||
}
|
}
|
||||||
updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid);
|
updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid);
|
||||||
getImsDao().executeUpdate(updateSql);
|
GyImsDbUtil.update(updateSql);
|
||||||
} catch (BusinessException e) {
|
} catch (BusinessException e) {
|
||||||
logger.error("MaterialOutGyImsPlugin Error: ", e);
|
logger.error("MaterialOutGyImsPlugin Error: ", e);
|
||||||
String updateSql = "update BIPOutMainTab set err_msg = ? where cgeneralhid = ?";
|
String updateSql = "update BIPOutMainTab set err_msg = ? where cgeneralhid = ?";
|
||||||
SQLParameter parameter = new SQLParameter();
|
Object[] params = new Object[]{e.getMessage(), cgeneralhid};
|
||||||
parameter.addParam(e.getMessage());
|
int rows = GyImsDbUtil.update(updateSql, params);
|
||||||
parameter.addParam(cgeneralhid);
|
|
||||||
int rows = getImsDao().executeUpdate(updateSql, parameter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.error("---end----高压IMS-材料出库-任务结束运行--");
|
logger.error("---end----高压IMS-材料出库-任务结束运行--");
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,12 @@ import nc.bs.logging.Log;
|
||||||
import nc.bs.pub.pa.PreAlertObject;
|
import nc.bs.pub.pa.PreAlertObject;
|
||||||
import nc.bs.pub.taskcenter.BgWorkingContext;
|
import nc.bs.pub.taskcenter.BgWorkingContext;
|
||||||
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
|
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
|
||||||
|
import nc.bs.uapbd.util.GyImsDbUtil;
|
||||||
import nc.bs.uapbd.util.MyHelper;
|
import nc.bs.uapbd.util.MyHelper;
|
||||||
import nc.itf.mmpac.pmo.pac0002.IPMOQueryService;
|
import nc.itf.mmpac.pmo.pac0002.IPMOQueryService;
|
||||||
import nc.itf.mmpac.wr.IWrMaintainService;
|
import nc.itf.mmpac.wr.IWrMaintainService;
|
||||||
import nc.itf.uap.pf.busiflow.PfButtonClickContext;
|
import nc.itf.uap.pf.busiflow.PfButtonClickContext;
|
||||||
import nc.jdbc.framework.SQLParameter;
|
|
||||||
import nc.jdbc.framework.processor.ColumnProcessor;
|
import nc.jdbc.framework.processor.ColumnProcessor;
|
||||||
import nc.jdbc.framework.processor.MapListProcessor;
|
|
||||||
import nc.util.mmf.busi.service.PFPubService;
|
import nc.util.mmf.busi.service.PFPubService;
|
||||||
import nc.util.mmf.framework.base.MMStringUtil;
|
import nc.util.mmf.framework.base.MMStringUtil;
|
||||||
import nc.util.mmf.framework.base.MMValueCheck;
|
import nc.util.mmf.framework.base.MMValueCheck;
|
||||||
|
|
@ -46,24 +45,10 @@ import java.util.Map;
|
||||||
public class ProductReportGyImsPlugin implements IBackgroundWorkPlugin {
|
public class ProductReportGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
private static final String LOG_INFO_NAME = "gyimslog";
|
private static final String LOG_INFO_NAME = "gyimslog";
|
||||||
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
||||||
private SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
|
private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
private SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
private final SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
// BIP数据源
|
// BIP数据源
|
||||||
public static final BaseDAO baseDAO = new BaseDAO();
|
public static final BaseDAO baseDAO = new BaseDAO();
|
||||||
// IMS数据源
|
|
||||||
public BaseDAO imsDao;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取IMS数据源
|
|
||||||
*/
|
|
||||||
public BaseDAO getImsDao() {
|
|
||||||
if (imsDao == null) {
|
|
||||||
imsDao = new BaseDAO("gyims");
|
|
||||||
// 禁用时间戳ts
|
|
||||||
imsDao.setAddTimeStamp(false);
|
|
||||||
}
|
|
||||||
return imsDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
|
public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
|
||||||
|
|
@ -71,15 +56,15 @@ public class ProductReportGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
IPMOQueryService pmoQueryService = NCLocator.getInstance().lookup(IPMOQueryService.class);
|
IPMOQueryService pmoQueryService = NCLocator.getInstance().lookup(IPMOQueryService.class);
|
||||||
IWrMaintainService wrService = NCLocator.getInstance().lookup(IWrMaintainService.class);
|
IWrMaintainService wrService = NCLocator.getInstance().lookup(IWrMaintainService.class);
|
||||||
String selSql = "select * from BIPReportMainTab where status = 'C'";
|
String selSql = "select * from BIPReportMainTab where status = 'C'";
|
||||||
List<Map<String, Object>> mainList = (List<Map<String, Object>>) getImsDao().executeQuery(selSql, new MapListProcessor());
|
List<Map<String, Object>> mainList = GyImsDbUtil.query(selSql);
|
||||||
for (Map<String, Object> mainMap : mainList) {
|
for (Map<String, Object> mainMap : mainList) {
|
||||||
// IMS生产报告的主键
|
// IMS生产报告的主键
|
||||||
String pkWr = mainMap.get("pk_wr").toString();
|
String pkWr = mainMap.get("pk_wr") + "";
|
||||||
// 同步标识 IMS:C-创建;U-修改 BIP:2-BIP单据接收完成
|
// 同步标识 IMS:C-创建;U-修改 BIP:2-BIP单据接收完成
|
||||||
String status = mainMap.get("status").toString();
|
String status = mainMap.get("status").toString();
|
||||||
try {
|
try {
|
||||||
String pkWrSql = "select * from BIPReportDetailTab where pk_wr = '" + pkWr + "'";
|
String pkWrSql = "select * from BIPReportDetailTab where pk_wr = '" + pkWr + "'";
|
||||||
List<Map<String, Object>> detailList = (List<Map<String, Object>>) getImsDao().executeQuery(pkWrSql, new MapListProcessor());
|
List<Map<String, Object>> detailList = GyImsDbUtil.query(pkWrSql);
|
||||||
Map<String, Map<String, Object>> detailIdMap = new HashMap<>();
|
Map<String, Map<String, Object>> detailIdMap = new HashMap<>();
|
||||||
for (Map<String, Object> detailMap : detailList) {
|
for (Map<String, Object> detailMap : detailList) {
|
||||||
detailIdMap.put(detailMap.get("vbdef23").toString(), detailMap);
|
detailIdMap.put(detailMap.get("vbdef23").toString(), detailMap);
|
||||||
|
|
@ -168,14 +153,12 @@ public class ProductReportGyImsPlugin implements IBackgroundWorkPlugin {
|
||||||
updateSql = updateSql.replace("[err_msg]", "新增返回空");
|
updateSql = updateSql.replace("[err_msg]", "新增返回空");
|
||||||
}
|
}
|
||||||
updateSql = updateSql.replace("[pkWr]", pkWr);
|
updateSql = updateSql.replace("[pkWr]", pkWr);
|
||||||
getImsDao().executeUpdate(updateSql);
|
GyImsDbUtil.update(updateSql);
|
||||||
} catch (BusinessException e) {
|
} catch (BusinessException e) {
|
||||||
logger.error("ProductReportGyImsPlugin Error: " + e.getMessage(), e);
|
logger.error("ProductReportGyImsPlugin Error: " + e.getMessage(), e);
|
||||||
String updateSql = "update BIPReportMainTab set err_msg = ? where pk_wr = ?";
|
String updateSql = "update BIPReportMainTab set err_msg = ? where pk_wr = ?";
|
||||||
SQLParameter parameter = new SQLParameter();
|
Object[] params = new Object[]{e.getMessage(), pkWr};
|
||||||
parameter.addParam(e.getMessage());
|
int rows = GyImsDbUtil.update(updateSql, params);
|
||||||
parameter.addParam(pkWr);
|
|
||||||
int rows = getImsDao().executeUpdate(updateSql, parameter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.error("---end----高压IMS-生产报告-任务结束运行--");
|
logger.error("---end----高压IMS-生产报告-任务结束运行--");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,541 @@
|
||||||
|
package nc.bs.uapbd.util;
|
||||||
|
|
||||||
|
import nc.bs.logging.Log;
|
||||||
|
import nc.vo.pub.BusinessException;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 高压IMS数据库连接工具类
|
||||||
|
*
|
||||||
|
* @author system
|
||||||
|
* @date 2025/01/01
|
||||||
|
*/
|
||||||
|
public class GyImsDbUtil {
|
||||||
|
|
||||||
|
private static final Log logger = Log.getInstance("gyimslog");
|
||||||
|
// 数据库连接信息
|
||||||
|
private static final String DB_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
||||||
|
private static String DB_URL = "jdbc:sqlserver://[db_ip];databaseName=[db_name];encrypt=false;trustServerCertificate=true";
|
||||||
|
private static String DB_USER = "BIP";
|
||||||
|
private static String DB_PWD = "TkBip@2025";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据库连接
|
||||||
|
*
|
||||||
|
* @return Connection 数据库连接对象
|
||||||
|
* @throws BusinessException 连接异常
|
||||||
|
*/
|
||||||
|
public static Connection getConnection() throws BusinessException {
|
||||||
|
try {
|
||||||
|
Map<String, String> configParams = MyHelper.getConfigParams("gy-config", null);
|
||||||
|
Class.forName(DB_DRIVER);
|
||||||
|
String dbIp = configParams.get("db_ip");
|
||||||
|
String dbName = configParams.get("db_name");
|
||||||
|
DB_URL = DB_URL.replace("[db_ip]", dbIp).replace("[db_name]", dbName);
|
||||||
|
DB_USER = configParams.get("db_user");
|
||||||
|
DB_PWD = configParams.get("db_pwd");
|
||||||
|
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
|
||||||
|
logger.info("成功连接到高压IMS数据库");
|
||||||
|
return conn;
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
logger.error("SQL Server驱动未找到", e);
|
||||||
|
throw new BusinessException("SQL Server驱动未找到: " + e.getMessage(), e);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("连接高压IMS数据库失败", e);
|
||||||
|
throw new BusinessException("连接高压IMS数据库失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行查询SQL,返回List<Map<String, Object>>结果集
|
||||||
|
*
|
||||||
|
* @param sql 查询SQL语句
|
||||||
|
* @return List<Map < String, Object>> 查询结果列表
|
||||||
|
* @throws BusinessException 查询异常
|
||||||
|
*/
|
||||||
|
public static List<Map<String, Object>> query(String sql) throws BusinessException {
|
||||||
|
return query(sql, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行查询SQL,返回List<Map<String, Object>>结果集
|
||||||
|
*
|
||||||
|
* @param sql 查询SQL语句
|
||||||
|
* @param params 参数数组
|
||||||
|
* @return List<Map < String, Object>> 查询结果列表
|
||||||
|
* @throws BusinessException 查询异常
|
||||||
|
*/
|
||||||
|
public static List<Map<String, Object>> query(String sql, Object[] params) throws BusinessException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
pstmt = conn.prepareStatement(sql);
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
if (params != null && params.length > 0) {
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
pstmt.setObject(i + 1, params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("执行查询SQL: " + sql);
|
||||||
|
rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
// 获取结果集元数据
|
||||||
|
ResultSetMetaData metaData = rs.getMetaData();
|
||||||
|
int columnCount = metaData.getColumnCount();
|
||||||
|
|
||||||
|
// 遍历结果集
|
||||||
|
while (rs.next()) {
|
||||||
|
Map<String, Object> row = new HashMap<>();
|
||||||
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
|
String columnName = metaData.getColumnName(i);
|
||||||
|
Object value = rs.getObject(i);
|
||||||
|
row.put(columnName, value);
|
||||||
|
}
|
||||||
|
resultList.add(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("查询成功,返回 " + resultList.size() + " 条记录");
|
||||||
|
return resultList;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("执行查询SQL失败: " + sql, e);
|
||||||
|
throw new BusinessException("执行查询SQL失败: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
closeResources(rs, pstmt, conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行查询SQL,返回单条记录Map
|
||||||
|
*
|
||||||
|
* @param sql 查询SQL语句
|
||||||
|
* @return Map<String, Object> 单条记录,如果无记录返回null
|
||||||
|
* @throws BusinessException 查询异常
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> queryOne(String sql) throws BusinessException {
|
||||||
|
return queryOne(sql, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行查询SQL,返回单条记录Map
|
||||||
|
*
|
||||||
|
* @param sql 查询SQL语句
|
||||||
|
* @param params 参数数组
|
||||||
|
* @return Map<String, Object> 单条记录,如果无记录返回null
|
||||||
|
* @throws BusinessException 查询异常
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> queryOne(String sql, Object[] params) throws BusinessException {
|
||||||
|
List<Map<String, Object>> list = query(sql, params);
|
||||||
|
if (list != null && !list.isEmpty()) {
|
||||||
|
return list.get(0);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行查询SQL,返回单个值
|
||||||
|
*
|
||||||
|
* @param sql 查询SQL语句
|
||||||
|
* @return Object 查询结果的第一行第一列的值
|
||||||
|
* @throws BusinessException 查询异常
|
||||||
|
*/
|
||||||
|
public static Object querySingle(String sql) throws BusinessException {
|
||||||
|
return querySingle(sql, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行查询SQL,返回单个值
|
||||||
|
*
|
||||||
|
* @param sql 查询SQL语句
|
||||||
|
* @param params 参数数组
|
||||||
|
* @return Object 查询结果的第一行第一列的值
|
||||||
|
* @throws BusinessException 查询异常
|
||||||
|
*/
|
||||||
|
public static Object querySingle(String sql, Object[] params) throws BusinessException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
pstmt = conn.prepareStatement(sql);
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
if (params != null && params.length > 0) {
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
pstmt.setObject(i + 1, params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("执行查询SQL: " + sql);
|
||||||
|
rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
Object result = rs.getObject(1);
|
||||||
|
logger.info("查询成功,返回值: " + result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("执行查询SQL失败: " + sql, e);
|
||||||
|
throw new BusinessException("执行查询SQL失败: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
closeResources(rs, pstmt, conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行插入SQL
|
||||||
|
*
|
||||||
|
* @param sql 插入SQL语句
|
||||||
|
* @return int 影响的行数
|
||||||
|
* @throws BusinessException 插入异常
|
||||||
|
*/
|
||||||
|
public static int insert(String sql) throws BusinessException {
|
||||||
|
return insert(sql, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行插入SQL
|
||||||
|
*
|
||||||
|
* @param sql 插入SQL语句
|
||||||
|
* @param params 参数数组
|
||||||
|
* @return int 影响的行数
|
||||||
|
* @throws BusinessException 插入异常
|
||||||
|
*/
|
||||||
|
public static int insert(String sql, Object[] params) throws BusinessException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement pstmt = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
pstmt = conn.prepareStatement(sql);
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
if (params != null && params.length > 0) {
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
pstmt.setObject(i + 1, params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("执行插入SQL: " + sql);
|
||||||
|
int rows = pstmt.executeUpdate();
|
||||||
|
conn.commit();
|
||||||
|
|
||||||
|
logger.info("插入成功,影响行数: " + rows);
|
||||||
|
return rows;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
logger.info("事务已回滚");
|
||||||
|
} catch (SQLException rollbackEx) {
|
||||||
|
logger.error("回滚事务异常", rollbackEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.error("执行插入SQL失败: " + sql, e);
|
||||||
|
throw new BusinessException("执行插入SQL失败: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
closeResources(null, pstmt, conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行插入SQL并返回生成的主键(适用于自增主键)
|
||||||
|
*
|
||||||
|
* @param sql 插入SQL语句
|
||||||
|
* @return Object 生成的主键值
|
||||||
|
* @throws BusinessException 插入异常
|
||||||
|
*/
|
||||||
|
public static Object insertAndGetKey(String sql) throws BusinessException {
|
||||||
|
return insertAndGetKey(sql, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行插入SQL并返回生成的主键(适用于自增主键)
|
||||||
|
*
|
||||||
|
* @param sql 插入SQL语句
|
||||||
|
* @param params 参数数组
|
||||||
|
* @return Object 生成的主键值
|
||||||
|
* @throws BusinessException 插入异常
|
||||||
|
*/
|
||||||
|
public static Object insertAndGetKey(String sql, Object[] params) throws BusinessException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
// 指定返回生成的主键
|
||||||
|
pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
if (params != null && params.length > 0) {
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
pstmt.setObject(i + 1, params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("执行插入SQL: " + sql);
|
||||||
|
int rows = pstmt.executeUpdate();
|
||||||
|
|
||||||
|
if (rows > 0) {
|
||||||
|
// 获取生成的主键
|
||||||
|
rs = pstmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
Object generatedKey = rs.getObject(1);
|
||||||
|
conn.commit();
|
||||||
|
logger.info("插入成功,生成的主键: " + generatedKey);
|
||||||
|
return generatedKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.commit();
|
||||||
|
logger.info("插入成功,但未获取到主键");
|
||||||
|
return null;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
logger.info("事务已回滚");
|
||||||
|
} catch (SQLException rollbackEx) {
|
||||||
|
logger.error("回滚事务异常", rollbackEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.error("执行插入SQL失败: " + sql, e);
|
||||||
|
throw new BusinessException("执行插入SQL失败: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
closeResources(rs, pstmt, conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行更新SQL(INSERT、UPDATE、DELETE)
|
||||||
|
*
|
||||||
|
* @param sql 更新SQL语句
|
||||||
|
* @return int 影响的行数
|
||||||
|
* @throws BusinessException 更新异常
|
||||||
|
*/
|
||||||
|
public static int update(String sql) throws BusinessException {
|
||||||
|
return update(sql, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行更新SQL(INSERT、UPDATE、DELETE)
|
||||||
|
*
|
||||||
|
* @param sql 更新SQL语句
|
||||||
|
* @param params 参数数组
|
||||||
|
* @return int 影响的行数
|
||||||
|
* @throws BusinessException 更新异常
|
||||||
|
*/
|
||||||
|
public static int update(String sql, Object[] params) throws BusinessException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement pstmt = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
pstmt = conn.prepareStatement(sql);
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
if (params != null && params.length > 0) {
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
pstmt.setObject(i + 1, params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("执行更新SQL: " + sql);
|
||||||
|
int rows = pstmt.executeUpdate();
|
||||||
|
conn.commit();
|
||||||
|
|
||||||
|
logger.info("更新成功,影响行数: " + rows);
|
||||||
|
return rows;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
logger.info("事务已回滚");
|
||||||
|
} catch (SQLException rollbackEx) {
|
||||||
|
logger.error("回滚事务异常", rollbackEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.error("执行更新SQL失败: " + sql, e);
|
||||||
|
throw new BusinessException("执行更新SQL失败: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
closeResources(null, pstmt, conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量执行更新SQL
|
||||||
|
*
|
||||||
|
* @param sqlList SQL语句列表
|
||||||
|
* @return int[] 每条SQL影响的行数数组
|
||||||
|
* @throws BusinessException 更新异常
|
||||||
|
*/
|
||||||
|
public static int[] batchUpdate(List<String> sqlList) throws BusinessException {
|
||||||
|
if (sqlList == null || sqlList.isEmpty()) {
|
||||||
|
return new int[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
Statement stmt = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
stmt = conn.createStatement();
|
||||||
|
|
||||||
|
for (String sql : sqlList) {
|
||||||
|
stmt.addBatch(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("批量执行更新SQL,共 " + sqlList.size() + " 条");
|
||||||
|
int[] rows = stmt.executeBatch();
|
||||||
|
conn.commit();
|
||||||
|
|
||||||
|
logger.info("批量更新成功");
|
||||||
|
return rows;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
logger.info("事务已回滚");
|
||||||
|
} catch (SQLException rollbackEx) {
|
||||||
|
logger.error("回滚事务异常", rollbackEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.error("批量执行更新SQL失败", e);
|
||||||
|
throw new BusinessException("批量执行更新SQL失败: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
closeResources(null, stmt, conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行带参数的批量更新
|
||||||
|
*
|
||||||
|
* @param sql SQL语句
|
||||||
|
* @param paramsList 参数列表
|
||||||
|
* @return int[] 每条SQL影响的行数数组
|
||||||
|
* @throws BusinessException 更新异常
|
||||||
|
*/
|
||||||
|
public static int[] batchUpdate(String sql, List<Object[]> paramsList) throws BusinessException {
|
||||||
|
if (paramsList == null || paramsList.isEmpty()) {
|
||||||
|
return new int[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement pstmt = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
pstmt = conn.prepareStatement(sql);
|
||||||
|
|
||||||
|
for (Object[] params : paramsList) {
|
||||||
|
if (params != null && params.length > 0) {
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
pstmt.setObject(i + 1, params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pstmt.addBatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("批量执行更新SQL: " + sql + ",共 " + paramsList.size() + " 条");
|
||||||
|
int[] rows = pstmt.executeBatch();
|
||||||
|
conn.commit();
|
||||||
|
|
||||||
|
logger.info("批量更新成功");
|
||||||
|
return rows;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
logger.info("事务已回滚");
|
||||||
|
} catch (SQLException rollbackEx) {
|
||||||
|
logger.error("回滚事务异常", rollbackEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.error("批量执行更新SQL失败: " + sql, e);
|
||||||
|
throw new BusinessException("批量执行更新SQL失败: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
closeResources(null, pstmt, conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭数据库资源
|
||||||
|
*
|
||||||
|
* @param rs ResultSet
|
||||||
|
* @param stmt Statement
|
||||||
|
* @param conn Connection
|
||||||
|
*/
|
||||||
|
private static void closeResources(ResultSet rs, Statement stmt, Connection conn) {
|
||||||
|
if (rs != null) {
|
||||||
|
try {
|
||||||
|
rs.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("关闭ResultSet异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (stmt != null) {
|
||||||
|
try {
|
||||||
|
stmt.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("关闭Statement异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
|
if (!conn.getAutoCommit()) {
|
||||||
|
conn.setAutoCommit(true);
|
||||||
|
}
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error("关闭Connection异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试数据库连接
|
||||||
|
*
|
||||||
|
* @return boolean 连接是否成功
|
||||||
|
*/
|
||||||
|
public static boolean testConnection() {
|
||||||
|
try {
|
||||||
|
Connection conn = getConnection();
|
||||||
|
if (conn != null && !conn.isClosed()) {
|
||||||
|
conn.close();
|
||||||
|
logger.info("数据库连接测试成功");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("数据库连接测试失败", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -18,7 +18,7 @@ import java.sql.SQLException;
|
||||||
*/
|
*/
|
||||||
public class ImsDaoUtil {
|
public class ImsDaoUtil {
|
||||||
|
|
||||||
private static final String LOG_INFO_NAME = "gymeslog";
|
private static final String LOG_INFO_NAME = "gyimslog";
|
||||||
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
||||||
|
|
||||||
public static int executeUpdate(String sql, String targetId) throws BusinessException {
|
public static int executeUpdate(String sql, String targetId) throws BusinessException {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue