package com.steering.mes.mcp.Vrp;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.common.EntityTool;
import com.steering.mes.mcp.Mch.FrmMcpLoading;
import com.steering.mes.mcp.common.OfflineHelper;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 * 离线管理
 * 
 * @author TJG 2016/09/27
 */
public class FrmVrpDownLineManage extends DBCommon {
	/**
	 * 离线查询
	 * 
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryOffLine(ArrayList<String> list,
			String[] plineCode) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.doQueryOffLine");
		if (!list.get(0).equals("")) {
			sql += "  AND T.PRO_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 (!plineCode.equals("")) {
//			sql += " AND T.PLINE_CODE ='" + plineCode + "'";
//		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		if (!list.get(4).equals("")) {
			sql += " AND T.STATION_CODE ='" + list.get(4) + "'";
		}
		return this.getData(sql);
	}

	/**
	 * 脱单
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doTake(ArrayList<String> parm, String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		String sql = "";
		ResultSet rs = null;
		int acount = 0;

		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertSql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.insertVrpTakeReslut");// 插入脱单实绩表,脱单,回写PLN_ORDER_ZG_S
			PreparedStatement insertPsta = connection
					.prepareStatement(insertSql);

			for (String str : parm) {
				VrpOfflineResultEntity milOfflineEntity = EntityTool
						.getEntityByJSONStr(str, VrpOfflineResultEntity.class);

				// 查询是否已脱单
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.doQueryTake");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milOfflineEntity.getJudgeStoveNo());
				psta.setString(2, milOfflineEntity.getZyBatchId());
				psta.setString(3, milOfflineEntity.getStationCode());
				psta.setString(4, milOfflineEntity.getProPlanId());
				psta.setString(5, milOfflineEntity.getGxPlanNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
//				sql = CoreXmlSqlParsersFactory
//						.getSql("FrmVrpDownLineManage.doQueryTake2");
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milOfflineEntity.getJudgeStoveNo());
//				psta.setString(2, milOfflineEntity.getLotNo());
//				psta.setString(3, milOfflineEntity.getPlineCode());
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					acount += Integer.parseInt(rs.getString("ACOUNT"));
//				}
				rs.close();
				psta.close();
				if (acount != 0) {
					cro.setV_errMsg("此炉已脱单,不可再次脱单!");
					return cro;
				}

				if (milOfflineEntity.getProPlanId().equals("")) {
					cro.setV_errMsg("此炉已脱单,不可再次脱单!");
					return cro;
				}

				insertPsta.setString(1, milOfflineEntity.getJudgeStoveNo());
				insertPsta.setString(2, milOfflineEntity.getStationCode());
				insertPsta.setString(3, milOfflineEntity.getPlineCode());
				insertPsta.setString(4, userName);
				insertPsta.addBatch();
			}
			insertPsta.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 list
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQuery(String sortCode) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory.getSql("FrmVrpDownLineManage.doQuery");
		return this.getData(sql, new Object[] { sortCode });
	}

	/**
	 * 组炉实绩查询
	 * 
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryOffLineGroup(ArrayList<String> list,
			String[] plineCode) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.doQueryOffLineGroup");
		if (!list.get(0).equals("")) {
			sql += "  AND T.PRO_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 (!plineCode.equals("")) {
//			sql += " AND T.PLINE_CODE ='" + plineCode + "'";
//		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		if (!list.get(4).equals("")) {
			sql += " AND T.STATION_CODE ='" + list.get(4) + "'";
		}
		return this.getData(sql);
	}

	/**
	 * 返线实绩查询
	 * 
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryReturnLine(ArrayList<String> list,
			String[] plineCode) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.doQueryReturnLine");
		if (!list.get(0).equals("")) {
			sql += "  AND T.RE_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 (!plineCode.equals("")) {
//			sql += " AND T.PLINE_CODE ='" + plineCode + "'";
//		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		if (!list.get(4).equals("")) {
			sql += " AND T.STATION_CODE ='" + list.get(4) + "'";
		}
		return this.getData(sql);
	}

	/**
	 * 委外实绩查询
	 * 
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryOutSourc(ArrayList<String> list,
			String[] plineCode) {

		String sql = "";
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.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 (!plineCode.equals("")) {
//			sql += " AND T.PLINE_CODE ='" + plineCode + "'";
//		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		if (!list.get(4).equals("")) {
			sql += " AND T.SUBCONTRACT_LOG ='" + list.get(4) + "'";
		}
		return this.getData(sql);
	}

	/***
	 * 查询合同信息
	 * 
	 * @param orderNo
	 * @return
	 */
	public CoreReturnObject getOrder(String plineCode, String proId,
			String gxPlanId, String orderNo, String dimater, String height) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.getOrder");

		if (!dimater.equals("")) {
			sql += " AND T2.DIMATER ='" + dimater + "'";
		}
		if (!height.equals("")) {
			sql += " AND T2.HEIGHT ='" + height + "'";
		}
		sql += " ORDER BY T.PRO_PLAN_ID ASC";
		return this.getData(sql, new Object[] { plineCode, proId, gxPlanId,
				orderNo });
	}

	/**
	 * 组炉
	 * 
	 * @param result
	 * @return
	 */
	public CoreReturnObject doFurnace(ArrayList<String> parm,
			ArrayList<String> parm1, ArrayList<String> parm2) {
		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");
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyMMdd");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String addOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.AddOffLineResu");// 新增修改离线表组炉记录
			PreparedStatement pstaAddOff = con.prepareStatement(addOff);
			// String updatePort = CoreXmlSqlParsersFactory
			// .getSql("FrmVrpDownLineManage.UpdatePortVrp");// 修改管号队列表
			// PreparedStatement pstaUpdatePort =
			// con.prepareStatement(updatePort);
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateOffLineResu");// 修改离线表，新增组炉履历表
			PreparedStatement pstaUpdateOff = con.prepareStatement(updateOff);
			String zlJusNo = parm2.get(0);
			String lotNo = parm2.get(1);
			String name = parm2.get(2);
			String bc = parm2.get(4);
			String bz = parm2.get(3);
			String zlBatchNo = ""; // 组炉批号



			String sqlCheck = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.queryHttM");
			psta = con.prepareStatement(sqlCheck);
			psta.setObject(1, zlJusNo);
			rs = psta.executeQuery();
			if (rs.next()) {

				if(rs.getInt("COUNT")>0){
					throw new Exception("组炉炉号重复,不允许组炉!");
				}
			}
			rs.close();
			psta.close();

			VrpOfflineResultEntity tity = EntityTool.getEntityByJSONStr(parm1
					.get(0).toString(), VrpOfflineResultEntity.class);
			String groupId = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.QueryGroupResult");// 查询组操作Id
			psta = con.prepareStatement(sql);
			psta.setString(1, dateFormat2.format(now));
			psta.setString(2, dateFormat1.format(now));
			rs = psta.executeQuery();
			if (rs.next()) {
				groupId = rs.getString("GROUP_ID");
			}
			rs.close();
			psta.close();
			int groupXh = 01;
			int totalCount = 0;// 组炉总支数
			double totalWeight = 0;// 组炉总重量
			for (String str : parm) {
				VrpOfflineResultEntity vrpOfflineEntity = EntityTool
						.getEntityByJSONStr(str.toString(),
								VrpOfflineResultEntity.class);
				int count = 0;
				String offgroupId = "";// 离线组炉Id
				String groupStart = "";// 组炉状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryOffLineGroupStart");// 查询是否组炉
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getJudgeStoveNo());
				psta.setString(2, vrpOfflineEntity.getBatchNo());
				psta.setString(3, vrpOfflineEntity.getResultNo());
				psta.setString(4, vrpOfflineEntity.getHeatPlanNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					offgroupId = rs.getString("GROUP_ID");
					groupStart = rs.getString("GROUP_LOG");
					count = Integer.parseInt(rs.getString("COUNT_NUM"));
				}
				rs.close();
				psta.close();
				if (offgroupId != null) {
					if (!offgroupId.equals("")) {
						cor.setV_errMsg("此炉已组炉!");
						return cor;
					}
				}
				if (groupStart.equals("1")) {
					cor.setV_errMsg("此炉是组炉炉号,不允许组炉!");
					return cor;
				}

				if (Integer.parseInt(vrpOfflineEntity.getOfflineBranch()) > count) {
					cor.setV_errMsg("此炉组炉支数大于离线支数,不允许组炉!");
					return cor;
				}
				DecimalFormat df = new DecimalFormat("0.000");
				double weight = (Double.parseDouble(vrpOfflineEntity
						.getOfflineWt()) / Integer.parseInt(vrpOfflineEntity
						.getOfflineNum()))
						* Integer.parseInt(vrpOfflineEntity.getOfflineBranch());
				String weight1 = df.format(weight);
				totalCount += Integer.parseInt(vrpOfflineEntity
						.getOfflineBranch());
				totalWeight += Double.parseDouble(weight1);

				if(lotNo.equals("") || null == lotNo ){
					lotNo= vrpOfflineEntity.getBatchNo();
				}

				// 修改组炉支数
				pstaUpdateOff.setObject(1, vrpOfflineEntity.getOfflineBranch());
				pstaUpdateOff.setObject(2, String.valueOf(weight1));
				pstaUpdateOff.setObject(3, systime);
				pstaUpdateOff.setObject(4, name);
				pstaUpdateOff.setObject(5, vrpOfflineEntity.getResultNo());
				// 新增组炉履历表
				pstaUpdateOff.setObject(6, groupId);
				pstaUpdateOff.setObject(7,
						StringTool.formatDecimal(groupXh, "00"));
				pstaUpdateOff.setObject(8, zlJusNo);
				pstaUpdateOff.setObject(9, lotNo);
				pstaUpdateOff.setObject(10, vrpOfflineEntity.getJudgeStoveNo());
				pstaUpdateOff.setObject(11, vrpOfflineEntity.getBatchNo());
				pstaUpdateOff
						.setObject(12, vrpOfflineEntity.getOfflineBranch());
				pstaUpdateOff.setObject(13, String.valueOf(weight1));// 重量
				pstaUpdateOff.setObject(14, name);
				pstaUpdateOff.setObject(15, bc);
				pstaUpdateOff.setObject(16, bz);
				pstaUpdateOff.setObject(17, systime);
				pstaUpdateOff.setObject(18, vrpOfflineEntity.getResultNo());
				pstaUpdateOff.addBatch();
				groupXh++;
			}
			pstaUpdateOff.executeBatch();



			String resultNo = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.QueryOffLineMaxResultNo");// 查询离线最大实绩编号
			psta = con.prepareStatement(sqlloading);
			psta.setObject(1, tity.getPlineCode()  + tity.getStationCode() + "L" + dateFormat2.format(now));
			psta.setObject(2,tity.getPlineCode()  + tity.getStationCode() + "L" + dateFormat2.format(now));
			rs = psta.executeQuery();
			if (rs.next()) {
				resultNo = rs.getString("RESULT_NO");
			}
			rs.close();
			psta.close();

			// 新增离线实绩表
			pstaAddOff.setObject(1, resultNo);
			pstaAddOff.setObject(2, tity.getPlineCode());
			pstaAddOff.setObject(3, tity.getPlineName());
			pstaAddOff.setObject(4, tity.getStationCode());
			pstaAddOff.setObject(5, tity.getStationDesc());
			pstaAddOff.setObject(6, "");// 炉计划
			pstaAddOff.setObject(7, tity.getZyBatchId());// 生产批号
			pstaAddOff.setObject(8, tity.getProPlanId());
			pstaAddOff.setObject(9, tity.getGxPlanNo());
			pstaAddOff.setObject(10, zlJusNo);// 组炉炉号
			pstaAddOff.setObject(11, lotNo);// 批号
			pstaAddOff.setObject(12, tity.getProduccode());
			pstaAddOff.setObject(13, tity.getProducname());
			pstaAddOff.setObject(14, tity.getSteelcode());
			pstaAddOff.setObject(15, tity.getSteelname());
			pstaAddOff.setObject(16, tity.getGradecode());
			pstaAddOff.setObject(17, tity.getGradename());
			pstaAddOff.setObject(18, tity.getModelCode());
			pstaAddOff.setObject(19, tity.getModelDesc());
			pstaAddOff.setObject(20, tity.getStdCode());
			pstaAddOff.setObject(21, tity.getStdName());
			pstaAddOff.setObject(22, tity.getStdStyle());
			pstaAddOff.setObject(23, tity.getStdStyleDesc());
			pstaAddOff.setObject(24, tity.getActDimater());
			pstaAddOff.setObject(25, tity.getActHeight());
			pstaAddOff.setObject(26, tity.getActLen());
			pstaAddOff.setObject(27, tity.getActLenMin());
			pstaAddOff.setObject(28, tity.getActLenMax());
			pstaAddOff.setObject(29, tity.getJudgeStoveNo());
			pstaAddOff.setObject(30, tity.getBatchNo());
			pstaAddOff.setObject(31, totalCount);
			pstaAddOff.setObject(32, totalWeight);
			pstaAddOff.setObject(33, name);
			pstaAddOff.setObject(34, bc);
			pstaAddOff.setObject(35, bz);
			pstaAddOff.setObject(36, systime);
			pstaAddOff.setObject(37, groupId);
			pstaAddOff.setObject(38, tity.getOrdLnDlyPk());
			pstaAddOff.setObject(39, tity.getOrderNo());
			pstaAddOff.setObject(40, tity.getOrderSeq());
			pstaAddOff.setObject(41, tity.getDeliveryNo());
			pstaAddOff.setObject(42, tity.getHeatPlanNo());
			pstaAddOff.setObject(43, tity.getBatchGroudNo());
			pstaAddOff.addBatch();
			pstaAddOff.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
	 */
	public CoreReturnObject doCancelFurnace(ArrayList<String> 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 addDelete = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.DeleteOffLineResu");// 删除离线表组炉记录
			PreparedStatement pstaAddDelete = con.prepareStatement(addDelete);
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.DaeleteGroupResu");// 修改离线表，删除组炉履历表
			PreparedStatement pstaUpdateOff = con.prepareStatement(updateOff);

			for (String str : parm) {
				VrpOfflineResultEntity vrpOfflineEntity = EntityTool
						.getEntityByJSONStr(str.toString(),
								VrpOfflineResultEntity.class);
				int count = 0;
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryOffLineGroupId");// 查询组炉信息
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getResultNo());
				psta.setString(2, vrpOfflineEntity.getGroupId());
				rs = psta.executeQuery();
				if (rs.next()) {
					count = Integer.parseInt(rs.getString("COUNT"));
				}
				rs.close();
				psta.close();
				if (count == 0) {
					cor.setV_errMsg("此组炉信息已撤销,不允许此操作!");
					return cor;
				}
				int remCount = 0;// 返线支数
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryOffLineGroupStart");// 查询是否组炉
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getJudgeStoveNo());
				psta.setString(2, vrpOfflineEntity.getBatchNo());
				psta.setString(3, vrpOfflineEntity.getResultNo());
				psta.setString(4, vrpOfflineEntity.getHeatPlanNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					remCount = Integer.parseInt(rs.getString("RE_NUM"));
				}
				rs.close();
				psta.close();
				if (remCount == 0) {
					cor.setV_errMsg("此炉已返线!");
					return cor;
				}
				List<VrpGroupResultEntity> list = new ArrayList<VrpGroupResultEntity>();
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryGroupVrpResult");// 查询组炉履历表信息
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getGroupId());
				rs = psta.executeQuery();
				list = EntityTool
						.getEntitysByRs(rs, VrpGroupResultEntity.class);
				rs.close();
				psta.close();
				for (VrpGroupResultEntity groupList : list) {

					// 修改组炉支数
					pstaUpdateOff.setObject(1, groupList.getGroupNum());
					pstaUpdateOff.setObject(2, groupList.getGroupWt());
					pstaUpdateOff.setObject(3, "");
					pstaUpdateOff.setObject(4, "");
					pstaUpdateOff.setObject(5, groupList.getLxResultNo());
					// 删除组炉履历表
					pstaUpdateOff.setObject(6, groupList.getGroupId());
					pstaUpdateOff.setObject(7, groupList.getGroupXh());
					pstaUpdateOff.addBatch();
				}
				pstaUpdateOff.executeBatch();

				pstaAddDelete.setObject(1, vrpOfflineEntity.getResultNo());
				pstaAddDelete.setObject(2, vrpOfflineEntity.getGroupId());
				pstaAddDelete.addBatch();

			}
			pstaAddDelete.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
	 */
	public CoreReturnObject doOnlineComm(ArrayList<String> parm,
			ArrayList<String> parm1) {
		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 updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateOffLineComm");// 修改离线表数据（改判）
			PreparedStatement pstaUpdateOff = con.prepareStatement(updateOff);

			String name = parm1.get(2);
			for (String str : parm) {
				VrpOfflineResultEntity vrpOfflineEntity = EntityTool
						.getEntityByJSONStr(str.toString(),
								VrpOfflineResultEntity.class);
				int count = 0;
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryOffLineGroupStart");// 查询是否组炉
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getJudgeStoveNo());
				psta.setString(2, vrpOfflineEntity.getBatchNo());
				psta.setString(3, vrpOfflineEntity.getResultNo());
				psta.setString(4, vrpOfflineEntity.getHeatPlanNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					count = Integer.parseInt(rs.getString("RE_NUM"));
				}
				rs.close();
				psta.close();
				if (count == 0) {
					cor.setV_errMsg("已返线,不允许改判！");
					return cor;
				}
				// 在线改判
				pstaUpdateOff.setObject(1, vrpOfflineEntity.getProduccode());
				pstaUpdateOff.setObject(2, vrpOfflineEntity.getProducname());
				pstaUpdateOff.setObject(3, vrpOfflineEntity.getSteelcode());
				pstaUpdateOff.setObject(4, vrpOfflineEntity.getSteelname());
				pstaUpdateOff.setObject(5, vrpOfflineEntity.getModelCode());
				pstaUpdateOff.setObject(6, vrpOfflineEntity.getModelDesc());
				pstaUpdateOff.setObject(7, vrpOfflineEntity.getStdCode());
				pstaUpdateOff.setObject(8, vrpOfflineEntity.getStdName());
				pstaUpdateOff.setObject(9, vrpOfflineEntity.getStdStyle());
				pstaUpdateOff.setObject(10, vrpOfflineEntity.getStdStyleDesc());
				pstaUpdateOff.setObject(11, vrpOfflineEntity.getActDimater());
				pstaUpdateOff.setObject(12, vrpOfflineEntity.getActHeight());
				pstaUpdateOff.setObject(13, vrpOfflineEntity.getActLen());
				pstaUpdateOff.setObject(14, vrpOfflineEntity.getOrdLnDlyPk());
				pstaUpdateOff.setObject(15, vrpOfflineEntity.getOrderNo());
				pstaUpdateOff.setObject(16, vrpOfflineEntity.getOrderSeq());
				pstaUpdateOff.setObject(17, vrpOfflineEntity.getDeliveryNo());
				pstaUpdateOff.setObject(18, systime);
				pstaUpdateOff.setObject(19, name);
				pstaUpdateOff.setObject(20, vrpOfflineEntity.getProPlanId());
				pstaUpdateOff.setObject(21, vrpOfflineEntity.getGxPlanNo());
				pstaUpdateOff.setObject(22, vrpOfflineEntity.getResultNo());
				pstaUpdateOff.addBatch();
			}
			pstaUpdateOff.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 conn
	 *            c
	 * @param strLcode
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryMatNoM(Connection conn, String strLcode, String strBatch)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.queryMatNoM");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strLcode);
		psta.setString(2, strBatch);
		psta.setString(3, strLcode);
		psta.setString(4, strBatch);
		psta.setString(5, strLcode);
		psta.setString(6, strBatch);
		psta.setString(7, strLcode);
		psta.setString(8, strBatch);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("MAT_NO");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 获取最大计划编号
	 * 
	 * @param conn
	 *            c
	 * @throws Exception
	 *             异常处理
	 */
	public static String queryPlanMaxNo(Connection conn) throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.queryPlanMaxNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, matter1.format(dt));
		psta.setString(2, matter.format(dt));
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("HEAT_PLAN_NO");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 获计划状态
	 * 
	 * @param conn
	 *            c
	 * @throws Exception
	 *             异常处理
	 */
	public static String queryPlnZYDbkStrat(Connection conn, String heraNo)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.doQueryPlanStrat");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, heraNo);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("PLANSTATUS");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 返线
	 * 
	 * @param result
	 * @return
	 */
	public CoreReturnObject doReturnLine(ArrayList<String> parm,
			ArrayList<String> parm1, Integer totalNum) {
		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");
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyMMdd");
		String systime = dateFormat.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String yearMoth = "";//改25号结转
		
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String addOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.AddReturnResu");// 新增镦拔扩计划表
			PreparedStatement pstaAddOff = con.prepareStatement(addOff);
			String addOffC = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.AddReturnResuC");// 新增加工线计划从表
			PreparedStatement pstaAddOffC = con.prepareStatement(addOffC);
			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdatePortVrpReturn");// 修改管号队列表
			PreparedStatement pstaUpdatePort = con.prepareStatement(updatePort);
			String addPort = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.AddPortVrpReturn");// 新增管号队列表
			PreparedStatement pstaAddPort = con.prepareStatement(addPort);


			/*
			 * String addMat = CoreXmlSqlParsersFactory
			 * .getSql("FrmVrpDownLineManage.AddMatReturn");// 新增材料表
			 * PreparedStatement pstaAddMat = con.prepareStatement(addMat);
			 * String addButt = CoreXmlSqlParsersFactory
			 * .getSql("FrmVrpDownLineManage.AddButtReturn");// 新增位置表
			 * PreparedStatement pstaAddButt = con.prepareStatement(addButt);
			 */
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateOff");// 修改离线表返线支数
			PreparedStatement pstaupdateOff = con.prepareStatement(updateOff);
			String updateStatus = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateStatus");// 修改组炉返线材料状态30
			PreparedStatement pstaupdateStatus = con
					.prepareStatement(updateStatus);


			String updatePlnStatus = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdatePlanStatus");// 修改离线表返线支数
			PreparedStatement pupdatePlnStatus = con
					.prepareStatement(updatePlnStatus);
			// String addfx = CoreXmlSqlParsersFactory
			// .getSql("FrmVrpExpandResult.AddComInfoResult");// 新增返线公共信息
			// PreparedStatement patsPlan = con.prepareStatement(addfx);
			String plineCode = parm1.get(0);
			yearMoth = com.steering.common.StringTool.GetBalYear(com.steering.common.StringTool.GetSupplyUnit(plineCode));//改25号结转
			String name = parm1.get(1);
			String bz = parm1.get(2);
			String bc = parm1.get(3);
			String turnLinGx = parm1.get(4);// 返线工序点
			String storageNo = parm1.get(5);// 仓库
			String turnGxName = parm1.get(8);
			String slgx = parm1.get(9);// 上料工序点
			String xkgx = parm1.get(10);// 旋库工序点
			String bjgx = parm1.get(11);// 表检工序点
			String djgx = parm1.get(12);// 定径工序点
			String yzgx = parm1.get(13);// 匀整工序点
			String cldgx = parm1.get(14);// 测量点工序点
			String IsInstore = parm1.size()>15?parm1.get(15): "0";

			ArrayList<String> HeatPlanIds = new ArrayList<String>();

			for (String str : parm) {
				VrpOfflineResultEntity vrpOfflineEntity = EntityTool
						.getEntityByJSONStr(str.toString(),
								VrpOfflineResultEntity.class);

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.doQueryOffLine2");// 查询是否组炉
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getResultNo());
				rs = psta.executeQuery();
				String OfflineBranch = vrpOfflineEntity.getOfflineBranch();
				List<VrpOfflineResultEntity> data = EntityTool.getEntitysByRs(
						rs, VrpOfflineResultEntity.class);

				psta.close();
				rs.close();
				if (data.size() <= 0) {
					cor.setV_errMsg("离线数据不存在,请刷新数据!");
					return cor;
				}

				vrpOfflineEntity = data.get(0);
				vrpOfflineEntity.setOfflineBranch(OfflineBranch);

				DecimalFormat df = new DecimalFormat("0.000");
				double weight = (Double.parseDouble(vrpOfflineEntity
						.getOfflineWt()) / Integer.parseInt(vrpOfflineEntity
						.getOfflineNum()))
						* Integer.parseInt(vrpOfflineEntity.getOfflineBranch());
				String weight1 = df.format(weight);
				int count = 0;
				int remCount = 0;// 返线支数
				// String offgroupId = "";// 离线组炉Id
				String groupStart = "";// 组炉状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryOffLineGroupStart");// 查询是否组炉
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getJudgeStoveNo());
				psta.setString(2, vrpOfflineEntity.getBatchNo());
				psta.setString(3, vrpOfflineEntity.getResultNo());
				psta.setString(4, vrpOfflineEntity.getHeatPlanNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					// offgroupId = rs.getString("GROUP_ID");
					groupStart = rs.getString("GROUP_LOG");
					count = Integer.parseInt(rs.getString("COUNT_NUM"));
					remCount = Integer.parseInt(rs.getString("RE_NUM"));
				}
				rs.close();
				psta.close();
				if (remCount == 0) {
					cor.setV_errMsg("此炉已返线!");
					return cor;
				}
//				if (Integer.parseInt(vrpOfflineEntity.getTotalcount()) < Integer
//						.parseInt(vrpOfflineEntity.getOfflineBranch())) {
//					cor.setV_errMsg("此炉返线支数大于离线支数,不允许返线!");
//					return cor;
//				}
				String matNoSqe = queryMatNoM(con,
						vrpOfflineEntity.getJudgeStoveNo(),
						vrpOfflineEntity.getBatchNo()); // 材料号
				int g = Integer.parseInt(matNoSqe.substring(
						matNoSqe.length() - 4, matNoSqe.length()));

				// String resultNo = "";
				// String sqlloading = CoreXmlSqlParsersFactory
				// .getSql("FrmVrpDownLineManage.doQueryComInfo");// 查询最大实绩编号
				// psta = con.prepareStatement(sqlloading);
				// psta.setObject(1, plineCode + slgx + "F"
				// + dateFormat2.format(now));
				// psta.setObject(2, dateFormat1.format(now));
				// psta.setObject(3, plineCode);
				// ResultSet rs1 = psta.executeQuery();
				// if (rs1.next()) {
				// resultNo = rs1.getString("RESULT_NO");
				// }
				// rs1.close();
				// psta.close();
				// 获取最大镦拔扩计划编号

				String inDoc = "";
				if(IsInstore.equals("1")){

					inDoc =  FrmMcpLoading.querySendIn(con, "800701");
				}

				if (groupStart.equals("1")) {

					List<VrpGroupResultEntity> list = new ArrayList<VrpGroupResultEntity>();
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.QueryGroupVrpResult");// 查询组炉履历表信息
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getGroupId());
					rs = psta.executeQuery();
					list = EntityTool.getEntitysByRs(rs,
							VrpGroupResultEntity.class);
					rs.close();
					psta.close();
					List<PortVrpBatchSampleResultEntity> matNo = new ArrayList<PortVrpBatchSampleResultEntity>();

					for (VrpGroupResultEntity groupList : list) {
						sql = CoreXmlSqlParsersFactory
								.getSql("FrmVrpDownLineManage.QueryOffMatNo2");// 查询管号队列离线材料号
						psta = con.prepareStatement(sql);
						psta.setString(1, groupList.getLxResultNo());
						rs = psta.executeQuery();
						List<PortVrpBatchSampleResultEntity> matNo1 =  EntityTool.getEntitysByRs(rs,
								PortVrpBatchSampleResultEntity.class);
						matNo.addAll(matNo1);
						rs.close();
						psta.close();

						/*if(matNo1.size()!= Integer.parseInt(groupList.getGroupNum())){
							cor.setV_errMsg("组炉数据异常！");
							return cor;
						}*/
					}

					if (matNo.size() == 0) {
						cor.setV_errMsg("管号队列无对应的材料！");
						return cor;
					}




					PlnZyDbkCEntity PlnZyDbkCEntity = PlanService
							.AddNewDBKPlan(con, vrpOfflineEntity, name);
										

					HeatPlanIds.add(PlnZyDbkCEntity.getHeatPlanNo() + "," + PlnZyDbkCEntity.getProPlanId()  + "," +  PlnZyDbkCEntity.getGxPlanNo());


					PlnZyDbkMEntity OldPln = PlanService.getSendDBKPlanCInfo(con,
							vrpOfflineEntity.getZyBatchId1());

					if (OldPln == null) {
						throw new Exception("找不到原炉计划信息!");
					}


					String ordPk = "";
					String ordLnPk = "";
					String belongCode ="";
					String belongName= "";

					if(IsInstore.equals("1")){
						sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.selectBelong");
						sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
						psta = con.prepareStatement(sql);
						rs = psta.executeQuery();
						if(rs.next())
						{
							belongCode = rs.getString("DEPARTMENT_CODE");
							belongName = rs.getString("DEPARTMENT_DESC");
						}
						rs.close();
						psta.close();


						sql = CoreXmlSqlParsersFactory
								.getSql("FrmVrpInStore.selectOrdPk");
						sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
						psta = con.prepareStatement(sql);
						rs = psta.executeQuery();
						if(rs.next())
						{
							ordPk = rs.getString("ORD_PK");
							ordLnPk = rs.getString("ORD_LN_PK");
						}
						rs.close();
						psta.close();
					}




					for (int i = 0; i < matNo.size(); i++) {

									// 新增管号队列表
									pstaAddPort.setObject(1,
											PlnZyDbkCEntity.getHeatPlanNo());
									pstaAddPort.setObject(
											2,
											vrpOfflineEntity.getJudgeStoveNo()
													+ vrpOfflineEntity
															.getBatchNo()
													+ StringTool.formatDecimal(
															i, "0000"));
									pstaAddPort.setObject(3, "");
									pstaAddPort.setObject(4, "");
									pstaAddPort.setObject(5,
											vrpOfflineEntity.getZyBatchId());
									pstaAddPort.setObject(6, "");
									pstaAddPort.setObject(7,
											vrpOfflineEntity.getPlineCode());
									pstaAddPort.setObject(8,
											vrpOfflineEntity.getPlineName());
									pstaAddPort.setObject(9,
											vrpOfflineEntity.getJudgeStoveNo());
									pstaAddPort.setObject(10,
											matNo.get(i).getBatchNo());
									pstaAddPort.setObject(11, "001");
									pstaAddPort.setObject(12, PlnZyDbkCEntity.getStoveNo());
									pstaAddPort.setObject(13, "");
									pstaAddPort.setObject(14,
											vrpOfflineEntity.getActDimater());
									pstaAddPort.setObject(15,
											vrpOfflineEntity.getActHeight());
									pstaAddPort.setObject(16,
											matNo.get(i).getActLen());
									pstaAddPort.setObject(17,
											matNo.get(i).getActLenMin());
									pstaAddPort.setObject(18,
											matNo.get(i).getActLenMax());
									pstaAddPort.setObject(19, "1");
									pstaAddPort.setObject(20,matNo.get(i).getActWeight());
									pstaAddPort.setObject(21, matNo.get(i).getActTheoryWeight());
									pstaAddPort.setObject(22, turnLinGx);
									pstaAddPort.setObject(23, "20");// 离线标志
									pstaAddPort.setObject(24, name);
									pstaAddPort.setObject(25, bc);
									pstaAddPort.setObject(26, bz);
									pstaAddPort.setObject(27, systime);
									if (vrpOfflineEntity.getPlineCode().equals(
											"C018")) {
										pstaAddPort.setObject(28, slgx + "-"
												+ xkgx + "-" + yzgx + "-"
												+ djgx + "-" + "0" + "-" + bjgx
												+ "-" + "0");
									} else {
										pstaAddPort.setObject(28, slgx + "-"
												+ cldgx + "-" + "0-" + bjgx
												+ "-" + "0");
									}
									pstaAddPort.setObject(29, turnLinGx);
									pstaAddPort.setObject(30, "00");// 材料状态
									pstaAddPort.setObject(31,
											vrpOfflineEntity.getBatchNo());// 材料状态
//									pstaAddPort.setObject(32,
//											FeedStoveNo.get(countFeedNum++));// 材料状态
									pstaAddPort.setObject(32, PlnZyDbkCEntity.getFeedHeatNo());
									pstaAddPort.setObject(33,
											PlnZyDbkCEntity.getHeatPlanSqe());// 材料状态
						pstaAddPort.setObject(34,
								IsInstore);// 材料状态
						pstaAddPort.setObject(35,
								yearMoth);// 材料状态
						
									pstaAddPort.addBatch();



						pstaupdateStatus.setObject(1,matNo.get(i).getHeatPlanNo());
						pstaupdateStatus.setObject(2, matNo.get(i).getMatNo());
						pstaupdateStatus.addBatch();

						if(IsInstore.equals("0")) {
							PlanService.DeleteOffLineMat(con,matNo.get(i).getMatNo(),true,vrpOfflineEntity.getPlineCode());
						}
						else {
							//region 返线入库 写入库记录  更新库存数据

							MatBcMEntity MatBcMEntity = OfflineHelper.getMatBcM(con,matNo.get(i).getMatNo());
							if (MatBcMEntity==null || MatBcMEntity.getMatNo().equals("")){
								throw new Exception("找不到待处理库存!");
							}
							if(MatBcMEntity.getStorageNo().equals("")){
								throw new Exception("离线管正在倒运途中,不允许此操作!");
							}


							MatBcMEntity matBcEntity = new MatBcMEntity();
							matBcEntity.setMatNo(matNo.get(i).getMatNo());
							//matBcEntity.setTestCount(String.valueOf(n));
							matBcEntity.setInstockDoc(inDoc);
							matBcEntity.setInstockTypeCode("800701");
							matBcEntity.setInstockTypeName("产出入库");
							matBcEntity.setLaryNo("1");
							matBcEntity.setLotNo(OldPln.getZyBatchId());
							matBcEntity.setSampleFlag(matNo.get(i).getIsSamplePipe());

							if (!OldPln.getDecision().equals("A")) {
								matBcEntity.setProductFlag("801401");
								matBcEntity.setMatStatus("80150101");
							} else {
								matBcEntity.setProductFlag("801402");
								matBcEntity.setMatStatus("80150201");
							}
							matBcEntity.setJudgeStoveNo(vrpOfflineEntity.getJudgeStoveNo());


							matBcEntity.setBatchNo(vrpOfflineEntity.getBatchNo());
							matBcEntity.setMaterialNo(OldPln.getOutwlId());
							matBcEntity.setMaterialName(OldPln.getOutwlDesc());
							matBcEntity.setProduccode(OldPln.getProduccode());
							matBcEntity.setProducname(OldPln.getProducname());
							matBcEntity.setSteelcode(OldPln.getSteelcode());
							matBcEntity.setSteelname(OldPln.getSteelname());
							matBcEntity.setStdStyle(OldPln.getStdStyle());
							matBcEntity.setStdStyleDesc(OldPln.getStdStyleDesc());
							matBcEntity.setSpecCode(OldPln.getSpecCode());
							matBcEntity.setSpecName(OldPln.getSpecName());
							matBcEntity.setModelCode(OldPln.getModelCode());
							matBcEntity.setModelDesc(OldPln.getModelDesc());
							matBcEntity.setGradecode(OldPln.getGradecode());
							matBcEntity.setGradename(OldPln.getGradename());
							matBcEntity.setStdCode("");
							matBcEntity.setStdName(OldPln.getProdStandard());
							matBcEntity.setProOrderNo(OldPln.getOrderNo()+"/"+OldPln.getOrderSeq());
							matBcEntity.setProcessCdoe("E");
							matBcEntity.setProcessDesc("镦拔扩");
							matBcEntity
									.setNextProcessCdoe(OldPln.getNextProcessCode());
							matBcEntity.setNextProcessDesc("");
							matBcEntity.setPlineCode(OldPln.getPlineCode());
							matBcEntity.setPlineName(OldPln.getPlineName());
							matBcEntity.setMatOriginCode("801302");
							matBcEntity.setMatOriginName("产线");
							matBcEntity.setActDimater(OldPln.getAimoutdiameter());
							matBcEntity.setActHeight(OldPln.getAimwallthick());
							matBcEntity.setActLenMin(matNo.get(i).getActLenMin());
							matBcEntity.setActLenMax(matNo.get(i).getActLenMax());
							matBcEntity.setBelongCode(belongCode);
							matBcEntity.setBelongName(belongName);
							matBcEntity.setCreateName(name);
							matBcEntity.setCreateTime(systime);

							matBcEntity.setBalYearMonth(yearMoth);
							matBcEntity.setJudgeStdCode("");
							matBcEntity.setJudgeStdName(OldPln.getOutputStandard());
							matBcEntity.setGxPlanNo(OldPln.getGxPlanNo());
							matBcEntity.setQcmStaus("00");
							matBcEntity.setHeatPlanNo(OldPln.getHeatPlanNo());
							matBcEntity.setActCount(matNo.get(i).getActCount());
							matBcEntity.setProOrderId(OldPln.getProPlanId());
							matBcEntity.setGxPlanNo(OldPln.getGxPlanNo());
							matBcEntity.setPhyBatchNo(matNo.get(i).getPhyBatchNo());
							matBcEntity.setIsSamplePipe(matNo.get(i).getIsSamplePipe());
							matBcEntity.setCrafNo(OldPln.getCraftFileNo());
							matBcEntity.setCraftSeq(OldPln.getCraftSeq());
							matBcEntity.setOrdLnDlyPk(OldPln.getOrdLnDlyPk());
							matBcEntity.setOrderNo(OldPln.getOrderNo());
							matBcEntity.setOrderSeq(OldPln.getOrderSeq());
							matBcEntity.setOrderSeqNo(OldPln.getDeliveryNo());
							matBcEntity.setMatGowhereCode(OldPln.getGowhereCode());
							matBcEntity.setMatGowhereName(OldPln.getGowhereName());
							matBcEntity.setStoveNo(OldPln.getStoveNo());
							matBcEntity.setFlagStoveNoZg(matNo.get(i).getFlagStoveNo());
							matBcEntity.setTarget(OldPln.getDecision()); //判定字头
							matBcEntity.setInBc(bc);
							matBcEntity.setInBz(bz);
							matBcEntity.setActLen(matNo.get(i).getActLen());
							matBcEntity.setActWeight(matNo.get(i).getActWeight());
							matBcEntity.setActTheoryWeight(matNo.get(i).getActWeight());


							if ((OldPln.getCic() + "+" + OldPln.getPic() + "+"
									+ OldPln.getSic() + "+" + OldPln.getDic())
									.equals("++++")) {
								matBcEntity.setJudgeStdName("");
							} else {
								if (OldPln.getIfProduct().equals("1")) {
									matBcEntity.setJudgeStdName(OldPln.getCic() + "+"
											+ OldPln.getPic() + "+"
											+ OldPln.getSic() + "+"
											+ OldPln.getDic());
								} else {
									matBcEntity.setJudgeStdName("" + "+"
											+ OldPln.getPic() + "+"
											+ OldPln.getSic() + "+"
											+ OldPln.getDic());
								}
							}

							matBcEntity.setCic(OldPln.getCic());
							matBcEntity.setHeatPlanNoOld(OldPln.getHeatPlanNo());
							if(ordPk==null||ordPk.equals(""))
							{
								matBcEntity.setOrdPk("");
							}else{
								matBcEntity.setOrdPk(ordPk);
							}
							if(ordLnPk==null||ordLnPk.equals(""))
							{
								matBcEntity.setOrdLnPk("");
							}else{
								matBcEntity.setOrdLnPk(ordLnPk);
							}


							String updateMatsql = CoreXmlSqlParsersFactory
									.getSql("FrmMchInStore.updateMatBcM");// 返线交库 更改材料信息
							updateMatsql = com.steering.comm.converter.EntityTool.fillSqlByEntity(updateMatsql, matBcEntity);
							PreparedStatement pstaUpdateMatsql= con.prepareStatement(updateMatsql);
							pstaUpdateMatsql.addBatch();
							pstaUpdateMatsql.executeBatch();


							//endregion
						}

					}
					// pstaAddMat.executeBatch();
					/* pstaAddButt.executeBatch(); */
					pstaAddPort.executeBatch();

					pstaupdateOff.setObject(
							1,
							Integer.parseInt(vrpOfflineEntity
									.getOfflineBranch())
									+ Integer.parseInt(vrpOfflineEntity
											.getReNum()));
					pstaupdateOff.setObject(2, Double.parseDouble(weight1)
							+ Double.parseDouble(vrpOfflineEntity.getReWt()));
					pstaupdateOff.setObject(3, systime);
					pstaupdateOff.setObject(4, name);
					pstaupdateOff.setObject(5, turnLinGx);
					pstaupdateOff.setObject(6, PlnZyDbkCEntity.getHeatPlanNo());
					pstaupdateOff.setObject(7, vrpOfflineEntity.getResultNo());
					pstaupdateOff.addBatch();


					pupdatePlnStatus.setObject(1,
							vrpOfflineEntity.getZyBatchId1());
					pupdatePlnStatus.addBatch();
					pupdatePlnStatus.executeBatch();


					pstaupdateStatus.executeBatch();

					if(IsInstore.equals("1")){
						String updateMatsql = CoreXmlSqlParsersFactory
								.getSql("FrmVrpInStore.insertVrpComInfo2");// 返线入库  插入公共信息表
						PreparedStatement pstaUpdateMatsql= con.prepareStatement(updateMatsql);
						pstaUpdateMatsql.setObject(1,inDoc);
						pstaUpdateMatsql.setObject(2, PlnZyDbkCEntity.getHeatPlanNo());
						pstaUpdateMatsql.addBatch();
						pstaUpdateMatsql.executeBatch();


						String matGpNum = "";// 已分配到订单支数
						int gpreqCount = 0;// 未分配支数
						int inCount = matNo.size();

						if (!OldPln.getNextProcessCode().equals("")) {

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

							} else if (OldPln.getNextProcessCode().equals("G")) {
								sql = CoreXmlSqlParsersFactory
										.getSql("FrmVrpInStore.selectPlnZyJg");
							} else if (OldPln.getNextProcessCode().equals("H")) {
								sql = CoreXmlSqlParsersFactory
										.getSql("FrmVrpInStore.selectPlnZyJgm");
							}
							sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
							psta = con.prepareStatement(sql);
							rs = psta.executeQuery();
							List<PlnZyRclMEntity1> plnOrderZy = com.steering.comm.converter.EntityTool.getEntitysByRs(
									rs, PlnZyRclMEntity1.class);
							rs.close();
							psta.close();
							String codeBl = "";
							if (OldPln.getNextProcessCode().equals("H")) {
								sql = CoreXmlSqlParsersFactory
										.getSql("FrmVrpInStore.selectPlnZyJgmProp");
								sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
								psta = con.prepareStatement(sql);
								rs = psta.executeQuery();
								if (rs.next()) {
									codeBl = rs.getString("CODE_JG_BL");
								}
								rs.close();
								psta.close();
							}
							if (plnOrderZy.size() > 0) {
								for (int i = 0; i < plnOrderZy.size(); i++) {
									// 该订单下已分配多少支
									sql = CoreXmlSqlParsersFactory
											.getSql("FrmVrpInStore.selectMatGpMNumZg");
									sql = com.steering.comm.converter.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);

									MatBcMEntity matEntity = new MatBcMEntity();
									matEntity
											.setJudgeStoveNo(PlnZyDbkCEntity.getJudgeStoveNo());
									matEntity.setProOrderId(OldPln.getProPlanId());
									matEntity.setGxPlanNo(OldPln.getGxPlanNo());
									matEntity.setHeatPlanNo(OldPln.getHeatPlanNo());
									matEntity.setProPlanIdNext(plnOrderZy.get(i)
											.getProPlanId());
									matEntity.setGxPlanNoNext(plnOrderZy.get(i)
											.getGxPlanNo());
									matEntity.setHeatPlanNoNext(plnOrderZy.get(i)
											.getHeatPlanNo());
									matEntity.setPlineCode(OldPln.getPlineCode());
									matEntity.setCodeJgBl(codeBl);

									if (i == plnOrderZy.size() - 1) {
										matEntity.setActCount(String.valueOf(inCount));
									} else if (inCount < gpreqCount) {
										matEntity.setActCount(String.valueOf(inCount));
									} else {
										matEntity.setActCount(String.valueOf(gpreqCount));
									}
									String updateMatBcMSql = CoreXmlSqlParsersFactory
											.getSql("FrmVrpInStore.updateMatBcM");
									updateMatBcMSql = com.steering.comm.converter.EntityTool.fillSqlByEntity(
											updateMatBcMSql, matEntity);
									PreparedStatement pstaupdateMatBcMSql= con.prepareStatement(updateMatBcMSql);
									pstaupdateMatBcMSql.addBatch();
									pstaupdateMatBcMSql.executeBatch();
									inCount = inCount - gpreqCount;
									if (inCount <= 0 && i + 1 < plnOrderZy.size()) {
										break;
									}

								}
							}
						}

					}


				} else {
//					//查询管号队列
//					
//					 //获取镦拔扩计划从表实体类
//		            PlnZyDbkCEntity plnC = PlanService.getSendDBKPlanCInfoC(con,
//		            		vrpOfflineEntity.getHeatPlanNo(),hertSqe);
//					



					PlnZyDbkMEntity OldPln = PlanService.getSendDBKPlanCInfo(con,
							vrpOfflineEntity.getHeatPlanNo());

					if (OldPln == null) {
						throw new Exception("找不到炉计划信息!");
					}


					if (OldPln.getPlanstatus().equals("30")) {
						cor.setV_errMsg("该炉对应的计划已结炉,不允许次操作！");
						return cor;
					}
					if (OldPln.getPlanstatus().equals("40")) {
						cor.setV_errMsg("该炉对应的计划已关闭,不允许次操作！");
						return cor;
					}


					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.QueryOffMatNo2");// 查询管号队列离线材料号
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getResultNo());
					rs = psta.executeQuery();
					ArrayList<PortVrpBatchSampleResultEntity> ListPort =  EntityTool
							.getEntitysByRs(rs, PortVrpBatchSampleResultEntity.class);
					rs.close();
					psta.close();


					if (ListPort.size() == 0) {
						cor.setV_errMsg("管号队列无对应的材料！");
						return cor;
					}

					if (ListPort.size() < Integer.parseInt(vrpOfflineEntity
							.getOfflineBranch())) {
						cor.setV_errMsg("管号队列无足够的离线管！");
						return cor;
					}

					if(ListPort.size()!= Integer.parseInt(vrpOfflineEntity
							.getTotalcount()) ){
						cor.setV_errMsg("数据异常,队列离线支与离线记录剩余支不一致！");
						return cor;
					}

					String ordPk = "";
					String ordLnPk = "";
					String belongCode ="";
					String belongName= "";

					if(IsInstore.equals("1")){
						sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.selectBelong");
						sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
						psta = con.prepareStatement(sql);
						rs = psta.executeQuery();
						if(rs.next())
						{
							belongCode = rs.getString("DEPARTMENT_CODE");
							belongName = rs.getString("DEPARTMENT_DESC");
						}
						rs.close();
						psta.close();


						sql = CoreXmlSqlParsersFactory
								.getSql("FrmVrpInStore.selectOrdPk");
						sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
						psta = con.prepareStatement(sql);
						rs = psta.executeQuery();
						if(rs.next())
						{
							ordPk = rs.getString("ORD_PK");
							ordLnPk = rs.getString("ORD_LN_PK");
						}
						rs.close();
						psta.close();
					}



					for (int i = 0; i <  Integer.parseInt(vrpOfflineEntity
							.getOfflineBranch()); i++) {

						// 修改管号队列
						pstaUpdatePort.setObject(1, "00");
						pstaUpdatePort.setObject(2, "20");
						if (vrpOfflineEntity.getPlineCode().equals("C018")) {
							pstaUpdatePort.setObject(3, slgx + "-" + xkgx + "-"
									+ yzgx + "-" + djgx + "-" + "0" + "-"
									+ bjgx + "-" + "0");
						} else {
							pstaUpdatePort.setObject(3, slgx + "-" + cldgx
									+ "-" + "0-" + bjgx + "-" + "0");
						}
						pstaUpdatePort.setObject(4, turnLinGx);
						pstaUpdatePort.setObject(5,
								vrpOfflineEntity.getFlagStoveNo());
						pstaUpdatePort.setObject(6, IsInstore);
						pstaUpdatePort.setObject(7,
								vrpOfflineEntity.getHeatPlanNo());
						pstaUpdatePort.setObject(8, ListPort.get(i).getMatNo());
						pstaUpdatePort.addBatch();


						if(IsInstore.equals("0")) {
							PlanService.DeleteOffLineMat(con,ListPort.get(i).getMatNo(),true,vrpOfflineEntity.getPlineCode());
						}
						else {
							//region 返线入库 写入库记录  更新库存数据

							if(ListPort.get(i).getPhyBatchNo().equals("")){
								cor.setV_errMsg("批号为空的不允许做返线入库,请用正常的返线操作");
								return cor;
							}

							MatBcMEntity matBcEntity = new MatBcMEntity();
							matBcEntity.setMatNo(ListPort.get(i).getMatNo());
							//matBcEntity.setTestCount(String.valueOf(n));
							matBcEntity.setInstockDoc(inDoc);
							matBcEntity.setInstockTypeCode("800701");
							matBcEntity.setInstockTypeName("产出入库");
							matBcEntity.setLaryNo("1");
							matBcEntity.setLotNo(OldPln.getZyBatchId());
							matBcEntity.setSampleFlag(ListPort.get(i).getIsSamplePipe());

							if (!OldPln.getDecision().equals("A")) {
								matBcEntity.setProductFlag("801401");
								matBcEntity.setMatStatus("80150101");
							} else {
								matBcEntity.setProductFlag("801402");
								matBcEntity.setMatStatus("80150201");
							}
							matBcEntity.setJudgeStoveNo(vrpOfflineEntity.getJudgeStoveNo());
							matBcEntity.setBatchNo(ListPort.get(i).getPhyBatchNo());
							matBcEntity.setMaterialNo(OldPln.getOutwlId());
							matBcEntity.setMaterialName(OldPln.getOutwlDesc());
							matBcEntity.setProduccode(OldPln.getProduccode());
							matBcEntity.setProducname(OldPln.getProducname());
							matBcEntity.setSteelcode(OldPln.getSteelcode());
							matBcEntity.setSteelname(OldPln.getSteelname());
							matBcEntity.setStdStyle(OldPln.getStdStyle());
							matBcEntity.setStdStyleDesc(OldPln.getStdStyleDesc());
							matBcEntity.setSpecCode(OldPln.getSpecCode());
							matBcEntity.setSpecName(OldPln.getSpecName());
							matBcEntity.setModelCode(OldPln.getModelCode());
							matBcEntity.setModelDesc(OldPln.getModelDesc());
							matBcEntity.setGradecode(OldPln.getGradecode());
							matBcEntity.setGradename(OldPln.getGradename());
							matBcEntity.setStdCode("");
							matBcEntity.setStdName(OldPln.getProdStandard());
							matBcEntity.setProOrderNo(OldPln.getOrderNo()+"/"+OldPln.getOrderSeq());
							matBcEntity.setProcessCdoe("E");
							matBcEntity.setProcessDesc("镦拔扩");
							matBcEntity
									.setNextProcessCdoe(OldPln.getNextProcessCode());
							matBcEntity.setNextProcessDesc("");
							matBcEntity.setPlineCode(OldPln.getPlineCode());
							matBcEntity.setPlineName(OldPln.getPlineName());
							matBcEntity.setMatOriginCode("801302");
							matBcEntity.setMatOriginName("产线");
							matBcEntity.setActDimater(OldPln.getAimoutdiameter());
							matBcEntity.setActHeight(OldPln.getAimwallthick());
							matBcEntity.setActLenMin(ListPort.get(i).getActLenMin());
							matBcEntity.setActLenMax(ListPort.get(i).getActLenMax());
							matBcEntity.setBelongCode(belongCode);
							matBcEntity.setBelongName(belongName);
							matBcEntity.setCreateName(name);
							matBcEntity.setCreateTime(systime);


							matBcEntity.setBalYearMonth(yearMoth);
							matBcEntity.setJudgeStdCode("");
							matBcEntity.setJudgeStdName(OldPln.getOutputStandard());
							matBcEntity.setGxPlanNo(OldPln.getGxPlanNo());
							matBcEntity.setQcmStaus("00");
							matBcEntity.setHeatPlanNo(OldPln.getHeatPlanNo());
							matBcEntity.setActCount(ListPort.get(i).getActCount());
							matBcEntity.setProOrderId(OldPln.getProPlanId());
							matBcEntity.setGxPlanNo(OldPln.getGxPlanNo());
							matBcEntity.setPhyBatchNo(ListPort.get(i).getPhyBatchNo());
							matBcEntity.setIsSamplePipe(ListPort.get(i).getIsSamplePipe());
							matBcEntity.setCrafNo(OldPln.getCraftFileNo());
							matBcEntity.setCraftSeq(OldPln.getCraftSeq());
							matBcEntity.setOrdLnDlyPk(OldPln.getOrdLnDlyPk());
							matBcEntity.setOrderNo(OldPln.getOrderNo());
							matBcEntity.setOrderSeq(OldPln.getOrderSeq());
							matBcEntity.setOrderSeqNo(OldPln.getDeliveryNo());
							matBcEntity.setMatGowhereCode(OldPln.getGowhereCode());
							matBcEntity.setMatGowhereName(OldPln.getGowhereName());
							matBcEntity.setStoveNo(OldPln.getStoveNo());
							matBcEntity.setFlagStoveNoZg(ListPort.get(i).getFlagStoveNo());
							matBcEntity.setTarget(OldPln.getDecision()); //判定字头
							matBcEntity.setInBc(bc);
							matBcEntity.setInBz(bz);
							matBcEntity.setActLen(ListPort.get(i).getActLen());
							matBcEntity.setActWeight(ListPort.get(i).getActWeight());
							matBcEntity.setActTheoryWeight(ListPort.get(i).getActWeight());


							if ((OldPln.getCic() + "+" + OldPln.getPic() + "+"
									+ OldPln.getSic() + "+" + OldPln.getDic())
									.equals("++++")) {
								matBcEntity.setJudgeStdName("");
							} else {
								if (OldPln.getIfProduct().equals("1")) {
									matBcEntity.setJudgeStdName(OldPln.getCic() + "+"
											+ OldPln.getPic() + "+"
											+ OldPln.getSic() + "+"
											+ OldPln.getDic());
								} else {
									matBcEntity.setJudgeStdName("" + "+"
											+ OldPln.getPic() + "+"
											+ OldPln.getSic() + "+"
											+ OldPln.getDic());
								}
							}

							matBcEntity.setCic(OldPln.getCic());
							matBcEntity.setHeatPlanNoOld(OldPln.getHeatPlanNo());
							if(ordPk==null||ordPk.equals(""))
							{
								matBcEntity.setOrdPk("");
							}else{
								matBcEntity.setOrdPk(ordPk);
							}
							if(ordLnPk==null||ordLnPk.equals(""))
							{
								matBcEntity.setOrdLnPk("");
							}else{
								matBcEntity.setOrdLnPk(ordLnPk);
							}


							String updateMatsql = CoreXmlSqlParsersFactory
									.getSql("FrmMchInStore.updateMatBcM");// 返线交库 更改材料信息
							updateMatsql = com.steering.comm.converter.EntityTool.fillSqlByEntity(updateMatsql, matBcEntity);
							PreparedStatement pstaUpdateMatsql= con.prepareStatement(updateMatsql);
							pstaUpdateMatsql.addBatch();
							pstaUpdateMatsql.executeBatch();


							//endregion
						}
					}
					pstaUpdatePort.executeBatch();

					pstaupdateOff.setObject(
							1,
							Integer.parseInt(vrpOfflineEntity
									.getOfflineBranch())
									+ Integer.parseInt(vrpOfflineEntity
									.getReNum()));
					pstaupdateOff.setObject(2, Double.parseDouble(weight1)
							+ Double.parseDouble(vrpOfflineEntity.getReWt()));
					pstaupdateOff.setObject(3, systime);
					pstaupdateOff.setObject(4, name);
					pstaupdateOff.setObject(5, turnLinGx);
					pstaupdateOff
							.setObject(6, vrpOfflineEntity.getHeatPlanNo());
					pstaupdateOff.setObject(7, vrpOfflineEntity.getResultNo());
					pstaupdateOff.addBatch();


					if(IsInstore.equals("1")){
						String updateMatsql = CoreXmlSqlParsersFactory
								.getSql("FrmVrpInStore.insertVrpComInfo2");// 返线入库  插入公共信息表
						PreparedStatement pstaUpdateMatsql= con.prepareStatement(updateMatsql);
						pstaUpdateMatsql.setObject(1,inDoc);
						pstaUpdateMatsql.setObject(2, OldPln.getHeatPlanNo());
						pstaUpdateMatsql.addBatch();
						pstaUpdateMatsql.executeBatch();


						String matGpNum = "";// 已分配到订单支数
						int gpreqCount = 0;// 未分配支数
						int inCount = Integer.parseInt(vrpOfflineEntity
								.getOfflineBranch());

						if (!OldPln.getNextProcessCode().equals("")) {

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

							} else if (OldPln.getNextProcessCode().equals("G")) {
								sql = CoreXmlSqlParsersFactory
										.getSql("FrmVrpInStore.selectPlnZyJg");
							} else if (OldPln.getNextProcessCode().equals("H")) {
								sql = CoreXmlSqlParsersFactory
										.getSql("FrmVrpInStore.selectPlnZyJgm");
							}
							sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
							psta = con.prepareStatement(sql);
							rs = psta.executeQuery();
							List<PlnZyRclMEntity1> plnOrderZy = com.steering.comm.converter.EntityTool.getEntitysByRs(
									rs, PlnZyRclMEntity1.class);
							rs.close();
							psta.close();
							String codeBl = "";
							if (OldPln.getNextProcessCode().equals("H")) {
								sql = CoreXmlSqlParsersFactory
										.getSql("FrmVrpInStore.selectPlnZyJgmProp");
								sql = com.steering.comm.converter.EntityTool.fillSqlByEntity(sql, OldPln);
								psta = con.prepareStatement(sql);
								rs = psta.executeQuery();
								if (rs.next()) {
									codeBl = rs.getString("CODE_JG_BL");
								}
								rs.close();
								psta.close();
							}
							if (plnOrderZy.size() > 0) {
								for (int i = 0; i < plnOrderZy.size(); i++) {
									// 该订单下已分配多少支
									sql = CoreXmlSqlParsersFactory
											.getSql("FrmVrpInStore.selectMatGpMNumZg");
									sql = com.steering.comm.converter.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);

									MatBcMEntity matEntity = new MatBcMEntity();
									matEntity
											.setJudgeStoveNo(OldPln.getJudgeStoveNo());
									matEntity.setProOrderId(OldPln.getProPlanId());
									matEntity.setGxPlanNo(OldPln.getGxPlanNo());
									matEntity.setHeatPlanNo(OldPln.getHeatPlanNo());
									matEntity.setProPlanIdNext(plnOrderZy.get(i)
											.getProPlanId());
									matEntity.setGxPlanNoNext(plnOrderZy.get(i)
											.getGxPlanNo());
									matEntity.setHeatPlanNoNext(plnOrderZy.get(i)
											.getHeatPlanNo());
									matEntity.setPlineCode(OldPln.getPlineCode());
									matEntity.setCodeJgBl(codeBl);

									if (i == plnOrderZy.size() - 1) {
										matEntity.setActCount(String.valueOf(inCount));
									} else if (inCount < gpreqCount) {
										matEntity.setActCount(String.valueOf(inCount));
									} else {
										matEntity.setActCount(String.valueOf(gpreqCount));
									}
									String updateMatBcMSql = CoreXmlSqlParsersFactory
											.getSql("FrmVrpInStore.updateMatBcM");
									updateMatBcMSql = com.steering.comm.converter.EntityTool.fillSqlByEntity(
											updateMatBcMSql, matEntity);
									PreparedStatement pstaupdateMatBcMSql= con.prepareStatement(updateMatBcMSql);
									pstaupdateMatBcMSql.addBatch();
									pstaupdateMatBcMSql.executeBatch();
									inCount = inCount - gpreqCount;
									if (inCount <= 0 && i + 1 < plnOrderZy.size()) {
										break;
									}

								}
							}
						}

					}
					
				/*
					// 如果输入支数大于离线的支数,新增管号队列
					String MatNo = "";
					String mat1 = "";
					String addPortVrp = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.AddPortVrpReturn");// 新增管号队列
					PreparedStatement pastaddPortVrp = con
							.prepareStatement(addPortVrp);
					String MatNosql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.getQueryMatNo");// 查询管号队列对应最大的材料号
					psta = con.prepareStatement(MatNosql);
					psta.setObject(1, vrpOfflineEntity.getHeatPlanNo());
					psta.setObject(2, vrpOfflineEntity.getJudgeStoveNo());
					psta.setObject(3, vrpOfflineEntity.getBatchNo());
					ResultSet rs5 = psta.executeQuery();
					// listMat = EntityTool.getEntitysByRs(rs5,
					// MatZcMEntity.class);
					if (rs5.next()) {
						MatNo = rs5.getString("MAT_NO");
					}
					rs5.close();
					psta.close();
					//查询对应的管号队列数据
					List<PortVrpBatchSampleResultEntity> list = new ArrayList<PortVrpBatchSampleResultEntity>();
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.GetQueryVrpPort");// 查询对应的管号队列表
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getHeatPlanNo());
					psta.setString(2, vrpOfflineEntity.getJudgeStoveNo());
					psta.setObject(3, vrpOfflineEntity.getBatchNo());
					rs = psta.executeQuery();
					list = EntityTool.getEntitysByRs(rs,
							PortVrpBatchSampleResultEntity.class);
					rs.close();
					psta.close();
					String groupNo = "";
					String stoveNo = "";
					String actWeight = "";
					String getFeedHeatNo = "";
					String heatPlanSqe = "";
					String actDimater = "";
					String actHeight = "";
					for (PortVrpBatchSampleResultEntity groupList : list) {
					groupNo = groupList.getGroupNo();
					stoveNo = groupList.getStoveNo();
					actWeight = groupList.getActWeight();
					getFeedHeatNo = groupList.getFeedHeatNo();
					heatPlanSqe = groupList.getHeatPlanSqe();
					actDimater = groupList.getActDimater();
					actHeight = groupList.getActHeight();
					}
																													
					//查询对应的管号队列数据支数
					int offlineBranch = 0;
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.GetQueryVrpPortCount");// 查询对应的管号队列表
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getHeatPlanNo());
					psta.setString(2, vrpOfflineEntity.getJudgeStoveNo());
					psta.setObject(3, vrpOfflineEntity.getBatchNo());
					rs = psta.executeQuery();
					if(rs.next())
					{
						offlineBranch = Integer.parseInt(rs.getString("COUNT"));
					}
					rs.close();
					psta.close();
//					if (totalNum < Integer.parseInt(vrpOfflineEntity
//							.getOfflineBranch())) {
					if(offlineBranch <Integer.parseInt(vrpOfflineEntity
						.getOfflineBranch()))
					{
//									for (int i = 1; i < Integer
//											.parseInt(vrpOfflineEntity
//													.getOfflineBranch())
//											- totalNum + 1; i++) {
						for (int i = 1; i < Integer
								.parseInt(vrpOfflineEntity
								.getOfflineBranch())-offlineBranch
								 + 1; i++) {
										DecimalFormat dfPort = new DecimalFormat(
												"0");
										String mt = dfPort.format(i);
										mat1 = MatNo + mt; // 在最大的管号队列的材料号后面加1一直加
									
										pastaddPortVrp.setObject(1,
												vrpOfflineEntity
														.getHeatPlanNo());
										pastaddPortVrp.setObject(2, mat1);
										pastaddPortVrp.setObject(3, "");
										pastaddPortVrp.setObject(4, "");
										pastaddPortVrp
												.setObject(5, vrpOfflineEntity
														.getZyBatchId());
										pastaddPortVrp.setObject(6, "");
										pastaddPortVrp
												.setObject(7, vrpOfflineEntity
														.getPlineCode());
										pastaddPortVrp
												.setObject(8, vrpOfflineEntity
														.getPlineName());
										pastaddPortVrp.setObject(9,
												vrpOfflineEntity
														.getJudgeStoveNo());
										pastaddPortVrp.setObject(10,
												vrpOfflineEntity.getBatchNo());
										pastaddPortVrp.setObject(11,
												groupNo);
										pastaddPortVrp.setObject(12,
												stoveNo);
										pastaddPortVrp.setObject(13,
												vrpOfflineEntity
														.getFlagStoveNo());
										pastaddPortVrp.setObject(14,
												actDimater);
										pastaddPortVrp
												.setObject(15, actHeight);
										pastaddPortVrp.setObject(16,
												vrpOfflineEntity.getActLen());
										pastaddPortVrp
												.setObject(17, vrpOfflineEntity
														.getActLenMin());
										pastaddPortVrp
												.setObject(18, vrpOfflineEntity
														.getActLenMax());
										pastaddPortVrp.setObject(19, "1");
										pastaddPortVrp.setObject(20,
												actWeight);
										pastaddPortVrp.setObject(21, "");
										pastaddPortVrp.setObject(22, turnLinGx);
										pastaddPortVrp.setObject(23, "20");
										pastaddPortVrp.setObject(24, name);
										pastaddPortVrp.setObject(25, bc);
										pastaddPortVrp.setObject(26, bz);
										pastaddPortVrp.setObject(27, systime);
										if (vrpOfflineEntity.getPlineCode()
												.equals("C018")) {
											pastaddPortVrp.setObject(28, slgx
													+ "-" + xkgx + "-" + yzgx
													+ "-" + djgx + "-" + "0"
													+ "-" + bjgx + "-" + "0");
										} else {
											pastaddPortVrp.setObject(28, slgx
													+ "-" + cldgx + "-" + "0-"
													+ bjgx + "-" + "0");
										}
										pastaddPortVrp.setObject(29, turnLinGx);
										pastaddPortVrp.setObject(30, "00");
										pastaddPortVrp.setObject(31,
												vrpOfflineEntity.getBatchNo());// 材料状态
										pastaddPortVrp
												.setObject(32, getFeedHeatNo);// 材料状态
										pastaddPortVrp.setObject(33,
												heatPlanSqe);// 材料状态
										pastaddPortVrp.addBatch();
							
						}
						
						pastaddPortVrp.executeBatch();
									
					}*/
				}

			}
			pstaupdateOff.executeBatch();


			cor.setResult(HeatPlanIds);
			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
	 */
	public CoreReturnObject doCancelReturnLine(ArrayList<String> parm,
			ArrayList<String> parm1) {
		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 deleteOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.DeleteReturnResu");// 删除镦拔扩计划表
			PreparedStatement pstaDeleteOff = con.prepareStatement(deleteOff);
			String deleteOffC = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.DeleteReturnResuC");// 删除镦拔扩计划从表
			PreparedStatement pstaDeleteOffC = con.prepareStatement(deleteOffC);
			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdatePortVrpReturn");// 修改管号队列表
			PreparedStatement pstaUpdatePort = con.prepareStatement(updatePort);
			String deletePort = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.DeletePortVrpReturn");// 删除管号队列表
			PreparedStatement pstaDeletePort = con.prepareStatement(deletePort);
			String updateMat = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateMatReturn");// 修改材料表
			PreparedStatement pstaUpdateMat = con.prepareStatement(updateMat);



			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateOff");// 修改离线表返线支数
			PreparedStatement pstaupdateOff = con.prepareStatement(updateOff);
			String updateStatus = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateStatus1");// 修改组炉返线材料状态20
			PreparedStatement pstaupdateStatus = con
					.prepareStatement(updateStatus);


			String updatePlnStatus = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdatePlanStatus");//
			PreparedStatement pupdatePlnStatus = con
					.prepareStatement(updatePlnStatus);
			// String updatePlnInfo = CoreXmlSqlParsersFactory
			// .getSql("FrmVrpLoading.deleteVrpResult");// 删除镦拔扩公共信息
			// PreparedStatement patsupdatePlan = con
			// .prepareStatement(updatePlnInfo);
			// String plineCode = parm1.get(0);
			String name = parm1.get(1);
			String storageNo = parm1.get(4);// 仓库

			for (String str : parm) {
				VrpOfflineResultEntity vrpOfflineEntity = EntityTool
						.getEntityByJSONStr(str.toString(),
								VrpOfflineResultEntity.class);

				// String offgroupId = "";// 离线组炉Id
				String groupStart = "";// 组炉状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryOffLineGroupStart");// 查询是否组炉
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getJudgeStoveNo());
				psta.setString(2, vrpOfflineEntity.getBatchNo());
				psta.setString(3, vrpOfflineEntity.getResultNo());
				psta.setString(4, vrpOfflineEntity.getHeatPlanNo());
				rs = psta.executeQuery();
				String flag = "0";
				if (rs.next()) {
					groupStart = rs.getString("GROUP_LOG");
					flag = rs.getString("FLAG");
				}

				if (flag.equals("1")) {
					cor.setV_errMsg("物料已发生改变,不允许撤销!");
					return cor;
				}

				rs.close();
				psta.close();
				List<String> matNo = new ArrayList<String>();
				if (groupStart.equals("1")) {

				/*	sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.QueryReturnMatNo");// 查询管号队列返线材料号
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getResultNo());
					rs = psta.executeQuery();
					while (rs.next()) {
						matNo.add(rs.getString("MAT_NO"));
					}
					rs.close();
					psta.close();
					if (matNo.size() == 0) {
						cor.setV_errMsg("物料已发生改变,不允许撤销!");
						return cor;
					}*/

					int recount = 0;
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.QueryComVrpRes");// 查询返线后是否存在实绩
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getHeatPlanNo());
					rs = psta.executeQuery();
					if (rs.next()) {
						recount = Integer.parseInt(rs.getString("COUNT"));
					}
					rs.close();
					psta.close();
					if (recount != 0) {
						cor.setV_errMsg("此炉已存在实绩,不允许此操作!");
						return cor;
					}
					// 已组炉撤销返线
					/*
					 * for (int j = 0; j < matNo.size(); j++) { // 删除材料表
					 *//*
						 * pstaDeleteMat.setObject(1, matNo.get(j));
						 * pstaDeleteMat.addBatch();
						 * 
						 * if (vrpOfflineEntity.getReturnProcess().equals(
						 * parm1.get(5))) { // 删除位置表 pstaDeleteButt.setObject(1,
						 * matNo.get(j)); pstaDeleteButt.addBatch(); } else { //
						 * 删除管号队列表 pstaDeletePort.setObject(1, vrpOfflineEntity
						 * .getHeatPlanNo()); pstaDeletePort.setObject(2,
						 * matNo.get(j)); pstaDeletePort.addBatch(); }
						 *//*
							 * // 如果有废品新增到材料表 if (!vrpOfflineEntity
							 * .getReNum().equals("") && !vrpOfflineEntity
							 * .getReNum().equals("0")) {
							 * pastaddMatBc.setObject(1, matNo.get(j));
							 * pastaddMatBc.addBatch();
							 * 
							 * 
							 * pastupdateMatBc.setObject(1, matNo.get(j));
							 * pastupdateMatBc.addBatch();
							 * 
							 * }
							 * 
							 * }
							 */

					// pstaDeleteMat.executeBatch();
					// pstaDeleteButt.executeBatch();


					PlanService.deletePlnFurnRcl(vrpOfflineEntity.getHeatPlanNo(),con);

					pstaDeletePort.setObject(1,
							vrpOfflineEntity.getHeatPlanNo());
					pstaDeletePort.addBatch();
					pstaDeletePort.executeBatch();

					// 删除镦拔扩计划表
					pstaDeleteOff
							.setObject(1, vrpOfflineEntity.getHeatPlanNo());
					pstaDeleteOff.addBatch();
					pstaDeleteOff.executeBatch();

					// 删除镦拔扩计划从表
					pstaDeleteOffC.setObject(1,
							vrpOfflineEntity.getHeatPlanNo());
					pstaDeleteOffC.addBatch();
					pstaDeleteOffC.executeBatch();

					// 修改离线表返线支数
					pstaupdateOff.setObject(1, "0");
					pstaupdateOff.setObject(2, "0");
					pstaupdateOff.setObject(3, systime);
					pstaupdateOff.setObject(4, name);
					pstaupdateOff.setObject(5, "");
					pstaupdateOff.setObject(6, "");
					pstaupdateOff.setObject(7, vrpOfflineEntity.getResultNo());
					pstaupdateOff.addBatch();


					List<VrpGroupResultEntity> list = new ArrayList<VrpGroupResultEntity>();
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.QueryGroupVrpResult");// 查询组炉履历表信息
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getGroupId());
					rs = psta.executeQuery();
					list = EntityTool.getEntitysByRs(rs,
							VrpGroupResultEntity.class);
					rs.close();
					psta.close();


					for (VrpGroupResultEntity groupList : list) {
						sql = CoreXmlSqlParsersFactory
								.getSql("FrmVrpDownLineManage.QueryNewFurnMatNo");// 查询管号队列离线材料号
						psta = con.prepareStatement(sql);
						psta.setString(1, groupList.getLxResultNo());
						rs = psta.executeQuery();
						while (rs.next()) {
							matNo.add(rs.getString("MAT_NO"));
						}
						rs.close();
						psta.close();
					}


					if (matNo.size() == 0) {
						cor.setV_errMsg("管号队列无对应的材料！");
						return cor;
					}
					for (int i = 0; i < matNo.size(); i++) {
						PlanService.AddOffLineMat(con,"E",name,"11",
								matNo.get(i),"",vrpOfflineEntity.getZyBatchId1());



						pstaupdateStatus.setObject(1,
								vrpOfflineEntity.getZyBatchId1());
						pstaupdateStatus.setObject(2, matNo.get(i));
						pstaupdateStatus.addBatch();
					}


					pupdatePlnStatus.setObject(1,
							vrpOfflineEntity.getZyBatchId1());
					pupdatePlnStatus.addBatch();
					pupdatePlnStatus.executeBatch();

					pstaupdateStatus.executeBatch();

					/*
					 * //修改组炉返线材料状态20 for(int i=0;i<
					 * Integer.parseInt(vrpOfflineEntity .getReNum());i++) {
					 * pstaupdateStatus.setObject(1,
					 * vrpOfflineEntity.getReturnProcess());
					 * pstaupdateStatus.setObject(2,
					 * vrpOfflineEntity.getZyBatchId1());
					 * pstaupdateStatus.addBatch();
					 * pstaupdateStatus.executeBatch(); }
					 */
				} else {
					
					 ArrayList<String> matNo1=new ArrayList<String>();
	                 String sqlloading3 = CoreXmlSqlParsersFactory
	                         .getSql("FrmVrpDownLineManage.queryVrpMatNo");//查询管号队列表里待处理材料号
	                 psta = con.prepareStatement(sqlloading3);
	                 psta.setObject(1, vrpOfflineEntity.getResultNo());
	                 ResultSet rs4 = psta.executeQuery();
	                 while (rs4.next()) {
	                	 matNo1.add(rs4.getString("MAT_NO"));
	                 }
	                 rs4.close();
	                 psta.close();
					//查询离线支数
					int count = 0;
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.doQueryOffLineNum");
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getHeatPlanNo());
					psta.setString(2, vrpOfflineEntity.getResultNo());
					rs = psta.executeQuery();
					if (rs.next()) {
		            	count=Integer.parseInt(rs.getString("TOTALCOUNT"));
		            }
					rs.close();
					psta.close();
					//删除管号队列新增数			
					String deteleVrpMat = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.deteleVrpPort");
					PreparedStatement pstadeteleVrpMat = con.prepareStatement(deteleVrpMat);
					if(Integer.parseInt(vrpOfflineEntity.getReNum())>count)
					{
						for(int i =1;i<count+1;i++)
						{
							pstadeteleVrpMat.setObject(1, matNo1.get(i-1));
							pstadeteleVrpMat.setObject(2, vrpOfflineEntity.getHeatPlanNo());
							pstadeteleVrpMat.setObject(3, vrpOfflineEntity.getReturnProcess());
							pstadeteleVrpMat.addBatch();
						}
						pstadeteleVrpMat.executeBatch();
						
					String plnStrat = queryPlnZYDbkStrat(con,
							vrpOfflineEntity.getHeatPlanNo());
					if (plnStrat.equals("30")) {
						cor.setV_errMsg("该炉对应的计划已结炉,不允许次操作！");
						return cor;
					}
					if (plnStrat.equals("40")) {
						cor.setV_errMsg("该炉对应的计划已关闭,不允许次操作！");
						return cor;
					}
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpDownLineManage.QueryReturnMatNo");// 查询管号队列返线材料号
					psta = con.prepareStatement(sql);
					psta.setString(1, vrpOfflineEntity.getResultNo());
					rs = psta.executeQuery();
					while (rs.next()) {
						matNo.add(rs.getString("MAT_NO"));
					}
					rs.close();
					psta.close();
					if (matNo.size() == 0) {
						cor.setV_errMsg("物料已发生改变,不允许撤销!");
						return cor;
					}

						if (matNo.size() < Integer.parseInt(vrpOfflineEntity.getReNum()) ) {
							cor.setV_errMsg("物料已发生改变,不允许撤销!");
							return cor;
						}


						for (int i = 0; i < matNo.size(); i++) {
						// 修改管号队列
						pstaUpdatePort.setObject(1, "20");
						pstaUpdatePort.setObject(2, "10");
						if (vrpOfflineEntity.getPlineCode().equals("C018")) {
							pstaUpdatePort.setObject(
									3,
									vrpOfflineEntity.getGxsl() + "-"
											+ vrpOfflineEntity.getGxxk() + "-"
											+ vrpOfflineEntity.getGxyz() + "-"
											+ vrpOfflineEntity.getGxdj() + "-"
											+ "0" + "-"
											+ vrpOfflineEntity.getGxbj() + "-"
											+ "0");
						} else {
							pstaUpdatePort.setObject(3,
									vrpOfflineEntity.getGxsl() + "-"
											+ vrpOfflineEntity.getGxcld() + "-"
											+ "0-" + vrpOfflineEntity.getGxbj()
											+ "-" + "0");
						}
						pstaUpdatePort.setObject(4, "");
						pstaUpdatePort.setObject(5,
								vrpOfflineEntity.getFlagStoveNo());
							pstaUpdatePort.setObject(6,"0");
						pstaUpdatePort.setObject(7,
								vrpOfflineEntity.getHeatPlanNo());
						pstaUpdatePort.setObject(8, matNo.get(i));
						pstaUpdatePort.addBatch();


						// 如果有废品新增到材料表
						if (!vrpOfflineEntity.getReNum().equals("")
								&& !vrpOfflineEntity.getReNum().equals("0")) {

							PlanService.AddOffLineMat(con,"E",name,"11",
									matNo.get(i),"",vrpOfflineEntity.getHeatPlanNo());



						}

					}


					pstaUpdatePort.executeBatch();
					pstaUpdateMat.executeBatch();

					// 修改离线表返线支数
					pstaupdateOff.setObject(1, "0");
					pstaupdateOff.setObject(2, "0");
					pstaupdateOff.setObject(3, systime);
					pstaupdateOff.setObject(4, name);
					pstaupdateOff.setObject(5, "");
					pstaupdateOff
							.setObject(6, vrpOfflineEntity.getHeatPlanNo());
					pstaupdateOff.setObject(7, vrpOfflineEntity.getResultNo());
					pstaupdateOff.addBatch();
					
					
				
					}
				}
			}
			pstaupdateOff.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 conn
	 *            c
	 * @throws Exception
	 *             异常处理
	 */
	public static String queryEntrustrMax(Connection conn) throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpDownLineManage.queryEntrustrMax");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, matter1.format(dt));
		psta.setString(2, matter.format(dt));
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("SUBCONTRACT_NO");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 委外加工
	 * 
	 * @param result
	 * @return
	 */
	public CoreReturnObject doOutSourc(ArrayList<String> parm,
			ArrayList<String> parm1) {
		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 addOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.AddOutSourc");// 新增委外加工表
			PreparedStatement pstaAddSourc = con.prepareStatement(addOff);
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateOffOutSourc");// 修改离线表委外支数
			PreparedStatement pstaupdateOff = con.prepareStatement(updateOff);
			String memo = parm1.get(0);// 委托说明
			// String plineCode = parm1.get(1);
			String name = parm1.get(2);
			String bc = parm1.get(4);
			String bz = parm1.get(3);
			String subPlinecode = parm1.get(6);
			String subPlineName = parm1.get(7);
			String subType = parm1.get(8);
			String subGx = parm1.get(9);

			String entrusNo = queryEntrustrMax(con);
			int g = 01;

			for (String str : parm) {
				VrpOfflineResultEntity vrpOfflineEntity = EntityTool
						.getEntityByJSONStr(str.toString(),
								VrpOfflineResultEntity.class);
				String plnStrat = queryPlnZYDbkStrat(con,
						vrpOfflineEntity.getHeatPlanNo());
				if (plnStrat.equals("30")) {
					cor.setV_errMsg("该炉对应的计划已结炉,不允许次操作！");
					return cor;
				}
				if (plnStrat.equals("40")) {
					cor.setV_errMsg("该炉对应的计划已关闭,不允许次操作！");
					return cor;
				}
				int count = 0;
				int remCount = 0;// 返线支数
				String groupStart = "";// 组炉状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.QueryOffLineGroupStart");// 查询是委外
				psta = con.prepareStatement(sql);
				psta.setString(1, vrpOfflineEntity.getJudgeStoveNo());
				psta.setString(2, vrpOfflineEntity.getBatchNo());
				psta.setString(3, vrpOfflineEntity.getResultNo());
				psta.setString(4, vrpOfflineEntity.getHeatPlanNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					groupStart = rs.getString("GROUP_LOG");
					count = Integer.parseInt(rs.getString("COUNT_NUM"));
					remCount = Integer
							.parseInt(rs.getString("SUBCONTRACT_NUM"));
				}
				rs.close();
				psta.close();
				if (remCount == 0) {
					cor.setV_errMsg("选择的炉号已委外加工,不允许此操作!");
					return cor;
				}
				if (groupStart.equals("1")) {
					cor.setV_errMsg("选择的炉号是组炉记录,不允许此操作!");
					return cor;
				}
				// 新增委外加工表
				pstaAddSourc.setObject(1, entrusNo);
				pstaAddSourc.setObject(2, StringTool.formatDecimal(g, "00"));
				pstaAddSourc.setObject(3, vrpOfflineEntity.getPlineCode());
				pstaAddSourc.setObject(4, vrpOfflineEntity.getPlineName());
				pstaAddSourc.setObject(5, vrpOfflineEntity.getStationCode());
				pstaAddSourc.setObject(6, vrpOfflineEntity.getStationDesc());
				pstaAddSourc.setObject(7, vrpOfflineEntity.getHeatPlanNo());
				pstaAddSourc.setObject(8, vrpOfflineEntity.getZyBatchId());
				pstaAddSourc.setObject(9, vrpOfflineEntity.getProPlanId());
				pstaAddSourc.setObject(10, vrpOfflineEntity.getGxPlanNo());
				pstaAddSourc.setObject(11, vrpOfflineEntity.getJudgeStoveNo());
				pstaAddSourc.setObject(12, vrpOfflineEntity.getBatchNo());
				pstaAddSourc.setObject(13, vrpOfflineEntity.getProduccode());
				pstaAddSourc.setObject(14, vrpOfflineEntity.getProducname());
				pstaAddSourc.setObject(15, vrpOfflineEntity.getSteelcode());
				pstaAddSourc.setObject(16, vrpOfflineEntity.getSteelname());
				pstaAddSourc.setObject(17, vrpOfflineEntity.getGradecode());
				pstaAddSourc.setObject(18, vrpOfflineEntity.getGradename());
				pstaAddSourc.setObject(19, vrpOfflineEntity.getModelCode());
				pstaAddSourc.setObject(20, vrpOfflineEntity.getModelDesc());
				pstaAddSourc.setObject(21, vrpOfflineEntity.getStdCode());
				pstaAddSourc.setObject(22, vrpOfflineEntity.getStdName());
				pstaAddSourc.setObject(23, vrpOfflineEntity.getStdStyle());
				pstaAddSourc.setObject(24, vrpOfflineEntity.getStdStyleDesc());
				pstaAddSourc.setObject(25, vrpOfflineEntity.getActDimater());
				pstaAddSourc.setObject(26, vrpOfflineEntity.getActHeight());
				pstaAddSourc.setObject(27, vrpOfflineEntity.getActLen());
				pstaAddSourc.setObject(28, vrpOfflineEntity.getActLenMin());
				pstaAddSourc.setObject(29, vrpOfflineEntity.getActLenMax());
				pstaAddSourc.setObject(30, "");// 工艺文件号
				pstaAddSourc.setObject(31, vrpOfflineEntity.getOfflineNum());
				pstaAddSourc.setObject(32, vrpOfflineEntity.getOfflineWt());
				pstaAddSourc.setObject(33, name);
				pstaAddSourc.setObject(34, bc);
				pstaAddSourc.setObject(35, bz);
				pstaAddSourc.setObject(36, systime);
				pstaAddSourc.setObject(37, "00");
				pstaAddSourc.setObject(38, memo);
				pstaAddSourc.setObject(39, vrpOfflineEntity.getResultNo());
				pstaAddSourc.setObject(40, subPlinecode);
				pstaAddSourc.setObject(41, subPlineName);
				pstaAddSourc.setObject(42, subType);
				pstaAddSourc.setObject(43, subGx);
				pstaAddSourc.setObject(44, vrpOfflineEntity.getBatchGroudNo());
				pstaAddSourc.addBatch();

				// 修改离线表返线支数
				pstaupdateOff.setObject(1, vrpOfflineEntity.getOfflineNum());
				pstaupdateOff.setObject(2, vrpOfflineEntity.getOfflineWt());
				pstaupdateOff.setObject(3, systime);
				pstaupdateOff.setObject(4, name);
				pstaupdateOff.setObject(5, entrusNo);
				pstaupdateOff.setObject(6, StringTool.formatDecimal(g, "00"));
				pstaupdateOff.setObject(7, vrpOfflineEntity.getResultNo());
				pstaupdateOff.addBatch();
				g++;
			}
			pstaAddSourc.executeBatch();
			pstaupdateOff.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 doCancelOutSourc(ArrayList<Object> parm) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String deleteOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.deleteOutSourc");// 删除委外加工表
			PreparedStatement pstaDeleteSourc = con.prepareStatement(deleteOff);
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.UpdateOffOutSourc");// 修改离线表委外支数
			PreparedStatement pstaupdateOff = con.prepareStatement(updateOff);

			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String resuNo = list.get(0);// 离线主键
				String subNo = list.get(1);// 委托单号
				String subNoSeq = list.get(2);// 委托单序号
				String bc = list.get(4);
				String bz = list.get(3);
				String groupStart = "";// 状态
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpDownLineManage.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;
				}
				// 删除委外加工表
				pstaDeleteSourc.setObject(1, subNo);
				pstaDeleteSourc.setObject(2, subNoSeq);
				pstaDeleteSourc.addBatch();

				// 修改离线表返线支数
				pstaupdateOff.setObject(1, "0");
				pstaupdateOff.setObject(2, "0");
				pstaupdateOff.setObject(3, "");
				pstaupdateOff.setObject(4, "");
				pstaupdateOff.setObject(5, "");
				pstaupdateOff.setObject(6, "");
				pstaupdateOff.setObject(7, resuNo);
				pstaupdateOff.addBatch();
			}
			pstaDeleteSourc.executeBatch();
			pstaupdateOff.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 doUpdateFlagStoveNo(ArrayList<Object> parm) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		// String sql = "";
		// PreparedStatement psta = null;
		// ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String updateOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpDownLineManage.doUpdateFlagStoveNo");// 修改离线标识炉号
			PreparedStatement pstaupdateOff = con.prepareStatement(updateOff);

			for (int i = 0; i < parm.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String resuNo = list.get(1);// 离线主键
				String subNo = list.get(0);//

				// 修改离线表返线支数
				pstaupdateOff.setObject(1, subNo);
				pstaupdateOff.setObject(2, resuNo);
				pstaupdateOff.addBatch();
			}
			pstaupdateOff.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;
	}

}
