package com.steering.pss.plnsaleord.planStove;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.planStove.entity.MatGpTrackEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.ControlOrderPlan;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;
import com.steering.pss.plnsaleord.steelMarkingPlan.StoveNoGenerate;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnOrderCastSEntity;
import com.steering.pss.plnsaleord.stove.ComPlanFurnace;
import com.steering.pss.plnsaleord.stove.corePlnZyFurnM;
import com.steering.pss.plnsaleord.stove.entity.*;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

/**
 * 管坯材料信息
 * 
 * @author teng 2017.11.21
 */
public class FrmPlanSteelMakMod extends DBCommon {

	/**
	 * 查询炼钢所属管坯
	 * 
	 * @return
	 */
	public CoreReturnObject queryMaterial(String belongCode, String gradeCode,
			String judgeNo, String[] belongCodeAll, Boolean flag) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.queryMaterial");
		// if (belongCode.equals("")) {
		// sql += CoreConverter.toSqlConditionString("T.BELONG_CODE",
		// belongCodeAll);
		// }
		// else {
		// sql += " AND T.BELONG_CODE='" + belongCode + "'";
		// }
		if (!gradeCode.equals("")) {
			sql += " AND T.GRADECODE='" + gradeCode + "'";
		}
		if (!judgeNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO ='" + judgeNo + "'";
		}
		sql += "   AND T.QCM_STAUS='00'";
		if (flag) {
			sql += " AND T.MAT_STATUS ='80150102' AND INSTR(T.DIMATER,'t') > 0 AND T.PRO_PLAN_ID_ZG IS NULL";
		} else {
			// if (flag1) {
			// sql +=
			// " AND T.MAT_STATUS='80150102' AND T.JUDGE_RESULT_CODE='40740704'";
			// }
			// else {
			sql += " AND ((T.MAT_STATUS = '80150102' AND T.JUDGE_RESULT_CODE = '40740704') or "
					+ " t.MAT_STATUS = '80150101' AND T.JUGDE_APPLY_CODE IS NULL)";
			// }

		}
		sql += " GROUP BY T.PRODUCT_FLAG, T.MAT_STATUS, T.STOVE_NO, T.FLAG_STOVE_NO, T.JUDGE_STOVE_NO,T.CAST_NO, "
				+ "T.MATERIAL_NO, T.MATERIAL_NAME, T.INPUT_MATERIAL_NAME,T.INPUT_MATERIAL_NO, T.GRADECODE, T.GRADENAME, "
				+ "T.ORDER_NO, T.ORDER_SEQ, T.PLINE_CODE, T.PLINE_NAME, T.DIMATER, T.HEIGHT, T.ACT_LEN, T.FIX_NUM,"
				+ "T.FIX_LEN, T.BELONG_NAME, T.BELONG_CODE, T.CIC,T.PIC, T.SIC, T.DIC, "
				+ "T.GPTYPE, T.SEQUENCENO, T.FURNACENO, T.QCM_STAUS,T.LG_CRAFT_NO, T.PRODUCCODE, T.PRODUCNAME, T.PLAN_ROUTE, "
				+ "T.PRO_PLAN_ID_ZG,T.GX_PLAN_NO_ZG,t.JUDGE_RESULT_CODE, T.JUDGE_STOVE_NO_OLD,T.JUDGE_FLAG ORDER BY T.JUDGE_STOVE_NO";
		return this.getData(sql);
	}

	/**
	 * 查询轧管订单
	 * 
	 * @return
	 */
	public CoreReturnObject selectPlnOrderZgS(String gradCode,
			String diammterGp, String fixLen, String gptype) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.selectPlnOrderZgS");
		sql += " AND T.OUTS_BILLET_FL = '0'  AND T.DIAMETER_GP =TO_NUMBER('"
				+ diammterGp + "') ";// AND T.LEN_GP_SINGLE =TO_NUMBER('" +
		// fixLen+ "')

		if (gptype.equals("锻坯")) {
			sql += " AND INSTR(T.WHOLE_BACKLOG_DESC,'锻造') > 0 ";
		}
		sql += " AND T.EXE_STATUS = '0' AND (T.IS_REPAIR_PLN IS NULL OR T.IS_REPAIR_PLN = '0')"
				+ " AND T.IS_PLAN_OK ='1' ";
		sql += " AND T.GPREQ_ID IS NOT NULL";
		sql += " AND T.PLINE_CODE IN ('C008','C009','C010','C012','C017')) ZG WHERE INSTR(ZG.GRADE_ALL, '"
				+ gradCode + "') > 0";
		return this.getData(sql);

	}

	/**
	 * 根据管坯需求号查询轧管订单
	 * 
	 * @return
	 */
	public CoreReturnObject selectPlnOrderZgSGp(String gpreqId) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.selectPlnOrderZgS");
		sql += " AND T.GPREQ_ID='" + gpreqId + "'";
		sql += " AND T.PLINE_CODE IN ('C008','C009','C010','C012','C017')) ZG";
		return this.getData(sql);

	}

	/**
	 * 查询铸造生产订单信息
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getOrderBInfoByContion(String diammterGp,
			String gradCode, String gptype) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.getOrderBInfo");
		if (gptype.equals("锻坯")) {
			sql += " AND T.DIAMETER_LZ||'t'='" + diammterGp + "'";
		} else {
			sql += " AND T.DIAMETER_LZ='" + diammterGp + "'";
		}
		// sql += " AND T.GRADENAME='" + gradCode + "'";
		// sql += " AND T.LEN_GP_SINGLE=TO_NUMBER('"
		// + fixLen
		// + "')";
		sql += "AND T.EXE_STATUS IN ('0')  AND (T.IS_PLAN_OK='0' OR T.IS_PLAN_OK IS NULL) ";
		sql += " AND TO_NUMBER(NVL(T.PLAN_WT_S,'0'))-TO_NUMBER(NVL(T.PLANED_WT,'0'))-TO_NUMBER(NVL(T.PLAN_WT_SOLD, '0'))>0";
		sql += " order by TO_NUMBER(T.GX_PRO_SEQ) ASC";
		sql += ") H WHERE H.EXE_STATUS_ZG = '计划'";
		sql += " AND INSTR(H.GRADECODEZG,'" + gradCode + "') > 0";
		return this.getData(sql);
	}

	/**
	 * 查询锻造生产订单信息
	 * 
	 * @return CoreReturnObject
	 */

	public CoreReturnObject getOrderBInfoByContionDz(String gradCode,
			String diammterGp, String fixLen, Boolean flag) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.getOrderBInfoByContionDz");
		sql += " AND T.DIAMETER_IN='" + diammterGp + "'";
		if (flag) {
			sql += " AND T.GRADECODE='" + gradCode + "'";
		}
		sql += " AND T.EXE_STATUS IN ('0')";
		// AND
		// TO_NUMBER(NVL(T.PLAN_WT_S,'0'))-TO_NUMBER(NVL(T.PLANED_WT,'0'))-TO_NUMBER(NVL(T.PLAN_WT_SOLD,
		// '0'))>0 AND TO_NUMBER(NVL(T.NUM_S,'0'))>0
		sql += "  order by TO_NUMBER(T.GX_PRO_SEQ) ASC) T3";
		return this.getData(sql);
	}

	/**
	 * 根据管坯需求号查询断面,单倍坯长
	 * 
	 * @param proPlanId
	 * @return
	 */
	public CoreReturnObject getOrderDiameter(String proPlanId) {
		String sql = "SELECT T.LEN_GP_SINGLE,T.DIAMETER_GP FROM PLN_ORDER_ZG_S T WHERE T.GPREQ_ID='"
				+ proPlanId + "' GROUP BY T.LEN_GP_SINGLE,T.DIAMETER_GP";
		return this.getData(sql);
	}

	/**
	 * 查询排产信息
	 * 
	 * @param con
	 * @param proPlanId
	 *            订单编号
	 * @param gxPlanNo
	 *            排产序号
	 * @return PlnOrderCastSEntity
	 * @throws SQLException
	 */
	public static <T> T getPlanOrderZg(Connection con, String proPlanId,
			String gxPlanNo, Class<T> cls) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("ComPlanFurnace.queryPlnOrderZgS");
		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 judgeStoveNo
	 *            判定炉号
	 * @throws SQLException
	 */
	public static List<MatGpMEntity> getQureyMatGpML(Connection con,
			String judgeStoveNo, double actLen, double fixLen, int cutNumGp,
			int takeCount, String proPlanId, String gxPlanNoZg)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.QueryMatGp");
		if (proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID_ZG IS NULL";
		} else {
			sql += " AND T.PRO_PLAN_ID_ZG='" + proPlanId + "' ";
			sql += " AND T.GX_PLAN_NO_ZG='" + gxPlanNoZg + "' ";
		}
		// if (matStratus.equals("80150103")) {
		// sql += " AND T.HANGING_FLAG = '1'";
		// }
		// else {
		// sql += " AND T.HANGING_FLAG = '0'";
		// }
		sql += " ORDER BY T.MAT_NO ASC) WHERE ROWNUM <= ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		ps.setObject(2, actLen);
		ps.setObject(3, fixLen);
		ps.setObject(4, cutNumGp);
		ps.setObject(5, takeCount);
		ResultSet rs = ps.executeQuery();
		List<MatGpMEntity> list = EntityTool.getEntitysByRs(rs,
				MatGpMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatGpMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 查询坯子库存信息(脱单)
	 * 
	 * @param con
	 * @param judgeStoveNo
	 *            判定炉号
	 * @throws SQLException
	 */
	public static List<MatGpMEntity> getQureyMatGpMLTd(Connection con,
			String judgeStoveNo, double actLen, double fixLen, int cutNumGp,
			String proPlanId, String gxPlanNoZg) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.QueryMatGpTD");
		if (proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID_ZG IS NULL";
		} else {
			sql += " AND T.PRO_PLAN_ID_ZG='" + proPlanId + "' ";
			sql += " AND T.GX_PLAN_NO_ZG='" + gxPlanNoZg + "' ";
		}
		sql += " ORDER BY T.MAT_NO ASC";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		ps.setObject(2, actLen);
		ps.setObject(3, fixLen);
		ps.setObject(4, cutNumGp);
		ResultSet rs = ps.executeQuery();
		List<MatGpMEntity> list = EntityTool.getEntitysByRs(rs,
				MatGpMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatGpMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 炼钢坯子利库(利轧管)
	 * 
	 * @param jslist
	 * @param userName
	 *            利库人
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doAddLibraryLg(ArrayList<String> list,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// ResultSet rs = null;
			SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat dfMonth = new SimpleDateFormat("yyyyMM");
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			DecimalFormat df = new DecimalFormat("0000000000");
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);

			String proPlanId = list.get(0);
			String gxPlanNo = list.get(1);
			// double reallWt = Double.parseDouble(list.get(2));
			String judgeStoveNo = list.get(3);
			double actLenTake = Double.parseDouble(list.get(4));// 倍尺坯长
			double fixLenTake = Double.parseDouble(list.get(5));// 单倍
			int cutNumGp = Integer.parseInt(list.get(6));
			int takeCount = Integer.parseInt(list.get(7));// 利库支
			String dimaterKc = list.get(8);
			String gptype = list.get(9);
			double singeLen = Double.parseDouble(list.get(10));// 选择记录单倍坯长

			int cutNum = (int) (actLenTake / singeLen);

			// 切损长度
			// double cutLength = actLenTake - cutNum * singeLen;

			// double takeWeight = Double.parseDouble(list.get(4));// 利库吨
			// 查询轧管信息
			PlnOrderZgSEntityL plnZgS = getPlanOrderZg(conn, proPlanId,
					gxPlanNo, PlnOrderZgSEntityL.class);
			if (plnZgS == null) {
				cro.setV_errMsg("未找到轧管订单(" + proPlanId + "/" + gxPlanNo
						+ ")信息!");
				return cro;
			}
			int num = takeCount * cutNum;
			int dimater = Integer.parseInt(plnZgS.getDiameterGp());
			if (dimater == 251) {
				dimater = 250;
			}
			double mSingleWeight = (7.8 * Math.PI / 4 * dimater * dimater) / 1000 / 1000;// 米单重
			// 单倍坯重
			double weightGpOne = mSingleWeight
					* (Double.parseDouble(plnZgS.getLenGpSingle()) / 1000);
			// 倍尺坯单支重=坯米单重*倍尺坯长/1000
			// double weightM = mSingleWeight * (actLenTake / 1000);
			List<MatGpMEntity> matGpMlist = getQureyMatGpML(conn, judgeStoveNo,
					actLenTake, fixLenTake, cutNumGp, takeCount, "", "");
			if (matGpMlist == null) {
				cro.setV_errMsg("管坯物料已发生改变,请刷新!");
				return cro;
			}
			int seq = 0;
			double actWt = 0;
			String stoveNo = "";
			String flagStoveNo = "";
			String furnaceno = "";
			String proPlanIdZz = "";
			String gxPlanNoZz = "";
			String inwlId = "";
			String inwlDesc = "";
			String gradename = "";
			String gradecode = "";
			double lengthWt = 0;
			String matStrat = "";
			String jugdeApplyCode = "";
			String cic = "";
			for (MatGpMEntity matGpM : matGpMlist) {
				actWt += Double.parseDouble(matGpM.getActWeight());
				lengthWt = Double.parseDouble(matGpM.getActWeight());
				stoveNo = matGpM.getStoveNo();
				flagStoveNo = matGpM.getFlagStoveNo();
				furnaceno = matGpM.getFurnaceno() + "/"
						+ matGpM.getSequenceno();
				proPlanIdZz = matGpM.getProPlanId();
				gxPlanNoZz = matGpM.getGxPlanNo();
				inwlId = matGpM.getMaterialNo();
				inwlDesc = matGpM.getMaterialName();
				gradename = matGpM.getGradename();
				gradecode = matGpM.getGradecode();
				matStrat = matGpM.getMatStatus();
				cic = matGpM.getCic();
				jugdeApplyCode = matGpM.getJugdeApplyCode();
				// if (!matGpM.getProductFlag().equals("801401")) {
				// cro.setV_errMsg("材料类别不是在制品,不能利库!");
				// return cro;
				// }
				// if (!matGpM.getMatStatus().equals("80150101")
				// || !matGpM.getMatStatus().equals("80150102")) {
				// cro.setV_errMsg("材料状态不是产出待判或产出已判,不能利库");
				// return cro;
				// }
				if (!matGpM.getDimater().contains("t")) {
					if (!matGpM.getJudgeResultCode().equals("40740704")
							&& matGpM.getMatStatus().equals("80150102")) {
						cro
								.setV_errMsg("材料状态是产出已判并且综合判定结果不是" + "不合格"
										+ ",不能利库");
						return cro;
					}
				} else {
					if (matGpM.getMatStatus().equals("80150103")) {
						cro.setV_errMsg("材料状态是材料已遍计划,不能利库");
						return cro;
					}
				}
				if (!matGpM.getQcmStaus().equals("00")) {
					cro.setV_errMsg("交库状态不是合格状态,不能利库!");
					return cro;
				}
				if (matGpM.getProPlanIdZg() != "") {
					cro.setV_errMsg("你选择的管坯物料已挂合同,不能利库。如需利库,请先脱单!");
					return cro;
				}

				// 修改坯子库存信息
				String sqlUpdateMat = CoreXmlSqlParsersFactory
						.getSql("FrmPlanSteelMakMod.UpdateMatGpOld");
				sqlUpdateMat += " ,T.JUGDE_APPLY_FLAG='0',T.JUGDE_APPLY_CODE='',T.JUGDE_APPLY_TIME='',T.JUDGE_RESULT_CODE='',T.JUDGE_RESULT_TIME='',T.JUDGE_MAKER=''";
				sqlUpdateMat += "WHERE T.MAT_NO = ?";
				ps = conn.prepareStatement(sqlUpdateMat);
				ps.setObject(1, proPlanId);
				ps.setObject(2, gxPlanNo);
				ps.setObject(3, plnZgS.getOrderNo());
				ps.setObject(4, plnZgS.getOrderSeq());
				ps.setObject(5, plnZgS.getDeliveryNo());
				if (plnZgS.getOrderSource().equals("备料需求")) {
					ps.setObject(6, "");
					ps.setObject(7, "");
				} else {
					ps.setObject(6, plnZgS.getOrdLnDlyPk().substring(0,
							plnZgS.getOrdLnDlyPk().length() - 6));
					ps.setObject(7, plnZgS.getOrdLnDlyPk().substring(0,
							plnZgS.getOrdLnDlyPk().length() - 3));
				}
				ps.setObject(8, plnZgS.getOrdLnDlyPk());
				ps.setObject(9, "2");
				ps.setObject(10, matGpM.getProPlanId());
				ps.setObject(11, matGpM.getGxPlanNo());
				// if (!matGpM.getMatStatus().equals("80150101")) {
				ps.setObject(12, "80150101");
				// } else {
				// ps.setObject(12, matGpM.getMatStatus());
				// }
				ps.setObject(13, "0");
				// if (gradename.equals(plnZgS.getGradename())) {
				// ps.setObject(14, matGpM.getMaterialNo());
				// ps.setObject(15, matGpM.getMaterialName());
				// ps.setObject(16, matGpM.getInputMaterialName());
				// ps.setObject(17, matGpM.getInputMaterialNo());
				// ps.setObject(18, matGpM.getGradecode());
				// ps.setObject(19, matGpM.getGradename());
				// ps.setObject(20, matGpM.getCic());
				// ps.setObject(21, matGpM.getOldGradecode());
				// ps.setObject(22, matGpM.getOldGradename());
				// ps.setObject(23, matGpM.getOldCic());
				// ps.setObject(24, matGpM.getOldMaterialNo());
				// ps.setObject(25, matGpM.getOldMaterialName());
				// } else {
				// ps.setObject(14, plnZgS.getInwlId());
				// ps.setObject(15, plnZgS.getInwlDesc());
				// ps.setObject(16, plnZgS.getGradecode());
				// ps.setObject(17, plnZgS.getGradename());
				// ps.setObject(18, plnZgS.getGradecode());
				// ps.setObject(19, plnZgS.getGradename());
				// ps.setObject(20, plnZgS.getCicGp());
				// ps.setObject(21, matGpM.getGradecode());
				// ps.setObject(22, matGpM.getGradename());
				// ps.setObject(23, matGpM.getCic());
				// ps.setObject(24, matGpM.getMaterialNo());
				// ps.setObject(25, matGpM.getMaterialName());
				// }
				// ps.setObject(14, cutLength);
				// ps.setObject(15, cutNum);
				// ps.setObject(16, singeLen);
				ps.setObject(14, matGpM.getMatNo());
				ps.executeUpdate();
				ps.close();

				seq += 1;
				String resumeSeq = dateFormat.format(now) + df.format(seq);
				// 生成利库日志
				MatGpTrackEntity matGpTrack = new MatGpTrackEntity();
				matGpTrack.setResumeSeq(resumeSeq);
				matGpTrack.setEventId("900102");
				matGpTrack.setEventDesc("炼钢利库");
				matGpTrack
						.setFormName("com.steering.pss.plnsaleord.planStove.FrmPlanSteelMakMod");
				matGpTrack.setCallFuncCoding("doAddLibraryLg");
				matGpTrack.setKeyvalue1("List<MatGpMEntity> matGpMlist");
				matGpTrack.setKeyvalue1Desc("物料主表关键字实体类集合");
				matGpTrack.setKeyvalue2("String userName");
				matGpTrack.setKeyvalue2Desc2("操作人");
				matGpTrack.setMatNo(matGpM.getMatNo());
				matGpTrack.setProductFlag(matGpM.getProductFlag());
				matGpTrack.setMatStatus(matGpM.getMatStatus());
				matGpTrack.setStoveNo(matGpM.getStoveNo());
				matGpTrack.setFlagStoveNo(matGpM.getFlagStoveNo());
				matGpTrack.setJudgeStoveNo(matGpM.getJudgeStoveNo());
				matGpTrack.setBatchNo("");
				matGpTrack.setGroudNo(matGpM.getGroudNo());
				matGpTrack.setCastNo(matGpM.getCastNo());
				matGpTrack.setCcmid(matGpM.getCcmid());
				matGpTrack.setGradecode(matGpM.getGradecode());
				matGpTrack.setGradename(matGpM.getGradename());
				matGpTrack.setStdCode(matGpM.getStdCode());
				matGpTrack.setStdName(matGpM.getStdName());
				matGpTrack.setActDimater(matGpM.getDimater());
				matGpTrack.setActHeight(matGpM.getHeight());
				matGpTrack.setActLen(String.valueOf(actLenTake));
				matGpTrack.setActLenMax(plnZgS.getLenRollMax());
				matGpTrack.setActLenMin(plnZgS.getLenRollMin());
				matGpTrack.setActFixLen(matGpM.getFixLen());
				matGpTrack.setActFixNum(matGpM.getFixNum());
				matGpTrack.setActCount("1");
				matGpTrack.setActWeight(matGpM.getActWeight());
				matGpTrack.setActTheoryWeight(matGpM.getActTheoryWeight());
				if (plnZgS.getOrderSource().equals("备料需求")) {
					matGpTrack.setOrdPk("");
					matGpTrack.setOrdLnPk("");
				} else {
					matGpTrack.setOrdPk(plnZgS.getOrdLnDlyPk().substring(0,
							plnZgS.getOrdLnDlyPk().length() - 6));
					matGpTrack.setOrdLnPk(plnZgS.getOrdLnDlyPk().substring(0,
							plnZgS.getOrdLnDlyPk().length() - 3));
				}
				matGpTrack.setOrdLnDlyPk(plnZgS.getOrdLnDlyPk());
				matGpTrack.setOrderNo(plnZgS.getOrderNo());
				matGpTrack.setOrderSeq(plnZgS.getOrderSeq());
				matGpTrack.setOrderSeqNo(plnZgS.getDeliveryNo());
				// matGpTrack.setOldOrdPk(matGpM.getOrdPk());
				// matGpTrack.setOldOrdLnPk(matGpM.getOrdLnPk());
				// matGpTrack.setOldOrdLnDlyPk(matGpM.getOrdLnDlyPk());
				matGpTrack.setOldOrderNo(matGpM.getOldOrderNo());
				matGpTrack.setOldOrderSeq(matGpM.getOldOrderSeq());
				matGpTrack.setOldOrderSeqNo(matGpM.getOldOrderSeqNo());
				matGpTrack.setMemo("");
				matGpTrack.setCreateName(userName);
				matGpTrack.setCreateTime(dateTime);
				matGpTrack.setOldJudgeStoveNo(matGpM.getOldJudgeStoveNo());
				matGpTrack.setOldBatchNo("");
				matGpTrack.setOldGroudNo(matGpM.getOldGroudNo());
				matGpTrack.setOldGradecode(matGpM.getOldGradecode());
				matGpTrack.setOldGradename(matGpM.getOldGradename());
				matGpTrack.setOldStdCode(matGpM.getOldStdCode());
				matGpTrack.setOldStdName(matGpM.getOldStdName());
				matGpTrack.setOldStoveNo(matGpM.getOldStoveNo());
				String sqlAddTrack = EntityTool
						.fillSqlByEntity(
								CoreXmlSqlParsersFactory
										.getSql("FrmPlanMaterialSelection.addMatGpTrack"),
								matGpTrack);
				ps = conn.prepareStatement(sqlAddTrack);
				ps.executeUpdate();
				ps.close();
			}
			if (!dimaterKc.contains("t")) {// 铸造
				PlnZyZgMEntity plnZgM = new PlnZyZgMEntity();
				String heatPlanNo = "";
				int countM = 0;
				String planStatus = "00";
				// 根据判定炉号，轧管订单号查询是否存在虚拟炉计划
				String sqlQuery = "SELECT COUNT(*) COUNT,T.HEAT_PLAN_NO,T.PLANSTATUS,T.FEED_HEAT_NO,T.LEN_GP_SINGLE,T.IF_MATCH_ODD FROM PLN_ZY_ZG_M T WHERE T.JUDGE_STOVE_NO=? "
						+ " AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? GROUP BY T.HEAT_PLAN_NO,T.PLANSTATUS,T.FEED_HEAT_NO,T.LEN_GP_SINGLE,T.IF_MATCH_ODD";
				ps = conn.prepareStatement(sqlQuery);
				ps.setObject(1, judgeStoveNo);
				ps.setObject(2, proPlanId);
				ps.setObject(3, gxPlanNo);
				ResultSet rs = ps.executeQuery();
				while (rs.next()) {
					heatPlanNo = rs.getString("HEAT_PLAN_NO");
					planStatus = rs.getString("PLANSTATUS");
					if (rs.getString("IF_MATCH_ODD").equals("2")
							|| rs.getString("IF_MATCH_ODD").equals("3")) {
						countM = rs.getInt("COUNT");
						if (!planStatus.equals("00")) {
							cro.setV_errMsg("该轧管订单(" + proPlanId + "/"
									+ gxPlanNo + "),判定炉号(" + judgeStoveNo
									+ ")已存在炉计划,并已下发,如需利库,请先撤销炉计划!");
							return cro;
						}
					} else {
						cro.setV_errMsg("该轧管订单(" + proPlanId + "/" + gxPlanNo
								+ "),判定炉号(" + judgeStoveNo
								+ ")已存在不是利库炉计划,如需利库,请先撤销炉计划!");
						return cro;
					}
				}
				rs.close();
				ps.close();

				// 算预计成品吨
				double aimOutdiam = Double.parseDouble(plnZgS
						.getAimoutdiameter());
				double aimwallthick = Double.parseDouble(plnZgS
						.getAimwallthick());
				double weightOne = (0.02466 * aimwallthick * (aimOutdiam - aimwallthick)) / 1000;
				double cutHeadEnd = (Double.parseDouble(plnZgS.getLenCutend()) + Double
						.parseDouble(plnZgS.getLenCuthead())) / 1000;
				double cpWt = weightOne
						* (Double.parseDouble(plnZgS.getRollLength()) - cutHeadEnd)
						* num;
				// 折算吨
				double reducedWt = (Double.parseDouble(plnZgS.getGpreqWeight()) / Double
						.parseDouble(plnZgS.getWeigthS()))
						* cpWt;
				if (countM == 0) {
					heatPlanNo = corePlnZyFurnM.getMaxHeatPlanNo(conn,
							proPlanId + "6", proPlanId, "D");
					// 获取炉计划顺序号
					int gxProSeq = corePlnZyFurnM.getMaxGxPlnSeq(conn, plnZgS
							.getPlineCode(), "D");
					plnZgM.setHeatPlanNo(heatPlanNo);
					plnZgM.setJudgeStoveNo(judgeStoveNo);
					plnZgM.setStoveNo(stoveNo);
					plnZgM.setFlagStoveNo(flagStoveNo);
					plnZgM.setGxProSeq(String.valueOf(gxProSeq));
					plnZgM.setUpperHeatplanId(furnaceno);
					plnZgM.setZyBatchId(plnZgS.getZyBatchId());
					plnZgM.setPlanstatus("00");
					if (proPlanIdZz.contains("DP")) {
						plnZgM.setUpperProcessCode("C");
					} else {
						plnZgM.setUpperProcessCode("B");
					}
					plnZgM.setPlineCode(plnZgS.getPlineCode());
					plnZgM.setPlineName(plnZgS.getPlineName());
					if (!plnZgS.getDecision().equals("A")) {
						// 通过制程判定轧管是否末工序
						if (plnZgS.getWholeBacklogDesc().contains("热处理")) {
							plnZgM.setNextProcessCode("F");
							plnZgM.setIsendgx("0");
						} else if (plnZgS.getWholeBacklogDesc().contains("镦拔扩")) {
							plnZgM.setNextProcessCode("E");
							plnZgM.setIsendgx("0");
						} else if (plnZgS.getWholeBacklogDesc().contains("加工")) {
							plnZgM.setNextProcessCode("G");
							plnZgM.setIsendgx("0");
						} else {
							plnZgM.setNextProcessCode("");
							plnZgM.setIsendgx("1");
						}
					} else {
						plnZgM.setNextProcessCode("");
						plnZgM.setIsendgx("1");
					}
					// 末工序获取去向
					// String gowWhere = ComPlanFurnace.getPlineCodeGroup(conn,
					// plnZgS.getPlineCode(), plnZgS.getProPlanId(),
					// plnZgS.getGxPlanNo());
					// if (gowWhere != null) {
					// if (gowWhere.contains("汽运")) {
					// plnZgM.setGowhereCode("110501");
					// } else if (gowWhere.contains("铁运")) {
					// plnZgM.setGowhereCode("110502");
					// } else {
					// String sql1 =
					// "SELECT T.PLINE_CODE FROM COM_BASE_PLINE T WHERE T.PLINE_NAME=?";
					// ps = conn.prepareStatement(sql1);
					// ps.setObject(1, gowWhere);
					// ResultSet rs1 = ps.executeQuery();
					// if (rs1.next()) {
					// plnZgM.setGowhereCode(rs1
					// .getString("PLINE_CODE"));
					// }
					// rs1.close();
					// ps.close();
					// }
					// }
					// plnZgM.setGowhereName(gowWhere);
					plnZgM.setIfMatchOdd("3");
					plnZgM.setIfDoHeatPlan("0");
					plnZgM.setProPlanId(plnZgS.getProPlanId());
					plnZgM.setGxPlanNo(plnZgS.getGxPlanNo());
					plnZgM.setCcmProPlanId(proPlanIdZz);
					plnZgM.setCcmGxPlanNo(gxPlanNoZz);
					plnZgM.setGradecode(gradecode);
					plnZgM.setGradename(gradename);
					plnZgM.setSteelcode(plnZgS.getSteelcode());
					plnZgM.setSteelname(plnZgS.getSteelname());
					// if (!gradename.equals(plnZgS.getGradename())) {
					// // 查询产出物料
					// String[] bomMat = StoveHelper.getQueryMateratBom(conn,
					// gradename, inwlDesc, plnZgS.getDiameterGp(),
					// proPlanId, gxPlanNo);
					// plnZgM.setInwlId(bomMat[0]);
					// plnZgM.setInwlDesc(bomMat[1]);
					// plnZgM.setOutwlId(bomMat[2]);
					// plnZgM.setOutwlDesc(bomMat[3]);
					// plnZgM.setBom(bomMat[4]);
					// }
					// else {
					plnZgM.setInwlId(plnZgS.getInwlId());
					plnZgM.setInwlDesc(plnZgS.getInwlDesc());
					plnZgM.setOutwlId(plnZgS.getOutwlId());
					plnZgM.setOutwlDesc(plnZgS.getOutwlDesc());
					plnZgM.setBom(plnZgS.getBom());
					// }

					plnZgM.setPassCode(plnZgS.getPassCode());
					plnZgM.setPassName(plnZgS.getPassName());
					plnZgM.setPlnInsingleNum(String.valueOf(num));
					plnZgM.setPlnInsingleWt(String.valueOf(actWt));

					plnZgM.setPlnOutsingleNum(String.valueOf(num));
					plnZgM.setPlnOutsingleW(String.valueOf(cpWt));
					plnZgM.setReducedWt(String.valueOf(reducedWt));
					plnZgM.setProcessCodeC(plnZgS.getProcessCodeC());
					plnZgM.setProcessDescC(plnZgS.getProcessDescC());
					plnZgM.setOrderSource(plnZgS.getOrderSource());
					plnZgM.setOrderNo(plnZgS.getOrderNo());
					plnZgM.setOrderSeq(plnZgS.getOrderSeq());
					plnZgM.setDeliveryNo(plnZgS.getDeliveryNo());
					plnZgM.setOrdLnDlyPk(plnZgS.getOrdLnDlyPk());
					plnZgM.setReqBlId(plnZgS.getReqBlId());
					plnZgM.setIfExport(plnZgS.getIfExport());
					plnZgM.setPriority(plnZgS.getPriority());
					plnZgM.setPriorityMemo(plnZgS.getPriorityMemo());
					plnZgM.setProMonth(plnZgS.getProMonth());
					plnZgM.setIfLenControl(plnZgS.getIfLenControl());
					plnZgM.setIfMinusSicRoll(plnZgS.getIfMinusSicRoll());
					plnZgM.setProdStandard(plnZgS.getProdStandard());
					plnZgM.setOutputStandard(plnZgS.getOutputStandard());
					plnZgM.setDecision(plnZgS.getDecision());
					plnZgM.setProgyMemo(plnZgS.getProgyMemo());
					plnZgM.setProduccode(plnZgS.getProduccode());
					plnZgM.setProducname(plnZgS.getProducname());
					plnZgM.setModelCode(plnZgS.getModelCode());
					plnZgM.setModelDesc(plnZgS.getModelDesc());
					plnZgM.setOutdiameter(plnZgS.getOutdiameter());
					plnZgM.setWallthick(plnZgS.getWallthick());
					plnZgM.setOutdiameterZg(plnZgS.getOutdiameterZg());
					plnZgM.setWallthickZg(plnZgS.getWallthickZg());
					plnZgM.setAimlength(plnZgS.getAimlength());
					plnZgM.setAimoutdiameter(plnZgS.getAimoutdiameter());
					plnZgM.setAimwallthick(plnZgS.getAimwallthick());
					plnZgM.setThicknessWall(plnZgS.getThicknessWall());
					plnZgM.setLenCutend(plnZgS.getLenCutend());
					plnZgM.setLenCuthead(plnZgS.getLenCuthead());
					plnZgM.setLenRollMax(plnZgS.getLenRollMax());
					plnZgM.setLenRollMin(plnZgS.getLenRollMin());
					plnZgM.setRollLength(plnZgS.getRollLength());
					plnZgM.setLenGpSingle(plnZgS.getLenGpSingle());
					plnZgM.setDiameter(plnZgS.getDiameterGp());
					plnZgM.setWtGpSingle(String.valueOf(weightGpOne));
					plnZgM.setOutnumCut(plnZgS.getOutnumCut());
					plnZgM.setCic(cic);
					if (plnZgS.getSic() == null || plnZgS.getSic().equals("")) {
						plnZgM.setSic(plnZgS.getSic());
					} else {
						plnZgM.setSic(plnZgS.getSic().split(",")[0]);
					}
					if (plnZgS.getDic() == null || plnZgS.getDic().equals("")) {
						plnZgM.setDic(plnZgS.getDic());
					} else {
						plnZgM.setDic(plnZgS.getDic().split(",")[0]);
					}
					if (plnZgS.getPic() == null || plnZgS.getPic().equals("")) {
						plnZgM.setPic(plnZgS.getPic());
					} else {
						plnZgM.setPic(plnZgS.getPic().split(",")[0]);
					}
					plnZgM.setProDay(dFormat.format(dFormat.parse(dateTime)));
					plnZgM.setBalYearMonth(dfMonth.format(dfMonth
							.parse(dateTime)));
					plnZgM.setCreateName(userName);
					plnZgM.setCreateTime(dateTime);
					plnZgM.setCutType(plnZgS.getPassive());
					plnZgM.setIfMonitor(plnZgS.getIfMonitorZg());
					plnZgM.setDiameterMandrel(plnZgS.getDiameterMandrel());
					plnZgM.setPrdcrNo(plnZgS.getPrdcrNo());
					plnZgM.setMonitorPoint(plnZgS.getMonitorPoint());
					plnZgM.setWholeBacklog(plnZgS.getWholeBacklog());
					plnZgM.setWholeBacklogDesc(plnZgS.getWholeBacklogDesc());
					plnZgM.setCraftFileMan(plnZgS.getCraftFileMan());
					plnZgM.setCraftFileNo(plnZgS.getCraftFileNo());
					plnZgM.setCraftSeq(plnZgS.getCraftSeq());
					plnZgM.setSecCraftFileNo(plnZgS.getSecCraftFileNo());
					plnZgM.setStationRoad(plnZgS.getStationRoad());
					plnZgM.setIndexSeq(plnZgS.getIndexSeq());
					plnZgM.setGptype(gptype);
					plnZgM.setOutsBilletFl("0");// 自炼
					plnZgM.setPlnType("0");
					// 新增轧管虚拟炉计划
					String sqlZgM = EntityTool
							.fillSqlByEntity(
									CoreXmlSqlParsersFactory
											.getSql("FrmPlanMaterialSelection.addPlnZyZgM"),
									plnZgM);
					ps = conn.prepareStatement(sqlZgM);
					ps.executeUpdate();
					ps.close();
				} else {
					// 修改轧管虚拟炉计划量
					String update = CoreXmlSqlParsersFactory
							.getSql("FrmPlanSteelMakMod.updateZgM");
					ps = conn.prepareStatement(update);
					ps.setObject(1, num);
					ps.setObject(2, actWt);
					ps.setObject(3, num);
					ps.setObject(4, cpWt);
					ps.setObject(5, reducedWt);
					ps.setObject(6, heatPlanNo);
					ps.executeUpdate();
					ps.close();
				}
			}
			// else {
			//
			// PlnZyDzMEntity plnDzM = new PlnZyDzMEntity();
			// plnDzM.setStoveNo(stoveNo);
			// plnDzM.setJudgeStoveNo(judgeStoveNo);
			// plnDzM.setFlagStoveNo(flagStoveNo);
			// plnDzM.setUpperHeatplanId(furnaceno);
			// plnDzM.setPlanstatus("00");
			// plnDzM.setUpperProcessCode("B");
			// plnDzM.setPlineCode("C005");
			// plnDzM.setPlineName("二炼快锻");
			// plnDzM.setNextProcessCode("D");
			// plnDzM.setIsendgx("0");
			// plnDzM.setGowhereCode("");
			// plnDzM.setGowhereName("快锻");
			// plnDzM.setLenGpSingleOut(plnZgS.getLenGpSingle());
			// plnDzM.setIfMatchOdd("1");
			// plnDzM.setIfDoHeatPlan("0");
			// plnDzM.setProPlanId(proPlanIdZz);
			// plnDzM.setGxPlanNo(gxPlanNoZz);
			// plnDzM.setCcmProPlanId(proPlanIdZz);
			// plnDzM.setCcmGxPlanNo(gxPlanNoZz);
			// plnDzM.setCastingtype("2");
			// plnDzM.setGradecode(plnZgS.get);
			// plnDzM.setGradename(gradename);
			// plnDzM.setSteelcode(plnZgS.getSteelcode());
			// plnDzM.setSteelname(plnZgS.getSteelname());
			// plnDzM.setWlidIn(inwlId);
			// plnDzM.setWldescIn(inwlDesc);
			// plnDzM.setOutwlId(plnZgS.getInwlId());
			// plnDzM.setOutwlDesc(plnZgS.getInwlDesc());
			// plnDzM.setDiameterIn(dimaterKc);
			// plnDzM.setLengthIn("1");
			// plnDzM.setWtOneIn(String.valueOf(lengthWt));
			// plnDzM.setLenGpSingle("1");
			// plnDzM.setWtGpSingle(String.valueOf(lengthWt));
			// plnDzM.setCutNumGp(String.valueOf(cutNumGp));
			// plnDzM.setPlnIndoubleNum(String.valueOf(takeCount));
			// plnDzM.setPlnIndoubleWt(String.valueOf(actWt));
			// plnDzM.setPlnInsingleNum(String.valueOf(num));
			// plnDzM.setPlnInsingleWt(String.valueOf(actWt));
			// plnDzM.setCutNumOut(String.valueOf(cutNumGp));
			// plnDzM.setDiameterOut(plnZgS.getDiameterGp());
			// plnDzM.setLengthOut(String.valueOf(Double.parseDouble(plnZgS
			// .getLenGpSingle())
			// * cutNumGp));
			// plnDzM.setOutWtOne(String.valueOf(weightGpOne));
			// plnDzM.setPlnOutdoubleNum(String.valueOf(takeCount));
			// plnDzM.setPlnOutdoubleW(String.valueOf(actWt));
			// plnDzM.setPlnOutsingleNum(String.valueOf(num));
			// plnDzM.setPlnOutsingleW(String.valueOf(actWt));
			// plnDzM.setCic(plnZgS.getCicGp());
			// plnDzM.setSic(plnZgS.getSicGp());
			// plnDzM.setPic(plnZgS.getPicGp());
			// plnDzM.setDic(plnZgS.getDicGp());
			// plnDzM.setProDay(dFormat.format(dFormat.parse(dateTime)));
			// plnDzM.setBalYearMonth(dfMonth.format(dfMonth.parse(dateTime)));
			// plnDzM.setCreateName(userName);
			// plnDzM.setCreateTime(dateTime);
			// // 存在相同判定炉号的锻造虚拟炉计划
			// String heatPlanNo = corePlnZyFurnM.getMaxHeatPlanNo(conn,
			// plnDzM.getProPlanId() + plnDzM.getGxPlanNo(), "C");
			// // 获取炉计划顺序号
			// int gxProSeq = corePlnZyFurnM.getMaxGxPlnSeq(conn, plnDzM
			// .getPlineCode(), "C");
			// plnDzM.setHeatPlanNo(heatPlanNo);
			// plnDzM.setGxProSeq(String.valueOf(gxProSeq));
			// String addPlnGxDz = EntityTool.fillSqlByEntity(
			// CoreXmlSqlParsersFactory
			// .getSql("ComPlanFurnace.addPlnZyDzM"), plnDzM);
			// ps = conn.prepareStatement(addPlnGxDz);
			// ps.executeUpdate();
			// ps.close();
			// if (matStrat.equals("80150102")) {
			// PlnZyDzCEntity plnDzC = new PlnZyDzCEntity();
			// plnDzC.setHeatPlanNo(heatPlanNo);
			// plnDzC.setJugdeApplyCode(jugdeApplyCode);
			// plnDzC.setStoveNo(stoveNo);
			// plnDzC.setJudgeStoveNo(judgeStoveNo);
			// plnDzC.setFlagStoveNo(flagStoveNo);
			// // plnDzC.setZyBatchId(plnDzS.getZyBatchId());
			// plnDzC.setPlineCode("C005");
			// plnDzC.setPlineName("二炼快锻");
			// plnDzC.setProPlanId(proPlanIdZz);
			// plnDzC.setGxPlanNo(gxPlanNoZz);
			// plnDzC.setDiameter(dimaterKc);
			// plnDzC.setLenGpSingle("1");
			// plnDzC.setWtGpSingle("");
			// plnDzC.setCutNumGp("");
			// plnDzC.setLengthM("");
			// plnDzC.setWeightM("");
			// plnDzC.setPlanIndoubleNum(String.valueOf(num));
			// plnDzC.setPlanIndoubleWt(String.valueOf(actWt));
			// plnDzC.setCreateName(userName);
			// plnDzC.setCreateTime(dateTime);
			// // 获取上料行ID
			// String heatPlanSeq = corePlnZyFurnM.getHeatPlanSeq(conn,
			// heatPlanNo, "C");
			// plnDzC.setHeatPlanSqe(heatPlanSeq);
			// // 新增
			// String addPlnGxDzC = EntityTool.fillSqlByEntity(
			// CoreXmlSqlParsersFactory
			// .getSql("ComPlanFurnace.addPlnZyDzC"),
			// plnDzC);
			// ps = conn.prepareStatement(addPlnGxDzC);
			// ps.executeUpdate();
			// ps.close();
			// }
			// }
			cro.setV_errMsg("利库成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 炼钢脱单
	 * 
	 * @param jslist
	 * @param userName
	 *            脱单人
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doDeleteMonomerLg(ArrayList<String> list,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			DecimalFormat df = new DecimalFormat("0000000000");
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);

			// 循环库存信息
			// for (int i = 0; i < jslist.size(); i++) {
			// ArrayList<String> list = (ArrayList<String>) jslist.get(i);
			double actLen = Double.parseDouble(list.get(0));// 倍尺坯长
			double fixLen = Double.parseDouble(list.get(1));// 单倍
			int cutNumGp = Integer.parseInt(list.get(2));
			String judgeStoveNo = list.get(3);
			// String stoveNo = list.get(4);
			// String flagStoveNo = list.get(5);
			int takeCount = Integer.parseInt(list.get(6));// 利库支
			String proPlanIdZg = list.get(7);
			String dimart = list.get(8);
			String gxPlanNoZg = list.get(9);
			List<MatGpMEntity> matGpMlist = getQureyMatGpMLTd(conn,
					judgeStoveNo, actLen, fixLen, cutNumGp, proPlanIdZg,
					gxPlanNoZg);
			if (matGpMlist == null) {
				cro.setV_errMsg("管坯物料已发生改变,请刷新!");
				return cro;
			}
			int seq = 0;
			String proPlanId = "";
			String gxPlanNo = "";
			String lkFlag = "";
			String proPlanIdZz = "";
			String gxPlanNoZz = "";
			double actwt = 0;
			String jugdeApplyCode = "";
			String oldCic = "";
			for (MatGpMEntity matGpM : matGpMlist) {
				jugdeApplyCode = matGpM.getJugdeApplyCode();
				proPlanId = matGpM.getProPlanIdZg();
				gxPlanNo = matGpM.getGxPlanNoZg();
				lkFlag = matGpM.getLkFlag();
				proPlanIdZz = matGpM.getProPlanId();
				gxPlanNoZz = matGpM.getGxPlanNo();
				actwt += Double.parseDouble(matGpM.getActWeight());
				oldCic = matGpM.getOldCic();
				if (!matGpM.getQcmStaus().equals("00")) {
					cro.setV_errMsg("交库状态不是合格状态,不能脱单!");
					return cro;
				}
				if (!matGpM.getDimater().contains("t")) {

					if (matGpM.getMatStatus().equals("80150102")
							&& !matGpM.getJudgeResultCode().equals("40740704")) {
						cro.setV_errMsg("您选择判定炉号材料已判，综合判定结果不是不合格状态,不能脱单");
						return cro;
					}
					if (matGpM.getMatStatus().equals("80150101")
							&& !matGpM.getJugdeApplyCode().equals("")) {
						cro.setV_errMsg("您选择判定炉号材料未判,但已申请判定,不能脱单!");
						return cro;
					}
				} else {
					if (matGpM.getMatStatus().equals("80150101")
							&& !matGpM.getJugdeApplyCode().equals("")) {
						cro.setV_errMsg("您选择判定炉号材料未判,但已申请判订,不能脱单!");
						return cro;
					}
				}

				// if (matGpM.getOrderNo() == "") {
				// cro.setV_errMsg("你选择的管坯物料未挂合同,不能脱单!");
				// return cro;
				// }
				// 修改坯子库存信息
				String sqlUpdateMat = CoreXmlSqlParsersFactory
						.getSql("FrmPlanSteelMakMod.UpdateMatGp");
				sqlUpdateMat += ",T.HEAT_PLAN_NO_NEXT=''";
				if (matGpM.getJudgeResultCode().equals("40740704")) {
					sqlUpdateMat += " ,T.JUGDE_APPLY_FLAG='0',T.JUGDE_APPLY_CODE='',T.JUGDE_APPLY_TIME='',T.JUDGE_RESULT_CODE='',T.JUDGE_RESULT_TIME='',T.JUDGE_MAKER=''";
				}
				if (!oldCic.equals("")) {
					sqlUpdateMat += ",T.CIC='" + matGpM.getOldCic()
							+ "',T.OLD_CIC='',T.STEEL_STANDRAD='"
							+ matGpM.getOldCic() + "+" + matGpM.getPic() + "+"
							+ matGpM.getSic() + "+" + matGpM.getDic() + "'";
				}
				// if (matGpM.getLkFlag().equals("2")) {// 炼钢利库
				// sqlUpdateMat +=
				// ",T.MATERIAL_NO = ?,T.MATERIAL_NAME = ?,T.INPUT_MATERIAL_NAME = ?, T.INPUT_MATERIAL_NO = ?,"
				// +
				// "T.GRADECODE = ?,T.GRADENAME= ?,T.CIC= ?,T.OLD_GRADECODE= ?,T.OLD_GRADENAME= ?,T.OLD_CIC = ?,"
				// + "T.OLD_MATERIAL_NO=?,T.OLD_MATERIAL_NAME=?";
				// }
				sqlUpdateMat += " WHERE T.MAT_NO =?";
				ps = conn.prepareStatement(sqlUpdateMat);
				ps.setObject(1, "");
				ps.setObject(2, "");
				ps.setObject(3, "");
				ps.setObject(4, "");
				ps.setObject(5, "");
				ps.setObject(6, "");
				ps.setObject(7, "");
				ps.setObject(8, "");
				ps.setObject(9, "0");
				ps.setObject(10, matGpM.getProPlanId());
				ps.setObject(11, matGpM.getGxPlanNo());
				// if (matGpM.getJudgeResultCode().equals("40740704")) {
				ps.setObject(12, "80150101");
				// } else {
				// ps.setObject(12, matGpM.getMatStatus());
				// }
				ps.setObject(13, "0");
				// if (matGpM.getLkFlag().equals("2")) {
				// ps.setObject(14, matGpM.getOldMaterialNo());
				// ps.setObject(15, matGpM.getOldMaterialName());
				// ps.setObject(16, matGpM.getOldGradename());
				// ps.setObject(17, matGpM.getOldGradecode());
				// ps.setObject(18, matGpM.getOldGradecode());
				// ps.setObject(19, matGpM.getOldGradename());
				// ps.setObject(20, matGpM.getOldCic());
				// ps.setObject(21, "");
				// ps.setObject(22, "");
				// ps.setObject(23, "");
				// ps.setObject(24, "");
				// ps.setObject(25, "");
				// ps.setObject(26, matGpM.getMatNo());
				// } else {
				ps.setObject(14, matGpM.getMatNo());
				// }
				ps.executeUpdate();
				ps.close();

				seq += 1;
				String resumeSeq = dateFormat.format(now) + df.format(seq);
				// 生成利库日志
				MatGpTrackEntity matGpTrack = new MatGpTrackEntity();
				matGpTrack.setResumeSeq(resumeSeq);
				matGpTrack.setEventId("900101");
				matGpTrack.setEventDesc("炼钢脱单");
				matGpTrack
						.setFormName("com.steering.pss.plnsaleord.planStove.FrmPlanSteelMakMod");
				matGpTrack.setCallFuncCoding("doDeleteMonomerLg");
				matGpTrack.setKeyvalue1("List<MatGpMEntity> matGpMlist");
				matGpTrack.setKeyvalue1Desc("物料主表关键字实体类集合");
				matGpTrack.setKeyvalue2("String userName");
				matGpTrack.setKeyvalue2Desc2("操作人");
				matGpTrack.setKeyvalue3("doDeleteMonomerLg");
				matGpTrack.setKeyvalue3Desc3("删除虚拟炉计划及脱合同");
				matGpTrack.setMatNo(matGpM.getMatNo());
				matGpTrack.setProductFlag(matGpM.getProductFlag());
				matGpTrack.setMatStatus(matGpM.getMatStatus());
				matGpTrack.setStoveNo(matGpM.getStoveNo());
				matGpTrack.setFlagStoveNo(matGpM.getFlagStoveNo());
				matGpTrack.setJudgeStoveNo(matGpM.getJudgeStoveNo());
				matGpTrack.setBatchNo("");
				matGpTrack.setGroudNo(matGpM.getGroudNo());
				matGpTrack.setCastNo(matGpM.getCastNo());
				matGpTrack.setCcmid(matGpM.getCcmid());
				matGpTrack.setGradecode(matGpM.getGradecode());
				matGpTrack.setGradename(matGpM.getGradename());
				matGpTrack.setStdCode(matGpM.getStdCode());
				matGpTrack.setStdName(matGpM.getStdName());
				matGpTrack.setActDimater(matGpM.getDimater().replace("t", ""));
				matGpTrack.setActHeight(matGpM.getHeight());
				matGpTrack.setActLen(String.valueOf(actLen));
				matGpTrack.setActLenMax("");
				matGpTrack.setActLenMin("");
				matGpTrack.setActFixLen(String.valueOf(fixLen));
				matGpTrack.setActFixNum(matGpM.getFixNum());
				matGpTrack.setActCount("1");
				matGpTrack.setActWeight(matGpM.getActWeight());
				matGpTrack.setActTheoryWeight(matGpM.getActTheoryWeight());
				matGpTrack.setOrdPk("");
				matGpTrack.setOrdLnPk("");
				matGpTrack.setOrdLnDlyPk("");
				matGpTrack.setOrderNo("");
				matGpTrack.setOrderSeq("");
				matGpTrack.setOrderSeqNo("");
				// matGpTrack.setOldOrdPk(matGpM.getOrdPk());
				// matGpTrack.setOldOrdLnPk(matGpM.getOrdLnPk());
				// matGpTrack.setOldOrdLnDlyPk(matGpM.getOrdLnDlyPk());
				matGpTrack.setOldOrderNo(matGpM.getOrderNo());
				matGpTrack.setOldOrderSeq(matGpM.getOrderSeq());
				matGpTrack.setOldOrderSeqNo(matGpM.getOrderSeqNo());
				matGpTrack.setMemo("");
				matGpTrack.setCreateName(userName);
				matGpTrack.setCreateTime(dateTime);
				matGpTrack.setOldJudgeStoveNo(matGpM.getJudgeStoveNo());
				matGpTrack.setOldBatchNo("");
				matGpTrack.setOldGroudNo(matGpM.getGroudNo());
				matGpTrack.setOldGradecode(matGpM.getGradecode());
				matGpTrack.setOldGradename(matGpM.getGradename());
				matGpTrack.setOldStdCode(matGpM.getStdCode());
				matGpTrack.setOldStdName(matGpM.getStdName());
				matGpTrack.setOldStoveNo(matGpM.getStoveNo());
				String sqlAddTrack = EntityTool
						.fillSqlByEntity(
								CoreXmlSqlParsersFactory
										.getSql("FrmPlanMaterialSelection.addMatGpTrack"),
								matGpTrack);
				ps = conn.prepareStatement(sqlAddTrack);
				ps.executeUpdate();
				ps.close();
			}
			int count = 0;
			String heatPlanNo = "";
			String ifMatchOdd = "";
			// 修改申请判定表有效标志
			String updateApply = "UPDATE QCM_GP_JUDGE_APPLY T SET T.IS_FLAG='1' WHERE T.JUDGE_STOVE_NO=? AND T.JUGDE_APPLY_CODE||T.JUGDE_APPLY_SQE=? AND T.MULTIPLE_NUM=?";
			ps = conn.prepareStatement(updateApply);
			ps.setObject(1, judgeStoveNo);
			ps.setObject(2, jugdeApplyCode);
			ps.setObject(3, cutNumGp);
			ps.executeUpdate();
			ps.close();
			if (dimart.contains("t")) {
				// 删除锻造虚拟炉计划
				String sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.IF_MATCH_ODD FROM PLN_ZY_DZ_M  T "
						+ " WHERE T.JUDGE_STOVE_NO=? AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? ";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, judgeStoveNo);
				ps.setObject(2, proPlanId);
				ps.setObject(3, gxPlanNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					count++;
					heatPlanNo = rs.getString("HEAT_PLAN_NO");
					ifMatchOdd = rs.getString("IF_MATCH_ODD");
					if (!rs.getString("PLANSTATUS").equals("00")) {
						cro.setV_errMsg("您选中的数据,炉计划已下发,不能脱单!");
						return cro;
					}
				}
				rs.close();
				ps.close();

				if (count > 0) {
					String deleteSql = "DELETE PLN_ZY_DZ_M  WHERE HEAT_PLAN_NO=?";
					ps = conn.prepareStatement(deleteSql);
					ps.setObject(1, heatPlanNo);
					ps.executeUpdate();
					ps.close();
					String deleteSqlC = "DELETE PLN_ZY_DZ_C  WHERE HEAT_PLAN_NO=?";
					ps = conn.prepareStatement(deleteSqlC);
					ps.setObject(1, heatPlanNo);
					ps.executeUpdate();
					ps.close();
				}

			} else {
				// 删除轧管虚拟炉计划
				String sql = "SELECT T.HEAT_PLAN_NO,T.PLANSTATUS,T.IF_MATCH_ODD FROM PLN_ZY_ZG_M  T "
						+ " WHERE T.JUDGE_STOVE_NO=? AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? ";
				ps = conn.prepareStatement(sql);
				ps.setObject(1, judgeStoveNo);
				ps.setObject(2, proPlanId);
				ps.setObject(3, gxPlanNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					count++;
					heatPlanNo = rs.getString("HEAT_PLAN_NO");
					ifMatchOdd = rs.getString("IF_MATCH_ODD");
					if (!rs.getString("PLANSTATUS").equals("00")) {
						cro.setV_errMsg("您选中的数据,炉计划已下发,不能脱单!");
						return cro;
					}
				}
				rs.close();
				ps.close();
				if (count > 0) {
					String deleteSql = "DELETE PLN_ZY_ZG_M  WHERE HEAT_PLAN_NO=?";
					ps = conn.prepareStatement(deleteSql);
					ps.setObject(1, heatPlanNo);
					ps.executeUpdate();
					ps.close();
				}
			}
			if (lkFlag.equals("2") && ifMatchOdd.equals("2")) {
				// 修改铸造订单利库量
				String updateZz = "UPDATE PLN_ORDER_CAST_S T SET T.PLAN_WT_SOLD=DECODE(NVL(T.PLAN_NUM_SOLD, 0) - ?/T.CUT_NUM_GP,'0','0',NVL(T.PLAN_WT_SOLD, 0) -((?/T.CUT_NUM_GP) * NVL(T.WT_CAST_ONE, 0))),"
						+ "T.PLAN_NUM_SOLD=NVL(T.PLAN_NUM_SOLD,0)-?/T.CUT_NUM_GP,T.UPDATE_NAME=?,T.UPDATE_TIME=SYSDATE WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
				ps = conn.prepareStatement(updateZz);
				ps.setObject(1, takeCount*cutNumGp);
				ps.setObject(2, takeCount*cutNumGp);
				ps.setObject(3, takeCount*cutNumGp);
				ps.setObject(4, userName + "-[撤销利库]");
				ps.setObject(5, proPlanIdZz);
				ps.setObject(6, gxPlanNoZz);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("脱单成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 炼钢坯子利库铸造
	 * 
	 * @param jslist
	 * @param userName
	 *            利库人
	 * @throws ParseException
	 */
	public CoreReturnObject doAddCastLg(ArrayList<String> list, String userName)
			throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat dfMonth = new SimpleDateFormat("yyyyMM");
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			DecimalFormat df = new DecimalFormat("0000000000");
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);

			String proPlanId = list.get(0);
			String gxPlanNo = list.get(1);
			// double reallWt = Double.parseDouble(list.get(2));
			String judgeStoveNo = list.get(3);
			double actLenTake = Double.parseDouble(list.get(4));// 库存倍尺坯长
			double fixLenTake = Double.parseDouble(list.get(5));// 库存单倍
			int cutNumGpKc = Integer.parseInt(list.get(6));// 库存倍尺数
			int takeCount = Integer.parseInt(list.get(7));// 利库支
			String dimaterKc = list.get(8);
			String gptype = list.get(9);
			double takeWeight = Double.parseDouble(list.get(10));// 利库吨
			double lenGpSingle = Double.parseDouble(list.get(11));// 铸造订单单倍
			String gradeName = list.get(12);
			// if (actLenTake % lenGpSingle != 0) {
			// cro.setV_errMsg("倍尺数不是整数,不能利库!");
			// return cro;
			// }
			int cutNumGp = (int) (actLenTake / lenGpSingle);
			// 查询轧管信息
			PlnOrderCastSEntity entity1 = ControlOrderPlan.getOrderCastById(
					conn, proPlanId, gxPlanNo);
			if (entity1 == null) {
				cro.setV_errMsg("铸造数据异常,请刷新数据!");
				return cro;
			}
			if (Double.parseDouble(entity1.getCanWgt()) <= 0) {
				cro.setV_errMsg("可组炉重量为0,请刷新数据!");
				return cro;
			}
			List<PlnOrderZgSEntityL> plnZg = ComPlanFurnace.getPlanSteOrdZg(
					conn, proPlanId, gxPlanNo, "1");
			if (plnZg == null) {
				cro.setV_errMsg("未找到轧管订单信息!");
				return cro;
			}
			int num = takeCount * cutNumGp;
			int oldNum = 0;// 原料计划支
			int takeCountK = 0;// 可利库支
			double takeWtK = 0;// 可利库吨
			int seq = 0;
			int counNum=0;
			if (!dimaterKc.contains("t")) {// 铸造
				for (PlnOrderZgSEntityL plnZgS : plnZg) {
					counNum++;
					if (takeWeight <= 0) {
						continue;
					}
					int dimater = Integer.parseInt(plnZgS.getDiameterGp());
					if (dimater == 251) {
						dimater = 250;
					}
					String cicLg = "";
					String cicLg1 = "";
					// 获取炼钢CIC
					String sqlCic = CoreXmlSqlParsersFactory
							.getSql("FrmPlanSteelMakMod.queryCic");
					ps = conn.prepareStatement(sqlCic);
					ps.setObject(1, gradeName);
					ps.setObject(2, plnZgS.getProPlanId());
					ps.setObject(3, plnZgS.getGxPlanNo());
					rs = ps.executeQuery();
					while (rs.next()) {
						cicLg = rs.getString("CIC");
						cicLg1 += rs.getString("CIC") + ",";
					}
					rs.close();
					ps.close();
					if (cicLg.equals("")) {
						cro.setV_errMsg("CIC不能为空!");
						return cro;
					}
					if (!cicLg1.equals("")) {
						cicLg1 = cicLg1.substring(0, cicLg1.length() - 1);
					}
					if (entity1.getGradecode().equals(gradeName)
							&& cicLg1.contains(entity1.getCic())) {
						cicLg = entity1.getCic();
					}
					double mSingleWeight = (7.8 * Math.PI / 4 * dimater * dimater) / 1000 / 1000;// 米单重
					// 单倍坯重
					double weightGpOne = mSingleWeight
							* (Double.parseDouble(plnZgS.getLenGpSingle()) / 1000);
					// 倍尺坯单支重=坯米单重*倍尺坯长/1000
					// double weightM = mSingleWeight * (actLenTake / 1000);
					double wtIn = 0;// 已生成炉计划原料吨(已装炉)
					int numIn = 0;
					// 查询当前轧管订单是否存在炉计划
					String sql = "SELECT SUM(T.PLN_INSINGLE_NUM) PLN_INSINGLE_NUM,SUM(T.REDUCED_WT) PLN_INSINGLE_WT FROM PLN_ZY_ZG_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
					ps = conn.prepareStatement(sql);
					ps.setObject(1, plnZgS.getProPlanId());
					ps.setObject(2, plnZgS.getGxPlanNo());
					rs = ps.executeQuery();
					if (rs.next()) {
						wtIn = rs.getDouble("PLN_INSINGLE_WT");
						numIn = rs.getInt("PLN_INSINGLE_NUM");
					}
					rs.close();
					ps.close();
					if (plnZg.size() > 1) {
						if (takeWeight
								- (Double.parseDouble(plnZgS.getGpreqWeight()) - wtIn) < 0.00002) {
							// oldWt = takeWeight;
							oldNum = num;
						} else {
							oldNum = Integer.parseInt(plnZgS.getGpreqNum())
									- numIn;
							if(counNum==plnZg.size()){
								oldNum = num;
							}
							// oldWt = oldNum * weightGpOne;
						}

					} else if (plnZg.size() == 1) {
						// oldWt = takeWeight;
						oldNum = num;
					}

					if (oldNum <= 0) {
						continue;
					}
					int numkc = (int) Math.ceil(oldNum / cutNumGp);
					List<MatGpMEntity> matGpMlist = getQureyMatGpML(conn,
							judgeStoveNo, actLenTake, fixLenTake, cutNumGpKc,
							numkc, "", "");
					if (matGpMlist == null) {
						cro.setV_errMsg("管坯物料已发生改变,请刷新!");
						return cro;
					}

				
					String stoveNo = "";
					String flagStoveNo = "";
					String furnaceno = "";
					String inwlDesc = "";
					String gradename = "";
					String gradecode = "";
					double actWt = 0;
					String cic = "";
					String jugdeApplyCode = "";
					for (MatGpMEntity matGpM : matGpMlist) {
						jugdeApplyCode = matGpM.getJugdeApplyCode();
						takeCountK += Integer.parseInt(matGpM.getActCount());
						takeWtK += Double.parseDouble(matGpM.getActWeight());
						actWt += Double.parseDouble(matGpM.getActWeight());
						stoveNo = matGpM.getStoveNo();
						flagStoveNo = matGpM.getFlagStoveNo();
						furnaceno = matGpM.getFurnaceno() + "/"
								+ matGpM.getSequenceno();
						inwlDesc = matGpM.getMaterialName();
						gradename = matGpM.getGradename();
						gradecode = matGpM.getGradecode();
						cic = matGpM.getCic();
						// if (!matGpM.getProductFlag().equals("801401")) {
						// cro.setV_errMsg("材料类别不是在制品,不能利库!");
						// return cro;
						// }
						if (!matGpM.getMatStatus().equals("80150101")
								&& !matGpM.getMatStatus().equals("80150102")) {
							cro.setV_errMsg("材料状态不是产出待判或产出已判,不能利库");
							return cro;
						}
						if (matGpM.getMatStatus().equals("80150101")
								&& !matGpM.getJugdeApplyCode().equals("")) {
							cro.setV_errMsg("您选择判定炉号材料未判,但已申请判定,不能利库!");
							return cro;
						}
						if (!matGpM.getQcmStaus().equals("00")) {
							cro.setV_errMsg("交库状态不是合格状态,不能利库!");
							return cro;
						}
						if (matGpM.getProPlanIdZg() != "") {
							cro.setV_errMsg("你选择的管坯物料已挂合同,不能利库。如需利库,请先脱单!");
							return cro;
						}
						String udpatePlnSql  = CoreXmlSqlParsersFactory.getSql("FrmPlanSteelMakMod.UpdatePlnOrdCic");//修改连铸计划CIC
						ps = conn.prepareStatement(udpatePlnSql);
						ps.setObject(1,cicLg);
						ps.setObject(2,proPlanId);
						ps.setObject(3,gxPlanNo);
						ps.setObject(4,judgeStoveNo);
						ps.executeUpdate();
						ps.close();
						// 修改坯子库存信息
						String sqlUpdateMat = CoreXmlSqlParsersFactory
								.getSql("FrmPlanSteelMakMod.UpdateMatGpOld");
						sqlUpdateMat += ",T.OLD_CIC='" + matGpM.getCic()
								+ "',T.CIC='" + cicLg + "',T.STEEL_STANDRAD='"
								+ cicLg + "+" + matGpM.getPic() + "+"
								+ matGpM.getSic() + "+" + matGpM.getDic() + "'";
						sqlUpdateMat += ",T.JUGDE_APPLY_FLAG='0',T.JUGDE_APPLY_CODE='',T.JUGDE_APPLY_TIME='',T.JUDGE_RESULT_CODE='',T.JUDGE_RESULT_TIME='',T.JUDGE_MAKER=''";
						sqlUpdateMat += "WHERE T.MAT_NO = ?";
						ps = conn.prepareStatement(sqlUpdateMat);
						ps.setObject(1, plnZgS.getProPlanId());
						ps.setObject(2, plnZgS.getGxPlanNo());
						ps.setObject(3, plnZgS.getOrderNo());
						ps.setObject(4, plnZgS.getOrderSeq());
						ps.setObject(5, plnZgS.getDeliveryNo());
						if (plnZgS.getOrderSource().equals("备料需求")) {
							ps.setObject(6, "");
							ps.setObject(7, "");
						} else {
							ps.setObject(6, plnZgS.getOrdLnDlyPk().substring(0,
									plnZgS.getOrdLnDlyPk().length() - 6));
							ps.setObject(7, plnZgS.getOrdLnDlyPk().substring(0,
									plnZgS.getOrdLnDlyPk().length() - 3));
						}
						ps.setObject(8, plnZgS.getOrdLnDlyPk());
						ps.setObject(9, "2");
						ps.setObject(10, proPlanId);
						ps.setObject(11, gxPlanNo);
						// if (!matGpM.getMatStatus().equals("80150101")) {
						ps.setObject(12, "80150101");
						// } else {
						// ps.setObject(12, matGpM.getMatStatus());
						// }
						ps.setObject(13, "0");
						ps.setObject(14, matGpM.getMatNo());
						ps.executeUpdate();
						ps.close();

						seq += 1;
						String resumeSeq = dateFormat.format(now)
								+ df.format(seq);
						// 生成利库日志
						MatGpTrackEntity matGpTrack = new MatGpTrackEntity();
						matGpTrack.setResumeSeq(resumeSeq);
						matGpTrack.setEventId("900102");
						matGpTrack.setEventDesc("炼钢利库");
						matGpTrack
								.setFormName("com.steering.pss.plnsaleord.planStove.FrmPlanSteelMakMod");
						matGpTrack.setCallFuncCoding("doAddCastLg");
						matGpTrack
								.setKeyvalue1("List<MatGpMEntity> matGpMlist");
						matGpTrack.setKeyvalue1Desc("物料主表关键字实体类集合");
						matGpTrack.setKeyvalue2("String userName");
						matGpTrack.setKeyvalue2Desc2("操作人");
						matGpTrack.setKeyvalue3("doAddCastLg");
						matGpTrack.setKeyvalue3Desc3("炼钢利铸造订单");
						matGpTrack.setMatNo(matGpM.getMatNo());
						matGpTrack.setProductFlag(matGpM.getProductFlag());
						matGpTrack.setMatStatus(matGpM.getMatStatus());
						matGpTrack.setStoveNo(matGpM.getStoveNo());
						matGpTrack.setFlagStoveNo(matGpM.getFlagStoveNo());
						matGpTrack.setJudgeStoveNo(matGpM.getJudgeStoveNo());
						matGpTrack.setBatchNo("");
						matGpTrack.setGroudNo(matGpM.getGroudNo());
						matGpTrack.setCastNo(matGpM.getCastNo());
						matGpTrack.setCcmid(matGpM.getCcmid());
						matGpTrack.setGradecode(matGpM.getGradecode());
						matGpTrack.setGradename(matGpM.getGradename());
						matGpTrack.setStdCode(matGpM.getStdCode());
						matGpTrack.setStdName(matGpM.getStdName());
						matGpTrack.setActDimater(matGpM.getDimater());
						matGpTrack.setActHeight(matGpM.getHeight());
						matGpTrack.setActLen(String.valueOf(actLenTake));
						matGpTrack.setActLenMax(plnZgS.getLenRollMax());
						matGpTrack.setActLenMin(plnZgS.getLenRollMin());
						matGpTrack.setActFixLen(String.valueOf(lenGpSingle));
						matGpTrack.setActFixNum(String.valueOf(cutNumGp));
						matGpTrack.setActCount("1");
						matGpTrack.setActWeight(matGpM.getActWeight());
						matGpTrack.setActTheoryWeight(matGpM
								.getActTheoryWeight());
						if (plnZgS.getOrderSource().equals("备料需求")) {
							matGpTrack.setOrdPk("");
							matGpTrack.setOrdLnPk("");
						} else {
							matGpTrack
									.setOrdPk(plnZgS.getOrdLnDlyPk()
											.substring(
													0,
													plnZgS.getOrdLnDlyPk()
															.length() - 6));
							matGpTrack
									.setOrdLnPk(plnZgS.getOrdLnDlyPk()
											.substring(
													0,
													plnZgS.getOrdLnDlyPk()
															.length() - 3));
						}
						matGpTrack.setOrdLnDlyPk(plnZgS.getOrdLnDlyPk());
						matGpTrack.setOrderNo(plnZgS.getOrderNo());
						matGpTrack.setOrderSeq(plnZgS.getOrderSeq());
						matGpTrack.setOrderSeqNo(plnZgS.getDeliveryNo());
						// matGpTrack.setOldOrdPk(matGpM.getOrdPk());
						// matGpTrack.setOldOrdLnPk(matGpM.getOrdLnPk());
						// matGpTrack.setOldOrdLnDlyPk(matGpM.getOrdLnDlyPk());
						matGpTrack.setOldOrderNo(matGpM.getOldOrderNo());
						matGpTrack.setOldOrderSeq(matGpM.getOldOrderSeq());
						matGpTrack.setOldOrderSeqNo(matGpM.getOldOrderSeqNo());
						matGpTrack.setMemo("");
						matGpTrack.setCreateName(userName);
						matGpTrack.setCreateTime(dateTime);
						matGpTrack.setOldJudgeStoveNo(matGpM
								.getOldJudgeStoveNo());
						matGpTrack.setOldBatchNo("");
						matGpTrack.setOldGroudNo(matGpM.getOldGroudNo());
						matGpTrack.setOldGradecode(matGpM.getOldGradecode());
						matGpTrack.setOldGradename(matGpM.getOldGradename());
						matGpTrack.setOldStdCode(matGpM.getOldStdCode());
						matGpTrack.setOldStdName(matGpM.getOldStdName());
						matGpTrack.setOldStoveNo(matGpM.getOldStoveNo());
						String sqlAddTrack = EntityTool
								.fillSqlByEntity(
										CoreXmlSqlParsersFactory
												.getSql("FrmPlanMaterialSelection.addMatGpTrack"),
										matGpTrack);
						ps = conn.prepareStatement(sqlAddTrack);
						ps.executeUpdate();
						ps.close();
					}

					// 修改申请判定表有效标志
					String updateApply = "UPDATE QCM_GP_JUDGE_APPLY T SET T.IS_FLAG='1' WHERE T.JUDGE_STOVE_NO=? AND T.JUGDE_APPLY_CODE||T.JUGDE_APPLY_SQE=? AND T.MULTIPLE_NUM=?";
					ps = conn.prepareStatement(updateApply);
					ps.setObject(1, judgeStoveNo);
					ps.setObject(2, jugdeApplyCode);
					ps.setObject(3, cutNumGpKc);
					ps.executeUpdate();
					ps.close();

					PlnZyZgMEntity plnZgM = new PlnZyZgMEntity();
					String heatPlanNo = "";
					int countM = 0;
					String planStatus = "00";
					// 根据判定炉号，轧管订单号查询是否存在虚拟炉计划
					String sqlQuery = "SELECT COUNT(*) COUNT,T.HEAT_PLAN_NO,T.PLANSTATUS,T.FEED_HEAT_NO,T.LEN_GP_SINGLE,T.IF_MATCH_ODD FROM PLN_ZY_ZG_M T WHERE T.JUDGE_STOVE_NO=? "
							+ " AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? GROUP BY T.HEAT_PLAN_NO,T.PLANSTATUS,T.FEED_HEAT_NO,T.LEN_GP_SINGLE,T.IF_MATCH_ODD";
					ps = conn.prepareStatement(sqlQuery);
					ps.setObject(1, judgeStoveNo);
					ps.setObject(2, proPlanId);
					ps.setObject(3, gxPlanNo);
					rs = ps.executeQuery();
					while (rs.next()) {
						heatPlanNo = rs.getString("HEAT_PLAN_NO");
						planStatus = rs.getString("PLANSTATUS");
						if (rs.getString("IF_MATCH_ODD").equals("2")
								|| rs.getString("IF_MATCH_ODD").equals("3")) {
							countM = rs.getInt("COUNT");
							if (!planStatus.equals("00")) {
								cro.setV_errMsg("该轧管订单(" + proPlanId + "/"
										+ gxPlanNo + "),判定炉号(" + judgeStoveNo
										+ ")已存在炉计划,并已下发,如需利库,请先撤销炉计划!");
								return cro;
							}
						} else {
							cro.setV_errMsg("该轧管订单(" + proPlanId + "/"
									+ gxPlanNo + "),判定炉号(" + judgeStoveNo
									+ ")已存在不是利库炉计划,如需利库,请先撤销炉计划!");
							return cro;
						}
					}
					rs.close();
					ps.close();

					// 算预计成品吨
					double aimOutdiam = Double.parseDouble(plnZgS
							.getAimoutdiameter());
					double aimwallthick = Double.parseDouble(plnZgS
							.getAimwallthick());
					double weightOne = (0.02466 * aimwallthick * (aimOutdiam - aimwallthick)) / 1000;
					double cutHeadEnd = (Double.parseDouble(plnZgS
							.getLenCutend()) + Double.parseDouble(plnZgS
							.getLenCuthead())) / 1000;
					double cpWt = weightOne
							* (Double.parseDouble(plnZgS.getRollLength()) - cutHeadEnd)
							* oldNum;
					// 折算吨
					double reducedWt = (Double.parseDouble(plnZgS
							.getGpreqWeight()) / Double.parseDouble(plnZgS
							.getWeigthS()))
							* cpWt;
					if (countM == 0) {
						heatPlanNo = corePlnZyFurnM.getMaxHeatPlanNo(conn,
								plnZgS.getProPlanId() + "6", plnZgS
										.getProPlanId(), "D");
						// 获取炉计划顺序号
						int gxProSeq = corePlnZyFurnM.getMaxGxPlnSeq(conn,
								plnZgS.getPlineCode(), "D");
						plnZgM.setHeatPlanNo(heatPlanNo);
						plnZgM.setJudgeStoveNo(judgeStoveNo);
						plnZgM.setStoveNo(stoveNo);
						plnZgM.setFlagStoveNo(flagStoveNo);
						plnZgM.setGxProSeq(String.valueOf(gxProSeq));
						plnZgM.setUpperHeatplanId(furnaceno);
						plnZgM.setZyBatchId(plnZgS.getZyBatchId());
						plnZgM.setPlanstatus("00");
						if (proPlanId.contains("DP")) {
							plnZgM.setUpperProcessCode("C");
						} else {
							plnZgM.setUpperProcessCode("B");
						}
						plnZgM.setPlineCode(plnZgS.getPlineCode());
						plnZgM.setPlineName(plnZgS.getPlineName());
						if (!plnZgS.getDecision().equals("A")) {
							// 通过制程判定轧管是否末工序
							if (plnZgS.getWholeBacklogDesc().contains("热处理")) {
								plnZgM.setNextProcessCode("F");
								plnZgM.setIsendgx("0");
							} else if (plnZgS.getWholeBacklogDesc().contains(
									"镦拔扩")) {
								plnZgM.setNextProcessCode("E");
								plnZgM.setIsendgx("0");
							} else if (plnZgS.getWholeBacklogDesc().contains(
									"加工")) {
								plnZgM.setNextProcessCode("G");
								plnZgM.setIsendgx("0");
							} else {
								plnZgM.setNextProcessCode("");
								plnZgM.setIsendgx("1");
							}
						} else {
							plnZgM.setNextProcessCode("");
							plnZgM.setIsendgx("1");
						}
						// 末工序获取去向
						// String gowWhere = ComPlanFurnace.getPlineCodeGroup(
						// conn, plnZgS.getPlineCode(), plnZgS
						// .getProPlanId(), plnZgS.getGxPlanNo());
						// if (gowWhere != null) {
						// // 查询去向
						// String sqlGroup =
						// "SELECT T.STORAGE_NO,T.STORAGE_NAME FROM COM_BASE_PLINE_R_GROUP T WHERE T.PLINE_CODE_M=? AND T.PLINE_NAME_C=?";
						// ps = conn.prepareStatement(sqlGroup);
						// ps.setObject(1, plnZgS.getPlineCode());
						// ps.setObject(2, gowWhere);
						// rs = ps.executeQuery();
						// if (rs.next()) {
						// plnZgM.setGowhereName(rs
						// .getString("STORAGE_NAME"));
						// plnZgM.setGowhereCode(rs
						// .getString("STORAGE_NO"));
						// }
						// rs.close();
						// ps.close();
						// }
						// plnZgM.setGowhereName(gowWhere);
						plnZgM.setIfMatchOdd("2");
						plnZgM.setIfDoHeatPlan("0");
						plnZgM.setProPlanId(plnZgS.getProPlanId());
						plnZgM.setGxPlanNo(plnZgS.getGxPlanNo());
						plnZgM.setCcmProPlanId(proPlanId);
						plnZgM.setCcmGxPlanNo(gxPlanNo);
						plnZgM.setGradecode(gradecode);
						plnZgM.setGradename(gradename);
						plnZgM.setSteelcode(plnZgS.getSteelcode());
						plnZgM.setSteelname(plnZgS.getSteelname());
						// if (!gradename.equals(plnZgS.getGradename())) {
						// // 查询产出物料
						// String[] bomMat =
						// StoveHelper.getQueryMateratBom(conn,
						// gradename, inwlDesc, plnZgS.getDiameterGp(),
						// plnZgS.getProPlanId(), plnZgS.getGxPlanNo());
						// plnZgM.setInwlId(bomMat[0]);
						// plnZgM.setInwlDesc(bomMat[1]);
						// plnZgM.setOutwlId(bomMat[2]);
						// plnZgM.setOutwlDesc(bomMat[3]);
						// plnZgM.setBom(bomMat[4]);
						// }
						// else {
						plnZgM.setInwlId(plnZgS.getInwlId());
						plnZgM.setInwlDesc(plnZgS.getInwlDesc());
						plnZgM.setOutwlId(plnZgS.getOutwlId());
						plnZgM.setOutwlDesc(plnZgS.getOutwlDesc());
						plnZgM.setBom(plnZgS.getBom());
						// }

						plnZgM.setPassCode(plnZgS.getPassCode());
						plnZgM.setPassName(plnZgS.getPassName());
						plnZgM.setPlnInsingleNum(String.valueOf(oldNum));
						plnZgM.setPlnInsingleWt(String.valueOf(actWt));

						plnZgM.setPlnOutsingleNum(String.valueOf(oldNum
								* Integer.parseInt(plnZgS.getOutnumCut())));
						plnZgM.setPlnOutsingleW(String.valueOf(cpWt));
						plnZgM.setReducedWt(String.valueOf(reducedWt));
						plnZgM.setProcessCodeC(plnZgS.getProcessCodeC());
						plnZgM.setProcessDescC(plnZgS.getProcessDescC());
						plnZgM.setOrderSource(plnZgS.getOrderSource());
						plnZgM.setOrderNo(plnZgS.getOrderNo());
						plnZgM.setOrderSeq(plnZgS.getOrderSeq());
						plnZgM.setDeliveryNo(plnZgS.getDeliveryNo());
						plnZgM.setOrdLnDlyPk(plnZgS.getOrdLnDlyPk());
						plnZgM.setReqBlId(plnZgS.getReqBlId());
						plnZgM.setIfExport(plnZgS.getIfExport());
						plnZgM.setPriority(plnZgS.getPriority());
						plnZgM.setPriorityMemo(plnZgS.getPriorityMemo());
						plnZgM.setProMonth(plnZgS.getProMonth());
						plnZgM.setIfLenControl(plnZgS.getIfLenControl());
						plnZgM.setIfMinusSicRoll(plnZgS.getIfMinusSicRoll());
						plnZgM.setProdStandard(plnZgS.getProdStandard());
						plnZgM.setOutputStandard(plnZgS.getOutputStandard());
						plnZgM.setDecision(plnZgS.getDecision());
						plnZgM.setProgyMemo(plnZgS.getProgyMemo());
						plnZgM.setProduccode(plnZgS.getProduccode());
						plnZgM.setProducname(plnZgS.getProducname());
						plnZgM.setModelCode(plnZgS.getModelCode());
						plnZgM.setModelDesc(plnZgS.getModelDesc());
						plnZgM.setOutdiameter(plnZgS.getOutdiameter());
						plnZgM.setWallthick(plnZgS.getWallthick());
						plnZgM.setOutdiameterZg(plnZgS.getOutdiameterZg());
						plnZgM.setWallthickZg(plnZgS.getWallthickZg());
						plnZgM.setAimlength(plnZgS.getAimlength());
						plnZgM.setAimoutdiameter(plnZgS.getAimoutdiameter());
						plnZgM.setAimwallthick(plnZgS.getAimwallthick());
						plnZgM.setThicknessWall(plnZgS.getThicknessWall());
						plnZgM.setLenCutend(plnZgS.getLenCutend());
						plnZgM.setLenCuthead(plnZgS.getLenCuthead());
						plnZgM.setLenRollMax(plnZgS.getLenRollMax());
						plnZgM.setLenRollMin(plnZgS.getLenRollMin());
						plnZgM.setRollLength(plnZgS.getRollLength());
						plnZgM.setLenGpSingle(plnZgS.getLenGpSingle());
						plnZgM.setDiameter(plnZgS.getDiameterGp());
						plnZgM.setWtGpSingle(String.valueOf(weightGpOne));
						plnZgM.setOutnumCut(plnZgS.getOutnumCut());
						plnZgM.setCic(cicLg);
						rs.close();
						ps.close();
						if (plnZgS.getSic() == null
								|| plnZgS.getSic().equals("")) {
							plnZgM.setSic(plnZgS.getSic());
						} else {
							plnZgM.setSic(plnZgS.getSic().split(",")[0]);
						}
						if (plnZgS.getDic() == null
								|| plnZgS.getDic().equals("")) {
							plnZgM.setDic(plnZgS.getDic());
						} else {
							plnZgM.setDic(plnZgS.getDic().split(",")[0]);
						}
						if (plnZgS.getPic() == null
								|| plnZgS.getPic().equals("")) {
							plnZgM.setPic(plnZgS.getPic());
						} else {
							plnZgM.setPic(plnZgS.getPic().split(",")[0]);
						}
						plnZgM.setProDay(dFormat
								.format(dFormat.parse(dateTime)));
						plnZgM.setBalYearMonth(dfMonth.format(dfMonth
								.parse(dateTime)));
						plnZgM.setCreateName(userName);
						plnZgM.setCreateTime(dateTime);
						plnZgM.setCutType(plnZgS.getPassive());
						plnZgM.setIfMonitor(plnZgS.getIfMonitorZg());
						plnZgM.setDiameterMandrel(plnZgS.getDiameterMandrel());
						plnZgM.setPrdcrNo(plnZgS.getPrdcrNo());
						plnZgM.setMonitorPoint(plnZgS.getMonitorPoint());
						plnZgM.setWholeBacklog(plnZgS.getWholeBacklog());
						plnZgM
								.setWholeBacklogDesc(plnZgS
										.getWholeBacklogDesc());
						plnZgM.setCraftFileMan(plnZgS.getCraftFileMan());
						plnZgM.setCraftFileNo(plnZgS.getCraftFileNo());
						plnZgM.setCraftSeq(plnZgS.getCraftSeq());
						plnZgM.setSecCraftFileNo(plnZgS.getSecCraftFileNo());
						plnZgM.setStationRoad(plnZgS.getStationRoad());
						plnZgM.setIndexSeq(plnZgS.getIndexSeq());
						plnZgM.setGptype(gptype);
						plnZgM.setOutsBilletFl("0");// 自炼
						plnZgM.setPlnType("0");
						// 新增轧管虚拟炉计划
						String sqlZgM = EntityTool
								.fillSqlByEntity(
										CoreXmlSqlParsersFactory
												.getSql("FrmPlanMaterialSelection.addPlnZyZgM"),
										plnZgM);
						ps = conn.prepareStatement(sqlZgM);
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改轧管虚拟炉计划量
						String update = CoreXmlSqlParsersFactory
								.getSql("FrmPlanSteelMakMod.updateZgM");
						ps = conn.prepareStatement(update);
						ps.setObject(1, num);
						ps.setObject(2, actWt);
						ps.setObject(3, num
								* Integer.parseInt(plnZgS.getOutnumCut()));
						ps.setObject(4, cpWt);
						ps.setObject(5, reducedWt);
						ps.setObject(6, heatPlanNo);
						ps.executeUpdate();
						ps.close();
					}
					takeWeight = takeWeight - actWt;
					num = num - oldNum;
					// 回写库存下工序炉计划Id
					String sqlUpdate = "UPDATE MAT_GP_M T SET T.HEAT_PLAN_NO_NEXT=? WHERE T.JUDGE_STOVE_NO=? AND T.PRO_PLAN_ID_ZG=? AND T.GX_PLAN_NO_ZG=?";
					ps = conn.prepareStatement(sqlUpdate);
					ps.setObject(1, heatPlanNo);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, plnZgS.getProPlanId());
					ps.setObject(4, plnZgS.getGxPlanNo());
					ps.executeUpdate();
					ps.close();
				}

			} else {
				List<PlnOrderDzSEntityL> plnDz = ComPlanFurnace.getPlanSteOrd(
						conn, proPlanId, "");
				if (plnDz == null) {
					cro.setV_errMsg("未找到锻造订单信息!");
					return cro;
				}
				int numdz = takeCount;
				for (PlnOrderDzSEntityL plnDzS : plnDz) {
					if (takeWeight <= 0) {
						continue;
					}
					double wtIn = 0;// 已生成炉计划原料吨
					int numIn = 0;
					// 查询当前锻造订单是否存在炉计划
					String sql = "SELECT SUM(T.PLN_INSINGLE_NUM) PLN_INSINGLE_NUM,SUM(T.PLN_INSINGLE_WT) PLN_INSINGLE_WT FROM PLN_ZY_DZ_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
					ps = conn.prepareStatement(sql);
					ps.setObject(1, plnDzS.getProPlanId());
					ps.setObject(2, plnDzS.getGxPlanNo());
					rs = ps.executeQuery();
					if (rs.next()) {
						wtIn = rs.getDouble("PLN_INSINGLE_WT");
						numIn = rs.getInt("PLN_INSINGLE_NUM");
					}
					rs.close();
					ps.close();
					// 判断铸造计划支吨是否大于锻造计划支吨
					// if (takeWeight
					// - (Double.parseDouble(plnDzS.getWeigthS()) - wtIn) <
					// 0.00002) {
					// oldWt = takeWeight;
					oldNum = numdz;
					// }
					// else {
					// // oldWt = Double.parseDouble(plnDzS.getWeigthS()) -
					// // wtIn;
					// oldNum = Integer.parseInt(plnDzS.getNumS()) - numIn;
					// // outWt += Double.parseDouble(plnDzS.getWeigthS()) -
					// // wtIn;
					// // outNum += Integer.parseInt(plnDzS.getNumS()) - numIn;
					// }
					if (oldNum <= 0) {
						continue;
					}
					List<MatGpMEntity> matGpMlist = getQureyMatGpML(conn,
							judgeStoveNo, actLenTake, fixLenTake, cutNumGpKc,
							oldNum, "", "");
					if (matGpMlist == null) {
						cro.setV_errMsg("管坯物料已发生改变,请刷新!");
						return cro;
					}
//					int seq = 0;

					String stoveNo = "";
					String flagStoveNo = "";
					String furnaceno = "";
					String inwlId = "";
					String inwlDesc = "";
					String gradename = "";
					String gradecode = "";
					double actWt = 0;
					String matStrat = "";
					String jugdeApplyCode = "";
					String falg = "0";// 是否锭子判定合格余材
					for (MatGpMEntity matGpM : matGpMlist) {
						takeCountK += Integer.parseInt(matGpM.getActCount());
						takeWtK += Double.parseDouble(matGpM.getActWeight());
						actWt += Double.parseDouble(matGpM.getActWeight());
						stoveNo = matGpM.getStoveNo();
						flagStoveNo = matGpM.getFlagStoveNo();
						furnaceno = matGpM.getFurnaceno() + "/"
								+ matGpM.getSequenceno();
						inwlId = matGpM.getMaterialNo();
						inwlDesc = matGpM.getMaterialName();
						gradename = matGpM.getGradename();
						gradecode = matGpM.getGradecode();
						matStrat = matGpM.getMatStatus();
						jugdeApplyCode = matGpM.getJugdeApplyCode();
						// if (!matGpM.getProductFlag().equals("801401")) {
						// cro.setV_errMsg("材料类别不是在制品,不能利库!");
						// return cro;
						// }
						// if (!matGpM.getMatStatus().equals("80150101")
						// || !matGpM.getMatStatus().equals("80150102")) {
						// cro.setV_errMsg("材料状态不是产出待判或产出已判,不能利库");
						// return cro;
						// }
						if (matGpM.getMatStatus().equals("80150103")) {
							cro.setV_errMsg("材料状态是已编计划,不能利库");
							return cro;
						}
						if (!matGpM.getQcmStaus().equals("00")) {
							cro.setV_errMsg("交库状态不是合格状态,不能利库!");
							return cro;
						}
						if (matGpM.getProPlanIdZg() != "") {
							cro.setV_errMsg("你选择的管坯物料已挂合同,不能利库。如需利库,请先脱单!");
							return cro;
						}

						if (matGpM.getJudgeResultCode().equals("40740701")
								&& matGpM.getProPlanIdZg().equals("")) {
							falg = "1";
							// 修改坯子库存信息
							String sqlUpdateMat = CoreXmlSqlParsersFactory
									.getSql("FrmPlanSteelMakMod.UpdateMatGpOld");
							sqlUpdateMat += "WHERE T.MAT_NO = ?";
							ps = conn.prepareStatement(sqlUpdateMat);
							ps.setObject(1, plnDzS.getProPlanId());
							ps.setObject(2, plnDzS.getGxPlanNo());
							ps.setObject(3, "");
							ps.setObject(4, "");
							ps.setObject(5, "");
							ps.setObject(6, "");
							ps.setObject(7, "");
							ps.setObject(8, "");
							ps.setObject(9, "2");
							ps.setObject(10, proPlanId);
							ps.setObject(11, gxPlanNo);
							ps.setObject(12, "80150103");
							ps.setObject(13, "1");
							ps.setObject(14, matGpM.getMatNo());
							ps.executeUpdate();
							ps.close();
						} else {
							// 修改坯子库存信息
							String sqlUpdateMat = CoreXmlSqlParsersFactory
									.getSql("FrmPlanSteelMakMod.UpdateMatGpOld");
							sqlUpdateMat += " ,T.JUGDE_APPLY_FLAG='0',T.JUGDE_APPLY_CODE='',T.JUGDE_APPLY_TIME='',T.JUDGE_RESULT_CODE='',T.JUDGE_RESULT_TIME='',T.JUDGE_MAKER=''";
							sqlUpdateMat += "WHERE T.MAT_NO = ?";
							ps = conn.prepareStatement(sqlUpdateMat);
							ps.setObject(1, plnDzS.getProPlanId());
							ps.setObject(2, plnDzS.getGxPlanNo());
							ps.setObject(3, "");
							ps.setObject(4, "");
							ps.setObject(5, "");
							ps.setObject(6, "");
							ps.setObject(7, "");
							ps.setObject(8, "");
							ps.setObject(9, "2");
							ps.setObject(10, proPlanId);
							ps.setObject(11, gxPlanNo);
							// if (!matGpM.getMatStatus().equals("80150101")) {
							ps.setObject(12, "80150101");
							// } else {
							// ps.setObject(12, matGpM.getMatStatus());
							// }
							ps.setObject(13, "1");
							ps.setObject(14, matGpM.getMatNo());
							ps.executeUpdate();
							ps.close();
						}
						seq += 1;
						String resumeSeq = dateFormat.format(now)
								+ df.format(seq);
						// 生成利库日志
						MatGpTrackEntity matGpTrack = new MatGpTrackEntity();
						matGpTrack.setResumeSeq(resumeSeq);
						matGpTrack.setEventId("900102");
						matGpTrack.setEventDesc("炼钢利库");
						matGpTrack
								.setFormName("com.steering.pss.plnsaleord.planStove.FrmPlanSteelMakMod");
						matGpTrack.setCallFuncCoding("doAddLibraryLg");
						matGpTrack
								.setKeyvalue1("List<MatGpMEntity> matGpMlist");
						matGpTrack.setKeyvalue1Desc("物料主表关键字实体类集合");
						matGpTrack.setKeyvalue2("String userName");
						matGpTrack.setKeyvalue2Desc2("操作人");
						matGpTrack.setKeyvalue3("doAddCastLg");
						matGpTrack.setKeyvalue3Desc3("炼钢利铸造订单");
						matGpTrack.setMatNo(matGpM.getMatNo());
						matGpTrack.setProductFlag(matGpM.getProductFlag());
						matGpTrack.setMatStatus(matGpM.getMatStatus());
						matGpTrack.setStoveNo(matGpM.getStoveNo());
						matGpTrack.setFlagStoveNo(matGpM.getFlagStoveNo());
						matGpTrack.setJudgeStoveNo(matGpM.getJudgeStoveNo());
						matGpTrack.setBatchNo("");
						matGpTrack.setGroudNo(matGpM.getGroudNo());
						matGpTrack.setCastNo(matGpM.getCastNo());
						matGpTrack.setCcmid(matGpM.getCcmid());
						matGpTrack.setGradecode(matGpM.getGradecode());
						matGpTrack.setGradename(matGpM.getGradename());
						matGpTrack.setStdCode(matGpM.getStdCode());
						matGpTrack.setStdName(matGpM.getStdName());
						matGpTrack.setActDimater(matGpM.getDimater().replace(
								"t", ""));
						matGpTrack.setActHeight(matGpM.getHeight());
						matGpTrack.setActLen(String.valueOf(actLenTake));
						matGpTrack.setActFixLen(matGpM.getFixLen());
						matGpTrack.setActFixNum(matGpM.getFixNum());
						matGpTrack.setActCount("1");
						matGpTrack.setActWeight(matGpM.getActWeight());
						matGpTrack.setActTheoryWeight(matGpM
								.getActTheoryWeight());
						matGpTrack.setOldOrderNo(matGpM.getOldOrderNo());
						matGpTrack.setOldOrderSeq(matGpM.getOldOrderSeq());
						matGpTrack.setOldOrderSeqNo(matGpM.getOldOrderSeqNo());
						matGpTrack.setMemo("");
						matGpTrack.setCreateName(userName);
						matGpTrack.setCreateTime(dateTime);
						matGpTrack.setOldJudgeStoveNo(matGpM
								.getOldJudgeStoveNo());
						matGpTrack.setOldBatchNo("");
						matGpTrack.setOldGroudNo(matGpM.getOldGroudNo());
						matGpTrack.setOldGradecode(matGpM.getOldGradecode());
						matGpTrack.setOldGradename(matGpM.getOldGradename());
						matGpTrack.setOldStdCode(matGpM.getOldStdCode());
						matGpTrack.setOldStdName(matGpM.getOldStdName());
						matGpTrack.setOldStoveNo(matGpM.getOldStoveNo());
						String sqlAddTrack = EntityTool
								.fillSqlByEntity(
										CoreXmlSqlParsersFactory
												.getSql("FrmPlanMaterialSelection.addMatGpTrack"),
										matGpTrack);
						ps = conn.prepareStatement(sqlAddTrack);
						ps.executeUpdate();
						ps.close();
					}
					if (falg.equals("0")) {
						// 修改申请判定表有效标志
						String updateApply = "UPDATE QCM_GP_JUDGE_APPLY T SET T.IS_FLAG='1' WHERE T.JUDGE_STOVE_NO=? AND T.JUGDE_APPLY_CODE||T.JUGDE_APPLY_SQE=? AND T.MULTIPLE_NUM=?";
						ps = conn.prepareStatement(updateApply);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, jugdeApplyCode);
						ps.setObject(3, cutNumGpKc);
						ps.executeUpdate();
						ps.close();
					}
					int countdz = 0;
					String heatPlanNo = "";
					// 判定是否已生成虚拟炉计划
					String sqlQuery = "SELECT COUNT(*) COUNT,T.HEAT_PLAN_NO FROM PLN_ZY_DZ_M T "
							+ "WHERE T.JUDGE_STOVE_NO=? AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? AND T.IF_MATCH_ODD='1' AND T.PLANSTATUS='00'  GROUP BY T.HEAT_PLAN_NO";
					ps = conn.prepareStatement(sqlQuery);
					ps.setObject(1, judgeStoveNo);
					ps.setObject(2, plnDzS.getProPlanId());
					ps.setObject(3, plnDzS.getGxPlanNo());
					rs = ps.executeQuery();
					if (rs.next()) {
						countdz++;
						heatPlanNo = rs.getString("HEAT_PLAN_NO");
					}
					rs.close();
					ps.close();
					if (heatPlanNo.equals("")) {
						PlnZyDzMEntity plnDzM = new PlnZyDzMEntity();
						plnDzM.setStoveNo(stoveNo);
						plnDzM.setJudgeStoveNo(judgeStoveNo);
						plnDzM.setFlagStoveNo(flagStoveNo);
						plnDzM.setUpperHeatplanId(furnaceno);
						plnDzM.setPlanstatus("00");
						plnDzM.setUpperProcessCode("B");
						plnDzM.setPlineCode(plnDzS.getPlineCode());
						plnDzM.setPlineName(plnDzS.getPlineName());
						plnDzM.setNextProcessCode("D");
						plnDzM.setIsendgx("0");
						plnDzM.setGowhereCode("");
						plnDzM.setGowhereName("快锻");

						sql = "SELECT T.LEN_GP_SINGLE,T.STEELCODE,T.STEELNAME FROM PLN_ORDER_ZG_S T WHERE T.GPREQ_ID = ? AND ROWNUM = 1";
						ps = conn.prepareStatement(sql);
						ps.setObject(1, proPlanId);
						rs = ps.executeQuery();
						if (rs.next()) {
							plnDzM.setLenGpSingleOut(rs
									.getString("LEN_GP_SINGLE"));
							plnDzM.setSteelcode(rs.getString("STEELCODE"));
							plnDzM.setSteelname(rs.getString("STEELNAME"));
						}
						rs.close();
						ps.close();
						plnDzM.setIfMatchOdd("2");
						plnDzM.setIfDoHeatPlan("0");
						plnDzM.setProPlanId(plnDzS.getProPlanId());
						plnDzM.setGxPlanNo(plnDzS.getGxPlanNo());
						plnDzM.setCcmProPlanId(proPlanId);
						plnDzM.setCcmGxPlanNo(gxPlanNo);
						plnDzM.setCastingtype("2");
						plnDzM.setGradecode(plnDzS.getGradecode());
						plnDzM.setGradename(plnDzS.getGradename());
						plnDzM.setWlidIn(plnDzS.getWlidIn());
						plnDzM.setWldescIn(plnDzS.getWldescIn());
						plnDzM.setOutwlId(plnDzS.getOutwlId());
						plnDzM.setOutwlDesc(plnDzS.getOutwlDesc());
						plnDzM.setDiameterIn(dimaterKc);
						plnDzM.setLengthIn("1");
						plnDzM.setWtOneIn(plnDzS.getWtOneIn());
						plnDzM.setLenGpSingle("1");
						plnDzM.setWtGpSingle(plnDzS.getWtOneIn());
						plnDzM.setCutNumGp(String.valueOf(cutNumGp));
						plnDzM.setPlnIndoubleNum(String.valueOf(oldNum));
						plnDzM.setPlnIndoubleWt(String.valueOf(actWt));
						plnDzM.setPlnInsingleNum(String.valueOf(oldNum));
						plnDzM.setPlnInsingleWt(String.valueOf(actWt));
						plnDzM.setCutNumOut("1");
						plnDzM.setDiameterOut(plnDzS.getDiameterOut());
						plnDzM.setLengthOut(plnDzS.getLengthOut());
						plnDzM.setOutWtOne(plnDzS.getOutWtOne());
						plnDzM.setPlnOutdoubleNum(String.valueOf(numdz));
						plnDzM.setPlnOutdoubleW(String.valueOf(actWt));
						plnDzM.setPlnOutsingleNum(String.valueOf(numdz));
						plnDzM.setPlnOutsingleW(String.valueOf(actWt));
						plnDzM.setCic(plnDzS.getCic());
						plnDzM.setSic(plnDzS.getSic());
						plnDzM.setPic(plnDzS.getPic());
						plnDzM.setDic(plnDzS.getDic());
						plnDzM.setProDay(dfMonth
								.format(dfMonth.parse(dateTime)));
						plnDzM.setBalYearMonth(dfMonth.format(dfMonth
								.parse(dateTime)));
						plnDzM.setCreateName(userName);
						plnDzM.setCreateTime(dateTime);

						// 存在相同判定炉号的锻造虚拟炉计划
						heatPlanNo = corePlnZyFurnM.getMaxHeatPlanNo(conn,
								plnDzM.getProPlanId() + "6", plnDzM
										.getProPlanId(), "C");
						// 获取炉计划顺序号
						int gxProSeq = corePlnZyFurnM.getMaxGxPlnSeq(conn,
								plnDzM.getPlineCode(), "C");
						plnDzM.setHeatPlanNo(heatPlanNo);
						plnDzM.setGxProSeq(String.valueOf(gxProSeq));
						String addPlnGxDz = EntityTool.fillSqlByEntity(
								CoreXmlSqlParsersFactory
										.getSql("ComPlanFurnace.addPlnZyDzM"),
								plnDzM);
						ps = conn.prepareStatement(addPlnGxDz);
						ps.executeUpdate();
						ps.close();
					}
					if (falg.equals("1")) {
						int countC = 0;
						String heatPlanSqe = "";
						// 判定是否存在相同坯长，倍尺，单倍坯长，判定炉号，判定单号，同炉计划ID数据
						String sqlC = "SELECT COUNT(*) COUNT1,T.HEAT_PLAN_SQE FROM PLN_ZY_DZ_C T WHERE "
								+ "T.HEAT_PLAN_NO=? AND T.JUGDE_APPLY_CODE=? AND T.JUDGE_STOVE_NO=? "
								+ "AND T.CUT_NUM_GP=? AND T.LENGTH_M=? AND T.LEN_GP_SINGLE=? GROUP BY T.HEAT_PLAN_SQE";
						ps = conn.prepareStatement(sqlC);
						ps.setObject(1, heatPlanNo);
						ps.setObject(2, jugdeApplyCode);
						ps.setObject(3, judgeStoveNo);
						ps.setObject(4, cutNumGp);
						ps.setObject(5, actLenTake);
						ps.setObject(6, fixLenTake);
						rs = ps.executeQuery();
						if (rs.next()) {
							countC = rs.getInt("COUNT1");
							heatPlanSqe = rs.getString("HEAT_PLAN_SQE");
						}
						rs.close();
						ps.close();
						if (countC == 0) {
							PlnZyDzCEntity plnDzC = new PlnZyDzCEntity();
							plnDzC.setHeatPlanNo(heatPlanNo);
							plnDzC.setJugdeApplyCode(jugdeApplyCode);
							plnDzC.setStoveNo(stoveNo);
							plnDzC.setJudgeStoveNo(judgeStoveNo);
							plnDzC.setFlagStoveNo(flagStoveNo);
							// plnDzC.setZyBatchId(plnDzS.getZyBatchId());
							plnDzC.setPlineCode("C005");
							plnDzC.setPlineName("二炼快锻");
							plnDzC.setProPlanId(plnDzS.getProPlanId());
							plnDzC.setGxPlanNo(plnDzS.getGxPlanNo());
							plnDzC.setDiameter(dimaterKc);
							plnDzC.setLenGpSingle("1");
							plnDzC.setWtGpSingle(plnDzS.getWtOneIn());
							plnDzC.setCutNumGp(String.valueOf(cutNumGp));
							plnDzC.setLengthM("1");
							plnDzC.setWeightM(plnDzS.getWtOneIn());
							plnDzC.setPlanIndoubleNum(String.valueOf(oldNum));
							plnDzC.setPlanIndoubleWt(String.valueOf(actWt));
							plnDzC.setCreateName(userName);
							plnDzC.setCreateTime(dateTime);
							// 获取上料行ID
							String heatPlanSeq = corePlnZyFurnM.getHeatPlanSeq(
									conn, heatPlanNo, "C");
							plnDzC.setHeatPlanSqe(heatPlanSeq);
							// 新增
							String addPlnGxDzC = EntityTool
									.fillSqlByEntity(
											CoreXmlSqlParsersFactory
													.getSql("ComPlanFurnace.addPlnZyDzC"),
											plnDzC);
							ps = conn.prepareStatement(addPlnGxDzC);
							ps.executeUpdate();
							ps.close();
						} else {
							// 修改炉计划实表
							String updateC = "UPDATE PLN_ZY_DZ_C T SET T.PLAN_INDOUBLE_NUM=T.PLAN_INDOUBLE_NUM+TO_NUMBER(?),"
									+ "T.PLAN_INDOUBLE_WT=T.PLAN_INDOUBLE_WT+TO_NUMBER(?) ";
							updateC += " WHERE T.HEAT_PLAN_NO=? AND T.HEAT_PLAN_SQE=?";
							ps = conn.prepareStatement(updateC);
							ps.setObject(1, oldNum);
							ps.setObject(2, actWt);
							ps.setObject(3, heatPlanNo);
							ps.setObject(4, heatPlanSqe);
							ps.executeUpdate();
							ps.close();
						}
					}
					if (countdz > 0) {
						// 修改炉计划主表量
						String sqlUpdate = CoreXmlSqlParsersFactory
								.getSql("FrmPlanSteelMakMod.updatePlnDzM");
						ps = conn.prepareStatement(sqlUpdate);
						ps.setObject(1, heatPlanNo);
						ps.setObject(2, heatPlanNo);
						ps.executeUpdate();
						ps.close();
					}
					takeWeight = takeWeight - actWt;
					numdz = numdz - oldNum;
					// 回写库存下工序炉计划Id
					String sqlUpdate = "UPDATE MAT_GP_M T SET T.HEAT_PLAN_NO_NEXT=? WHERE T.JUDGE_STOVE_NO=? AND T.PRO_PLAN_ID_ZG=? AND T.GX_PLAN_NO_ZG=?";
					ps = conn.prepareStatement(sqlUpdate);
					ps.setObject(1, heatPlanNo);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, plnDzS.getProPlanId());
					ps.setObject(4, plnDzS.getGxPlanNo());
					ps.executeUpdate();
					ps.close();
				}
			}
			// 修改铸造订单利库量
			String updateZz = "UPDATE PLN_ORDER_CAST_S T SET T.PLAN_WT_SOLD=NVL(T.PLAN_WT_SOLD,0)+((?/T.CUT_NUM_GP)*NVL(T.WT_CAST_ONE, 0)),"
					+ "T.PLAN_NUM_SOLD=NVL(T.PLAN_NUM_SOLD,0)+(?/T.CUT_NUM_GP),T.UPDATE_NAME=?,T.UPDATE_TIME=SYSDATE WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
			ps = conn.prepareStatement(updateZz);
			ps.setObject(1, takeCountK*cutNumGp);
			ps.setObject(2, takeCountK*cutNumGp);
			ps.setObject(3, userName + "-[利库]");
			ps.setObject(4, proPlanId);
			ps.setObject(5, gxPlanNo);
			ps.executeUpdate();
			ps.close();
			cro.setV_errMsg("利库成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 炼钢坯子利库锻造
	 * 
	 * @param jslist
	 * @param userName
	 *            利库人
	 * @throws ParseException
	 */
	public CoreReturnObject doAddDzLg(ArrayList<ArrayList<String>> jslist,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat dfMonth = new SimpleDateFormat("yyyyMM");

			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String proPlanId = list.get(0);
				String gxPlanNo = list.get(1);
				String judgeStoveNo = list.get(2);
				double actLenTake = Double.parseDouble(list.get(3));// 倍尺坯长
				double fixLenTake = Double.parseDouble(list.get(4));// 单倍
				int cutNumGp = Integer.parseInt(list.get(5));
				int takeCount = Integer.parseInt(list.get(6));// 利库支
				String dimaterKc = list.get(7);
				// String gptype = list.get(8);
				// double takeWeight = Double.parseDouble(list.get(10));// 利库吨

				// int num = takeCount * cutNumGp;
				int takeCountK = 0;// 可利库支
				double takeWtK = 0;// 可利库吨
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyyMMddHHmmss");
				DecimalFormat df = new DecimalFormat("0000000000");
				List<PlnOrderDzSEntityL> plnDz = ComPlanFurnace.getPlanSteOrd(
						conn, proPlanId, gxPlanNo);
				if (plnDz == null) {
					cro.setV_errMsg("未找到锻造订单信息!");
					return cro;
				}
				for (PlnOrderDzSEntityL plnDzS : plnDz) {
					List<MatGpMEntity> matGpMlist = getQureyMatGpML(conn,
							judgeStoveNo, actLenTake, fixLenTake, cutNumGp,
							takeCount, "", "");
					if (matGpMlist == null) {
						cro.setV_errMsg("管坯物料已发生改变,请刷新!");
						return cro;
					}
					int seq = 0;
					String stoveNo = "";
					String flagStoveNo = "";
					String furnaceno = "";
					String inwlId = "";
					String inwlDesc = "";
					String gradename = "";
					String gradecode = "";
					double actWt = 0;
					String matStrat = "";
					String jugdeApplyCode = "";
					String proPlanIdZz = "";
					String gxPlanNoZz = "";
					String falg = "0";
					for (MatGpMEntity matGpM : matGpMlist) {
						takeCountK += Integer.parseInt(matGpM.getActCount());
						takeWtK += Double.parseDouble(matGpM.getActWeight());
						actWt += Double.parseDouble(matGpM.getActWeight());
						stoveNo = matGpM.getStoveNo();
						flagStoveNo = matGpM.getFlagStoveNo();
						furnaceno = matGpM.getFurnaceno() + "/"
								+ matGpM.getSequenceno();
						inwlId = matGpM.getMaterialNo();
						inwlDesc = matGpM.getMaterialName();
						gradename = matGpM.getGradename();
						gradecode = matGpM.getGradecode();
						matStrat = matGpM.getMatStatus();
						jugdeApplyCode = matGpM.getJugdeApplyCode();
						proPlanIdZz = matGpM.getProPlanId();
						gxPlanNoZz = matGpM.getGxPlanNo();
						// if (!matGpM.getProductFlag().equals("801401")) {
						// cro.setV_errMsg("材料类别不是在制品,不能利库!");
						// return cro;
						// }
						if (matGpM.getMatStatus().equals("80150103")) {
							cro.setV_errMsg("材料状态是已编计划,不能利库");
							return cro;
						}
						if (!matGpM.getQcmStaus().equals("00")) {
							cro.setV_errMsg("交库状态不是合格状态,不能利库!");
							return cro;
						}
						if (matGpM.getProPlanIdZg() != "") {
							cro.setV_errMsg("你选择的管坯物料已挂合同,不能利库。如需利库,请先脱单!");
							return cro;
						}
						if (matGpM.getJudgeResultCode().equals("40740701")
								&& matGpM.getProPlanIdZg().equals("")) {
							falg = "1";
							// 修改坯子库存信息
							String sqlUpdateMat = CoreXmlSqlParsersFactory
									.getSql("FrmPlanSteelMakMod.UpdateMatGpOld");
							sqlUpdateMat += "WHERE T.MAT_NO = ?";
							ps = conn.prepareStatement(sqlUpdateMat);
							ps.setObject(1, plnDzS.getProPlanId());
							ps.setObject(2, plnDzS.getGxPlanNo());
							ps.setObject(3, "");
							ps.setObject(4, "");
							ps.setObject(5, "");
							ps.setObject(6, "");
							ps.setObject(7, "");
							ps.setObject(8, "");
							ps.setObject(9, "2");
							ps.setObject(10, proPlanId);
							ps.setObject(11, gxPlanNo);
							ps.setObject(12, "80150103");
							ps.setObject(13, "1");
							ps.setObject(14, matGpM.getMatNo());
							ps.executeUpdate();
							ps.close();
						} else {
							// 修改坯子库存信息
							String sqlUpdateMat = CoreXmlSqlParsersFactory
									.getSql("FrmPlanSteelMakMod.UpdateMatGpOld");
							sqlUpdateMat += " ,T.JUGDE_APPLY_FLAG='0',T.JUGDE_APPLY_CODE='',T.JUGDE_APPLY_TIME='',T.JUDGE_RESULT_CODE='',T.JUDGE_RESULT_TIME='',T.JUDGE_MAKER=''";
							sqlUpdateMat += "WHERE T.MAT_NO = ?";
							ps = conn.prepareStatement(sqlUpdateMat);
							ps.setObject(1, plnDzS.getProPlanId());
							ps.setObject(2, plnDzS.getGxPlanNo());
							ps.setObject(3, "");
							ps.setObject(4, "");
							ps.setObject(5, "");
							ps.setObject(6, "");
							ps.setObject(7, "");
							ps.setObject(8, "");
							ps.setObject(9, "2");
							ps.setObject(10, proPlanId);
							ps.setObject(11, gxPlanNo);
							// if (!matGpM.getMatStatus().equals("80150101")) {
							ps.setObject(12, "80150101");
							// } else {
							// ps.setObject(12, matGpM.getMatStatus());
							// }
							ps.setObject(13, "1");

							ps.setObject(14, matGpM.getMatNo());
							ps.executeUpdate();
							ps.close();
						}
						seq += 1;
						String resumeSeq = dateFormat.format(now)
								+ df.format(seq);
						// 生成利库日志
						MatGpTrackEntity matGpTrack = new MatGpTrackEntity();
						matGpTrack.setResumeSeq(resumeSeq);
						matGpTrack.setEventId("900102");
						matGpTrack.setEventDesc("炼钢利库");
						matGpTrack
								.setFormName("com.steering.pss.plnsaleord.planStove.FrmPlanSteelMakMod");
						matGpTrack.setCallFuncCoding("doAddDzLg");
						matGpTrack
								.setKeyvalue1("List<MatGpMEntity> matGpMlist");
						matGpTrack.setKeyvalue1Desc("物料主表关键字实体类集合");
						matGpTrack.setKeyvalue2("String userName");
						matGpTrack.setKeyvalue2Desc2("操作人");
						matGpTrack.setKeyvalue3("doAddDzLg");
						matGpTrack.setKeyvalue3Desc3("炼钢利铸造订单");
						matGpTrack.setMatNo(matGpM.getMatNo());
						matGpTrack.setProductFlag(matGpM.getProductFlag());
						matGpTrack.setMatStatus(matGpM.getMatStatus());
						matGpTrack.setStoveNo(matGpM.getStoveNo());
						matGpTrack.setFlagStoveNo(matGpM.getFlagStoveNo());
						matGpTrack.setJudgeStoveNo(matGpM.getJudgeStoveNo());
						matGpTrack.setBatchNo("");
						matGpTrack.setGroudNo(matGpM.getGroudNo());
						matGpTrack.setCastNo(matGpM.getCastNo());
						matGpTrack.setCcmid(matGpM.getCcmid());
						matGpTrack.setGradecode(matGpM.getGradecode());
						matGpTrack.setGradename(matGpM.getGradename());
						matGpTrack.setStdCode(matGpM.getStdCode());
						matGpTrack.setStdName(matGpM.getStdName());
						matGpTrack.setActDimater(matGpM.getDimater().replace(
								"t", ""));
						matGpTrack.setActHeight(matGpM.getHeight());
						matGpTrack.setActLen(String.valueOf(actLenTake));
						matGpTrack.setActFixLen(matGpM.getFixLen());
						matGpTrack.setActFixNum(matGpM.getFixNum());
						matGpTrack.setActCount("1");
						matGpTrack.setActWeight(matGpM.getActWeight());
						matGpTrack.setActTheoryWeight(matGpM
								.getActTheoryWeight());
						matGpTrack.setOldOrderNo(matGpM.getOldOrderNo());
						matGpTrack.setOldOrderSeq(matGpM.getOldOrderSeq());
						matGpTrack.setOldOrderSeqNo(matGpM.getOldOrderSeqNo());
						matGpTrack.setMemo("");
						matGpTrack.setCreateName(userName);
						matGpTrack.setCreateTime(dateTime);
						matGpTrack.setOldJudgeStoveNo(matGpM
								.getOldJudgeStoveNo());
						matGpTrack.setOldBatchNo("");
						matGpTrack.setOldGroudNo(matGpM.getOldGroudNo());
						matGpTrack.setOldGradecode(matGpM.getOldGradecode());
						matGpTrack.setOldGradename(matGpM.getOldGradename());
						matGpTrack.setOldStdCode(matGpM.getOldStdCode());
						matGpTrack.setOldStdName(matGpM.getOldStdName());
						matGpTrack.setOldStoveNo(matGpM.getOldStoveNo());
						String sqlAddTrack = EntityTool
								.fillSqlByEntity(
										CoreXmlSqlParsersFactory
												.getSql("FrmPlanMaterialSelection.addMatGpTrack"),
										matGpTrack);
						ps = conn.prepareStatement(sqlAddTrack);
						ps.executeUpdate();
						ps.close();
					}

					if (falg.equals("0")) {
						// 修改申请判定表有效标志
						String updateApply = "UPDATE QCM_GP_JUDGE_APPLY T SET T.IS_FLAG='1' WHERE T.JUDGE_STOVE_NO=? AND T.JUGDE_APPLY_CODE||T.JUGDE_APPLY_SQE=? AND T.MULTIPLE_NUM=?";
						ps = conn.prepareStatement(updateApply);
						ps.setObject(1, judgeStoveNo);
						ps.setObject(2, jugdeApplyCode);
						ps.setObject(3, cutNumGp);
						ps.executeUpdate();
						ps.close();
					}
					int countdz = 0;
					String heatPlanNo = "";
					// 判定是否已生成虚拟炉计划
					String sqlQuery = "SELECT COUNT(*) COUNT,T.HEAT_PLAN_NO FROM PLN_ZY_DZ_M T "
							+ "WHERE T.JUDGE_STOVE_NO=? AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? AND T.IF_MATCH_ODD='2' AND T.PLANSTATUS='00' GROUP BY T.HEAT_PLAN_NO";
					ps = conn.prepareStatement(sqlQuery);
					ps.setObject(1, judgeStoveNo);
					ps.setObject(2, plnDzS.getProPlanId());
					ps.setObject(3, plnDzS.getGxPlanNo());
					rs = ps.executeQuery();
					if (rs.next()) {
						countdz++;
						heatPlanNo = rs.getString("HEAT_PLAN_NO");
					}
					rs.close();
					ps.close();
					if (heatPlanNo.equals("")) {
						PlnZyDzMEntity plnDzM = new PlnZyDzMEntity();
						plnDzM.setStoveNo(stoveNo);
						plnDzM.setJudgeStoveNo(judgeStoveNo);
						plnDzM.setFlagStoveNo(flagStoveNo);
						plnDzM.setUpperHeatplanId(furnaceno);
						plnDzM.setPlanstatus("00");
						plnDzM.setUpperProcessCode("B");
						plnDzM.setPlineCode(plnDzS.getPlineCode());
						plnDzM.setPlineName(plnDzS.getPlineName());
						plnDzM.setNextProcessCode("D");
						plnDzM.setIsendgx("0");
						plnDzM.setGowhereCode("");
						plnDzM.setGowhereName("快锻");

						String sql = "SELECT T.LEN_GP_SINGLE,T.STEELCODE,T.STEELNAME FROM PLN_ORDER_ZG_S T WHERE T.GPREQ_ID = ? AND ROWNUM = 1";
						ps = conn.prepareStatement(sql);
						ps.setObject(1, proPlanId);
						rs = ps.executeQuery();
						if (rs.next()) {
							plnDzM.setLenGpSingleOut(rs
									.getString("LEN_GP_SINGLE"));
							plnDzM.setSteelcode(rs.getString("STEELCODE"));
							plnDzM.setSteelname(rs.getString("STEELNAME"));
						}
						rs.close();
						ps.close();
						plnDzM.setIfMatchOdd("2");
						plnDzM.setIfDoHeatPlan("0");
						plnDzM.setProPlanId(plnDzS.getProPlanId());
						plnDzM.setGxPlanNo(plnDzS.getGxPlanNo());
						plnDzM.setCcmProPlanId(proPlanIdZz);
						plnDzM.setCcmGxPlanNo(gxPlanNoZz);
						plnDzM.setCastingtype("2");
						plnDzM.setGradecode(plnDzS.getGradecode());
						plnDzM.setGradename(plnDzS.getGradename());
						plnDzM.setWlidIn(plnDzS.getWlidIn());
						plnDzM.setWldescIn(plnDzS.getWldescIn());
						plnDzM.setOutwlId(plnDzS.getOutwlId());
						plnDzM.setOutwlDesc(plnDzS.getOutwlDesc());
						plnDzM.setDiameterIn(dimaterKc);
						plnDzM.setLengthIn("1");
						plnDzM.setWtOneIn(plnDzS.getWtOneIn());
						plnDzM.setLenGpSingle("1");
						plnDzM.setWtGpSingle(plnDzS.getWtOneIn());
						plnDzM.setCutNumGp(String.valueOf(cutNumGp));
						plnDzM.setPlnIndoubleNum(String.valueOf(takeCount));
						plnDzM.setPlnIndoubleWt(String.valueOf(actWt));
						plnDzM.setPlnInsingleNum(String.valueOf(takeCount));
						plnDzM.setPlnInsingleWt(String.valueOf(actWt));
						plnDzM.setCutNumOut("1");
						plnDzM.setDiameterOut(plnDzS.getDiameterOut());
						plnDzM.setLengthOut(plnDzS.getLengthOut());
						plnDzM.setOutWtOne(plnDzS.getOutWtOne());
						plnDzM.setPlnOutdoubleNum(String.valueOf(takeCount));
						plnDzM.setPlnOutdoubleW(String.valueOf(actWt));
						plnDzM.setPlnOutsingleNum(String.valueOf(takeCount));
						plnDzM.setPlnOutsingleW(String.valueOf(actWt));
						plnDzM.setCic(plnDzS.getCic());
						plnDzM.setSic(plnDzS.getSic());
						plnDzM.setPic(plnDzS.getPic());
						plnDzM.setDic(plnDzS.getDic());
						plnDzM.setProDay(dFormat
								.format(dFormat.parse(dateTime)));
						plnDzM.setBalYearMonth(dfMonth.format(dfMonth
								.parse(dateTime)));
						plnDzM.setCreateName(userName);
						plnDzM.setCreateTime(dateTime);

						// 存在相同判定炉号的锻造虚拟炉计划
						heatPlanNo = corePlnZyFurnM.getMaxHeatPlanNo(conn,
								plnDzM.getProPlanId() + "6", plnDzM
										.getProPlanId(), "C");
						// 获取炉计划顺序号
						int gxProSeq = corePlnZyFurnM.getMaxGxPlnSeq(conn,
								plnDzM.getPlineCode(), "C");
						plnDzM.setHeatPlanNo(heatPlanNo);
						plnDzM.setGxProSeq(String.valueOf(gxProSeq));
						String addPlnGxDz = EntityTool.fillSqlByEntity(
								CoreXmlSqlParsersFactory
										.getSql("ComPlanFurnace.addPlnZyDzM"),
								plnDzM);
						ps = conn.prepareStatement(addPlnGxDz);
						ps.executeUpdate();
						ps.close();
					}
					if (falg.equals("1")) {
						int countC = 0;
						String heatPlanSqe = "";
						// 判定是否存在相同坯长，倍尺，单倍坯长，判定炉号，判定单号，同炉计划ID数据
						String sqlC = "SELECT COUNT(*) COUNT1,T.HEAT_PLAN_SQE FROM PLN_ZY_DZ_C T WHERE "
								+ "T.HEAT_PLAN_NO=? AND T.JUGDE_APPLY_CODE=? AND T.JUDGE_STOVE_NO=? "
								+ "AND T.CUT_NUM_GP=? AND T.LENGTH_M=? AND T.LEN_GP_SINGLE=? GROUP BY T.HEAT_PLAN_SQE";
						ps = conn.prepareStatement(sqlC);
						ps.setObject(1, heatPlanNo);
						ps.setObject(2, jugdeApplyCode);
						ps.setObject(3, judgeStoveNo);
						ps.setObject(4, cutNumGp);
						ps.setObject(5, actLenTake);
						ps.setObject(6, fixLenTake);
						rs = ps.executeQuery();
						if (rs.next()) {
							countC = rs.getInt("COUNT1");
							heatPlanSqe = rs.getString("HEAT_PLAN_SQE");
						}
						rs.close();
						ps.close();
						if (countC == 0) {
							PlnZyDzCEntity plnDzC = new PlnZyDzCEntity();
							plnDzC.setHeatPlanNo(heatPlanNo);
							plnDzC.setJugdeApplyCode(jugdeApplyCode);
							plnDzC.setStoveNo(stoveNo);
							plnDzC.setJudgeStoveNo(judgeStoveNo);
							plnDzC.setFlagStoveNo(flagStoveNo);
							// plnDzC.setZyBatchId(plnDzS.getZyBatchId());
							plnDzC.setPlineCode("C005");
							plnDzC.setPlineName("二炼快锻");
							plnDzC.setProPlanId(plnDzS.getProPlanId());
							plnDzC.setGxPlanNo(plnDzS.getGxPlanNo());
							plnDzC.setDiameter(dimaterKc);
							plnDzC.setLenGpSingle("1");
							plnDzC.setWtGpSingle(plnDzS.getWtOneIn());
							plnDzC.setCutNumGp(String.valueOf(cutNumGp));
							plnDzC.setLengthM("1");
							plnDzC.setWeightM(plnDzS.getWtOneIn());
							plnDzC
									.setPlanIndoubleNum(String
											.valueOf(takeCount));
							plnDzC.setPlanIndoubleWt(String.valueOf(actWt));
							plnDzC.setCreateName(userName);
							plnDzC.setCreateTime(dateTime);
							// 获取上料行ID
							String heatPlanSeq = corePlnZyFurnM.getHeatPlanSeq(
									conn, heatPlanNo, "C");
							plnDzC.setHeatPlanSqe(heatPlanSeq);
							// 新增
							String addPlnGxDzC = EntityTool
									.fillSqlByEntity(
											CoreXmlSqlParsersFactory
													.getSql("ComPlanFurnace.addPlnZyDzC"),
											plnDzC);
							ps = conn.prepareStatement(addPlnGxDzC);
							ps.executeUpdate();
							ps.close();
						} else {
							// 修改炉计划实表
							String updateC = "UPDATE PLN_ZY_DZ_C T SET T.PLAN_INDOUBLE_NUM=T.PLAN_INDOUBLE_NUM+TO_NUMBER(?),"
									+ "T.PLAN_INDOUBLE_WT=T.PLAN_INDOUBLE_WT+TO_NUMBER(?) ";
							updateC += " WHERE T.HEAT_PLAN_NO=? AND T.HEAT_PLAN_SQE=?";
							ps = conn.prepareStatement(updateC);
							ps.setObject(1, takeCount);
							ps.setObject(2, actWt);
							ps.setObject(3, heatPlanNo);
							ps.setObject(4, heatPlanSqe);
							ps.executeUpdate();
							ps.close();
						}
					}
					if (countdz > 0) {
						// 修改炉计划主表量
						String sqlUpdate = CoreXmlSqlParsersFactory
								.getSql("FrmPlanSteelMakMod.updatePlnDzM");
						ps = conn.prepareStatement(sqlUpdate);
						ps.setObject(1, heatPlanNo);
						ps.setObject(2, heatPlanNo);
						ps.executeUpdate();
						ps.close();
					}
				}
				double wt = 0;
				int num = 0;
				// 查询锻造订单对应铸造订单
				String sqlCast = CoreXmlSqlParsersFactory
						.getSql("FrmPlanSteelMakMod.getOrderZz");
				ps = conn.prepareStatement(sqlCast);
				ps.setObject(1, proPlanId);
				rs = ps.executeQuery();
				while (rs.next()) {
					int numlk = 0;
					double wtLk = 0;
					wt = rs.getDouble("CAN_WGT");
					num = rs.getInt("CAN_NUM");
					if (takeCountK > num) {
						numlk = num;
						wtLk = wt;
					} else {
						numlk = takeCountK;
						wtLk = takeWtK;
					}
					// 修改铸造订单利库量
					String updateZz = "UPDATE PLN_ORDER_CAST_S T SET T.PLAN_WT_SOLD=NVL(T.PLAN_WT_SOLD,0)+(? * NVL(T.WT_CAST_ONE, 0)),"
							+ "T.PLAN_NUM_SOLD=NVL(T.PLAN_NUM_SOLD,0)+?,T.UPDATE_NAME=?,T.UPDATE_TIME=SYSDATE WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
					PreparedStatement ps1 = conn.prepareStatement(updateZz);
					ps1.setObject(1, numlk);
					ps1.setObject(2, numlk);
					ps1.setObject(3, userName + "-[利库]");
					ps1.setObject(4, rs.getString("PRO_PLAN_ID"));
					ps1.setObject(5, rs.getString("GX_PLAN_NO"));
					ps1.executeUpdate();
					ps1.close();
					takeCountK = takeCountK - numlk;
					takeWtK = takeWtK - wtLk;
				}
				rs.close();
				ps.close();

			}
			cro.setV_errMsg("利库成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 查询库存重量
	 * 
	 * @return
	 */
	public CoreReturnObject queryMatWeigth(String judgNo, String fixNum,
			String lengthM, String lengSingGp, String num, Boolean flag) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanSteelMakMod.queryMatWeigth");
		sql += " AND T.JUDGE_STOVE_NO = '" + judgNo
				+ "' AND T.FIX_NUM = TO_NUMBER('" + fixNum
				+ "') AND T.ACT_LEN = TO_NUMBER('" + lengthM
				+ "') AND T.FIX_LEN = TO_NUMBER('" + lengSingGp + "')   ";
		// if (flag) {
		// sql += " AND T.MAT_STATUS ='80150102'";
		// } else {
		sql += " AND T.MAT_STATUS in('80150101','80150102')";
		// }
		sql += " ORDER BY T.MAT_NO ASC) T1 WHERE ROWNUM <= '" + num + "'";
		return this.getData(sql);
	}

	/**
	 * 自动换炉号
	 * 
	 * @param jslist
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doChangeFurnNo(ArrayList<ArrayList<String>> jslist)
			throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			String judStoveNo = "";
			String judgeStoveNoNew = "";// 新炉号
			int numCount = 0;
			StoveNoGenerate stGenerat = new StoveNoGenerate();
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String judgeStoveNo = list.get(0);
				int takeCount = Integer.parseInt(list.get(1));// 支
				double actLen = Double.parseDouble(list.get(2));// 倍尺坯长
				double fixLen = Double.parseDouble(list.get(3));// 单倍
				int cutNumGp = Integer.parseInt(list.get(4));
				// String gptype = list.get(5);
				String proPlanIdZg = list.get(6);
				String gxPlanNoZg = list.get(7);
				if (!proPlanIdZg.equals("")) {
					cro.setV_errMsg("已挂轧管订单,请先脱单再变炉号!");
					return cro;
				}
				List<MatGpMEntity> matGpMlist = getQureyMatGpML(conn,
						judgeStoveNo, actLen, fixLen, cutNumGp, takeCount,
						proPlanIdZg, gxPlanNoZg);
				if (matGpMlist == null) {
					cro.setV_errMsg("管坯物料已发生改变,请刷新!");
					return cro;
				}
				int kcNumCount = 0;
				// 查询库存判定炉号对应支数
				String sqlCount = "SELECT SUM(T.ACT_COUNT) ACT_COUNT  FROM MAT_GP_M T WHERE T.JUDGE_STOVE_NO=?";
				ps = conn.prepareStatement(sqlCount);
				ps.setObject(1, judgeStoveNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					kcNumCount = rs.getInt("ACT_COUNT");
				}
				rs.close();
				ps.close();

				if (judStoveNo == null || !judStoveNo.equals(judgeStoveNo)) {
					numCount = 0;
					judStoveNo = judgeStoveNo;
					String proMonth = "";
					String efPlineCode = "";
					// 查询电炉产线
					String sqlPline = "SELECT T.EF_PLINE_CODE,T.EF_PLINE_NAME,T.JUDGE_STOVE_NO,T.PRO_MONTH FROM PLN_STEELFOR_ORD T WHERE T.JUDGE_STOVE_NO=?";
					ps = conn.prepareStatement(sqlPline);
					ps.setObject(1, judgeStoveNo);
					rs = ps.executeQuery();
					if (rs.next()) {
						efPlineCode = rs.getString("EF_PLINE_CODE");
						proMonth = rs.getString("PRO_MONTH").substring(0, 4);
					}
					rs.close();
					ps.close();

					judgeStoveNoNew = stGenerat.GetNewJudgeStove(efPlineCode,
							"1", 2L, proMonth);
					if (!judgeStoveNoNew.equals("")) {
						// 新建新炉号记录表
						String addNewJudNo = "INSERT INTO PLN_STEELFOR_ORD_LKJUDGESTOVE (JUDGE_STOVE_NO) VALUES (?)";
						ps = conn.prepareStatement(addNewJudNo);
						ps.setObject(1, judgeStoveNoNew);
						ps.executeUpdate();
						ps.close();
					}
					numCount = takeCount;
				} else {
					numCount += takeCount;
				}
				if (kcNumCount <= numCount) {
					cro.setV_errMsg("不能整炉变炉号!");
					return cro;
				}
				if (judgeStoveNoNew.equals("")) {
					cro.setV_errMsg("原炉号(" + judgeStoveNo + ")未获取到新炉号,请确认!");
					return cro;
				}
				for (MatGpMEntity matGpM : matGpMlist) {
					// 修改坯子库存判定炉号
					String sqlUpdateMat = "UPDATE MAT_GP_M T SET T.JUDGE_STOVE_NO=? ,T.JUDGE_FLAG='1',T.JUDGE_STOVE_NO_OLD=? WHERE T.MAT_NO=?";
					ps = conn.prepareStatement(sqlUpdateMat);
					ps.setObject(1, judgeStoveNoNew);
					ps.setObject(2, judgeStoveNo);
					ps.setObject(3, matGpM.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
			}
			cro.setV_errMsg("成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 取消换炉号
	 * 
	 * @param jslist
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doUndoFurnNo(ArrayList<ArrayList<String>> jslist)
			throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// ResultSet rs = null;
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String judgeStoveNo = list.get(0);
				int takeCount = Integer.parseInt(list.get(1));// 支
				double actLen = Double.parseDouble(list.get(2));// 倍尺坯长
				double fixLen = Double.parseDouble(list.get(3));// 单倍
				int cutNumGp = Integer.parseInt(list.get(4));
				// String gptype = list.get(5);
				String proPlanIdZg = list.get(6);
				// String JudgeFlag = list.get(7);
				String JudgeStoveNoOld = list.get(8);// 原炉号
				if (!proPlanIdZg.equals("")) {
					cro.setV_errMsg("已挂轧管订单,请先脱单撤销变炉号!");
					return cro;
				}
				List<MatGpMEntity> matGpMlist = getQureyMatGpML(conn,
						judgeStoveNo, actLen, fixLen, cutNumGp, takeCount,
						proPlanIdZg, "");
				if (matGpMlist == null) {
					cro.setV_errMsg("管坯物料已发生改变,请刷新!");
					return cro;
				}

				// 删除新炉号记录表
				String addNewJudNo = "DELETE PLN_STEELFOR_ORD_LKJUDGESTOVE WHERE JUDGE_STOVE_NO=?";
				ps = conn.prepareStatement(addNewJudNo);
				ps.setObject(1, judgeStoveNo);
				ps.executeUpdate();
				ps.close();

				for (MatGpMEntity matGpM : matGpMlist) {
					if (matGpM.getJudgeFlag().equals("0")) {
						cro.setV_errMsg("判定炉号(" + judgeStoveNo
								+ ")未变炉号,不能进行此操作!");
						return cro;
					}
					// 修改坯子库存判定炉号
					String sqlUpdateMat = "UPDATE MAT_GP_M T SET T.JUDGE_STOVE_NO=? ,T.JUDGE_FLAG='0',T.JUDGE_STOVE_NO_OLD='' WHERE T.MAT_NO=?";
					ps = conn.prepareStatement(sqlUpdateMat);
					ps.setObject(1, JudgeStoveNoOld);
					ps.setObject(2, matGpM.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
			}
			cro.setV_errMsg("成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}
}
