refactor(ims): 重构物料出库IMS插件的数据库操作

- 将executeUpdate方法移至ImsDaoUtil工具类中
This commit is contained in:
mzr 2025-10-09 09:50:24 +08:00
parent 9afab81578
commit 5ffb10c7b6
2 changed files with 87 additions and 68 deletions

View File

@ -7,12 +7,10 @@ import nc.bs.logging.Logger;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.uapbd.util.ImsDaoUtil;
import nc.bs.uapbd.util.MyHelper;
import nc.itf.mmpac.pickm.IPickmQueryService;
import nc.itf.uap.pf.busiflow.PfButtonClickContext;
import nc.jdbc.framework.JdbcSession;
import nc.jdbc.framework.PersistenceManager;
import nc.jdbc.framework.exception.DbException;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.pubitf.ic.m4d.api.IMaterialOutMaintainAPI;
import nc.util.mmf.busi.service.PFPubService;
@ -32,8 +30,6 @@ import nc.vo.scmpub.res.billtype.MMBillType;
import nccloud.baseapp.core.log.NCCForUAPLogger;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
@ -152,7 +148,7 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin {
updateSql = updateSql.replace("[err_msg]", e.getMessage());
updateSql = updateSql.replace("[cgeneralhid]", cgeneralhid);
NCCForUAPLogger.debug("MaterialOutGyImsPlugin-updateSql = " + updateSql);
int rows = executeUpdate(updateSql, cgeneralhid);
int rows = ImsDaoUtil.executeUpdate(updateSql, cgeneralhid);
NCCForUAPLogger.debug("MaterialOutGyImsPlugin-rows = " + rows);
}
}
@ -160,7 +156,7 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin {
// 修改状态为 完成
String inSql = SqlUtils.getInStr("cgeneralhid", imsIdSet.toArray(new String[0]), Boolean.TRUE);
String updateSql = "update BIPOutMainTab set status = '2',err_msg = null where " + inSql;
executeUpdate(updateSql, imsIdSet.toString());
ImsDaoUtil.executeUpdate(updateSql, imsIdSet.toString());
}
Logger.error("---end----任务结束运行--");
} catch (Exception e) {
@ -193,66 +189,5 @@ public class MaterialOutGyImsPlugin implements IBackgroundWorkPlugin {
}
}
private int executeUpdate(String sql, String targetId) throws BusinessException {
// 2. 直接通过静态工厂方法获取 PersistenceManager
PersistenceManager pm = null;
JdbcSession jdbcSession = null;
Connection conn = null;
int rows = 0;
try {
// 直接调用 PersistenceManager 的静态方法 getInstance()
pm = PersistenceManager.getInstance("gyims");
pm.setAddTimeStamp(false);// 不添加时间戳ts
// 2.1 获取 JdbcSession Connection
jdbcSession = pm.getJdbcSession();
jdbcSession.setSQLTranslator(true); // 保持 SQL 转换
conn = jdbcSession.getConnection();
conn.setAutoCommit(false);
logger.info("直接获取PersistenceManager手动事务开启targetId" + targetId);
// 4. 执行主表+子表操作 (传入共享的 pm)
logger.info("事务内执行更新targetId" + targetId);
rows = pm.getJdbcSession().executeUpdate(sql);
logger.info("事务内执行更新rows" + rows);
logger.info("事务内操作全部成功准备提交targetId" + targetId);
} catch (DbException | SQLException e) { // 捕获 DbException SQLException
logger.error("事务执行失败触发回滚targetId" + targetId);
if (conn != null) {
try {
conn.rollback();
logger.info("事务回滚完成targetId" + targetId);
} catch (SQLException rollbackE) {
logger.error("事务回滚异常targetId" + targetId);
}
}
throw new BusinessException("备料计划同步事务失败:" + e.getMessage(), e);
} finally {
if (conn != null) {
try {
conn.commit();
logger.info("事务提交成功targetId" + targetId);
// 恢复自动提交模式确保不会影响其他操作
conn.setAutoCommit(true);
} catch (SQLException commitE) {
logger.error("事务提交异常targetId" + targetId);
}
}
// 5. 释放 PersistenceManager (至关重要)
if (pm != null) {
try {
pm.release(); // 必须调用 release() 将连接归还给连接池
logger.info("PersistenceManager 释放完成targetId" + targetId);
} catch (Exception releaseE) {
logger.error("PersistenceManager 释放异常", releaseE);
}
}
}
return rows;
}
}

View File

@ -0,0 +1,84 @@
package nc.bs.uapbd.util;
import nc.bs.logging.Log;
import nc.jdbc.framework.JdbcSession;
import nc.jdbc.framework.PersistenceManager;
import nc.jdbc.framework.exception.DbException;
import nc.vo.pub.BusinessException;
import java.sql.Connection;
import java.sql.SQLException;
/**
* 高压加工车间-IMS-DAO 工具类
*
* @author mzr
* @date 2025/10/08
*/
public class ImsDaoUtil {
private static final String LOG_INFO_NAME = "gymeslog";
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
public static int executeUpdate(String sql, String targetId) throws BusinessException {
// 2. 直接通过静态工厂方法获取 PersistenceManager
PersistenceManager pm = null;
JdbcSession jdbcSession = null;
Connection conn = null;
int rows = 0;
try {
// 直接调用 PersistenceManager 的静态方法 getInstance()
pm = PersistenceManager.getInstance("gyims");
pm.setAddTimeStamp(false);// 不添加时间戳ts
// 2.1 获取 JdbcSession Connection
jdbcSession = pm.getJdbcSession();
jdbcSession.setSQLTranslator(true); // 保持 SQL 转换
conn = jdbcSession.getConnection();
conn.setAutoCommit(false);
logger.info("直接获取PersistenceManager手动事务开启targetId" + targetId);
// 4. 执行主表+子表操作 (传入共享的 pm)
logger.info("事务内执行更新targetId" + targetId);
rows = pm.getJdbcSession().executeUpdate(sql);
logger.info("事务内执行更新rows" + rows);
logger.info("事务内操作全部成功准备提交targetId" + targetId);
} catch (DbException | SQLException e) { // 捕获 DbException SQLException
logger.error("事务执行失败触发回滚targetId" + targetId);
if (conn != null) {
try {
conn.rollback();
logger.info("事务回滚完成targetId" + targetId);
} catch (SQLException rollbackE) {
logger.error("事务回滚异常targetId" + targetId);
}
}
throw new BusinessException("备料计划同步事务失败:" + e.getMessage(), e);
} finally {
if (conn != null) {
try {
conn.commit();
logger.info("事务提交成功targetId" + targetId);
// 恢复自动提交模式确保不会影响其他操作
conn.setAutoCommit(true);
} catch (SQLException commitE) {
logger.error("事务提交异常targetId" + targetId);
}
}
// 5. 释放 PersistenceManager (至关重要)
if (pm != null) {
try {
pm.release(); // 必须调用 release() 将连接归还给连接池
logger.info("PersistenceManager 释放完成targetId" + targetId);
} catch (Exception releaseE) {
logger.error("PersistenceManager 释放异常", releaseE);
}
}
}
return rows;
}
}