From 70f72c23a86fdc8c8b35d2316b2e4b89536d37fb Mon Sep 17 00:00:00 2001 From: mzr Date: Fri, 21 Nov 2025 10:37:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ims):=20=E9=87=8D=E6=9E=84=E9=AB=98?= =?UTF-8?q?=E5=8E=8BIMS=E6=95=B0=E6=8D=AE=E5=BA=93=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../task/ims/ErpIaI2billFromGyImsPlugin.java | 33 +- .../task/ims/MaterialOutGyImsPlugin.java | 35 +- .../task/ims/ProductReportGyImsPlugin.java | 35 +- .../public/nc/bs/uapbd/util/GyImsDbUtil.java | 541 ++++++++++++++++++ .../public/nc/bs/uapbd/util/ImsDaoUtil.java | 2 +- 5 files changed, 568 insertions(+), 78 deletions(-) create mode 100644 uapbd/src/public/nc/bs/uapbd/util/GyImsDbUtil.java diff --git a/uapbd/src/private/nc/bs/uapbd/task/ims/ErpIaI2billFromGyImsPlugin.java b/uapbd/src/private/nc/bs/uapbd/task/ims/ErpIaI2billFromGyImsPlugin.java index daa7b5d8..0b44fe44 100644 --- a/uapbd/src/private/nc/bs/uapbd/task/ims/ErpIaI2billFromGyImsPlugin.java +++ b/uapbd/src/private/nc/bs/uapbd/task/ims/ErpIaI2billFromGyImsPlugin.java @@ -8,9 +8,8 @@ import nc.bs.logging.Log; import nc.bs.pub.pa.PreAlertObject; import nc.bs.pub.taskcenter.BgWorkingContext; import nc.bs.pub.taskcenter.IBackgroundWorkPlugin; +import nc.bs.uapbd.util.GyImsDbUtil; 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.vo.ic.m45.entity.PurchaseInVO; import nc.vo.pub.BusinessException; @@ -32,28 +31,14 @@ import java.util.Map; public class ErpIaI2billFromGyImsPlugin implements IBackgroundWorkPlugin { private static final String LOG_INFO_NAME = "gyimslog"; private static final Log logger = Log.getInstance(LOG_INFO_NAME); - private SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); - private 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; - } + private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); + private final SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public PreAlertObject executeTask(BgWorkingContext bgWorkingContext) throws BusinessException { // 查询高压ims中间表需要同步的数据 String sql = "SELECT * FROM BIPInputMainTab a WHERE a.status = 'C'"; - List> remain = (List>) getImsDao().executeQuery(sql, new MapListProcessor()); + List> remain = GyImsDbUtil.query(sql); if (remain == null || remain.isEmpty()) { return null; } @@ -64,7 +49,7 @@ public class ErpIaI2billFromGyImsPlugin implements IBackgroundWorkPlugin { String status = map.get("status") + ""; try { String bodySql = " SELECT *" + " from BIPInputDetailTab a " + " where a.cgeneralhid ='" + cgeneralhid + "'"; - List> bodyList = (List>) getImsDao().executeQuery(bodySql, new MapListProcessor()); + List> bodyList = GyImsDbUtil.query(bodySql); if (bodyList == null || bodyList.isEmpty()) { continue; } @@ -160,14 +145,12 @@ public class ErpIaI2billFromGyImsPlugin implements IBackgroundWorkPlugin { updateSql = updateSql.replace("[err_msg]", "新增返回空"); } updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid); - getImsDao().executeUpdate(updateSql); + GyImsDbUtil.update(updateSql); } catch (BusinessException e) { logger.error("ErpIaI2billFromGyImsPlugin Error: " + e.getMessage(), e); String updateSql = "update BIPInputMainTab set err_msg = ? where cgeneralhid = ?"; - SQLParameter parameter = new SQLParameter(); - parameter.addParam(e.getMessage()); - parameter.addParam(cgeneralhid); - int rows = getImsDao().executeUpdate(updateSql, parameter); + Object[] params = new Object[]{e.getMessage(), cgeneralhid}; + int rows = GyImsDbUtil.update(updateSql, params); } } return null; diff --git a/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java b/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java index 74c57403..73b5c384 100644 --- a/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java +++ b/uapbd/src/private/nc/bs/uapbd/task/ims/MaterialOutGyImsPlugin.java @@ -6,12 +6,11 @@ import nc.bs.logging.Log; import nc.bs.pub.pa.PreAlertObject; import nc.bs.pub.taskcenter.BgWorkingContext; import nc.bs.pub.taskcenter.IBackgroundWorkPlugin; +import nc.bs.uapbd.util.GyImsDbUtil; import nc.bs.uapbd.util.MyHelper; import nc.itf.mmpac.pickm.IPickmQueryService; import nc.itf.uap.pf.busiflow.PfButtonClickContext; -import nc.jdbc.framework.SQLParameter; import nc.jdbc.framework.processor.ColumnProcessor; -import nc.jdbc.framework.processor.MapListProcessor; import nc.pubitf.ic.m4d.api.IMaterialOutMaintainAPI; import nc.util.mmf.busi.service.PFPubService; 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 Log logger = Log.getInstance(LOG_INFO_NAME); - private SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); - private SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); + private final SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // BIP数据源 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 public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException { logger.error("---start----高压IMS-材料出库-任务开始运行--"); IPickmQueryService queryService = NCLocator.getInstance().lookup(IPickmQueryService.class); // IMS 同步状态:BIP:C-创建;U-修改D-删除 IMS:2-MES单据接收完成 - String selSql = "select * from BIPOutMainTab where status = 'C'"; - List> mainList = (List>) getImsDao().executeQuery(selSql, new MapListProcessor()); + String selSql = "select * from BIPOutMainTab where status = 'C1'"; + List> mainList = GyImsDbUtil.query(selSql); for (Map mainMap : mainList) { String cgeneralhid = mainMap.get("cgeneralhid") + ""; // 同步状态:IMS:C-创建;U-修改; 4-BIP审核出库MES接收确认 BIP:2-BIP审核出库 String status = mainMap.get("status") + ""; try { String pkSql = "select * from BIPOutDetailTab where cgeneralhid = '" + cgeneralhid + "'"; - List> detailList = (List>) getImsDao().executeQuery(pkSql, new MapListProcessor()); + List> detailList = GyImsDbUtil.query(pkSql); Map> detailIdMap = new HashMap<>(); for (Map detailMap : detailList) { detailIdMap.put(detailMap.get("vbdef17") + "", detailMap); @@ -166,14 +151,12 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin { updateSql = updateSql.replace("[err_msg]", "新增返回空"); } updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid); - getImsDao().executeUpdate(updateSql); + GyImsDbUtil.update(updateSql); } catch (BusinessException e) { logger.error("MaterialOutGyImsPlugin Error: ", e); String updateSql = "update BIPOutMainTab set err_msg = ? where cgeneralhid = ?"; - SQLParameter parameter = new SQLParameter(); - parameter.addParam(e.getMessage()); - parameter.addParam(cgeneralhid); - int rows = getImsDao().executeUpdate(updateSql, parameter); + Object[] params = new Object[]{e.getMessage(), cgeneralhid}; + int rows = GyImsDbUtil.update(updateSql, params); } } logger.error("---end----高压IMS-材料出库-任务结束运行--"); diff --git a/uapbd/src/private/nc/bs/uapbd/task/ims/ProductReportGyImsPlugin.java b/uapbd/src/private/nc/bs/uapbd/task/ims/ProductReportGyImsPlugin.java index 07b849bb..ac138c05 100644 --- a/uapbd/src/private/nc/bs/uapbd/task/ims/ProductReportGyImsPlugin.java +++ b/uapbd/src/private/nc/bs/uapbd/task/ims/ProductReportGyImsPlugin.java @@ -6,13 +6,12 @@ import nc.bs.logging.Log; import nc.bs.pub.pa.PreAlertObject; import nc.bs.pub.taskcenter.BgWorkingContext; import nc.bs.pub.taskcenter.IBackgroundWorkPlugin; +import nc.bs.uapbd.util.GyImsDbUtil; import nc.bs.uapbd.util.MyHelper; import nc.itf.mmpac.pmo.pac0002.IPMOQueryService; import nc.itf.mmpac.wr.IWrMaintainService; import nc.itf.uap.pf.busiflow.PfButtonClickContext; -import nc.jdbc.framework.SQLParameter; import nc.jdbc.framework.processor.ColumnProcessor; -import nc.jdbc.framework.processor.MapListProcessor; import nc.util.mmf.busi.service.PFPubService; import nc.util.mmf.framework.base.MMStringUtil; import nc.util.mmf.framework.base.MMValueCheck; @@ -46,24 +45,10 @@ import java.util.Map; public class ProductReportGyImsPlugin implements IBackgroundWorkPlugin { private static final String LOG_INFO_NAME = "gyimslog"; private static final Log logger = Log.getInstance(LOG_INFO_NAME); - private SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); - private SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); + private final SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // BIP数据源 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 public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException { @@ -71,15 +56,15 @@ public class ProductReportGyImsPlugin implements IBackgroundWorkPlugin { IPMOQueryService pmoQueryService = NCLocator.getInstance().lookup(IPMOQueryService.class); IWrMaintainService wrService = NCLocator.getInstance().lookup(IWrMaintainService.class); String selSql = "select * from BIPReportMainTab where status = 'C'"; - List> mainList = (List>) getImsDao().executeQuery(selSql, new MapListProcessor()); + List> mainList = GyImsDbUtil.query(selSql); for (Map mainMap : mainList) { // IMS生产报告的主键 - String pkWr = mainMap.get("pk_wr").toString(); + String pkWr = mainMap.get("pk_wr") + ""; // 同步标识 IMS:C-创建;U-修改 BIP:2-BIP单据接收完成 String status = mainMap.get("status").toString(); try { String pkWrSql = "select * from BIPReportDetailTab where pk_wr = '" + pkWr + "'"; - List> detailList = (List>) getImsDao().executeQuery(pkWrSql, new MapListProcessor()); + List> detailList = GyImsDbUtil.query(pkWrSql); Map> detailIdMap = new HashMap<>(); for (Map detailMap : detailList) { detailIdMap.put(detailMap.get("vbdef23").toString(), detailMap); @@ -168,14 +153,12 @@ public class ProductReportGyImsPlugin implements IBackgroundWorkPlugin { updateSql = updateSql.replace("[err_msg]", "新增返回空"); } updateSql = updateSql.replace("[pkWr]", pkWr); - getImsDao().executeUpdate(updateSql); + GyImsDbUtil.update(updateSql); } catch (BusinessException e) { logger.error("ProductReportGyImsPlugin Error: " + e.getMessage(), e); String updateSql = "update BIPReportMainTab set err_msg = ? where pk_wr = ?"; - SQLParameter parameter = new SQLParameter(); - parameter.addParam(e.getMessage()); - parameter.addParam(pkWr); - int rows = getImsDao().executeUpdate(updateSql, parameter); + Object[] params = new Object[]{e.getMessage(), pkWr}; + int rows = GyImsDbUtil.update(updateSql, params); } } logger.error("---end----高压IMS-生产报告-任务结束运行--"); diff --git a/uapbd/src/public/nc/bs/uapbd/util/GyImsDbUtil.java b/uapbd/src/public/nc/bs/uapbd/util/GyImsDbUtil.java new file mode 100644 index 00000000..469cebf6 --- /dev/null +++ b/uapbd/src/public/nc/bs/uapbd/util/GyImsDbUtil.java @@ -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 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("鎴愬姛杩炴帴鍒伴珮鍘婭MS鏁版嵁搴"); + 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锛岃繑鍥濴ist>缁撴灉闆 + * + * @param sql 鏌ヨSQL璇彞 + * @return List> 鏌ヨ缁撴灉鍒楄〃 + * @throws BusinessException 鏌ヨ寮傚父 + */ + public static List> query(String sql) throws BusinessException { + return query(sql, null); + } + + /** + * 鎵ц鏌ヨSQL锛岃繑鍥濴ist>缁撴灉闆 + * + * @param sql 鏌ヨSQL璇彞 + * @param params 鍙傛暟鏁扮粍 + * @return List> 鏌ヨ缁撴灉鍒楄〃 + * @throws BusinessException 鏌ヨ寮傚父 + */ + public static List> query(String sql, Object[] params) throws BusinessException { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + List> 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 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锛岃繑鍥炲崟鏉¤褰昅ap + * + * @param sql 鏌ヨSQL璇彞 + * @return Map 鍗曟潯璁板綍锛屽鏋滄棤璁板綍杩斿洖null + * @throws BusinessException 鏌ヨ寮傚父 + */ + public static Map queryOne(String sql) throws BusinessException { + return queryOne(sql, null); + } + + /** + * 鎵ц鏌ヨSQL锛岃繑鍥炲崟鏉¤褰昅ap + * + * @param sql 鏌ヨSQL璇彞 + * @param params 鍙傛暟鏁扮粍 + * @return Map 鍗曟潯璁板綍锛屽鏋滄棤璁板綍杩斿洖null + * @throws BusinessException 鏌ヨ寮傚父 + */ + public static Map queryOne(String sql, Object[] params) throws BusinessException { + List> 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锛圛NSERT銆乁PDATE銆丏ELETE锛 + * + * @param sql 鏇存柊SQL璇彞 + * @return int 褰卞搷鐨勮鏁 + * @throws BusinessException 鏇存柊寮傚父 + */ + public static int update(String sql) throws BusinessException { + return update(sql, null); + } + + /** + * 鎵ц鏇存柊SQL锛圛NSERT銆乁PDATE銆丏ELETE锛 + * + * @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 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 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; + } + } +} + diff --git a/uapbd/src/public/nc/bs/uapbd/util/ImsDaoUtil.java b/uapbd/src/public/nc/bs/uapbd/util/ImsDaoUtil.java index 3b1963c8..4ee911a7 100644 --- a/uapbd/src/public/nc/bs/uapbd/util/ImsDaoUtil.java +++ b/uapbd/src/public/nc/bs/uapbd/util/ImsDaoUtil.java @@ -18,7 +18,7 @@ import java.sql.SQLException; */ 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); public static int executeUpdate(String sql, String targetId) throws BusinessException {