package com.steering.pss.plnsaleord.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.plnsaleord.GpOptimize.CorePlnZgMStoGp;
import com.steering.pss.plnsaleord.GpOptimize.model.PlnGxCastMEntity;
import com.steering.pss.plnsaleord.lenthCal.PlnZglenthCal;
import com.steering.pss.plnsaleord.ordAmCal.PlnZgProcCal;
import com.steering.pss.plnsaleord.ordAmCal.model.InZgOrdCalEntity;
import com.steering.pss.plnsaleord.ordAmCal.model.OutZgOrdCalEntity;
import com.steering.pss.plnsaleord.order.baseMethod.CoreTolerOfNk;
import com.steering.pss.plnsaleord.order.model.*;
import com.steering.pss.plnsaleord.processOrder.CorePlnOrdProUseTime;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;

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.util.ArrayList;
import java.util.List;

/**
 * 销售合同下发工序合同
 * 
 * @author tjg
 * @version 1.1
 */
public class CorePlnProcessOrd extends DBCommon {

	private int errCode = 0;
	private String errMessage = "";

	/**
	 * 创建工序合同
	 * 
	 * @param conn
	 * @param name
	 *            下发人
	 * @param _ordLnDlyPk
	 *            交货行pk
	 * @param divideId
	 *            排产流水号
	 * @param versionNo
	 *            下发版次
	 * @throws SQLException
	 */
	public boolean addPrcOrder(Connection conn, String name,
			String _ordLnDlyPk, String divideId, String versionNo,
			String _orderNoSeq, String _orderSource) throws SQLException {
		PreparedStatement psta = null;
		PlnOrdInfo ordInfo = new PlnOrdInfo();
		double planPMain = 0;
		ordInfo.setOrdLnDlyPk(_ordLnDlyPk);
		ordInfo.setCreateName(name);
		ordInfo.setPlnDivideId(divideId);
		String groupJgx = "";
		String groupRcl = "";
		// 修改版次号 2016/07/26
		CorePlnProcessOrd.updatePlnSaleordProcVersionNo(conn, _ordLnDlyPk,
				divideId, versionNo, name, "0");
		// 新增版次号从表
		PlnSaleordEntityA entityA = CorePlnProcessOrd.getPlnSaleordEntityById(
				conn, _ordLnDlyPk, divideId);
		if (entityA != null) {
			PlnSaleordEntity saleordEntity = CorePlnProcessOrd
					.getPlnSaleordByPk(conn, _ordLnDlyPk);
			String rollWellMonth = "";
			String acceptNo = "";
			String ifPassZg = "";
			String ifPassJgx = "";
			if (saleordEntity != null) {
				rollWellMonth = saleordEntity.getRollWellMonth();
				acceptNo = saleordEntity.getAcceptNo();
			}
			if (entityA.getDIsOk().equals("") || entityA.getDIsOk().equals("2")) {
				ifPassZg = "0";
			} else {
				ifPassZg = "1";
			}
			if (entityA.getGIsOk().equals("") || entityA.getGIsOk().equals("2")) {
				ifPassJgx = "0";
			} else {
				ifPassJgx = "1";
			}
			CorePlnProcessOrd.addPlnOrderDownLog(conn, versionNo, _ordLnDlyPk,
					divideId, "0", entityA.getOrderSource(), entityA
							.getOrderNo(), entityA.getOrderSeq(), entityA
							.getDeliveryNo(), entityA.getRNumId(), entityA
							.getProcessSeq(), name, rollWellMonth, acceptNo,
					ifPassZg, ifPassJgx);
		}
		String cutHeadDbk = "";
		String cutendDbk = "";
		String longerDbk = "";
		String gradeCode = "";
		String specNameZz="";
		String specCodeZz="";
		String gradeName="";
		String craftNoFile= "";
		String craftNo="";
		// 查询合同信息
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.querySaleProcStatus");
		psta = conn.prepareStatement(sql);
		psta.setString(1, _ordLnDlyPk);
		psta.setString(2, divideId);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			gradeCode = rs.getString("GRADECODE");
			gradeName = rs.getString("GRADENAME");
			specCodeZz = rs.getString("SPEC_CODE");
			specNameZz = rs.getString("SPEC_NAME");
			planPMain = rs.getDouble("PLAN_P_MAIN");
			craftNoFile= rs.getString("CRAFT_FILE_NO");
			craftNo= rs.getString("CRAFT_SEQ");
			ordInfo.setOrderNo(rs.getString("ORDER_NO"));
			ordInfo.setOrderSeq(rs.getString("ORDER_SEQ"));
			ordInfo.setPlanWtD(rs.getDouble("PLAN_P_MAIN"));
			ordInfo.setPlanNumD(rs.getInt("PLAN_NUM_D"));
			ordInfo.setOutMultD(rs.getInt("OUT_MULT_D"));
			ordInfo.setGradecodeAll(rs.getString("GRADECODE_ALL"));
			if (planPMain == 0) {
				return true;
			}
			if (planPMain < 0) {
				errMessage = "下发计划量必须大于零。";
				return false;
			}
			if (rs.getString("INDEX_SEQ") == null) {
				errMessage = "未找到制程索引号。";
				return false;
			}
			if (rs.getString("DESGIN_KEY") == null) {
				errMessage = "未找到质量设计（DesginKey）。";
				return false;
			}
			if (rs.getString("BOM") == null) {
				errMessage = "未找到BOM号。";
				return false;
			}
			if (rs.getString("ROLL_MONTH") == null) {
				errMessage = "没有排产月份。";
				return false;
			}
			cutHeadDbk = rs.getString("LEN_CUTHEAD_DBK");
			cutendDbk = rs.getString("LEN_CUTEND_DBK");
			longerDbk = rs.getString("LEN_LONGER_DBK");
			ordInfo.setAddDbkLen(rs.getDouble("LEN_CUTHEAD_DBK")
					+ rs.getDouble("LEN_CUTEND_DBK")); //镦粗切头+切尾
			ordInfo.setAddDcLen(rs.getDouble("LEN_LONGER_DBK"));//镦粗加长
			ordInfo.setLenCutheadDbk(cutHeadDbk);
			ordInfo.setLenCutendCbk(cutendDbk);
			ordInfo.setRatioDbk(rs.getDouble("RATIO_DBK"));//变径率
			ordInfo.setTotlength(rs.getDouble("TOTLENGTH"));
			ordInfo.setTotlengthUnit(rs.getString("TOTLENGTH_UNIT"));
			ordInfo.setAbc(rs.getString("ABC"));
			ordInfo.setOutputStdNo(rs.getString("OUTPUT_STD_NO"));
			ordInfo.setUseDesc(rs.getString("USE_DESC"));
			ordInfo.setStdName(rs.getString("STD_NAME"));
			ordInfo.setRollMonth(rs.getString("ROLL_MONTH"));
			ordInfo.setEndlenSpecstd(rs.getDouble("ENDLEN_SPECSTD"));
			ordInfo.setPlanPMainMin(rs.getDouble("PLAN_P_MAIN_MIN"));
			ordInfo.setAvgLenMin(rs.getDouble("AVG_LEN_MIN"));
			ordInfo.setMsc(rs.getString("MSC"));
			ordInfo.setIndexSeq(rs.getString("INDEX_SEQ"));
			ordInfo.setVersionNo(versionNo);
			ordInfo.setDesginKey(rs.getString("DESGIN_KEY"));
			ordInfo.setBom(rs.getString("BOM"));
			ordInfo.setOutdiameter(rs.getDouble("OUTDIAMETER"));
			ordInfo.setWallthick(rs.getDouble("WALLTHICK"));
			ordInfo.setAimLenRoll(rs.getDouble("AIM_LEN_ROLL"));
			ordInfo.setwJ(rs.getDouble("WEIGHT_PER_M"));
			ordInfo.setLengthmax(rs.getDouble("LENGTHMAX"));
			ordInfo.setLengthmin(rs.getDouble("LENGTHMIN"));
			ordInfo.setSiglLen(rs.getDouble("SIGL_LEN"));
			ordInfo.setShortest(rs.getDouble("SHORTEST"));
			ordInfo.setCheckJVal(rs.getDouble("CHECK_J_VAL"));
			ordInfo.setAddInspect(rs.getDouble("INSPECTION_VALUE"));
			ordInfo.setLenRollMin(rs.getDouble("LEN_ROLL_MIN"));
			ordInfo.setLenRollMax(rs.getDouble("LEN_ROLL_MAX"));
			ordInfo.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
			ordInfo.setOrderUnit(rs.getString("ORDER_UNIT"));
			ordInfo.setPlineName(rs.getString("GROUP_ROLL_NAME"));
			ordInfo.setProduceSort(rs.getString("PRODUCE_SORT"));
			ordInfo.setOrderSource(rs.getString("ORDER_SOURCE"));
			ordInfo.setLenScope(rs.getDouble("LEN_SCOPE"));//长度范围
			if (rs.getString("ORDER_UNIT") != null) {
				ordInfo.setOrderUnit(rs.getString("ORDER_UNIT"));
			} else {
				errMessage = "订货单位未知，请核对合同信息。";
				return false;
			}
			if (rs.getString("ORDER_UNIT") != null
					&& (rs.getString("ORDER_UNIT").trim().equals("吨") || rs
							.getString("ORDER_UNIT").trim().equals("英磅"))) {
				if (rs.getString("WEIGH_TYPE") != null
						&& rs.getString("WEIGH_TYPE").contains("理")) {
					ordInfo.setWeighType("0");
				} else {
					ordInfo.setWeighType("1");
				}
			} else {
				ordInfo.setWeighType("-1");
			}
			if (rs.getString("SHRT_STOVE") != null) {
				ordInfo.setShrtStove(rs.getString("SHRT_STOVE"));
			}
			if (rs.getString("PRDCR_NO") != null) {
				ordInfo.setPrdcrNo(rs.getString("PRDCR_NO"));
			}
			if (rs.getString("GROUP_ROLL") != null) {
				ordInfo.setPlineD(rs.getString("GROUP_ROLL"));
			}
			if (rs.getString("MODEL_DESC") != null) {
				ordInfo.setModelDesc(rs.getString("MODEL_DESC"));
			}
			if (rs.getString("GROUP_JGX") != null) {
				groupJgx = rs.getString("GROUP_JGX");
			}
			if (rs.getString("GROUP_RCL") != null) {
				groupRcl = rs.getString("GROUP_RCL");
			}
			if (rs.getString("OUTS_STL_PIPE_FL") != null) {
				ordInfo.setOutsStlPipeFl(rs.getString("OUTS_STL_PIPE_FL"));
			}
			if (rs.getString("OUTS_BILLET_FL") != null) {
				if (rs.getString("OUTS_BILLET_FL").equals("121801")) {
					ordInfo.setOutsBilletFl("1");// 外购
				} else {
					ordInfo.setOutsBilletFl("0");// 自炼
				}
			}else
			{
				ordInfo.setOutsBilletFl("0");// 自炼
			}
		} else {
			errMessage = "未找到合同信息。";
			return false;
		}
		rs.close();
		psta.close();

		// 查询制程
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryDesginMscpl");
		psta = conn.prepareStatement(sql);
		psta.setString(1, ordInfo.getDesginKey());
		psta.setString(2, ordInfo.getIndexSeq());
		rs = psta.executeQuery();
		if (rs.next()) {
			if (rs.getString("WHOLE_BACKLOG") == null) {
				errMessage = "未找到制程经过工序点。";
				return false;
			}
			// if (ordInfo.getOrderUnit().equals("支")
			// && !rs.getString("WHOLE_BACKLOG").trim().equals("G")) {
			// errMessage = "制程有误。";
			// return false;
			// }
			ordInfo.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
			ordInfo.setWholeBacklogDesc(rs.getString("WHOLE_BACKLOG_DESC"));
		} else {
			errMessage = "未找到制程信息。";
			return false;
		}
		rs.close();
		psta.close();

		// 查询监制工序
		// if (!ordInfo.getPrdcrNo().equals("")) {
		String blType = "";
		if (_orderSource.equals("备料需求")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryPorderBlReq");
			psta = conn.prepareStatement(sql);
			psta.setString(1, _orderNoSeq);
			rs = psta.executeQuery();
			if (rs.next()) {
				blType = rs.getString("BLTYPE");
			}
			rs.close();
			psta.close();
			// ordInfo.setOutsBilletFl("0");// 自炼
		}
		if (blType.equals("接箍料")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryOrderLinePrdcrLst1");
			psta = conn.prepareStatement(sql);
			psta.setString(1, ordInfo.getPrdcrNo());
			psta.setString(2, ordInfo.getIndexSeq());
			rs = psta.executeQuery();
			if (rs.next()) {
				if (rs.getString("PRDCR_PRC") != null&&!rs.getString("PRDCR_PRC").equals("")) {
					ordInfo.setPrdcrPrc(rs.getString("PRDCR_PRC"));
				}
			}
			rs.close();
			psta.close();

			// 生成接箍排产订单
			String mess = CorePlnOrderJgS.addOrderJgS(conn, ordInfo, gradeCode,
					planPMain);
			if (!mess.equals("")) {
				errMessage = mess;
				return false;
			}
		} else if (blType.equals("短节备料")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryOrderLinePrdcrLst2");
			psta = conn.prepareStatement(sql);
			psta.setString(1, ordInfo.getPrdcrNo());
			psta.setString(2, ordInfo.getIndexSeq());
			rs = psta.executeQuery();
			if (rs.next()) {
				if (rs.getString("PRDCR_PRC") != null&&!rs.getString("PRDCR_PRC").equals("")) {
					ordInfo.setPrdcrPrc(rs.getString("PRDCR_PRC"));
				}
			}
			rs.close();
			psta.close();

		} else {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryOrderLinePrdcrLst");
			psta = conn.prepareStatement(sql);
			psta.setString(1, ordInfo.getPrdcrNo());
			psta.setString(2, ordInfo.getIndexSeq());
			rs = psta.executeQuery();
			if (rs.next()) {
				if (rs.getString("PRDCR_PRC") != null&&!rs.getString("PRDCR_PRC").equals("")) {
					ordInfo.setPrdcrPrc(rs.getString("PRDCR_PRC"));
				}
			}
			rs.close();
			psta.close();
		}
		// }

		// 订单流水号
		ordInfo.setSerialNum(CorePlnGxOrdM.createGxMNo(conn));
		ordInfo.setVersionNo(versionNo);

		// 获取轧管加长原则，转移长度上下限，加工分切,控制外径壁厚 ，管单重
		ordInfo = PlnZglenthCal.calcPlnInfo(conn, ordInfo);
		if (ordInfo.getErrCode() == -1) {
			ordInfo.setErrCode(-1);
			errMessage = ordInfo.getErrMessage();
			return false;
		}

		// 经过工序倒推
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryMscplPrc");
		psta = conn.prepareStatement(sql);
		psta.setString(1, ordInfo.getDesginKey());
		psta.setString(2, ordInfo.getIndexSeq());
		rs = psta.executeQuery();
		while (rs.next()) {
			if (rs.getString("PROCESS_CODE") == null) {
				errMessage = "工序点代码为空。";
				return false;
			}
			if (rs.getString("PROCESS_CODE").trim().equals("G")) {
				PlnOrderJgxEntity jgxEntity = new PlnOrderJgxEntity();
				jgxEntity.setVersionNo(versionNo);// 版本号
				jgxEntity.setBacklogSeq(String
						.valueOf(rs.getInt("BACKLOG_SEQ")));
				jgxEntity.setGroupJgx(groupJgx);
				if (rs.getString("PROCESS_CODE_C") != null) {
					jgxEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					jgxEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					jgxEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				if (rs.getString("PREVIOUS_GX") != null) {
					jgxEntity.setPreviousGx(rs.getString("PREVIOUS_GX"));
				}
				if (rs.getString("PREVIOUS_GX_CODE") != null) {
					jgxEntity.setPreviousGxCode(rs
							.getString("PREVIOUS_GX_CODE"));
				}
				planPMain = addJgxPrcOrder(jgxEntity, ordInfo, planPMain, conn);
				if (errCode == -1) {
					return false;
				}
			}
			if (rs.getString("PROCESS_CODE").trim().equals("F")
					&& !ordInfo.getOrderUnit().equals("支")) {
				PlnOrderRclEntity rclEntity = new PlnOrderRclEntity();
				rclEntity.setVersionNo(versionNo);
				rclEntity.setBacklogSeq(String
						.valueOf(rs.getInt("BACKLOG_SEQ")));
				rclEntity.setGroupRcl(groupRcl);
				if (rs.getString("PROCESS_CODE_C") != null) {
					rclEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					rclEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					rclEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				if (rs.getString("PREVIOUS_GX") != null) {
					rclEntity.setPreviousGx(rs.getString("PREVIOUS_GX"));
				}
				if (rs.getString("PREVIOUS_GX_CODE") != null) {
					rclEntity.setPreviousGxCode(rs
							.getString("PREVIOUS_GX_CODE"));
				}
				planPMain = addRclPrcOrder(rclEntity, ordInfo, planPMain, conn);
				if (errCode == -1) {
					return false;
				}
			}
			if (rs.getString("PROCESS_CODE").trim().equals("E")
					&& !ordInfo.getOrderUnit().equals("支")) {
				PlnOrderDbkEntity dbkEntity = new PlnOrderDbkEntity();
				dbkEntity.setVersionNo(versionNo);
				dbkEntity.setBacklogSeq(String
						.valueOf(rs.getInt("BACKLOG_SEQ")));
				if (rs.getString("PROCESS_CODE_C") != null) {
					dbkEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					dbkEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					dbkEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				if (rs.getString("PREVIOUS_GX") != null) {
					dbkEntity.setPreviousGx(rs.getString("PREVIOUS_GX"));
				}
				if (rs.getString("PREVIOUS_GX_CODE") != null) {
					dbkEntity.setPreviousGxCode(rs
							.getString("PREVIOUS_GX_CODE"));
				}
				planPMain = addDbkPrcOrder(dbkEntity, ordInfo, planPMain, conn);
				if (errCode == -1) {
					return false;
				}
			}
			//&& !ordInfo.getPlineD().equals("C072")
 			if (rs.getString("PROCESS_CODE").trim().equals("D")
					&& !ordInfo.getOrderUnit().equals("支")) {
				PlnOrderZgEntity zgEntity = new PlnOrderZgEntity();
				zgEntity.setVersionNo(versionNo);
				String sic = "";
				if (rs.getString("SIC") != null) {
					String[] strs = rs.getString("SIC").split(",");
					sic = strs[0].toString().trim();
				}
				if (sic.equals("")) {
					errMessage = "无内控壁厚公差索引码,请联系工艺人员。";
					return false;
				}
				zgEntity
						.setBacklogSeq(String.valueOf(rs.getInt("BACKLOG_SEQ")));
				if (rs.getString("PROCESS_CODE_C") != null) {
					zgEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					zgEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					zgEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				// 获取轧管加长原则，转移长度上下限，加工分切,控制外径壁厚 ，管单重
				ordInfo = PlnZglenthCal.calcPlnInfo(conn, ordInfo);
				if (ordInfo.getErrCode() == -1) {
					errMessage = ordInfo.getErrMessage();
					return false;
				}
				planPMain = addZgPrcOrder(zgEntity, ordInfo, planPMain, sic,
						conn);
				if (errCode == -1) {
					return false;
				}
			}
			if(ordInfo.getProduceSort().equals("商品坯")){
				 if (rs.getString("PROCESS_CODE").trim().equals("C")) {
					 
				 }
				if (rs.getString("PROCESS_CODE").trim().equals("B")) {
					String gpReqId =ordInfo.getOrderNo()+'/'+ordInfo.getOrderSeq();
					// 获取管坯需求号
//					sql = CoreXmlSqlParsersFactory
//							.getSql("CorePlnProcessOrd.getGpReqinfId");
//					PreparedStatement ps = conn.prepareStatement(sql);
//					ps.setObject(1, "GP_S");
//					ResultSet rs1 = ps.executeQuery();
//					if (rs1.next()) {
//						if (rs1.getString("PRO_PLAN_ID") != null) {
//							gpReqId= rs1.getString("PRO_PLAN_ID");
//						}
//					} else {
//						errMessage = "生产铸造订单号出错！。";
//						return false;
//					}
//					rs1.close();
//					ps.close();
					ordInfo.setGpreqId(gpReqId);
					String sqlAddCast = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.addPlnGxCastM");
					PlnGxCastMEntity gxCastMEntity = new PlnGxCastMEntity();
					String plineCodeMay = "";
					String plineNameMay = "";
					String sqlAddZz = "";
			    	String	outMaterialNo = "PA" + specCodeZz + gradeCode;
			    	String  outMaterialNm = "PA" + specNameZz + "," + gradeName;
					// 管坯是铸坯，新增连铸坯订单
					String sqlPl = CoreXmlSqlParsersFactory
							.getSql("CorePlnZgMStoGp.selectProcessBPline1");
					sqlPl += " ORDER BY T.PLINE_LEVEL ASC, T.PRD_RATE DESC, T.PLINE_CODE ASC";
					PreparedStatement ps = conn.prepareStatement(sqlPl);
					ps.setObject(1, gradeCode);
					ps.setObject(2,outMaterialNo);
					ResultSet rs1 = ps.executeQuery();
					while (rs1.next()) {
						plineCodeMay += rs1.getString("PLINE_CODE") + ",";
						plineNameMay += rs1.getString("PLINE_NAME") + ",";
					}
					rs1.close();
					ps.close();
					if (plineCodeMay.length() > 0 && plineCodeMay.contains(",")) {
						plineCodeMay = plineCodeMay.substring(0,
								plineCodeMay.lastIndexOf(","));
					}
					if (plineNameMay.length() > 0 && plineNameMay.contains(",")) {
						plineNameMay = plineNameMay.substring(0,
								plineNameMay.lastIndexOf(","));
					}
					if (!plineCodeMay.equals("")) {
						String[] strs = plineCodeMay.split(",");
						String[] strs1 = plineNameMay.split(",");
						for(int i = 0;i<strs.length;i++)
						{
							if(!strs[i].toString().equals("C063") && !strs[i].equals("C064"))
							{
								gxCastMEntity.setPlineCode(strs[i].toString().trim());
								gxCastMEntity.setPlineName(strs1[i].toString().trim());
								break;
							}
						}



					}
//					if (!plineNameMay.equals("")) {
//						String[] strs = plineNameMay.split(",");
//						gxCastMEntity.setPlineName(strs[0].toString().trim());
//					}
					gxCastMEntity.setProPlanId(gpReqId);
					gxCastMEntity.setGptype("1");
					gxCastMEntity.setCastingtype("1");
					gxCastMEntity.setPlineCodeMay(plineCodeMay);
					gxCastMEntity.setPlineNameMay(plineNameMay);
					gxCastMEntity.setGradecode(gradeCode);
					gxCastMEntity.setGradename(gradeName);
					// if(gpReqEntity.getCastingtype().equals("1")){//连铸
					gxCastMEntity.setDiameterLz(String.valueOf((int)Double.parseDouble(specNameZz)));
					// }else{
					// gxCastMEntity.setDiameterLz(gpReqEntity.getDiameterZz());
					// }
					if(ordInfo.getSiglLen() > 0)
					{

					}else
					{
						errMessage = "单倍尺长需要大于0";
						return false;
					}
					int cutNum=(int)(ordInfo.getLengthmax()/ordInfo.getSiglLen());
					double cutLenBurn = (cutNum - 1) * 10;
					gxCastMEntity.setLenGpSingle(String.valueOf(ordInfo.getSiglLen()*1000));
					gxCastMEntity.setCutNumGp(String.valueOf(cutNum));
					gxCastMEntity.setCutLoseLen(String.valueOf(cutLenBurn));
					gxCastMEntity.setLengthLz(String.valueOf((ordInfo.getLengthmax()-ordInfo.getEndlenSpecstd()) * 1000));
					gxCastMEntity.setMaterialNo(outMaterialNo);
					gxCastMEntity
							.setMaterialName(outMaterialNm);
					
					
					//查询CIC
			    	String 	sqlCIc = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.queryPlnGxCastCIC");
				    ps = conn.prepareStatement(sqlCIc);
					ps.setObject(1, ordInfo.getOrdLnDlyPk());
					rs1 = ps.executeQuery();
					if (rs1.next()) {
						gxCastMEntity.setCic(rs1.getString("CIC"));
						gxCastMEntity.setPic(rs1.getString("PIC"));
						gxCastMEntity.setSic(rs1.getString("SIC"));
						gxCastMEntity.setDic(rs1.getString("DIC"));
						gxCastMEntity.setWic(rs1.getString("WIC"));
						gxCastMEntity.setGic(rs1.getString("GIC"));
					}
					rs1.close();
					ps.close();	
					if(ordInfo.getPrdcrNo().equals("")){
						gxCastMEntity.setIfMonitorLg("0");
					}else{
						gxCastMEntity.setIfMonitorLg("1");
					}					
					gxCastMEntity.setPrdcrNo(ordInfo.getPrdcrNo());
				    double Pi = 3.1415926; // 圆周率
					double dzSWtLongN = 7.8 * Pi * Double.parseDouble(specNameZz) * (Double.parseDouble(specNameZz) / 4)
							*(ordInfo.getSiglLen()*1000) / 1000 / 1000 / 1000;
					double lzDiamWtSingle = dzSWtLongN / (1 - 0 / 100);//0--烧损 默认0
					// 查询炼钢监制点
					String monitPoint = CorePlnZgMStoGp.getMonitorPoint(conn,
							ordInfo.getPrdcrNo(),ordInfo.getIndexSeq(), "A");
					gxCastMEntity.setMonitorPoint(monitPoint);
					gxCastMEntity.setWtCastOne(String.valueOf(lzDiamWtSingle));
					gxCastMEntity.setPlanWt(String.valueOf(ordInfo.getPlanWtD()));
					gxCastMEntity.setPlanNum(String.valueOf(ordInfo.getPlanNumD()));
					gxCastMEntity.setReqStatus("1");
					gxCastMEntity.setCreateName(name);
					sqlAddZz = EntityTool.fillSqlByEntity(sqlAddCast,
							gxCastMEntity);
					ps = conn.prepareStatement(sqlAddZz);
					ps.executeUpdate();
					ps.close();
					String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
					
					CorePlnOrdProUseTime cuseTime = new CorePlnOrdProUseTime();
					// 工序排产
					if (cuseTime.addOrderCastS(conn, gpReqId,
							name,
							ordInfo.getRollMonth(),
							dateTime,
							gxCastMEntity.getPlineCode(),
							gxCastMEntity.getPlineName(),
							String.valueOf(ordInfo.getPlanPMainMin()),
							"",
							"")) {

					} else {
						errMessage = cuseTime.getVerrMsg();
						return false;
					}
					///修改分解表管坯需求号
					String update="UPDATE PLN_SALEORD_PROC T SET  T.GPREQ_ID=?  WHERE T.ORD_LN_DLY_PK=? AND T.PLN_DIVIDE_ID=?";
					ps=conn.prepareStatement(update);
					ps.setObject(1, gpReqId);
					ps.setObject(2, ordInfo.getOrdLnDlyPk());
					ps.setObject(3, ordInfo.getPlnDivideId());
					ps.executeUpdate();
					ps.close();
					PlnOrderZgSEntity zgSEntity = new PlnOrderZgSEntity();
					zgSEntity.setProPlanId(gpReqId);
					zgSEntity.setGxPlanNo("1");
					zgSEntity.setOrdLnDlyPk(ordInfo.getOrdLnDlyPk());
					zgSEntity.setPlanGroupId("");
					zgSEntity.setPlnDivideId(ordInfo.getPlnDivideId());
					zgSEntity.setOrderNo(ordInfo.getOrderNo());
					zgSEntity.setOrderSeq(ordInfo.getOrderSeq());
					zgSEntity.setOrderSource(ordInfo.getOrderSource());
					zgSEntity.setDeliveryNo("001");
					zgSEntity.setGradecode(gradeCode);
					zgSEntity.setGradename(gradeName);
					zgSEntity.setCic(gxCastMEntity.getCic());
					zgSEntity.setCicGp(gxCastMEntity.getCic());
					zgSEntity.setCicGroupGp(gxCastMEntity.getCic());
					zgSEntity.setGpreqId(gpReqId);
					zgSEntity.setMsc(ordInfo.getMsc());
					zgSEntity.setIndexSeq(ordInfo.getIndexSeq());
					zgSEntity.setCraftFileNo(craftNoFile);
					zgSEntity.setCraftSeq(craftNo);
					//新增轧管S表
					String addZgS= EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory
							.getSql("CorePlnOrdProUseTime.addOrderZgSP"),zgSEntity);
					ps = conn.prepareStatement(addZgS);
					ps.executeUpdate();
					ps.close();
				}
			}
			/*
			 * if (rs.getString("PROCESS_CODE").trim().equals("C")) {
			 * PlnOrderDzEntity dzEntity = new PlnOrderDzEntity();
			 * dzEntity.setBacklogSeq(String.valueOf(rs.getInt("BACKLOG_SEQ")));
			 * planPMain = addDzPrcOrder(dzEntity, ordInfo, planPMain, conn); if
			 * (errCode == -1) { return false; } }
			 */
		}

		rs.close();
		psta.close();
		return true;
	}

	/**
	 * 创建工序合同
	 * 
	 * @param conn
	 * @param name
	 *            下发人
	 * @param _ordLnDlyPk
	 *            交货行pk
	 * @param divideId
	 *            排产流水号
	 * @param _processCode
	 *            截止工序
	 * @param versionNo
	 *            下发版次
	 * @throws SQLException
	 */
	public boolean addPrcOrder1(Connection conn, String name,
			String _ordLnDlyPk, String divideId, String versionNo,
			String _processCode, String _orderNoSeq, String _orderSource)
			throws SQLException {
		PreparedStatement psta = null;
		PlnOrdInfo ordInfo = new PlnOrdInfo();

		double planPMain = 0;
		ordInfo.setOrdLnDlyPk(_ordLnDlyPk);
		ordInfo.setCreateName(name);
		ordInfo.setPlnDivideId(divideId);
		String groupJgx = "";
		String groupRcl = "";
		String cutHeadDbk = "";
		String cutendDbk = "";
		String longerDbk = "";
		String gradeCode = "";
		// 查询合同信息
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.querySaleProcStatus");
		psta = conn.prepareStatement(sql);
		psta.setString(1, _ordLnDlyPk);
		psta.setString(2, divideId);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			gradeCode = rs.getString("GRADECODE");
			planPMain = rs.getDouble("PLAN_P_MAIN");
			ordInfo.setOrderNo(rs.getString("ORDER_NO"));
			ordInfo.setOrderSeq(rs.getString("ORDER_SEQ"));
			ordInfo.setPlanWtD(rs.getDouble("PLAN_WT_D"));
			ordInfo.setPlanNumD(rs.getInt("PLAN_NUM_D"));
			ordInfo.setOutMultD(rs.getInt("OUT_MULT_D"));
			if (planPMain == 0) {
				return true;
			}
			if (planPMain < 0) {
				errMessage = "下发计划量必须大于零。";
				return false;
			}
			if (rs.getString("INDEX_SEQ") == null) {
				errMessage = "未找到制程索引号。";
				return false;
			}
			if (rs.getString("DESGIN_KEY") == null) {
				errMessage = "未找到质量设计（DesginKey）。";
				return false;
			}
			if (rs.getString("BOM") == null) {
				errMessage = "未找到BOM号。";
				return false;
			}
			if (rs.getString("ROLL_MONTH") == null) {
				errMessage = "没有排产月份。";
				return false;
			}
			cutHeadDbk = rs.getString("LEN_CUTHEAD_DBK");
			cutendDbk = rs.getString("LEN_CUTEND_DBK");
			longerDbk = rs.getString("LEN_LONGER_DBK");
			ordInfo.setAddDbkLen(rs.getDouble("LEN_CUTHEAD_DBK")
					+ rs.getDouble("LEN_CUTEND_DBK"));
			ordInfo.setAddDcLen(rs.getDouble("LEN_LONGER_DBK"));
			ordInfo.setLenCutheadDbk(cutHeadDbk);
			ordInfo.setLenCutendCbk(cutendDbk);
			ordInfo.setRatioDbk(rs.getDouble("RATIO_DBK"));
			ordInfo.setAbc(rs.getString("ABC"));
			ordInfo.setOutputStdNo(rs.getString("OUTPUT_STD_NO"));
			ordInfo.setUseDesc(rs.getString("USE_DESC"));
			ordInfo.setStdName(rs.getString("STD_NAME"));
			ordInfo.setRollMonth(rs.getString("ROLL_MONTH"));
			ordInfo.setEndlenSpecstd(rs.getDouble("ENDLEN_SPECSTD"));
			ordInfo.setPlanPMainMin(rs.getDouble("PLAN_P_MAIN_MIN"));
			ordInfo.setAvgLenMin(rs.getDouble("AVG_LEN_MIN"));
			ordInfo.setMsc(rs.getString("MSC"));
			ordInfo.setDesginKey(rs.getString("DESGIN_KEY"));
			ordInfo.setIndexSeq(rs.getString("INDEX_SEQ"));
			ordInfo.setBom(rs.getString("BOM"));
			ordInfo.setOutdiameter(rs.getDouble("OUTDIAMETER"));
			ordInfo.setWallthick(rs.getDouble("WALLTHICK"));
			ordInfo.setAimLenRoll(rs.getDouble("AIM_LEN_ROLL"));
			ordInfo.setwJ(rs.getDouble("WEIGHT_PER_M"));
			ordInfo.setLengthmax(rs.getDouble("LENGTHMAX"));
			ordInfo.setLengthmin(rs.getDouble("LENGTHMIN"));
			ordInfo.setSiglLen(rs.getDouble("SIGL_LEN"));
			ordInfo.setShortest(rs.getDouble("SHORTEST"));
			ordInfo.setCheckJVal(rs.getDouble("CHECK_J_VAL"));
			ordInfo.setAddInspect(rs.getDouble("INSPECTION_VALUE"));
			ordInfo.setLenRollMin(rs.getDouble("LEN_ROLL_MIN"));
			ordInfo.setLenRollMax(rs.getDouble("LEN_ROLL_MAX"));
			ordInfo.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
			ordInfo.setOrderUnit(rs.getString("ORDER_UNIT"));
			ordInfo.setTotlength(rs.getDouble("TOTLENGTH"));
			ordInfo.setTotlengthUnit(rs.getString("TOTLENGTH_UNIT"));
			ordInfo.setProduceSort(rs.getString("PRODUCE_SORT"));
			ordInfo.setPlineName(rs.getString("GROUP_ROLL_NAME"));
			ordInfo.setOrderSource(rs.getString("ORDER_SOURCE"));
			ordInfo.setLenScope(rs.getDouble("LEN_SCOPE"));
			if (rs.getString("ORDER_UNIT") != null) {
				ordInfo.setOrderUnit(rs.getString("ORDER_UNIT"));
			} else {
				errMessage = "订货单位未知，请核对合同信息。";
				return false;
			}
			if (rs.getString("ORDER_UNIT") != null
					&& (rs.getString("ORDER_UNIT").trim().equals("吨") || rs
							.getString("ORDER_UNIT").trim().equals("英磅"))) {
				if (rs.getString("WEIGH_TYPE") != null
						&& rs.getString("WEIGH_TYPE").contains("理")) {
					ordInfo.setWeighType("0");
				} else {
					ordInfo.setWeighType("1");
				}
			} else {
				ordInfo.setWeighType("-1");
			}
			if (rs.getString("SHRT_STOVE") != null) {
				ordInfo.setShrtStove(rs.getString("SHRT_STOVE"));
			}
			if (rs.getString("PRDCR_NO") != null) {
				ordInfo.setPrdcrNo(rs.getString("PRDCR_NO"));
			}
			if (rs.getString("GROUP_ROLL") != null) {
				ordInfo.setPlineD(rs.getString("GROUP_ROLL"));
			}
			if (rs.getString("MODEL_DESC") != null) {
				ordInfo.setModelDesc(rs.getString("MODEL_DESC"));
			}
			if (rs.getString("GROUP_JGX") != null) {
				groupJgx = rs.getString("GROUP_JGX");
			}
			if (rs.getString("GROUP_RCL") != null) {
				groupRcl = rs.getString("GROUP_RCL");
			}
			if (rs.getString("OUTS_BILLET_FL") != null) {
				if (rs.getString("OUTS_BILLET_FL").equals("121801")) {
					ordInfo.setOutsBilletFl("1");// 外购
				} else {
					ordInfo.setOutsBilletFl("0");// 自炼
				}
			}
		} else {
			errMessage = "未找到合同信息。";
			return false;
		}
		rs.close();
		psta.close();

		// 查询制程
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryDesginMscpl");
		psta = conn.prepareStatement(sql);
		psta.setString(1, ordInfo.getDesginKey());
		psta.setString(2, ordInfo.getIndexSeq());
		rs = psta.executeQuery();
		if (rs.next()) {
			if (rs.getString("WHOLE_BACKLOG") == null) {
				errMessage = "未找到制程经过工序点。";
				return false;
			}
			ordInfo.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
			ordInfo.setWholeBacklogDesc(rs.getString("WHOLE_BACKLOG_DESC"));
		} else {
			errMessage = "未找到制程信息。";
			return false;
		}
		rs.close();
		psta.close();

		if (ordInfo.getWholeBacklog() != null
				&& !ordInfo.getWholeBacklog().equals("")
				&& ordInfo.getWholeBacklog().contains("E")) {
			String processCodeC = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnZgMStoGp.selectMscPlProc");
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setObject(1, ordInfo.getDesginKey());
			ps.setObject(2, ordInfo.getIndexSeq());
			ps.setObject(3, "E");
			ResultSet rsta = ps.executeQuery();
			if (rsta.next()) {
				if (rsta.getString("PROCESS_CODE_C") == null) {
					ordInfo.setErrCode(-1);
					ordInfo.setErrMessage(ordInfo.getErrMessage() + "\r\n"
							+ "未找到镦拔扩二级工序。");
					return false;
				} else {
					processCodeC = rsta.getString("PROCESS_CODE_C");
				}
			}
			rsta.close();
			ps.close();
			// 镦粗
			if (processCodeC.equals("B008")) {
				if (longerDbk == null || longerDbk.equals("")) {
					ordInfo.setErrCode(-1);
					ordInfo.setErrMessage(ordInfo.getErrMessage() + "\r\n"
							+ "镦粗加长为空,请确认。");
					return false;
				}
			} else if (ordInfo.getAddDbkLen() < 0) {
				ordInfo.setErrCode(-1);
				ordInfo.setErrMessage(ordInfo.getErrMessage() + "\r\n"
						+ "拔扩切头尾小于零,请确认。");
				return false;
			} else if (cutHeadDbk == null || cutHeadDbk.equals("")) {
				ordInfo.setErrCode(-1);
				ordInfo.setErrMessage(ordInfo.getErrMessage() + "\r\n"
						+ "拔扩切头为空,请确认。");
				return false;
			} else if (cutendDbk == null || cutendDbk.equals("")) {
				ordInfo.setErrCode(-1);
				ordInfo.setErrMessage(ordInfo.getErrMessage() + "\r\n"
						+ "拔扩切尾为空,请确认。");
				return false;
			}
		}
		// 查询监制工序
		// if (!ordInfo.getPrdcrNo().equals("")) {
		String blType = "";
		if (_orderSource.equals("备料需求")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryPorderBlReq");
			psta = conn.prepareStatement(sql);
			psta.setString(1, _orderNoSeq);
			rs = psta.executeQuery();
			if (rs.next()) {
				blType = rs.getString("BLTYPE");
			}
			rs.close();
			psta.close();
			// ordInfo.setOutsBilletFl("0");// 自炼
		}
		if (blType.equals("接箍料")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryOrderLinePrdcrLst1");
			psta = conn.prepareStatement(sql);
			psta.setString(1, ordInfo.getPrdcrNo());
			psta.setString(2, ordInfo.getIndexSeq());
			rs = psta.executeQuery();
			if (rs.next()) {
				if (rs.getString("PRDCR_PRC") != null) {
					ordInfo.setPrdcrPrc(rs.getString("PRDCR_PRC"));
				}
			}
			rs.close();
			psta.close();

			// 生成接箍排产订单
			String mess = CorePlnOrderJgS.addOrderJgS(conn, ordInfo, gradeCode,
					planPMain);
			if (!mess.equals("")) {
				errMessage = mess;
				return false;
			}
		} else if (blType.equals("短节备料")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryOrderLinePrdcrLst2");
			psta = conn.prepareStatement(sql);
			psta.setString(1, ordInfo.getPrdcrNo());
			psta.setString(2, ordInfo.getIndexSeq());
			rs = psta.executeQuery();
			if (rs.next()) {
				if (rs.getString("PRDCR_PRC") != null) {
					ordInfo.setPrdcrPrc(rs.getString("PRDCR_PRC"));
				}
			}
			rs.close();
			psta.close();
		} else {
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryOrderLinePrdcrLst");
			psta = conn.prepareStatement(sql);
			psta.setString(1, ordInfo.getPrdcrNo());
			psta.setString(2, ordInfo.getIndexSeq());
			rs = psta.executeQuery();
			if (rs.next()) {
				if (rs.getString("PRDCR_PRC") != null) {
					ordInfo.setPrdcrPrc(rs.getString("PRDCR_PRC"));
				}
			}
			rs.close();
			psta.close();
		}
		// }

		ordInfo.setSerialNum(CorePlnGxOrdM.createGxMNo(conn));// 订单流水号
		ordInfo.setVersionNo(versionNo);
		// 获取轧管加长原则，转移长度上下限，加工分切,控制外径壁厚 ，管单重
		ordInfo = PlnZglenthCal.calcPlnInfo(conn, ordInfo);
		if (ordInfo.getErrCode() == -1) {
			ordInfo.setErrCode(-1);
			ordInfo.setErrMessage(ordInfo.getErrMessage());
			return false;
		}
		// 经过工序倒推
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryMscplPrc");
		psta = conn.prepareStatement(sql);
		psta.setString(1, ordInfo.getDesginKey());
		psta.setString(2, ordInfo.getIndexSeq());
		rs = psta.executeQuery();
		while (rs.next()) {
			if (rs.getString("PROCESS_CODE") == null) {
				errMessage = "工序点代码为空。";
				return false;
			}
			if (rs.getString("PROCESS_CODE").equals(_processCode)) {
				break;
			}
			if (rs.getString("PROCESS_CODE").trim().equals("G")) {
				PlnOrderJgxEntity jgxEntity = new PlnOrderJgxEntity();
				jgxEntity.setBacklogSeq(String
						.valueOf(rs.getInt("BACKLOG_SEQ")));
				jgxEntity.setGroupJgx(groupJgx);
				if (rs.getString("PROCESS_CODE_C") != null) {
					jgxEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					jgxEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					jgxEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				if (rs.getString("PREVIOUS_GX") != null) {
					jgxEntity.setPreviousGx(rs.getString("PREVIOUS_GX"));
				}
				if (rs.getString("PREVIOUS_GX_CODE") != null) {
					jgxEntity.setPreviousGxCode(rs
							.getString("PREVIOUS_GX_CODE"));
				}
				planPMain = addJgxPrcOrder(jgxEntity, ordInfo, planPMain, conn);
				if (errCode == -1) {
					break;
				}
			}
			if (rs.getString("PROCESS_CODE").trim().equals("F")
					&& !ordInfo.getOrderUnit().equals("支")) {
				PlnOrderRclEntity rclEntity = new PlnOrderRclEntity();
				rclEntity.setBacklogSeq(String
						.valueOf(rs.getInt("BACKLOG_SEQ")));
				rclEntity.setGroupRcl(groupRcl);

				if (rs.getString("PROCESS_CODE_C") != null) {
					rclEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					rclEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					rclEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				if (rs.getString("PREVIOUS_GX") != null) {
					rclEntity.setPreviousGx(rs.getString("PREVIOUS_GX"));
				}
				if (rs.getString("PREVIOUS_GX_CODE") != null) {
					rclEntity.setPreviousGxCode(rs
							.getString("PREVIOUS_GX_CODE"));
				}
				planPMain = addRclPrcOrder(rclEntity, ordInfo, planPMain, conn);
				if (errCode == -1) {
					break;
				}
			}
			if (rs.getString("PROCESS_CODE").trim().equals("E")
					&& !ordInfo.getOrderUnit().equals("支")) {
				PlnOrderDbkEntity dbkEntity = new PlnOrderDbkEntity();
				dbkEntity.setBacklogSeq(String
						.valueOf(rs.getInt("BACKLOG_SEQ")));
				if (rs.getString("PROCESS_CODE_C") != null) {
					dbkEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					dbkEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					dbkEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				if (rs.getString("PREVIOUS_GX") != null) {
					dbkEntity.setPreviousGx(rs.getString("PREVIOUS_GX"));
				}
				if (rs.getString("PREVIOUS_GX_CODE") != null) {
					dbkEntity.setPreviousGxCode(rs
							.getString("PREVIOUS_GX_CODE"));
				}
				planPMain = addDbkPrcOrder(dbkEntity, ordInfo, planPMain, conn);
				if (errCode == -1) {
					break;
				}
			}
			//&& !ordInfo.getPlineD().equals("C072")
			if (rs.getString("PROCESS_CODE").trim().equals("D")

					&& !ordInfo.getOrderUnit().equals("支")) {
				PlnOrderZgEntity zgEntity = new PlnOrderZgEntity();
				String sic = "";
				if (rs.getString("SIC") != null) {
					String[] strs = rs.getString("SIC").split(",");
					sic = strs[0].toString().trim();
				}
				if (sic.equals("")) {
					errMessage = "无内控壁厚公差索引码,请联系工艺人员。";
					return false;
				}
				zgEntity
						.setBacklogSeq(String.valueOf(rs.getInt("BACKLOG_SEQ")));
				if (rs.getString("PROCESS_CODE_C") != null) {
					zgEntity.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
				}
				if (rs.getString("PROCESS_DESC_C") != null) {
					zgEntity.setProcessDescC(rs.getString("PROCESS_DESC_C"));
				}
				if (rs.getString("STATION_ROAD") != null) {
					zgEntity.setStationRoad(rs.getString("STATION_ROAD"));
				}
				planPMain = addZgPrcOrder(zgEntity, ordInfo, planPMain, sic,
						conn);
				if (errCode == -1) {
					break;
				}
			}
			/*
			 * if (rs.getString("PROCESS_CODE").trim().equals("C")) {
			 * PlnOrderDzEntity dzEntity = new PlnOrderDzEntity();
			 * dzEntity.setBacklogSeq(String.valueOf(rs.getInt("BACKLOG_SEQ")));
			 * planPMain = addDzPrcOrder(dzEntity, ordInfo, planPMain, conn); if
			 * (errCode == -1) { break; } }
			 */
		}
		rs.close();
		psta.close();
		return true;
	}

	/**
	 * 加工工序
	 * 
	 * @param jgxEntity
	 * @param outWeight
	 *            计划重量
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	private double addJgxPrcOrder(PlnOrderJgxEntity jgxEntity,
			PlnOrdInfo ordInfo, double outWeight, Connection conn)
			throws SQLException {
		double inWeight = outWeight;
		double inWeightMin = ordInfo.getPlanPMainMin();// 管体排产吨Min
		double prdRate = 1; // 成才率
		int addNum = 0;// 加投支数
		double addWeight = 0;// 加投重量
		int backlogSeq = Integer.parseInt(jgxEntity.getBacklogSeq());

		int outMultD = 1;
		if (ordInfo.getOutMultD() != 0) {
			outMultD = ordInfo.getOutMultD();
		}
		// 待投入支数
		int inNum = Integer.parseInt(String.valueOf((int) Math
				.ceil((float) ordInfo.getPlanNumD() / outMultD)));
		PreparedStatement ps = null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryDesginBomI");
		if (!ordInfo.getOrderUnit().equals("支")) {
			sql += " AND T.MARK = '1'";
		}
		ps = conn.prepareStatement(sql);
		ps.setString(1, ordInfo.getBom());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			jgxEntity.setInwlId(rs.getString("IN_MATERIAL_NO"));
			jgxEntity.setOutwlId(rs.getString("OUT_MATERIAL_NO"));
		} else {
			errCode = -1;
			errMessage = "未找到管加工工序投入产出。";
			return inWeight + addWeight;
		}
		rs.close();
		ps.close();

		// double x = 1;
		// sql = CoreXmlSqlParsersFactory
		// .getSql("CorePlnProcessOrd.queryDesginBom");
		// ps = conn.prepareStatement(sql);
		// ps.setString(1, ordInfo.getBom());
		// rs = ps.executeQuery();
		// if (rs.next()) {
		// double outdiameterIn = rs.getDouble("DIMATER");
		// double wallthickIn = rs.getDouble("HEIGHT");
		// String eInMaterialNo = "";
		// String eOutMaterialNo = "";
		// if (rs.getString("E_IN_MATERIAL_NO") != null) {
		// eInMaterialNo = rs.getString("E_IN_MATERIAL_NO");
		// }
		// if (rs.getString("E_OUT_MATERIAL_NO") != null) {
		// eOutMaterialNo = rs.getString("E_OUT_MATERIAL_NO");
		// }
		// String[] dbkAdd = addLenDbk(conn, eInMaterialNo, eOutMaterialNo,
		// ordInfo.getDesginKey(), ordInfo.getIndexSeq());
		// dbkpline = dbkAdd[2];
		// String sqldbk = CoreXmlSqlParsersFactory
		// .getSql("CorePlnProcessOrd.queryBasePlineD");
		// PreparedStatement psta = conn.prepareStatement(sqldbk);
		// psta.setObject(1, dbkpline);
		// ResultSet result = psta.executeQuery();
		// if (result.next()) {
		// burnoutRate = result.getDouble("BURNOUT_RATE");
		// }
		// result.close();
		// psta.close();
		//
		// if (wallthickIn != 0 && outdiameterIn != 0) {
		// x = PlnZglenthCal.getDhChangeRate(ordInfo.getOutdiameter(),
		// ordInfo.getWallthick(), outdiameterIn, wallthickIn,
		// burnoutRate);
		// }
		// addDbk = Double.parseDouble(dbkAdd[0]) * x;
		// }
		// rs.close();
		// ps.close();

		// 成材率
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryBaseMaterialIPl");
		ps = conn.prepareStatement(sql);
		ps.setString(1, jgxEntity.getInwlId());
		ps.setString(2, jgxEntity.getOutwlId());
		ps.setString(3, ordInfo.getDesginKey());
		ps.setString(4, ordInfo.getIndexSeq());
		ps.setString(5, jgxEntity.getGroupJgx());
		rs = ps.executeQuery();
		if (rs.next()) {
			double wastRate = rs.getDouble("WASTE_RATE") / 10000;
			addNum = (int) Math.ceil(wastRate * inNum);
			// addWeight = addNum
			// * ordInfo.getwJ()
			// * (ordInfo.getAimLenRoll() / x - addDbk / 1000 - ordInfo
			// .getAddInspect() / 1000);
			addWeight = wastRate * outWeight + (ordInfo.getFinisWj())
					* (ordInfo.getAddlenZgRuleMax() / 1000)
					* ordInfo.getPlanNumD();
			if (rs.getString("PLINE_CODE") != null) {
				jgxEntity.setPlineCode(rs.getString("PLINE_CODE"));
			}
			if (rs.getString("PLINE_NAME") != null) {
				jgxEntity.setPlineName(rs.getString("PLINE_NAME"));
			}
			jgxEntity.setAddNumIn(String.valueOf(addNum));
			if (StringTool.isNullOrEmpty(rs.getString("WASTE_RATE"))) {
				jgxEntity.setAddWtIn("0");
			} else {
				jgxEntity.setAddWtIn(String.valueOf(new BigDecimal(addWeight)
						.setScale(3, RoundingMode.HALF_UP)));
			}

		} else {
			errCode = -1;
			errMessage = "未找到加工产线。\n";
			String sqlr = "SELECT PLINE_CODE,PLINE_NAME, IN_MATERIAL_DESC, OUT_MATERIAL_DESC"
					+ " FROM SLM_BASE_MATERIAL_K_PL T WHERE T.OUT_MATERIAL_NO = ? AND T.IN_MATERIAL_NO = ?";
			PreparedStatement psta = conn.prepareStatement(sqlr);
			psta.setObject(1, jgxEntity.getOutwlId());
			psta.setObject(2, jgxEntity.getInwlId());
			ResultSet rsta = psta.executeQuery();
			if (rsta.next()) {
				if (jgxEntity.getGroupJgx().trim().equals("")) {
					errMessage += "加工没有分组！";
				} else {
					sqlr = "SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE = ?";
					PreparedStatement psta1 = conn.prepareStatement(sqlr);
					psta1.setObject(1, jgxEntity.getGroupJgx());
					ResultSet rsta1 = psta1.executeQuery();
					if (rsta1.next()) {
						errMessage += "加工组：" + rsta1.getString("BASENAME")
								+ "。";
					}
					rsta1.close();
					psta1.close();
				}
			} else {
				sqlr = "SELECT A.OUT_MATERIAL_DESC FROM SLM_BASE_MATERIAL_K A WHERE A.OUT_MATERIAL_NO = ?";
				PreparedStatement psta1 = conn.prepareStatement(sqlr);
				psta1.setObject(1, jgxEntity.getOutwlId());
				ResultSet rsta1 = psta1.executeQuery();
				if (rsta1.next()) {
					errMessage += "物料：" + rsta1.getString("OUT_MATERIAL_DESC")
							+ "。";
				}
				rsta1.close();
				psta1.close();
			}
			rsta.close();
			psta.close();
			return inWeight + addWeight;
		}
		rs.close();
		ps.close();

		// 是否加工监制
		if (!ordInfo.getPrdcrPrc().equals("")
				&& ordInfo.getPrdcrPrc().contains("G")) {
			jgxEntity.setIfMonitorJgx("1");
		} else {
			jgxEntity.setIfMonitorJgx("0");
		}
		// inWeight = PlnProcCal.ordJgxInventory(outWeight,
		// ordInfo.getPlanNumD(),
		// addNum, ordInfo.getAimLenRoll(), ordInfo.getwJ(), ordInfo
		// .getAddDbkLen(), ordInfo.getAddlenZgRuleMax());
		prdRate = outWeight / inWeight;

		jgxEntity.setOrdLnDlyPk(ordInfo.getOrdLnDlyPk());
		jgxEntity.setPlnDivideId(ordInfo.getPlnDivideId());
		jgxEntity.setCreateName(ordInfo.getCreateName());
		jgxEntity.setBom(ordInfo.getBom());
		jgxEntity.setPrdcrNo(ordInfo.getPrdcrNo());
		jgxEntity.setGetrate(String.valueOf(new BigDecimal(prdRate).setScale(3,
				RoundingMode.HALF_UP)));
		jgxEntity.setOrderweight(String.valueOf(ordInfo.getPlanWtD()));
		jgxEntity.setOrdernum(String.valueOf(ordInfo.getPlanNumD()));

		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getJgxReqOrderNo");
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("ORDER_NO") != null) {
				String proOrderNo = rs.getString("ORDER_NO");
				String[] proPlanId = new String[1];// 工序订单编号
				CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
				PreparedStatement psta = null;

				// 是否末工序
				if ((ordInfo.getWholeBacklog().length() + 1) / 2 == backlogSeq) {
					jgxEntity.setIsendgx("1");
				}

				jgxEntity.setInNum(String.valueOf(inNum));
				jgxEntity.setInWeight(String.valueOf(inWeight));
				jgxEntity.setProOrderNo(proOrderNo);
				String sqlUp = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("CorePlnProcessOrd.addPlnOrderJgx"),
						jgxEntity);
				psta = conn.prepareStatement(sqlUp);
				psta.executeUpdate();
				psta.close();

				// 工序订单
				if (gxOrdM.addOrJoinJgxM(conn, proOrderNo, ordInfo
						.getVersionNo(), ordInfo.getSerialNum(), jgxEntity
						.getPreviousGxCode(), addNum + ordInfo.getPlanNumD(),
						inWeight, ordInfo.getCreateName(), proPlanId, ordInfo,
						jgxEntity)) {
					String sqle = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.updateJgxOrdGxMNo");
					psta = conn.prepareStatement(sqle);
					psta.setString(1, proPlanId[0]);
					psta.setString(2, proOrderNo);
					psta.executeUpdate();
					psta.close();
				} else {
					errCode = -1;
					errMessage = gxOrdM.getVerrMsg();
					return inWeight + addWeight;
				}
			}
		}
		rs.close();
		ps.close();

		ordInfo.setPlanNumD(addNum + inNum);
		ordInfo.setPlanPMainMin(inWeightMin + addWeight);
		return inWeight + addWeight;
	}

	/**
	 * 热处理
	 * 
	 * @param rclEntity
	 * @param outWeight
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	private double addRclPrcOrder(PlnOrderRclEntity rclEntity,
			PlnOrdInfo ordInfo, double outWeight, Connection conn)
			throws SQLException {
		double inWeight = outWeight;
		double inWeightMin = ordInfo.getPlanPMainMin();// 管体排产吨Min
		PreparedStatement ps = null;
		double prdRate = 1; // 成才率
		int addNum = 0;// 加投支数
		double addWeight = 0;// 加投重量
		double buringRate = 0;// 热处理烧损率（%）
		double addlenInspect = ordInfo.getAddInspect();// 盲区切长（mm）

		int backlogSeq = Integer.parseInt(rclEntity.getBacklogSeq());
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryDesginBom");
		ps = conn.prepareStatement(sql);
		ps.setString(1, ordInfo.getBom());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			// double outdiameterIn = rs.getDouble("DIMATER");
			// double wallthickIn = rs.getDouble("HEIGHT");
			// String eInMaterialNo = "";
			// String eOutMaterialNo = "";
			// if (rs.getString("E_IN_MATERIAL_NO") != null) {
			// eInMaterialNo = rs.getString("E_IN_MATERIAL_NO");
			// }
			// if (rs.getString("E_OUT_MATERIAL_NO") != null) {
			// eOutMaterialNo = rs.getString("E_OUT_MATERIAL_NO");
			// }
			// String[] dbkAdd = addLenDbk(conn, eInMaterialNo, eOutMaterialNo,
			// ordInfo.getDesginKey(), ordInfo.getIndexSeq());
			// dbkpline = dbkAdd[2];
			// String sqldbk = CoreXmlSqlParsersFactory
			// .getSql("CorePlnProcessOrd.queryBasePlineD");
			// PreparedStatement psta = conn.prepareStatement(sqldbk);
			// psta.setObject(1, dbkpline);
			// ResultSet result = psta.executeQuery();
			// if (result.next()) {
			// buringRateDbk = result.getDouble("BURNOUT_RATE");
			// }
			// result.close();
			// psta.close();
			//
			// if (wallthickIn != 0 && outdiameterIn != 0) {
			// x = PlnZglenthCal.getDhChangeRate(ordInfo.getOutdiameter(),
			// ordInfo.getWallthick(), outdiameterIn, wallthickIn,
			// buringRateDbk);
			// }
			// addDbk = Double.parseDouble(dbkAdd[0]) * x;
			if (rs.getString("F_IN_MATERIAL_NO") != null) {
				rclEntity.setInwlId(rs.getString("F_IN_MATERIAL_NO"));
			}
			if (rs.getString("F_IN_MATERIAL_NO") != null) {
				rclEntity.setOutwlId(rs.getString("F_OUT_MATERIAL_NO"));
			}
		}
		rs.close();
		ps.close();

		// 成材率
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryBaseMaterialFPl");
		ps = conn.prepareStatement(sql);
		ps.setString(1, rclEntity.getInwlId());
		ps.setString(2, rclEntity.getOutwlId());
		ps.setString(3, ordInfo.getDesginKey());
		ps.setString(4, ordInfo.getIndexSeq());
		ps.setString(5, rclEntity.getGroupRcl());
		rs = ps.executeQuery();
		if (rs.next()) {
			double wastRate = rs.getDouble("WASTE_RATE") / 10000;
			addNum = (int) Math.ceil(wastRate * ordInfo.getPlanNumD());

			if (rs.getString("PLINE_CODE") != null) {
				rclEntity.setPlineCode(rs.getString("PLINE_CODE"));
			}
			if (rs.getString("PLINE_NAME") != null) {
				rclEntity.setPlineName(rs.getString("PLINE_NAME"));
			}
			// 查询热处理的烧损率
			String sql1 = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryBasePlineD");
			PreparedStatement ps1 = conn.prepareStatement(sql1);
			ps1.setString(1, rclEntity.getPlineCode());
			ResultSet rs1 = ps1.executeQuery();
			if (rs1.next()) {
				buringRate = rs1.getDouble("BURNOUT_RATE");
			}
			rs1.close();
			ps1.close();
			// 是否末工序
			if ((ordInfo.getWholeBacklog().length() + 1) / 2 == backlogSeq) {
				addWeight = wastRate * outWeight;
			} else {

				// addWeight = addNum * ordInfo.getwJ()
				// * (ordInfo.getAimLenRoll() / x - addDbk / 1000);
				int intCut = 0;
				if (ordInfo.getProCut() == 0) {
					intCut = 1;
				} else {
					intCut = (int) ordInfo.getProCut();
				}
				addWeight = (1 + buringRate / 100)
						* (wastRate * outWeight + (ordInfo.getFinisWj())
								* (addlenInspect / 1000)
								* (ordInfo.getPlanNumD() / intCut));// ordInfo.getProCut()
				// 说明:分切=0（默认1）
			}

			rclEntity.setAddNumIn(addNum);
			if (StringTool.isNullOrEmpty(rs.getString("WASTE_RATE"))) {
				rclEntity.setAddWtIn("0");
			} else {
				rclEntity.setAddWtIn(String.valueOf(new BigDecimal(addWeight)
						.setScale(3, RoundingMode.HALF_UP)));
			}
		} else {
			errCode = -1;
			errMessage = "未找到热处理产线。\n";
			String sqlr = "SELECT PLINE_CODE,PLINE_NAME FROM SLM_BASE_MATERIAL_F_PL T WHERE T.IN_MATERIAL_NO = ? AND T.OUT_MATERIAL_NO = ?";
			PreparedStatement psta = conn.prepareStatement(sqlr);
			psta.setObject(1, rclEntity.getInwlId());
			psta.setObject(2, rclEntity.getOutwlId());
			ResultSet rsta = psta.executeQuery();
			if (rsta.next()) {
				if (rclEntity.getGroupRcl().trim().equals("")) {
					errMessage += "热处理没有分组！";
				} else {
					sqlr = "SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE = ?";
					PreparedStatement psta1 = conn.prepareStatement(sqlr);
					psta1.setObject(1, rclEntity.getGroupRcl());
					ResultSet rsta1 = psta1.executeQuery();
					if (rsta1.next()) {
						errMessage += "热处理组：" + rsta1.getString("BASENAME")
								+ "。";
					}
					rsta1.close();
					psta1.close();
				}
			} else {
				sqlr = "SELECT A.IN_MATERIAL_DESC, A.OUT_MATERIAL_DESC"
						+ " FROM SLM_BASE_MATERIAL_F A"
						+ " WHERE A.IN_MATERIAL_NO = ?"
						+ " AND A.OUT_MATERIAL_NO = ?";
				PreparedStatement psta1 = conn.prepareStatement(sqlr);
				psta1.setObject(1, rclEntity.getInwlId());
				psta1.setObject(2, rclEntity.getOutwlId());
				ResultSet rsta1 = psta1.executeQuery();
				if (rsta1.next()) {
					errMessage += "投料：" + rsta1.getString("IN_MATERIAL_DESC")
							+ "，产料：" + rsta1.getString("OUT_MATERIAL_DESC")
							+ "。";
				}
				rsta1.close();
				psta1.close();
			}
			rsta.close();
			psta.close();
			return inWeight + addWeight;
		}
		rs.close();
		ps.close();

		// 是否监制
		if (!ordInfo.getPrdcrPrc().equals("")
				&& ordInfo.getPrdcrPrc().contains("F")) {
			rclEntity.setIfMonitorRcl("1");
		} else {
			rclEntity.setIfMonitorRcl("0");
		}

		// inWeight = PlnProcCal.ordRclInventory(outWeight,
		// ordInfo.getPlanNumD(),
		// addNum, ordInfo.getAimLenRoll(), ordInfo.getwJ(), ordInfo
		// .getAddDbkLen(), addlenInspect, buringRate);
		prdRate = outWeight / inWeight;
		rclEntity.setOrdLnDlyPk(ordInfo.getOrdLnDlyPk());
		rclEntity.setPlnDivideId(ordInfo.getPlnDivideId());
		rclEntity.setCreateName(ordInfo.getCreateName());
		rclEntity.setBom(ordInfo.getBom());
		rclEntity.setPrdcrNo(ordInfo.getPrdcrNo());
		rclEntity.setGetrate(String.valueOf(new BigDecimal(prdRate).setScale(3,
				RoundingMode.HALF_UP)));
		rclEntity.setOrderweight(String.valueOf(outWeight));
		rclEntity.setOrdernum(String.valueOf(ordInfo.getPlanNumD()));
		rclEntity.setWeigthSMin(inWeightMin);
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getRclReqOrderNo");
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("ORDER_NO") != null) {
				String proOrderNo = rs.getString("ORDER_NO");
				String[] proPlanId = new String[1];// 工序订单编号
				CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
				PreparedStatement psta = null;

				// 是否末工序
				if ((ordInfo.getWholeBacklog().length() + 1) / 2 == backlogSeq) {
					if (ordInfo.getOrderSource().equals("备料需求")) {
						rclEntity.setIsendgx("0");
					} else {
						rclEntity.setIsendgx("1");
					}
				} else {
					int nextBacklogSeq = backlogSeq + 1;
					String sqle = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.queryMscplPrcSeq");
					psta = conn.prepareStatement(sqle);
					psta.setString(1, ordInfo.getDesginKey());
					psta.setString(2, ordInfo.getIndexSeq());
					psta.setString(3, String.valueOf(nextBacklogSeq));
					ResultSet rst = psta.executeQuery();
					if (rst.next()) {
						rclEntity.setNextGx(rst.getString("PROCESS_CODE"));
					}
					rclEntity.setIsendgx("0");
					psta.close();
					rst.close();
				}
				rclEntity.setInNum(String.valueOf(ordInfo.getPlanNumD()));
				rclEntity.setInWeight(String.valueOf(new BigDecimal(inWeight)
						.setScale(3, RoundingMode.HALF_UP)));
				rclEntity.setProOrderNo(proOrderNo);
				rclEntity.setWeigthSMin(inWeightMin);
				String sqlUp = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("CorePlnProcessOrd.addPlnOrderRcl"),
						rclEntity);
				psta = conn.prepareStatement(sqlUp);
				psta.executeUpdate();
				psta.close();

				// 工序订单
				if (gxOrdM.addOrdJoinRclM(conn, proOrderNo, ordInfo
						.getVersionNo(), ordInfo.getSerialNum(), rclEntity
						.getPreviousGxCode(), addNum + ordInfo.getPlanNumD(),
						inWeight, ordInfo.getCreateName(), proPlanId, ordInfo,
						rclEntity)) {
					String sqle = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.updateRclOrdGxMNo");
					psta = conn.prepareStatement(sqle);
					psta.setString(1, proPlanId[0]);
					psta.setString(2, proOrderNo);
					psta.executeUpdate();
					psta.close();
				} else {
					errCode = -1;
					errMessage = gxOrdM.getVerrMsg();
					return inWeight + addWeight;
				}
			}
		}
		rs.close();
		ps.close();

		ordInfo.setPlanNumD(ordInfo.getPlanNumD() + addNum);
		ordInfo.setPlanPMainMin(inWeightMin + addWeight);
		return inWeight + addWeight;
	}

	/**
	 * 镦拔扩
	 * 
	 * @param dbkEntity
	 * @param outWeight
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	private double addDbkPrcOrder(PlnOrderDbkEntity dbkEntity,
			PlnOrdInfo ordInfo, double outWeight, Connection conn)
			throws SQLException {
		double inWeight = outWeight;
		double inWeightMin = ordInfo.getPlanPMainMin();// 排产MIn吨
		PreparedStatement ps = null;
		double prdRate = 1; // 成才率
		int addNum = 0;// 加投支数
		double addWeight = 0;// 加投支数
		double outdiameterIn = 0;// 原料外径
		double wallthickIn = 0;// 原料壁厚
		double lengthmaxIn = 0;// 投入物料长度上限
		double lengthminIn = 0;// 投入物料长度下限
		int backlogSeq = Integer.parseInt(dbkEntity.getBacklogSeq());

		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryDesginBom");
		ps = conn.prepareStatement(sql);
		ps.setString(1, ordInfo.getBom());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("E_IN_MATERIAL_NO") != null
					&& rs.getString("E_OUT_MATERIAL_NO") != null) {
				dbkEntity.setInwlId(rs.getString("E_IN_MATERIAL_NO"));
				if (rs.getString("E_IN_MATERIAL_DESC") != null) {
					dbkEntity.setInwlDesc(rs.getString("E_IN_MATERIAL_DESC"));
				}
				dbkEntity.setOutwlId(rs.getString("E_OUT_MATERIAL_NO"));
				if (rs.getString("E_OUT_MATERIAL_DESC") != null) {
					dbkEntity.setOutwlDesc(rs.getString("E_OUT_MATERIAL_DESC"));
				}
			} else {
				errCode = -1;
				errMessage = "未找到镦拔扩投入产出物料。";
				return inWeight + addWeight;
			}
			outdiameterIn = rs.getDouble("DIMATER");
			wallthickIn = rs.getDouble("HEIGHT");
			// String[] dbkAdd = addLenDbk(conn, dbkEntity.getInwlDesc(),
			// dbkEntity.getOutwlDesc(), ordInfo.getDesginKey(), ordInfo
			// .getIndexSeq());
			// dbkpline = dbkAdd[2];
			// sql = CoreXmlSqlParsersFactory
			// .getSql("CorePlnProcessOrd.queryBasePlineD");
			// ps = conn.prepareStatement(sql);
			// ps.setString(1, dbkpline);
			// rs = ps.executeQuery();
			// if (rs.next()) {
			// buringRate = rs.getDouble("BURNOUT_RATE");
			// }
			// rs.close();
			// ps.close();
			// addDbk = Double.parseDouble(dbkAdd[0]);
			// addDc = Double.parseDouble(dbkAdd[1]);
		}
		rs.close();
		ps.close();

		if (outdiameterIn <= 0 || wallthickIn <= 0) {
			errCode = -1;
			errMessage = "未找到原料管规格。";
			return inWeight + addWeight;
		}
		// double x = 1;

		// // 轧管转移长度
		// if (ordInfo.getLenRollMin() > 0
		// && ordInfo.getLenRollMax() > ordInfo.getLenRollMin()) {
		// lengthminIn = ordInfo.getLenRollMin();
		// lengthmaxIn = ordInfo.getLenRollMax();
		// }
		// else {
		// double addlenZgRule = PlnZglenthCal.addLenZgRule(ordInfo
		// .getLengthmax(), ordInfo.getLengthmin(), ordInfo
		// .getModelDesc());// 轧管加长原则(mm)
		// x = PlnZglenthCal.getDhChangeRate(ordInfo.getOutdiameter(), ordInfo
		// .getWallthick(), outdiameterIn, wallthickIn, buringRate);
		// lengthmaxIn = PlnZglenthCal.lenthZgCal(ordInfo.getLengthmax(), x,
		// addDc, addDbk, ordInfo.getAddInspect(), ordInfo
		// .getCheckJVal(), addlenZgRule, 1);
		// lengthminIn = PlnZglenthCal.lenthZgCal(ordInfo.getLengthmin(), x,
		// addDc, addDbk, ordInfo.getAddInspect(), ordInfo
		// .getCheckJVal(), addlenZgRule, 1);
		// }
		// 成材率
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryBaseMaterialEPl");
		ps = conn.prepareStatement(sql);
		ps.setString(1, dbkEntity.getInwlId());
		ps.setString(2, dbkEntity.getOutwlId());
		ps.setString(3, ordInfo.getDesginKey());
		ps.setString(4, ordInfo.getIndexSeq());
		rs = ps.executeQuery();
		if (rs.next()) {
			double wastRate = rs.getDouble("WASTE_RATE") / 10000;
			addNum = (int) Math.ceil(wastRate * ordInfo.getPlanNumD());
			// 是否末工序
			if ((ordInfo.getWholeBacklog().length() + 1) / 2 == backlogSeq) {
				addWeight = wastRate * outWeight;
			} else {
				// addWeight = addNum * ordInfo.getwJ() *
				// ordInfo.getAimLenRoll()
				// / x;
				int intCut = 0;
				if (ordInfo.getProCut() == 0) {
					intCut = 1;
				} else {
					intCut = (int) ordInfo.getProCut();
				}
				addWeight = (1 + ordInfo.getBuringRateDbk() / 100)
						* (wastRate * outWeight + (ordInfo.getFinisWj())
								* (ordInfo.getAddDbkLen() / 1000)
								* (ordInfo.getPlanNumD() / intCut));// ordInfo.getProCut()
				// 说明:分切=0（默认1）
			}
			dbkEntity.setPlineCode(rs.getString("PLINE_CODE"));
			dbkEntity.setPlineName(rs.getString("PLINE_NAME"));
			dbkEntity.setAddNumIn(String.valueOf(addNum));
			if (StringTool.isNullOrEmpty(rs.getString("WASTE_RATE"))) {
				dbkEntity.setAddWtIn("0");
			} else {
				dbkEntity.setAddWtIn(String.valueOf(new BigDecimal(addWeight)
						.setScale(3, RoundingMode.HALF_UP)));
			}

		} else {
			errCode = -1;
			errMessage = "未找到镦拔扩产线。\n";
			String sqlr = "SELECT A.IN_MATERIAL_DESC, A.OUT_MATERIAL_DESC"
					+ " FROM SLM_BASE_MATERIAL_E A"
					+ " WHERE A.IN_MATERIAL_NO = ?"
					+ " AND A.OUT_MATERIAL_NO = ?";
			PreparedStatement psta1 = conn.prepareStatement(sqlr);
			psta1.setObject(1, dbkEntity.getInwlId());
			psta1.setObject(2, dbkEntity.getOutwlId());
			ResultSet rsta1 = psta1.executeQuery();
			if (rsta1.next()) {
				errMessage += "投料：" + rsta1.getString("IN_MATERIAL_DESC")
						+ "，产料：" + rsta1.getString("OUT_MATERIAL_DESC") + "。";
			}
			rsta1.close();
			psta1.close();
			return inWeight + addWeight;
		}
		rs.close();
		ps.close();

		// 是否监制
		if (!ordInfo.getPrdcrPrc().equals("")
				&& ordInfo.getPrdcrPrc().contains("E")) {
			dbkEntity.setIfMonitorBj("1");
		} else {
			dbkEntity.setIfMonitorBj("0");
		}

		// inWeight = PlnProcCal.ordDbkInventory(outWeight,
		// ordInfo.getPlanNumD(),
		// addNum, ordInfo.getAddDbkLen(), ordInfo.getAimLenRoll(),
		// ordInfo.getwJ(), ordInfo.getBuringRateDbk());
		prdRate = outWeight / inWeight;
		dbkEntity.setOutdiameter(String.valueOf(ordInfo.getOutdiameter()));// 成品外径
		dbkEntity.setWallthick(String.valueOf(ordInfo.getWallthick()));// 成品壁厚
		dbkEntity.setOutdiameterIn(String.valueOf(outdiameterIn));
		dbkEntity.setWallthickIn(String.valueOf(wallthickIn));
		dbkEntity.setLengthmaxIn(String.valueOf(lengthmaxIn));
		dbkEntity.setLengthminIn(String.valueOf(lengthminIn));
		dbkEntity.setOrdLnDlyPk(ordInfo.getOrdLnDlyPk());
		dbkEntity.setPlnDivideId(ordInfo.getPlnDivideId());
		dbkEntity.setCreateName(ordInfo.getCreateName());
		dbkEntity.setBom(ordInfo.getBom());
		dbkEntity.setPrdcrNo(ordInfo.getPrdcrNo());
		if (prdRate == 0) {
			dbkEntity.setGetrate("0");
		} else {
			dbkEntity.setGetrate(String.valueOf(new BigDecimal(prdRate)
					.setScale(3, RoundingMode.HALF_UP)));
		}

		dbkEntity.setOrderweight(String.valueOf(outWeight));
		dbkEntity.setOrdernum(String.valueOf(ordInfo.getPlanNumD()));
		dbkEntity.setWeigthSMin(inWeightMin);
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getDbkReqOrderNo");
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("ORDER_NO") != null) {
				String proOrderNo = rs.getString("ORDER_NO");
				String preViousGx = dbkEntity.getPreviousGxCode();
				String[] proPlanId = new String[1];// 工序订单编号
				CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
				PreparedStatement psta = null;

				// 是否末工序
				if ((ordInfo.getWholeBacklog().length() + 1) / 2 == backlogSeq) {
					if (ordInfo.getOrderSource().equals("备料需求")) {
						dbkEntity.setIsendgx("0");
					} else {
						dbkEntity.setIsendgx("1");
					}
				} else {
					int nextBacklogSeq = backlogSeq + 1;
					String sqle = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.queryMscplPrcSeq");
					psta = conn.prepareStatement(sqle);
					psta.setString(1, ordInfo.getDesginKey());
					psta.setString(2, ordInfo.getIndexSeq());
					psta.setString(3, String.valueOf(nextBacklogSeq));
					ResultSet rst = psta.executeQuery();
					if (rst.next()) {
						dbkEntity.setNextGx(rst.getString("PROCESS_CODE"));
					}
					dbkEntity.setIsendgx("0");
					psta.close();
					rst.close();
				}

				dbkEntity.setInNum(String.valueOf(ordInfo.getPlanNumD()));
				dbkEntity.setInWeight(String.valueOf(new BigDecimal(inWeight)
						.setScale(3, RoundingMode.HALF_UP)));
				dbkEntity.setProOrderNo(proOrderNo);
				String sqlUp = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("CorePlnProcessOrd.addPlnOrderDbk"),
						dbkEntity);
				psta = conn.prepareStatement(sqlUp);
				psta.executeUpdate();
				psta.close();

				// 工序订单
				if (gxOrdM
						.addOrdJoinDbkM(conn, proOrderNo, ordInfo
								.getVersionNo(), ordInfo.getSerialNum(),
								preViousGx, addNum + ordInfo.getPlanNumD(),
								inWeight, ordInfo.getCreateName(), proPlanId,
								ordInfo, dbkEntity)) {
					String sqle = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.updateDbkOrdGxMNo");
					psta = conn.prepareStatement(sqle);
					psta.setString(1, proPlanId[0]);
					psta.setString(2, proOrderNo);
					psta.executeUpdate();
					psta.close();
				} else {
					errCode = -1;
					errMessage = gxOrdM.getVerrMsg();
					return inWeight + addWeight;
				}
			}
		}
		rs.close();
		ps.close();
		ordInfo.setPlanNumD(ordInfo.getPlanNumD() + addNum);
		ordInfo.setPlanPMainMin(inWeightMin + addWeight);
		return inWeight + addWeight;
	}

	/**
	 * 轧管
	 * 
	 * @param zgEntity
	 * @param outWeight
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	private double addZgPrcOrder(PlnOrderZgEntity zgEntity, PlnOrdInfo ordInfo,
			double outWeight, String sic, Connection conn) throws SQLException {
		double inWeight = outWeight;
		double inWeightMin = ordInfo.getPlanPMainMin();// 排产吨min
		PreparedStatement ps = null;
		// PreparedStatement ps1 = null;
		double prdRate = 1; // 成才率
		int addNum = 0;// 加投支数
		double addWeight = 0;// 加投重量
		double outdiameterIn = 0;// 轧管名义外径
		double wallthickIn = 0;// 轧管名义壁厚
		int backlogSeq = Integer.parseInt(zgEntity.getBacklogSeq());

		double diameterGp = 0;// 管坯直径
		double lengthColdbed = 0;// 冷床长度
		double lengthMaxRing = 0;// 环形炉最长
		double burnoutRate = 0;// 烧损率
		double cropHead = 0;// 切头
		double cropEnd = 0;// 切尾
		double tubeEndRatio = 0;// 管端系数
		double lengthMinTube = 0;// 脱管机长度下限
		double lengthMaxTube = 0;// 脱管机长度上限
		double toleranceZone = 0;// 机组壁厚公差带能力%
		// 查询投入产出
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryDesginBom");
		ps = conn.prepareStatement(sql);
		ps.setString(1, ordInfo.getBom());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			outdiameterIn = rs.getDouble("DIMATER");
			wallthickIn = rs.getDouble("HEIGHT");
			if (wallthickIn == 0 || outdiameterIn == 0) {
				errCode = -1;
				errMessage = "未找到轧管规格。";
				return inWeight + addWeight;
			}
			if (rs.getString("D_OUT_MATERIAL_NO") != null
					&& rs.getString("D_IN_MATERIAL_NO") != null) {
				zgEntity.setOutwlId(rs.getString("D_OUT_MATERIAL_NO"));
				zgEntity.setInwlGp(rs.getString("D_IN_MATERIAL_NO"));
			} else {
				errCode = -1;
				errMessage = "未找到轧管投入产出物料。";
				return inWeight + addWeight;
			}
			diameterGp = rs.getDouble("SPEC_SECTION");
		}
		rs.close();
		ps.close();

		// /查询产线基础表
		zgEntity.setPlineCode(ordInfo.getPlineD());
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryBasePlineD");
		ps = conn.prepareStatement(sql);
		ps.setString(1, ordInfo.getPlineD());
		rs = ps.executeQuery();
		if (rs.next()) {
			lengthColdbed = rs.getDouble("LENGTH_COLDBED");
			lengthMaxRing = rs.getDouble("LENGTH_MAX_RING");
			burnoutRate = rs.getDouble("BURNOUT_RATE");
			zgEntity.setPlineName(rs.getString("PLINE_NAME"));
			lengthMinTube = rs.getDouble("LENGTH_MIN_TUBE");
			lengthMaxTube = rs.getDouble("LENGTH_MAX_TUBE");
			toleranceZone = rs.getDouble("TOLERANCE_ZONE");
		}
		rs.close();
		ps.close();
		int outMultd = 1;
		if (ordInfo.getOutMultD() != 0) {
			outMultd = ordInfo.getOutMultD();
		}
		double cutMax = 0;// 最大分切数
		// 成材率
		double wastRate = 0;// 万只废品
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryBaseMaterialDPl");
		ps = conn.prepareStatement(sql);
		ps.setString(1, zgEntity.getInwlGp());
		ps.setString(2, zgEntity.getOutwlId());
		ps.setString(3, ordInfo.getPlineD());
		rs = ps.executeQuery();
		if (rs.next()) {
			wastRate = rs.getDouble("WASTE_RATE") / 10000;
			addNum = (int) Math.ceil(wastRate * ordInfo.getPlanNumD()
					/ outMultd);
			addWeight = wastRate * outWeight;
			cropHead = rs.getDouble("CROP_HEAD");
			cropEnd = rs.getDouble("CROP_END");
			tubeEndRatio = rs.getDouble("TUBE_END_RATIO");
			cutMax = rs.getDouble("DBL_LEN_MAX");
			zgEntity.setAddNumIn(String.valueOf(addNum));
			zgEntity.setLenCuthead(rs.getString("CROP_HEAD"));
			zgEntity.setLenCutend(rs.getString("CROP_END"));
			zgEntity.setTubeEndRatio(rs.getString("TUBE_END_RATIO"));
		}
		rs.close();
		ps.close();

		InZgOrdCalEntity ordEntity = new InZgOrdCalEntity();
		OutZgOrdCalEntity ordCalEntity = new OutZgOrdCalEntity();
		CoreTolerOfNk tolerOfNk = new CoreTolerOfNk();
		CoreTolerOfNkParam nkParam = new CoreTolerOfNkParam();
		List<Double> listDiameter = new ArrayList<Double>();

		// 如果不是外购管，则计算投料管坯
		//&& !ordInfo.getPlineD().trim().equals("C072")
		if (!ordInfo.getPlineD().trim().equals("C065")
				&& !ordInfo.getPlineD().trim().equals("C066")
				&& !ordInfo.getPlineD().trim().equals("C098")
				&& !ordInfo.getPlineD().trim().equals("C099")
		) {
			listDiameter.add(diameterGp);
			// 轧管内控壁厚公差
			nkParam.setBias_code("404019");
			nkParam.setOutdiameter(outdiameterIn);
			nkParam.setWallthick(wallthickIn);
			nkParam.setSic(sic);
			nkParam.setLengthmax(ordInfo.getLengthmax());
			nkParam.setLengthmin(ordInfo.getLengthmin());
			nkParam = tolerOfNk.queryTolerance(nkParam, ordInfo.getDesginKey(),
					ordInfo.getIndexSeq(), "D");
			if (nkParam != null) {
				if (nkParam.getWallUpper() == 0 && nkParam.getWallLower() == 0) {
					// 获取交货标准壁厚公差
					// CoreOrderStdSic stdTolerance = new CoreOrderStdSic();
					// Double[] stdTole = null;
					// stdTole = stdTolerance.getStdTole(ordInfo.getDesginKey(),
					// ordInfo.getOutdiameter(), ordInfo.getWallthick());
					// if (stdTole != null) {
					// ordEntity.setWallUpper(stdTole[0]);
					// ordEntity.setWallLower(stdTole[1]);
					// }
					// else {
					// ordEntity.setWallUpper(0);
					// ordEntity.setWallLower(0);
					// }

					ordEntity.setWallUpper(0);
					ordEntity.setWallLower(0);
					zgEntity.setThicknessWall("");
					// errCode = -1;
					// errMessage = "未获取到内控壁厚公差。";
					// return inWeight + addWeight;
				} else {
					ordEntity.setWallUpper(nkParam.getWallUpper());
					ordEntity.setWallLower(nkParam.getWallLower());
					zgEntity.setThicknessWall("-"
							+ new BigDecimal(Math.abs(100
									* ordEntity.getWallLower() / wallthickIn))
									.setScale(2, RoundingMode.HALF_UP)
									.doubleValue()
							+ "%~+"
							+ new BigDecimal(100 * ordEntity.getWallUpper()
									/ wallthickIn).setScale(2,
									RoundingMode.HALF_UP).doubleValue() + "%");
				}
			}
			if (ordInfo.getShrtStove() != null
					&& !ordInfo.getShrtStove().equals("")
					&& ordInfo.getShrtStove().equals("/")) {
				ordEntity.setShrtstove(true);
			}
			// 轧管内外径公差
			nkParam.setBias_code("404012");
			nkParam.setOutdiameter(outdiameterIn);
			nkParam.setWallthick(wallthickIn);
			nkParam.setSic(sic);
			nkParam.setLengthmax(ordInfo.getLengthmax());
			nkParam.setLengthmin(ordInfo.getLengthmin());
			nkParam = tolerOfNk.queryToleranceAim(nkParam, ordInfo
					.getDesginKey(), ordInfo.getIndexSeq(), "D");
			if (nkParam != null) {
				if (nkParam.getAimUpper() == 0 && nkParam.getAimLower() == 0) {
					// // 获取交货标准外径公差
					// CoreOrderStdSic stdTolerance = new CoreOrderStdSic();
					// Double[] stdToleD = null;
					// stdToleD =
					// stdTolerance.getStdOutD(ordInfo.getDesginKey(),
					// ordInfo.getOutdiameter(), ordInfo.getWallthick());
					// if (stdToleD != null) {
					// ordEntity.setAimUpper(stdToleD[0]);
					// ordEntity.setAimLower(stdToleD[1]);
					// }
					// else {
					// ordEntity.setAimUpper(0);
					// ordEntity.setAimLower(0);
					// }
					ordEntity.setAimUpper(0);
					ordEntity.setAimLower(0);
				} else {
					ordEntity.setAimUpper(nkParam.getAimUpper());
					ordEntity.setAimLower(nkParam.getAimLower());
				}
			}

			// 计算轧管投入管坯
			ordEntity.setTubeEndRatio(tubeEndRatio);
			ordEntity.setPlanNum(ordInfo.getPlanNumD());
			ordEntity.setAddNum(addNum);
			ordEntity.setDiameter(outdiameterIn);
			ordEntity.setWallthick(wallthickIn);
			ordEntity.setSigllen(ordInfo.getSiglLen());
			ordEntity.setShortest(ordInfo.getShortest());
			ordEntity.setBurning(burnoutRate);
			ordEntity.setDiame(listDiameter);
			ordEntity.setLengmax(ordInfo.getLengthmax());
			ordEntity.setLengmin(ordInfo.getLengthmin());
			ordEntity.setLengthmax(ordInfo.getLenMax());
			ordEntity.setLengthmin(ordInfo.getLenMin());
			ordEntity.setCutHeadEnd(cropHead + cropEnd);
			ordEntity.setLengthMaxRing(lengthMaxRing);
			ordEntity.setLengthColdbed(lengthColdbed);
			ordEntity.setAvgLenMin(ordInfo.getAvgLenMin());
			ordEntity.setOrdLnDlyPk(ordInfo.getOrdLnDlyPk());
			ordEntity.setPassMame(zgEntity.getPassName());
			ordEntity.setPassCode(zgEntity.getPassCode());
			// ordEntity.setDiameterMandrel(ordInfo.getDiameterMandrel());
			ordEntity.setLengthMinTube(lengthMinTube);
			ordEntity.setLengthMaxTube(lengthMaxTube);
			ordEntity.setToleranceZone(toleranceZone);
			ordEntity.setPlineCode(ordInfo.getPlineD());
			ordEntity.setPlineName(ordInfo.getPlineName());
			ordEntity.setWeighType(ordInfo.getWeighType());
			ordEntity.setOrderUnit(ordInfo.getOrderUnit());
			ordEntity.setDimeterGp(diameterGp);
			ordEntity.setPlnDivideId(ordInfo.getPlnDivideId());
			ordEntity.setCutMax(cutMax);
			ordEntity.setEndlenSpecstd(ordInfo.getEndlenSpecstd());
			ordEntity.setAddWeight(addWeight);
			ordEntity.setPlanWeight(inWeight);
			ordEntity.setRollMonth(ordInfo.getRollMonth());
			ordEntity.setWeigthSMin(inWeightMin);
			// ordEntity.setOrderNo(ordInfo.geto)
			ordCalEntity = PlnZgProcCal.getZgOrdCal(conn, ordEntity, true);
			if (ordCalEntity.getErrCode() == -1) {
				this.errCode = -1;
				this.errMessage = ordCalEntity.getErrMessage();
				return inWeight + addWeight;
			}
			// addWeight = Double.parseDouble(ordCalEntity.getAddWtIn());
			zgEntity.setPassCode(ordCalEntity.getPassCode());
			zgEntity.setPassName(ordCalEntity.getPassMame());
			ordInfo.setDiameterMandrel(ordCalEntity.getDiameterMandrel());
			zgEntity.setVerify1(ordCalEntity.getVerify1());
			zgEntity.setVerify2(ordCalEntity.getVerify2());
			zgEntity.setVerify3(ordCalEntity.getVerify3());
			zgEntity.setGpreqNum(ordCalEntity.getGpreqNum());
			zgEntity.setGpreqWeight(ordCalEntity.getGpreqWeight());
			zgEntity.setGpreqWeightMin(ordCalEntity.getGpreqWeightMin());

			int plansNum = (int) Math.ceil((ordCalEntity.getGpreqNum()
					* ordCalEntity.getCutNum() * (1 - wastRate)));
			zgEntity.setPlansNum(String.valueOf(plansNum));

		} else {
			ordCalEntity
					.setAimZgLen((ordInfo.getLenMin() + ordInfo.getLenMax()) / 2);
		}
		if (addWeight == 0) {
			zgEntity.setAddWtIn("0");
		} else {
			zgEntity.setAddWtIn(String.valueOf(new BigDecimal(addWeight)
					.setScale(3, RoundingMode.HALF_UP)));
		}

		double inWeightI = ordCalEntity.getInvenWt();
		if (inWeightI > 0.000001) {
			prdRate = outWeight / inWeightI;
		}

		// 是否监制
		if (!ordInfo.getPrdcrPrc().equals("")
				&& ordInfo.getPrdcrPrc().contains("D")) {
			zgEntity.setIfMonitorZg("1");
		} else {
			zgEntity.setIfMonitorZg("0");
		}

		ordInfo.setDimeterGp(diameterGp);
		ordInfo.setLengthGp(ordCalEntity.getLenGp());
		ordInfo.setWeightGpOne(ordCalEntity.getWeightGpOne());

		// 附加要求项
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryProcOrdLineAddAsk");
		sql += " AND (T.ASK_ITME = '121001' OR T.ASK_ITME = '121007' OR"
				+ " T.ASK_ITME = '121006' OR T.ASK_ITME = '121003' OR"
				+ " T.ASK_ITME = '121004')";
		ps = conn.prepareStatement(sql);
		ps.setString(1, ordInfo.getPrdcrNo());
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("ADD_ASK") != null) {
				zgEntity.setOrderAddDesc(rs.getString("ADD_ASK"));
			}
		}
		rs.close();
		ps.close();
		zgEntity.setWeigthSMin(inWeightMin);
		zgEntity.setOutdiameterZg(String.valueOf(outdiameterIn));
		zgEntity.setWallthickZg(String.valueOf(wallthickIn));
		zgEntity.setOutdiameter(String.valueOf(ordInfo.getOutdiameter()));
		zgEntity.setWallthick(String.valueOf(ordInfo.getWallthick()));
		zgEntity
				.setAimwallthick(String.valueOf(ordCalEntity.getAimWallthick()));
		zgEntity.setLengthmax(String.valueOf(ordInfo.getLenMax()));
		zgEntity.setLengthmin(String.valueOf(ordInfo.getLenMin()));
		zgEntity.setAimlength(String.valueOf(ordCalEntity.getAimZgLen()));
		zgEntity.setOutLength(String.valueOf(ordCalEntity.getAimZgLen()));
		zgEntity.setRollLength(String.valueOf(ordCalEntity.getLenZz()));
		zgEntity.setOutnumSingle(String.valueOf(ordCalEntity.getCutNum()));
		zgEntity.setDiameterGp(String.valueOf(diameterGp));
		zgEntity.setLengthGp(String.valueOf(ordCalEntity.getLenGp() * 1000));
		zgEntity.setWeightGpOne(String.valueOf(ordInfo.getWeightGpOne()));

		zgEntity.setOrdLnDlyPk(ordInfo.getOrdLnDlyPk());
		zgEntity.setPlnDivideId(ordInfo.getPlnDivideId());
		zgEntity.setCreateName(ordInfo.getCreateName());
		zgEntity.setBom(ordInfo.getBom());
		zgEntity.setPrdcrNo(ordInfo.getPrdcrNo());
		zgEntity.setGetrate(String.valueOf(new BigDecimal(prdRate).setScale(3,
				RoundingMode.HALF_UP)));
		zgEntity.setOrderweight(String.valueOf(new BigDecimal(outWeight)
				.setScale(3, RoundingMode.HALF_UP)));
		zgEntity.setOrdernum(String.valueOf(ordInfo.getPlanNumD()));
		if (ordInfo.getOutsStlPipeFl().trim().equals("121902")
				|| ordInfo.getOutsStlPipeFl().trim().equals("121903")) {
			zgEntity.setIfbuyplan("1");
		} else {
			zgEntity.setIfbuyplan("0");
		}

		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getZgReqOrderNo");
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("ORDER_NO") != null) {
				String proOrderNo = rs.getString("ORDER_NO");
				String[] proPlanId = new String[1];// 工序订单编号
				CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
				PreparedStatement psta = null;
				// 是否末工序
				if ((ordInfo.getWholeBacklog().length() + 1) / 2 == backlogSeq) {
					zgEntity.setIsendgx("1");
				} else {
					int nextBacklogSeq = backlogSeq + 1;
					String sqle = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.queryMscplPrcSeq");
					psta = conn.prepareStatement(sqle);
					psta.setString(1, ordInfo.getDesginKey());
					psta.setString(2, ordInfo.getIndexSeq());
					psta.setString(3, String.valueOf(nextBacklogSeq));
					ResultSet rst = psta.executeQuery();
					if (rst.next()) {
						zgEntity.setNextGx(rst.getString("PROCESS_CODE"));
					}
					zgEntity.setIsendgx("0");
					psta.close();
					rst.close();
				}

				zgEntity.setInNum(String.valueOf(ordCalEntity.getInvenNum()));
				zgEntity.setInWt(String.valueOf(new BigDecimal(inWeight)
						.setScale(3, RoundingMode.HALF_UP)));
				zgEntity.setProOrderNo(proOrderNo);
				String sqlUp = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("CorePlnProcessOrd.addPlnOrderZg"),
						zgEntity);
				psta = conn.prepareStatement(sqlUp);
				psta.executeUpdate();
				psta.close();
				if (ordInfo.getUseDesc() == null
						|| ordInfo.getUseDesc().equals("")
						|| ordInfo.getUseDesc().equals("/")) {

					zgEntity.setProdStandard(ordInfo.getStdName());
				} else {
					zgEntity.setProdStandard(ordInfo.getStdName() + ","
							+ ordInfo.getUseDesc());
				}
				if (ordInfo.getOrderSource().equals("备料需求")) {
					zgEntity.setOutputStandard(ordInfo.getStdName());
				} else {
					if(ordInfo.getOutputStdNo() != null)
					{
						if (ordInfo.getOutputStdNo().equals("122701")) {// 产品标准
							zgEntity.setOutputStandard(ordInfo.getStdName());
						} else if (ordInfo.getOutputStdNo().equals("122702")) {// 用途码描述
							zgEntity.setOutputStandard(ordInfo.getUseDesc());
						} else if (ordInfo.getOutputStdNo().equals("122703")) {// 产品标准+用途码描述
							zgEntity.setOutputStandard(ordInfo.getStdName() + ","
									+ ordInfo.getUseDesc());
						}
					}

				}
				zgEntity.setDecision(ordInfo.getAbc());

				// 工序订单
				if (gxOrdM.addOrdJoinZgM(conn, proOrderNo, ordInfo
						.getVersionNo(), ordInfo.getSerialNum(), ordCalEntity
						.getInvenNum(), inWeight, ordInfo.getCreateName(),
						proPlanId, ordInfo, zgEntity, ordCalEntity)) {
					String sqle = CoreXmlSqlParsersFactory
							.getSql("CorePlnProcessOrd.updateZgOrdGxMNo");
					psta = conn.prepareStatement(sqle);
					psta.setString(1, proPlanId[0]);
					psta.setString(2, proOrderNo);
					psta.executeUpdate();
					psta.close();
				} else {
					errCode = -1;
					errMessage = gxOrdM.getVerrMsg();
					return inWeight + addWeight;
				}
			}
		}
		rs.close();
		ps.close();

		ordInfo.setPlanNumD(ordCalEntity.getInvenNum() + addNum);
		return inWeight + addWeight;
	}

	public static void addJg() {

	}

	/**
	 * 删除加工工序合同
	 * 
	 * @param conn
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @param operaMan
	 * @throws SQLException
	 * @throws ParseException
	 */
	public void deletePlnOrderJgx(Connection conn, String ordLnDlyPk,
			String plnDivideId, String operaMan) throws SQLException,
			ParseException {
		PreparedStatement psta = null;
		// 删除加工工序合同量
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.selectJgxDlyDividePro");
		psta = conn.prepareStatement(sql);
		psta.setObject(1, ordLnDlyPk);
		psta.setObject(2, plnDivideId);
		ResultSet rs = psta.executeQuery();
		while (rs.next()) {
			String proOrderNo = rs.getString("PRO_ORDER_NO");
			PreparedStatement ps = null;
			CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
			gxOrdM.updateJgxGxM(conn, proOrderNo, operaMan);

			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.deletePlnOrderJgx");
			ps = conn.prepareStatement(sql);
			ps.setObject(1, proOrderNo);
			ps.executeUpdate();
			ps.close();
		}
		rs.close();
		psta.close();

	}

	/**
	 * 删除热处理工序合同
	 * 
	 * @param conn
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @param operaMan
	 * @throws SQLException
	 * @throws ParseException
	 */
	public void deletePlnOrderRcl(Connection conn, String ordLnDlyPk,
			String plnDivideId, String operaMan) throws SQLException,
			ParseException {
		PreparedStatement psta = null;
		// 删除热处理工序合同量
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.selectRclDlyDividePro");
		psta = conn.prepareStatement(sql);
		psta.setObject(1, ordLnDlyPk);
		psta.setObject(2, plnDivideId);
		ResultSet rs = psta.executeQuery();
		while (rs.next()) {
			String proOrderNo = rs.getString("PRO_ORDER_NO");
			PreparedStatement ps = null;
			CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
			gxOrdM.updateRclGxM(conn, proOrderNo, operaMan);

			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.deletePlnOrderRcl");
			ps = conn.prepareStatement(sql);
			ps.setObject(1, proOrderNo);
			ps.executeUpdate();
			ps.close();
		}
		rs.close();
		psta.close();
	}

	/**
	 * 删除镦拔扩工序合同
	 * 
	 * @param conn
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @param operaMan
	 * @throws SQLException
	 * @throws ParseException
	 */
	public void deletePlnOrderDbk(Connection conn, String ordLnDlyPk,
			String plnDivideId, String operaMan) throws SQLException,
			ParseException {
		PreparedStatement psta = null;
		// 删除镦拔扩工序合同量
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.selectDbkDlyDividePro");
		psta = conn.prepareStatement(sql);
		psta.setObject(1, ordLnDlyPk);
		psta.setObject(2, plnDivideId);
		ResultSet rs = psta.executeQuery();
		while (rs.next()) {
			String proOrderNo = rs.getString("PRO_ORDER_NO");
			PreparedStatement ps = null;
			CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
			gxOrdM.updateDbkGxM(conn, proOrderNo, operaMan);

			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.deletePlnOrderDbk");
			ps = conn.prepareStatement(sql);
			ps.setObject(1, proOrderNo);
			ps.executeUpdate();
			ps.close();
		}
		rs.close();
		psta.close();
	}

	/**
	 * 删除轧管工序合同
	 * 
	 * @param conn
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @param operaMan
	 * @throws SQLException
	 * @throws ParseException
	 */
	public void deletePlnOrderZg(Connection conn, String ordLnDlyPk,
			String plnDivideId, String operaMan) throws SQLException,
			ParseException {
		PreparedStatement psta = null;
		// 删除轧管工序合同量
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.selectZgDlyDividePro");
		psta = conn.prepareStatement(sql);
		psta.setObject(1, ordLnDlyPk);
		psta.setObject(2, plnDivideId);
		ResultSet rs = psta.executeQuery();
		while (rs.next()) {
			String proOrderNo = rs.getString("PRO_ORDER_NO");
			PreparedStatement ps = null;
			CorePlnGxOrdM gxOrdM = new CorePlnGxOrdM();
			gxOrdM.updateZgGxM(conn, proOrderNo, operaMan);

			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.deletePlnOrderZg");
			ps = conn.prepareStatement(sql);
			ps.setObject(1, proOrderNo);
			ps.executeUpdate();
			ps.close();
		}
		rs.close();
		psta.close();
	}

	/**
	 * 修改版次号
	 * 
	 * @param con
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @param versionNo
	 * @param userName
	 * @param flag
	 * @throws SQLException
	 */
	static void updatePlnSaleordProcVersionNo(Connection con,
			String ordLnDlyPk, String plnDivideId, String versionNo,
			String userName, String flag) throws SQLException {
		String sql = "";
		if (flag.equals("0")) {
			sql = " update PLN_SALEORD_PROC t set t.VERSION_NO=?,t.LAST_UNDOWN_MAN=?,"
					+ "LAST_UNDOWN_TIME=null where t.ORD_LN_DLY_PK=? and t.PLN_DIVIDE_ID=?";
		} else {
			sql = " update PLN_SALEORD_PROC t set t.VERSION_NO=?,t.LAST_UNDOWN_MAN=?,"
					+ "LAST_UNDOWN_TIME=Sysdate where t.ORD_LN_DLY_PK=? and t.PLN_DIVIDE_ID=?";
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, versionNo);
		ps.setObject(2, userName);
		ps.setObject(3, ordLnDlyPk);
		ps.setObject(4, plnDivideId);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 新增版次从表
	 * 
	 * @param con
	 * @param versionNo
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @param versionType
	 * @param orderSource
	 * @param orderNo
	 * @param orderSeq
	 * @param deliveryNo
	 * @param rNumId
	 * @param processSeq
	 * @param operateName
	 * @throws SQLException
	 */
	public static void addPlnOrderDownLog(Connection con, String versionNo,
			String ordLnDlyPk, String plnDivideId, String versionType,
			String orderSource, String orderNo, String orderSeq,
			String deliveryNo, String rNumId, String processSeq,
			String operateName, String rollWellMonth, String acceptNo,
			String ifPassZg, String ifPassJgx) throws SQLException {
		String sql = "insert into pln_order_down_log"
				+ " (version_no, ord_ln_dly_pk, pln_divide_id, version_type, order_source, order_no,"
				+ "	  order_seq, delivery_no, r_num_id, process_seq, operate_name, operate_time,"
				+ "ROLL_WELL_MONTH,ACCEPT_NO,IF_PASS_ZG,IF_PASS_JGX)"
				+ " values"
				+ " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate,?,?,?,?)";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, versionNo);
		ps.setObject(2, ordLnDlyPk);
		ps.setObject(3, plnDivideId);
		ps.setObject(4, versionType);
		ps.setObject(5, orderSource);
		ps.setObject(6, orderNo);
		ps.setObject(7, orderSeq);
		ps.setObject(8, deliveryNo);
		ps.setObject(9, rNumId);
		ps.setObject(10, processSeq);
		ps.setObject(11, operateName);
		ps.setObject(12, rollWellMonth);
		ps.setObject(13, acceptNo);
		ps.setObject(14, ifPassZg);
		ps.setObject(15, ifPassJgx);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询合同排产制程分解表
	 * 
	 * @param con
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @return
	 * @throws SQLException
	 */
	public static PlnSaleordEntityA getPlnSaleordEntityById(Connection con,
			String ordLnDlyPk, String plnDivideId) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getPlnSaleordEntityById");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ps.setObject(2, plnDivideId);
		ResultSet rs = ps.executeQuery();
		PlnSaleordEntityA entity = EntityTool.getEntityByRs(rs,
				PlnSaleordEntityA.class);
		rs.close();
		ps.close();
		return entity;
	}

	/**
	 * 销售合同提报表
	 * 
	 * @param con
	 * @param ordLnDlyPk
	 * @return
	 * @throws SQLException
	 */
	public static PlnSaleordEntity getPlnSaleordByPk(Connection con,
			String ordLnDlyPk) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getPlnSaleOrdByPk");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ResultSet rs = ps.executeQuery();
		PlnSaleordEntity entity = EntityTool.getEntityByRs(rs,
				PlnSaleordEntity.class);
		rs.close();
		ps.close();
		return entity;
	}

	/**
	 * 接箍合同表
	 * 
	 * @param con
	 * @param ordLnDlyPk
	 * @return
	 * @throws SQLException
	 */
	public static PlnOrderJgEntity getPlnOrderJG(Connection con,
			String ordLnDlyPk) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getPlnOrderJG");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ResultSet rs = ps.executeQuery();
		PlnOrderJgEntity entity = EntityTool.getEntityByRs(rs,
				PlnOrderJgEntity.class);
		rs.close();
		ps.close();
		return entity;
	}

	/**
	 * 查询接箍工序订单主表
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static PlnGxJgMEntity getPlnGxJgM(Connection con, String proPlanId)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getPlnGxJgM");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		PlnGxJgMEntity entity = EntityTool.getEntityByRs(rs,
				PlnGxJgMEntity.class);
		rs.close();
		ps.close();
		return entity;
	}

	/**
	 * 查询接箍对应的备料
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static List<PlnJgAndBlEntity> getPlnJgAndBlList(Connection con,
			String proPlanId) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getPlnJgAndBlList");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		List<PlnJgAndBlEntity> list = EntityTool.getEntitysByRs(rs,
				PlnJgAndBlEntity.class);
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 查询接箍工序订单主表
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static PlnPorderBlReqEntity getBlReqById(Connection con,
			String blReqId) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getBlReq");
		sql += " and T.REQ_BL_ID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blReqId);
		ResultSet rs = ps.executeQuery();
		PlnPorderBlReqEntity entity = EntityTool.getEntityByRs(rs,
				PlnPorderBlReqEntity.class);
		rs.close();
		ps.close();
		return entity;
	}

	/**
	 * 查询接箍工序订单主表
	 * 
	 * @param con
	 * @param ordLnDlyPk
	 * @return
	 * @throws SQLException
	 */
	public static List<PlnPorderBlReqEntity> getBlReqByOrdLnPk(Connection con,
			String ordLnDlyPk) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.getBlReq");
		sql += " and T.ORD_LN_DLY_PK = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ResultSet rs = ps.executeQuery();
		List<PlnPorderBlReqEntity> list = EntityTool.getEntitysByRs(rs,
				PlnPorderBlReqEntity.class);
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 错误信息
	 * 
	 * @return the errMessage
	 */
	public String getErrMessage() {
		return errMessage;
	}

	/**
	 * 错误信息
	 * 
	 * @param errMessage
	 *            the errMessage to set
	 */
	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}

	/**
	 * 查询切头尾数据
	 * 
	 * @param inMaterialNo
	 * @param outMaterialNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject queryBaseMaterialDPl(String inMaterialNo,
			String outMaterialNo, String plineCode) {
		Object[] params = new Object[] { inMaterialNo, outMaterialNo, plineCode };
		return this.getDataBySqlID("CorePlnProcessOrd.queryBaseMaterialDPl",
				params);
	}

	/**
	 * 查询芯棒直径数据
	 * 
	 * @param plineCode
	 *            产线
	 * @param passCode
	 *            孔型代码
	 * @param aimoutdiameter
	 *            控制外径
	 * @param aimwallthick
	 *            控制壁厚
	 * @return
	 */
	public CoreReturnObject queryPlnMandrelId(String plineCode,
			String passCode, String aimoutdiameter, String aimwallthick) {
		Object[] params = new Object[] { plineCode, passCode, aimoutdiameter,
				aimwallthick };
		return this.getDataBySqlID("CorePlnProcessOrd.queryPlnMandrelIdSpec",
				params);
	}
}