1、定时 JDBC 的事务
2、事务提交
3、事务回滚
样例
@Transactional(propagation = Propagation.REQUIRES_NEW)
@RequestMapping(value = "/ix_work_order", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ApiDesc(description = "工单拆分通知")
public IfMessageOut ixWorkOrder(@RequestBody MiWoOrModel miWoOrModel) {
//错误检测
try {
this.checkConnection();
}catch (Exception e){
try {
Thread.sleep(300);
} catch (InterruptedException ex) {
}
}
IfMessageOut out = new IfMessageOut();
Date time = DateUtil.getDate();
DefaultTransactionDefinition def = null;
DataSourceTransactionManager transactionManager = null;
TransactionStatus status = null;
try{
IQueryManager t100QueryManager = BeanUtil.get(DataSourceManager.class).getQueryManager("T100TestDataSource");
transactionManager = new DataSourceTransactionManager();
def = new DefaultTransactionDefinition();
transactionManager.setDataSource(t100QueryManager.getDml().getDataSource());
status = transactionManager.getTransaction(def);
String currentTime = DateUtil.currentTimeStr();
String user = getUserId();
if(ValueUtil.isEmpty(miWoOrModel.getDhupdtime())){
miWoOrModel.setDhupdtime(currentTime); // 拆分的时间
}
if(ValueUtil.isEmpty(miWoOrModel.getCrecorder())){
miWoOrModel.setCrecorder(user); // 拆分的人员(工号)
}
if(ValueUtil.isEmpty(miWoOrModel.getChangetype())){
miWoOrModel.setChangetype("0"); // 是否变更,默认为0
}
// 效验必填字段
checkWoOrderField(miWoOrModel);
// 转换参数
Map<String, Object> parmMap = getParms(miWoOrModel);
// 对时间格式进行处理
if(ValueUtil.isNotEmpty(parmMap.get("dintime"))){
parmMap.put("dintime", DateUtil.parse((String) parmMap.get("dintime"), "yyyy-MM-dd HH:mm:ss"));
}
if(ValueUtil.isNotEmpty(parmMap.get("updtime"))){
parmMap.put("updtime", DateUtil.parse((String) parmMap.get("updtime"), "yyyy-MM-dd HH:mm:ss"));
}
if(ValueUtil.isNotEmpty(parmMap.get("dhupdtime"))){
parmMap.put("dhupdtime", DateUtil.parse((String) parmMap.get("dhupdtime"), "yyyy-MM-dd HH:mm:ss"));
}
// update by xuli, 20231224, if erp exists info, do update, not insert
int existNum = 0;
String checkExistSQL = "select * from dsdemo.m_mixworkorder where trim(cmaingroupid) =:cmaingroupid and trim(cproductgroupid) = :cproductgroupid";
Map<String, String> checkExistMap = new HashMap<>();
checkExistMap.put("cmaingroupid", miWoOrModel.getCmaingroupid());
checkExistMap.put("cproductgroupid", miWoOrModel.getCproductgroupid());
existNum = t100QueryManager.selectSizeBySql(checkExistSQL, checkExistMap);
// 不存在数据, 做插入
if (0 == existNum) {
// 拼接SQL
String insertSQl = getInsertSql(miWoOrModel, "dsdemo.M_MixWorkOrder");
// 向 ERP 插入数据
t100QueryManager.executeBySql(insertSQl, parmMap);
} else {
// 更新
String upfields = "";
Field[] fields = miWoOrModel.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object value = field.get(miWoOrModel);
if(ValueUtil.isNotEmpty(value)){
// 批次和拆除的子批字段是作为查询字段的,不做修改
if(ValueUtil.isNotEqual(field.getName(), "cmaingroupid") && ValueUtil.isNotEqual(field.getName(), "cproductgroupid")){
String fieldName = field.getName() ;
upfields = upfields + fieldName + " = :" + fieldName + " , " ;
}
}
} catch (IllegalAccessException e) {
System.out.println("无法获取属性值:" + field.getName());
}
}
// 出去字符串最后两位的 ", "
if(ValueUtil.isEqual(", ", upfields.substring(upfields.length() - 2, upfields.length()))){
upfields = upfields.substring(0, upfields.length() - 2) ;
}
// 拼接SQL
upfields = upfields.replace(":dintime", "to_date(:dintime, 'YYYY-MM-DD HH24:MI:SS')");
upfields = upfields.replace(":updtime", "to_date(:updtime, 'YYYY-MM-DD HH24:MI:SS')");
upfields = upfields.replace(":dhupdtime", "to_date(:dhupdtime, 'YYYY-MM-DD HH24:MI:SS')");
String upSql = "update dsdemo.m_mixworkorder set ";
upSql = upSql + upfields + " where trim(cmaingroupid) = :cmaingroupid and trim(cproductgroupid) = :cproductgroupid" ;
Map<String, Object> updateParamMap = getParms(miWoOrModel);
// 向 ERP 更新数据
t100QueryManager.executeBySql(upSql, updateParamMap);
}
out.setResult("PASS");
transactionManager.commit(status);
}catch(Exception e){
e.printStackTrace();
this.rollbackTransaction();
out.setResult("FAIL");
out.setErrorMessage(e.getMessage());
transactionManager.rollback(status);
}finally{
Map<String,String> rsData = new HashMap<>();
out.setRsData(rsData);
BeanUtil.get(MonitorSysUtil.class).setInterfaceLog(miWoOrModel,DateUtil.dateTimeStr(time,"yyyy-MM-dd HH:mm:ss:SSS"),out,"ix_work_order");
}
return out;
}