package com.steering.mes.mcp.CollarLibrary;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.converter.QualityDesignTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.CouplingMatButtressEntity;
import com.steering.mes.mcp.entity.CouplingMatDealEntity;
import com.steering.mes.mcp.entity.CouplingMatMEntity;
import com.steering.mes.mcp.entity.PlnOrderJgEntity;

/**
 * 接箍倒库
 * 
 * @author tjg 2019/01/23
 * @version v1.0
 */
public class FrmOutManagJg extends DBCommon {

	public static String toSqlConditionString(String column, String[] args) {
		StringBuffer str = new StringBuffer();
		if (args != null && args.length > 0) {
			str.append(" AND ");
			str.append(column.toUpperCase());
			str
					.append("  IN (SELECT B.PLINE_NAME  FROM COM_BASE_PLINE B WHERE B.PLINE_CODE IN(");
			for (String arg : args) {
				if (arg != null) {
					str.append("'");
					str.append(arg);
					str.append("',");
				}
			}
			str.deleteCharAt(str.lastIndexOf(","));
			str.append("))");
		}
		return str.toString();
	}

	/**
	 * 查询接箍库存信息
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject doQueryInfoResult(ArrayList<String> list,
			String[] arr, String orderNo, String codeJg) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.queryInventory");
		// if (!list.get(0).equals("")) {
		// sql += " AND T1.STORAGE_NO ='" + list.get(0) + "'";
		// } else {
		// sql += CoreConverter.toSqlConditionString("T1.STORAGE_NO", arr);
		// }
		sql += " AND T.judge_stove_no LIKE '%' || '" + list.get(0) + "' || '%'";
		if (!list.get(1).equals("")) {
			sql += " AND  T.create_time BETWEEN to_date('" + list.get(3)
					+ "', 'YYYY-MM-DD HH24:MI:SS')";
			sql += " AND  to_date('" + list.get(5)
					+ "', 'YYYY-MM-DD HH24:MI:SS')";
		}
		if (!orderNo.equals("")) {
			sql += " AND T2.ORDER_NO || '/' || T2.ORDER_SEQ like '%' || '"
					+ orderNo + "' || '%'";
		}
		if (!codeJg.equals("")) {
			sql += " AND T.CODE_JG='" + codeJg + "'";
		}
		sql += " ORDER BY T.create_time desc";
		return this.getData(sql);
	}

	/**
	 * 查询去向
	 */
	public CoreReturnObject getGoWhereName(String ordLnDPk) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.getGoWhereName");
		return this.getData(sql, new Object[] { ordLnDPk });
	}

	/**
	 * 查询交易表倒库实绩
	 * 
	 * @param list
	 *            传递参数
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject queryOutPerfo(ArrayList<String> list, String[] arr,
			String orderNo, String codeJg, String flag, String handleflag) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.queryOutPerfo");
		sql += " AND t.judge_stove_no LIKE '%' || '" + list.get(0) + "' || '%'";
		if (!list.get(1).equals("")) {
			sql += " AND  t.create_time BETWEEN  to_date('" + list.get(3)
					+ "', 'YYYY-MM-DD HH24:MI:SS')";
			sql += " AND  to_date('" + list.get(5)
					+ "', 'YYYY-MM-DD HH24:MI:SS') ";
		}

		sql += " AND t.DEAL_TYPE_CODE ='800801' AND T.OUT_FLAG='2'";
		if (!orderNo.equals("")) {
			sql += " AND T.ORDER_NO || '/' || T.ORDER_SEQ like '%' || '"
					+ orderNo + "' || '%'";
		}
		if (!codeJg.equals("")) {
			sql += " AND T.CODE_JG='" + codeJg + "'";
		}
		if (flag.equals("1")) {
			sql += " AND TO_NUMBER(T.DEAL_NUM)-TO_NUMBER(NVL(T.USED_NUM,0))-TO_NUMBER(NVL(T.FAILL_NUM,0))-TO_NUMBER(NVL(T.BACK_NUM,0))>0";
		}
		if (!handleflag.equals("")) {
			sql += " AND T.HANDLE_FLAG='" + handleflag + "'";
		}
		sql += " ORDER BY CREATE_TIME DESC";
		return this.getData(sql);
	}

	/**
	 * 查询领用信息
	 * 
	 * @param list
	 *            传递参数
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject queryRecipients(String strFurnace,
			String strRegStart, String strRegEnd, String orderNo,
			String codeJg, String flag, String[] plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.queryOutPerfo");
		// sql += "  AND KEP_NO LIKE '%' || '" + list.get(1) + "' || '%' ";
		if (plineCode != null && plineCode.length > 0) {
			sql += toSqlConditionString("T.GOWHERE_NAME", plineCode);
		}
		if (!strFurnace.equals("")) {
			sql += " AND t.judge_stove_no LIKE '%' || '" + strFurnace
					+ "' || '%'";
		}

		sql += " AND DECODE('" + strRegStart
				+ "', NULL, '1', t.create_time) BETWEEN DECODE('" + strRegStart
				+ "', NULL, '1', to_date('" + strRegStart
				+ "', 'YYYY-MM-DD HH24:MI:SS'))";
		sql += " AND DECODE('" + strRegEnd + "', NULL, '1', to_date('"
				+ strRegEnd + "', 'YYYY-MM-DD HH24:MI:SS')) ";
		sql += " AND t.DEAL_TYPE_CODE ='800801' AND T.OUT_FLAG='2'";
		if (!orderNo.equals("")) {
			sql += " AND T.ORDER_NO || '/' || T.ORDER_SEQ like '%' || '"
					+ orderNo + "' || '%'";
		}
		if (!codeJg.equals("")) {
			sql += " AND T.CODE_JG='" + codeJg + "'";
		}
		if (flag.equals("0")) {
			sql += " AND TO_NUMBER(T.DEAL_NUM)-TO_NUMBER(NVL(T.USED_NUM,0))-TO_NUMBER(NVL(T.FAILL_NUM,0))-TO_NUMBER(NVL(T.BACK_NUM,0))>0";
		}
		sql += " ORDER BY CREATE_TIME DESC";
		return this.getData(sql);
	}

	/**
	 * 查询计划信息
	 * 
	 * @param con
	 * @param sendPlanNo
	 * @return
	 * @throws SQLException
	 */
	public static CouplingMatMEntity getSendPlanCInfo(Connection con,
			String matNo, String matNoXh, String guid) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.QUERYMatButterResult");
		PreparedStatement ps = con.prepareStatement(sql);
//		ps.setObject(1, guid);
		ps.setObject(1, matNoXh);
		ps.setObject(2, matNo);
		ResultSet rs = ps.executeQuery();
		List<CouplingMatMEntity> sendPlanCEntitys = EntityTool.getEntitysByRs(
				rs, CouplingMatMEntity.class);
		rs.close();
		ps.close();
		if (sendPlanCEntitys == null || sendPlanCEntitys.size() == 0) {
			return null;
		} else {
			for (CouplingMatMEntity sendPlanCEntity : sendPlanCEntitys) {
				QualityDesignTool.nullConverNullString(sendPlanCEntity);
			}
			return sendPlanCEntitys.get(0);
		}
	}

	/**
	 * 查询交易表信息
	 * 
	 * @param con
	 * @param sendPlanNo
	 * @return
	 * @throws SQLException
	 */
	public static CouplingMatDealEntity getSendMatDeal(Connection con,
			String dealNo) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupMatCollarShar.QueryDeal");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dealNo);
		ResultSet rs = ps.executeQuery();
		List<CouplingMatDealEntity> sendPlanCEntitys = EntityTool
				.getEntitysByRs(rs, CouplingMatDealEntity.class);
		rs.close();
		ps.close();
		if (sendPlanCEntitys == null || sendPlanCEntitys.size() == 0) {
			return null;
		} else {
			for (CouplingMatDealEntity sendPlanCEntity : sendPlanCEntitys) {
				QualityDesignTool.nullConverNullString(sendPlanCEntity);
			}
			return sendPlanCEntitys.get(0);
		}
	}

	/***
	 * 生成交易单号
	 * 
	 * @param plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 */
	public static String doAddDealOrde(String lx, String woId,
			Connection connection) {
		String resultNo = "";
		PreparedStatement psta = null;
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.queryMaxDealOrde");// 查询交易单号
		Date now = new Date();
		SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd");
		String systime = date.format(now);
		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, lx);
			psta.setObject(2, systime);
			psta.setObject(3, date1.format(now));
			psta.setObject(4, woId);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("DEAL_ORDE");
			}
			rs1.close();
			psta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return resultNo;
	}

	/***
	 * 最大材料序号
	 * 
	 * @param matNo
	 * @param connection
	 * @return
	 */
	public static String doAddButterMax(String matNo, Connection connection) {
		String resultNo = "";
		PreparedStatement psta = null;
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.queryMaxButter");// 查询最大材料序号
		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, matNo);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("MAT_NO_XH");
			}
			rs1.close();
			psta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return resultNo;
	}

	/**
	 * 新增
	 * 
	 * @param list
	 *            传递参数
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject addOutLibResult(ArrayList<ArrayList<String>> list1) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String systime = dateFormat.format(now);

			for (int i = 0; i < list1.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) list1.get(i);
				// String relutNo = list.get(2);
				// String planNo = list.get(3);
				// String bc = list.get(4);
				// String bz = list.get(5);
				String name = list.get(6);
				int count = Integer.parseInt(list.get(7));
				// String woId = list.get(8);
				String matNo = list.get(9);
				String matNoXh = list.get(10);
				String weghit = list.get(11);
				String guid = list.get(12);
				String gowhereName = list.get(13);
				// 获取材料实体了
				CouplingMatMEntity pln = getSendPlanCInfo(connection, matNo,
						matNoXh, guid);
				if (pln == null) {
					cro.setV_errMsg("未找到库存信息,请确认！");
					return cro;
				}
				CouplingMatDealEntity matDeal = new CouplingMatDealEntity();
				// 获取交易单号
				String dealOrde = doAddDealOrde("OA", "800801", connection);
				// 新增交易表
				matDeal.setDealOrde(dealOrde);
				matDeal.setDealTypeCode("800801");
				matDeal.setDealTypeName("投料出库");
				matDeal.setSourceCode("");
				matDeal.setSourceName("");
				matDeal.setTarCode("");
				matDeal.setTarName("");
				matDeal.setKepNo(pln.getKepNo());
				matDeal.setMatNo(pln.getMatNo());
				matDeal.setResultNo(pln.getResultNo());
				matDeal.setZyPlanId(pln.getZyPlanId());
				matDeal.setWoId(pln.getWoId());
				matDeal.setProPlanId(pln.getProPlanId());
				matDeal.setJgGroupId("");
				matDeal.setPlineCode(pln.getPlineCode());
				matDeal.setPlineName(pln.getPlineName());
				matDeal.setMatOriginCode("");
				matDeal.setMatOriginName("");
				matDeal.setSoureCode("801302");
				matDeal.setSoureName("产线");
				matDeal.setJudgeStoveNo(pln.getJudgeStoveNo());
				matDeal.setBatchNo(pln.getBatchNo());
				matDeal.setBlWlId(pln.getBlWlId());
				matDeal.setBlWlDesc(pln.getBlWlDesc());
				matDeal.setMinD(pln.getMinD());
				matDeal.setMinH(pln.getMinH());
				matDeal.setCodeJg(pln.getCodeJg());
				matDeal.setGradecode(pln.getGradecode());
				matDeal.setGradename(pln.getGradename());
				matDeal.setSteelcode(pln.getSteelcode());
				matDeal.setSteelname(pln.getSteelname());
				matDeal.setModelCode(pln.getModelCode());
				matDeal.setModelDesc(pln.getModelDesc());
				matDeal.setOutdiameter(pln.getOutdiameter());
				matDeal.setWallthick(pln.getWallthick());
				matDeal.setSpecJg(pln.getSpecJg());
				matDeal.setSpecJgDesc(pln.getSpecJgDesc());
				matDeal.setStyleJg(pln.getStyleJg());
				matDeal.setDiameter(pln.getDiameter());
				matDeal.setCheckJVal(pln.getCheckJVal());
				matDeal.setAimlength(pln.getAimlength());
				matDeal.setSingleweight(pln.getSingleweight());
				matDeal.setMonitordemand(pln.getMonitordemand());
				matDeal.setIfMonitorJg(pln.getIfMonitorJg());
				matDeal.setPrdcrNo(pln.getPrdcrNo());
				matDeal.setProducer(pln.getProducer());
				matDeal.setProducerTime(pln.getProducerTime());
				matDeal.setMachNm(pln.getMachNm());
				matDeal.setMachNo(pln.getMachNo());
				matDeal.setDealNum(String.valueOf(count));
				matDeal.setDealWt(weghit);
				matDeal.setBalYearMonth(systime.substring(0, 7));
				matDeal.setFInTime(systime);
				matDeal.setDealTime(systime);
				matDeal.setStorageNo(pln.getStorageNo());
				matDeal.setLocationNo(pln.getLocationNo());
				matDeal.setStorageAttr(pln.getStorageAttr());
				matDeal.setStorageAttrName(pln.getStorageAttrName());
				matDeal.setStorageTypeNo(pln.getStorageTypeNo());
				matDeal.setStorageTypeName(pln.getStorageTypeName());
				matDeal.setHandFlag("1");
				matDeal.setDemandBlast(pln.getDemandBlast());
				matDeal.setDemandBonderizing(pln.getDemandBonderizing());
				matDeal.setDemandCopper(pln.getDemandCopper());
				matDeal.setDemandDetection(pln.getDemandDetection());
				matDeal.setCustAlphaNo("");
				matDeal.setOrderSpecialDesc(pln.getOrderSpecialDesc());
				matDeal.setAlphaCode(pln.getAlphaCode());
				matDeal.setDeterNo(pln.getDeterNo());
				matDeal.setDeterResults(pln.getDeterResults());
				matDeal.setColOperator(name);
				matDeal.setMemo(pln.getMemo());
				matDeal.setCreateName(name);
				matDeal.setCreateTime(systime);
				matDeal.setProductFlag(pln.getProductFlag());
				matDeal.setMatStatus(pln.getMatStatus());
				matDeal.setDeterStatus(pln.getDeterStatus());
				matDeal.setCodeJgBl(pln.getCodeJgBl());
				matDeal.setCraftFileNo(pln.getCraftFileNo());
				matDeal.setCraftSeq(pln.getCraftSeq());
				matDeal.setBelongCode(pln.getBelongCode());
				matDeal.setBelongName(pln.getBelongName());
				matDeal.setCoatType(pln.getCoatType());
				matDeal.setTarRecFlag("1");
				matDeal.setMatNoXh(matNoXh);
				matDeal.setQcmStaus(pln.getQcmStaus());
				matDeal.setOutFlag("2");
				matDeal.setJgOrderno(pln.getJgOrderno());
				matDeal.setOrderNo(pln.getOrderNo());
				matDeal.setOrderSeq(pln.getOrderSeq());
				matDeal.setDeliveryNo(pln.getOrderSeqNo());
				matDeal.setOrdPk(pln.getOrdPk());
				matDeal.setOrdLnPk(pln.getOrdLnPk());
				matDeal.setOrdLnDlyPk(pln.getOrdLnDlyPk());
				matDeal.setCraftFileNoOrd(pln.getCraftFileNoOrd());
				matDeal.setCraftSeqOrd(pln.getCraftSeqOrd());
				matDeal.setStoveNo(pln.getStoveNo());
				matDeal.setFlagStoveNo(pln.getFlagStoveNo());
				matDeal.setGowhereName(gowhereName);
				String sqlAddDeal = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.AddDealResult"), matDeal);// 新增交易信息
				ps = connection.prepareStatement(sqlAddDeal);
				ps.executeUpdate();
				ps.close();

				// 获取交易单号
				dealOrde = doAddDealOrde("CQ", "800809", connection);
				// 新增交易表
				matDeal.setDealOrde(dealOrde);
				matDeal.setDealTypeCode("800809");
				matDeal.setDealTypeName("产权出库");
				sqlAddDeal = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.AddDealResult"), matDeal);// 新增交易信息
				ps = connection.prepareStatement(sqlAddDeal);
				ps.executeUpdate();
				ps.close();

				// 判断是否该合同库存全部出库

				int proNumAll = Integer.parseInt(pln.getProNumAll()) - count;// 库存剩余个数
				int pieces = Integer.parseInt(pln.getPieces()) - count;// 库存位置剩余个数
				int butCOUNT = Integer.parseInt(pln.getProNum()) - count;// 合同剩余个

				int countBack = 0;
				// 查询材料备份表是否存在记录
				String sqlMatBack = "SELECT COUNT(*) COUNT FROM COUPLING_MAT_BACKUP T WHERE T.MAT_NO=?";
				ps = connection.prepareStatement(sqlMatBack);
				ps.setObject(1, pln.getMatNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countBack = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countBack == 0) {
					// 新增备份表
					String addMatBack = CoreXmlSqlParsersFactory
							.getSql("FrmOutManagJg.AddMatBack");
					ps = connection.prepareStatement(addMatBack);
					ps.setObject(1, count);
					ps.setObject(2, weghit);
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				} else {
					// 修改备份表
					String updateBack = "UPDATE COUPLING_MAT_BACKUP T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)+TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)+TO_NUMBER(?) WHERE T.MAT_NO=?";
					ps = connection.prepareStatement(updateBack);
					ps.setObject(1, count);
					ps.setObject(2, weghit);
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				}

				if (proNumAll == 0) {// 库存剩余为0
					// 删除材料表
					String deleteMat = "DELETE COUPLING_MAT_M WHERE MAT_NO = ?";
					ps = connection.prepareStatement(deleteMat);
					ps.setObject(1, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				} else {
					// 修改库存表个
					String updateMat = "UPDATE COUPLING_MAT_M T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)-TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)-TO_NUMBER(?) WHERE T.MAT_NO=?";
					ps = connection.prepareStatement(updateMat);
					ps.setObject(1, count);
					ps.setObject(2, weghit);
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
				if (pieces == 0) {// 库存位置剩余0
					// 删除位置表
					String deleteMatButt = "DELETE COUPLING_MAT_BUTTRESS  WHERE MAT_NO = ?  AND MAT_NO_XH = ?";
					ps = connection.prepareStatement(deleteMatButt);
					ps.setObject(1, pln.getMatNo());
					ps.setObject(2, matNoXh);
					ps.executeUpdate();
					ps.close();
				} else {
					// 修改位置个
					String updateMatButt = "UPDATE COUPLING_MAT_BUTTRESS T SET T.PIECES=TO_NUMBER(T.PIECES)-TO_NUMBER(?),T.WEIGHT=TO_NUMBER(T.WEIGHT)-TO_NUMBER(?) WHERE T.MAT_NO=? AND T.MAT_NO_XH = ?";
					ps = connection.prepareStatement(updateMatButt);
					ps.setObject(1, count);
					ps.setObject(2, weghit);
					ps.setObject(3, pln.getMatNo());
					ps.setObject(4, matNoXh);
					ps.executeUpdate();
					ps.close();
				}
				if (butCOUNT < 0) {// 接箍工单详情剩余0
					// 删除接箍工单详情
					// String deleteMidd =
					// "DELETE PLN_ZY_JG_MIDDLE  WHERE GUID = ? ";
					// ps = connection.prepareStatement(deleteMidd);
					// ps.setObject(1, guid);
					// ps.executeUpdate();
					// ps.close();
					cro.setV_errMsg("要领用支数不能大于可领用支数！");
					return cro;
				} else {
					// 修改接箍工单详情
					String updateMidd = "UPDATE PLN_ZY_JG_MIDDLE T SET T.PLAN_NUM=TO_NUMBER(T.PLAN_NUM)-TO_NUMBER(?),T.PLAN_WT=TO_NUMBER(T.PLAN_WT)-TO_NUMBER(?) WHERE T.GUID=?";
					ps = connection.prepareStatement(updateMidd);
					ps.setObject(1, count);
					ps.setObject(2, weghit);
					ps.setObject(3, guid);
					ps.executeUpdate();
					ps.close();
				}
			}

			cro.setV_errMsg("领用出库成功！");
			connection.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 撤销出库（倒库）实绩记录
	 * 
	 * @param list
	 *            传递参数
	 * @return obj
	 */
	public CoreReturnObject deleteOutLibResult(ArrayList<ArrayList<String>> list) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String systime = dateFormat.format(now);
			for (int i = 0; i < list.size(); i++) {
				ArrayList<String> pram = (ArrayList<String>) list.get(i);
				// String relutNo = pram.get(0);
				String matNo = pram.get(1);
				// String kenNo = pram.get(2);
				// String bc = pram.get(3);
				// String bz = pram.get(4);
				String name = pram.get(5);
				String dealNo = pram.get(6);
				String matNoXh = pram.get(7);
				String jgOrderno = pram.get(8);

				// 查询交易表信息
				CouplingMatDealEntity pln = getSendMatDeal(connection, dealNo);
				if (pln == null) {
					cro.setV_errMsg("未找到交易信息,请确认！");
					return cro;
				}
				if (Integer.parseInt(pln.getUsedNum()) > 0) {
					cro.setV_errMsg("您选择的领用出库记录已使用,不能撤销领用！");
					return cro;
				}
				// int deaNum = Integer.parseInt(pln.getDealNum());//出库个数
				// 删除交易表
				String deleteMatDeal = "DELETE COUPLING_MAT_DEAL T WHERE T.DEAL_ORDE = ? AND T.OUT_FLAG='2'";
				ps = connection.prepareStatement(deleteMatDeal);
				ps.setObject(1, dealNo);
				ps.executeUpdate();
				ps.close();

				// 删除产权出库
				String deleteMatDealC = "DELETE COUPLING_MAT_DEAL T WHERE T.DEAL_ORDE = ? AND T.OUT_FLAG='2'";
				ps = connection.prepareStatement(deleteMatDealC);
				ps.setObject(1, dealNo.replace("OA", "CQ"));
				ps.executeUpdate();
				ps.close();

				int countMat = 0;
				// 查询库存表是否存在数据
				String sqlMat = "SELECT COUNT(*) COUNT  FROM COUPLING_MAT_M T WHERE T.MAT_NO = ? ";
				ps = connection.prepareStatement(sqlMat);
				ps.setObject(1, pln.getMatNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countMat = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countMat == 0) {
					// 新增材料表
					String sqlAddMat = CoreXmlSqlParsersFactory
							.getSql("FrmOutManagJg.AddMatResult");// 新增库存数据
					ps = connection.prepareStatement(sqlAddMat);
					ps.setObject(1, pln.getDealNum());
					ps.setObject(2, pln.getDealWt());
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();

				} else {
					// 修改材料表
					String updateMat = "UPDATE COUPLING_MAT_M T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)+TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)+TO_NUMBER(?) WHERE T.MAT_NO=?";
					ps = connection.prepareStatement(updateMat);
					ps.setObject(1, pln.getDealNum());
					ps.setObject(2, pln.getDealWt());
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
				int countButt = 0;
				// 查询位置表示是否存在数据
				String sqlButt = "SELECT COUNT(*) COUNT  FROM COUPLING_MAT_BUTTRESS T WHERE T.MAT_NO = ? AND T.MAT_NO_XH=?";
				ps = connection.prepareStatement(sqlButt);
				ps.setObject(1, pln.getMatNo());
				ps.setObject(2, pln.getMatNoXh());
				rs = ps.executeQuery();
				if (rs.next()) {
					countButt = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();

				if (countButt == 0) {
					// 新增位置表
					CouplingMatButtressEntity butt = new CouplingMatButtressEntity();
					butt.setMatNo(pln.getMatNo());
					butt.setMatNoXh(pln.getMatNoXh());
					butt.setProductFlag(pln.getProductFlag());
					butt.setMatStatus(pln.getMatStatus());
					butt.setProPlanId(pln.getProPlanId());
					butt.setJgGroupId(pln.getJgGroupId());
					butt.setProPlanId1(pln.getProPlanId1());
					butt.setJgGroupId1(pln.getJgGroupId1());
					butt.setMachNo(pln.getMachNo());
					butt.setMachNm(pln.getMachNm());
					butt.setMatOriginCode(pln.getMatOriginCode());
					butt.setMatOriginName(pln.getMatOriginName());
					butt.setSoureCode(pln.getSoureCode());
					butt.setSoureName(pln.getSoureName());
					butt.setPieces(pln.getDealNum());
					butt.setWeight(pln.getDealWt());
					butt.setCustAlphaNo(pln.getCustAlphaNo());
					butt.setOrderSpecialDesc(pln.getOrderSpecialDesc());
					butt.setAlphaCode(pln.getAlphaCode());
					butt.setDealOrde("");
					butt.setKepNo(pln.getKepNo());
					butt.setStorageNo(pln.getStorageNo());
					butt.setLocationNo(pln.getLocationNo());
					butt.setStorageAttr(pln.getStorageAttr());
					butt.setStorageAttrName(pln.getStorageAttrName());
					butt.setStorageTypeNo(pln.getStorageTypeNo());
					butt.setStorageTypeName(pln.getStorageTypeName());
					butt.setManagementNo("");
					butt.setManagementName("");
					butt.setMemo("");
					butt.setCreateName(name);
					butt.setCreateTime(systime);
					String sqlAddButt = EntityTool.fillSqlByEntity(
							CoreXmlSqlParsersFactory
									.getSql("FrmOutManagJg.AddButterResult"),
							butt);// 新增位置表
					ps = connection.prepareStatement(sqlAddButt);
					ps.executeUpdate();
					ps.close();
				} else {
					// 修改位置表
					String updateMatButt = "UPDATE COUPLING_MAT_BUTTRESS T SET T.PIECES=TO_NUMBER(T.PIECES)+TO_NUMBER(?),T.WEIGHT=TO_NUMBER(T.WEIGHT)+TO_NUMBER(?) WHERE T.MAT_NO=? AND T.MAT_NO_XH = ?";
					ps = connection.prepareStatement(updateMatButt);
					ps.setObject(1, pln.getDealNum());
					ps.setObject(2, pln.getDealWt());
					ps.setObject(3, pln.getMatNo());
					ps.setObject(4, pln.getMatNoXh());
					ps.executeUpdate();
					ps.close();
				}

				int countBack = 0;
				// 查询材料备份表支数
				String sqlMatBack = "SELECT T.PRO_NUM  FROM COUPLING_MAT_BACKUP T WHERE T.MAT_NO= ?";
				ps = connection.prepareStatement(sqlMatBack);
				ps.setObject(1, pln.getMatNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countBack = rs.getInt("PRO_NUM");
				}
				rs.close();
				ps.close();
				if (  countBack <= Integer.parseInt(pln.getDealNum())) {
					// 删除备份表
					String updateBack = "DELETE COUPLING_MAT_BACKUP WHERE MAT_NO = ?";
					ps = connection.prepareStatement(updateBack);
					ps.setObject(1, pln.getMatNo());
					ps.executeUpdate();
					ps.close();

				} else {
					// 修改备份表
					String updateBack = "UPDATE COUPLING_MAT_BACKUP T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)-TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)-TO_NUMBER(?) WHERE T.MAT_NO=?";
					ps = connection.prepareStatement(updateBack);
					ps.setObject(1, pln.getDealNum());
					ps.setObject(2, pln.getDealWt());
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
				String guid = "";
				// 查询是否还存在接箍对应合同信息
				String sqlMidd = "SELECT T.GUID FROM PLN_ZY_JG_MIDDLE T WHERE T.MAT_NO=? AND T.MAT_NO_XH=? AND T.JG_ORDERNO=?";
				ps = connection.prepareStatement(sqlMidd);
				ps.setObject(1, matNo);
				ps.setObject(2, pln.getMatNoXh());
				ps.setObject(3, jgOrderno);
				rs = ps.executeQuery();
				if (rs.next()) {
					guid = rs.getString("GUID");
				}
				rs.close();
				ps.close();
				
				// 修改接箍工单详情
				String updateMidd = "UPDATE PLN_ZY_JG_MIDDLE T SET T.PLAN_NUM=TO_NUMBER(T.PLAN_NUM)+TO_NUMBER(?),T.PLAN_WT=TO_NUMBER(T.PLAN_WT)+TO_NUMBER(?) WHERE T.GUID=?";
				ps = connection.prepareStatement(updateMidd);
				ps.setObject(1, pln.getDealNum());
				ps.setObject(2, pln.getDealWt());
				ps.setObject(3, guid);
				ps.executeUpdate();
				ps.close();
				// }
			}
			cro.setV_errMsg("撤销成功！");
			connection.commit();

		} catch (Exception e) {
			System.out.println(e.getMessage());
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 回库
	 * 
	 * @param list
	 *            传递参数
	 * @return obj
	 */
	public CoreReturnObject doBackLibrary(ArrayList<ArrayList<String>> list) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String systime = dateFormat.format(now);
			for (int i = 0; i < list.size(); i++) {
				ArrayList<String> pram = (ArrayList<String>) list.get(i);
				String name = pram.get(0);
				String dealNo = pram.get(1);
				int num = Integer.parseInt(pram.get(2)); //回库个数
				// String bc = pram.get(3);
				// String bz = pram.get(4);
				// 查询交易表信息
				CouplingMatDealEntity pln = getSendMatDeal(connection, dealNo);
				if (pln == null) {
					cro.setV_errMsg("未找到交易信息,请确认！");
					return cro;
				}
				int BackNum = Integer.parseInt(pln.getDealNum())
						- Integer.parseInt(pln.getUsedNum())
						- Integer.parseInt(pln.getFaillNum())
						- Integer.parseInt(pln.getBackNum()); //可回库个数
				if (BackNum <= 0) {
					cro.setV_errMsg("可回库个数为零,不允许回库！");
					return cro;
				}
				if(num > BackNum){
					cro.setV_errMsg("输入的回库个数大于可回库个数,不允许回库！");
					return cro;
				}
				// 查询材料表是否存在数据
				int countMat = 0;
				String sqlMat = "SELECT COUNT(*) COUNT  FROM COUPLING_MAT_M T WHERE T.MAT_NO = ? ";
				ps = connection.prepareStatement(sqlMat);
				ps.setObject(1, pln.getMatNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countMat = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countMat == 0) {
					// 新增材料表
					String sqlAddMat = CoreXmlSqlParsersFactory
							.getSql("FrmOutManagJg.AddMatResult");// 新增库存数据
					ps = connection.prepareStatement(sqlAddMat);
					ps.setObject(1, num);
					ps
							.setObject(2,
									num
											* (Double.parseDouble(pln
													.getSingleweight()) / 1000));
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();

				} else {
					// 修改材料表
					String updateMat = "UPDATE COUPLING_MAT_M T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)+TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)+TO_NUMBER(?) WHERE T.MAT_NO=?";
					ps = connection.prepareStatement(updateMat);
					ps.setObject(1, num);
					ps
							.setObject(2,
									num
											* (Double.parseDouble(pln
													.getSingleweight()) / 1000));
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
				int countButt = 0;
				// 查询位置表示是否存在数据
				String sqlButt = "SELECT COUNT(*) COUNT  FROM COUPLING_MAT_BUTTRESS T WHERE T.MAT_NO = ? AND T.MAT_NO_XH=?";
				ps = connection.prepareStatement(sqlButt);
				ps.setObject(1, pln.getMatNo());
				ps.setObject(2, pln.getMatNoXh());
				rs = ps.executeQuery();
				if (rs.next()) {
					countButt = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();

				if (countButt == 0) {
					// 新增位置表
					CouplingMatButtressEntity butt = new CouplingMatButtressEntity();
					butt.setMatNo(pln.getMatNo());
					butt.setMatNoXh(pln.getMatNoXh());
					butt.setProductFlag(pln.getProductFlag());
					butt.setMatStatus(pln.getMatStatus());
					butt.setProPlanId(pln.getProPlanId());
					butt.setJgGroupId(pln.getJgGroupId());
					butt.setProPlanId1(pln.getProPlanId1());
					butt.setJgGroupId1(pln.getJgGroupId1());
					butt.setMachNo(pln.getMachNo());
					butt.setMachNm(pln.getMachNm());
					butt.setMatOriginCode(pln.getMatOriginCode());
					butt.setMatOriginName(pln.getMatOriginName());
					butt.setSoureCode(pln.getSoureCode());
					butt.setSoureName(pln.getSoureName());
					butt.setPieces(String.valueOf(num));
					butt
							.setWeight(String
									.valueOf(num
											* (Double.parseDouble(pln
													.getSingleweight()) / 1000)));
					butt.setCustAlphaNo(pln.getCustAlphaNo());
					butt.setOrderSpecialDesc(pln.getOrderSpecialDesc());
					butt.setAlphaCode(pln.getAlphaCode());
					butt.setDealOrde("");
					butt.setKepNo(pln.getKepNo());
					butt.setStorageNo(pln.getStorageNo());
					butt.setLocationNo(pln.getLocationNo());
					butt.setStorageAttr(pln.getStorageAttr());
					butt.setStorageAttrName(pln.getStorageAttrName());
					butt.setStorageTypeNo(pln.getStorageTypeNo());
					butt.setStorageTypeName(pln.getStorageTypeName());
					butt.setManagementNo("");
					butt.setManagementName("");
					butt.setMemo("");
					butt.setCreateName(name);
					butt.setCreateTime(systime);
					String sqlAddButt = EntityTool.fillSqlByEntity(
							CoreXmlSqlParsersFactory
									.getSql("FrmOutManagJg.AddButterResult"),
							butt);// 新增位置表
					ps = connection.prepareStatement(sqlAddButt);
					ps.executeUpdate();
					ps.close();
				} else {
					// 修改位置表
					String updateMatButt = "UPDATE COUPLING_MAT_BUTTRESS T SET T.PIECES=TO_NUMBER(T.PIECES)+TO_NUMBER(?),T.WEIGHT=TO_NUMBER(T.WEIGHT)+TO_NUMBER(?) WHERE T.MAT_NO=? AND T.MAT_NO_XH = ?";
					ps = connection.prepareStatement(updateMatButt);
					ps.setObject(1, num);
					ps
							.setObject(2,
									num
											* (Double.parseDouble(pln
													.getSingleweight()) / 1000));
					ps.setObject(3, pln.getMatNo());
					ps.setObject(4, pln.getMatNoXh());
					ps.executeUpdate();
					ps.close();
				}
				int countBack = 0;
				// 查询材料备份表是否存在记录
				String sqlMatBack = "SELECT PRO_NUM FROM COUPLING_MAT_BACKUP T WHERE T.MAT_NO=?";
				ps = connection.prepareStatement(sqlMatBack);
				ps.setObject(1, pln.getMatNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countBack = rs.getInt("PRO_NUM");
				}
				rs.close();
				ps.close();
				if (num - countBack >= 0) {
					// 删除备份表
					String updateBack = "DELETE COUPLING_MAT_BACKUP WHERE MAT_NO = ?";
					ps = connection.prepareStatement(updateBack);
					ps.setObject(1, pln.getMatNo());
					ps.executeUpdate();
					ps.close();

				} else {
					// 修改备份表
					String updateBack = "UPDATE COUPLING_MAT_BACKUP T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)-TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)-TO_NUMBER(?) WHERE T.MAT_NO=?";
					ps = connection.prepareStatement(updateBack);
					ps.setObject(1, num);
					ps
							.setObject(2,
									num
											* (Double.parseDouble(pln
													.getSingleweight()) / 1000));
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
				// String guid = "";
				// // 查询是否还存在接箍对应合同信息
				// String sqlMidd =
				// "SELECT T.GUID FROM PLN_ZY_JG_MIDDLE T WHERE T.MAT_NO=? AND T.JG_ORDERNO=?";
				// ps = connection.prepareStatement(sqlMidd);
				// ps.setObject(1, pln.getMatNo());
				// ps.setObject(2, pln.getJgOrderno());
				// rs = ps.executeQuery();
				// if (rs.next()) {
				// guid = rs.getString("GUID");
				// }
				// rs.close();
				// ps.close();
				// if (guid.equals("")) {
				// // 新增接箍工单详细表
				// String addPlnMidd = CoreXmlSqlParsersFactory
				// .getSql("FrmOutManagJg.AddPlnZyJgMidd");
				// ps = connection.prepareStatement(addPlnMidd);
				// ps.setObject(1, pln.getJgOrderno());
				// ps.setObject(2, num);
				// ps.setObject(
				// 3,
				// num
				// * (Double.parseDouble(pln.getSingleweight()) / 1000));
				// ps.setObject(4, "");
				// ps.setObject(5, name);
				// ps.setObject(6, pln.getOrderNo());
				// ps.setObject(7, pln.getOrderSeq());
				// ps.setObject(8, pln.getDeliveryNo());
				// ps.setObject(
				// 9,
				// pln.getOrdLnDlyPk().substring(0,
				// pln.getOrdLnDlyPk().length() - 6));
				// ps.setObject(
				// 10,
				// pln.getOrdLnDlyPk().substring(0,
				// pln.getOrdLnDlyPk().length() - 3));
				// ps.setObject(11, pln.getOrdLnDlyPk());
				// ps.setObject(12, pln.getCraftSeq());
				// ps.setObject(13, pln.getCraftFileNo());
				// ps.setObject(14, pln.getMatNoXh());
				// ps.setObject(15, pln.getMatNo());
				// ps.executeUpdate();
				// ps.close();
				// } else {
				// // 修改接箍工单详情
				// String updateMidd =
				// "UPDATE PLN_ZY_JG_MIDDLE T SET T.PLAN_NUM=TO_NUMBER(T.PLAN_NUM)+TO_NUMBER(?),T.PLAN_WT=TO_NUMBER(T.PLAN_WT)+TO_NUMBER(?) WHERE T.GUID=?";
				// ps = connection.prepareStatement(updateMidd);
				// ps.setObject(1, num);
				// ps.setObject(
				// 2,
				// num
				// * (Double.parseDouble(pln.getSingleweight()) / 1000));
				// ps.setObject(3, guid);
				// ps.executeUpdate();
				// ps.close();
				// }

				// 修改交易表回库个
				String updateDeal = CoreXmlSqlParsersFactory
						.getSql("FrmOutManagJg.UpdateCouplingDeal");
				ps = connection.prepareStatement(updateDeal);
				ps.setObject(1, num);
				ps.setObject(2, num
						* (Double.parseDouble(pln.getSingleweight()) / 1000));
				ps.setObject(3, num);
				ps.setObject(4, dealNo);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("回库成功！");
			connection.commit();

		} catch (Exception e) {
			System.out.println(e.getMessage());
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 处理
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject doHandle(ArrayList<ArrayList<String>> list) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String systime = dateFormat.format(now);
			for (int i = 0; i < list.size(); i++) {
				ArrayList<String> pram = (ArrayList<String>) list.get(i);
				String name = pram.get(0);
				String dealNo = pram.get(1);
				String handleflag = pram.get(2);

				// 修改处理标识
				String updateDeal = CoreXmlSqlParsersFactory
						.getSql("FrmOutManagJg.UpdateHandleflag");
				ps = connection.prepareStatement(updateDeal);
				ps.setObject(1, name);
				ps.setObject(2, handleflag);
				ps.setObject(3, dealNo);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("处理成功！");
			connection.commit();

		} catch (Exception e) {
			System.out.println(e.getMessage());
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 查询接箍合同信息
	 */
	public static <T> T getQureyJgOrder(Connection con, String JgOrderNo,
			Class<T> cls) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOutManagJg.getQureyJgOrder");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, JgOrderNo);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 拆分
	 * 
	 * @param list
	 *            传递参数
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject doBreakUp(String dealOrde, String jgOrderNo,
			String gowhereName, String num, String bc, String bz, String name) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String systime = dateFormat.format(now);

			// 获取材料实体了
			PlnOrderJgEntity pln = getQureyJgOrder(connection, jgOrderNo,
					PlnOrderJgEntity.class);
			if (pln == null) {
				cro.setV_errMsg("无接箍合同信息!");
				return cro;
			}
			CouplingMatDealEntity matDeal = getSendMatDeal(connection, dealOrde);
			if (matDeal == null) {
				cro.setV_errMsg("未找到交易信息,请确认！");
				return cro;
			}

			int numCount = Integer.parseInt(matDeal.getDealNum())
					- Integer.parseInt(matDeal.getUsedNum())
					- Integer.parseInt(matDeal.getFaillNum())
					- Integer.parseInt(matDeal.getBackNum());

			if (numCount < Integer.parseInt(num)) {
				cro.setV_errMsg("拆分支个数不能大于可以拆分个数！");
				return cro;
			}

			if (Integer.parseInt(num) == Integer.parseInt(matDeal.getDealNum())) {

				matDeal.setJgOrderno(pln.getJgOrderno());
				matDeal.setOrderNo(pln.getOrderNo());
				matDeal.setOrderSeq(pln.getOrderSeq());
				matDeal.setDeliveryNo(pln.getDeliveryNo());
				matDeal.setOrdPk(pln.getOrdLnDlyPk().substring(0,
						pln.getOrdLnDlyPk().length() - 6));
				matDeal.setOrdLnPk(pln.getOrdLnDlyPk().substring(0,
						pln.getOrdLnDlyPk().length() - 3));
				matDeal.setOrdLnDlyPk(pln.getOrdLnDlyPk());
				matDeal.setCraftFileNoOrd(pln.getCraftFileNo());
				matDeal.setCraftSeqOrd(pln.getCraftSeq());
				matDeal.setGowhereName(gowhereName);
				String sqlAddDeal = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.UpdateDeal"), matDeal);// 新增交易信息
				ps = connection.prepareStatement(sqlAddDeal);
				ps.setObject(1, dealOrde);
				ps.executeUpdate();
				ps.close();

				sqlAddDeal = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.UpdateDeal"), matDeal);// 新增交易信息
				ps = connection.prepareStatement(sqlAddDeal);
				ps
						.setObject(1, "CQ"
								+ dealOrde.substring(2, dealOrde.length()));
				ps.executeUpdate();
				ps.close();
			} else {
				double wt = Integer.parseInt(num)
						* Double.parseDouble(matDeal.getSingleweight()) / 1000;
				// 修改交表信息
				String update = "UPDATE COUPLING_MAT_DEAL T SET T.DEAL_NUM=TO_NUMBER(T.DEAL_NUM)-TO_NUMBER(?),T.DEAL_WT=TO_NUMBER(T.DEAL_WT)-TO_NUMBER(?) WHERE T.DEAL_ORDE=?";
				ps = connection.prepareStatement(update);
				ps.setObject(1, num);
				ps.setObject(2, wt);
				ps.setObject(3, dealOrde);
				ps.executeUpdate();
				ps.close();

				// 修改交表信息
				update = "UPDATE COUPLING_MAT_DEAL T SET T.DEAL_NUM=TO_NUMBER(T.DEAL_NUM)-TO_NUMBER(?),T.DEAL_WT=TO_NUMBER(T.DEAL_WT)-TO_NUMBER(?) WHERE T.DEAL_ORDE=?";
				ps = connection.prepareStatement(update);
				ps.setObject(1, num);
				ps.setObject(2, wt);
				ps
						.setObject(3, "CQ"
								+ dealOrde.substring(2, dealOrde.length()));
				ps.executeUpdate();
				ps.close();

				// 获取交易单号
				String dealOrdeNew = doAddDealOrde("OA", "800801", connection);
				// 新增交易表
				matDeal.setDealOrde(dealOrdeNew);
				matDeal.setDealNum(num);
				matDeal.setDealWt(String.valueOf(wt));
				matDeal.setBalYearMonth(systime.substring(0, 7));
				matDeal.setFInTime(systime);
				matDeal.setDealTime(systime);
				matDeal.setCreateName(name);
				matDeal.setCreateTime(systime);
				matDeal.setJgOrderno(pln.getJgOrderno());
				matDeal.setOrderNo(pln.getOrderNo());
				matDeal.setOrderSeq(pln.getOrderSeq());
				matDeal.setDeliveryNo(pln.getDeliveryNo());
				matDeal.setOrdPk(pln.getOrdLnDlyPk().substring(0,
						pln.getOrdLnDlyPk().length() - 6));
				matDeal.setOrdLnPk(pln.getOrdLnDlyPk().substring(0,
						pln.getOrdLnDlyPk().length() - 3));
				matDeal.setOrdLnDlyPk(pln.getOrdLnDlyPk());
				matDeal.setCraftFileNoOrd(pln.getCraftFileNo());
				matDeal.setCraftSeqOrd(pln.getCraftSeq());
				matDeal.setGowhereName(gowhereName);
				String sqlAddDeal = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.AddDealResult"), matDeal);// 新增交易信息
				ps = connection.prepareStatement(sqlAddDeal);
				ps.executeUpdate();
				ps.close();
				// 获取交易单号
				dealOrdeNew = doAddDealOrde("CQ", "800809", connection);
				// 新增交易表
				matDeal.setDealOrde(dealOrdeNew);
				matDeal.setDealTypeCode("800809");
				matDeal.setDealTypeName("产权出库");
				sqlAddDeal = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.AddDealResult"), matDeal);// 新增交易信息
				ps = connection.prepareStatement(sqlAddDeal);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("拆分成功！");
			connection.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			cro.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
