package com.steering.pss.plnsaleord.stove;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.planStove.FrmPlanMaterialSelection;
import com.steering.pss.plnsaleord.planStove.StoveHelper;
import com.steering.pss.plnsaleord.planStove.entity.MilFeedPlanEntity;
import com.steering.pss.plnsaleord.stove.entity.*;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

/**
 * 炉计划公用方法
 * 
 * @author teng 2017.10.27
 */
public class corePlnZyFurnM extends DBCommon {
	private static double Pi = 3.1415926; // 圆周率
	// private int errCode = 0;// 错误返回参数-1
	private String errMessageZg = "";// 返回错误信息
	private String errMessageDz = "";

	/**
	 * 轧管返回错误信息
	 * 
	 * @return
	 */
	public String getErrMessageZg() {
		return errMessageZg;
	}

	/**
	 * 轧管返回错误信息
	 * 
	 * @return
	 */
	public void setErrMessageZg(String errMessageZg) {
		this.errMessageZg = errMessageZg;
	}

	/**
	 * 锻造返回错误信息
	 * 
	 * @return
	 */
	public String getErrMessageDz() {
		return errMessageDz;
	}

	/**
	 * 锻造返回错误信息
	 * 
	 * @return
	 */
	public void setErrMessageDz(String errMessageDz) {
		this.errMessageDz = errMessageDz;
	}

	/**
	 * 新增锻造虚拟炉计划(自炼)
	 * 
	 * @return
	 * @throws SQLException
	 */
	// List<PlnOrderZgSEntityL> plnZg, int countZg
	public boolean addPlnZyDzM(Connection con, PlnSteelforOrdEntityL stord,
			List<PlnOrderDzSEntityL> plnDz, String createName, String createTime)
			throws SQLException {
		PlnZyDzMEntity plnDzM = new PlnZyDzMEntity();
		String sql = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		// double outWt = 0;// 锻造计划重量
		// int outNum = 0;// 锻造计划支
		double oldWt = 0;// 原料计划吨
		int oldNum = 0;// 原料计划支
		// String ccmproPlanIdNo = "";
		String heatPlanNo = "";
		double totWeightM = Double.parseDouble(stord.getTotWeightM());
		int totMumM = Integer.parseInt(stord.getTotMumM())
				* Integer.parseInt(stord.getCutNumGp());
		for (PlnOrderDzSEntityL plnDzS : plnDz) {
			if (totWeightM <= 0) {
				continue;
			}
			// // 通过判定炉号+锻造订单查询锻造虚拟炉计划是否存在记录
			// sql =
			// "SELECT T.HEAT_PLAN_NO,T.PRO_PLAN_ID,T.CCM_PRO_PLAN_ID FROM PLN_ZY_DZ_M T WHERE T.JUDGE_STOVE_NO=? AND INSTR(T.PRO_PLAN_ID||'/'||T.GX_PLAN_NO, ?) > 0  ";
			// ps = con.prepareStatement(sql);
			// ps.setObject(1, stord.getJudgeStoveNo());
			// ps.setObject(2, plnDzS.getProPlanId() + "/" +
			// plnDzS.getGxPlanNo());
			// rs = ps.executeQuery();
			// if (rs.next()) {
			// fl = true;
			// heatPlanNo = rs.getString("HEAT_PLAN_NO");
			// ccmproPlanIdNo = rs.getString("CCM_PRO_PLAN_ID");
			// }
			// rs.close();
			// ps.close();

			double wtIn = 0;// 已生成炉计划原料吨
			int numIn = 0;
			// 查询当前锻造订单是否存在炉计划
			sql = "SELECT NVL(SUM(T.PLN_INDOUBLE_NUM),0) PLN_INDOUBLE_NUM,NVL(SUM(T.PLN_INDOUBLE_WT),0) PLN_INDOUBLE_WT FROM PLN_ZY_DZ_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
			ps = con.prepareStatement(sql);
			ps.setObject(1, plnDzS.getProPlanId());
			ps.setObject(2, plnDzS.getGxPlanNo());
			rs = ps.executeQuery();
			if (rs.next()) {
				wtIn = rs.getDouble("PLN_INDOUBLE_WT");
				numIn = rs.getInt("PLN_INDOUBLE_NUM");
			}
			rs.close();
			ps.close();

			// 判断铸造计划支吨是否大于锻造计划支吨
			if (totWeightM - (Double.parseDouble(plnDzS.getWeigthS()) - wtIn) < 0.00002) {
				oldWt = totWeightM;
				oldNum = totMumM;
			} 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 (oldWt <= 0) {
				continue;
			}
			if (Integer.parseInt(stord.getStatus()) > 10) {
				plnDzM.setStoveNo(stord.getStoveNo());
				plnDzM.setJudgeStoveNo(stord.getJudgeStoveNo());
				plnDzM.setFlagStoveNo(stord.getFlagStoveNo());
			}
			plnDzM.setUpperHeatplanId(stord.getFurnaceno() + "/"
					+ stord.getSequenceno());
			plnDzM.setPlanstatus("00");
			plnDzM.setUpperProcessCode("B");
			plnDzM.setPlineCode(plnDzS.getPlineCode());
			plnDzM.setPlineName(plnDzS.getPlineName());
			// 判定是否锻造末工序 0,不是；1，是
			if (plnDzS.getIsendgx().equals("0")) {
				plnDzM.setNextProcessCode("D");
				plnDzM.setIsendgx("0");
				plnDzM.setGowhereCode(stord.getGowhereCode());
				plnDzM.setGowhereName(stord.getGowhereName());

				sql = "SELECT T.LEN_GP_SINGLE FROM PLN_ORDER_ZG_S T WHERE T.GPREQ_ID = ? AND ROWNUM = 1";
				ps = con.prepareStatement(sql);
				ps.setObject(1, plnDzS.getProPlanId());
				rs = ps.executeQuery();
				if (rs.next()) {
					plnDzM.setLenGpSingleOut(rs.getString("LEN_GP_SINGLE"));
				}
				rs.close();
				ps.close();

			} else {
				plnDzM.setNextProcessCode("");
				plnDzM.setIsendgx("1");
				// 末工序获取去向
				// String gowWhere = ComPlanFurnace.getPlineCodeGroup(con,
				// plnDzS.getPlineCode(),plnDzS.getProPlanId(),plnDzS.getGxPlanNo());
				// plnDzM.setGowhereCode(gowWhere[0]);
				plnDzM.setGowhereName("离线库");

			}
			plnDzM.setIfMatchOdd("0");
			plnDzM.setIfDoHeatPlan("0");
			plnDzM.setProPlanId(plnDzS.getProPlanId());
			plnDzM.setGxPlanNo(plnDzS.getGxPlanNo());
			plnDzM.setCcmProPlanId(stord.getProPlanId());
			plnDzM.setCcmGxPlanNo(stord.getGxPlanNo());
			plnDzM.setCastingtype(stord.getCastingtype());
			plnDzM.setGradecode(stord.getGradecode());
			plnDzM.setGradename(stord.getGradename());
			plnDzM.setSteelcode(stord.getSteelcode());
			plnDzM.setSteelname(stord.getSteelname());
			plnDzM.setWlidIn(stord.getMaterialNo());
			plnDzM.setWldescIn(stord.getMaterialName());
			plnDzM.setOutwlId(plnDzS.getOutwlId());
			plnDzM.setOutwlDesc(plnDzS.getOutwlDesc());
			plnDzM.setDiameterIn(stord.getDiameter());
			plnDzM.setLengthIn(stord.getLengthM());
			plnDzM.setWtOneIn(stord.getWeightM());
			plnDzM.setLenGpSingle(stord.getLenGpSingle());
			plnDzM.setWtGpSingle(stord.getWeightGpOne());
			plnDzM.setCutNumGp(stord.getCutNumGp());
			plnDzM.setPlnInsingleNum(String.valueOf(oldNum
					/ Integer.parseInt(stord.getCutNumGp())));
			plnDzM.setPlnInsingleWt(String.valueOf(oldWt));
			plnDzM.setPlnIndoubleNum(String.valueOf(oldNum));
			plnDzM.setPlnIndoubleWt(String.valueOf(oldWt));
			plnDzM.setPlnOutsingleNum(String.valueOf(oldNum));
			plnDzM.setPlnOutsingleW(String.valueOf(oldWt));
			// 判定是连铸，还是模铸，1.连铸，2.模铸
			if (stord.getCastingtype().equals("2")) {
				plnDzM.setCutNumOut("1");
			} else {
				plnDzM.setCutNumOut(stord.getCutNumGp());
			}
			plnDzM.setDiameterOut(plnDzS.getDiameterOut());
			plnDzM.setLengthOut(plnDzS.getLengthOut());
			plnDzM.setOutWtOne(plnDzS.getOutWtOne());
			plnDzM.setPlnOutdoubleNum(String.valueOf(oldNum));
			plnDzM.setPlnOutdoubleW(String.valueOf(oldWt));
			plnDzM.setCic(plnDzS.getCic());
			plnDzM.setSic(plnDzS.getSic());
			plnDzM.setPic(plnDzS.getPic());
			plnDzM.setDic(plnDzS.getDic());
			plnDzM.setProDay(stord.getProMonth());
			plnDzM.setBalYearMonth(stord.getProMonth().substring(0, 6));
			plnDzM.setCreateName(createName);
			plnDzM.setCreateTime(createTime);
			// plnDzM.setLenGpSingleOut(String.valueOf(Double.parseDouble(plnDzS.getLengthOut())
			// / Integer.parseInt(plnDzM.getCutNumOut())));

			// 存在相同判定炉号的锻造虚拟炉计划
			// if (!fl) {// 新增
			heatPlanNo = getMaxHeatPlanNo(con, plnDzM.getProPlanId() + "6",
					plnDzM.getProPlanId(), "C");
			// 获取炉计划顺序号
			int gxProSeq = getMaxGxPlnSeq(con, plnDzM.getPlineCode(), "C");
			plnDzM.setHeatPlanNo(heatPlanNo);
			plnDzM.setGxProSeq(String.valueOf(gxProSeq));
			String addPlnGxDz = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("ComPlanFurnace.addPlnZyDzM"), plnDzM);
			ps = con.prepareStatement(addPlnGxDz);
			ps.executeUpdate();
			ps.close();

			// }
			// else {

			// 是否包含当前锻造订单+序号
			// if (!proPlanIdNo.contains(plnDzS.getProPlanId() + "/"
			// + plnDzS.getGxPlanNo())) {
			// proPlanIdNo += "," + plnDzS.getProPlanId() + "/"
			// + plnDzS.getGxPlanNo();
			// }
			// // 是否包含当前锻造订单+序号
			// if (!ccmproPlanIdNo.contains(stord.getProPlanId() + "/"
			// + stord.getGxPlanNo())) {
			// ccmproPlanIdNo += "," + stord.getProPlanId() + "/"
			// + stord.getGxPlanNo();
			// }
			// 修改
			// String updatePlnOrderDz =
			// "UPDATE PLN_ZY_DZ_M T SET T.PLN_INDOUBLE_NUM=TO_NUMBER(NVL(T.PLN_INDOUBLE_NUM,0))+TO_NUMBER(?),"
			// +
			// " T.PLN_INDOUBLE_WT=TO_NUMBER(NVL(T.PLN_INDOUBLE_WT,0))+TO_NUMBER(?) "
			// + "WHERE T.HEAT_PLAN_NO=?";
			// ps = con.prepareStatement(updatePlnOrderDz);
			// ps.setObject(1, oldNum);
			// ps.setObject(2, oldWt);
			// ps.setObject(3, heatPlanNo);
			// ps.executeUpdate();
			// ps.close();
			// }
			// 判定锻造订单是否不是计划状态
			// if (plnDzS.getExeStatus().equals("0")) {
			// // 判定是否锻造末工序 0,不是；1，是
			// if (plnDzM.getIsendgx().equals("0")) {
			// // 生成轧管虚拟炉计划
			// if (addPlnZyZgM(con, stord, plnZg, createName, createTime,
			// "C", heatPlanNo, countZg)) {
			//
			// }
			// }
			// }
			// else {
			// if (getErrMessageDz().equals("")) {
			// setErrMessageDz(stord.getProPlanId() + "/"
			// + stord.getGxPlanNo());
			// }
			// else {
			// String message = stord.getProPlanId() + "/"
			// + stord.getGxPlanNo();
			// if (!getErrMessageDz().contains(message)) {
			// setErrMessageDz(getErrMessageDz() + "," + message);
			// }
			// }
			// continue;
			// }
			// 回写锻造已排产炉计划量
			// String updatePlnOrderDz =
			// "UPDATE PLN_ORDER_DZ_S T SET T.BATCHED_WT=TO_NUMBER(NVL(T.BATCHED_WT,0))+TO_NUMBER(?),"
			// + "T.BATCHED_NUM=TO_NUMBER(NVL(T.BATCHED_NUM,0))+TO_NUMBER(?) "
			// + " WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? ";
			// ps = con.prepareStatement(updatePlnOrderDz);
			// ps.setObject(1, oldWt);
			// ps.setObject(2, oldNum);
			// ps.setObject(3, plnDzS.getProPlanId());
			// ps.setObject(4, plnDzS.getGxPlanNo());
			// ps.executeUpdate();
			// ps.close();

			// 炉次量-装炉
			totWeightM = totWeightM - oldWt;
			totMumM = totMumM - oldNum;
		}
		return true;
	}

	/**
	 * 查询产线内炉计划最大生产序号
	 * 
	 * @param con
	 * @param time
	 * @return
	 * @throws SQLException
	 */
	public static int getMaxGxPlnSeq(Connection con, String plineCode,
			String processCode) throws SQLException {
		int gxPlnSeq = 1;
		String sql = CoreXmlSqlParsersFactory
				.getSql("ComPlanFurnace.getMaxGxPlnSeq");

		if (processCode.equals("C")) {
			sql += " FROM PLN_ZY_DZ_M T ";
		} else if (processCode.equals("D")) {
			sql += " FROM PLN_ZY_ZG_M T ";
		}
		sql += " WHERE T.PLINE_CODE=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			gxPlnSeq = rs.getInt("GX_PRO_SEQ");
		}
		rs.close();
		ps.close();
		return gxPlnSeq;
	}

	/**
	 * 获取实际炉计划上料行ID
	 * 
	 * @param con
	 * @param time
	 * @return
	 * @throws SQLException
	 */
	public static String getHeatPlanSeq(Connection con, String heatPlanNo,
			String processCode) throws SQLException {
		String heatPlanSeq = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("ComPlanFurnace.getHeatPlanSeq");

		if (processCode.equals("C")) {
			sql += " FROM PLN_ZY_DZ_C T ";
		} else if (processCode.equals("D")) {
			sql += " FROM PLN_ZY_ZG_C T ";
		}
		sql += " WHERE T.HEAT_PLAN_NO=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, heatPlanNo);
		ps.setObject(2, heatPlanNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			heatPlanSeq = rs.getString("HEAT_PLAN_SQE");
		}
		rs.close();
		ps.close();
		return heatPlanSeq;
	}

	/**
	 * 查询同订单/排产序号最大炉计划Id
	 * 
	 * @param con
	 * @param time
	 * @return
	 * @throws SQLException
	 */
	public static String getMaxHeatPlanNo(Connection con, String proPlanNoSeq,
			String proPlanId, String processCode) throws SQLException {
		String heatPlanNo = "";
		String sql = "SELECT ? ||  LPAD((NVL(MAX(TO_NUMBER(SUBSTR(T.HEAT_PLAN_NO,16))),'000')+1),3, '0') AS HEAT_PLAN_NO";

		if (processCode.equals("C")) {
			sql += " FROM PLN_ZY_DZ_M T ";
		} else if (processCode.equals("D")) {
			sql += " FROM PLN_ZY_ZG_M T ";
		}

		sql += " WHERE T.PRO_PLAN_ID=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanNoSeq);
		ps.setObject(2, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			heatPlanNo = rs.getString("HEAT_PLAN_NO");
		}
		rs.close();
		ps.close();
		return heatPlanNo;
	}

	/**
	 * 获取轧管产线去向
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static String[] getLastPlineCode(Connection con, String ordLnDlyPk,
			String plnDivideId, String processCode) throws SQLException {
		String[] param = new String[2];
		String sql = "SELECT DISTINCT T.PLINE_CODE,T.PLINE_NAME ";
		if (processCode.equals("F")) {
			sql += " FROM PLN_ORDER_RCL_S T WHERE T.ORD_LN_DLY_PK=? AND T.PLN_DIVIDE_ID=?";
		} else if (processCode.equals("E")) {
			sql += " FROM PLN_ORDER_DBK_S T WHERE T.ORD_LN_DLY_PK=? AND T.PLN_DIVIDE_ID=?";
		} else if (processCode.equals("F")) {
			sql += " FROM PLN_ORDER_JGX_S T WHERE T.ORD_LN_DLY_PK=? AND T.PLN_DIVIDE_ID=?";
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ps.setObject(2, plnDivideId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			param[0] = rs.getString("PLINE_CODE");
			param[1] = rs.getString("PLINE_NAME");
		}
		rs.close();
		ps.close();
		return param;
	}

	/**
	 * 新增轧管虚拟炉计划(自炼)
	 * 
	 * @return
	 * @throws SQLException
	 * @return
	 * @throws SQLException
	 */
	public boolean addPlnZyZgM(Connection con, PlnSteelforOrdEntityL stord,
			List<PlnOrderZgSEntityL> plnZg, String createName,
			String createTime, String processCode, String heatPlanNoDz,
			int countZg) throws SQLException {
		PlnZyZgMEntity plnZgM = new PlnZyZgMEntity();
		String sql = "";
		PreparedStatement ps = null;
		ResultSet rs = null;

		// double outWt = 0;// 轧管计划重量
		// int outNum = 0;// 轧管计划支
		double oldWt = 0;// 原料计划吨
		int oldNum = 0;// 原料计划支
		String ccmproPlanIdNo = "";
		String heatPlanNo = "";
		// 轧管单倍支
		int zgNum = Integer.parseInt(stord.getCutNumGp())
				* Integer.parseInt(stord.getTotMumM());
		double totWeightM = Double.parseDouble(stord.getTotWeightM());
		int contZgM = 0;
		for (PlnOrderZgSEntityL plnZgS : plnZg) {
			int fl = 0;// 是否存在符合条件的轧管虚拟炉计划
			contZgM++;
			if (totWeightM <= 0) {
				continue;
			}
			if (Integer.parseInt(plnZgS.getExeStatus()) > 1) {
				if (getErrMessageZg().equals("")) {
					setErrMessageZg(plnZgS.getProPlanId() + "/"
							+ plnZgS.getGxPlanNo());
				} else {
					String message = plnZgS.getProPlanId() + "/"
							+ plnZgS.getGxPlanNo();
					if (!getErrMessageZg().contains(message)) {
						setErrMessageZg(getErrMessageZg() + "," + message);
					}
				}
				continue;
			}
//			if (plnZgS.getIfStoveIssued().equals("1")) {
//				continue;
//			}
			// 通过判定炉号+锻造订单查询轧管虚拟炉计划是否存在记录
			sql = CoreXmlSqlParsersFactory
					.getSql("ComPlanFurnace.queryPlanZgOld");
			sql += " AND T.UPPER_HEATPLAN_ID = ?";
			ps = con.prepareStatement(sql);
			ps.setObject(1, plnZgS.getProPlanId() + "/" + plnZgS.getGxPlanNo());
			ps.setObject(2, stord.getFurnaceno() + "/" + stord.getSequenceno());
			rs = ps.executeQuery();
			if (rs.next()) {
				fl = 1;
				heatPlanNo = rs.getString("HEAT_PLAN_NO");
				ccmproPlanIdNo = rs.getString("CCM_PRO_PLAN_ID");
			}
			rs.close();
			ps.close();

			double wtIn = 0;// 已生成炉计划原料吨(已装炉)
			int numIn = 0;
			// 查询当前轧管订单是否存在炉计划
			sql = "SELECT NVL(SUM(T.PLN_INSINGLE_NUM),0) PLN_INSINGLE_NUM,NVL(SUM(T.PLN_INSINGLE_WT),0) PLN_INSINGLE_WT FROM PLN_ZY_ZG_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
			ps = con.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();

			// 利库支吨
			// double lkWt = Double.parseDouble(plnZgS.getMatchWtIn());
			// int lkNum = Integer.parseInt(plnZgS.getMatchNumIn());

			// 判断铸造计划支吨是否大于轧管需坯支吨
			int dimater = Integer.parseInt(plnZgS.getDiameterGp());
			if (dimater == 251) {
				dimater = 250;
			}
			double mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
			double lengSingleGp = Double.parseDouble(stord.getLenGpSingle());
			double lengthM = Double.parseDouble(stord.getLengthM());
			if (stord.getGptype().equals("2")) {
				lengSingleGp = Double.parseDouble(plnZgS.getLenGpSingle());
				lengthM = lengSingleGp * Integer.parseInt(stord.getCutNumGp());
			}
			// 单倍坯重
			double lenGpWt = new BigDecimal(mSingleWeight
					* (lengSingleGp / 1000)).setScale(3, RoundingMode.UP)
					.doubleValue();
			// 倍尺坯单支重=坯米单重*倍尺坯长/1000
			double weightM = new BigDecimal(mSingleWeight * (lengthM / 1000))
					.setScale(3, RoundingMode.UP).doubleValue();
			if (countZg > 1) {
				if (contZgM == countZg) {
					oldWt = totWeightM;
					oldNum = zgNum;
				} else {
					if (totWeightM
							- (Double.parseDouble(plnZgS.getGpreqWeight()) - wtIn) < 0.00002) {
						oldWt = totWeightM;
						oldNum = zgNum;
					} else {
						oldWt = Double.parseDouble(plnZgS.getGpreqWeight())
								- wtIn;
						oldNum = (int) Math.ceil(oldWt / weightM)
								* Integer.parseInt(stord.getCutNumGp());
						// Integer.parseInt(plnZgS.getGpreqNum()) - numIn;
						// outWt += Double.parseDouble(plnZgS.getGpreqWeight())
						// -
						// wtIn;
						// outNum += Integer.parseInt(plnZgS.getGpreqNum()) -
						// numIn;
						oldWt = oldNum * lenGpWt;
					}
				}
			} else if (countZg == 1) {
				oldWt = totWeightM;
				oldNum = zgNum;
			}

			if (oldNum <= 0) {
				continue;
			}
			if (Integer.parseInt(stord.getStatus()) > 10) {
				plnZgM.setStoveNo(stord.getStoveNo());
				plnZgM.setJudgeStoveNo(stord.getJudgeStoveNo());
				plnZgM.setFlagStoveNo(stord.getFlagStoveNo());
			}
			if (processCode.equals("B")) {
				plnZgM.setUpperHeatplanId(stord.getFurnaceno() + "/"
						+ stord.getSequenceno());
			} else {
				plnZgM.setUpperHeatplanId(heatPlanNoDz);
			}
			// plnZgM.setZyBatchId(plnZgS.getZyBatchId());
			plnZgM.setPlanstatus("00");
			plnZgM.setUpperProcessCode(processCode);
			plnZgM.setPlineCode(plnZgS.getPlineCode());
			plnZgM.setPlineName(plnZgS.getPlineName());
			if (!plnZgS.getDecision().equals("A")) {
				// 通过制程判定轧管是否末工序
				if (plnZgS.getWholeBacklogDesc().contains("镦拔扩")) {
					plnZgM.setNextProcessCode("E");			
				} else if (plnZgS.getWholeBacklogDesc().contains("热处理")) {
					plnZgM.setNextProcessCode("F");
				} else if (plnZgS.getWholeBacklogDesc().contains("加工")) {
					plnZgM.setNextProcessCode("G");
				} else {
					if (plnZgS.getOrdLnDlyPk().contains("ZZBY-J")) {
						plnZgM.setNextProcessCode("H");
					} else if (plnZgS.getOrdLnDlyPk().contains("ZZBY-D")) {
						plnZgM.setNextProcessCode("G");
					}else{
						plnZgM.setNextProcessCode("");
					}

				}
				plnZgM.setIsendgx("0");
			} else {
				plnZgM.setNextProcessCode("");
				plnZgM.setIsendgx("1");
			}

			// String gowWhere = ComPlanFurnace.getPlineCodeGroup(con, 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 = con.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.setIfMatchOdd("0");
			plnZgM.setIfDoHeatPlan("0");
			plnZgM.setProPlanId(plnZgS.getProPlanId());
			plnZgM.setGxPlanNo(plnZgS.getGxPlanNo());
			plnZgM.setCcmProPlanId(stord.getProPlanId());
			plnZgM.setCcmGxPlanNo(stord.getGxPlanNo());
			plnZgM.setGradecode(stord.getGradecode());
			plnZgM.setGradename(stord.getGradename());
			plnZgM.setSteelcode(plnZgS.getSteelcode());
			plnZgM.setSteelname(plnZgS.getSteelname());
			plnZgM.setInwlId(stord.getMaterialNo());
			plnZgM.setInwlDesc(stord.getMaterialName());
			plnZgM.setOutwlId(plnZgS.getOutwlId());
			plnZgM.setOutwlDesc(plnZgS.getOutwlDesc());
			plnZgM.setPassCode(plnZgS.getPassCode());
			plnZgM.setPassName(plnZgS.getPassName());
			plnZgM.setPlnInsingleNum(String.valueOf(oldNum));
			plnZgM.setPlnInsingleWt(String.valueOf(oldWt));
			// 算预计成品吨
			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 =0;
			if (plnZgS.getPassive().equals("0")) {// 被动
				cpWt = weightOne
						* (Double.parseDouble(plnZgS.getRollLength()) - cutHeadEnd)
						* oldNum;
			} else {
				// 主动
				cpWt = weightOne
						* (Double.parseDouble(plnZgS.getAimlength())*Integer.parseInt(plnZgS.getOutnumCut()))
						* oldNum;
			}
		
//			double cpWt = weightOne
//					* (Double.parseDouble(plnZgS.getRollLength()) - cutHeadEnd)
//					* oldNum;
			// 折算吨
			double reducedWt = (Double.parseDouble(plnZgS.getGpreqWeight()) / Double
					.parseDouble(plnZgS.getWeigthS()))
					* cpWt;
			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());
			if (!plnZgS.getSiglLen().equals("")) {
				double lenMin = Double.parseDouble(plnZgS.getAimlength())
						- Double.parseDouble(plnZgS.getEndlenSpecstd());

				lenMin = (int) (Double.parseDouble(plnZgS.getAimlength()) / Double
						.parseDouble(plnZgS.getSiglLen()))
						* Double.parseDouble(plnZgS.getSiglLen());
				if (lenMin - Double.parseDouble(plnZgS.getLenRollMin()) < 0) {
					lenMin = Double.parseDouble(plnZgS.getLenRollMin());
				}

				double lenMax = lenMin
						+ Double.parseDouble(plnZgS.getEndlenSpecstd());
				// 判定是否除以单倍尺长可以整除
				plnZgM.setLenRollMax(String.valueOf(lenMax));
				plnZgM.setLenRollMin(String.valueOf(lenMin));
			} else {
				plnZgM.setLenRollMax(plnZgS.getLenRollMax());
				plnZgM.setLenRollMin(plnZgS.getLenRollMin());
			}
			plnZgM.setRollLength(plnZgS.getRollLength());
			plnZgM.setLenGpSingle(plnZgS.getLenGpSingle());
			plnZgM.setDiameter(plnZgS.getDiameterGp());
			plnZgM.setWtGpSingle(stord.getWeightGpOne());
			plnZgM.setOutnumCut(plnZgS.getOutnumCut());
			plnZgM.setCic(plnZgS.getCicGp());
			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(createTime.substring(0, 10).replace("-", ""));
			plnZgM.setBalYearMonth(createTime.substring(0, 7).replace("-", ""));
			plnZgM.setCreateName(createName);
			plnZgM.setCreateTime(createTime);
			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.setBom(plnZgS.getBom());
			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(stord.getGptype());
			plnZgM.setOutsBilletFl("0");
			// 存在相同判定炉号的轧管虚拟炉计划
			if (fl == 0) {// 新增
				heatPlanNo = getMaxHeatPlanNo(con, plnZgM.getProPlanId() + "6",
						plnZgM.getProPlanId(), "D");
				// 获取炉计划顺序号
				int gxProSeq = getMaxGxPlnSeq(con, plnZgM.getPlineCode(), "D");
				plnZgM.setHeatPlanNo(heatPlanNo);
				plnZgM.setGxProSeq(String.valueOf(gxProSeq));
				String addPlnGxZg = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("ComPlanFurnace.addPlnZyZgM"), plnZgM);
				ps = con.prepareStatement(addPlnGxZg);
				ps.executeUpdate();
				ps.close();

			} else {
				// 修改
				String updatePlnOrderDz = "UPDATE PLN_ZY_ZG_M T SET "
						+ " T.PLN_INSINGLE_NUM=TO_NUMBER(NVL(T.PLN_INSINGLE_NUM,0))+TO_NUMBER(?),"
						+ " T.PLN_INSINGLE_WT=TO_NUMBER(NVL(T.PLN_INSINGLE_WT,0))+TO_NUMBER(?),"
						+ " T.PLN_OUTSINGLE_NUM=TO_NUMBER(NVL(T.PLN_OUTSINGLE_NUM,0))+TO_NUMBER(?),"
						+ " T.PLN_OUTSINGLE_W=TO_NUMBER(NVL(T.PLN_OUTSINGLE_W,0))+TO_NUMBER(?),"
						+ " T.REDUCED_WT=TO_NUMBER(NVL(T.REDUCED_WT,0))+TO_NUMBER(?) "
						+ " WHERE T.HEAT_PLAN_NO=?";
				ps = con.prepareStatement(updatePlnOrderDz);
				ps.setObject(1, oldNum);
				ps.setObject(2, oldWt);
				ps.setObject(3, oldNum
						* Integer.parseInt(plnZgS.getOutnumCut()));
				ps.setObject(4, cpWt);
				ps.setObject(5, reducedWt);
				ps.setObject(6, heatPlanNo);
				ps.executeUpdate();
				ps.close();
			}
			// // 回写轧管已排产炉计划量
			// String updatePlnOrderZg =
			// "UPDATE PLN_ORDER_ZG_S T SET T.BATCHED_WT_G=TO_NUMBER(NVL(T.BATCHED_WT_G,0))+TO_NUMBER(?),"
			// +
			// "T.BATCHED_NUM_G=TO_NUMBER(NVL(T.BATCHED_NUM_G,0))+TO_NUMBER(?) "
			// + " WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? ";
			// ps = con.prepareStatement(updatePlnOrderZg);
			// ps.setObject(1, oldWt);
			// ps.setObject(2, oldNum);
			// ps.setObject(3, plnZgS.getProPlanId());
			// ps.setObject(4, plnZgS.getGxPlanNo());
			// ps.executeUpdate();
			// ps.close();

			// 炉次量-装炉
			totWeightM = totWeightM - oldWt;
			zgNum = zgNum - oldNum;
		}
		return true;
	}

	/**
	 * 新增轧管虚拟炉计划(外购)
	 * 
	 * @return
	 * @throws SQLException
	 * @param con
	 * @param processCode
	 * @return
	 * @throws SQLException
	 * @throws ParseException
	 */
	public boolean addPlnZyZgMWg(Connection con, MatGpMEntity stord,
			PlnOrderZgSEntityL plnZgS, String createName, String createTime,
			String processCode, String heatPlanNoDz) throws SQLException {
		PlnZyZgMEntity plnZgM = new PlnZyZgMEntity();
		String sql = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		int fl = 0;// 是否存在符合条件的轧管虚拟炉计划
		double oldWt = 0;// 原料计划吨
		int oldNum = 0;// 原料计划支
		String heatPlanNo = "";
		// 轧管单倍支
		int zgNum = Integer.parseInt(stord.getActCount());
		double totWeightM = Double.parseDouble(stord.getActWeight());
		if (totWeightM <= 0) {
			return false;
		}
		// 通过判定炉号+锻造订单查询轧管虚拟炉计划是否存在记录
		sql = CoreXmlSqlParsersFactory.getSql("ComPlanFurnace.queryPlanZgOld");
		sql += " AND T.JUDGE_STOVE_NO= ?";
		ps = con.prepareStatement(sql);
		ps.setObject(1, plnZgS.getProPlanId() + "/" + plnZgS.getGxPlanNo());
		ps.setObject(2, stord.getJudgeStoveNo());
		rs = ps.executeQuery();
		if (rs.next()) {
			fl = 1;
			heatPlanNo = rs.getString("HEAT_PLAN_NO");
		}
		rs.close();
		ps.close();

		// double wtIn = 0;// 已生成炉计划原料吨(已装炉)
		// int numIn = 0;
		// // 查询当前轧管订单是否存在炉计划
		// sql =
		// "SELECT T.BATCHED_WT_G,T.BATCHED_NUM_G FROM PLN_ORDER_ZG_S T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
		// ps = con.prepareStatement(sql);
		// ps.setObject(1, plnZgS.getProPlanId());
		// ps.setObject(2, plnZgS.getGxPlanNo());
		// rs = ps.executeQuery();
		// if (rs.next()) {
		// wtIn = rs.getDouble("BATCHED_WT_G");
		// numIn = rs.getInt("BATCHED_NUM_G");
		// }
		// rs.close();
		// ps.close();
		int dimater = Integer.parseInt(plnZgS.getDiameterGp());
		if (dimater == 251) {
			dimater = 250;
		}
		double mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;// 米单重
		// 倍尺坯长
		// double lenLong = Double.parseDouble(stord.getActLen());
		// 倍尺坯单支重=坯米单重*倍尺坯长/1000
		// double weightM = mSingleWeight * (lenLong / 1000);
		// 单倍坯重
		double weightGpOne = mSingleWeight
				* (Double.parseDouble(plnZgS.getLenGpSingle()) / 1000);
		// 判断铸造计划支吨是否大于轧管需坯支吨
		// if (totWeightM - (Double.parseDouble(plnZgS.getGpreqWeight()) - wtIn)
		// < 0.00002) {
		oldWt = totWeightM;
		oldNum = zgNum;
		// }
		// else {
		// oldWt = Double.parseDouble(plnZgS.getGpreqWeight()) - wtIn;
		// oldNum = (int) Math.ceil(oldWt / weightM)
		// * Integer.parseInt(stord.getFixNum());
		// oldWt = oldNum * weightGpOne;
		// }

		if (oldWt <= 0) {
			return false;
		}
		plnZgM.setStoveNo(stord.getStoveNo());
		plnZgM.setJudgeStoveNo(stord.getJudgeStoveNo());
		plnZgM.setFlagStoveNo(stord.getFlagStoveNo());
		// if (processCode.equals("B")) {
		// plnZgM.setUpperHeatplanId(stord.getFurnaceno() + "/"
		// + stord.getSequenceno());
		// }
		// else {
		plnZgM.setUpperHeatplanId(stord.getFurnaceno() + "/"
				+ stord.getSequenceno());
		// }
		// plnZgM.setZyBatchId(plnZgS.getZyBatchId());
		plnZgM.setPlanstatus("00");
		plnZgM.setUpperProcessCode(processCode);
		plnZgM.setPlineCode(plnZgS.getPlineCode());
		plnZgM.setPlineName(plnZgS.getPlineName());
		if (!plnZgS.getDecision().equals("A")) {
			// 通过制程判定轧管是否末工序
			if (plnZgS.getWholeBacklogDesc().contains("镦拔扩")) {
				plnZgM.setNextProcessCode("E");
			} else if (plnZgS.getWholeBacklogDesc().contains("热处理")) {
				plnZgM.setNextProcessCode("F");
			} else if (plnZgS.getWholeBacklogDesc().contains("加工")) {
				plnZgM.setNextProcessCode("G");

			} else {
				if (plnZgS.getOrdLnDlyPk().contains("ZZBY-J")) {
					plnZgM.setNextProcessCode("H");
				} else {
					plnZgM.setNextProcessCode("");
				}
			}
			plnZgM.setIsendgx("0");
		} else {
			plnZgM.setNextProcessCode("");
			plnZgM.setIsendgx("1");
		}
		// // 末工序获取去向
		// String gowWhere = ComPlanFurnace.getPlineCodeGroup(con, 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 = con.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.setIfMatchOdd("0");
		plnZgM.setIfDoHeatPlan("0");
		plnZgM.setProPlanId(plnZgS.getProPlanId());
		plnZgM.setGxPlanNo(plnZgS.getGxPlanNo());
		plnZgM.setCcmProPlanId(stord.getGpreqId());
		plnZgM.setCcmGxPlanNo("");
		plnZgM.setGradecode(plnZgS.getGradecode());
		plnZgM.setGradename(plnZgS.getGradename());
		plnZgM.setSteelcode(plnZgS.getSteelcode());
		plnZgM.setSteelname(plnZgS.getSteelname());
		plnZgM.setInwlId(plnZgS.getInwlId());
		plnZgM.setInwlDesc(plnZgS.getInwlDesc());
		plnZgM.setOutwlId(plnZgS.getOutwlId());
		plnZgM.setOutwlDesc(plnZgS.getOutwlDesc());
		plnZgM.setPassCode(plnZgS.getPassCode());
		plnZgM.setPassName(plnZgS.getPassName());
		plnZgM.setPlnInsingleNum(String.valueOf(oldNum));
		plnZgM.setPlnInsingleWt(String.valueOf(oldWt));

		// 算预计成品吨
		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;
		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());
		if (!plnZgS.getSiglLen().equals("")) {
			double lenMin = Double.parseDouble(plnZgS.getAimlength())
					- Double.parseDouble(plnZgS.getEndlenSpecstd());

			lenMin = (int) (Double.parseDouble(plnZgS.getAimlength()) / Double
					.parseDouble(plnZgS.getSiglLen()))
					* Double.parseDouble(plnZgS.getSiglLen());
			if (lenMin - Double.parseDouble(plnZgS.getLenRollMin()) < 0) {
				lenMin = Double.parseDouble(plnZgS.getLenRollMin());
			}

			double lenMax = lenMin
					+ Double.parseDouble(plnZgS.getEndlenSpecstd());
			// 判定是否除以单倍尺长可以整除
			plnZgM.setLenRollMax(String.valueOf(lenMax));
			plnZgM.setLenRollMin(String.valueOf(lenMin));
		} else {
			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(plnZgS.getCicGp());
		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(createTime.substring(0, 10).replace("-", ""));
		plnZgM.setBalYearMonth(createTime.substring(0, 7).replace("-", ""));
		plnZgM.setCreateName(createName);
		plnZgM.setCreateTime(createTime);
		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.setBom(plnZgS.getBom());
		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(stord.getGptype());
		plnZgM.setOutsBilletFl("1");
		// 存在相同判定炉号的轧管虚拟炉计划
		if (fl == 0) {// 新增
			heatPlanNo = getMaxHeatPlanNo(con, plnZgM.getProPlanId() + "6",
					plnZgM.getProPlanId(), "D");
			// 获取炉计划顺序号
			int gxProSeq = getMaxGxPlnSeq(con, plnZgM.getPlineCode(), "D");
			plnZgM.setHeatPlanNo(heatPlanNo);
			plnZgM.setGxProSeq(String.valueOf(gxProSeq));
			String addPlnGxZg = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("ComPlanFurnace.addPlnZyZgM"), plnZgM);
			ps = con.prepareStatement(addPlnGxZg);
			ps.executeUpdate();
			ps.close();

		} else {
			// 修改
			String updatePlnOrderDz = "UPDATE PLN_ZY_ZG_M T SET "
					+ " T.PLN_INSINGLE_NUM=TO_NUMBER(NVL(T.PLN_INSINGLE_NUM,0))+TO_NUMBER(?),"
					+ " T.PLN_INSINGLE_WT=TO_NUMBER(NVL(T.PLN_INSINGLE_WT,0))+TO_NUMBER(?),"
					+ " T.PLN_OUTSINGLE_NUM=TO_NUMBER(NVL(T.PLN_OUTSINGLE_NUM,0))+TO_NUMBER(?),"
					+ " T.PLN_OUTSINGLE_W=TO_NUMBER(NVL(T.PLN_OUTSINGLE_W,0))+TO_NUMBER(?),"
					+ " T.REDUCED_WT=TO_NUMBER(NVL(T.REDUCED_WT,0))+TO_NUMBER(?) "
					+ " WHERE T.HEAT_PLAN_NO=?";
			ps = con.prepareStatement(updatePlnOrderDz);
			ps.setObject(1, oldNum);
			ps.setObject(2, oldWt);
			ps.setObject(3, oldNum * Integer.parseInt(plnZgS.getOutnumCut()));
			ps.setObject(4, cpWt);
			ps.setObject(5, reducedWt);
			ps.setObject(6, heatPlanNo);
			ps.executeUpdate();
			ps.close();
		}
		// // 回写轧管已排产炉计划量
		// String updatePlnOrderZg =
		// "UPDATE PLN_ORDER_ZG_S T SET T.BATCHED_WT_G=TO_NUMBER(NVL(T.BATCHED_WT_G,0))+TO_NUMBER(?),"
		// + "T.BATCHED_NUM_G=TO_NUMBER(NVL(T.BATCHED_NUM_G,0))+TO_NUMBER(?) "
		// + " WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? ";
		// ps = con.prepareStatement(updatePlnOrderZg);
		// ps.setObject(1, oldWt);
		// ps.setObject(2, oldNum);
		// ps.setObject(3, plnZgS.getProPlanId());
		// ps.setObject(4, plnZgS.getGxPlanNo());
		// ps.executeUpdate();
		// ps.close();

		// 炉次量-装炉
		// totWeightM = totWeightM - oldWt;
		// zgNum = zgNum - 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 = con.prepareStatement(sqlUpdate);
		ps.setObject(1, heatPlanNo);
		ps.setObject(2, stord.getJudgeStoveNo());
		ps.setObject(3, plnZgS.getProPlanId());
		ps.setObject(4, plnZgS.getGxPlanNo());
		ps.executeUpdate();
		ps.close();
		return true;
	}

	/**
	 * 新增锻造实际炉计划
	 * 
	 * @return
	 * @throws SQLException
	 */
	public boolean addPlnZyDzC(Connection con, List<PlnZyDzMEntity> plnDz,
			String createName, String createTime, int planInNum,
			double planInWt, String jugdeApplyCode, QcmGpJudgeApplyEntity qcmGp)
			throws SQLException {
		PlnZyDzCEntity plnDzC = new PlnZyDzCEntity();
		PreparedStatement ps = null;
		// 查询是否生成实际锻造炉计划
		// String sqlDz="SELECT COUNT(*) COUNT FROM PLN_ZY_DZ_C T WHERE 1=1 ";

		for (PlnZyDzMEntity plnDzS : plnDz) {
			plnDzC.setHeatPlanNo(plnDzS.getHeatPlanNo());
			plnDzC.setJugdeApplyCode(jugdeApplyCode);
			plnDzC.setStoveNo(plnDzS.getStoveNo());
			plnDzC.setJudgeStoveNo(plnDzS.getJudgeStoveNo());
			plnDzC.setFlagStoveNo(plnDzS.getFlagStoveNo());
			plnDzC.setZyBatchId(plnDzS.getZyBatchId());
			plnDzC.setPlineCode(plnDzS.getPlineCode());
			plnDzC.setPlineName(plnDzS.getPlineName());
			plnDzC.setProPlanId(plnDzS.getProPlanId());
			plnDzC.setGxPlanNo(plnDzS.getGxPlanNo());
			plnDzC.setDiameter(plnDzS.getDiameterIn());
			plnDzC.setLenGpSingle(plnDzS.getLenGpSingle());
			plnDzC.setWtGpSingle(plnDzS.getWtGpSingle());
			plnDzC.setCutNumGp(plnDzS.getCutNumGp());
			plnDzC.setLengthM(plnDzS.getLengthOut());
			plnDzC.setWeightM(plnDzS.getOutWtOne());
			plnDzC.setPlanIndoubleNum(String.valueOf(planInNum));
			plnDzC.setPlanIndoubleWt(String.valueOf(planInWt));
			plnDzC.setCreateName(createName);
			plnDzC.setCreateTime(createTime);
			// 获取上料行ID
			String heatPlanSeq = getHeatPlanSeq(con, plnDzS.getHeatPlanNo(),
					"C");
			plnDzC.setHeatPlanSqe(heatPlanSeq);
			// 新增
			String addPlnGxDz = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("ComPlanFurnace.addPlnZyDzC"), plnDzC);
			ps = con.prepareStatement(addPlnGxDz);
			ps.executeUpdate();
			ps.close();

			// // 修改管坯MAT_GP_M挂料状态
			// String updatesql =
			// "UPDATE MAT_GP_M T SET T.MAT_STATUS='80150103' WHERE T.FURNACENO=? AND T.SEQUENCENO=? AND T.JUDGE_STOVE_NO=?";
			// ps = con.prepareStatement(updatesql);
			// ps.setObject(1, stord.getFurnaceno());
			// ps.setObject(2, stord.getSequenceno());
			// ps.setObject(3, stord.getJudgeStoveNo());
			// ps.executeUpdate();
			// ps.close();
		}
		return true;
	}

	/**
	 * 新增轧管实际炉计划
	 * 
	 * @return
	 * @throws SQLException
	 * @param con
	 * @param plnZg
	 * @param createName
	 * @param createTime
	 * @param processCode
	 * @return
	 * @throws SQLException
	 */
	public boolean addPlnZyZgC(Connection con, PlnZyZgMEntity plnZgS,
			String createName, String createTime, int planInNum,
			double planInWt, String jugdeApplyCode, String multipleNum,
			double actLen, int numC, double wtC) throws SQLException {
		PlnZyZgCEntity plnZgC = new PlnZyZgCEntity();
		PreparedStatement ps = null;
		// int numM=Integer.parseInt(s)
		int diameter = Integer.parseInt(plnZgS.getDiameter());
		if (diameter == 251) {
			diameter = 250;
		}
		double mSingleWeight = (7.8 * Pi / 4 * diameter * diameter) / 1000 / 1000;// 米单重
		double oldfixLenTake = actLen / Integer.parseInt(multipleNum);// 库存单倍长度
		double lenSingle = Double.parseDouble(plnZgS.getLenGpSingle());// 轧管单倍
		int cutNum = (int) (actLen / lenSingle);// 新倍尺数
		double actSingLen = cutNum * lenSingle;// 新倍尺长度
		// 倍尺坯单支重=坯米单重*倍尺坯长/1000
		double weightM = mSingleWeight * (actSingLen / 1000);
		double weightModl = mSingleWeight * (actLen / 1000);
		double cutLength = actLen - actSingLen;// 切余长度
		double minLenSingle = 0;
		// 获取产线最小环形炉
		String sql = " SELECT NVL(T.LENGTH_MIN_RING,0) LENGTH_MIN_RING FROM COM_BASE_PLINE T WHERE T.PLINE_CODE=?";
		ps = con.prepareStatement(sql);
		ps.setObject(1, plnZgS.getPlineCode());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			minLenSingle = rs.getDouble("LENGTH_MIN_RING");
		}
		rs.close();
		ps.close();

		String cutTarget = "";
		double weightMs = mSingleWeight * (cutLength / 1000);
		double cutWeight = weightMs * planInNum;
		if (cutLength > 0) {
			if (minLenSingle - cutLength > 0) {
				cutTarget = "返废";
			} else {
				cutTarget = "回库";
			}
		}
		// 单倍坯重
		// double weightGpOne = mSingleWeight * (fixLenTake / 1000);
		// 判定生成实际炉计划量是否大于虚拟炉计划量
		// if (numC > Integer.parseInt(plnZgS.getPlnInsingleNum())) {
		// planInNum = numC - Integer.parseInt(plnZgS.getPlnInsingleNum());
		//
		// planInWt = weightM * planInNum;
		// }

		plnZgC.setHeatPlanNo(plnZgS.getHeatPlanNo());
		plnZgC.setJugdeApplyCode(jugdeApplyCode);
		plnZgC.setStoveNo(plnZgS.getStoveNo());
		plnZgC.setJudgeStoveNo(plnZgS.getJudgeStoveNo());
		plnZgC.setFlagStoveNo(plnZgS.getFlagStoveNo());
		if (!plnZgS.getPlanstatus().equals("00")) {
			plnZgC.setZyBatchId(plnZgS.getZyBatchId());
		}
		plnZgC.setProPlanId(plnZgS.getProPlanId());
		plnZgC.setGxPlanNo(plnZgS.getGxPlanNo());
		plnZgC.setPlineCode(plnZgS.getPlineCode());
		plnZgC.setPlineName(plnZgS.getPlineName());
		plnZgC.setLenGpSingle(plnZgS.getLenGpSingle());
		plnZgC.setDiameter(plnZgS.getDiameter());
		plnZgC.setWtGpSingle(plnZgS.getWtGpSingle());
		plnZgC.setCutNumGp(String.valueOf(cutNum));
		plnZgC.setLengthM(String.valueOf(actSingLen));
		plnZgC.setWeightM(String.valueOf(weightM));
		plnZgC.setPlanIndoubleNum(String.valueOf(planInNum));
		plnZgC.setPlanIndoubleWt(String.valueOf(planInWt));
		plnZgC.setCreateName(createName);
		plnZgC.setCreateTime(createTime);
		plnZgC.setFeedHeatNo(plnZgS.getFeedHeatNo());
		plnZgC.setCutNumOld(multipleNum);
		plnZgC.setLenGpSingleOld(String.valueOf(oldfixLenTake));
		plnZgC.setLengthMOld(String.valueOf(actLen));
		plnZgC.setPlanWeigth(String.valueOf(planInWt - cutWeight));
		plnZgC.setCutTarget(cutTarget);
		plnZgC.setCutLength(String.valueOf(cutLength));
		plnZgC.setCutWeight(String.valueOf(cutWeight));
		// 获取炉计划顺序号
		String heatPlanSqe = getHeatPlanSeq(con, plnZgS.getHeatPlanNo(), "D");
		plnZgC.setHeatPlanSqe(heatPlanSqe);
		// 新增
		String addPlnGxZg = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory
				.getSql("ComPlanFurnace.addPlnZyZgC"), plnZgC);
		ps = con.prepareStatement(addPlnGxZg);
		ps.executeUpdate();
		ps.close();

		if (plnZgS.getPlanstatus().equals("10")) {
			MilFeedPlanEntity milFeed = new MilFeedPlanEntity();
			milFeed.setFeedRowNo(plnZgC.getHeatPlanSqe());
			milFeed.setSeq("");
			milFeed.setLotNo(plnZgS.getZyBatchId());
			milFeed.setStoveNo(plnZgS.getStoveNo());
			milFeed.setJudgeStoveNo(plnZgS.getFeedHeatNo() + "-" + "01");
			milFeed.setPlineCode(plnZgS.getPlineCode());
			milFeed.setDbNum(multipleNum);
			milFeed.setMatHeight(String.valueOf(weightModl));
			milFeed.setMatTotalHeight(String.valueOf(planInWt));
			milFeed.setFeedNum(String.valueOf(planInNum));
			milFeed.setFeedLength(String.valueOf(actLen));
			milFeed.setUseLength(String.valueOf(actSingLen));
			milFeed.setCutTarget(cutTarget);
			milFeed.setCutLength(String.valueOf(cutLength));
			milFeed.setCutWeight(String.valueOf(cutWeight));
			milFeed.setRemark("");
			milFeed.setDbLength(plnZgS.getLenGpSingle());
			milFeed.setNonUseLength(String.valueOf(cutLength));
			milFeed.setPipeSection(plnZgS.getDiameter());
			milFeed.setDbHeight(plnZgS.getWtGpSingle());
			milFeed.setProPlanId(plnZgS.getProPlanId());
			milFeed.setGxPlanNo(plnZgS.getGxPlanNo());
			milFeed.setFeedNumPlan(String.valueOf(planInNum));
			milFeed.setCreateName(createName);
			milFeed.setCreateTime(createTime);
			// 新增上料行表
			String addSqlPls = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("FrmPlanStoveD.AddMilFeedPlan"), milFeed);
			ps = con.prepareStatement(addSqlPls);
			ps.executeUpdate();
			ps.close();
			// 修改炉计划接收表
			String sqlUpdate = CoreXmlSqlParsersFactory
					.getSql("ComPlanFurnace.updateMilPlan");
			ps = con.prepareStatement(sqlUpdate);
			ps.setObject(1, plnZgS.getHeatPlanNo());
			ps.setObject(2, plnZgS.getHeatPlanNo());
			ps.executeUpdate();
			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 singlCpWt = weightOne
				* (Double.parseDouble(plnZgS.getRollLength()) - cutHeadEnd);
		// 查询轧管信息
		PlnOrderZgSEntityL plnZgO = FrmPlanMaterialSelection.getPlanOrderZg(
				con, plnZgS.getProPlanId(), plnZgS.getGxPlanNo(),
				PlnOrderZgSEntityL.class);
		double singlReduWt = (Double.parseDouble(plnZgO.getGpreqWeight()) / Double
				.parseDouble(plnZgO.getWeigthS()))
				* singlCpWt;
		// 修改虚拟炉计划量
		String sqlUpdate = CoreXmlSqlParsersFactory
				.getSql("ComPlanFurnace.updatePlnZgM");
		ps = con.prepareStatement(sqlUpdate);
		ps.setObject(1, plnZgS.getHeatPlanNo());
		ps.setObject(2, singlCpWt);
		ps.setObject(3, singlReduWt);
		ps.setObject(4, plnZgS.getHeatPlanNo());
		ps.executeUpdate();
		ps.close();

		return true;
	}

	/**
	 * 查询坯子库存信息
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static List<MatGpMEntity> getQureyMatGpM(Connection con,
			String judgeStoveNo, String proPlanId, String gxPlanNo,
			String jugdeApplyCode, String jugdeApplySeq, String fixNum)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanMaterialSelection.QueryMatGp");
		sql += " AND T.PRO_PLAN_ID_ZG||'/'||T.GX_PLAN_NO_ZG ='" + proPlanId
				+ "/" + gxPlanNo + "'";
		sql += " AND T.JUGDE_APPLY_CODE='" + jugdeApplyCode + jugdeApplySeq
				+ "'";
		sql += " AND T.FIX_NUM=TO_NUMBER('" + fixNum + "')";
		sql += "  ORDER BY T.MAT_NO ASC)";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		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;
		}
	}

	/**
	 * 锭子委外二判时同时生成轧管虚拟，实际炉计划
	 * 
	 * @return
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static String addPlnDzOutZg(Connection con,
			QcmGpJudgeApplyEntity qcmGp, String createName, String createTime)
			throws SQLException, ParseException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		String mess = "";
		SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat dfMonth = new SimpleDateFormat("yyyyMM");
		String multipleNum = qcmGp.getMultipleNum();// 倍尺数
		int planInNum = Integer.parseInt(qcmGp.getActCount());
		int numC = Integer.parseInt(multipleNum) * planInNum;
		// 查询轧管信息
		PlnOrderZgSEntityL plnZgS = FrmPlanMaterialSelection.getPlanOrderZg(
				con, qcmGp.getProPlanIdNext(), qcmGp.getGxPlanNoNext(),
				PlnOrderZgSEntityL.class);
		if (plnZgS == null) {
			mess = "未找到轧管订单(" + qcmGp.getProPlanIdNext() + "/"
					+ qcmGp.getGxPlanNoNext() + ")信息!";
			return mess;
		}
		int diameter = Integer.parseInt(plnZgS.getDiameterGp());
		if (diameter == 251) {
			diameter = 250;
		}
		double mSingleWeight = (7.8 * Pi / 4 * diameter * diameter) / 1000 / 1000;// 米单重
		// 倍尺坯单支重=坯米单重*倍尺坯长/1000
		double weightM = mSingleWeight
				* (Double.parseDouble(qcmGp.getActLen()) / 1000);
		// 单倍坯重
		double weightGpOne = mSingleWeight
				* (Double.parseDouble(qcmGp.getLenGpSingle()) / 1000);
		List<MatGpMEntity> matGpMlist = getQureyMatGpM(con, qcmGp
				.getJudgeStoveNo(), qcmGp.getProPlanIdNext(), qcmGp
				.getGxPlanNoNext(), qcmGp.getJugdeApplyCode(), qcmGp
				.getJugdeApplySqe(), qcmGp.getMultipleNum());
		if (matGpMlist == null) {
			mess = "管坯物料已发生改变,请刷新!";
			return mess;
		}
		double actWt = 0;
		String furnaceno = "";
		String matOrigin = "";
		String inwlDesc = "";
		String flagStoveNo = "";
		for (MatGpMEntity matGpM : matGpMlist) {
			furnaceno = matGpM.getFurnaceno() + "/" + matGpM.getSequenceno();
			flagStoveNo = matGpM.getFlagStoveNo();
			actWt += Double.parseDouble(matGpM.getActWeight());
			matOrigin = matGpM.getMatOriginCode();
			inwlDesc = matGpM.getMaterialName();
			// // 修改坯子库存信息
			// String sqlUpdateMat = CoreXmlSqlParsersFactory
			// .getSql("ComPlanFurnace.UpdateMatGp");
			// ps = con.prepareStatement(sqlUpdateMat);
			// ps.setObject(1, "1");
			// ps.setObject(2, "80150103");
			// ps.setObject(3, matGpM.getMatNo());
			// ps.executeUpdate();
			// ps.close();
		}
		PlnZyZgMEntity plnZgM = new PlnZyZgMEntity();
		int countM = 0;
		String heatPlanNo = "";
		// 算预计成品吨
		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 singlCpWt = weightOne
				* (Double.parseDouble(plnZgS.getRollLength()) - cutHeadEnd);
		double singlReduWt = (Double.parseDouble(plnZgS.getGpreqWeight()) / Double
				.parseDouble(plnZgS.getWeigthS()))
				* singlCpWt;
		if (countM == 0) {
			heatPlanNo = corePlnZyFurnM.getMaxHeatPlanNo(con, qcmGp
					.getProPlanIdNext()
					+ "6", qcmGp.getProPlanIdNext(), "D");
			// 获取炉计划顺序号
			int gxProSeq = corePlnZyFurnM.getMaxGxPlnSeq(con, plnZgS
					.getPlineCode(), "D");
			plnZgM.setHeatPlanNo(heatPlanNo);
			plnZgM.setJudgeStoveNo(qcmGp.getJudgeStoveNo());
			plnZgM.setStoveNo(qcmGp.getStoveNo());
			plnZgM.setFlagStoveNo(flagStoveNo);
			plnZgM.setGxProSeq(String.valueOf(gxProSeq));
			plnZgM.setUpperHeatplanId(furnaceno);
			plnZgM.setPlanstatus("00");
			if (qcmGp.getProPlanId().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("E");
				} else if (plnZgS.getWholeBacklogDesc().contains("热处理")) {
					plnZgM.setNextProcessCode("F");
				} else if (plnZgS.getWholeBacklogDesc().contains("加工")) {
					plnZgM.setNextProcessCode("G");
				} else {
					if (plnZgS.getOrdLnDlyPk().contains("ZZBY-J")) {
						plnZgM.setNextProcessCode("H");
					}else{
						plnZgM.setNextProcessCode("");
					}
				}
				plnZgM.setIsendgx("0");
			} else {
				plnZgM.setNextProcessCode("");
				plnZgM.setIsendgx("1");
			}
			// 末工序获取去向
			// String gowWhere = ComPlanFurnace.getPlineCodeGroup(con, 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 = con.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.setIfMatchOdd("0");
			plnZgM.setIfDoHeatPlan("1");
			plnZgM.setProPlanId(plnZgS.getProPlanId());
			plnZgM.setGxPlanNo(plnZgS.getGxPlanNo());
			plnZgM.setCcmProPlanId(qcmGp.getProPlanId());
			plnZgM.setCcmGxPlanNo(qcmGp.getGxPlanNo());
			plnZgM.setGradecode(qcmGp.getGradecode());
			plnZgM.setGradename(qcmGp.getGradename());
			plnZgM.setSteelcode(plnZgS.getSteelcode());
			plnZgM.setSteelname(plnZgS.getSteelname());

			if (!plnZgS.getGradename().equals(qcmGp.getGradename())) {
				// 查询产出物料
				String[] bomMat = StoveHelper.getQueryMateratBom(con, qcmGp
						.getGradename(), 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.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.setAimoutdiameter(plnZgS.getAimoutdiameter());
			plnZgM.setAimwallthick(plnZgS.getAimwallthick());
			plnZgM.setThicknessWall(plnZgS.getThicknessWall());
			plnZgM.setLenCutend(plnZgS.getLenCutend());
			plnZgM.setLenCuthead(plnZgS.getLenCuthead());
			if (!plnZgS.getSiglLen().equals("")) {
				double lenMin = Double.parseDouble(plnZgS.getAimlength())
						- Double.parseDouble(plnZgS.getEndlenSpecstd());

				lenMin = (int) (Double.parseDouble(plnZgS.getAimlength()) / Double
						.parseDouble(plnZgS.getSiglLen()))
						* Double.parseDouble(plnZgS.getSiglLen());
				if (lenMin - Double.parseDouble(plnZgS.getLenRollMin()) < 0) {
					lenMin = Double.parseDouble(plnZgS.getLenRollMin());
				}

				double lenMax = lenMin
						+ Double.parseDouble(plnZgS.getEndlenSpecstd());
				// 判定是否除以单倍尺长可以整除
				plnZgM.setLenRollMax(String.valueOf(lenMax));
				plnZgM.setLenRollMin(String.valueOf(lenMin));
			} else {
				plnZgM.setLenRollMax(plnZgS.getLenRollMax());
				plnZgM.setLenRollMin(plnZgS.getLenRollMin());
			}

			plnZgM.setOutnumCut(plnZgS.getOutnumCut());
			plnZgM.setAimlength(plnZgS.getAimlength());
			plnZgM.setRollLength(plnZgS.getRollLength());
			plnZgM.setLenGpSingle(plnZgS.getLenGpSingle());
			plnZgM.setPlnInsingleNum(String.valueOf(numC));
			plnZgM.setPlnInsingleWt(qcmGp.getActTheoryWeight());
			plnZgM.setPlnOutsingleNum(String.valueOf(numC
					* Integer.parseInt(plnZgS.getOutnumCut())));
			plnZgM.setPlnOutsingleW(String.valueOf(singlCpWt * numC));
			plnZgM.setReducedWt(String.valueOf(singlReduWt * numC));
			plnZgM.setLenGpSingle(qcmGp.getLenGpSingle());
			plnZgM.setDiameter(plnZgS.getDiameterGp());
			plnZgM.setWtGpSingle(String.valueOf(weightGpOne));
			plnZgM.setCic(plnZgS.getCicGp());
			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(createTime)));
			plnZgM.setBalYearMonth(dfMonth.format(dfMonth.parse(createTime)));
			plnZgM.setCreateName(createName);
			plnZgM.setCreateTime(createTime);
			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("2");
			if (matOrigin.equals("801301")) {
				plnZgM.setOutsBilletFl("1");// 外购
			} else {
				plnZgM.setOutsBilletFl("0");// 自炼
			}
			plnZgM.setPlnType("0");
			// 新增轧管虚拟炉计划
			String sqlZgM = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory
					.getSql("FrmPlanMaterialSelection.addPlnZyZgM"), plnZgM);
			ps = con.prepareStatement(sqlZgM);
			ps.executeUpdate();
			ps.close();
		}
		int countC = 0;
		String heatPlanSqe = "";
		// 判定是否存在相同坯长，倍尺，单倍坯长，判定炉号，判定单号，同炉计划ID数据
		String sqlC = "SELECT COUNT(*) COUNT1,T.HEAT_PLAN_SQE FROM PLN_ZY_ZG_C T WHERE "
				+ "T.HEAT_PLAN_NO=? AND T.JUGDE_APPLY_CODE=? AND T.JUDGE_STOVE_NO=? "
				+ "AND T.CUT_NUM_OLD=? AND T.LENGTH_M_OLD=? AND T.LEN_GP_SINGLE_OLD=? GROUP BY T.HEAT_PLAN_SQE";
		ps = con.prepareStatement(sqlC);
		ps.setObject(1, heatPlanNo);
		ps.setObject(2, qcmGp.getJugdeApplyCode() + qcmGp.getJugdeApplySqe());
		ps.setObject(3, qcmGp.getJudgeStoveNo());
		ps.setObject(4, qcmGp.getMultipleNum());
		ps.setObject(5, qcmGp.getActLen());
		ps.setObject(6, qcmGp.getLenGpSingle());
		rs = ps.executeQuery();
		if (rs.next()) {
			countC = rs.getInt("COUNT1");
			heatPlanSqe = rs.getString("HEAT_PLAN_SQE");
		}
		rs.close();
		ps.close();
		if (countC == 0) {
			PlnZyZgCEntity plnZgC = new PlnZyZgCEntity();
			plnZgC.setHeatPlanNo(heatPlanNo);
			plnZgC.setJugdeApplyCode(qcmGp.getJugdeApplyCode()
					+ qcmGp.getJugdeApplySqe());
			plnZgC.setStoveNo(qcmGp.getStoveNo());
			plnZgC.setJudgeStoveNo(qcmGp.getJudgeStoveNo());
			plnZgC.setFlagStoveNo(flagStoveNo);
			plnZgC.setProPlanId(plnZgS.getProPlanId());
			plnZgC.setGxPlanNo(plnZgS.getGxPlanNo());
			plnZgC.setPlineCode(plnZgS.getPlineCode());
			plnZgC.setPlineName(plnZgS.getPlineName());
			plnZgC.setLenGpSingle(qcmGp.getLenGpSingle());
			plnZgC.setDiameter(plnZgS.getDiameterGp());
			plnZgC.setWtGpSingle(String.valueOf(weightGpOne));
			plnZgC.setCutNumGp(qcmGp.getMultipleNum());
			plnZgC.setLengthM(qcmGp.getActLen());
			plnZgC.setWeightM(String.valueOf(weightM));
			plnZgC.setPlanIndoubleNum(qcmGp.getActCount());
			plnZgC.setPlanIndoubleWt(qcmGp.getActTheoryWeight());
			plnZgC.setCreateName(createName);
			plnZgC.setCreateTime(createTime);
			plnZgC.setCutNumOld(qcmGp.getMultipleNum());
			plnZgC.setLenGpSingleOld(qcmGp.getLenGpSingle());
			plnZgC.setLengthMOld(qcmGp.getActLen());
			plnZgC.setPlanWeigth(qcmGp.getActTheoryWeight());
			// 获取炉计划顺序号
			heatPlanSqe = corePlnZyFurnM.getHeatPlanSeq(con, heatPlanNo, "D");
			plnZgC.setHeatPlanSqe(heatPlanSqe);
			// 新增轧管实际炉计划
			String sqlZgC = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory
					.getSql("FrmPlanMaterialSelection.addPlnZyZgC"), plnZgC);
			ps = con.prepareStatement(sqlZgC);
			ps.executeUpdate();
			ps.close();
		}
		if (countM > 0) {
			// 修改炉计划主表量
			String sqlUpdate = CoreXmlSqlParsersFactory
					.getSql("ComPlanFurnace.updatePlnZgM");
			ps = con.prepareStatement(sqlUpdate);
			ps.setObject(1, heatPlanNo);
			ps.setObject(2, singlCpWt);
			ps.setObject(3, singlReduWt);
			ps.setObject(4, heatPlanNo);
			ps.executeUpdate();
			ps.close();
		}
		// 回写库存下工序炉计划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 = con.prepareStatement(sqlUpdate);
		ps.setObject(1, heatPlanNo);
		ps.setObject(2, qcmGp.getJudgeStoveNo());
		ps.setObject(3, plnZgS.getProPlanId());
		ps.setObject(4, plnZgS.getGxPlanNo());
		ps.executeUpdate();
		ps.close();
		// 回写申请判定下工序炉计划Id
		sqlUpdate = "UPDATE QCM_GP_JUDGE_APPLY T SET T.HEAT_PLAN_NO_NEXT=? WHERE T.JUDGE_STOVE_NO=? AND T.PRO_PLAN_ID_NEXT=? AND T.GX_PLAN_NO_NEXT=?";
		ps = con.prepareStatement(sqlUpdate);
		ps.setObject(1, heatPlanNo);
		ps.setObject(2, qcmGp.getJudgeStoveNo());
		ps.setObject(3, plnZgS.getProPlanId());
		ps.setObject(4, plnZgS.getGxPlanNo());
		ps.executeUpdate();
		ps.close();
		return mess;
	}
}
