package com.steering.pss.plnsaleord.stove;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.processOrder.entity.PlnGxJgPropSEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;
import com.steering.pss.plnsaleord.stove.entity.*;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

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

/**
 * 炉计划公用方法
 * 
 * @author teng 2017.10.27
 */
public class PlanFurnProduction extends DBCommon {

	/**
	 * 根据轧管订单查询轧管炉号信息
	 */
	public static List<PlnZyZgMEntity> getPlanZyM(Connection con,
			String proPlanId, String gxPlanNo, String heatPlanNo,
			String strats, String judgeStoveNo) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.querySteOrdPln");
		sql += "   AND T.PRO_PLAN_ID ||'/'||T.GX_PLAN_NO = ?";
		if (!heatPlanNo.equals("")) {
			sql += " AND T.HEAT_PLAN_NO='" + heatPlanNo + "'";
		}
		// if (strats.equals("add")) {
		// sql += " AND T.PLANSTATUS='00'";
		// }
		// else if (strats.equals("delete")) {
		// sql += " AND T.PLANSTATUS='10'";
		// }
		if (!judgeStoveNo.equals("")) {
			sql += " AND T.FEED_HEAT_NO='" + judgeStoveNo + "'";
		}
		sql += " ORDER BY T.GX_PRO_SEQ ASC";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId + "/" + gxPlanNo);
		ResultSet rs = ps.executeQuery();
		List<PlnZyZgMEntity> list = EntityTool.getEntitysByRs(rs,
				PlnZyZgMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnZyZgMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 根据镦拔扩订单查询炉号信息
	 */
	public static List<PlnZyDbkMEntity> getPlanZyDbkM(Connection con,
			String proPlanId, String gxPlanNo, String heatPlanNo,
			String strats, String judgeStoveNo) throws SQLException {
		String sql = "SELECT T.* FROM PLN_ZY_DBK_M T WHERE 1=1";
		if (!heatPlanNo.equals("")) {
			sql += " AND T.HEAT_PLAN_NO='" + heatPlanNo + "'";
		}
		if (!judgeStoveNo.equals("")) {
			sql += " AND T.FEED_HEAT_NO='" + judgeStoveNo + "'";
		}
		sql += " AND T.PRO_PLAN_ID='" + proPlanId + "' AND T.GX_PLAN_NO='"
				+ gxPlanNo + "'";
		sql += " ORDER BY T.GX_PRO_SEQ ASC";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<PlnZyDbkMEntity> list = EntityTool.getEntitysByRs(rs,
				PlnZyDbkMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnZyDbkMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 根据热处理订单查询炉号信息
	 */
	public static List<PlnZyRclMEntity> getPlanZyRclM(Connection con,
			String proPlanId, String gxPlanNo, String heatPlanNo,
			String strats, String judgeStoveNo) throws SQLException {
		String sql = "SELECT T.* FROM PLN_ZY_RCL_M T WHERE 1=1";
		if (!heatPlanNo.equals("")) {
			sql += " AND T.HEAT_PLAN_NO='" + heatPlanNo + "'";
		}
		if (!judgeStoveNo.equals("")) {
			sql += " AND T.FEED_HEAT_NO='" + judgeStoveNo + "'";
		}
		sql += " AND T.PRO_PLAN_ID='" + proPlanId + "' AND T.GX_PLAN_NO='"
				+ gxPlanNo + "'";
		sql += " ORDER BY T.GX_PRO_SEQ ASC";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<PlnZyRclMEntity> list = EntityTool.getEntitysByRs(rs,
				PlnZyRclMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnZyRclMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 根据热处理订单查询炉号信息
	 */
	public static List<PlnZyJgxMEntity> getPlanZyJgxM(Connection con,
			String proPlanId, String gxPlanNo, String heatPlanNo,
			String strats, String judgeStoveNo) throws SQLException {
		String sql = "SELECT T.* FROM PLN_ZY_JGX_M T WHERE 1=1";
		if (!heatPlanNo.equals("")) {
			sql += " AND T.HEAT_PLAN_NO='" + heatPlanNo + "'";
		}
		if (!judgeStoveNo.equals("")) {
			sql += " AND T.FEED_HEAT_NO='" + judgeStoveNo + "'";
		}
		sql += " AND T.PRO_PLAN_ID='" + proPlanId + "' AND T.GX_PLAN_NO='"
				+ gxPlanNo + "'";
		sql += " ORDER BY T.GX_PRO_SEQ ASC";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<PlnZyJgxMEntity> list = EntityTool.getEntitysByRs(rs,
				PlnZyJgxMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnZyJgxMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 根据上工序Bom,制程索引号查询投入产出物料
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static String[] getQueryBom(Connection con, String bom,
			String mscPline, String ordLnDlyPk, String processCode,String grade)
			throws SQLException {
		String[] param = new String[9];
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.queryMrialBom");
		PreparedStatement ps = con.prepareStatement(sql);
		//ps.setObject(1, bom);
		ps.setObject(1, mscPline);
		ps.setObject(2, ordLnDlyPk);
		ps.setObject(3, grade);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if (processCode.equals("E")) {
				param[0] = rs.getString("E_IN_MATERIAL_NO");
				param[1] = rs.getString("E_IN_MATERIAL_DESC");
				param[2] = rs.getString("E_OUT_MATERIAL_NO");
				param[3] = rs.getString("E_OUT_MATERIAL_DESC");
			} else if (processCode.equals("F")) {
				param[0] = rs.getString("F_IN_MATERIAL_NO");
				param[1] = rs.getString("F_IN_MATERIAL_DESC");
				param[2] = rs.getString("F_OUT_MATERIAL_NO");
				param[3] = rs.getString("F_OUT_MATERIAL_DESC");
			} else if (processCode.equals("G")) {
				param[0] = rs.getString("I_IN_MATERIAL_NO");
				param[1] = rs.getString("I_IN_MATERIAL_DESC");
				param[2] = rs.getString("I_OUT_MATERIAL_NO");
				param[3] = rs.getString("I_OUT_MATERIAL_DESC");
			} else {
				param[0] = "";
				param[1] = "";
				param[2] = "";
				param[3] = "";
			}
			param[4] = rs.getString("BOM");
			param[5] = rs.getString("MSC");
			param[6] = rs.getString("MSC_PLINE");
			param[7] = rs.getString("SPEC_NAME_C");
			param[8] = rs.getString("SPEC_CODE_C");
		}
		rs.close();
		ps.close();
		return param;
	}
	/**
	 * 只有加工线制程获取物料
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static String[] getQueryBomJgx(Connection con,
			 String ordLnDlyPk,String grade)
			throws SQLException {
		String[] param = new String[5];
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.getQueryBomJgx");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ps.setObject(2, grade);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			
				param[0] = rs.getString("IN_MATERIAL_NO");
				param[1] = rs.getString("IN_MATERIAL_DESC");
				param[2] = rs.getString("OUT_MATERIAL_NO");
				param[3] = rs.getString("OUT_MATERIAL_DESC");
		
		
			param[4] = rs.getString("BOM");
		}
		rs.close();
		ps.close();
		return param;
	}

	/**
	 * 通过交货行PK,主排产流水号,工序，查询对应订单信息
	 */
	public static <T> ArrayList<T> getPlanOrder(Connection con,
			String OrdLnDlyPk, String plnDivideId, String processCode,
			Class<T> cls) throws SQLException {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrderDbk");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrdeRcl");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrderJgx");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrderJg");
		}
		sql += " AND T.ORD_LN_DLY_PK = ?  AND T.PLN_DIVIDE_ID = ?";
		sql += " ORDER BY TO_NUMBER(T.GX_PRO_SEQ) ASC";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, OrdLnDlyPk);
		ps.setObject(2, plnDivideId);
		ResultSet rs = ps.executeQuery();
		ArrayList<T> t = EntityTool.getEntitysByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 轧管炉计划下发根据工序生成对应虚拟炉计划(自炼)
	 * 
	 * @param con
	 * @param
	 * @throws SQLException
	 */
	public static String addPlnFurn(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con) throws SQLException {
		String mess = "";
		String delievStateCode = "";
		String delievStateName = "";
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs1 = null;
		String designKey = "";
		// 获取数据库当前时间
		// String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		// 获取当前下发轧管炉号
		List<PlnZyZgMEntity> plnZyPln = getPlanZyM(con, proPlanId, gxPlanNo,
				heatPlanNo, "add", "");
		if (plnZyPln == null) {
			mess = "未查到轧管炉计划信息,请确认!";
			return mess;
		}
		for (PlnZyZgMEntity stord : plnZyPln) {
			// 判断下工序
			if (!stord.getNextProcessCode().equals("")) {// 下工序不为空时判断经过那个工序
				if (stord.getNextProcessCode().equals("E")) {// 镦拔扩工序
					List<PlnOrderDbkSEntity> plnDbk = getPlanOrder(con,
							stord.getOrdLnDlyPk(), plnDivideId, "E",
							PlnOrderDbkSEntity.class);
					if (plnDbk.size() <= 0) {
						continue;
					}

					sql = CoreXmlSqlParsersFactory
							.getSql("addPlnFurn.SelectDesignKey");// 查询designkey和mscPline
					psta = con.prepareStatement(sql);
					psta.setString(1, stord.getCraftFileNo());
					psta.setString(2, stord.getCraftSeq());
					rs1 = psta.executeQuery();
					if (rs1.next()) {
						designKey = rs1.getString("DESGIN_KEY");
					}
					rs1.close();
					psta.close();

					//查询交货状态
					sql = CoreXmlSqlParsersFactory.getSql("addPlnFurn.selectState");
					psta = con.prepareStatement(sql);
					psta.setString(1,designKey);
					psta.setString(2,stord.getIndexSeq());
					psta.setString(3,stord.getDecision());
					rs1 = psta.executeQuery();
					if(rs1.next())
					{
						delievStateCode  = rs1.getString("BASECODE");
						delievStateName = rs1.getString("BASENAME");
					}
					rs1.close();;
					psta.close();
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成镦拔扩虚拟炉计划
					if (furnPlan.addPlnZyDbkM(con, stord, plnDbk, createName,
							dateTime)) {
						continue;
					}
				} else if (stord.getNextProcessCode().equals("F")) {// 热处理工序
					List<PlnOrderRclSEntity> plnRcl = getPlanOrder(con,
							stord.getOrdLnDlyPk(), plnDivideId, "F",
							PlnOrderRclSEntity.class);
					if (plnRcl.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成热处理虚拟炉计划
					if (furnPlan.addPlnZyRclM(con, stord, plnRcl, createName,
							dateTime)) {
						// continue;
					}
				} else if (stord.getNextProcessCode().equals("G")) {// 加工工序
					if (!stord.getWholeBacklog().contains("G")
							&& stord.getOrderSource().equals("备料需求")) {
						// 查询备料需求与成品对应关系
						String sqlBL = "SELECT T.ORD_LN_DLY_PK FROM PLN_PORDER_BL_REQ T WHERE T.REQ_BL_ID=?";
						PreparedStatement ps = con.prepareStatement(sqlBL);
						ps.setObject(1, stord.getOrdLnDlyPk());
						ResultSet rs = ps.executeQuery();
						if (rs.next()) {
							stord.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
							plnDivideId = "01";
						}
						rs.close();
						ps.close();
					}
					List<PlnOrderJgxSEntity> plnJgx = getPlanOrder(con,
							stord.getOrdLnDlyPk(), plnDivideId, "G",
							PlnOrderJgxSEntity.class);
					if (plnJgx.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成加工线虚拟炉计划
					if (furnPlan.addPlnZyJgxM(con, stord, plnJgx, createName,
							dateTime,delievStateCode,delievStateName)) {
						// continue;
					}
				} else if (stord.getNextProcessCode().equals("H")) {// 接箍
					List<PlnGxJgPropSEntity> plnRcl = getPlanOrder(con,
							stord.getOrdLnDlyPk(), plnDivideId, "H",
							PlnGxJgPropSEntity.class);
					if (plnRcl.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成接箍虚拟炉计划
					if (furnPlan.addPlnZyJgZME(con, stord, plnRcl, createName,
							dateTime)) {
						// continue;
					}
				}
			}
			// else {
			// // 轧管出成品并且是接箍备料合同
			// if (stord.getOrdLnDlyPk().contains("ZZBY-J")) {
			// List<PlnGxJgPropSEntity> plnRcl = getPlanOrder(con, stord
			// .getOrdLnDlyPk(), plnDivideId, "H",
			// PlnGxJgPropSEntity.class);
			// if (plnRcl.size() <= 0) {
			// continue;
			// }
			// corePlanFurnProM furnPlan = new corePlanFurnProM();
			// // 生成接箍虚拟炉计划
			// if (furnPlan.addPlnZyJgZME(con, stord, plnRcl, createName,
			// dateTime)) {
			// continue;
			// }
			// }
			// }
		}
		return mess;
	}

	/**
	 * 轧管炉计划取消下发删除虚拟炉计划(自炼)
	 */
	public static String deletePlnFurn(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con) throws SQLException {
		String mess = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		// 获取当前下发炉次订单信息
		List<PlnZyZgMEntity> plnZyPln = getPlanZyM(con, proPlanId, gxPlanNo,
				heatPlanNo, "delete", "");
		if (plnZyPln == null) {
			// mess = "未查到轧管炉计划信息,请确认!";
			return mess;
		}
		for (PlnZyZgMEntity stord : plnZyPln) {
			// 判定是否末工序 1.是，0.否
			// if (stord.getIsendgx().equals("0")) {
			if (!stord.getNextProcessCode().equals("")) {
				// 删除虚拟炉计划
				if (stord.getNextProcessCode().equals("E")) {// 镦拔扩工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_DBK_M  T WHERE T.UPPER_HEATPLAN_ID=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "轧管炉号:" + stord.getFeedHeatNo()
									+ "的镦拔扩虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_DBK_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "轧管炉号:" + stord.getFeedHeatNo()
									+ "的镦拔扩虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_DBK_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();

						// 更新订单虚拟挂料量
						// String update =
						// "UPDATE PLN_ORDER_DBK_S T SET T.BATCHED_NUM=TO_NUMBER(NVL(T.BATCHED_NUM,0))-TO_NUMBER(?),"
						// +
						// "T.BATCHED_WT=TO_NUMBER(NVL(T.BATCHED_WT,0))-TO_NUMBER(?) WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
						// ps = con.prepareStatement(update);
						// ps.setObject(1, rs.getString("PLN_INSINGLE_NUM"));
						// ps.setObject(2, rs.getString("PLN_INSINGLE_WT"));
						// ps.setObject(3, rs.getString("PRO_PLAN_ID"));
						// ps.setObject(4, rs.getString("GX_PLAN_NO"));
						// ps.executeUpdate();
						// ps.close();
						// 更新顺序号
						// updateGxProSeq(con, rs.getString("PLINE_CODE"),
						// Integer
						// .parseInt(rs
						// .getString("GX_PRO_SEQ")),
						// -1, "D");

					}
					rs.close();
					ps.close();
				} else if (stord.getNextProcessCode().equals("F")) {// 热处理工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_RCL_M  T WHERE T.UPPER_HEATPLAN_ID=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "轧管炉号:" + stord.getFeedHeatNo()
									+ "的热处理虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_RCL_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "轧管炉号:" + stord.getFeedHeatNo()
									+ "的热处理虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_RCL_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();

						// 更新订单虚拟挂料量
						// String update =
						// "UPDATE PLN_ORDER_RCL_S T SET T.BATCHED_NUM=TO_NUMBER(NVL(T.BATCHED_NUM,0))-TO_NUMBER(?),"
						// +
						// "T.BATCHED_WT=TO_NUMBER(NVL(T.BATCHED_WT,0))-TO_NUMBER(?) WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
						// ps = con.prepareStatement(update);
						// ps.setObject(1, rs.getString("PLN_INSINGLE_NUM"));
						// ps.setObject(2, rs.getString("PLN_INSINGLE_WT"));
						// ps.setObject(3, rs.getString("PRO_PLAN_ID"));
						// ps.setObject(4, rs.getString("GX_PLAN_NO"));
						// ps.executeUpdate();
						// ps.close();
					}
					rs.close();
					ps.close();
				} else if (stord.getNextProcessCode().equals("G")) {// 加工工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JGX_M  T WHERE T.UPPER_HEATPLAN_ID=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "轧管炉号:" + stord.getFeedHeatNo()
									+ "的加工线虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "轧管炉号:" + stord.getFeedHeatNo()
									+ "的加工线虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();

						// 更新订单虚拟挂料量
						// String update =
						// "UPDATE PLN_ORDER_JGX_S T SET T.BATCHED_NUM=TO_NUMBER(NVL(T.BATCHED_NUM,0))-TO_NUMBER(?),"
						// +
						// "T.BATCHED_WT=TO_NUMBER(NVL(T.BATCHED_WT,0))-TO_NUMBER(?) WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
						// ps = con.prepareStatement(update);
						// ps.setObject(1, rs.getString("PLN_INSINGLE_NUM"));
						// ps.setObject(2, rs.getString("PLN_INSINGLE_WT"));
						// ps.setObject(3, rs.getString("PRO_PLAN_ID"));
						// ps.setObject(4, rs.getString("GX_PLAN_NO"));
						// ps.executeUpdate();
						// ps.close();

					}
					rs.close();
					ps.close();
				} else if (stord.getNextProcessCode().equals("H")) {// 接箍
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JG_M  T WHERE T.UPPER_HEATPLAN_ID=? AND T.ORD_LN_DLY_PK = ?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					ps.setObject(2, stord.getOrdLnDlyPk());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "轧管炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();
				}
			}
			// else {
			// if (stord.getOrdLnDlyPk().contains("ZZBY-J")) {
			// sql =
			// "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JG_M  T WHERE T.UPPER_HEATPLAN_ID=? AND T.ORD_LN_DLY_PK = ?";
			// ps = con.prepareStatement(sql);
			// ps.setObject(1, stord.getHeatPlanNo());
			// ps.setObject(2, stord.getOrdLnDlyPk());
			// rs = ps.executeQuery();
			// while (rs.next()) {
			// int countNum = 0;
			// if (!rs.getString("PLANSTATUS").equals("00")) {
			// mess = "轧管炉号:" + stord.getFeedHeatNo()
			// + "的接箍虚拟炉计划已下发,不能撤销";
			// return mess;
			// }
			// // 查询是否存在实际炉计划
			// String sqlZgC =
			// "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE T.HEAT_PLAN_NO=?";
			// PreparedStatement psC = con.prepareStatement(sqlZgC);
			// psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
			// ResultSet rsC = psC.executeQuery();
			// if (rsC.next()) {
			// countNum = rsC.getInt("COUNT");
			// }
			// rsC.close();
			// psC.close();
			// if (countNum > 0) {
			// mess = "判定炉号:" + stord.getFeedHeatNo()
			// + "的接箍虚拟炉计划存在实际炉计划,不能撤销";
			// return mess;
			// }
			// // 删除虚拟炉计划
			// String sqlDeleteZg =
			// "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
			// psC = con.prepareStatement(sqlDeleteZg);
			// psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
			// psC.executeUpdate();
			// psC.close();
			// }
			// rs.close();
			// ps.close();
			// }
			// }
		}
		return mess;
	}

	/**
	 * 更新虚拟炉计划顺序号
	 * 
	 * @param con
	 * @param plineCode
	 * @param proSeqB
	 * @param num
	 * @throws SQLException
	 */
	public static void updateGxProSeq(Connection con, String plineCode,
			int proSeqB, int num, String processCode) throws SQLException {
		String sql = "";
		if (processCode.equals("E")) {
			sql = " UPDATE PLN_ZY_DBK_M T  SET T.GX_PRO_SEQ = T.GX_PRO_SEQ + ?  "
					+ " WHERE T.PLINE_CODE = ? AND T.GX_PRO_SEQ >=?";
		} else if (processCode.equals("F")) {
			sql = " UPDATE PLN_ZY_RCL_M T  SET T.GX_PRO_SEQ = T.GX_PRO_SEQ + ?  "
					+ " WHERE T.PLINE_CODE = ? AND T.GX_PRO_SEQ >=?";
		} else if (processCode.equals("G")) {
			sql = " UPDATE PLN_ZY_JGX_M T  SET T.GX_PRO_SEQ = T.GX_PRO_SEQ + ?  "
					+ " WHERE T.PLINE_CODE = ? AND T.GX_PRO_SEQ >=?";
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, num);
		ps.setObject(2, plineCode);
		ps.setObject(3, proSeqB);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 获取产线对应去向
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static String getPlineCodeGroup(Connection con, String plineCode,
			String proPlanId, String gxPlanNo) throws SQLException {
		String param = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("ComPlanFurnace.queryPlinCodeGroup");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ps.setObject(2, proPlanId);
		ps.setObject(3, gxPlanNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			param = rs.getString("AF_INFO");
		}
		rs.close();
		ps.close();
		return param;
	}

	/**
	 * 判定单信息查询
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static List<QcmZgJugdeApplyEntity> getQcmZgApp(Connection con,
			String jugdeApplyCode, String judgeStoveNo, String processCode,
			String batchNo) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.QueryQcmZgApply");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jugdeApplyCode);
		ps.setObject(2, judgeStoveNo);
		ps.setObject(3, processCode);
		ps.setObject(4, batchNo);
		ResultSet rs = ps.executeQuery();
		List<QcmZgJugdeApplyEntity> list = EntityTool.getEntitysByRs(rs,
				QcmZgJugdeApplyEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (QcmZgJugdeApplyEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 库存信息查询
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static List<MatBcMEntityPln> getMatBcMApply(Connection con,
			String jugdeApplyCode, String judgeStoveNo, String processCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.QueryMatBcMApply");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jugdeApplyCode);
		ps.setObject(2, judgeStoveNo);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		List<MatBcMEntityPln> list = EntityTool.getEntitysByRs(rs,
				MatBcMEntityPln.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatBcMEntityPln listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 库存信息查询(生成炉计划)
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static List<MatBcMEntityPln> getMatBcMAdd(Connection con,
			String jugdeApplyCode, String judgeStoveNo, String processCode,
			String batchNo, String falg) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.QueryMatBcMAdd");
		if (!batchNo.equals("")) {
			sql += " AND T.BATCH_NO='" + batchNo + "'";
		}
		if (falg.equals("add")) {
			sql += " AND (T.MAT_STATUS ='80150102'  AND T.INSTOCK_FLAG='1' AND T.JUDGE_RESULT_CODE IN ('40740701','40740709') OR T.MAT_STATUS ='80150101'AND T.INSTANCY_FLAG='1')";
		}
		// else if(falg.equals("delete")){
		// sql += " AND T.MAT_STATUS IN ('80150102','80150103')";
		// }
		sql += " GROUP BY T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.JUGDE_APPLY_CODE,T.PROCESS_CDOE,T.PRO_PLAN_ID_NEXT,T.GX_PLAN_NO_NEXT,T.HEAT_PLAN_NO_NEXT,T.NEXT_PROCESS_CDOE,T.GRADECODE,T.GRADENAME,T.PLINE_CODE  ";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jugdeApplyCode);
		ps.setObject(2, judgeStoveNo);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		List<MatBcMEntityPln> list = EntityTool.getEntitysByRs(rs,
				MatBcMEntityPln.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatBcMEntityPln listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 查询是否生成镦拔扩，热处理，加工线实际炉计划
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static int queryFurnC(Connection con, String jugdeApplyCode,
			String judgeStoveNo, String batchNo, String groupNo,
			String proPlanId, String gxPlanNo, String processCode,
			String heatPlanNoNext) throws SQLException {
		int count = 0;
		String sql = "";
		if (processCode.equals("E")) {
			sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_DBK_C T WHERE 1=1";
		} else if (processCode.equals("F")) {
			sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_RCL_C T WHERE 1=1";
		} else if (processCode.equals("G")) {
			sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C T WHERE 1=1";
		} else if (processCode.equals("H")) {
			sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE 1=1";
		}
		if (heatPlanNoNext.equals("")) {
			sql += " AND T.PRO_PLAN_ID='" + proPlanId + "' AND T.GX_PLAN_NO='"
					+ gxPlanNo + "'";
		} else {
			sql += " AND T.HEAT_PLAN_NO='" + heatPlanNoNext + "'";
		}
		sql += " AND T.FEED_HEAT_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		ps.setObject(2, batchNo);
		ps.setObject(3, groupNo);
		ps.setObject(4, jugdeApplyCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 轧管判定生成下工序实际炉计划
	 * 
	 * @throws Exception
	 */
	public static void addPlnZyC(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		// 获取数据库当前时间
		String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		// 通过判定单号，判定炉号查询判定信息
		List<QcmZgJugdeApplyEntity> qcmApp = getQcmZgApp(con, jugdeApplyCode,
				judgeStoveNo, "D", batchNo);
		if (qcmApp == null) {
			message = "判定信息已改变,请刷新判定数据!";
			throw new Exception(message);
		}
		String processCode = "";
		String heatPlanNo = "";
		for (QcmZgJugdeApplyEntity qcmGp : qcmApp) {

			// 查询库存信息循环生成实绩炉计划
			List<MatBcMEntityPln> matApp = getMatBcMAdd(con, jugdeApplyCode,
					judgeStoveNo, "D", qcmGp.getBatchNo(), "add");
			if (matApp == null) {
				continue;
				// message = "未找到库存信息,确认是否有库存!";
				// throw new Exception(message);
			}
			for (MatBcMEntityPln matGp : matApp) {
				String proPlanIdNext = matGp.getProPlanIdNext();
				String gxPlanNoNext = matGp.getGxPlanNoNext();
				String heatPlanNoNext = matGp.getHeatPlanNoNext();
				int planInNum = Integer.parseInt(matGp.getActCount());
				int planZyCount = 0;
				// double planInWt = Double.parseDouble(qcmGp.getActWeight());//
				// 实重
				// 判定是否存在轧管虚拟炉计划
				// List<PlnZyZgMEntity> plnZg = getPlanZyM(con, proPlanId,
				// gxPlanNo,
				// "", "", judgeStoveNo);
				// if (plnZg == null) {
				// // message += "未找到轧管虚拟炉计划,";
				// continue;
				// }
				PlnZyDbkMEntity plnDbk = null;
				PlnZyRclMEntity plnRcl = null;
				PlnZyJgxMEntity plnJgx = null;
				corePlanFurnProM furnPlan = new corePlanFurnProM();
				if (proPlanIdNext.contains("DBK")) {
					processCode = "E";
					// 镦拔扩
					plnDbk = getPlanM(con, proPlanIdNext, gxPlanNoNext,
							judgeStoveNo, "E", heatPlanNoNext,
							PlnZyDbkMEntity.class);
					if (plnDbk == null) {
						continue;
					}
					if (plnDbk.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnDbk.getHeatPlanNo();
					if (Integer.parseInt(plnDbk.getPlanstatus()) < 30) {
//						planZyCount++;
						// 判端是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "E", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_DBK_C T WHERE T.FEED_HEAT_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成镦拔扩实际炉计划
						if (furnPlan.addPlnZyC(con, plnDbk, null, null, null,
								createName, dateTime, matGp, "E")) {
							planZyCount++;
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "D");
						ps.executeUpdate();
						ps.close();
					}
				} else if (proPlanIdNext.contains("RCL")) {
					processCode = "F";
					// 热处理
					plnRcl = getPlanM(con, proPlanIdNext, gxPlanNoNext,
							judgeStoveNo, "F", heatPlanNoNext,
							PlnZyRclMEntity.class);
					if (plnRcl == null) {
						continue;
					}
					if (plnRcl.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnRcl.getHeatPlanNo();
					if (Integer.parseInt(plnRcl.getPlanstatus()) < 20) {
//						planZyCount++;
						// 判断是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "F", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_RCL_C T WHERE T.FEED_HEAT_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=?  AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						//判断库存钢种与热处理炉计划钢种是否一致
						if(!matGp.getGradename().equals(plnRcl.getGradename()))
						{
							String sqlUpdatePlnM = "UPDATE PLN_ZY_RCL_M T SET T.GRADECODE = '"+matGp.getGradecode()+"',T.GRADENAME = '"+matGp.getGradename()+"' WHERE T.HEAT_PLAN_NO = '"+heatPlanNoNext+"'";
							ps = con.prepareStatement(sqlUpdatePlnM);
							ps.executeUpdate();
							ps.close();
						}
						// 生成热处理实际炉计划
						if (furnPlan.addPlnZyC(con, null, plnRcl, null, null,
								createName, dateTime, matGp, "F")) {
							planZyCount++;
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "D");
						ps.executeUpdate();
						ps.close();
					}
				} else if (proPlanIdNext.contains("JGX")) {
					processCode = "G";
					// 加工线
					plnJgx = getPlanM(con, proPlanIdNext, gxPlanNoNext,
							judgeStoveNo, "G", heatPlanNoNext,
							PlnZyJgxMEntity.class);
					if (plnJgx == null) {
						continue;
					}
					if (plnJgx.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJgx.getHeatPlanNo();
					if (Integer.parseInt(plnJgx.getPlanstatus()) < 20) {
//						planZyCount++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "G", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JGX_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成加工线实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, plnJgx, null,
								createName, dateTime, matGp, "G")) {
							planZyCount++;
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "D");
						ps.executeUpdate();
						ps.close();
					}
				} else if (proPlanIdNext.contains("JG_")) {
					processCode = "H";
					// 加工线
					PlnZyJgMEntity plnJg = getPlanM(con, proPlanIdNext,
							gxPlanNoNext, judgeStoveNo, "H", heatPlanNoNext,
							PlnZyJgMEntity.class);
					if (plnJg == null) {
						continue;
					}
					if (plnJg.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJg.getHeatPlanNo();
					if (Integer.parseInt(plnJg.getPlanstatus()) < 20) {

						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "H", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JG_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成加工线实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, null, plnJg,
								createName, dateTime, matGp, "H")) {
							planZyCount++;
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "D");
						ps.executeUpdate();
						ps.close();
					}
				}

				if (planZyCount > 0) {// 判定是否挂下工序虚拟炉计划
					// 修改管坯MAT_BC_M挂料状态
					String updatesql = CoreXmlSqlParsersFactory
							.getSql("PlanFurnProduction.updateMatBcMR");
					ps = con.prepareStatement(updatesql);
					ps.setObject(1, jugdeApplyCode);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, matGp.getBatchNo());
					ps.setObject(4, matGp.getBatchGroudNo());
					ps.setObject(5, heatPlanNoNext);
					ps.setObject(6, processCode);
					ps.setObject(7, planInNum);
					ps.setObject(8, "1");
					ps.setObject(9, "80150103");
					ps.executeUpdate();
					ps.close();
				}

//				if (!qcmGp.getPlineCode().equals("C065")
//						&& !qcmGp.getPlineCode().equals("C066")) {
//					int countColse = 0;
//					// 查询进程表量 （预计支-废品-判定支=0）
//					String sqlMil = "SELECT NVL(T.YJZ - T.UNDETERMINEDNUM - T.SCRAPNUM - T.WWFPZ,0) -"
//							+ "(SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1 WHERE T1.PROCESS_CODE = 'D' "
//							+ " AND T1.VALIDFLAG = '20' AND T1.IS_FLAG='0' AND T1.JUDGE_STOVE_NO = ?) ACOUNT FROM MIL_PROCESSES T WHERE T.JUDGE_STOVE_NO = ?";
//					ps = con.prepareStatement(sqlMil);
//					ps.setObject(1, judgeStoveNo);
//					ps.setObject(2, judgeStoveNo);
//					ResultSet rs = ps.executeQuery();
//					if (rs.next()) {
//						countColse = rs.getInt("ACOUNT");
//					}
//					rs.close();
//					ps.close();
//					if (qcmGp.getJudgeResult().equals("40740701")) {
//						if (countColse == 0) {
//							// 修改本工序炉计划状态--关闭
//							String updateZgM = "UPDATE PLN_ZY_ZG_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0' WHERE T.FEED_HEAT_NO=? AND T.PLANSTATUS='30'";
//							ps = con.prepareStatement(updateZgM);
//							ps.setObject(1, judgeStoveNo);
//							ps.executeUpdate();
//							ps.close();
//						}
//					}
//				}
				// 修改虚拟炉计划表支吨
				// if (processCode.equals("E")) {
				// // 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialDbk.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
				// } else if (processCode.equals("F")) {
				// // 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialRcl.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
				// } else if (processCode.equals("G")) {
				// // 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialJgx.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
				// }
				// else {
				// // 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialJgx.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
				//
				// }
			}
		}
	}

	/**
	 * 轧管判定回退，判定不合删除实际炉计划
	 * 
	 * @param con
	 */
	public static void deletePlnC(String jugdeApplyCode, String judgeStoveNo,
								  String createName, Connection con, String batchNo) throws Exception {
		deletePlnC(jugdeApplyCode,judgeStoveNo,createName,con,batchNo,"");
	}
	public static void deletePlnC(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo,String isJugde) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		// 通过判定单号，判定炉号查询库存信息
		List<MatBcMEntityPln> qcmApp = getMatBcMAdd(con, jugdeApplyCode,
				judgeStoveNo, "D", batchNo, "delete");
		if (qcmApp == null) {
			message = "未找到库存信息,确认是否有库存!";
			throw new Exception(message);
		}
		for (MatBcMEntityPln qcmGp : qcmApp) {
			// 如果是末工序跳出执行下一循环
			if (qcmGp.getNextProcessCdoe().equals("")) {
				continue;
			}
			String proPlanIdNext = qcmGp.getProPlanIdNext();
			// String gxPlanNoNext = qcmGp.getGxPlanNoNext();
			String heatPlanNoNext = qcmGp.getHeatPlanNoNext();
			int planInNum = Integer.parseInt(qcmGp.getActCount());
			PlnZyDbkCEntity plnDbk = null;
			PlnZyRclCEntity plnRcl = null;
			PlnZyJgxCEntity plnJgx = null;
			String processCode = "";
			int count = 0;
			if (proPlanIdNext.contains("DBK")) {
				processCode = "E";
				// 镦拔扩实际炉计划
				plnDbk = getPlanC(con, jugdeApplyCode, judgeStoveNo,
						qcmGp.getBatchNo(), qcmGp.getBatchGroudNo(), "E",
						heatPlanNoNext, PlnZyDbkCEntity.class);
				if (plnDbk == null) {
					continue;
				}
				count++;
				if (!plnDbk.getPlanstatus().equals("00")
						&& !plnDbk.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),镦拔扩不 是计划和下发状态,不能取消确认,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_DBK_C WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnDbk.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();

				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_DBK_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnDbk.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnDbk.getIfMatchOdd().equals("1")) {
						// 如果是利库炉计划则同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_DBK_M  WHERE HEAT_PLAN_NO = ?";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnDbk.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
						// message = "该判定炉号(" + judgeStoveNo
						// + "),镦拔扩是利库计划,不能取消确认,请联系相关人员删除炉计划!";
						// throw new Exception(message);
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_DBK_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnDbk.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}
			} else if (proPlanIdNext.contains("RCL")) {
				processCode = "F";
				// 热处理
				plnRcl = getPlanC(con, jugdeApplyCode, judgeStoveNo,
						qcmGp.getBatchNo(), qcmGp.getBatchGroudNo(), "F",
						heatPlanNoNext, PlnZyRclCEntity.class);
				if (plnRcl == null) {
					continue;
				}
				count++;
				// 撤销判定必须先删除炉计划
//				if("1".equals(isJugde)){
//					message = "该判定炉号(" + judgeStoveNo + "),热处理已生成炉计划,不能撤销判定,请联系相关人员撤销炉计划!";
//					throw new Exception(message);
//				}
				if (!plnRcl.getPlanstatus().equals("00")
						&& !plnRcl.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),热处理不 是计划和下发状态,不能取消确认,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnRcl.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),热处理是利库计划,不能取消确认,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_RCL_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnRcl.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();
				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_RCL_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnRcl.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnRcl.getIfMatchOdd().equals("1")) {
						// 如果是利库炉计划则同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_RCL_M  WHERE HEAT_PLAN_NO = ?";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnRcl.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_RCL_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnRcl.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}
			} else if (proPlanIdNext.contains("JGX")) {
				processCode = "G";
				// 加工线
				plnJgx = getPlanC(con, jugdeApplyCode, judgeStoveNo,
						qcmGp.getBatchNo(), qcmGp.getBatchGroudNo(), "G",
						heatPlanNoNext, PlnZyJgxCEntity.class);
				if (plnJgx == null) {
					continue;
				}
				count++;
				if (!plnJgx.getPlanstatus().equals("00")
						&& !plnJgx.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),加工线不 是计划和下发状态,不能取消确认,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJgx.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),加工线是利库计划,不能取消确认,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JGX_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();
				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJgx.getIfMatchOdd().equals("1")) {
						deletesqlZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ?";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_JGX_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}
			} else if (proPlanIdNext.contains("JG_")) {
				processCode = "H";
				// 加工线
				PlnZyJgCEntity plnJg = getPlanC(con, jugdeApplyCode,
						judgeStoveNo, qcmGp.getBatchNo(),
						qcmGp.getBatchGroudNo(), "H", heatPlanNoNext,
						PlnZyJgCEntity.class);
				if (plnJg == null) {
					continue;
				}
				count++;
				if (!plnJg.getPlanstatus().equals("00")
						&& !plnJg.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),接箍不 是计划和下发状态,不能取消,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJg.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),接箍是利库计划,不能取消,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JG_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJg.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();
				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJg.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJg.getIfMatchOdd().equals("1")) {
						deletesqlZg = "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ?";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJg.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_JG_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnJg.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}

			}

			if (count > 0) {
				// 修改管坯MAT_BC_M挂料状态
				String updatesql = CoreXmlSqlParsersFactory
						.getSql("PlanFurnProduction.updateMatBcMR");
				ps = con.prepareStatement(updatesql);
				ps.setObject(1, jugdeApplyCode);
				ps.setObject(2, judgeStoveNo);
				ps.setObject(3, qcmGp.getBatchNo());
				ps.setObject(4, qcmGp.getBatchGroudNo());
				ps.setObject(5, heatPlanNoNext);
				ps.setObject(6, processCode);
				ps.setObject(7, planInNum);
				ps.setObject(8, "0");
				ps.setObject(9, "80150102");
				ps.executeUpdate();
				ps.close();
			}

		}

		// 修改本工序炉计划状态--生产完成
//		String updateZgM = "UPDATE PLN_ZY_ZG_M T SET T.PLANSTATUS='30',T.IS_CLOSE=''  WHere T.FEED_HEAT_NO=?  AND T.PLANSTATUS='40'";
//		ps = con.prepareStatement(updateZgM);
//		ps.setObject(1, judgeStoveNo);
//		ps.executeUpdate();
//		ps.close();
	}

	/**
	 * 通过轧管判定表下工序订单，订单序号，判定炉号，查询下工序对应实际炉计划
	 */
	public static <T> T getPlanC(Connection con, String jugdeApplyCode,
			String judgeStoveNo, String BatchNo, String BatchNoGroup,
			String processCode, String heatPlanNoNext, Class<T> cls)
			throws SQLException {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyDbkC");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyRclC");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyJgxC");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyJgC");
		}
		if (!heatPlanNoNext.equals("")) {
			sql += " AND T.HEAT_PLAN_NO='" + heatPlanNoNext + "'";
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		ps.setObject(2, BatchNo);
		ps.setObject(3, BatchNoGroup);
		ps.setObject(4, jugdeApplyCode);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 通过轧管判定表下工序订单，订单序号，判定炉号，查询下工序对应虚拟炉计划
	 */
	public static <T> T getPlanM(Connection con, String proPlanId,
			String gxPlanNo, String judgeStoveNo, String processCode,
			String heatPlanNoNext, Class<T> cls) throws SQLException {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyDbk");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyRcl");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyJgx");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.QueryPlnZyJg");
		}
		if (!heatPlanNoNext.equals("")) {
			sql += " WHERE T.HEAT_PLAN_NO= '" + heatPlanNoNext + "'  ";
		} else {
			sql += " WHERE T.PRO_PLAN_ID= '" + proPlanId
					+ "'  AND T.GX_PLAN_NO = '" + gxPlanNo + "'";
		}
		if (processCode.equals("H")) {
			sql += " AND T.JUDGE_STOVE_NO=?";
		} else {
			sql += " AND T.Feed_Heat_No=?";
		}

		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/***
	 * 组炉调用生成下工序炉计划和撤销
	 * 
	 * @param proPlanId
	 * @param gxPlanNo
	 * @param createName
	 * @param heatPlanNo
	 * @return
	 */
	public CoreReturnObject addPlnFurn(String proPlanId, String gxPlanNo,
			String createName, String heatPlanNo, String processCode,
			String flag) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		try {
			con.setAutoCommit(false); // 取消自动提交事务

			String plnDivideId = "";
			String mess = "";
			if (processCode.equals("E")) {

				String sql = "SELECT T.PLN_DIVIDE_ID FROM PLN_ORDER_DBK_S T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, proPlanId);
				ps.setObject(2, gxPlanNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					plnDivideId = rs.getString("PLN_DIVIDE_ID");
				}
				rs.close();
				ps.close();

				if (flag.equals("1")) {
					mess = addPlnFurnRcl(proPlanId, gxPlanNo, createName,
							systime, heatPlanNo, plnDivideId, con);

				} else {
					mess = deletePlnFurnRcl(proPlanId, gxPlanNo, createName,
							systime, heatPlanNo, plnDivideId, con);
				}

			} else if (processCode.equals("F")) {

				String sql = "SELECT T.PLN_DIVIDE_ID FROM PLN_ORDER_RCL_S T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, proPlanId);
				ps.setObject(2, gxPlanNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					plnDivideId = rs.getString("PLN_DIVIDE_ID");
				}
				rs.close();
				ps.close();
				if (flag.equals("1")) {
					mess = addPlnFurnJgx(proPlanId, gxPlanNo, createName,
							systime, heatPlanNo, plnDivideId, con,"","");
				} else {
					mess = deletePlnFurnJgx(proPlanId, gxPlanNo, createName,
							systime, heatPlanNo, plnDivideId, con);
				}

			} else if (processCode.equals("G")) {

				String sql = "SELECT T.PLN_DIVIDE_ID FROM PLN_ORDER_JGX_S T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, proPlanId);
				ps.setObject(2, gxPlanNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					plnDivideId = rs.getString("PLN_DIVIDE_ID");
				}
				rs.close();
				ps.close();
				if (flag.equals("1")) {
					mess = addPlnFurnJg(proPlanId, gxPlanNo, createName,
							systime, heatPlanNo, plnDivideId, con);
				} else {
					mess = deletePlnFurnJg(proPlanId, gxPlanNo, createName,
							systime, heatPlanNo, plnDivideId, con);
				}

			}
			if (!mess.equals("")) {
				cro.setV_errMsg(mess);
				return cro;
			}

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

	/**
	 * 镦拔扩炉计划下发根据工序生成对应虚拟炉计划(自炼)
	 */
	public static String addPlnFurnRcl(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con) throws SQLException {
		String mess = "";
		// 获取数据库当前时间
		// String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		// 获取当前下发镦拔扩炉号
		List<PlnZyDbkMEntity> plnZyPln = getPlanZyDbkM(con, proPlanId,
				gxPlanNo, heatPlanNo, "add", "");
		if (plnZyPln == null) {
			mess = "未查到镦拔扩炉计划信息,请确认!";
			return mess;
		}

		for (PlnZyDbkMEntity stordDbk : plnZyPln) {
//			if (!stordDbk.getHeatPlanNoOld().equals("")) {
//				continue;
//			}
			// 判断下工序
			if (!stordDbk.getNextProcessCode().equals("")) {// 下工序不为空时判断经过那个工序
				if (stordDbk.getNextProcessCode().equals("F")) {// 热处理工序
					List<PlnOrderRclSEntity> plnRcl = getPlanOrder(con,
							stordDbk.getOrdLnDlyPk(), plnDivideId, "F",
							PlnOrderRclSEntity.class);
					if (plnRcl.size() <= 0) {
						// mess = "未找热处理排产订单,请确认!";
						// return mess;
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成虚拟炉计划
					if (furnPlan.addPlnZyRclMD(con, stordDbk, plnRcl,
							createName, dateTime)) {
						continue;
					}
				} else if (stordDbk.getNextProcessCode().equals("G")) {// 加工工序
					if (!stordDbk.getWholeBacklog().contains("G")
							&& stordDbk.getOrderSource().equals("备料需求")) {
						// 查询备料需求与成品对应关系
						String sqlBL = "SELECT T.ORD_LN_DLY_PK FROM PLN_PORDER_BL_REQ T WHERE T.REQ_BL_ID=?";
						PreparedStatement ps = con.prepareStatement(sqlBL);
						ps.setObject(1, stordDbk.getOrdLnDlyPk());
						ResultSet rs = ps.executeQuery();
						if (rs.next()) {
							stordDbk.setOrdLnDlyPk(rs
									.getString("ORD_LN_DLY_PK"));
							plnDivideId = "01";
						}
						rs.close();
						ps.close();
					}
					List<PlnOrderJgxSEntity> plnJgx = getPlanOrder(con,
							stordDbk.getOrdLnDlyPk(), plnDivideId, "G",
							PlnOrderJgxSEntity.class);
					if (plnJgx.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成虚拟炉计划
					if (furnPlan.addPlnZyJgxMD(con, stordDbk, plnJgx,
							createName, dateTime)) {
						continue;
					}
				} else if (stordDbk.getNextProcessCode().equals("H")) {
					List<PlnGxJgPropSEntity> plnRcl = getPlanOrder(con,
							stordDbk.getOrdLnDlyPk(), plnDivideId, "H",
							PlnGxJgPropSEntity.class);
					if (plnRcl.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成接箍虚拟炉计划
					if (furnPlan.addPlnZyJgDME(con, stordDbk, plnRcl,
							createName, dateTime)) {
						continue;
					}
				}
			}
			// else {// 轧管出成品并且是接箍备料合同
			// if (stordDbk.getOrdLnDlyPk().contains("ZZBY-J")) {
			// List<PlnGxJgPropSEntity> plnRcl = getPlanOrder(con,
			// stordDbk.getOrdLnDlyPk(), plnDivideId, "H",
			// PlnGxJgPropSEntity.class);
			// if (plnRcl.size() <= 0) {
			// continue;
			// }
			// corePlanFurnProM furnPlan = new corePlanFurnProM();
			// // 生成接箍虚拟炉计划
			// if (furnPlan.addPlnZyJgDME(con, stordDbk, plnRcl,
			// createName, dateTime)) {
			// continue;
			// }
			// }
			// }
		}
		return mess;
	}

	/**
	 * 镦拔扩炉计划取消下发根据工序删除虚拟炉计划(自炼)
	 */
	public static String deletePlnFurnRcl(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con) throws SQLException {
		String mess = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		// 获取当前下发炉次订单信息
		List<PlnZyDbkMEntity> plnZyPln = getPlanZyDbkM(con, proPlanId,
				gxPlanNo, heatPlanNo, "delete", "");
		if (plnZyPln == null) {
			mess = "未查到镦拔扩炉计划信息,请确认!";
			return mess;
		}
		for (PlnZyDbkMEntity stord : plnZyPln) {
//			if (!stord.getHeatPlanNoOld().equals("")) {
//				continue;
//			}
			if (!stord.getNextProcessCode().equals("")) {
				// 删除虚拟炉计划
				if (stord.getNextProcessCode().equals("F")) {// 热处理工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_RCL_M  T WHERE T.UPPER_HEATPLAN_ID=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的热处理虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_RCL_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getJudgeStoveNo()
									+ "的热处理虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_RCL_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();

						// 更新订单虚拟挂料量
						// String update =
						// "UPDATE PLN_ORDER_RCL_S T SET T.BATCHED_NUM=TO_NUMBER(NVL(T.BATCHED_NUM,0))-TO_NUMBER(?),"
						// +
						// "T.BATCHED_WT=TO_NUMBER(NVL(T.BATCHED_WT,0))-TO_NUMBER(?) WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
						// ps = con.prepareStatement(update);
						// ps.setObject(1, rs.getString("PLN_INSINGLE_NUM"));
						// ps.setObject(2, rs.getString("PLN_INSINGLE_WT"));
						// ps.setObject(3, rs.getString("PRO_PLAN_ID"));
						// ps.setObject(4, rs.getString("GX_PLAN_NO"));
						// ps.executeUpdate();
						// ps.close();
					}
					rs.close();
					ps.close();
				} else if (stord.getNextProcessCode().equals("G")) {// 加工工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JGX_M  T WHERE T.UPPER_HEATPLAN_ID=? ";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的加工线虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getJudgeStoveNo()
									+ "的加工线虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();
						// 更新订单虚拟挂料量
						// String update =
						// "UPDATE PLN_ORDER_JGX_S T SET T.BATCHED_NUM=TO_NUMBER(NVL(T.BATCHED_NUM,0))-TO_NUMBER(?),"
						// +
						// "T.BATCHED_WT=TO_NUMBER(NVL(T.BATCHED_WT,0))-TO_NUMBER(?) WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
						// ps = con.prepareStatement(update);
						// ps.setObject(1, rs.getString("PLN_INSINGLE_NUM"));
						// ps.setObject(2, rs.getString("PLN_INSINGLE_WT"));
						// ps.setObject(3, rs.getString("PRO_PLAN_ID"));
						// ps.setObject(4, rs.getString("GX_PLAN_NO"));
						// ps.executeUpdate();
						// ps.close();

					}
					rs.close();
					ps.close();
				} else if (stord.getNextProcessCode().equals("H")) {
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JG_M  T WHERE T.UPPER_HEATPLAN_ID=? AND T.ORD_LN_DLY_PK = ? ";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					ps.setObject(2, stord.getOrdLnDlyPk());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();
				}
			}
			// else {
			// if (stord.getOrdLnDlyPk().contains("ZZBY-J")) {
			// sql =
			// "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JG_M  T WHERE T.UPPER_HEATPLAN_ID=? AND T.ORD_LN_DLY_PK = ? ";
			// ps = con.prepareStatement(sql);
			// ps.setObject(1, stord.getHeatPlanNo());
			// ps.setObject(2, stord.getOrdLnDlyPk());
			// rs = ps.executeQuery();
			// while (rs.next()) {
			// int countNum = 0;
			// if (!rs.getString("PLANSTATUS").equals("00")) {
			// mess = "判定炉号:" + stord.getFeedHeatNo()
			// + "的接箍虚拟炉计划已下发,不能撤销";
			// return mess;
			// }
			// // 查询是否存在实际炉计划
			// String sqlZgC =
			// "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE T.HEAT_PLAN_NO=?";
			// PreparedStatement psC = con.prepareStatement(sqlZgC);
			// psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
			// ResultSet rsC = psC.executeQuery();
			// if (rsC.next()) {
			// countNum = rsC.getInt("COUNT");
			// }
			// rsC.close();
			// psC.close();
			// if (countNum > 0) {
			// mess = "判定炉号:" + stord.getFeedHeatNo()
			// + "的接箍虚拟炉计划存在实际炉计划,不能撤销";
			// return mess;
			// }
			// // 删除虚拟炉计划
			// String sqlDeleteZg =
			// "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
			// psC = con.prepareStatement(sqlDeleteZg);
			// psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
			// psC.executeUpdate();
			// psC.close();
			// }
			// rs.close();
			// ps.close();
			// }
			// }
		}
		return mess;
	}

	/**
	 * 镦拔扩判定生成下工序实际炉计划
	 * 
	 * @throws Exception
	 */
	public static void addPlnZyCD(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		// 获取数据库当前时间
		String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		// 通过判定单号，判定炉号查询判定信息
		List<QcmZgJugdeApplyEntity> qcmApp = getQcmZgApp(con, jugdeApplyCode,
				judgeStoveNo, "E", batchNo);
		if (qcmApp == null) {
			message = "判定信息已改变,请刷新判定数据!";
			throw new Exception(message);
		}
		String heatPlanNo = "";
		String processCode = "";
		for (QcmZgJugdeApplyEntity qcmGp : qcmApp) {
			// 查询库存信息循环生成实绩炉计划
			List<MatBcMEntityPln> matApp = getMatBcMAdd(con, jugdeApplyCode,
					judgeStoveNo, "E", qcmGp.getBatchNo(), "add");
			if (matApp == null) {
				continue;
				// message = "未找到库存信息,确认是否有库存!";
				// throw new Exception(message);
			}
			for (MatBcMEntityPln matGp : matApp) {
				String heatPlanNoDbk = matGp.getHeatPlanNo();
				String proPlanIdNext = matGp.getProPlanIdNext();
				String gxPlanNoNext = matGp.getGxPlanNoNext();
				String heatPlanNoNext = matGp.getHeatPlanNoNext();
				int planInNum = Integer.parseInt(matGp.getActCount());
				// double planInWt = Double
				// .parseDouble(matGp.getActTheoryWeight());// 实重
				// 判定是否存在轧管虚拟卢计划
				// List<PlnZyDbkMEntity> plnZg = getPlanZyDbkM(con, proPlanId,
				// gxPlanNo, "", "", judgeStoveNo);
				// if (plnZg == null) {
				// // message += "未找到镦拔扩虚拟炉计划,";
				// continue;
				// }
				PlnZyRclMEntity plnRcl = null;
				PlnZyJgxMEntity plnJgx = null;
				int planDbkCont = 0;
				corePlanFurnProM furnPlan = new corePlanFurnProM();
				if (proPlanIdNext.contains("RCL")) {
					processCode = "F";
					// 热处理
					plnRcl = getPlanM(con, proPlanIdNext, gxPlanNoNext,
							judgeStoveNo, "F", heatPlanNoNext,
							PlnZyRclMEntity.class);
					if (plnRcl == null) {
						continue;
					}

					if (plnRcl.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnRcl.getHeatPlanNo();
					if (Integer.parseInt(plnRcl.getPlanstatus()) < 30) {
						planDbkCont++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "F", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_RCL_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成轧管实际炉计划
						if (furnPlan.addPlnZyC(con, null, plnRcl, null, null,
								createName, dateTime, matGp, "F")) {
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "E");
						ps.executeUpdate();
						ps.close();
					}
				} else if (proPlanIdNext.contains("JGX")) {
					processCode = "G";
					// 加工线
					plnJgx = getPlanM(con, proPlanIdNext, gxPlanNoNext,
							judgeStoveNo, "G", heatPlanNoNext,
							PlnZyJgxMEntity.class);
					if (plnJgx == null) {
						continue;
					}
					if (plnJgx.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJgx.getHeatPlanNo();
					if (Integer.parseInt(plnJgx.getPlanstatus()) < 20) {
						planDbkCont++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "G", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JGX_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成轧管实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, plnJgx, null,
								createName, dateTime, matGp, "G")) {
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "E");
						ps.executeUpdate();
						ps.close();
					}
				} else if (proPlanIdNext.contains("JG_")) {
					processCode = "H";
					// 接箍
					PlnZyJgMEntity plnJg = getPlanM(con, proPlanIdNext,
							gxPlanNoNext, judgeStoveNo, "H", heatPlanNoNext,
							PlnZyJgMEntity.class);
					if (plnJg == null) {
						continue;
					}
					if (plnJg.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJg.getHeatPlanNo();
					if (Integer.parseInt(plnJg.getPlanstatus()) < 30) {
						planDbkCont++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "H", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JG_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成加工线实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, null, plnJg,
								createName, dateTime, matGp, "H")) {
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "E");
						ps.executeUpdate();
						ps.close();
					}
				}
				if (planDbkCont > 0) {
					// 修改管坯MAT_BC_M挂料状态
					String updatesql = CoreXmlSqlParsersFactory
							.getSql("PlanFurnProduction.updateMatBcMR");
					ps = con.prepareStatement(updatesql);
					ps.setObject(1, jugdeApplyCode);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, matGp.getBatchNo());
					ps.setObject(4, matGp.getBatchGroudNo());
					ps.setObject(5, heatPlanNoNext);
					ps.setObject(6, processCode);
					ps.setObject(7, planInNum);
					ps.setObject(8, "1");
					ps.setObject(9, "80150103");
					ps.executeUpdate();
					ps.close();
				}
//				// 判定关闭镦拔扩炉计划
//				int countColse = 0;
//				int countdBK = 0;
//				// 查询炉计划出库支
//				String sqlMil = "SELECT NVL(SUM(T.REAL_OUTDOUBLE_NUM), 0) REAL_OUTDOUBLE_NUM FROM PLN_ZY_DBK_C T ,PLN_ZY_DBK_M T1 WHERE T.HEAT_PLAN_NO = T1.HEAT_PLAN_NO AND T.JUDGE_STOVE_NO = ? AND T1.PLANSTATUS='30'";
//				ps = con.prepareStatement(sqlMil);
//				ps.setObject(1, judgeStoveNo);
//				ResultSet rs = ps.executeQuery();
//				if (rs.next()) {
//					countdBK = rs.getInt("REAL_OUTDOUBLE_NUM");
//				}
//				rs.close();
//				ps.close();
//
//				// 查询判定表已判定支
//				sqlMil = "SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1  WHERE T1.PROCESS_CODE = 'E'  AND T1.VALIDFLAG = '20'  AND T1.IS_FLAG = '0' AND T1.JUDGE_STOVE_NO = ?";
//				ps = con.prepareStatement(sqlMil);
//				ps.setObject(1, judgeStoveNo);
//				rs = ps.executeQuery();
//				if (rs.next()) {
//					countColse = rs.getInt("ACT_COUNT");
//				}
//				rs.close();
//				ps.close();
////				if (qcmGp.getJudgeResult().equals("40740701")) {
//					if (countdBK - countColse <= 0) {
//						// 修改本工序炉计划状态--关闭
//						String updateZgM = "UPDATE PLN_ZY_DBK_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0'  WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='30'";
//						ps = con.prepareStatement(updateZgM);
//						ps.setObject(1, judgeStoveNo);
//						ps.executeUpdate();
//						ps.close();
//					}
////				}
				// if (processCode.equals("F")) {
				// // 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialRcl.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
				// } else if (processCode.equals("G")) {
				// // 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialJgx.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
				// }
			}
		}
	}

	/**
	 * 镦拔扩判定回退，判定不合删除实际炉计划
	 * 
	 * @param con
	 * @throws Exception
	 */
	public static void deletePlnCD(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		// 通过判定单号，判定炉号查询库存信息
		List<MatBcMEntityPln> qcmApp = getMatBcMAdd(con, jugdeApplyCode,
				judgeStoveNo, "E", batchNo, "delete");
		if (qcmApp == null) {
			message = "未找到库存信息,确认是否有库存!";
			throw new Exception(message);
		}
		for (MatBcMEntityPln qcmGp : qcmApp) {
			// 如果是末工序跳出执行下一循环
			// if (qcmGp.getNextProcessCdoe().equals("")) {
			// continue;
			// }
			// String proPlanId = qcmGp.getProOrderId();
			// String gxPlanNo = qcmGp.getGxPlanNo();
			String proPlanIdNext = qcmGp.getProPlanIdNext();
			// String gxPlanNoNext = qcmGp.getGxPlanNoNext();
			String heatPlanNoNext = qcmGp.getHeatPlanNoNext();
			int planInNum = Integer.parseInt(qcmGp.getActCount());
			// 判定是否存在轧管虚拟卢计划
			// List<PlnZyDbkMEntity> plnZg = getPlanZyDbkM(con, proPlanId,
			// gxPlanNo, "", "", judgeStoveNo);
			// if (plnZg == null) {
			// // message += "未找到镦拔扩虚拟炉计划,";
			// continue;
			// }
			PlnZyRclCEntity plnRcl = null;
			PlnZyJgxCEntity plnJgx = null;
			String processCode = "";
			int count = 0;
			if (proPlanIdNext.contains("RCL")) {
				processCode = "F";
				// 热处理
				plnRcl = getPlanC(con, jugdeApplyCode, judgeStoveNo,
						qcmGp.getBatchNo(), qcmGp.getBatchGroudNo(), "F",
						heatPlanNoNext, PlnZyRclCEntity.class);
				if (plnRcl == null) {
					continue;
				}
				count++;
				if (!plnRcl.getPlanstatus().equals("00")
						&& !plnRcl.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),热处理不是计划和下发状态,不能取消确认,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnRcl.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),热处理是利库计划,不能取消确认,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_RCL_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnRcl.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();
				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_RCL_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnRcl.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnRcl.getIfMatchOdd().equals("1")) {
						// 如果是利库计划同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_RCL_M  WHERE HEAT_PLAN_NO = ? ";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnRcl.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_RCL_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnRcl.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}
			} else if (proPlanIdNext.contains("JGX")) {
				processCode = "G";
				// 加工线
				plnJgx = getPlanC(con, jugdeApplyCode, judgeStoveNo,
						qcmGp.getBatchNo(), qcmGp.getBatchGroudNo(), "E",
						heatPlanNoNext, PlnZyJgxCEntity.class);
				if (plnJgx == null) {
					continue;
				}
				count++;
				if (!plnJgx.getPlanstatus().equals("00")
						&& !plnJgx.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),加工线不是计划和下发状态,不能取消确认,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJgx.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),加工线是利库计划,不能取消确认,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JGX_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();
				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJgx.getIfMatchOdd().equals("1")) {
						// 如果是利库计划同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_JGX_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}

			} else if (proPlanIdNext.contains("JG_")) {
				processCode = "H";
				// 加工线
				PlnZyJgCEntity plnJg = getPlanC(con, jugdeApplyCode,
						judgeStoveNo, qcmGp.getBatchNo(),
						qcmGp.getBatchGroudNo(), "H", heatPlanNoNext,
						PlnZyJgCEntity.class);
				if (plnJg == null) {
					continue;
				}
				count++;
				if (!plnJg.getPlanstatus().equals("00")
						&& !plnJg.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),接箍不是计划和下发状态,不能取消,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJg.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),接箍是利库计划,不能取消,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JG_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJg.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();
				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJg.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJg.getIfMatchOdd().equals("1")) {
						// 如果是利库计划同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_JG_M WHERE HEAT_PLAN_NO = ?";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJg.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
					// 修改虚拟炉计划状态
					String update = "UPDATE PLN_ZY_JG_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
					ps = con.prepareStatement(update);
					ps.setObject(1, plnJg.getHeatPlanNo());
					ps.executeUpdate();
					ps.close();
				}

			}

			if (count > 0) {
				// 修改管坯MAT_BC_M挂料状态
				String updatesql = CoreXmlSqlParsersFactory
						.getSql("PlanFurnProduction.updateMatBcMR");
				ps = con.prepareStatement(updatesql);
				ps.setObject(1, jugdeApplyCode);
				ps.setObject(2, judgeStoveNo);
				ps.setObject(3, qcmGp.getBatchNo());
				ps.setObject(4, qcmGp.getBatchGroudNo());
				ps.setObject(5, heatPlanNoNext);
				ps.setObject(6, processCode);
				ps.setObject(7, planInNum);
				ps.setObject(8, "0");
				ps.setObject(9, "80150102");
				ps.executeUpdate();
				ps.close();
			}
			// 修改本工序炉计划状态--生产完成
//			String updateZgM = "UPDATE PLN_ZY_DBK_M T SET T.PLANSTATUS='30',T.IS_CLOSE=''  WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='40'";
//			ps = con.prepareStatement(updateZgM);
//			ps.setObject(1, judgeStoveNo);
//			ps.executeUpdate();
//			ps.close();
		}
	}

	/**
	 * 热处理炉计划下发根据工序生成对应虚拟炉计划(自炼)
	 */
	public static String addPlnFurnJgx(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con,String delievStateCode,String delievStateName) throws SQLException {
		String mess = "";
		// 获取数据库当前时间
		// String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		// 获取当前下发镦拔扩炉号
		List<PlnZyRclMEntity> plnZyPln = getPlanZyRclM(con, proPlanId,
				gxPlanNo, heatPlanNo, "add", "");
		if (plnZyPln == null) {
			// mess = "未查到热处理炉计划信息,请确认!";
			return mess;
		}
		for (PlnZyRclMEntity stordRcl : plnZyPln) {
//			if (!stordRcl.getHeatPlanNoOld().equals("")) {
//				continue;
//			}
			// 判断下工序
			if (!stordRcl.getNextProcessCode().equals("")) {// 下工序不为空时判断经过那个工序
				if (stordRcl.getNextProcessCode().equals("G")) {// 加工工序
					if (!stordRcl.getWholeBacklog().contains("G")
							&& stordRcl.getOrderSource().equals("备料需求")) {
						// 查询备料需求与成品对应关系
						String sqlBL = "SELECT T.ORD_LN_DLY_PK FROM PLN_PORDER_BL_REQ T WHERE T.REQ_BL_ID=?";
						PreparedStatement ps = con.prepareStatement(sqlBL);
						ps.setObject(1, stordRcl.getOrdLnDlyPk());
						ResultSet rs = ps.executeQuery();
						if (rs.next()) {
							stordRcl.setOrdLnDlyPk(rs
									.getString("ORD_LN_DLY_PK"));
							plnDivideId = "01";
						}
						rs.close();
						ps.close();
					}
					List<PlnOrderJgxSEntity> plnJgx = getPlanOrder(con,
							stordRcl.getOrdLnDlyPk(), plnDivideId, "G",
							PlnOrderJgxSEntity.class);
					if (plnJgx.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成虚拟炉计划
					if (furnPlan.addPlnZyJgxME(con, stordRcl, plnJgx,
							createName, dateTime,delievStateCode,delievStateName)) {
						continue;
					}
				} else if (stordRcl.getNextProcessCode().equals("H")) {
					List<PlnGxJgPropSEntity> plnRcl = getPlanOrder(con,
							stordRcl.getOrdLnDlyPk(), plnDivideId, "H",
							PlnGxJgPropSEntity.class);
					if (plnRcl.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成接箍虚拟炉计划
					if (furnPlan.addPlnZyJgRME(con, stordRcl, plnRcl,
							createName, dateTime)) {
						continue;
					}
				}
			}
			// else {
			// if (stordRcl.getOrdLnDlyPk().contains("ZZBY-J")) {
			// List<PlnGxJgPropSEntity> plnRcl = getPlanOrder(con,
			// stordRcl.getOrdLnDlyPk(), plnDivideId, "H",
			// PlnGxJgPropSEntity.class);
			// if (plnRcl.size() <= 0) {
			// continue;
			// }
			// corePlanFurnProM furnPlan = new corePlanFurnProM();
			// // 生成接箍虚拟炉计划
			// if (furnPlan.addPlnZyJgRME(con, stordRcl, plnRcl,
			// createName, dateTime)) {
			// continue;
			// }
			// }
			// }
		}
		return mess;
	}

	/**
	 * 热处理炉计划取消下发根据工序删除虚拟炉计划(自炼)
	 */
	public static String deletePlnFurnJgx(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con) throws SQLException {
		String mess = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		// 获取当前下发炉次订单信息
		List<PlnZyRclMEntity> plnZyPln = getPlanZyRclM(con, proPlanId,
				gxPlanNo, heatPlanNo, "delete", "");
		if (plnZyPln == null) {
			// mess = "未查到热处理炉计划信息,请确认!";
			return mess;
		}
		for (PlnZyRclMEntity stord : plnZyPln) {
//			if (!stord.getHeatPlanNoOld().equals("")) {
//				continue;
//			}
			if (!stord.getNextProcessCode().equals("")) {
				// 删除虚拟炉计划
				if (stord.getNextProcessCode().equals("G")) {// 加工工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JGX_M  T WHERE T.UPPER_HEATPLAN_ID=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的加工线虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的加工线虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();
						// 更新订单虚拟挂料量
						// String update =
						// "UPDATE PLN_ORDER_JGX_S T SET T.BATCHED_NUM=TO_NUMBER(NVL(T.BATCHED_NUM,0))-TO_NUMBER(?),"
						// +
						// "T.BATCHED_WT=TO_NUMBER(NVL(T.BATCHED_WT,0))-TO_NUMBER(?) WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
						// ps = con.prepareStatement(update);
						// ps.setObject(1, rs.getString("PLN_INSINGLE_NUM"));
						// ps.setObject(2, rs.getString("PLN_INSINGLE_WT"));
						// ps.setObject(3, rs.getString("PRO_PLAN_ID"));
						// ps.setObject(4, rs.getString("GX_PLAN_NO"));
						// ps.executeUpdate();
						// ps.close();

					}
					rs.close();
					ps.close();
				} else if (stord.getNextProcessCode().equals("H")) {
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JG_M  T WHERE T.UPPER_HEATPLAN_ID=? AND T.ORD_LN_DLY_PK = ?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					ps.setObject(2, stord.getOrdLnDlyPk());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();
				}
			}
			// else {
			// if (stord.getOrdLnDlyPk().contains("ZZBY-J")) {
			// sql =
			// "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JG_M  T WHERE T.UPPER_HEATPLAN_ID=? AND T.ORD_LN_DLY_PK = ?";
			// ps = con.prepareStatement(sql);
			// ps.setObject(1, stord.getHeatPlanNo());
			// ps.setObject(2, stord.getOrdLnDlyPk());
			// rs = ps.executeQuery();
			// while (rs.next()) {
			// int countNum = 0;
			// if (!rs.getString("PLANSTATUS").equals("00")) {
			// mess = "判定炉号:" + stord.getFeedHeatNo()
			// + "的接箍虚拟炉计划已下发,不能撤销";
			// return mess;
			// }
			// // 查询是否存在实际炉计划
			// String sqlZgC =
			// "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE T.HEAT_PLAN_NO=?";
			// PreparedStatement psC = con.prepareStatement(sqlZgC);
			// psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
			// ResultSet rsC = psC.executeQuery();
			// if (rsC.next()) {
			// countNum = rsC.getInt("COUNT");
			// }
			// rsC.close();
			// psC.close();
			// if (countNum > 0) {
			// mess = "判定炉号:" + stord.getFeedHeatNo()
			// + "的接箍虚拟炉计划存在实际炉计划,不能撤销";
			// return mess;
			// }
			// // 删除虚拟炉计划
			// String sqlDeleteZg =
			// "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
			// psC = con.prepareStatement(sqlDeleteZg);
			// psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
			// psC.executeUpdate();
			// psC.close();
			// }
			// rs.close();
			// ps.close();
			// }
			// }
		}
		return mess;
	}

	/**
	 * 热处理判定生成下工序实际炉计划
	 * 
	 * @throws Exception
	 */
	public static void addPlnZyCF(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		// 获取数据库当前时间
		String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		// 通过判定单号，判定炉号查询判定信息
		List<QcmZgJugdeApplyEntity> qcmApp = getQcmZgApp(con, jugdeApplyCode,
				judgeStoveNo, "F", batchNo);
		if (qcmApp == null) {
			message = "判定信息已改变,请刷新判定数据!";
			throw new Exception(message);
		}
		String heatPlanNo = "";
		String processCode = "";
		for (QcmZgJugdeApplyEntity qcmGp : qcmApp) {
			List<MatBcMEntityPln> matApp = getMatBcMAdd(con, jugdeApplyCode,
					judgeStoveNo, "F", qcmGp.getBatchNo(), "add");
			if (matApp == null) {
				// message = "未找到库存信息,确认是否有库存!";
				// throw new Exception(message);
				continue;
			}
			for (MatBcMEntityPln matGp : matApp) {
				String proPlanIdNext = matGp.getProPlanIdNext();
				String gxPlanNoNext = matGp.getGxPlanNoNext();
				int planInNum = Integer.parseInt(matGp.getActCount());
				String heatPlanNoNext = matGp.getHeatPlanNoNext();
				// double planInWt = Double
				// .parseDouble(matGp.getActTheoryWeight());// 实重
				// 判定是否存在虚拟炉计划
				// List<PlnZyRclMEntity> plnZg = getPlanZyRclM(con, proPlanId,
				// gxPlanNo, "", "", judgeStoveNo);
				// if (plnZg == null) {
				// // message += "未找到热处理虚拟炉计划,";
				// continue;
				// }
				PlnZyJgxMEntity plnJgx = null;

				int planJgxCount = 0;
				corePlanFurnProM furnPlan = new corePlanFurnProM();
				if (proPlanIdNext.contains("JGX")) {
					processCode = "G";
					// 加工线
					plnJgx = getPlanM(con, proPlanIdNext, gxPlanNoNext,
							judgeStoveNo, "G", heatPlanNoNext,
							PlnZyJgxMEntity.class);
					if (plnJgx == null) {
						continue;
					}
					if (plnJgx.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJgx.getHeatPlanNo();
					if (Integer.parseInt(plnJgx.getPlanstatus()) < 30) {
						planJgxCount++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "G", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JGX_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成加工实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, plnJgx, null,
								createName, dateTime, matGp, "G")) {
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "F");
						ps.executeUpdate();
						ps.close();
					}
				} else if (proPlanIdNext.contains("JG_")) {
					processCode = "H";
					// 接箍线
					PlnZyJgMEntity plnJg = getPlanM(con, proPlanIdNext,
							gxPlanNoNext, judgeStoveNo, "H", heatPlanNoNext,
							PlnZyJgMEntity.class);
					if (plnJg == null) {
						continue;
					}
					if (plnJg.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJg.getHeatPlanNo();
					if (Integer.parseInt(plnJg.getPlanstatus()) < 30) {
						planJgxCount++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "H", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JG_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成接箍线实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, null, plnJg,
								createName, dateTime, matGp, "H")) {
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "F");
						ps.executeUpdate();
						ps.close();
					}
				}

				if (planJgxCount > 0) {
					// 修改管坯MAT_BC_M挂料状态
					String updatesql = CoreXmlSqlParsersFactory
							.getSql("PlanFurnProduction.updateMatBcMR");
					ps = con.prepareStatement(updatesql);
					ps.setObject(1, jugdeApplyCode);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, matGp.getBatchNo());
					ps.setObject(4, matGp.getBatchGroudNo());
					ps.setObject(5, heatPlanNoNext);
					ps.setObject(6, processCode);
					ps.setObject(7, planInNum);
					ps.setObject(8, "1");
					ps.setObject(9, "80150103");
					ps.executeUpdate();
					ps.close();
				}
				// 判定关闭热处理炉计划
//				int countColse = 0;
//				int countdBK = 0;
//				// 查询炉计划出库支
//				String sqlMil = "SELECT NVL(SUM(T.REAL_OUTDOUBLE_NUM), 0) REAL_OUTDOUBLE_NUM FROM PLN_ZY_RCL_C T,PLN_ZY_RCL_M T1 WHERE T.HEAT_PLAN_NO = T1.HEAT_PLAN_NO AND T.JUDGE_STOVE_NO = ? AND T1.PLANSTATUS='30'";
//				ps = con.prepareStatement(sqlMil);
//				ps.setObject(1, judgeStoveNo);
//				ResultSet rs = ps.executeQuery();
//				if (rs.next()) {
//					countdBK = rs.getInt("REAL_OUTDOUBLE_NUM");
//				}
//				rs.close();
//				ps.close();
//
//				// 查询判定表已判定支
//				sqlMil = "SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1  WHERE T1.PROCESS_CODE = 'F'  AND T1.VALIDFLAG = '20'  AND T1.IS_FLAG = '0' AND T1.JUDGE_STOVE_NO = ?";
//				ps = con.prepareStatement(sqlMil);
//				ps.setObject(1, judgeStoveNo);
//				rs = ps.executeQuery();
//				if (rs.next()) {
//					countColse = rs.getInt("ACT_COUNT");
//				}
//				rs.close();
//				ps.close();
////				if (qcmGp.getJudgeResult().equals("40740701")) {
//					if (countdBK - countColse <= 0) {
//						// 修改本工序炉计划状态--关闭
//						String updateZgM = "UPDATE PLN_ZY_RCL_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='30'";
//						ps = con.prepareStatement(updateZgM);
//						ps.setObject(1, judgeStoveNo);
//						ps.executeUpdate();
//						ps.close();
//					}
////				}
				// if (processCode.equals("G")) {
				// // 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialJgx.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
				// }
			}
		}
	}

	/**
	 * 热处理判定回退，判定不合删除实际炉计划
	 * 
	 * @param con
	 * @throws Exception
	 * @throws SQLException
	 */
	public static void deletePlnCF(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		// 通过判定单号，判定炉号查询库存信息
		List<MatBcMEntityPln> qcmApp = getMatBcMAdd(con, jugdeApplyCode,
				judgeStoveNo, "F", batchNo, "delete");
		if (qcmApp == null) {
			message = "未找到库存信息,确认是否有库存!";
			throw new Exception(message);
		}
		for (MatBcMEntityPln qcmGp : qcmApp) {
			// 如果是末工序跳出执行下一循环
			// if (qcmGp.getNextProcessCdoe().equals("")) {
			// continue;
			// }
			// String proPlanId = qcmGp.getProOrderId();
			// String gxPlanNo = qcmGp.getGxPlanNo();
			String proPlanIdNext = qcmGp.getProPlanIdNext();
			// String gxPlanNoNext = qcmGp.getGxPlanNoNext();
			String heatPlanNoNext = qcmGp.getHeatPlanNoNext();
			int planInNum = Integer.parseInt(qcmGp.getActCount());
			// 判定是否存在轧管虚拟卢计划
			// List<PlnZyRclMEntity> plnZg = getPlanZyRclM(con, proPlanId,
			// gxPlanNo, "", "", judgeStoveNo);
			// if (plnZg == null) {
			// // message += "未找到热处理虚拟炉计划,";
			// continue;
			// }
			PlnZyJgxCEntity plnJgx = null;
			String processCode = "";
			int count = 0;
			if (proPlanIdNext.contains("JGX")) {
				processCode = "G";
				// 加工线
				plnJgx = getPlanC(con, jugdeApplyCode, judgeStoveNo,
						qcmGp.getBatchNo(), qcmGp.getBatchGroudNo(), "G",
						heatPlanNoNext, PlnZyJgxCEntity.class);
				if (plnJgx == null) {
					continue;
				}
				count++;
				if (!plnJgx.getPlanstatus().equals("00")
						&& !plnJgx.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),加工线不是计划和下发状态,不能取消确认,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJgx.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),加工线是利库计划,不能取消确认,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JGX_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();

				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJgx.getIfMatchOdd().equals("1")) {
						// 如果是利库计划则同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_JGX_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}
			} else if (proPlanIdNext.contains("JG_")) {
				processCode = "H";
				// 加工线
				PlnZyJgCEntity plnJg = getPlanC(con, jugdeApplyCode,
						judgeStoveNo, qcmGp.getBatchNo(),
						qcmGp.getBatchGroudNo(), "H", heatPlanNoNext,
						PlnZyJgCEntity.class);
				if (plnJg == null) {
					continue;
				}
				count++;
				if (!plnJg.getPlanstatus().equals("00")
						&& !plnJg.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),接箍不是计划和下发状态,不能取消,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJg.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),接箍是利库计划,不能取消下发,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JG_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJg.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();

				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJg.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJg.getIfMatchOdd().equals("1")) {
						// 如果是利库计划则同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ?";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJg.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_JG_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnJg.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}

			}
			if (count > 0) {
				// 修改管坯MAT_BC_M挂料状态
				String updatesql = CoreXmlSqlParsersFactory
						.getSql("PlanFurnProduction.updateMatBcMR");
				ps = con.prepareStatement(updatesql);
				ps.setObject(1, jugdeApplyCode);
				ps.setObject(2, judgeStoveNo);
				ps.setObject(3, qcmGp.getBatchNo());
				ps.setObject(4, qcmGp.getBatchGroudNo());
				ps.setObject(5, heatPlanNoNext);
				ps.setObject(6, processCode);
				ps.setObject(7, planInNum);
				ps.setObject(8, "0");
				ps.setObject(9, "80150102");
				ps.executeUpdate();
				ps.close();
			}
			// 修改本工序炉计划状态--生产完成
//			String updateZgM = "UPDATE PLN_ZY_RCL_M T SET T.PLANSTATUS='30',T.IS_CLOSE='' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='40'";
//			ps = con.prepareStatement(updateZgM);
//			ps.setObject(1, judgeStoveNo);
//			ps.executeUpdate();
//			ps.close();
		}
	}

	/**
	 * 根据下工序订单号，排产序号，判定炉号查询材料表数据
	 */
	public static List<MatBcMEntityPln> getPlanMatBcM(Connection con,
			String proPlanId, String gxPlanNo, String judgeStoveNo)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.QueryMatBcM");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ps.setObject(2, gxPlanNo);
		ps.setObject(3, judgeStoveNo);
		// ps.setObject(4, processCode);
		ResultSet rs = ps.executeQuery();
		List<MatBcMEntityPln> list = EntityTool.getEntitysByRs(rs,
				MatBcMEntityPln.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatBcMEntityPln listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 根据下工序炉计划ID，判定炉号查询材料表数据
	 */
	public static List<MatBcMEntityPln> getPlanMatBcH(Connection con,
			String proPlanId, String judgeStoveNo) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PlanFurnProduction.QueryMatBcH");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ps.setObject(2, judgeStoveNo);
		ResultSet rs = ps.executeQuery();
		List<MatBcMEntityPln> list = EntityTool.getEntitysByRs(rs,
				MatBcMEntityPln.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatBcMEntityPln listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 通过订单号,排产序号，查询对应订单信息
	 */
	public static <T> T getPlanOrderNext(Connection con, String proPlanId,
			String gxPlanNo, String processCode, Class<T> cls)
			throws SQLException {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrderDbk");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrdeRcl");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrderJgx");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("PlanFurnProduction.queryPlnOrderJg");
		}
		sql += " AND T.PRO_PLAN_ID = ?  AND T.GX_PLAN_NO = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ps.setObject(2, gxPlanNo);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 镦拔扩，热处理，加工线生成对应虚拟炉计划(外购)
	 * 
	 * @param con
	 * @param proPlanId
	 *            下工序订单
	 * @param gxPlanNo
	 *            下工序排产序号
	 * @param
	 *
	 * @throws Exception
	 */
	public static String addPlnFurnWg(String proPlanId, String gxPlanNo,
			String createName, String judgeStoveNo, String processCdoe,
			Connection con) throws Exception {
		String mess = "";
		// 获取数据库当前时间
		String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		if (proPlanId.equals("")) {
			return mess;
		}
		// 获取库存信息
		List<MatBcMEntityPln> matBcPln = getPlanMatBcM(con, proPlanId,
				gxPlanNo, judgeStoveNo);
		if (matBcPln == null) {
			// mess = "未找到库存信息,请确认!";
			// throw new Exception(mess);
			return mess;
		}
		for (MatBcMEntityPln matBc : matBcPln) {
			// 判断下工序
			if (!matBc.getNextProcessCdoe().equals("")) {// 下工序不为空时判断经过那个工序
				if (matBc.getNextProcessCdoe().equals("E")) {// 镦拔扩工序
					PlnOrderDbkSEntity plnDbk = getPlanOrderNext(con,
							matBc.getProPlanIdNext(), matBc.getGxPlanNoNext(),
							"E", PlnOrderDbkSEntity.class);
					if (plnDbk == null) {
						// mess = "未找镦拔扩排产订单,请确认!";
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成轧管虚拟炉计划
					if (furnPlan.addPlnZyDbkMWg(con, matBc, plnDbk, createName,
							dateTime)) {
						continue;
					}
				} else if (matBc.getNextProcessCdoe().equals("F")) {// 热处理工序
					PlnOrderRclSEntity plnRcl = getPlanOrderNext(con,
							matBc.getProPlanIdNext(), matBc.getGxPlanNoNext(),
							"F", PlnOrderRclSEntity.class);
					if (plnRcl == null) {
						// mess = "未找热处理排产订单,请确认!";
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成轧管虚拟炉计划
					if (furnPlan.addPlnZyRclMWg(con, matBc, plnRcl, createName,
							dateTime)) {
						continue;
					}
				} else if (matBc.getNextProcessCdoe().equals("G")) {// 加工工序
					PlnOrderJgxSEntity plnJgx = getPlanOrderNext(con,
							matBc.getProPlanIdNext(), matBc.getGxPlanNoNext(),
							"G", PlnOrderJgxSEntity.class);
					if (plnJgx == null) {
						// mess = "未找加工线排产订单,请确认!";
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成轧管虚拟炉计划
					if (furnPlan.addPlnZyJgxMWg(con, matBc, plnJgx, createName,
							dateTime)) {
						continue;
					}
				}
			}
		}
		return mess;
	}

	/**
	 * 镦拔扩，热处理，加工线删除对应虚拟炉计划(外购)
	 * 
	 * @param con
	 * @param proPlanId
	 *            下工序订单
	 * @param
	 *
	 * @param
	 *
	 * @throws Exception
	 */
	public static String deletePlnFurnWg(String proPlanId, String judgeStoveNo,
			Connection con) throws Exception {
		String mess = "";
		String sql = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		// 获取库存信息
		List<MatBcMEntityPln> matBcPln = getPlanMatBcH(con, proPlanId,
				judgeStoveNo);
		if (matBcPln == null) {
			mess = "未找到库存信息,请确认!";
			throw new Exception(mess);
		}
		for (MatBcMEntityPln matBc : matBcPln) {
			// 判断下工序
			if (!matBc.getNextProcessCdoe().equals("")) {
				// 删除虚拟炉计划
				if (matBc.getNextProcessCdoe().equals("E")) {// 镦拔扩工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_DBK_M  T WHERE T.HEAT_PLAN_NO=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, matBc.getHeatPlanNoNext());
					rs = ps.executeQuery();
					if (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "轧管炉号:" + judgeStoveNo + "的镦拔扩虚拟炉计划已下发,不能撤销";
							throw new Exception(mess);
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_DBK_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, matBc.getHeatPlanNoNext());
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "轧管炉号:" + judgeStoveNo
									+ "的镦拔扩虚拟炉计划存在实际炉计划,不能撤销";
							throw new Exception(mess);
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_DBK_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, matBc.getHeatPlanNoNext());
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();
				} else if (matBc.getNextProcessCdoe().equals("F")) {// 热处理工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_RCL_M  T WHERE T.HEAT_PLAN_NO=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, matBc.getHeatPlanNoNext());
					rs = ps.executeQuery();
					if (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "轧管炉号:" + judgeStoveNo + "的热处理虚拟炉计划已下发,不能撤销";
							throw new Exception(mess);
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_RCL_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, matBc.getHeatPlanNoNext());
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "轧管炉号:" + judgeStoveNo
									+ "的热处理虚拟炉计划存在实际炉计划,不能撤销";
							throw new Exception(mess);
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_RCL_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, matBc.getHeatPlanNoNext());
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();
				} else if (matBc.getNextProcessCdoe().equals("G")) {// 加工工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JGX_M  T WHERE T.HEAT_PLAN_NO=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, matBc.getHeatPlanNoNext());
					rs = ps.executeQuery();
					if (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "轧管炉号:" + judgeStoveNo + "的加工线虚拟炉计划已下发,不能撤销";
							throw new Exception(mess);
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, matBc.getHeatPlanNoNext());
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "轧管炉号:" + judgeStoveNo
									+ "的加工线虚拟炉计划存在实际炉计划,不能撤销";
							throw new Exception(mess);
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, matBc.getHeatPlanNoNext());
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();
				}
			}
		}
		return mess;
	}

	/**
	 * 加工线计划下发生成接箍炉计划
	 */
	public static String addPlnFurnJg(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con) throws SQLException {
		String mess = "";
		// 获取当前下发炉号
		List<PlnZyJgxMEntity> plnZyPln = getPlanZyJgxM(con, proPlanId,
				gxPlanNo, heatPlanNo, "add", "");
		if (plnZyPln == null) {
			// mess = "未查到热处理炉计划信息,请确认!";
			return mess;
		}
		for (PlnZyJgxMEntity stordRcl : plnZyPln) {
//			if (!stordRcl.getHeatPlanNoOld().equals("")) {
//				continue;
//			}
			if (stordRcl.getNextProcessCode().equals("G")) {
				if (stordRcl.getOrderSource().equals("备料需求")) {
					// 查询备料需求与成品对应关系
					String sqlBL = "SELECT T.ORD_LN_DLY_PK FROM PLN_PORDER_BL_REQ T WHERE T.REQ_BL_ID=?";
					PreparedStatement ps = con.prepareStatement(sqlBL);
					ps.setObject(1, stordRcl.getOrdLnDlyPk());
					ResultSet rs = ps.executeQuery();
					if (rs.next()) {
						stordRcl.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
						plnDivideId = "01";
					}
					rs.close();
					ps.close();

					List<PlnOrderJgxSEntity> plnJgx = getPlanOrder(con,
							stordRcl.getOrdLnDlyPk(), plnDivideId, "G",
							PlnOrderJgxSEntity.class);
					if (plnJgx.size() <= 0) {
						continue;
					}
					corePlanFurnProM furnPlan = new corePlanFurnProM();
					// 生成虚拟炉计划
					if (furnPlan.addPlnZyJgxMG(con, stordRcl, plnJgx,
							createName, dateTime)) {
						continue;
					}
				}

			} else {
				// 判断是否接箍备料
				List<PlnGxJgPropSEntity> plnJg = getPlanOrder(con,
						stordRcl.getOrdLnDlyPk(), plnDivideId, "H",
						PlnGxJgPropSEntity.class);
				if (plnJg.size() <= 0) {
					continue;
				}
				corePlanFurnProM furnPlan = new corePlanFurnProM();
				// 生成虚拟炉计划
				if (furnPlan.addPlnZyJgJRME(con, stordRcl, plnJg, createName,
						dateTime)) {
					continue;
				}
			}
		}
		return mess;
	}

	/**
	 * 加工线计划取消下发删除接箍虚拟炉计划
	 */
	public static String deletePlnFurnJg(String proPlanId, String gxPlanNo,
			String createName, String dateTime, String heatPlanNo,
			String plnDivideId, Connection con) throws SQLException {
		String mess = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		// 获取当前下发炉次订单信息
		List<PlnZyJgxMEntity> plnZyPln = getPlanZyJgxM(con, proPlanId,
				gxPlanNo, heatPlanNo, "delete", "");
		if (plnZyPln == null) {
			// mess = "未查到热处理炉计划信息,请确认!";
			return mess;
		}
		for (PlnZyJgxMEntity stord : plnZyPln) {
//			if (!stord.getHeatPlanNoOld().equals("")) {
//				continue;
//			}
			if (!stord.getNextProcessCode().equals("")) {
				// 删除虚拟炉计划
				if (stord.getNextProcessCode().equals("G")) {// 加工工序
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PLN_INSINGLE_NUM,T.PLN_INSINGLE_WT,"
							+ "T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JGX_M  T WHERE T.UPPER_HEATPLAN_ID=?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的加工线虚拟炉计划已下发,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的加工线虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();
				} else if (stord.getNextProcessCode().equals("H")) {
					sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.PRO_PLAN_ID,T.GX_PLAN_NO,T.PLINE_CODE,T.GX_PRO_SEQ FROM PLN_ZY_JG_M  T WHERE T.UPPER_HEATPLAN_ID=? AND T.ORD_LN_DLY_PK = ?";
					ps = con.prepareStatement(sql);
					ps.setObject(1, stord.getHeatPlanNo());
					ps.setObject(2, stord.getOrdLnDlyPk());
					rs = ps.executeQuery();
					while (rs.next()) {
						int countNum = 0;
						if (!rs.getString("PLANSTATUS").equals("00")
								&& !rs.getString("PLANSTATUS").equals("10")) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划不是计划和下发状态,不能撤销";
							return mess;
						}
						// 查询是否存在实际炉计划
						String sqlZgC = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C T WHERE T.HEAT_PLAN_NO=?";
						PreparedStatement psC = con.prepareStatement(sqlZgC);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						ResultSet rsC = psC.executeQuery();
						if (rsC.next()) {
							countNum = rsC.getInt("COUNT");
						}
						rsC.close();
						psC.close();
						if (countNum > 0) {
							mess = "判定炉号:" + stord.getFeedHeatNo()
									+ "的接箍虚拟炉计划存在实际炉计划,不能撤销";
							return mess;
						}
						// 删除虚拟炉计划
						String sqlDeleteZg = "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
						psC = con.prepareStatement(sqlDeleteZg);
						psC.setObject(1, rs.getString("HEAT_PLAN_NO"));
						psC.executeUpdate();
						psC.close();
					}
					rs.close();
					ps.close();

				}
			}
		}
		return mess;
	}

	/**
	 * 加工线判定生成下工序实际炉计划
	 * 
	 * @throws Exception
	 */
	public static void addPlnZyCG(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		// 获取数据库当前时间
		String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
		// 通过判定单号，判定炉号查询判定信息
		List<QcmZgJugdeApplyEntity> qcmApp = getQcmZgApp(con, jugdeApplyCode,
				judgeStoveNo, "G", batchNo);
		if (qcmApp == null) {
			message = "判定信息已改变,请刷新判定数据!";
			throw new Exception(message);
		}
		String heatPlanNo = "";
		for (QcmZgJugdeApplyEntity qcmGp : qcmApp) {
			List<MatBcMEntityPln> matApp = getMatBcMAdd(con, jugdeApplyCode,
					judgeStoveNo, "G", qcmGp.getBatchNo(), "add");
			if (matApp == null) {
				// message = "未找到库存信息,确认是否有库存!";
				// throw new Exception(message);
				continue;
			}
			for (MatBcMEntityPln matGp : matApp) {
				String proPlanIdNext = matGp.getProPlanIdNext();
				String gxPlanNoNext = matGp.getGxPlanNoNext();
				int planInNum = Integer.parseInt(matGp.getActCount());
				String heatPlanNoNext = matGp.getHeatPlanNoNext();
				// double planInWt = Double
				// .parseDouble(matGp.getActTheoryWeight());// 实重
				// 判定是否存在虚拟炉计划
				// List<PlnZyRclMEntity> plnZg = getPlanZyRclM(con, proPlanId,
				// gxPlanNo, "", "", judgeStoveNo);
				// if (plnZg == null) {
				// // message += "未找到热处理虚拟炉计划,";
				// continue;
				// }
				String processCode = "";
				int planJgxCount = 0;

				corePlanFurnProM furnPlan = new corePlanFurnProM();
				if (proPlanIdNext.contains("JGX")) {
					processCode = "G";
					// 加工线
					PlnZyJgxMEntity plnJgx = getPlanM(con, proPlanIdNext,
							gxPlanNoNext, judgeStoveNo, "G", heatPlanNoNext,
							PlnZyJgxMEntity.class);
					if (plnJgx == null) {
						continue;
					}
					if (plnJgx.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJgx.getHeatPlanNo();
					if (Integer.parseInt(plnJgx.getPlanstatus()) < 30) {
						planJgxCount++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "G", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JGX_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成轧管实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, plnJgx, null,
								createName, dateTime, matGp, "G")) {
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "G");
						ps.executeUpdate();
						ps.close();
					}
				} else if (proPlanIdNext.contains("JG_")) {
					processCode = "H";
					// 加工线
					PlnZyJgMEntity plnJg = getPlanM(con, proPlanIdNext,
							gxPlanNoNext, judgeStoveNo, "H", heatPlanNoNext,
							PlnZyJgMEntity.class);
					if (plnJg == null) {
						continue;
					}
					if (plnJg.getIfMatchOdd().equals("1")) {
						continue;
					}
					heatPlanNo = plnJg.getHeatPlanNo();
					if (Integer.parseInt(plnJg.getPlanstatus()) < 30) {
						planJgxCount++;
						// 判定是否生成下工序实际炉计划
						int count = queryFurnC(con, jugdeApplyCode,
								judgeStoveNo, matGp.getBatchNo(),
								matGp.getBatchGroudNo(), proPlanIdNext,
								gxPlanNoNext, "H", heatPlanNoNext);
						if (count != 0) {
							// 删除实际炉计划
							String sqlupdte = "DELETE PLN_ZY_JG_C T WHERE T.JUDGE_STOVE_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/')";
							if (heatPlanNoNext.equals("")) {
								sqlupdte += " AND T.PRO_PLAN_ID='"
										+ proPlanIdNext
										+ "' AND T.GX_PLAN_NO='" + gxPlanNoNext
										+ "' ";
							} else {
								sqlupdte += " AND T.HEAT_PLAN_NO='"
										+ heatPlanNoNext + "'";
							}
							ps = con.prepareStatement(sqlupdte);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, matGp.getBatchNo());
							ps.setObject(3, matGp.getBatchGroudNo());
							ps.setObject(4, jugdeApplyCode);
							ps.executeUpdate();
							ps.close();
						}
						// 生成加工线实际炉计划
						if (furnPlan.addPlnZyC(con, null, null, null, plnJg,
								createName, dateTime, matGp, "H")) {
						}
					} else {
						// 修改库存成余材
						String updateMat = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.updateMatBcDbkYc");
						ps = con.prepareStatement(updateMat);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, matGp.getBatchNo());
						ps.setObject(3, matGp.getBatchGroudNo());
						ps.setObject(4, jugdeApplyCode);
						ps.setObject(5, "G");
						ps.executeUpdate();
						ps.close();
					}
				}
				if (planJgxCount > 0) {
					// 修改管坯MAT_BC_M挂料状态
					String updatesql = CoreXmlSqlParsersFactory
							.getSql("PlanFurnProduction.updateMatBcMR");
					ps = con.prepareStatement(updatesql);
					ps.setObject(1, jugdeApplyCode);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, matGp.getBatchNo());
					ps.setObject(4, matGp.getBatchGroudNo());
					ps.setObject(5, heatPlanNoNext);
					ps.setObject(6, processCode);
					ps.setObject(7, planInNum);
					ps.setObject(8, "1");
					ps.setObject(9, "80150103");
					ps.executeUpdate();
					ps.close();
				}
//				// 判定关闭加工线炉计划
//				int countColse = 0;
//				int countdBK = 0;
//				// 查询炉计划出库支
//				String sqlMil = "SELECT NVL(SUM(T.REAL_OUTDOUBLE_NUM), 0) REAL_OUTDOUBLE_NUM FROM PLN_ZY_JGX_C T,PLN_ZY_JGX_M T1 WHERE T.HEAT_PLAN_NO = T1.HEAT_PLAN_NO AND T.JUDGE_STOVE_NO = ? AND T1.PLANSTATUS='30'";
//				ps = con.prepareStatement(sqlMil);
//				ps.setObject(1, judgeStoveNo);
//				ResultSet rs = ps.executeQuery();
//				if (rs.next()) {
//					countdBK = rs.getInt("REAL_OUTDOUBLE_NUM");
//				}
//				rs.close();
//				ps.close();
//
//				// 查询判定表已判定支
//				sqlMil = "SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1  WHERE T1.PROCESS_CODE = 'G'  AND T1.VALIDFLAG = '20'  AND T1.IS_FLAG = '0' AND T1.JUDGE_STOVE_NO = ?";
//				ps = con.prepareStatement(sqlMil);
//				ps.setObject(1, judgeStoveNo);
//				rs = ps.executeQuery();
//				if (rs.next()) {
//					countColse = rs.getInt("ACT_COUNT");
//				}
//				rs.close();
//				ps.close();
//				if (qcmGp.getJudgeResult().equals("40740701")) {
//					if (countdBK - countColse <= 0) {
//						// 修改本工序炉计划状态--关闭
//						String updateZgM = "UPDATE PLN_ZY_JGX_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='30'";
//						ps = con.prepareStatement(updateZgM);
//						ps.setObject(1, judgeStoveNo);
//						ps.executeUpdate();
//						ps.close();
//					}
//				}
				// 修改炉计划主表量
				// String sqlUpdate = CoreXmlSqlParsersFactory
				// .getSql("FrmPlanMaterialJgx.updatePlnZgM");
				// ps = con.prepareStatement(sqlUpdate);
				// ps.setObject(1, heatPlanNo);
				// ps.setObject(2, heatPlanNo);
				// ps.executeUpdate();
				// ps.close();
			}
		}

	}

	/**
	 * 加工线判定回退，判定不合删除实际炉计划
	 * 
	 * @param con
	 * @throws Exception
	 * @throws SQLException
	 */
	public static void deletePlnCG(String jugdeApplyCode, String judgeStoveNo,
			String createName, Connection con, String batchNo) throws Exception {
		String message = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		// 通过判定单号，判定炉号查询库存信息
		List<MatBcMEntityPln> qcmApp = getMatBcMAdd(con, jugdeApplyCode,
				judgeStoveNo, "G", batchNo, "delete");
		if (qcmApp == null) {
			message = "未找到库存信息,确认是否有库存!";
			throw new Exception(message);
		}
		for (MatBcMEntityPln qcmGp : qcmApp) {
			// 如果是末工序跳出执行下一循环
			String proPlanIdNext = qcmGp.getProPlanIdNext();
			// String gxPlanNoNext = qcmGp.getGxPlanNoNext();
			String heatPlanNoNext = qcmGp.getHeatPlanNoNext();
			int planInNum = Integer.parseInt(qcmGp.getActCount());
			// 判定是否存在轧管虚拟卢计划
			// List<PlnZyRclMEntity> plnZg = getPlanZyRclM(con, proPlanId,
			// gxPlanNo, "", "", judgeStoveNo);
			// if (plnZg == null) {
			// // message += "未找到热处理虚拟炉计划,";
			// continue;
			// }
			String processCode = "";
			int count = 0;
			if (proPlanIdNext.contains("JGX")) {
				processCode = "G";
				// 加工线
				PlnZyJgxCEntity plnJgx = getPlanC(con, jugdeApplyCode,
						judgeStoveNo, qcmGp.getBatchNo(),
						qcmGp.getBatchGroudNo(), "G", heatPlanNoNext,
						PlnZyJgxCEntity.class);
				if (plnJgx == null) {
					continue;
				}
				count++;
				if (!plnJgx.getPlanstatus().equals("00")
						&& !plnJgx.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),加工线不是计划和下发状态,不能取消确认,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJgx.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),加工线是利库计划,不能取消确认,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JGX_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();

				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JGX_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJgx.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJgx.getIfMatchOdd().equals("1")) {
						// 如果是利库计划则同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_JGX_M  WHERE HEAT_PLAN_NO = ? ";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_JGX_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnJgx.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}
			} else if (proPlanIdNext.contains("JG_")) {
				processCode = "H";
				// 加工线
				PlnZyJgCEntity plnJg = getPlanC(con, jugdeApplyCode,
						judgeStoveNo, qcmGp.getBatchNo(),
						qcmGp.getBatchGroudNo(), "H", heatPlanNoNext,
						PlnZyJgCEntity.class);
				if (plnJg == null) {
					continue;
				}
				count++;
				if (!plnJg.getPlanstatus().equals("00")
						&& !plnJg.getPlanstatus().equals("10")) {
					message = "该判定炉号(" + judgeStoveNo
							+ "),接箍不是计划和下发状态,不能取消下发,请联系相关人员撤销炉计划!";
					throw new Exception(message);
				}
				// if (plnJg.getIfMatchOdd().equals("1")) {
				// message = "该判定炉号(" + judgeStoveNo
				// + "),接箍是利库计划,不能取消下发,请联系相关人员删除炉计划!";
				// throw new Exception(message);
				// }
				// 删除实际炉计划
				String deletesqlZg = "DELETE PLN_ZY_JG_C  WHERE HEAT_PLAN_NO = ? AND NVL(JUGDE_APPLY_CODE,'/') = NVL(?,'/')";
				ps = con.prepareStatement(deletesqlZg);
				ps.setObject(1, plnJg.getHeatPlanNo());
				ps.setObject(2, jugdeApplyCode);
				ps.executeUpdate();
				ps.close();

				int countC = 0;
				// 查询是否存在实际炉计划
				String sql = "SELECT COUNT(*) COUNT FROM PLN_ZY_JG_C WHERE HEAT_PLAN_NO = ?";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnJg.getHeatPlanNo());
				rs = ps.executeQuery();
				if (rs.next()) {
					countC = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (countC == 0) {
					if (plnJg.getIfMatchOdd().equals("1")) {
						// 如果是利库计划则同时删除虚拟炉计划
						deletesqlZg = "DELETE PLN_ZY_JG_M  WHERE HEAT_PLAN_NO = ? ";
						ps = con.prepareStatement(deletesqlZg);
						ps.setObject(1, plnJg.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改虚拟炉计划状态
						String update = "UPDATE PLN_ZY_JG_M T SET T.IF_DO_HEAT_PLAN='0' WHERE T.HEAT_PLAN_NO=?";
						ps = con.prepareStatement(update);
						ps.setObject(1, plnJg.getHeatPlanNo());
						ps.executeUpdate();
						ps.close();
					}
				}

				if (count > 0) {
					// 修改管坯MAT_BC_M挂料状态
					String updatesql = CoreXmlSqlParsersFactory
							.getSql("PlanFurnProduction.updateMatBcMR");
					ps = con.prepareStatement(updatesql);
					ps.setObject(1, jugdeApplyCode);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, qcmGp.getBatchNo());
					ps.setObject(4, qcmGp.getBatchGroudNo());
					ps.setObject(5, heatPlanNoNext);
					ps.setObject(6, processCode);
					ps.setObject(7, planInNum);
					ps.setObject(8, "0");
					ps.setObject(9, "80150102");
					ps.executeUpdate();
					ps.close();
				}
				// 修改炉计划生产完成
//				String updateZgM = "UPDATE PLN_ZY_JGX_M T SET T.PLANSTATUS='30',T.IS_CLOSE='' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='40'";
//				ps = con.prepareStatement(updateZgM);
//				ps.setObject(1, judgeStoveNo);
//				ps.executeUpdate();
//				ps.close();
			}
		}
	}

	/**
	 * 判定调用炉计划关闭
	 * 
	 * @param judgeStoveNo
	 * @param con
	 * @throws Exception
	 */
	public static void productClose(String judgeStoveNo, Connection con,
			String prossCode, String flag) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		// String message = "";
		// flag=1关闭，0撤销关闭
		if (flag.equals("1")) {
			// 判定关闭加工线炉计划
			int countColse = 0;
			int countdBK = 0;
			if (prossCode.equals("D")) {
				// 查询进程表量 （预计支-废品-判定支=0）
				String sqlMil = "SELECT NVL(T.YJZ - T.UNDETERMINEDNUM - T.SCRAPNUM - T.WWFPZ,0) -"
						+ "(SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1 WHERE T1.PROCESS_CODE = 'D' "
						+ " AND (T1.JUDGE_RESULT_CODE IN ('40740701','40740709','40740707') OR T1.INSTANCY_FLAG = '1') AND T1.IS_FLAG='0' AND T1.JUDGE_STOVE_NO = ?) ACOUNT FROM MIL_PROCESSES T WHERE T.JUDGE_STOVE_NO = ?";
				ps = con.prepareStatement(sqlMil);
				ps.setObject(1, judgeStoveNo);
				ps.setObject(2, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					countColse = rs.getInt("ACOUNT");
				}
				rs.close();
				ps.close();
				if (countColse == 0) {
					// 修改本工序炉计划状态--关闭
					String updateZgM = "UPDATE PLN_ZY_ZG_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0' WHERE T.FEED_HEAT_NO=? AND T.IF_DO_HEAT_PLAN='1'";
					ps = con.prepareStatement(updateZgM);
					ps.setObject(1, judgeStoveNo);
					ps.executeUpdate();
					ps.close();
				}
			} else if (prossCode.equals("E")) {
				// 查询炉计划出库支
				String sqlMil = "SELECT NVL(SUM(T.REAL_OUTDOUBLE_NUM), 0) REAL_OUTDOUBLE_NUM FROM PLN_ZY_DBK_C T,PLN_ZY_DBK_M T1 WHERE T.HEAT_PLAN_NO = T1.HEAT_PLAN_NO AND T.JUDGE_STOVE_NO = ? AND T1.PLANSTATUS='30'";
				ps = con.prepareStatement(sqlMil);
				ps.setObject(1, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					countdBK = rs.getInt("REAL_OUTDOUBLE_NUM");
				}
				rs.close();
				ps.close();

				// 查询判定表已判定支
				sqlMil = "SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1  WHERE T1.PROCESS_CODE = 'E'  AND (T1.JUDGE_RESULT_CODE IN ('40740701','40740709','40740707') OR T1.INSTANCY_FLAG = '1')  AND T1.IS_FLAG = '0' AND T1.JUDGE_STOVE_NO = ?";
				ps = con.prepareStatement(sqlMil);
				ps.setObject(1, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					countColse = rs.getInt("ACT_COUNT");
				}
				rs.close();
				ps.close();
				if (countdBK - countColse <= 0) {
					// 修改本工序炉计划状态--关闭
					String updateZgM = "UPDATE PLN_ZY_DBK_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='30'";
					ps = con.prepareStatement(updateZgM);
					ps.setObject(1, judgeStoveNo);
					ps.executeUpdate();
					ps.close();
				}
			} else if (prossCode.equals("F")) {
				// 查询炉计划出库支
				String sqlMil = "SELECT NVL(SUM(T.REAL_OUTDOUBLE_NUM), 0) REAL_OUTDOUBLE_NUM FROM PLN_ZY_RCL_C T,PLN_ZY_RCL_M T1 WHERE T.HEAT_PLAN_NO = T1.HEAT_PLAN_NO AND T.JUDGE_STOVE_NO = ? AND T1.PLANSTATUS='30'";
				ps = con.prepareStatement(sqlMil);
				ps.setObject(1, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					countdBK = rs.getInt("REAL_OUTDOUBLE_NUM");
				}
				rs.close();
				ps.close();

				// 查询判定表已判定支
				sqlMil = "SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1  WHERE T1.PROCESS_CODE = 'F'  AND (T1.JUDGE_RESULT_CODE IN ('40740701','40740709','40740707') OR T1.INSTANCY_FLAG = '1')  AND T1.IS_FLAG = '0' AND T1.JUDGE_STOVE_NO = ?";
				ps = con.prepareStatement(sqlMil);
				ps.setObject(1, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					countColse = rs.getInt("ACT_COUNT");
				}
				rs.close();
				ps.close();
				if (countdBK - countColse <= 0) {
					// 修改本工序炉计划状态--关闭
					String updateZgM = "UPDATE PLN_ZY_RCL_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='30'";
					ps = con.prepareStatement(updateZgM);
					ps.setObject(1, judgeStoveNo);
					ps.executeUpdate();
					ps.close();
				}
			} else if (prossCode.equals("G")) {
				// 查询炉计划出库支
				String sqlMil = "SELECT NVL(SUM(T.REAL_OUTDOUBLE_NUM), 0) REAL_OUTDOUBLE_NUM FROM PLN_ZY_JGX_C T,PLN_ZY_JGX_M T1 WHERE T.HEAT_PLAN_NO = T1.HEAT_PLAN_NO AND T.JUDGE_STOVE_NO = ? AND T1.PLANSTATUS='30'";
				ps = con.prepareStatement(sqlMil);
				ps.setObject(1, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					countdBK = rs.getInt("REAL_OUTDOUBLE_NUM");
				}
				rs.close();
				ps.close();

				// 查询判定表已判定支
				sqlMil = "SELECT NVL(SUM(T1.ACT_COUNT), 0) ACT_COUNT FROM QCM_ZG_JUGDE_APPLY T1  WHERE T1.PROCESS_CODE = 'G'  AND (T1.JUDGE_RESULT_CODE IN ('40740701','40740709','40740707') OR T1.INSTANCY_FLAG = '1')  AND T1.IS_FLAG = '0' AND T1.JUDGE_STOVE_NO = ?";
				ps = con.prepareStatement(sqlMil);
				ps.setObject(1, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					countColse = rs.getInt("ACT_COUNT");
				}
				rs.close();
				ps.close();
				if (countdBK - countColse <= 0) {
					// 修改本工序炉计划状态--关闭
					String updateZgM = "UPDATE PLN_ZY_JGX_M T SET T.PLANSTATUS='40',T.IS_CLOSE='0' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='30'";
					ps = con.prepareStatement(updateZgM);
					ps.setObject(1, judgeStoveNo);
					ps.executeUpdate();
					ps.close();
				}
			}
		} else {
			if (prossCode.equals("D")) {
				// 修改炉计划生产完成
				String updateZgM = "UPDATE PLN_ZY_ZG_M T SET T.PLANSTATUS='30',T.IS_CLOSE='' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='40'";
				ps = con.prepareStatement(updateZgM);
				ps.setObject(1, judgeStoveNo);
				ps.executeUpdate();
				ps.close();
			} else if (prossCode.equals("E")) {
				// 修改炉计划生产完成
				String updateZgM = "UPDATE PLN_ZY_DBK_M T SET T.PLANSTATUS='30',T.IS_CLOSE='' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='40'";
				ps = con.prepareStatement(updateZgM);
				ps.setObject(1, judgeStoveNo);
				ps.executeUpdate();
				ps.close();
			} else if (prossCode.equals("F")) {
				// 修改炉计划生产完成
				String updateZgM = "UPDATE PLN_ZY_RCL_M T SET T.PLANSTATUS='30',T.IS_CLOSE='' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='40'";
				ps = con.prepareStatement(updateZgM);
				ps.setObject(1, judgeStoveNo);
				ps.executeUpdate();
				ps.close();
			} else if (prossCode.equals("G")) {
				// 修改炉计划生产完成
				String updateZgM = "UPDATE PLN_ZY_JGX_M T SET T.PLANSTATUS='30',T.IS_CLOSE='' WHERE T.JUDGE_STOVE_NO=? AND T.PLANSTATUS='40'";
				ps = con.prepareStatement(updateZgM);
				ps.setObject(1, judgeStoveNo);
				ps.executeUpdate();
				ps.close();
			}
		}
	}
}
