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.PlnZyJgMiddleEntity;

/**
 * 接箍盘库出库
 * 
 * @author siy 2015/05/28
 * @version v1.0
 */
public class FrmCollarOutventory extends DBCommon {

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

	/**
	 * 查询盘库出库实绩
	 * 
	 * @param list
	 *            传递参数
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject queryOutPerfo(ArrayList<String> list, String[] arr,
			String orderNo, String codeJg, String strFurnace) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmCollarOutventory.queryOutPerfo");
		if (!strFurnace.equals("")) {
			sql += " AND judge_stove_no LIKE '%' || '" + strFurnace
					+ "' || '%'";
		}
		if (!orderNo.equals("")) {
			sql += " AND ORDER_NO || '/' || ORDER_SEQ like '%' || '" + orderNo
					+ "' || '%'";
		}
		if (!codeJg.equals("")) {
			sql += " AND CODE_JG='" + codeJg + "'";
		}
		sql += " AND DECODE('" + list.get(0)
				+ "', NULL, '1', create_time) BETWEEN DECODE('" + list.get(1)
				+ "', NULL, '1', to_date('" + list.get(2)
				+ "', 'YYYY-MM-DD HH24:MI:SS'))";
		sql += " AND DECODE('" + list.get(3) + "', NULL, '1', to_date('"
				+ list.get(4) + "', 'YYYY-MM-DD HH24:MI:SS')) ";
		sql += " AND DEAL_TYPE_CODE ='800802'";
		sql += " ORDER BY CREATE_TIME desc";
		return this.getData(sql);
	}

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

	/**
	 * 新增
	 * 
	 * @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 matNo = list.get(0);
				// String relutNo = list.get(1);
				// String planNo = list.get(2);
				// String storage = list.get(3);
				// String kenNo = list.get(4);
				String memo = list.get(5);
				// String bc = list.get(6);
				// String bz = list.get(7);
				String name = list.get(8);
				int count = Integer.parseInt(list.get(9));
				double weghit = Double.parseDouble(list.get(10));
				// String strOutNo = list.get(11);
				// String strOutname = list.get(12);
				String matNoXh = list.get(14);
				String guid = list.get(15);

				PlnZyJgMiddleEntity midd = null;
				String jgOrderNo = "";
				String orderNo = "";
				String orderSeq = "";
				String orderSeqNo = "";
				String ordPk = "";
				String ordLnPk = "";
				String ordLnDPk = "";
				String cpFileNo = "";
				String cpCraftNo = "";
				if (!guid.equals("")) {
					midd = getPlnMiddOrder(connection, guid);
					if (midd == null) {
						cro.setV_errMsg("未找库存对应合同信息,请确认！");
						return cro;
					}
					jgOrderNo = midd.getJgOrderno();
					orderNo = midd.getOrderNo();
					orderSeq = midd.getOrderSeq();
					orderSeqNo = midd.getOrderSeqNo();
					ordPk = midd.getOrdPk();
					ordLnPk = midd.getOrdLnPk();
					ordLnDPk = midd.getOrdLnDlyPk();
					cpFileNo = midd.getCraftFileNoOrd();
					cpCraftNo = midd.getCraftSeqOrd();
				}
				// 获取材料实体了
				CouplingMatMEntity pln = getSendPlanCInfo(connection, matNoXh,
						matNo);
				if (pln == null) {
					cro.setV_errMsg("未找到库存信息,请确认！");
					return cro;
				}
				CouplingMatDealEntity matDeal = new CouplingMatDealEntity();
				// 获取交易单号
				String dealOrde = FrmCollarOutBound.doAddDealOrde("OB",
						"800802", connection);
				// 新增交易表
				matDeal.setDealOrde(dealOrde);
				matDeal.setDealTypeCode("800802");
				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(String.valueOf(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(memo);
				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("0");
				matDeal.setJgOrderno(jgOrderNo);
				matDeal.setOrderNo(orderNo);
				matDeal.setOrderSeq(orderSeq);
				matDeal.setDeliveryNo(orderSeqNo);
				matDeal.setOrdPk(ordPk);
				matDeal.setOrdLnPk(ordLnPk);
				matDeal.setOrdLnDlyPk(ordLnDPk);
				matDeal.setCraftFileNoOrd(cpFileNo);
				matDeal.setCraftSeqOrd(cpCraftNo);
				matDeal.setStoveNo(pln.getStoveNo());
				matDeal.setFlagStoveNo(pln.getFlagStoveNo());
				String sqlAddDeal = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.AddDealResult"), matDeal);// 新增交易信息
				ps = connection.prepareStatement(sqlAddDeal);
				ps.executeUpdate();
				ps.close();

				if (!guid.equals("")) {
					if (Integer.parseInt(midd.getPlanNum()) - count <= 0) {
						// 删除接箍工单详情
						String deleteMidd = "DELETE PLN_ZY_JG_MIDDLE  WHERE GUID = ? ";
						ps = connection.prepareStatement(deleteMidd);
						ps.setObject(1, guid);
						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, count);
						ps.setObject(2, weghit);
						ps.setObject(3, guid);
						ps.executeUpdate();
						ps.close();
					}
				}

				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();
				}
				int proNumAll = Integer.parseInt(pln.getProNumAll()) - count;// 库存剩余个数
				int pieces = Integer.parseInt(pln.getProNum()) - count;// 库存位置剩余个数
				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();
				}
			}
			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 planNo = pram.get(1);
				String matNo = pram.get(2);
				// String kenNo = pram.get(3);
				// String storage = pram.get(4);
				// String count = pram.get(5);
				// String weghit = pram.get(6);
				String name = pram.get(7);
				// String bc = pram.get(8);
				// String bz = pram.get(9);
				String dealNo = pram.get(10);
				// String matNoXh = pram.get(11);
				String jgOrderno = pram.get(12);
				// 查询交易表信息
				CouplingMatDealEntity pln = FrmOutManagJg.getSendMatDeal(
						connection, dealNo);
				if (pln == null) {
					cro.setV_errMsg("未找到交易信息,请确认！");
					return cro;
				}
				// int deaNum = Integer.parseInt(pln.getDealNum());//出库个数
				// 删除交易表
				String deleteMatDeal = "DELETE COUPLING_MAT_DEAL  WHERE DEAL_ORDE = ? ";
				ps = connection.prepareStatement(deleteMatDeal);
				ps.setObject(1, dealNo);
				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 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 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 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 (Integer.parseInt(pln.getDealNum()) - 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, pln.getDealNum());
					ps.setObject(2, pln.getDealWt());
					ps.setObject(3, pln.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
				if (!jgOrderno.equals("")) {
					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();
					if (guid.equals("")) {
						// 新增接箍工单详细表
						String addPlnMidd = CoreXmlSqlParsersFactory
								.getSql("FrmOutManagJg.AddPlnZyJgMidd");
						ps = connection.prepareStatement(addPlnMidd);
						ps.setObject(1, jgOrderno);
						ps.setObject(2, pln.getDealNum());
						ps.setObject(3, pln.getDealWt());
						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.getCraftSeqOrd());
						ps.setObject(13, pln.getCraftFileNoOrd());
						ps.setObject(14, pln.getMatNoXh());
						ps.setObject(15, matNo);
						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, 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;
	}
}
