package com.steering.mes.mcp.coup;

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.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.EntityTool;
import com.steering.comm.converter.QualityDesignTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.PlnOrderJgEntity;
import com.steering.mes.mcp.entity.PlnZyJgEntity;
import com.steering.mes.mcp.entity.MchEntity.CouplingComInfoEntity;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgWorkbillEntity;

/**
 * 接箍交库实绩
 * 
 * @author siy 2015/05/28
 * @version v1.0
 */
public class FrmInLibrary extends DBCommon {
	public CoreReturnObject doQuery() {
		String sql = CoreXmlSqlParsersFactory.getSql("FrmInLibrary.Query");
		return this.getData(sql);
	}

	/***
	 * 查询作业工单计划
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryZygNo(ArrayList<String> list, String plinCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.doQueryZygNo");

		return this.getData(sql, list.toArray());
	}

	// /***
	// * 查询接箍合同数据
	// *
	// * @return
	// */
	// public CoreReturnObject QueryPlnOrderJg(String ) {
	// String sql = CoreXmlSqlParsersFactory
	// .getSql("FrmInLibrary.doQueryZygNo");
	//
	// return this.getData(sql, list.toArray());
	// }

	/***
	 * 查询接箍材料信息
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryMatNo(String woid, String plineCode) {
		Object[] pamar = new Object[] { plineCode, woid };
		return this.getDataBySqlID("FrmInLibrary.doQueryMatNo", pamar);
	}

	/**
	 * 查询仓库
	 * 
	 * @param list
	 *            传递参数
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject queryStorage() {
		return this.getDataBySqlID("FrmInLibrary.queryStorage");
	}

	/**
	 * 查询交库实绩
	 * 
	 * @param planNO
	 * @return
	 */
	public CoreReturnObject doQueryInfoResult(String planNO, String woId) {
		Object[] pamar = new Object[] { planNO, woId };
		return this.getDataBySqlID("FrmInLibrary.doQueryInfoResult", pamar);
	}

	/**
	 * 新增交库实绩
	 * 
	 * @param result
	 *            交库实绩
	 * @return·
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject addLibraryResult(ArrayList<Object> list1,
			String cutGx, String woId) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		String systime = dateFormat.format(now);
		String last = date.format(ca.getTime()) + " 21:00:00";
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String insertLinfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.AddLibraryResult");// 新增交库实绩表
			PreparedStatement insertLinPsta = con
					.prepareStatement(insertLinfoSql);
			String strMatM = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.AddMatM");// 新增接箍材料表
			PreparedStatement pstaMatM = con.prepareStatement(strMatM);
			String strMatButters = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.AddMatButters");// 新增位置表
			PreparedStatement pstaMatButters = con
					.prepareStatement(strMatButters);
			String strMatDeal = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.AddMatDeal");// 新增交易表
			PreparedStatement pstaMatDeal = con.prepareStatement(strMatDeal);
			PreparedStatement psta = null;
			ResultSet rs = null;
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(con,
					woId, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				cor.setV_errMsg("未查询到工单信息,请确认!");
				return cor;
			}
			if (!work.getZyBillStatus().equals("2")) {
				cor.setV_errMsg("工单状态不是生产中,不允许此操作!");
				return cor;
			}
			for (int i = 0; i < list1.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) list1.get(i);
				String plnNo = list.get(0);
				String pline = list.get(1);
				int sjNum = Integer.parseInt(list.get(3));
				String failNum = list.get(4);
				String user = list.get(5);
				String bc = list.get(6);
				String bz = list.get(7);
				String spcount = list.get(8);
				String lutime = systime;
				if (PlanService.compareDate(systime, last) == true) {
					if (bc.equals("3")) {
						ca.add(Calendar.DAY_OF_MONTH, 1);
						lutime = date.format(ca.getTime()) + " 00:00:01";
					}
				}

				String status = "";
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.queryComPlanJg");
				psta = con.prepareStatement(sqlloading);
				psta.setObject(1, woId);
				rs = psta.executeQuery();
				while (rs.next()) {
					status = rs.getString("STATION_STATUS");
					if (status.equals("1")) {
						cor.setV_errMsg("该工单" + rs.getString("STATION_DESC")
								+ "未结炉！");
						return cor;
					}
				}
				rs.close();
				psta.close();

				String storageNo = "TCP1";
				String storageAtte = "";
				String storageAtteName = "";
				String storageType = "";
				String storageTypeName = "";
				String managNo = "";
				String managName = "";
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.queryStorageType");
				psta = con.prepareStatement(sqlloading);
				psta.setObject(1, storageNo);
				rs = psta.executeQuery();
				if (rs.next()) {
					storageAtte = rs.getString("STORAGE_ATTR");
					storageAtteName = rs.getString("STORAGE_ATTR_NAME");
					storageType = rs.getString("STORAGE_TYPE_NO");
					storageTypeName = rs.getString("STORAGE_TYPE_NAME");
					managNo = rs.getString("MANAGEMENT_NO");
					managName = rs.getString("MANAGEMENT_NAME");
				}
				rs.close();
				psta.close();
				String belongCode = "";
				String belongName = "";
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.selectBelong");
				psta = con.prepareStatement(sqlloading);
				psta.setObject(1, pline);
				rs = psta.executeQuery();
				if (rs.next()) {
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				psta.close();

				// 获取交库实绩编号
				String resultNo = doAddResultNo(pline, woId, con);

				// 新增交库信息
				insertLinPsta.setObject(1, resultNo);
				insertLinPsta.setObject(2, work.getPlineCode());
				insertLinPsta.setObject(3, plnNo);
				insertLinPsta.setObject(4, woId);
				insertLinPsta.setObject(5, spcount);
				insertLinPsta.setObject(6, failNum);
				insertLinPsta.setObject(7, "");
				insertLinPsta.setObject(8, user);
				insertLinPsta.setObject(9, bc);
				insertLinPsta.setObject(10, bz);
				insertLinPsta.setObject(11, "");
				insertLinPsta.setObject(12, systime);
				insertLinPsta.setObject(13, work.getPlineName());
				insertLinPsta.addBatch();
				insertLinPsta.executeBatch();
				insertLinPsta.close();

				CouplingComInfoEntity comInfo = new CouplingComInfoEntity();
				// 新增接箍公共信息
				comInfo.setResultNo(resultNo);
				comInfo.setWoId(work.getWoId());
				comInfo.setJudgeStoveNo(work.getJudgeStoveNo());
				comInfo.setProPlanId("");
				comInfo.setCodeJgBl(work.getCodeJgBl());
				comInfo.setIfOutRpo("0");
				comInfo.setPlineCode(work.getPlineCode());
				comInfo.setPlineName(work.getPlineName());
				comInfo.setStationCode(cutGx);
				comInfo.setStationDesc("交库");
				comInfo.setBatchNo(work.getBatchNo());
				comInfo.setBlWlId(work.getBlWlId());
				comInfo.setBlWlDesc(work.getBlWlDesc());
				comInfo.setMinD(work.getMinD());
				comInfo.setMinH(work.getMinH());
				comInfo.setAimlengthBl(work.getAimlengthBl());
				comInfo.setAimCutLen(work.getAimCutLen());
				comInfo.setLengthMax(work.getLengthMax());
				comInfo.setLengthMin(work.getLengthMin());
				comInfo.setTaper(work.getTaper());
				comInfo.setCodeJg(work.getCodeJg());
				comInfo.setGradecode(work.getGradecode());
				comInfo.setGradename(work.getGradename());
				comInfo.setSteelcode(work.getSteelcode());
				comInfo.setSteelname(work.getSteelname());
				comInfo.setModelCode(work.getModelCode());
				comInfo.setModelDesc(work.getModelDesc());
				comInfo.setOutdiameter(work.getDiameterJg());
				comInfo.setWallthick(work.getHeightJg());
				comInfo.setSpecJg(work.getSpecJg());
				comInfo.setSpecJgDesc(work.getSpecJgDesc());
				comInfo.setProduccodeJg(work.getProduccodeJg());
				comInfo.setProduccodeJgDesc(work.getProduccodeJgDesc());
				comInfo.setStdStyle(work.getStdStyle());
				comInfo.setStdStyleDesc(work.getStdStyleDesc());
				comInfo.setStdCode(work.getStdCode());
				comInfo.setStdName(work.getStdName());
				comInfo.setStyleJg(work.getStyleJg());
				comInfo.setDiameter(work.getDiameter());
				comInfo.setCheckJVal(work.getCheckJVal());
				comInfo.setAimlength(work.getLengthJg());
				comInfo.setSingleweight(work.getSingleweight());
				comInfo.setIfMonitorJg(work.getIfMonitorJg());
				comInfo.setPrdcrNo(work.getPrdcrNo());
				comInfo.setMachNo("");
				comInfo.setMachNm("");
				comInfo.setProNum(spcount);
				comInfo.setProWt(String.valueOf(Integer.parseInt(spcount)
						* Double.parseDouble(work.getSingleweight()) / 1000));
				comInfo.setFailNum(failNum);
				comInfo.setFailWt(String.valueOf(Integer.parseInt(failNum)
						* Double.parseDouble(work.getSingleweight()) / 1000));
				comInfo.setRroTime(lutime);
				comInfo.setProUser(user);
				comInfo.setProShift(bc);
				comInfo.setProGroup(bz);
				comInfo.setIfAllOutWork(work.getIfAllOutWork());
				comInfo.setCreateTime(systime);
				comInfo.setColOperator("");
				comInfo.setOutwlId(work.getOutwlId());
				comInfo.setOutwlDesc(work.getOutwlDesc());
				comInfo.setStationStatus("1");
				// 新增接箍公共记录
				String addInfo = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("CoupLoadingService.AddPlnInfo"),
						comInfo);
				ps = con.prepareStatement(addInfo);
				ps.executeUpdate();
				ps.close();

				// 获取交易单号
				String dealOrde = doAddDealOrde(woId, con);
				// 获取材料号
				String matNo = doAddMatNo(work.getJudgeStoveNo(), resultNo,
						woId, con);

				// int jgCount = 0;
				// // 查询接箍信息数量
				// String sqlload = CoreXmlSqlParsersFactory
				// .getSql("FrmInLibrary.doQueryMatNoCount");
				// psta = con.prepareStatement(sqlload);
				// psta.setObject(1, list.get(1));
				// psta.setObject(2, woId);
				// psta.setObject(3, "00");
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// jgCount = Integer.parseInt(rs.getString("COUNT"));
				// }
				// rs.close();
				// psta.close();
				//
				// if (jgCount == 0) {
				// 新增材料表
				pstaMatM.setObject(1, matNo);
				pstaMatM.setObject(2, resultNo);
				pstaMatM.setObject(3, plnNo);
				pstaMatM.setObject(4, woId);
				pstaMatM.setObject(5, work.getPlineCode());
				pstaMatM.setObject(6, work.getPlineName());
				pstaMatM.setObject(7, work.getJudgeStoveNo());
				pstaMatM.setObject(8, work.getBatchNo());
				pstaMatM.setObject(9, work.getOutwlId());
				pstaMatM.setObject(10, work.getOutwlDesc());
				pstaMatM.setObject(11, work.getMinD());
				pstaMatM.setObject(12, work.getMinH());
				pstaMatM.setObject(13, work.getCodeJg());
				pstaMatM.setObject(14, work.getGradecode());
				pstaMatM.setObject(15, work.getGradename());
				pstaMatM.setObject(16, work.getSteelcode());
				pstaMatM.setObject(17, work.getSteelname());
				pstaMatM.setObject(18, work.getModelCode());
				pstaMatM.setObject(19, work.getModelDesc());
				pstaMatM.setObject(20, work.getDiameterJg());
				pstaMatM.setObject(21, work.getHeightJg());
				pstaMatM.setObject(22, work.getSpecJg());
				pstaMatM.setObject(23, work.getSpecJgDesc());
				pstaMatM.setObject(24, work.getStyleJg());
				pstaMatM.setObject(25, work.getDiameter());
				pstaMatM.setObject(26, work.getCheckJVal());
				pstaMatM.setObject(27, work.getLengthJg());
				pstaMatM.setObject(28, work.getSingleweight());
				pstaMatM.setObject(29, "");
				pstaMatM.setObject(30, work.getIfMonitorJg());
				pstaMatM.setObject(31, work.getPrdcrNo());
				pstaMatM.setObject(32, "");
				pstaMatM.setObject(33, String.valueOf(sjNum));
				if (work.getSingleweight().equals("")) {
					pstaMatM.setObject(34, 0);
				} else {
					pstaMatM.setObject(34,
							sjNum * Double.parseDouble(work.getSingleweight())
									/ 1000);// 重量
				}
				pstaMatM.setObject(35, lutime);
				pstaMatM.setObject(36, bc);
				pstaMatM.setObject(37, bz);
				pstaMatM.setObject(38, "");// 操作人
				pstaMatM.setObject(39, user);
				pstaMatM.setObject(40, systime);
				pstaMatM.setObject(41, systime);
				pstaMatM.setObject(42,
						work.getOrderNo() + "/" + work.getOrderSeq());
				pstaMatM.setObject(43, belongCode);
				pstaMatM.setObject(44, belongName);
				pstaMatM.setObject(45, "00");
				pstaMatM.addBatch();
				pstaMatM.executeBatch();
				// pstaMatM.close();
				// 新增材料从表
				pstaMatButters.setObject(1, matNo);
				pstaMatButters.setObject(2, "0001");
				pstaMatButters.setObject(3, "801402");
				pstaMatButters.setObject(4, "80150201");
				pstaMatButters.setObject(5, "");
				pstaMatButters.setObject(6, sjNum);
				if (work.getSingleweight().equals("")) {
					pstaMatButters.setObject(7, 0);
				} else {
					pstaMatButters.setObject(7,
							sjNum * Double.parseDouble(work.getSingleweight())
									/ 1000);
				}
				pstaMatButters.setObject(8, "");
				pstaMatButters.setObject(9, "");
				pstaMatButters.setObject(10, "");
				pstaMatButters.setObject(11, storageNo);
				pstaMatButters.setObject(12, storageNo + "10101");
				pstaMatButters.setObject(13, storageAtte);
				pstaMatButters.setObject(14, storageAtteName);
				pstaMatButters.setObject(15, storageType);
				pstaMatButters.setObject(16, storageTypeName);
				pstaMatButters.setObject(17, managNo);
				pstaMatButters.setObject(18, managName);
				pstaMatButters.setObject(19, "");
				pstaMatButters.setObject(20, user);
				pstaMatButters.setObject(21, systime);
				pstaMatButters.setObject(22, "0");
				pstaMatButters.setObject(23, "");
				pstaMatButters.setObject(24, dealOrde);
				pstaMatButters.setObject(25, work.getLatheGrpNo());
				pstaMatButters.setObject(26, work.getLatheGrpNm());
				pstaMatButters.setObject(27, "");
				pstaMatButters.addBatch();
				pstaMatButters.executeBatch();
				// pstaMatButters.close();

				// 新增交易表
				pstaMatDeal.setObject(1, managNo);// 作业科室编码
				pstaMatDeal.setObject(2, managName);
				pstaMatDeal.setObject(3, "");
				pstaMatDeal.setObject(4, matNo);
				pstaMatDeal.setObject(5, resultNo);
				pstaMatDeal.setObject(6, plnNo);
				pstaMatDeal.setObject(7, work.getMinD());
				pstaMatDeal.setObject(8, work.getMinH());
				pstaMatDeal.setObject(9, work.getDiameterJg());
				pstaMatDeal.setObject(10, work.getHeightJg());
				pstaMatDeal.setObject(11, work.getDiameter());
				pstaMatDeal.setObject(12, work.getCheckJVal());
				pstaMatDeal.setObject(13, work.getLengthJg());
				pstaMatDeal.setObject(14, "");// 监制人
				pstaMatDeal.setObject(15, "");// 监制时间
				pstaMatDeal.setObject(16, sjNum);
				pstaMatDeal.setObject(17,
						sjNum * Double.parseDouble(work.getSingleweight())
								/ 1000);// 重量
				pstaMatDeal.setObject(18, lutime);
				pstaMatDeal.setObject(19, storageNo);
				pstaMatDeal.setObject(20, storageNo + "10101");
				pstaMatDeal.setObject(21, storageAtte);
				pstaMatDeal.setObject(22, storageAtteName);
				pstaMatDeal.setObject(23, storageType);
				pstaMatDeal.setObject(24, storageTypeName);
				pstaMatDeal.setObject(25, "");
				pstaMatDeal.setObject(26, user);
				pstaMatDeal.setObject(27, systime);
				pstaMatDeal.setObject(28, "");
				pstaMatDeal.setObject(29, dealOrde);
				pstaMatDeal.setObject(30, systime);
				pstaMatDeal.setObject(31, "00");
				pstaMatDeal.setObject(32, woId);
				pstaMatDeal.addBatch();
				pstaMatDeal.executeBatch();
				// pstaMatDeal.close();
				// 新增废品材料信息
				if (Integer.parseInt(failNum) > 0) {
					// 获取交易单号
					dealOrde = doAddDealOrde(woId, con);
					// 获取材料号
					matNo = doAddMatNo(work.getJudgeStoveNo(), resultNo, woId,
							con);
					// 新增材料表
					pstaMatM.setObject(1, matNo);
					pstaMatM.setObject(2, resultNo);
					pstaMatM.setObject(3, plnNo);
					pstaMatM.setObject(4, woId);
					pstaMatM.setObject(5, work.getPlineCode());
					pstaMatM.setObject(6, work.getPlineName());
					pstaMatM.setObject(7, work.getJudgeStoveNo());
					pstaMatM.setObject(8, work.getBatchNo());
					pstaMatM.setObject(9, work.getBlWlId());
					pstaMatM.setObject(10, work.getBlWlDesc());
					pstaMatM.setObject(11, work.getMinD());
					pstaMatM.setObject(12, work.getMinH());
					pstaMatM.setObject(13, work.getCodeJg());
					pstaMatM.setObject(14, work.getGradecode());
					pstaMatM.setObject(15, work.getGradename());
					pstaMatM.setObject(16, work.getSteelcode());
					pstaMatM.setObject(17, work.getSteelname());
					pstaMatM.setObject(18, work.getModelCode());
					pstaMatM.setObject(19, work.getModelDesc());
					pstaMatM.setObject(20, work.getDiameterJg());
					pstaMatM.setObject(21, work.getHeightJg());
					pstaMatM.setObject(22, work.getSpecJg());
					pstaMatM.setObject(23, work.getSpecJgDesc());
					pstaMatM.setObject(24, work.getStyleJg());
					pstaMatM.setObject(25, work.getDiameter());
					pstaMatM.setObject(26, work.getCheckJVal());
					pstaMatM.setObject(27, work.getLengthJg());
					pstaMatM.setObject(28, work.getSingleweight());
					pstaMatM.setObject(29, "");
					pstaMatM.setObject(30, work.getIfMonitorJg());
					pstaMatM.setObject(31, work.getPrdcrNo());
					pstaMatM.setObject(32, "");
					pstaMatM.setObject(33, String.valueOf(failNum));
					if (work.getSingleweight().equals("")) {
						pstaMatM.setObject(34, 0);
					} else {
						pstaMatM.setObject(34, Double.parseDouble(failNum)
								* Double.parseDouble(work.getSingleweight())
								/ 1000);// 重量
					}
					pstaMatM.setObject(35, lutime);
					pstaMatM.setObject(36, bc);
					pstaMatM.setObject(37, bz);
					pstaMatM.setObject(38, "");// 操作人
					pstaMatM.setObject(39, user);
					pstaMatM.setObject(40, systime);
					pstaMatM.setObject(41, systime);
					pstaMatM.setObject(42,
							work.getOrderNo() + "/" + work.getOrderSeq());
					pstaMatM.setObject(43, belongCode);
					pstaMatM.setObject(44, belongName);
					pstaMatM.setObject(45, "10");
					pstaMatM.addBatch();
					pstaMatM.executeBatch();
					pstaMatM.close();
					// 新增材料从表
					pstaMatButters.setObject(1, matNo);
					pstaMatButters.setObject(2, "0001");
					pstaMatButters.setObject(3, "801402");
					pstaMatButters.setObject(4, "80150201");
					pstaMatButters.setObject(5, "");
					pstaMatButters.setObject(6, failNum);
					if (work.getSingleweight().equals("")) {
						pstaMatButters.setObject(7, 0);
					} else {
						pstaMatButters.setObject(7, Double.parseDouble(failNum)
								* Double.parseDouble(work.getSingleweight())
								/ 1000);
					}
					pstaMatButters.setObject(8, "");
					pstaMatButters.setObject(9, "");
					pstaMatButters.setObject(10, "");
					pstaMatButters.setObject(11, storageNo);
					pstaMatButters.setObject(12, storageNo + "10101");
					pstaMatButters.setObject(13, storageAtte);
					pstaMatButters.setObject(14, storageAtteName);
					pstaMatButters.setObject(15, storageType);
					pstaMatButters.setObject(16, storageTypeName);
					pstaMatButters.setObject(17, managNo);
					pstaMatButters.setObject(18, managName);
					pstaMatButters.setObject(19, "");
					pstaMatButters.setObject(20, user);
					pstaMatButters.setObject(21, systime);
					pstaMatButters.setObject(22, "0");
					pstaMatButters.setObject(23, "");
					pstaMatButters.setObject(24, dealOrde);
					pstaMatButters.setObject(25, work.getLatheGrpNo());
					pstaMatButters.setObject(26, work.getLatheGrpNm());
					pstaMatButters.setObject(27, "");
					pstaMatButters.addBatch();
					pstaMatButters.executeBatch();
					pstaMatButters.close();

					// 新增交易表
					pstaMatDeal.setObject(1, managNo);// 作业科室编码
					pstaMatDeal.setObject(2, managName);
					pstaMatDeal.setObject(3, "");
					pstaMatDeal.setObject(4, matNo);
					pstaMatDeal.setObject(5, resultNo);
					pstaMatDeal.setObject(6, plnNo);
					pstaMatDeal.setObject(7, work.getMinD());
					pstaMatDeal.setObject(8, work.getMinH());
					pstaMatDeal.setObject(9, work.getDiameterJg());
					pstaMatDeal.setObject(10, work.getHeightJg());
					pstaMatDeal.setObject(11, work.getDiameter());
					pstaMatDeal.setObject(12, work.getCheckJVal());
					pstaMatDeal.setObject(13, work.getLengthJg());
					pstaMatDeal.setObject(14, "");// 监制人
					pstaMatDeal.setObject(15, "");// 监制时间
					pstaMatDeal.setObject(16, failNum);
					pstaMatDeal
							.setObject(
									17,
									Integer.parseInt(failNum)
											* Double.parseDouble(work
													.getSingleweight()) / 1000);// 重量
					pstaMatDeal.setObject(18, lutime);
					pstaMatDeal.setObject(19, storageNo);
					pstaMatDeal.setObject(20, storageNo + "10101");
					pstaMatDeal.setObject(21, storageAtte);
					pstaMatDeal.setObject(22, storageAtteName);
					pstaMatDeal.setObject(23, storageType);
					pstaMatDeal.setObject(24, storageTypeName);
					pstaMatDeal.setObject(25, "");
					pstaMatDeal.setObject(26, user);
					pstaMatDeal.setObject(27, systime);
					pstaMatDeal.setObject(28, "");
					pstaMatDeal.setObject(29, dealOrde);
					pstaMatDeal.setObject(30, systime);
					pstaMatDeal.setObject(31, "10");
					pstaMatDeal.setObject(32, woId);
					pstaMatDeal.addBatch();
					pstaMatDeal.executeBatch();
					pstaMatDeal.close();
				}

				// } else {
				// String deOrde = "";
				// String sqlloadlib = CoreXmlSqlParsersFactory
				// .getSql("FrmInLibrary.doQueryMatNo");// 查询交库信息里的数据
				// psta = con.prepareStatement(sqlloadlib);
				// psta.setObject(1, pline);
				// psta.setObject(2, woId);
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// deOrde = rs.getString("DEAL_ORDE");
				// }
				// rs.close();
				// psta.close();
				//
				// String strMat = CoreXmlSqlParsersFactory
				// .getSql("FrmInLibrary.UpdateMatStuts");// 修改位置表支数
				// PreparedStatement pstaMat = con.prepareStatement(strMat);
				// pstaMat.setObject(1, String.valueOf(sjNum));
				// if (work.getSingleweight().equals("")) {
				// pstaMat.setObject(2, 0);
				// } else {
				// pstaMat.setObject(
				// 2,
				// Double.parseDouble(sjNum)
				// * Double.parseDouble(work
				// .getSingleweight()) / 1000);// 重量
				// }
				// pstaMat.setObject(3, deOrde);
				// pstaMat.addBatch();
				// pstaMat.executeBatch();
				// pstaMat.close();
				//
				// String strMatBut = CoreXmlSqlParsersFactory
				// .getSql("FrmInLibrary.UpdateMatStutsBut");// 修改材料表支数
				// PreparedStatement pstaMatBut =
				// con.prepareStatement(strMatBut);
				// pstaMatBut.setObject(1, String.valueOf(sjNum));
				// if (work.getSingleweight().equals("")) {
				// pstaMatBut.setObject(2, 0);
				// } else {
				// pstaMatBut.setObject(
				// 2,
				// Double.parseDouble(sjNum)
				// * Double.parseDouble(work
				// .getSingleweight()) / 1000);// 重量
				// }
				// pstaMatBut.setObject(3, deOrde);
				// pstaMatBut.addBatch();
				// pstaMatBut.executeBatch();
				// pstaMatBut.close();
				//
				//
				// String strDeal = CoreXmlSqlParsersFactory
				// .getSql("FrmInLibrary.UpdateDeal");// 修改交易表支數
				// PreparedStatement pstaDeal = con.prepareStatement(strDeal);
				// pstaDeal.setObject(1, String.valueOf(sjNum));
				// if (work.getSingleweight().equals("")) {
				// pstaDeal.setObject(2, 0);
				// } else {
				// pstaDeal.setObject(2, Double.parseDouble(sjNum)
				// * Double.parseDouble(work.getSingleweight())
				// / 1000);// 重量
				// }
				// pstaDeal.setObject(3, deOrde);
				// pstaDeal.addBatch();
				// pstaDeal.executeBatch();
				// pstaDeal.close();
				// }
				String strPln = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.UpdateJGPln");// 修改计划表支数
				PreparedStatement pstaPln = con.prepareStatement(strPln);
				pstaPln.setObject(1, String.valueOf(sjNum));
				if (work.getSingleweight().equals("")) {
					pstaPln.setObject(2, 0);
				} else {
					pstaPln.setObject(2,
							sjNum * Double.parseDouble(work.getSingleweight())
									/ 1000);// 重量
				}
				pstaPln.setObject(3, woId);
				pstaPln.addBatch();
				pstaPln.executeBatch();

				// 查询工单对应接箍成品合同
				List<PlnOrderJgEntity> orderJg = getQureyCpOrder(con, woId);
				if (orderJg == null) {
					cor.setV_errMsg("未找到接箍对应成品合同!");
					return cor;
				}
				// 对应只有一个成品合同
				int num = 0;
				int orderNum = 0;
				for (PlnOrderJgEntity order : orderJg) {
					if (orderJg.size() == 1) {
						// 新增接箍工单详细表
						String addPlnMidd = CoreXmlSqlParsersFactory
								.getSql("FrmInLibrary.AddPlnZyJgMidd");
						ps = con.prepareStatement(addPlnMidd);
						ps.setObject(1, "");
						ps.setObject(2, sjNum);
						ps.setObject(
								3,
								sjNum
										* Double.parseDouble(work
												.getSingleweight()));
						ps.setObject(4, "");
						ps.setObject(5, user);
						ps.setObject(6, order.getOrderNo());
						ps.setObject(7, order.getOrderSeq());
						ps.setObject(8, order.getDeliveryNo());
						ps.setObject(
								9,
								order.getOrdLnDlyPk().substring(0,
										order.getOrdLnDlyPk().length() - 6));
						ps.setObject(
								10,
								order.getOrdLnDlyPk().substring(0,
										order.getOrdLnDlyPk().length() - 3));
						ps.setObject(11, order.getOrdLnDlyPk());
						ps.setObject(12, woId);
						ps.executeUpdate();
						ps.close();
						// 修改接箍合同已挂个数
						String updateOrderJ = "UPDATE PLN_ORDER_JG T SET T.HANGKC_NUM=TO_NUMBER(T.HANGKC_NUM)+TO_NUMBER(?) WHERE T.JG_ORDERNO=?";
						ps = con.prepareStatement(updateOrderJ);
						ps.setObject(1, sjNum);
						ps.setObject(
								2,
								sjNum
										* Double.parseDouble(work
												.getSingleweight()));
						ps.setObject(3, work.getJgOrdernoAll());
						ps.executeUpdate();
						ps.close();
					} else {
						orderNum = (int) (Integer.parseInt(order.getOrdernum()) * (1 + 0.03));
						num = orderNum - Integer.parseInt(order.getHangkcNum());
						if (num <= 0) {
							continue;
						}
						if (sjNum <= 0) {
							continue;
						}
						if (sjNum - orderNum < 0) {
							// 新增接箍工单详细表
							String addPlnMidd = CoreXmlSqlParsersFactory
									.getSql("FrmInLibrary.AddPlnZyJgMidd");
							ps = con.prepareStatement(addPlnMidd);
							ps.setObject(1, "");
							ps.setObject(2, sjNum);
							ps.setObject(
									3,
									sjNum
											* Double.parseDouble(work
													.getSingleweight()));
							ps.setObject(4, "");
							ps.setObject(5, user);
							ps.setObject(6, order.getOrderNo());
							ps.setObject(7, order.getOrderSeq());
							ps.setObject(8, order.getDeliveryNo());
							ps.setObject(
									9,
									order.getOrdLnDlyPk().substring(0,
											order.getOrdLnDlyPk().length() - 6));
							ps.setObject(
									10,
									order.getOrdLnDlyPk().substring(0,
											order.getOrdLnDlyPk().length() - 3));
							ps.setObject(11, order.getOrdLnDlyPk());
							ps.setObject(12, woId);
							ps.executeUpdate();
							ps.close();

							// 修改接箍合同已挂个数
							String updateOrderJ = "UPDATE PLN_ORDER_JG T SET T.HANGKC_NUM=TO_NUMBER(T.HANGKC_NUM)+TO_NUMBER(?) WHERE T.JG_ORDERNO=?";
							ps = con.prepareStatement(updateOrderJ);
							ps.setObject(1, sjNum);
							ps.setObject(
									2,
									sjNum
											* Double.parseDouble(work
													.getSingleweight()));
							ps.setObject(3, work.getJgOrdernoAll());
							ps.executeUpdate();
							ps.close();
							break;
						} else {
							// 新增接箍工单详细表
							String addPlnMidd = CoreXmlSqlParsersFactory
									.getSql("FrmInLibrary.AddPlnZyJgMidd");
							ps = con.prepareStatement(addPlnMidd);
							ps.setObject(1, "");
							ps.setObject(2, orderNum);
							ps.setObject(
									3,
									orderNum
											* Double.parseDouble(work
													.getSingleweight()));
							ps.setObject(4, "");
							ps.setObject(5, user);
							ps.setObject(6, order.getOrderNo());
							ps.setObject(7, order.getOrderSeq());
							ps.setObject(8, order.getDeliveryNo());
							ps.setObject(
									9,
									order.getOrdLnDlyPk().substring(0,
											order.getOrdLnDlyPk().length() - 6));
							ps.setObject(
									10,
									order.getOrdLnDlyPk().substring(0,
											order.getOrdLnDlyPk().length() - 3));
							ps.setObject(11, order.getOrdLnDlyPk());
							ps.setObject(12, woId);
							ps.executeUpdate();
							ps.close();
							// 修改接箍合同已挂个数
							String updateOrderJ = "UPDATE PLN_ORDER_JG T SET T.HANGKC_NUM=TO_NUMBER(T.HANGKC_NUM)+TO_NUMBER(?) WHERE T.JG_ORDERNO=?";
							ps = con.prepareStatement(updateOrderJ);
							ps.setObject(1, orderNum);
							ps.setObject(
									2,
									orderNum
											* Double.parseDouble(work
													.getSingleweight()));
							ps.setObject(3, work.getJgOrdernoAll());
							ps.executeUpdate();
							ps.close();
						}
						sjNum = sjNum - orderNum;
					}

				}
			}
			cor.setV_errMsg("交库成功！");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					cor.setV_errMsg(e.getMessage());

					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return cor;
	}

	/**
	 * 查询接箍合同对应成品合同
	 * 
	 * @param con
	 * @param matStatus
	 *            材料状态
	 * @throws SQLException
	 */
	public static List<PlnOrderJgEntity> getQureyCpOrder(Connection con,
			String woId) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.QueryWorkCpOrder");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, woId);
		ResultSet rs = ps.executeQuery();
		List<PlnOrderJgEntity> list = EntityTool.getEntitysByRs(rs,
				PlnOrderJgEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnOrderJgEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 修改库存位置
	 * 
	 * @param result
	 *            修改库存位置
	 * @return
	 */
	public CoreReturnObject updateLibraryResult(
			ArrayList<ArrayList<String>> pram) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String strMatButters = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.UpdateMatButters");// 修改位置表
			PreparedStatement pstaMatButters = con
					.prepareStatement(strMatButters);
			String strMatDeal = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.UpdateMatDeal");// 修改交易表
			PreparedStatement pstaMatDeal = con.prepareStatement(strMatDeal);

			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String matNo = list.get(0);
				String plnNo = list.get(1);
				String resltuNo = list.get(2);
				String kepNo = list.get(3);
				String stroager = list.get(4);
				String userName = list.get(5);
				String userTime = list.get(6);

				// 查询计划状态
				int intCount = 0;
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.QUERYMATDELV");
				PreparedStatement psta = con.prepareStatement(sqlloading);
				psta.setObject(1, resltuNo);
				psta.setObject(2, plnNo);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					intCount = Integer.parseInt(rs.getString("COUNT"));
				}
				rs.close();
				psta.close();

				if (intCount != 0) {
					cor.setV_errMsg("该工单计划交库后交易表已发生变化,不允许此操作！");
					return cor;
				}
				String storageAtte = "";
				String storageAtteName = "";
				String storageType = "";
				String storageTypeName = "";
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.queryStorageType");
				psta = con.prepareStatement(sqlloading);
				psta.setObject(1, stroager);
				rs = psta.executeQuery();
				if (rs.next()) {
					storageAtte = rs.getString("STORAGE_ATTR");
					storageAtteName = rs.getString("STORAGE_ATTR_NAME");
					storageType = rs.getString("STORAGE_TYPE_NO");
					storageTypeName = rs.getString("STORAGE_TYPE_NAME");
				}
				rs.close();
				psta.close();
				// 修改位置表
				pstaMatButters.setObject(1, kepNo);
				pstaMatButters.setObject(2, stroager);
				pstaMatButters.setObject(3, stroager + "10101");
				pstaMatButters.setObject(4, storageAtte);
				pstaMatButters.setObject(5, storageAtteName);
				pstaMatButters.setObject(6, storageType);
				pstaMatButters.setObject(7, storageTypeName);
				pstaMatButters.setObject(8, userName);
				pstaMatButters.setObject(9, userTime);
				pstaMatButters.setObject(10, matNo);
				pstaMatButters.setObject(11, plnNo);
				pstaMatButters.setObject(12, resltuNo);
				pstaMatButters.addBatch();
				// 修改交易表
				pstaMatDeal.setObject(1, kepNo);
				pstaMatDeal.setObject(2, stroager);
				pstaMatDeal.setObject(3, stroager + "10101");
				pstaMatDeal.setObject(4, storageAtte);
				pstaMatDeal.setObject(5, storageAtteName);
				pstaMatDeal.setObject(6, storageType);
				pstaMatDeal.setObject(7, storageTypeName);
				pstaMatDeal.setObject(8, userName);
				pstaMatDeal.setObject(9, userTime);
				pstaMatDeal.setObject(10, matNo);
				pstaMatDeal.setObject(11, plnNo);
				pstaMatDeal.setObject(12, resltuNo);
				pstaMatDeal.addBatch();

			}
			pstaMatButters.executeBatch();
			pstaMatDeal.executeBatch();
			cor.setV_errMsg("修改位置成功！");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					cor.setV_errMsg(e.getMessage());

					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return cor;
	}

	/**
	 * 删除交库实绩
	 * 
	 * @param result
	 *            删除交库实绩
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject deleteLibraryResult(ArrayList<Object> pram) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.DeleteComInfoResult");// 删除comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
			String strMatM = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.DeleteMatM");// 删除接箍材料表
			PreparedStatement pstaMatM = con.prepareStatement(strMatM);
			String strMatButters = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.DeleteMatButters");// 删除位置表
			PreparedStatement pstaMatButters = con
					.prepareStatement(strMatButters);
			String strMatDeal = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.DeleteMatDeal");// 删除交易表
			PreparedStatement pstaMatDeal = con.prepareStatement(strMatDeal);
			String deleteLin = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.DeleteLinResult");// 删除交库实绩表
			PreparedStatement pstadeleteLin = con.prepareStatement(deleteLin);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String dealOrde = list.get(0);
				String woId = list.get(1);
				String plnNo = list.get(2);
				String judNo = list.get(3);
				String batch = list.get(4);
				String user = list.get(5);
				String bc = list.get(6);
				String bz = list.get(7);
				String stGx = list.get(8);
				String libCount = list.get(9);// 判定个数
				String resultNo = list.get(10);// 判定个数
				// 查询工单信息
				PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
						con, woId, PlnZyJgWorkbillEntity.class);
				if (work == null) {
					cor.setV_errMsg("未查询到工单信息,请确认!");
					return cor;
				}
				if (!work.getZyBillStatus().equals("2")) {
					cor.setV_errMsg("工单状态不是生产中,不允许此操作!");
					return cor;
				}
				int intCount = 0;
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.QUERYMATDELV");
				psta = con.prepareStatement(sqlloading);
				// psta.setObject(1, plnNo);
				// psta.setObject(2, woId);
				psta.setObject(1, dealOrde);
				rs = psta.executeQuery();
				if (rs.next()) {
					intCount = Integer.parseInt(rs.getString("DETER_STATUS"));
				}
				rs.close();
				psta.close();
				if (intCount == 1) {
					cor.setV_errMsg("该交库信息已申请判定,不允许此操作！");
					return cor;
				}
				if (intCount == 2) {
					cor.setV_errMsg("该交库信息已判定,不允许此操作！");
					return cor;
				}

				comInfoPsta.setObject(1, woId);
				comInfoPsta.setObject(2, resultNo);
				comInfoPsta.setObject(3, stGx);
				comInfoPsta.addBatch();
				// 删除材料表
				pstaMatM.setObject(1, woId);
				pstaMatM.setObject(2, judNo);
				pstaMatM.setObject(3, batch);
				pstaMatM.addBatch();
				// 删除位置表
				pstaMatButters.setObject(1, dealOrde);
				pstaMatButters.addBatch();
				// 删除交易表
				pstaMatDeal.setObject(1, dealOrde);
				pstaMatDeal.setObject(2, resultNo);
				pstaMatDeal.addBatch();
				// 删除交库实绩表
				pstadeleteLin.setObject(1, woId);
				pstadeleteLin.setObject(2, resultNo);
				pstadeleteLin.addBatch();

			}
			comInfoPsta.executeBatch();
			pstaMatM.executeBatch();
			pstaMatButters.executeBatch();
			pstaMatDeal.executeBatch();
			pstadeleteLin.executeBatch();
			cor.setV_errMsg("撤销交库成功！");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					cor.setV_errMsg(e.getMessage());

					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return cor;
	}

	/**
	 * 申请判定
	 * 
	 * @param result
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject doApplyResult(ArrayList<Object> pram) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String deleteLin = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.UpdateButtersStrat");// 修改材料从表判定状态
			PreparedStatement pstadeleteLin = con.prepareStatement(deleteLin);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String dealOrde = list.get(0);
				String woId = list.get(1);
				String plnNo = list.get(2);
				String judNo = list.get(3);
				String batch = list.get(4);
				String user = list.get(5);
				String libCount = list.get(6);// 判定个数
				String time = list.get(7);
				String pline = list.get(8);
				// 获取交库实绩编号
				String resultNo = doAddResultNo(pline, woId, con);
				// 获取计划实体了
				PlnZyJgEntity pln = PlanService.getSendPlanCInfo(con, plnNo);
				// 获取材料号
				String matNo = doAddMatNo(judNo, resultNo, woId, con);
				int intCount = 0;
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.QUERYMATDELV");
				PreparedStatement psta = con.prepareStatement(sqlloading);
				// psta.setObject(1, plnNo);
				// psta.setObject(2, woId);
				// psta.setObject(1, matNo);
				psta.setObject(1, dealOrde);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					intCount = Integer.parseInt(rs.getString("DETER_STATUS"));
				}
				rs.close();
				psta.close();
				if (intCount == 1) {
					cor.setV_errMsg("该交库信息已申请判定,不允许此操作！");
					return cor;
				}
				if (intCount == 2) {
					cor.setV_errMsg("该交库信息已判定,不允许此操作！");
					return cor;
				}

				// 修改材料从表状态
				pstadeleteLin.setObject(1, "1");
				pstadeleteLin.setObject(2, dealOrde + "01");
				pstadeleteLin.setObject(3, user);
				pstadeleteLin.setObject(4, time);
				// pstadeleteLin.setObject(5, woId);
				// pstadeleteLin.setObject(6, plnNo);
				// pstadeleteLin.setObject(5, matNo);
				pstadeleteLin.setObject(5, dealOrde);
				pstadeleteLin.addBatch();

			}
			pstadeleteLin.executeBatch();
			cor.setV_errMsg("申请判定成功！");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					cor.setV_errMsg(e.getMessage());

					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return cor;
	}

	/**
	 * 撤销判定
	 * 
	 * @param result
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject undoApplyResult(ArrayList<Object> pram) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String deleteLin = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.UpdateButtersStrat");// 修改材料从表判定状态
			PreparedStatement pstadeleteLin = con.prepareStatement(deleteLin);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String dealOrde = list.get(0);
				String woId = list.get(1);
				String plnNo = list.get(2);
				String judNo = list.get(3);
				String batch = list.get(4);
				String user = list.get(5);
				String libCount = list.get(6);// 判定个数
				String pline = list.get(7);
				// 获取交库实绩编号
				String resultNo = doAddResultNo(pline, woId, con);
				// 获取计划实体了
				PlnZyJgEntity pln = PlanService.getSendPlanCInfo(con, plnNo);
				// 获取材料号
				String matNo = doAddMatNo(judNo, resultNo, woId, con);
				int intCount = 0;
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("FrmInLibrary.QUERYMATDELV");
				PreparedStatement psta = con.prepareStatement(sqlloading);
				// psta.setObject(1, plnNo);
				// psta.setObject(2, woId);
				// psta.setObject(1, matNo);
				psta.setObject(1, dealOrde);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					intCount = Integer.parseInt(rs.getString("DETER_STATUS"));
				}
				rs.close();
				psta.close();
				if (intCount == 2) {
					cor.setV_errMsg("该交库信息已判定,不允许此操作！");
					return cor;
				}

				// 修改材料从表状态
				pstadeleteLin.setObject(1, "0");
				pstadeleteLin.setObject(2, "");
				pstadeleteLin.setObject(3, "");
				pstadeleteLin.setObject(4, "");
				// pstadeleteLin.setObject(5, woId);
				// pstadeleteLin.setObject(6, plnNo);
				// pstadeleteLin.setObject(5, matNo);
				pstadeleteLin.setObject(5, dealOrde);
				pstadeleteLin.addBatch();

			}
			pstadeleteLin.executeBatch();
			cor.setV_errMsg("撤销判定成功！");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					cor.setV_errMsg(e.getMessage());

					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return cor;
	}

	/***
	 * 生成实绩编号
	 * 
	 * @param plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 */
	public String doAddResultNo(String plineCode, String woId,
			Connection connection) {
		String resultNo = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.queryLoadRESULT");// 查询最大实绩编号

		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, plineCode);
			psta.setObject(2, matter1.format(dt));
			psta.setObject(3, matter.format(dt));
			psta.setObject(4, woId);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return resultNo;
	}

	/***
	 * 生成交易单号
	 * 
	 * @param plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 */
	public String doAddDealOrde(String woId, Connection connection) {
		String resultNo = "";
		PreparedStatement psta = null;
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.queryMaxDealOrde");// 查询交易单号
		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, woId);
			psta.setObject(2, 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 plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 */
	public String doAddMatNo(String judNo, String result, String woId,
			Connection connection) {
		String MatNo = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		// SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.queryMaxMatNo");// 查询最大材料号

		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, judNo);
			psta.setObject(2, judNo);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				MatNo = rs1.getString("MAT_NO");
			}
			rs1.close();
			psta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return MatNo;
	}

	/***
	 * 结炉
	 * 
	 * @param zYgNo
	 * @return
	 */
	public CoreReturnObject doBeside(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.doBeside");
			PreparedStatement updateSqlPsta = connection
					.prepareStatement(updateSql);

			sql = CoreXmlSqlParsersFactory.getSql("FrmInLibrary.selectComInfo");
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(2));
			psta.setString(2, parm.get(1));
			rs = psta.executeQuery();
			if (rs.next()) {

			} else {
				cro.setV_errMsg("没有交库,无法进行结炉!");
				return cro;
			}
			// ;
			rs.close();
			psta.close();
			// //查询工序是否平
			// String statuCode = "";
			// String statuDesc = "";
			String proNum = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.queryComPieces");// 废品支数和磷化个数
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, parm.get(1));
			rs = psta.executeQuery();
			while (rs.next()) {
				proNum = rs.getString("FAIL_NUM");
			}
			rs.close();
			psta.close();
			// 查询锯切个数
			String cutNum = "";
			String sqlloading1 = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.queryCutPieces");// 锯切个数
			psta = connection.prepareStatement(sqlloading1);
			psta.setObject(1, parm.get(1));
			rs = psta.executeQuery();
			while (rs.next()) {
				cutNum = rs.getString("QUALIFIED_NUMBER");
			}
			rs.close();
			psta.close();
			if (Integer.parseInt(proNum) != Integer.parseInt(cutNum)) {
				cro.setV_errMsg("实绩个数不平!");
			}
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmInLibrary.queryComPlanJgCodeCut");
			// PreparedStatement psta1 = connection.prepareStatement(sql);
			// psta1.setString(1, parm.get(1));
			// psta1.setString(2, statuCode);
			// ResultSet rs1 = psta1.executeQuery();
			// if (rs1.next()) {
			// proNum = rs1.getString("PRO_NUM");
			// if(!parm.get(3).equals(proNum))
			// {
			// cro.setV_errMsg("该工单" + statuDesc
			// + "数据不平！");
			// return cro;
			// }
			// }
			// rs1.close();
			// psta1.close();
			// }
			// rs.close();
			// psta.close();

			updateSqlPsta.setString(1, parm.get(2));
			updateSqlPsta.setString(2, parm.get(1));
			updateSqlPsta.setString(3, parm.get(1));
			updateSqlPsta.addBatch();

			updateSqlPsta.executeBatch();
			cro.setV_errMsg("结炉成功!");
			connection.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());

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

	/***
	 * 撤销结炉
	 * 
	 * @param zYgNo
	 * @return
	 */
	public CoreReturnObject undoBeside(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.unDoBeside");
			PreparedStatement updateSqlPsta = connection
					.prepareStatement(updateSql);

			int cutFalg = 0;
			sql = CoreXmlSqlParsersFactory.getSql("FrmInLibrary.selectQcmApp");
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(1));
			rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			psta.close();
			if (cutFalg == 0) {
				cro.setV_errMsg("该炉已申请判定,不允许结炉回退!");
				return cro;
			}

			updateSqlPsta.setString(1, parm.get(2));
			updateSqlPsta.setString(2, parm.get(1));
			updateSqlPsta.setString(3, parm.get(1));
			updateSqlPsta.addBatch();

			updateSqlPsta.executeBatch();
			cro.setV_errMsg("撤销结炉成功!");
			connection.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());

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