泛微二开系列(五)事务回滚

一、事务回滚相关方法

RecordSetTrans rs = new RecordSetTrans();//创建事务

rs.setAutoCommit(false);//开启手动提交
rs.rollback();//回滚

rs.commit();//提交

二、具体例子

package weaversj.webservice.workflow.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaversj.util.BigDecimalArithUtil;
import weaversj.util.InsertOrUpdateTableUtil;
import weaversj.util.LogInfoUtil;
import weaversj.util.WeaverSJUtil;

public class OptionBudgetAction {
	private static LogInfoUtil logs = new LogInfoUtil("budget_ys");
	private static final String info = "【预算管理】";
	
	
	private String mess = "";
	private String nowDate = "";
	public synchronized String execute(String type,String reqs) {
		logs.logWriter("---reqs----"+reqs);
		JSONObject returnJson = new JSONObject();
		try {
			JSONArray jsonArrData = JSONArray.parseArray(reqs);
			nowDate = WeaverSJUtil.getNowDate("yyyy-MM-dd");
			if("".equals(type)) {
				returnJson.put("code", 1);
				returnJson.put("mess", "type 为空");
				return returnJson.toString();
			}
			
			
			if(jsonArrData.size() == 0) {
				returnJson.put("code", 1);
				returnJson.put("mess", "reqs 为空");
				return returnJson.toString();
			}
			
			for(int i=0;i<jsonArrData.size();i++) {
				JSONObject jsonData = jsonArrData.getJSONObject(i);
				String xmbh = Util.null2String(jsonData.get("projectCode"));
				String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
				String linNum = Util.null2String(jsonData.get("linNum"));
				String reqNum = Util.null2String(jsonData.get("reqNum"));

				if("".equals(xmbh)) {
					returnJson.put("code", 1);
					returnJson.put("mess", "xmbh 为空");
					return returnJson.toString();
				}
				if("".equals(linNum)) {
					returnJson.put("code", 1);
					returnJson.put("mess", "linNum 为空");
					return returnJson.toString();
				}
				if("".equals(reqNum)) {
					returnJson.put("code", 1);
					returnJson.put("mess", "reqNum 为空");
					return returnJson.toString();
				}
				if(("0".equals(type)||"1".equals(type)||"2".equals(type))&&"".equals(budgetMoney)) {
					returnJson.put("code", 1);
					returnJson.put("mess", "金额必填");
					return returnJson.toString();
				}
			}
			
			
			if("4".equals(type)) {
				returnJson.put("code", 0);
				returnJson.put("mess", "success");
				returnJson.put("data",getBudgetData(jsonArrData));
			}else if("0".equals(type)) {
				if(ZYBudget(jsonArrData)) {
					returnJson.put("code", 0);
					returnJson.put("mess", "success");
				}else{
					returnJson.put("code", 1);
					returnJson.put("mess", mess+"占用失败");
				}
			}else if("1".equals(type)) {
				if(SFBudget(jsonArrData)) {
					returnJson.put("code", 0);
					returnJson.put("mess", "success");
				}else{
					returnJson.put("code", 1);
					returnJson.put("mess", mess+"释放失败");
				}
			}else if("2".equals(type)) {
				if(XHBudget(jsonArrData)) {
					returnJson.put("code", 0);
					returnJson.put("mess", "success");
				}else{
					returnJson.put("code", 1);
					returnJson.put("mess",mess+"消耗失败");
				}
			}
		}catch (Exception e) {
			logs.logWriter("---e----",e);

			e.printStackTrace();
			// TODO: handle exception
			returnJson.put("code", 1);
			returnJson.put("mess", e.getMessage());
			return returnJson.toString();
		}
		return returnJson.toString();
	}

	//消耗
	private boolean XHBudget(JSONArray jsonArrData) {
		
		boolean flag = false;
		RecordSetTrans rs = new RecordSetTrans();
		try {
			rs.setAutoCommit(false);
			for(int i=0;i<jsonArrData.size();i++) {
				JSONObject jsonData = jsonArrData.getJSONObject(i);
				String xmbh = Util.null2String(jsonData.get("projectCode"));
				String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
				
				rs.execute("select shijhf,hlyzy,xmzys from uf_xmlxglb where xmbh = '"+xmbh+"'");
				if(rs.next()) {
					double shijhf = BigDecimalArithUtil.getDouble(Util.null2String(rs.getString("shijhf")));

					double hlyzy = BigDecimalArithUtil.getDouble(Util.null2String(rs.getString("hlyzy")));
					double zy_je = BigDecimalArithUtil.getDouble(budgetMoney);
					if(zy_je>hlyzy) {
						rs.rollback();
						mess = xmbh;
						return false;
					}
					
					double zje = BigDecimalArithUtil.sub(hlyzy, zy_je);
					double xhzye = BigDecimalArithUtil.add(shijhf, zy_je);
					String sql = "update uf_xmlxglb set hlyzy = '"+zje+"',shijhf='"+xhzye+"' where xmbh = '"+xmbh+"'";
					flag = rs.execute(sql);
					logs.logWriter("---消耗----"+sql);
				
				}
			}
			if(flag) {
				rs.commit();
				JSONObject jsonsLog = null;
				for(int i=0;i<jsonArrData.size();i++) {
					JSONObject jsonData = jsonArrData.getJSONObject(i);
					String xmbh = Util.null2String(jsonData.get("projectCode"));
					String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
					String linNum = Util.null2String(jsonData.get("linNum"));
					String reqNum = Util.null2String(jsonData.get("reqNum"));
					jsonsLog  =  new JSONObject();
					jsonsLog.put("projectCode",xmbh);
					jsonsLog.put("reqNum",reqNum);
					jsonsLog.put("linNum",linNum);
					jsonsLog.put("budgetMoney",budgetMoney);
					jsonsLog.put("type","2");
					jsonsLog.put("reday",nowDate);
					sendLog(jsonsLog);
				}
			}else {
				rs.rollback();
			}
		}catch (Exception e) {
			rs.rollback();
			// TODO: handle exception
		}
		return flag;
	}

	//释放
	private boolean SFBudget(JSONArray jsonArrData) {
		
		boolean flag = false;
		RecordSetTrans rs = new RecordSetTrans();

		try {
			rs.setAutoCommit(false);
			for(int i=0;i<jsonArrData.size();i++) {
				JSONObject jsonData = jsonArrData.getJSONObject(i);
				String xmbh = Util.null2String(jsonData.get("projectCode"));
				String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
				
				rs.execute("select shijhf,hlyzy,xmzys from uf_xmlxglb where xmbh = '"+xmbh+"'");
				if(rs.next()) {
					double hlyzy = BigDecimalArithUtil.getDouble(Util.null2String(rs.getString("hlyzy")));
					double zy_je = BigDecimalArithUtil.getDouble(budgetMoney);
					if(zy_je>hlyzy) {
						rs.rollback();
						mess = xmbh;
						return false;
					}
					double zje = BigDecimalArithUtil.sub(hlyzy, zy_je);
					String sql = "update uf_xmlxglb set hlyzy = '"+zje+"' where xmbh = '"+xmbh+"'";
					flag = rs.execute(sql);
					
					logs.logWriter("--释放----"+sql);

				}
			}
			if(flag) {
				rs.commit();
				JSONObject jsonsLog = null;
				for(int i=0;i<jsonArrData.size();i++) {
					JSONObject jsonData = jsonArrData.getJSONObject(i);
					String xmbh = Util.null2String(jsonData.get("projectCode"));
					String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
					String linNum = Util.null2String(jsonData.get("linNum"));
					String reqNum = Util.null2String(jsonData.get("reqNum"));
					jsonsLog  =  new JSONObject();
					jsonsLog.put("projectCode",xmbh);
					jsonsLog.put("reqNum",reqNum);
					jsonsLog.put("linNum",linNum);
					jsonsLog.put("budgetMoney",budgetMoney);
					jsonsLog.put("type","1");
					jsonsLog.put("reday",nowDate);
					sendLog(jsonsLog);
				}
			}else {
				rs.rollback();
			}
		}catch (Exception e) {
			rs.rollback();
			// TODO: handle exception
		}
		return flag;
	}
	
	//占用
	private boolean ZYBudget(JSONArray jsonArrData) {
		boolean flag = false;
		RecordSetTrans rs = new RecordSetTrans();

		try {
			rs.setAutoCommit(false);
			for(int i=0;i<jsonArrData.size();i++) {
				JSONObject jsonData = jsonArrData.getJSONObject(i);
				String xmbh = Util.null2String(jsonData.get("projectCode"));
				String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
				logs.logWriter("--xmbh----"+xmbh);
				logs.logWriter("--budgetMoney----"+budgetMoney);

				rs.execute("select shijhf,hlyzy,xmzys from uf_xmlxglb where xmbh = '"+xmbh+"'");
				if(rs.next()) {
					double hlyzy = BigDecimalArithUtil.getDouble(Util.null2String(rs.getString("hlyzy")));
					double zy_je = BigDecimalArithUtil.getDouble(budgetMoney);
					double zje = BigDecimalArithUtil.add(hlyzy, zy_je);
					
					logs.logWriter("--hlyzy----"+hlyzy);
					logs.logWriter("--zy_je----"+zy_je);
					logs.logWriter("--zje----"+zje);

					String sql = "update uf_xmlxglb set hlyzy = '"+zje+"' where xmbh = '"+xmbh+"'";
					flag = rs.execute(sql);
					
					logs.logWriter("--占用----"+sql);

				}
			}
			if(flag) {
				rs.commit();
				JSONObject jsonsLog = null;
				for(int i=0;i<jsonArrData.size();i++) {
					JSONObject jsonData = jsonArrData.getJSONObject(i);
					String xmbh = Util.null2String(jsonData.get("projectCode"));
					String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
					String linNum = Util.null2String(jsonData.get("linNum"));
					String reqNum = Util.null2String(jsonData.get("reqNum"));
					jsonsLog  =  new JSONObject();
					jsonsLog.put("projectCode",xmbh);
					jsonsLog.put("reqNum",reqNum);
					jsonsLog.put("linNum",linNum);
					jsonsLog.put("budgetMoney",budgetMoney);
					jsonsLog.put("type","0");
					jsonsLog.put("reday",nowDate);
					sendLog(jsonsLog);
				}
			}else {
				rs.rollback();
			}
		}catch (Exception e) {
			rs.rollback();
			// TODO: handle exception
		}
		return flag;
	}
	
	private JSONArray getBudgetData(JSONArray jsonArrData) {
		
		RecordSet rs = new RecordSet();

		JSONArray jsonArr = new JSONArray();
		JSONObject jsons = null;
		JSONObject jsonsLog = null;

		for(int i=0;i<jsonArrData.size();i++) {
			JSONObject jsonData = jsonArrData.getJSONObject(i);
			String xmbh = Util.null2String(jsonData.get("projectCode"));
			String budgetMoney = Util.null2String(jsonData.get("budgetMoney"));
			String linNum = Util.null2String(jsonData.get("linNum"));
			String reqNum = Util.null2String(jsonData.get("reqNum"));
			
			rs.execute("select * from uf_xmlxglb where xmbh = '"+xmbh+"'");
			if(rs.next()) {
				jsons =  new JSONObject();
				jsons.put("projectCode", xmbh);//项目编号
				jsons.put("consumeBudgetMoney", isNullThen0(Util.null2String(rs.getString("shijhf"))));//消耗金额
				jsons.put("occupyBudgetMoney",isNullThen0( Util.null2String(rs.getString("hlyzy"))));//已占用金额
				jsons.put("totalBudgetMoney", isNullThen0(Util.null2String(rs.getString("xmzys"))));//预算金额
				jsons.put("linNum", linNum);//预算金额
				jsonArr.add(jsons);
			}
			
			jsonsLog  =  new JSONObject();
			jsonsLog.put("projectCode",xmbh);
			jsonsLog.put("reqNum",reqNum);
			jsonsLog.put("linNum",linNum);
			jsonsLog.put("budgetMoney",budgetMoney);
			jsonsLog.put("type","3");
			jsonsLog.put("reday",nowDate);
			sendLog(jsonsLog);
		}
		return jsonArr;
	}
	
	private void sendLog(JSONObject jsons) {
		InsertOrUpdateTableUtil insert = new InsertOrUpdateTableUtil();
		insert.insertData(new RecordSet(), jsons, "uf_hlylxlog");
	}
	
	private String isNullThen0(String str) {
		return "".equals(str)?"0.00":str;
	}
}