package com.steering.mes.mcp.Vrp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
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.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.MatBcMEntity;
import com.steering.mes.mcp.entity.PlnZyDbkMEntity;
import com.steering.mes.mcp.entity.PlnZyRclMEntity1;
import com.steering.mes.mcp.entity.PortVrpBatchSampleResultEntity;
import com.steering.mes.mcp.entity.VrpComInfoEntity;
import com.steering.mes.mcp.entity.VrpEntrustedBillEntity;
import com.steering.mes.mcp.entity.VrpInstoreResultEntity;
import com.steering.mes.mcp.entity.YdmBcInlistEntity;

/***
 * 委外审批管理
 * 
 * @author TJG 2016/09/27
 */
public class FrmOutSourcApp extends DBCommon {
	/**
	 * 委外实绩查询
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject doQuery(ArrayList<String> list) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory.getSql("FrmOutSourcApp.doQueryOutSourc");
		if (!list.get(0).equals("")) {
			sql += "  AND T.SUBCONTRACT_TIME BETWEEN TO_DATE('" + list.get(0)
					+ "','YYYY-MM-DD HH24:MI:SS') " + " AND TO_DATE('"
					+ list.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if (!list.get(2).equals("")) {
			sql += " AND T.JUDGE_STOVE_NO || '&' LIKE '%' || '" + list.get(2)
					+ "' || '%'";
		}
		if (!list.get(3).equals("")) {
			sql += " AND T.ZY_BATCH_ID || '&' LIKE '%' || '" + list.get(3)
					+ "' || '%'";
		}
		if (!list.get(4).equals("")) {
			sql += " AND T.SUBCONTRACT_LOG ='" + list.get(4) + "'";
		}
		return this.getData(sql);
	}
	/***
	 * 查询交库实绩表
	 * @param parmList
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryInList(ArrayList<String> parmList,String[] plineCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmOutSourcApp.doQueryInList");
		if (!parmList.get(0).equals("")) {
			sql += " AND TRUNC(T.IN_TIME_F)>=TO_DATE('" + parmList.get(0)
					+ "','YYYY-MM-DD') AND TRUNC(T.IN_TIME_F)<= TO_DATE('"
					+ parmList.get(1) + "','YYYY-MM-DD')";
		}
		if(!parmList.get(2).equals(""))
		{
			sql += " AND T.LOT_NO = '"+parmList.get(2)+"'";
		}
		if(!parmList.get(3).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+parmList.get(3)+"'";
		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		sql += " group by T.SUBCONTRACT_NO,t.JUDGE_STOVE_NO,t.BATCH_NO,t.INSTOCK_DOC,t.PRODUCCODE,t.PRODUCNAME,t.STD_CODE,t.STD_NAME,t.STEELCODE,t.STEELNAME,t.STD_STYLE,t.STD_STYLE_DESC,t.SPEC_CODE,t.SPEC_NAME,t.JUDGE_STD_CODE,t.JUDGE_STD_NAME,t.GRADECODE,t.GRADENAME,t.BAL_YEAR_MONTH,t.PIPMEMO,t.BATCH_GROUD_NO,t.MATERIAL_NO,t.MATERIAL_NAME,t.MAT_STATUS,T.STORAGE_NO,";
		sql += " t.MODEL_DESC,t.ACT_DIMATER,t.ACT_HEIGHT,t.ACT_LEN_MIN,t.ACT_LEN_MAX,t.BELONG_CODE,t.BELONG_NAME,t.MEMO,t.CREATE_NAME,t.ORDER_NO,t.ORDER_SEQ,t.DELIVERY_NO,t.MODEL_CODE,t.PRODUCT_FLAG,t.PLINE_CODE,t.PLINE_NAME,t.ACT_LEN,t.GX_PLAN_NO,t.PRO_PLAN_ID,T.LOT_NO";
		sql += " order by t.instock_doc desc";
		return this.getData(sql);
	}
	/**
	 * 查询产线
	 * 
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryPlineCode(String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmOutSourcApp.doQueryPlineCode");
		sql += "  ORDER BY T.PLINE_NAME ASC";
		return this.getData(sql);
	}

	/**
	 * 委外审批通过
	 * 
	 * @param parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject doAppThrough(ArrayList<Object> parm) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String updateSourc = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateOutSourc");// 修改审批状态
			PreparedStatement pstaUpdateSourc = con
					.prepareStatement(updateSourc);

			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String spNot = list.get(0);// 审批说明
				String plineCode = list.get(1);
				String plineName = list.get(2);
				String subNo = list.get(3);// 委托单号
				String subNoSeq = list.get(4);// 委托单序号
				String name = list.get(5);
				String groupStart = "";// 状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.QuerySubConStart");// 查询委托状态
				psta = con.prepareStatement(sql);
				psta.setString(1, subNo);
				psta.setString(2, subNoSeq);
				rs = psta.executeQuery();
				if (rs.next()) {
					groupStart = rs.getString("SUBCONTRACT_LOG");
				}
				rs.close();
				psta.close();
				if (groupStart.equals("")) {
					cor.setV_errMsg("选择委外单已撤销,不允许此操作!");
					return cor;
				} else if (!groupStart.equals("00")) {
					cor.setV_errMsg("选择委外单不是委外申请状态,不允许此操作!");
					return cor;
				}
				// 修改审批状态
				pstaUpdateSourc.setObject(1, name);
				pstaUpdateSourc.setObject(2, systime);
				pstaUpdateSourc.setObject(3, "10");
				pstaUpdateSourc.setObject(4, plineCode);
				pstaUpdateSourc.setObject(5, plineName);
				pstaUpdateSourc.setObject(6, spNot);
				pstaUpdateSourc.setObject(7, subNo);
				pstaUpdateSourc.setObject(8, subNoSeq);
				pstaUpdateSourc.addBatch();

			}
			pstaUpdateSourc.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 parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject doAppNotThrough(ArrayList<Object> parm) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String updateSourc = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateOutSourc");// 修改审批状态
			PreparedStatement pstaUpdateSourc = con
					.prepareStatement(updateSourc);
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateOff");// 释放离线表委外支吨
			PreparedStatement pstaUpdatOff = con.prepareStatement(updateOff);
			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String spNot = list.get(0);// 审批说明
				String plineCode = list.get(1);
				String plineName = list.get(2);
				String subNo = list.get(3);// 委托单号
				String subNoSeq = list.get(4);// 委托单序号
				String name = list.get(5);
				String resultNo = list.get(8);
				String groupStart = "";// 状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.QuerySubConStart");// 查询委托状态
				psta = con.prepareStatement(sql);
				psta.setString(1, subNo);
				psta.setString(2, subNoSeq);
				rs = psta.executeQuery();
				if (rs.next()) {
					groupStart = rs.getString("SUBCONTRACT_LOG");
				}
				rs.close();
				psta.close();
				if (groupStart.equals("")) {
					cor.setV_errMsg("选择委外单已撤销,不允许此操作!");
					return cor;
				} else if (!groupStart.equals("00")) {
					cor.setV_errMsg("选择委外单不是委外申请状态,不允许此操作!");
					return cor;
				}
				// 修改审批状态
				pstaUpdateSourc.setObject(1, name);
				pstaUpdateSourc.setObject(2, systime);
				pstaUpdateSourc.setObject(3, "20");
				pstaUpdateSourc.setObject(4, plineCode);
				pstaUpdateSourc.setObject(5, plineName);
				pstaUpdateSourc.setObject(6, spNot);
				pstaUpdateSourc.setObject(7, subNo);
				pstaUpdateSourc.setObject(8, subNoSeq);
				pstaUpdateSourc.addBatch();
				// 释放离线表委外支吨
				pstaUpdatOff.setObject(1, "0");
				pstaUpdatOff.setObject(2, "0");
				pstaUpdatOff.setObject(3, systime);
				pstaUpdatOff.setObject(4, name);
				pstaUpdatOff.setObject(5, resultNo);
				pstaUpdatOff.addBatch();
			}
			pstaUpdateSourc.executeBatch();
			pstaUpdatOff.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 parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject doOutsourcReturn(ArrayList<Object> parm) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String updateSourc = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateOutSourcNum");// 修改委外回报量
			PreparedStatement pstaUpdateSourc = con
					.prepareStatement(updateSourc);
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateOffSourc");// 修改离线表委外回报
			PreparedStatement pstaUpdatOff = con.prepareStatement(updateOff);

			String updateBCMatStaus = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateBCMatStatus");// 标记材料表处理判废
			PreparedStatement pstaUpdateBCMatStaus = con
					.prepareStatement(updateBCMatStaus);

			String updatePortVRPMatStaus = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdatePortVRPMatStatus");// 标记管号队列为委外判废
			PreparedStatement pstaUpdatePortVRPMatStaus = con
					.prepareStatement(updatePortVRPMatStaus);

			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String hgCounNum = list.get(0);// 委外回报合格支
				String hgCounWt = list.get(1);// 委外回报合格吨
				String fpCounNum = list.get(2).trim();// 委外回报废品支
				String fpCounWt = list.get(3);// 委外回报废品吨
				String subNo = list.get(4);// 委托单号
				String subNoSeq = list.get(5);// 委托单序号
				String name = list.get(7);
				String resultNo = list.get(9);
				String groupStart = "";// 状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.QuerySubConStart");// 查询委托状态
				psta = con.prepareStatement(sql);
				psta.setString(1, subNo);
				psta.setString(2, subNoSeq);
				rs = psta.executeQuery();
				if (rs.next()) {
					groupStart = rs.getString("SUBCONTRACT_LOG");
				}
				rs.close();
				psta.close();
				if (groupStart.equals("")) {
					cor.setV_errMsg("选择委外单已撤销,不允许此操作!");
					return cor;
				} else if (!groupStart.equals("10")) {
					cor.setV_errMsg("选择委外单不是审批通过状态,不允许此操作!");
					return cor;
				}

				if (!"0".equals(fpCounNum)) {

					sql = CoreXmlSqlParsersFactory
							.getSql("FrmOutSourcApp.QueryOfflineMat");//
					psta = con.prepareStatement(sql);
					psta.setObject(1, resultNo);
					psta.setObject(2, fpCounNum);
					rs = psta.executeQuery();

					List<PortVrpBatchSampleResultEntity> Entitys = EntityTool
							.getEntitysByRs(rs,
									PortVrpBatchSampleResultEntity.class);
					rs.close();
					psta.close();

					if (Entitys.size() != Integer.parseInt(fpCounNum)) {
						cor.setV_errMsg("管号队列数据异常!");
						return cor;
					}

					for (PortVrpBatchSampleResultEntity entity : Entitys) {
						pstaUpdateBCMatStaus.setObject(1, entity.getMatNo());
						pstaUpdatePortVRPMatStaus.setObject(1,
								entity.getMatNo());
						pstaUpdateBCMatStaus.addBatch();
						pstaUpdatePortVRPMatStaus.addBatch();

					}
				}

				// 修改委外回报量
				pstaUpdateSourc.setObject(1, hgCounNum);
				pstaUpdateSourc.setObject(2, hgCounWt);
				pstaUpdateSourc.setObject(3, fpCounNum);
				pstaUpdateSourc.setObject(4, fpCounWt);
				pstaUpdateSourc.setObject(5, "30");
				pstaUpdateSourc.setObject(6, subNo);
				pstaUpdateSourc.setObject(7, subNoSeq);
				pstaUpdateSourc.addBatch();
				// 修改离线表委外回报
				pstaUpdatOff.setObject(1, Integer.parseInt(hgCounNum) + Integer.parseInt(fpCounNum));
				pstaUpdatOff.setObject(2, hgCounWt);
				pstaUpdatOff.setObject(3, fpCounNum);
				pstaUpdatOff.setObject(4, fpCounWt);
				pstaUpdatOff.setObject(5, resultNo);
				pstaUpdatOff.addBatch();
			}
			pstaUpdateBCMatStaus.executeBatch();
			pstaUpdatePortVRPMatStaus.executeBatch();
			pstaUpdateSourc.executeBatch();
			pstaUpdatOff.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 parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject cancelOutsourcReturn(ArrayList<Object> parm) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String updateSourc = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateOutSourcNum");// 修改委外回报量
			PreparedStatement pstaUpdateSourc = con
					.prepareStatement(updateSourc);
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateOffSourc");// 修改离线表委外回报
			PreparedStatement pstaUpdatOff = con.prepareStatement(updateOff);

			String updateBCMatStaus = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdateBCMatStatusNo");// 标记材料表待处理
			PreparedStatement pstaUpdateBCMatStaus = con
					.prepareStatement(updateBCMatStaus);

			String updatePortVRPMatStaus = CoreXmlSqlParsersFactory
					.getSql("FrmOutSourcApp.UpdatePortVRPMatStatusNo");// 标记管号队列为待处理
			PreparedStatement pstaUpdatePortVRPMatStaus = con
					.prepareStatement(updatePortVRPMatStaus);

			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String hgCounNum = list.get(0);// 委外回报合格支
				String hgCounWt = list.get(1);// 委外回报合格吨
				String fpCounNum = list.get(2).trim();// 委外回报废品支
				String fpCounWt = list.get(3);// 委外回报废品吨
				String subNo = list.get(4);// 委托单号
				String subNoSeq = list.get(5);// 委托单序号
				String name = list.get(7);
				String resultNo = list.get(9);
				String subCountNum = list.get(10);
				String subCountWt = list.get(11);
				String groupStart = "";// 状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.QuerySubConStart");// 查询委托状态
				psta = con.prepareStatement(sql);
				psta.setString(1, subNo);
				psta.setString(2, subNoSeq);
				rs = psta.executeQuery();
				if (rs.next()) {
					groupStart = rs.getString("SUBCONTRACT_LOG");
				}
				rs.close();
				psta.close();
				if (groupStart.equals("")) {
					cor.setV_errMsg("选择委外单已撤销,不允许此操作!");
					return cor;
				} else if (!groupStart.equals("30")) {
					cor.setV_errMsg("选择委外单不是委外完毕状态,不允许此操作!");
					return cor;
				}

				if (!"0".equals(fpCounNum)) {

					sql = CoreXmlSqlParsersFactory
							.getSql("FrmOutSourcApp.QueryOfflineMatChe");//
					psta = con.prepareStatement(sql);
					psta.setObject(1, resultNo);
					psta.setObject(2, fpCounNum);
					rs = psta.executeQuery();

					List<PortVrpBatchSampleResultEntity> Entitys = EntityTool
							.getEntitysByRs(rs,
									PortVrpBatchSampleResultEntity.class);
					rs.close();
					psta.close();

					if (Entitys.size() != Integer.parseInt(fpCounNum)) {
						cor.setV_errMsg("管号队列数据异常!");
						return cor;
					}

					for (PortVrpBatchSampleResultEntity entity : Entitys) {
						pstaUpdateBCMatStaus.setObject(1, entity.getMatNo());
						pstaUpdatePortVRPMatStaus.setObject(1,
								entity.getMatNo());
						pstaUpdateBCMatStaus.addBatch();
						pstaUpdatePortVRPMatStaus.addBatch();

					}
				}

				// 修改委外回报量
				pstaUpdateSourc.setObject(1, subCountNum);
				pstaUpdateSourc.setObject(2, subCountWt);
				pstaUpdateSourc.setObject(3, "0");
				pstaUpdateSourc.setObject(4, "0");
				pstaUpdateSourc.setObject(5, "10");
				pstaUpdateSourc.setObject(6, subNo);
				pstaUpdateSourc.setObject(7, subNoSeq);
				pstaUpdateSourc.addBatch();
				// 修改离线表委外回报
				pstaUpdatOff.setObject(1, subCountNum);
				pstaUpdatOff.setObject(2, subCountWt);
				pstaUpdatOff.setObject(3, "0");
				pstaUpdatOff.setObject(4, "0");
				pstaUpdatOff.setObject(5, resultNo);
				pstaUpdatOff.addBatch();
			}
			pstaUpdateBCMatStaus.executeBatch();
			pstaUpdatePortVRPMatStaus.executeBatch();
			pstaUpdateSourc.executeBatch();
			pstaUpdatOff.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 parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject OutsourceInStore(ArrayList<Object> parm,
			ArrayList<String> parmList, String plnZyTity, String userName,
			String mangMentNo, String userOrder, String userClass) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
//			String detelePortStaus = CoreXmlSqlParsersFactory
//					.getSql("FrmOutSourcApp.DetelePortStatus");// 删除管号队列数据委外废品
//			PreparedStatement pstadetelePortStaus = con
//					.prepareStatement(detelePortStaus);

//			String addport = CoreXmlSqlParsersFactory
//					.getSql("FrmVrpLoading.DeletePortVrp");// 删除管号队列合格
//			PreparedStatement patsport = con.prepareStatement(addport);
			String hgCounNum = "";
			String hgCounWt = "";
			String fpCounNum = "";
			String fpCounWt = "";

			String phyBatchNo = "";
			String issamplePipe = "";
			String heatPlanNo = "";
			String subcontractPline = "";

			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				hgCounNum = list.get(0);// 委外回报合格支
				hgCounWt = list.get(1);// 委外回报合格吨
				fpCounNum = list.get(2);// 委外回报废品支
				fpCounWt = list.get(3);// 委外回报废品吨
				String subNo = list.get(4);// 委托单号
				String subNoSeq = list.get(5);// 委托单序号
				String name = list.get(7);
				String resultNo = list.get(9);
				String stationCode = list.get(10);
				heatPlanNo = list.get(11);
				String batchNo = list.get(13);
				subcontractPline = list.get(14);

				List<String> matNo = new ArrayList<String>();
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.QueryOffMatNo");// 查询管号队列离线材料号
				psta = con.prepareStatement(sql);
				psta.setString(1, batchNo);
				psta.setString(2, heatPlanNo);
				psta.setString(3, stationCode);
				psta.setObject(4, Integer.parseInt(hgCounNum) + 1);
				rs = psta.executeQuery();				
				while (rs.next()) {
					matNo.add(rs.getString("MAT_NO"));
				}				
				rs.close();
				psta.close();
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.QueryOffMatNo");// 查询管号队列离线材料号
				psta = con.prepareStatement(sql);
				psta.setString(1, batchNo);
				psta.setString(2, heatPlanNo);
				psta.setString(3, stationCode);
				psta.setObject(4, Integer.parseInt(hgCounNum) + 1);
				rs = psta.executeQuery();	
				List<PortVrpBatchSampleResultEntity> Entitys1 = EntityTool
						.getEntitysByRs(rs,
								PortVrpBatchSampleResultEntity.class);
				for (PortVrpBatchSampleResultEntity entity : Entitys1) {
				phyBatchNo = entity.getPhyBatchNo();
				issamplePipe = entity.getIsSamplePipe();
				}
				rs.close();
				psta.close();
				for (int m = 0; m < matNo.size(); m++) {
					PlanService.DeleteOffLineMat(con, matNo.get(m));
//					// 删除管号队列
//					patsport.setObject(1, heatPlanNo);
//					patsport.setObject(2, matNo.get(m));
//					patsport.addBatch();
				}
//				patsport.executeBatch();

				if (!"0".equals(fpCounNum)) {

					sql = CoreXmlSqlParsersFactory
							.getSql("FrmOutSourcApp.QueryOfflineMatNo");//
					psta = con.prepareStatement(sql);
					psta.setObject(1, resultNo);
					psta.setObject(2, fpCounNum);
					rs = psta.executeQuery();

					List<PortVrpBatchSampleResultEntity> Entitys = EntityTool
							.getEntitysByRs(rs,
									PortVrpBatchSampleResultEntity.class);
					rs.close();
					psta.close();

					if (Entitys.size() != Integer.parseInt(fpCounNum)) {
						cor.setV_errMsg("管号队列数据异常!");
						return cor;
					}

//					for (PortVrpBatchSampleResultEntity entity : Entitys) {
//						pstadetelePortStaus.setObject(1, entity.getMatNo());
//						pstadetelePortStaus.addBatch();
//					}
				}
			}
//			pstadetelePortStaus.executeBatch();

			SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
			SimpleDateFormat dd = new SimpleDateFormat("yyyyMMdd");
			String yearMoth = df.format(now);
			String yearMothDay = dd.format(now);
			String inDoc = "";// 入库单号
			String storageNo = "";// 仓库号
			String strInBound = "800701";
			String belongCode = "";
			String belongName = "";
			String groupNo = "";// 组号
			int inCount = 0;// 入库总支数
			String gpreqNum = "";// 需求支
			String matGpNum = "";// 已分配到订单支数
			int gpreqCount = 0;// 未分配支数
			int interNum = 0;// 整数支
			int remianNum = 0;// 余数支
			// PlnZyDbkMEntity plnZyEntity =
			// EntityTool.getEntityByJSONStr(plnZyTity, PlnZyDbkMEntity.class);
			PlnZyDbkMEntity plnZyEntity = PlanService.getSendDBKPlanCInfo(con,
					heatPlanNo);
			inDoc = getInListDoc(con, strInBound);
			storageNo = getStorageNo(con, subcontractPline);
			ArrayList<String> sqllist = new ArrayList<String>();
			ArrayList<String> sqllist1 = new ArrayList<String>();
			sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.selectBelong");
			sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
			psta = con.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				belongCode = rs.getString("DEPARTMENT_CODE");
				belongName = rs.getString("DEPARTMENT_DESC");
			}
			rs.close();
			psta.close();

			for (String str : parmList) {
				VrpEntrustedBillEntity portVrpEntity = EntityTool
						.getEntityByJSONStr(str, VrpEntrustedBillEntity.class);
				groupNo = creatGroup(con, plnZyTity, str);
				// String inListSeq = querySendSql(connection, inDoc); // 入库流水号
				// int n = Integer.parseInt(inListSeq.substring(
				// inListSeq.length() - 4, inListSeq.length()));
				MatBcMEntity matBcEntity = new MatBcMEntity();
				// matBcEntity.setTestCount(String.valueOf(n));
				matBcEntity.setInstockDoc(inDoc);
				matBcEntity.setInstockTypeCode(strInBound);
				matBcEntity.setInstockTypeName("产出入库");

				matBcEntity.setStorageNo(storageNo);
				matBcEntity.setLocationNo(storageNo + "10101");
				matBcEntity.setLaryNo("1");

				matBcEntity.setBatchGroudNo(groupNo);
				if (plnZyEntity.getIsendgx().equals("0")) {
					matBcEntity.setProductFlag("801401");
					matBcEntity.setMatStatus("80150101");
				} else {
					matBcEntity.setProductFlag("801402");
					matBcEntity.setMatStatus("80150201");
				}
				matBcEntity.setJudgeStoveNo(plnZyEntity.getJudgeStoveNo());
				matBcEntity.setMaterialNo(plnZyEntity.getOutwlId());
				matBcEntity.setMaterialName(plnZyEntity.getOutwlDesc());
				matBcEntity.setProduccode(plnZyEntity.getProduccode());
				matBcEntity.setProducname(plnZyEntity.getProducname());
				matBcEntity.setSteelcode(plnZyEntity.getSteelcode());
				matBcEntity.setSteelname(plnZyEntity.getSteelname());
				matBcEntity.setStdStyle(plnZyEntity.getStdStyle());
				matBcEntity.setStdStyleDesc(plnZyEntity.getStdStyleDesc());
				matBcEntity.setSpecCode(plnZyEntity.getSpecCode());
				matBcEntity.setSpecName(plnZyEntity.getSpecName());
				matBcEntity.setModelCode(plnZyEntity.getModelCode());
				matBcEntity.setModelDesc(plnZyEntity.getModelDesc());
				matBcEntity.setGradecode(plnZyEntity.getGradecode());
				matBcEntity.setGradename(plnZyEntity.getGradename());
				matBcEntity.setStdCode(plnZyEntity.getStdCode());
				matBcEntity.setStdName(plnZyEntity.getStdName());
				matBcEntity.setProOrderNo(plnZyEntity.getOrderNo() + "/"
						+ plnZyEntity.getOrderSeq());
				matBcEntity.setProcessCdoe("E");
				matBcEntity.setProcessDesc("镦拔扩");
				matBcEntity
						.setNextProcessCdoe(plnZyEntity.getNextProcessCode());
				matBcEntity.setNextProcessDesc("");
				matBcEntity.setPlineCode(plnZyEntity.getPlineCode());
				matBcEntity.setPlineName(plnZyEntity.getPlineName());
				matBcEntity.setMatOriginCode("801302");
				matBcEntity.setMatOriginName("产线");
				matBcEntity.setActDimater(portVrpEntity.getActDimater());
				matBcEntity.setActHeight(portVrpEntity.getActHeight());
				matBcEntity.setActLenMin(portVrpEntity.getActLenMin());
				matBcEntity.setActLenMax(portVrpEntity.getActLenMax());
				matBcEntity.setBelongCode(belongCode);
				matBcEntity.setBelongName(belongName);
				matBcEntity.setCreateName(userName);
				matBcEntity.setCreateTime(systime);
				matBcEntity.setBalYearMonth(yearMoth);
				matBcEntity.setJudgeStdCode("");
				if ((plnZyEntity.getCic() + "+" + plnZyEntity.getPic() + "+"
						+ plnZyEntity.getSic() + "+" + plnZyEntity.getDic())
						.equals("++++")) {
					matBcEntity.setJudgeStdName("");
				} else {
					matBcEntity.setJudgeStdName(plnZyEntity.getCic() + "+"
							+ plnZyEntity.getPic() + "+" + plnZyEntity.getSic()
							+ "+" + plnZyEntity.getDic());
				}

				matBcEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
				matBcEntity.setQcmStaus("00");
				matBcEntity.setHeatPlanNo(plnZyEntity.getHeatPlanNo());
				matBcEntity.setActCount(portVrpEntity.getSubcontractNum());
				matBcEntity.setProOrderId(plnZyEntity.getProPlanId());
				matBcEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
				matBcEntity.setPhyBatchNo(phyBatchNo);
				matBcEntity.setIsSamplePipe(issamplePipe);
				matBcEntity.setCrafNo(plnZyEntity.getCraftFileNo());
				matBcEntity.setCraftSeq(plnZyEntity.getCraftSeq());
				matBcEntity.setOrdLnDlyPk(plnZyEntity.getOrdLnDlyPk());
				matBcEntity.setOrderNo(plnZyEntity.getOrderNo());
				matBcEntity.setOrderSeq(plnZyEntity.getOrderSeq());
				matBcEntity.setOrderSeqNo(plnZyEntity.getDeliveryNo());
				matBcEntity.setTarget(plnZyEntity.getDecision());
				matBcEntity.setFlagStoveNoZg(plnZyEntity.getJudgeStoveNo());
				matBcEntity.setMatGowhereCode(plnZyEntity.getGowhereCode());
				matBcEntity.setMatGowhereName(plnZyEntity.getGowhereName());

				String insertBcSql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.insertMatBcM");// 插入库存、交易
				insertBcSql = EntityTool.fillSqlByEntity(insertBcSql,
						matBcEntity);
				sqllist.add(insertBcSql);

				VrpInstoreResultEntity vrpInstoreEntity = new VrpInstoreResultEntity();
				vrpInstoreEntity.setHeatPlanNo(plnZyEntity.getHeatPlanNo());
				vrpInstoreEntity.setJudgeStoveNo(plnZyEntity.getJudgeStoveNo());
				vrpInstoreEntity.setProNum(portVrpEntity.getSubcontractNum());
				vrpInstoreEntity.setProWt(portVrpEntity.getSubcontractWt());
				vrpInstoreEntity.setPlineCode(plnZyEntity.getPlineCode());
				vrpInstoreEntity.setPlineName(plnZyEntity.getPlineName());
				vrpInstoreEntity.setCreateName(userName);
				vrpInstoreEntity.setColShift(userOrder);
				vrpInstoreEntity.setColGroup(userClass);
				vrpInstoreEntity.setZyBatchId(plnZyEntity.getZyBatchId());
				vrpInstoreEntity.setProPlanId(plnZyEntity.getProPlanId());
				vrpInstoreEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());

				String insertInstoreSql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpInStore.insertInStore");
				insertInstoreSql = EntityTool.fillSqlByEntity(insertInstoreSql,
						vrpInstoreEntity);
				sqllist.add(insertInstoreSql);
				
				//修改委外状态
				VrpEntrustedBillEntity vrpBill = new VrpEntrustedBillEntity();
				vrpBill.setSubcontractNo(portVrpEntity.getSubcontractNo());
				vrpBill.setSubcontractNum(portVrpEntity.getSubcontractNum());
				vrpBill.setJudgeStoveNo(portVrpEntity.getJudgeStoveNo());
				vrpBill.setBatchNo(portVrpEntity.getBatchNo());
				String insertInlist = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.insertInList");
				insertInlist = EntityTool.fillSqlByEntity(insertInlist,
						vrpBill);
				sqllist1.add(insertInlist);

				vrpBill.setSubcontractNo(portVrpEntity.getSubcontractNo());
				String updateInstoreSql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.updateInStore");
				updateInstoreSql = EntityTool.fillSqlByEntity(updateInstoreSql,
						vrpBill);
				sqllist.add(updateInstoreSql);
				

				VrpComInfoEntity vrpEntity = new VrpComInfoEntity();
				vrpEntity.setPlineCode(plnZyEntity.getPlineCode());
				vrpEntity.setPlineName(plnZyEntity.getPlineName());
				vrpEntity.setHeatPlanNo(plnZyEntity.getHeatPlanNo());
				vrpEntity.setZyBatchId(plnZyEntity.getZyBatchId());
				vrpEntity.setProPlanId(plnZyEntity.getProPlanId());
				vrpEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
				vrpEntity.setIfMonitorDbk(plnZyEntity.getIfMonitor());
				vrpEntity.setPrdcrNo(plnZyEntity.getPrdcrNo());
				vrpEntity.setMonitordemand("");
				vrpEntity.setCraftFileNo(plnZyEntity.getCraftFileNo());
				vrpEntity.setCraftSeq(plnZyEntity.getCraftSeq());
				vrpEntity.setCraftFileMan(plnZyEntity.getCraftFileMan());
				vrpEntity.setJudgeStoveNo(plnZyEntity.getJudgeStoveNo());
				vrpEntity.setBatchNo(portVrpEntity.getBatchNo());
				vrpEntity.setBatchGroudNo(groupNo);
				vrpEntity.setGradecode(plnZyEntity.getGradecode());
				vrpEntity.setGradename(plnZyEntity.getGradename());
				vrpEntity.setSteelcode(plnZyEntity.getSteelcode());
				vrpEntity.setSteelname(plnZyEntity.getSteelname());
				vrpEntity.setSpecCode(plnZyEntity.getSpecCode());
				vrpEntity.setSpecName(plnZyEntity.getSpecName());
				vrpEntity.setLen(plnZyEntity.getLengthIn());
				vrpEntity.setDimater(plnZyEntity.getOutdiameterIn());
				vrpEntity.setHeight(plnZyEntity.getWallthickIn());
				vrpEntity.setPlnLen(portVrpEntity.getActLen());
				vrpEntity.setPlnLenMin(plnZyEntity.getAimlengthMin());
				vrpEntity.setPlnLenMax(plnZyEntity.getAimlengthMax());
				vrpEntity.setLenMin(plnZyEntity.getLengthminIn());
				vrpEntity.setLenMax(plnZyEntity.getLengthmaxIn());
				vrpEntity.setLenCutHead("");// 切头
				vrpEntity.setLenCutEnd("");// 切尾
				vrpEntity.setActSpecCode(plnZyEntity.getSpecCode());
				vrpEntity.setActSpecName(plnZyEntity.getSpecName());
				vrpEntity.setActDimater(plnZyEntity.getAimoutdiameter());
				vrpEntity.setActHeight(plnZyEntity.getAimwallthick());
				vrpEntity.setActLen(portVrpEntity.getActLen());
				vrpEntity.setActLenMin(plnZyEntity.getAimlengthMin());
				vrpEntity.setActLenMax(plnZyEntity.getAimlengthMax());
				vrpEntity.setPlnSpecCode(plnZyEntity.getSpecCode());
				vrpEntity.setPlnSpecName(plnZyEntity.getSpecName());
				vrpEntity.setPlnDimater(plnZyEntity.getAimoutdiameter());
				vrpEntity.setPlnHeight(plnZyEntity.getAimwallthick());
				vrpEntity.setActMaterialNo(plnZyEntity.getOutwlId());
				vrpEntity.setActMaterialName(plnZyEntity.getOutwlDesc());
				vrpEntity.setInWlId(plnZyEntity.getInwlId());
				vrpEntity.setInWlDesc(plnZyEntity.getInwlDesc());
				vrpEntity.setProNum(hgCounNum);
				vrpEntity.setProWt(hgCounWt);
				vrpEntity.setStationStatus("1");
				vrpEntity.setCreateTime(systime);
				vrpEntity.setRroTime(systime);
				vrpEntity.setProUser(userName);
				vrpEntity.setProGroup(userClass);
				vrpEntity.setProShift(userOrder);
				vrpEntity.setColOperator(userName);
				vrpEntity.setBalYearMonth(yearMoth);
				vrpEntity.setInstockDoc(inDoc);
				vrpEntity.setProYearMonthDay(yearMothDay);
				String inserVrpComInfoSql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpInStore.insertVrpComInfo");// 插入公共表
				inserVrpComInfoSql = EntityTool.fillSqlByEntity(
						inserVrpComInfoSql, vrpEntity);
				sqllist.add(inserVrpComInfoSql);

				inCount = inCount
						+ Integer.parseInt(portVrpEntity.getSubcontractNum());
			}
			if (!plnZyEntity.getNextProcessCode().equals("")) {

				if (plnZyEntity.getNextProcessCode().equals("F"))// 热处理
				{
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.selectPlnZyRcl");

				} else if (plnZyEntity.getNextProcessCode().equals("G")) {
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.selectPlnZyJg");
				} else if (plnZyEntity.getNextProcessCode().equals("H"))
				{
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.selectPlnZyJgm");
				}
				sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
				psta = con.prepareStatement(sql);
				rs = psta.executeQuery();
				List<PlnZyRclMEntity1> plnOrderZy = EntityTool.getEntitysByRs(
						rs, PlnZyRclMEntity1.class);
				rs.close();
				psta.close();
				if (plnOrderZy.size() > 0) {
					for (int i = 0; i < plnOrderZy.size(); i++) {
						// 该订单下已分配多少支
						sql = CoreXmlSqlParsersFactory
								.getSql("FrmVrpInStore.selectMatGpMNumZg");
						sql = EntityTool
								.fillSqlByEntity(sql, plnOrderZy.get(i));
						psta = con.prepareStatement(sql);
						rs = psta.executeQuery();
						if (rs.next()) {
							matGpNum = rs.getString("ACT_COUNT");
						}
						rs.close();
						psta.close();

						gpreqCount = Integer.parseInt(plnOrderZy.get(i)
								.getPlnInsingleNum())
								- Integer.parseInt(matGpNum);
						if (gpreqCount <= 0 && i + 1 < plnOrderZy.size()) {
							continue;
						} else {
							MatBcMEntity matEntity = new MatBcMEntity();
							matEntity.setJudgeStoveNo(plnZyEntity
									.getJudgeStoveNo());
							matEntity.setProOrderId(plnZyEntity.getProPlanId());
							matEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
							matEntity
									.setHeatPlanNo(plnZyEntity.getHeatPlanNo());
							matEntity.setProPlanIdNext(plnOrderZy.get(i)
									.getProPlanId());
							matEntity.setGxPlanNoNext(plnOrderZy.get(i)
									.getGxPlanNo());
							matEntity.setHeatPlanNoNext(plnOrderZy.get(i)
									.getHeatPlanNo());
							matEntity.setPlineCode(plnZyEntity.getPlineCode());
							matEntity.setCodeJgBl(plnOrderZy.get(i).getCodeJgBl());
							if (inCount < gpreqCount) {
								matEntity.setActCount(String.valueOf(inCount));
							} else {
								matEntity.setActCount(String.valueOf(inCount));
							}
							String updateMatBcMSql = CoreXmlSqlParsersFactory
									.getSql("FrmVrpInStore.updateMatBcM");
							updateMatBcMSql = EntityTool.fillSqlByEntity(
									updateMatBcMSql, matEntity);
							sqllist.add(updateMatBcMSql);
							gpreqCount = gpreqCount - inCount;
							if (gpreqCount <= 0 && i + 1 < plnOrderZy.size()) {
								break;
							}
						}
					}
				}
			}
			cor = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			cor = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist1);
			if(cor.getV_errMsg().equals(""))
			{
			cor.setV_errMsg("委外入库成功！");
			}
		} 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 parm
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject CancelOutsourceInStore(
			ArrayList<String> parmList) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String addport = CoreXmlSqlParsersFactory
					.getSql("FrmVrpLoading.AddPortVrp");// 新增管号队列
			PreparedStatement patsport = con.prepareStatement(addport);

			int appCount = 0;
			ArrayList<String> sqllist = new ArrayList<String>();
			for (String str : parmList) {
				YdmBcInlistEntity ydmEntity = EntityTool.getEntityByJSONStr(
						str, YdmBcInlistEntity.class);
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpInStore.selectNode");// 查询是否已结炉
				sql = EntityTool.fillSqlByEntity(sql, ydmEntity);
				psta = con.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					appCount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (appCount > 0) {
					cor.setV_errMsg("该炉已结炉,不允许撤销交库!");
					return cor;
				}

				
				List<String> matNo = new ArrayList<String>();
				if (Integer.parseInt(ydmEntity.getActCount()) > 0) {
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmOutSourcApp.QueryInlistMat");// 查询入库表里材料号
					psta = con.prepareStatement(sql);
					psta.setString(1, ydmEntity.getInstockDoc());
					psta.setObject(2, Integer.parseInt(ydmEntity.getActCount()) + 1);
					rs = psta.executeQuery();
					while(rs.next())
					{
						matNo.add(rs.getString("MAT_NO"));
					}
					rs.close();
					psta.close();

//					for (PortVrpBatchSampleResultEntity entity : Entitys) {
//						patsport.setObject(1, entity.getHeatPlanNo());
//						patsport.setObject(2, entity.getMatNo());
//						patsport.setObject(3, "");
//						patsport.setObject(4, "");
//						patsport.setObject(5, entity.getLotNo());
//						patsport.setObject(6, "");
//						patsport.setObject(7, entity.getPlineCode());
//						patsport.setObject(8, entity.getPlineName());
//						patsport.setObject(9, entity.getJudgeStoveNo());
//						patsport.setObject(10, entity.getBatchNo());
//						patsport.setObject(11, entity.getGroupNo());
//						patsport.setObject(12, entity.getStoveNo());
//						patsport.setObject(13, entity.getActDimater());
//						patsport.setObject(14, entity.getActHeight());
//						patsport.setObject(15, entity.getActLen());
//						patsport.setObject(16, entity.getActLenMin());
//						patsport.setObject(17, entity.getActLenMax());
//						patsport.setObject(18, entity.getActCount());
//						patsport.setObject(19, entity.getActWeight());
//						patsport.setObject(20, entity.getActTheoryWeight());
//						patsport.setObject(21, entity.getOpProcess());
//						patsport.setObject(22, "20");
//						patsport.setObject(23, entity.getProUser());
//						patsport.setObject(24, entity.getProShift());
//						patsport.setObject(25, entity.getProGroup());
//						patsport.setObject(26, systime);
//						patsport.setObject(27, entity.getPrcTechnologyPath());
//						patsport.setObject(28, entity.getFlagStoveNo());
//						patsport.setObject(29, entity.getBatchNo());
//						patsport.setObject(30, entity.getFeedHeatNo());
//						patsport.setObject(31, entity.getHeatPlanSqe());
//						patsport.addBatch();
//					}
//					patsport.executeBatch();
				}
				for (int m = 0; m < matNo.size(); m++) {
					PlanService.AddOffLineMat(con, "E", ydmEntity.getCreateName(), "11",
							matNo.get(m), "", ydmEntity.getJudgeStoveNo());

				}

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpInStore.SelectMatGpStorage");// 查询在库物料支数
				sql = EntityTool.fillSqlByEntity(sql, ydmEntity);
				psta = con.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					appCount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (appCount != Integer.parseInt(ydmEntity.getActCount())) {
					cor.setV_errMsg("物料已发运,请先撤销发运!");
					return cor;
				}
				
				//修改委外状态
				VrpEntrustedBillEntity vrpBill = new VrpEntrustedBillEntity();

				vrpBill.setSubcontractNo(ydmEntity.getSubcontractNo());
				String updateInstoreSql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.updateOutStuts");
				updateInstoreSql = EntityTool.fillSqlByEntity(updateInstoreSql,
						vrpBill);
				sqllist.add(updateInstoreSql);
				
				String deleteInListSql = CoreXmlSqlParsersFactory
						.getSql("FrmOutSourcApp.DeleteInList");
				deleteInListSql = EntityTool.fillSqlByEntity(deleteInListSql,
						ydmEntity);
				sqllist.add(deleteInListSql);
			}
			cor = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			if(cor.getV_errMsg().equals(""))
			{
			cor.setV_errMsg("取消委外入库成功！");
			}
		} 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 conn
	 * @param strInbound
	 * @return
	 * @throws SQLException
	 */
	public String getInListDoc(Connection conn, String strInbound)
			throws SQLException {
		String inListDoc = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.getInListDoc");
		PreparedStatement psta = conn.prepareStatement(sql);
		psta.setString(1, strInbound);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDoc = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return inListDoc;
	}

	/***
	 * 查询仓库
	 * 
	 * @param connection
	 * @param mangMentNo
	 * @return
	 * @throws SQLException
	 */
	private String getStorageNo(Connection conn, String plineCode)
			throws SQLException {
		String storageNo = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.getStorageNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, plineCode);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			storageNo = rs.getString("STORAGE_NO");
		}
		rs.close();
		psta.close();
		return storageNo;
	}

	/***
	 * 生成组号
	 * 
	 * @param con
	 * @param matTity
	 * @param parm
	 * @return
	 */
	public String creatGroup(Connection con, String plnZyTity, String str)
			throws Exception {
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String strGroup = "";
		String actLenMin = "";
		String actLenMax = "";
		String[] actLenM = new String[2];
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHMMSSFFF");
		String systime = dateFormat.format(now);
		VrpEntrustedBillEntity milPlan = EntityTool.getEntityByJSONStr(plnZyTity,
				VrpEntrustedBillEntity.class);
		PortVrpBatchSampleResultEntity portEntity = EntityTool
				.getEntityByJSONStr(str, PortVrpBatchSampleResultEntity.class);
		sql = CoreXmlSqlParsersFactory.getSql("creatGroup.selectMilInStore");
		psta = con.prepareStatement(sql);
		psta.setString(1, milPlan.getJudgeStoveNo());
		psta.setString(2, portEntity.getPhyBatchNo());
		psta.setString(3, portEntity.getActLen());
		psta.setString(4, portEntity.getActLen());
		rs = psta.executeQuery();
		if (rs.next()) {
			strGroup = rs.getString("BATCH_GROUD_NO");
		}
		rs.close();
		psta.close();

		if (strGroup.equals("")||strGroup==null) {
			strGroup = queryGroupNo(con, milPlan.getJudgeStoveNo(),
					portEntity.getPhyBatchNo());// 组号
			if (Double.parseDouble(milPlan.getActLen()) > Double
					.parseDouble(portEntity.getActLen())) {
				actLenM = lessThanActMin(portEntity.getActLen(),
						new DecimalFormat("0.000").format(Double
								.parseDouble(milPlan.getActLenMin()
										.toString())));// 长度小于下限
				actLenMin = actLenM[0];
				actLenMax = actLenM[1];
			} else if (Double.parseDouble(portEntity.getActLen()) > Double
					.parseDouble(milPlan.getActLenMax())) {
				actLenM = moreThanActMax(portEntity.getActLen(),
						new DecimalFormat("0.000").format(Double
								.parseDouble(milPlan.getActLenMax()
										.toString())));// 长度大于上限
				actLenMin = actLenM[0];
				actLenMax = actLenM[1];
			} else {
				actLenMin = milPlan.getActLenMin();
				actLenMax = milPlan.getActLenMax();
			}
			sql = CoreXmlSqlParsersFactory
					.getSql("creatGroup.InsertMilInStore");// 插入记录
			psta = con.prepareStatement(sql);
			psta.setString(1, systime + strGroup);
			psta.setString(2, milPlan.getJudgeStoveNo());
			psta.setString(3, portEntity.getPhyBatchNo());
			psta.setString(4, portEntity.getIsSamplePipe());
			psta.setString(5, "");
			psta.setString(6, actLenMin);
			psta.setString(7, actLenMax);
			psta.setString(8, strGroup);
			psta.addBatch();
			psta.executeBatch();
			con.commit();
		}
		return strGroup;
	}

	/**
	 * 根据炉号、批号自动组号
	 * 
	 * @param conn
	 *            c
	 * @param strLcode
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryGroupNo(Connection conn, String strLcode, String strBatch)
			throws Exception {
		int count = 0;
		String groupNo = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.queryGroupNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strLcode);
		psta.setString(2, strBatch);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			groupNo = rs.getString("BATCH_GROUD_NO");
		}
		rs.close();
		psta.close();
		return groupNo;
	}

	/***
	 * 小于长度下限时
	 * 
	 * @param length
	 * @param actLenMin
	 * @return
	 */
	public String[] lessThanActMin(String length, String actLenMin) {
		String[] arr = new String[2];
		String[] lengmin = new String[2];// 下限返回的整数位 和小数位
		String[] lengmax = new String[2];// 上限
		double lengthmin = Double.parseDouble(actLenMin);
		double lengthmax = Double.parseDouble(actLenMin);

		for (int i = 0; i < 5; i++) {
			lengthmax = lengthmin;
			lengthmin = lengthmin - 1;
			lengmin = getNumber(String.valueOf(lengthmin));
			if (Double.parseDouble(lengmin[1]) > 0
					&& 800 > Double.parseDouble(lengmin[1])
					&& !lengmin[1].equals("5")) {
				lengthmin = Double.parseDouble(lengmin[0].toString() + ".5");
			} else if (Double.parseDouble(lengmin[1]) > 800) {
				lengthmin = Double.parseDouble(String.valueOf(Integer
						.parseInt(lengmin[0]) + 1) + ".0");
			}
			if (Double.parseDouble(length) >= lengthmin
					&& lengthmax > Double.parseDouble(length)) {
				arr[0] = String.valueOf(lengthmin);
				arr[1] = String.valueOf(lengthmax - 0.001);
				break;
			}
		}
		return arr;
	}

	/***
	 * () 大于长度上限时
	 * 
	 * @param length
	 * @param actLenMin
	 * @return
	 */
	public String[] moreThanActMax(String length, String actLenMax) {
		String[] arr = new String[2];
		String[] lengmax = new String[2];// 上限返回的整数位 和小数位
		String[] lengmin = new String[2];// 下限
		double lengthmin = Double.parseDouble(actLenMax);
		double lengthmax = Double.parseDouble(actLenMax);

		for (int i = 0; i < 5; i++) {
			lengthmin = lengthmax;
			lengthmax = lengthmax + 1;

			lengmax = getNumber(String.valueOf(lengthmax));
			if (Integer.parseInt(lengmax[1]) > 0
					&& 500 > Integer.parseInt(lengmax[1])
					&& !lengmax[1].equals("5")) {
				lengthmax = Double.parseDouble(lengmax[0].toString() + ".0");
			} else if (Integer.parseInt(lengmax[1]) > 500
					&& Integer.parseInt(lengmax[1]) < 800) {
				lengthmax = Double.parseDouble(lengmax[0].toString() + ".5");
			} else if (Integer.parseInt(lengmax[1]) > 800) {
				lengthmax = Double.parseDouble(String.valueOf(Integer
						.parseInt(lengmax[0])) + ".5");
			}

			if (Double.parseDouble(length) > lengthmin
					&& lengthmax >= Double.parseDouble(length)) {
				arr[0] = String.valueOf(lengthmin + 0.001);
				arr[1] = String.valueOf(lengthmax);
				break;
			}

		}
		return arr;
	}

	public String[] getNumber(String len) {
		String[] decimal = new String[2];
		int num = len.indexOf(".");
		decimal[0] = len.substring(0, num);
		decimal[1] = len.substring(num + 1);
		return decimal;
	}
}
