package com.steering.pss.plnsaleord.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.order.baseMethod.CoreOrdBase;
import com.steering.pss.plnsaleord.order.model.PlnGxJgPropCEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrdInfo;
import com.steering.pss.plnsaleord.order.model.PlnOrderJgEntity;
import com.steering.pss.plnsaleord.processOrder.base.ProducHelperBean;
import com.steering.pss.plnsaleord.processOrder.entity.PlnGxJgPropSEntity;
import com.steering.pss.plnsaleord.processOrder.entity.TimeEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

/**
 * 
 * 新增接箍订单排产耗时
 * 
 * @author tjg
 * @version 1.1
 */
public class CorePlnOrderJgS extends DBCommon {

	// 新增接箍排产订单主表
	public static String addOrderJgS(Connection con, PlnOrdInfo ordInfo,
			String gradeCode, double planPMain) throws SQLException {
		PlnGxJgPropSEntity gxPropS = new PlnGxJgPropSEntity();
		PreparedStatement ps = null;
		ResultSet rs = null;
		double hrCapcty = 0;// 耗时
		double blWtAll = ordInfo.getPlanWtD();// 总备料吨
		int blNumAll = ordInfo.getPlanNumD();// 总备料支
		double blSingwt = 0;// 备料单重
		double jgSingwt = 0;// 接箍单重
		String message = "";
		double jgWtAll = 0;// 总接箍吨
		int jgNumAll = 0;// 总接箍个
		int singleCutNun = 0;// 单支可切个数
		double wtble = blWtAll / blNumAll;
		double aimBl = 0;
		double aimlength = 0;
		// 查询备料合同中个数
		String sql = "SELECT SUM(T.NUM_JG) NUM_JG, SUM(T.WEIGHT_JG) WEIGHT_JG, MIN(NVL(T.SINGLEWEIGHT2,0)) SINGLEWEIGHT2, MIN(NVL(T.SINGLEWEIGHT,0)) SINGLEWEIGHT,MIN(NVL(T.AIMLENGTH_BL,0)) AIMLENGTH_BL,MIN(NVL(T.AIMLENGTH,0)) AIMLENGTH FROM PLN_JG_AND_BL T WHERE T.REQ_BL_ID=?";
		ps = con.prepareStatement(sql);
		ps.setObject(1, ordInfo.getOrdLnDlyPk());
		rs = ps.executeQuery();
		if (rs.next()) {
			jgWtAll = rs.getDouble("WEIGHT_JG");
			jgNumAll = rs.getInt("NUM_JG");
			blSingwt = rs.getDouble("SINGLEWEIGHT2");
			jgSingwt = rs.getDouble("SINGLEWEIGHT");
			aimBl = rs.getDouble("AIMLENGTH_BL");
			aimlength = rs.getDouble("AIMLENGTH");
		}
		rs.close();
		ps.close();

		// singleCutNun = (int) (jgNumAll / blNumAll);
		//singleCutNun = (int) (aimBl / ((aimlength + 15) / 1000)) - 2;//接箍料长度/(接箍长度+15)/1000 -2
		singleCutNun = (int) (aimBl / ((aimlength + 5) / 1000)) - 1;//接箍料长度/(接箍长度+15)/1000 -2
		//singleCutNun = (int) (aimBl / (aimlength) / 1000);//接箍料长度/(接箍长度+15)/1000 -2
		// 通过接箍备料查询接箍合同
		List<PlnOrderJgEntity> orderJg = getPlnOrderJg(con,
				ordInfo.getOrdLnDlyPk(), "");
		if (orderJg == null) {
			message = "未找到接箍合同!";
			return message;
		}
		String proPlanId = "";
		String codeJg = "";// 接箍码
		String demandCopper = "";// 压印.喷标
		String prdcrNo = "";// 监制编号
		int count = 0;

		int numJgS = 0;// 接箍排产个数
		double wtJgS = 0;// 接箍排产吨
		int blNumS = 0;// 接箍备料排产支
		double blWtS = 0;// 接箍备料排产吨
		int i = 0;
		int num = 0;
		double wt = 0;
		for (PlnOrderJgEntity plnJg : orderJg) {
			double ordNumJg = Double.parseDouble(plnJg.getOrdernum());// 接箍合同个数
			double orderNum = 0;
			// 查询接箍订单对应接箍合同总个数
			String orderJgNum = "SELECT SUM(T.ORDERNUM) ORDERNUM FROM PLN_ORDER_JG T WHERE T.PRO_PLAN_ID=?";
			ps = con.prepareStatement(orderJgNum);
			ps.setObject(1, plnJg.getProPlanId());
			rs = ps.executeQuery();
			if (rs.next()) {
				orderNum = rs.getDouble("ORDERNUM");
			}
			rs.close();
			ps.close();

			numJgS = (int) Math.round((ordNumJg / orderNum)
					* Integer.parseInt(plnJg.getNumJg()));//接箍合同个数/接箍合同总个数  * 单比接箍合同个数
			wtJgS = numJgS * jgSingwt / 1000;

			blNumS = (int) Math.round((double) numJgS / (double) singleCutNun);
			if (blNumS == 0) {
				blNumS = 1;
			}
			blWtS = blNumS * wtble;//接箍合同个数/接箍合同总个数  * 单比接箍合同个数 /(接箍料长度/(接箍长度+15)/1000 -2) * 总备料吨/总备料支
			wt += blWtS;
			i++;
			if (orderJg.size() == i) {
				if (wt - blWtAll < 0) {
					blNumS = blNumAll - num + blNumS;
					blWtS = blNumS * wtble;
				}
			}

			// String[] param = getSinBlToJgNum(con, plnJg.getCodeJg(), Integer
			// .parseInt(plnJg.getNumJg()), plnJg.getCodeJgBl());
			// if (param.length <= 0) {
			// message = "未!";
			// return message;
			// }
			// 判断接箍码相同
			if (codeJg.equals("") || codeJg.equals(plnJg.getCodeJg())) {
				// 判断压印相同
				if (demandCopper.equals("")
						|| demandCopper.equals(plnJg.getDemandCopper())) {
					// 判断监制相同
					if (prdcrNo.equals("")
							|| prdcrNo.equals(plnJg.getPrdcrNo())) {
						count++;
					}
				}
			}
			codeJg = plnJg.getCodeJg();
			demandCopper = plnJg.getDemandCopper();
			prdcrNo = plnJg.getPrdcrNo();
			if (count <= 1) {
				gxPropS.setCodeJg(plnJg.getCodeJg());
				// 产线最大订单号
				String sqlMaxPln = CoreXmlSqlParsersFactory
						.getSql("CorePlnOrderJgS.selectProPlanIdMax");
				ps = con.prepareStatement(sqlMaxPln);
				rs = ps.executeQuery();
				if (rs.next()) {
					proPlanId = rs.getString("PRO_PLAN_ID");
				}
				rs.close();
				ps.close();
				gxPropS.setJgGroupId(plnJg.getJgGroupId());
				gxPropS.setProPlanId(proPlanId);
				gxPropS.setGxPlanNo("1");
				gxPropS.setPlineCode(plnJg.getPlineCode());
				gxPropS.setPlineName(plnJg.getPlineName());
				gxPropS.setSpecJg(plnJg.getSpecJg());
				gxPropS.setSpecJgDesc(plnJg.getSpecJgDesc());
				gxPropS.setCodeJgBl(plnJg.getCodeJgBl());
				gxPropS.setSteelcode(plnJg.getSteelcode());
				gxPropS.setSteelname(plnJg.getSteelname());
				// 查询钢种
				String sqlGrade = "SELECT T.GRADENAME FROM COM_BASE_GRADE T WHERE T.GRADECODE=? AND T.VALIDFLAG='1'";
				ps = con.prepareStatement(sqlGrade);
				ps.setObject(1, gradeCode);
				rs = ps.executeQuery();
				if (rs.next()) {
					gxPropS.setGradecode(gradeCode);
					gxPropS.setGradename(rs.getString("GRADENAME"));
				}
				rs.close();
				ps.close();
				gxPropS.setMinD(plnJg.getMinD());
				gxPropS.setMinH(plnJg.getMinH());
				gxPropS.setAimlengthBl(plnJg.getAimlengthBl());
				gxPropS.setAimCutLen(plnJg.getAimlengthBl());
				gxPropS.setLengthMin(plnJg.getLengthMin());
				gxPropS.setLengthMax(plnJg.getLengthMax());
				gxPropS.setTaper(plnJg.getTaper());
				gxPropS.setModelCode(plnJg.getModelCode());
				gxPropS.setModelDesc(plnJg.getModelDesc());
				gxPropS.setDemandCopper(plnJg.getDemandCopper());
				gxPropS.setGroupNumS(String.valueOf(numJgS));
				gxPropS.setGroupWtS(String.valueOf(wtJgS));
				gxPropS.setSingleCutNun(String.valueOf(singleCutNun));
				gxPropS.setGroupBlNumS(String.valueOf(blNumS));
				gxPropS.setGroupBlWtS(String.valueOf(blWtS));
				gxPropS.setProMonth(ordInfo.getRollMonth());
				gxPropS.setTotalCost("");
				gxPropS.setProcessingCost("");
				if (plnJg.getMonitordemand1().equals("接箍监制。")) {
					gxPropS.setIfMonitorJg(plnJg.getIfMonitorJg());
				} else if (plnJg.getMonitordemand1().equals("接箍、接箍料监制。")) {
					gxPropS.setIfMonitorJg(plnJg.getIfMonitorJg());
					gxPropS.setIfMonitorBl(plnJg.getIfMonitorJg());
				} else if (plnJg.getMonitordemand1().equals("接箍料监制。")) {
					gxPropS.setIfMonitorBl(plnJg.getIfMonitorJg());
				} else {
					gxPropS.setIfMonitorJg("0");
					gxPropS.setIfMonitorBl("0");
				}
				gxPropS.setPrdcrNo(plnJg.getPrdcrNo());
				gxPropS.setOrdLnDlyPk(ordInfo.getOrdLnDlyPk());
				gxPropS.setPlnDivideId(ordInfo.getPlnDivideId());
				gxPropS.setBlWlId(plnJg.getBlWlId());
				gxPropS.setBlWlDesc(plnJg.getBlWlDesc());
				gxPropS.setReqBlId(ordInfo.getOrdLnDlyPk());
				gxPropS.setVersionNo(ordInfo.getVersionNo());
				gxPropS.setCodeBlAll(plnJg.getAllCodeBl());
				// 查询接箍信息
				String sqlCodeJg = CoreXmlSqlParsersFactory
						.getSql("PlanFurnProduction.queryCodeJg");
				ps = con.prepareStatement(sqlCodeJg);
				ps.setObject(1, gxPropS.getCodeJg());
				rs = ps.executeQuery();
				if (rs.next()) {
					gxPropS.setStyleJg(rs.getString("STYLE_JG"));
					gxPropS.setProduccodeJg(rs.getString("PRODUCCODE_JG"));
					gxPropS.setProduccodeJgDesc(rs
							.getString("PRODUCCODE_JG_DESC"));
					gxPropS.setStdCode(rs.getString("STD_CODE"));
					gxPropS.setStdName(rs.getString("STD_NAME"));
					gxPropS.setStdStyle(rs.getString("STD_STYLE"));
					gxPropS.setStdStyleDesc(rs.getString("STD_STYLE_DESC"));
				}
				rs.close();
				ps.close();
				String decision = "B";
				// 查询排产分解表工艺文件
				String sqlPlnS = CoreXmlSqlParsersFactory
						.getSql("CorePlnOrderJgS.queryPlnSarCarft");
				ps = con.prepareStatement(sqlPlnS);
				ps.setObject(1, ordInfo.getDesginKey());
				ps.setObject(2, ordInfo.getOrdLnDlyPk());
				ps.setObject(3, ordInfo.getPlnDivideId());
				rs = ps.executeQuery();
				if (rs.next()) {
					gxPropS.setCraftFileMan(rs.getString("CRAFT_FILE_MAN"));
					gxPropS.setCraftFileNo(rs.getString("CRAFT_FILE_NO"));
					gxPropS.setCraftSeq(rs.getString("CRAFT_SEQ"));
					gxPropS.setStationRoad(rs.getString("STATION_ROAD"));
//					if (rs.getString("PROCESS_SEQ").equals("H")) {
//						decision = "A";
//					}
				}
				rs.close();
				ps.close();

				sql  = CoreXmlSqlParsersFactory.getSql("CorePlnOrderJgS.queryProType");
				ps = con.prepareStatement(sql);
				ps.setObject(1,ordInfo.getOrdLnDlyPk());
				rs = ps.executeQuery();
				if(rs.next())
				{
					if(rs.getString("PRODUC_TYPE").equals("D"))
					{
						decision = "A";
					}
				}
				rs.close();
				ps.close();

				double wasteRate = 0;
				String outwlDesc = "";
				String outwlId = "";
				double capacity = 0;// 小时能力
				if (decision.equals("B")) {
					// 查询产出物料
					String sqlwl = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrderJgS.selectJgMaterialPl");
					sqlwl += " ORDER BY T.PRD_RATE DESC, T.WASTE_RATE ASC";
					ps = con.prepareStatement(sqlwl);
					ps.setObject(1, gxPropS.getCodeJgBl());// 备料码
					ps.setObject(2, plnJg.getCodeJg());
					ps.setObject(3, gxPropS.getPlineCode());// 产线
					rs = ps.executeQuery();
					if (rs.next()) {
						outwlDesc = rs.getString("OUT_MATERIAL_DESC");
						outwlId = rs.getString("OUT_MATERIAL_NO");
						wasteRate = rs.getDouble("WASTE_RATE") / 10000;
						capacity = rs.getDouble("HR_CAPCTY");
					}
					rs.close();
					ps.close();
					if (outwlDesc == null || outwlDesc.equals("")) {
						message = "合同号:" + ordInfo.getOrdLnDlyPk()
								+ ",未找到接箍加工产出物料。";
						return message;
					}
				} else if (decision.equals("A")) {
					String sqlwl = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrderJgS.selectJgMaterialPl");
					sqlwl += " ORDER BY T.PRD_RATE DESC, T.WASTE_RATE ASC";
					ps = con.prepareStatement(sqlwl);
					ps.setObject(1, gxPropS.getCodeJgBl());
					ps.setObject(2, plnJg.getCodeJg());
					ps.setObject(3, gxPropS.getPlineCode());// 产线
					rs = ps.executeQuery();
					if (rs.next()) {
						outwlDesc = rs.getString("OUT_MATERIAL_DESC");
						outwlId = rs.getString("OUT_MATERIAL_NO");
						wasteRate = rs.getDouble("WASTE_RATE") / 10000;
						capacity = rs.getDouble("HR_CAPCTY");
					}
					rs.close();
					ps.close();
					if (outwlDesc == null || outwlDesc.equals("")) {
						message = "合同号:" + ordInfo.getOrdLnDlyPk()
								+ ",未找到成品接箍加工产出物料。";
						return message;
					}
				}
				gxPropS.setOutwlDesc(outwlDesc);
				gxPropS.setOutwlId(outwlId);
				// 查询排产分解表工艺文件
				String sqlMonit = CoreXmlSqlParsersFactory
						.getSql("CorePlnOrderJgS.queryPlnMonit");
				ps = con.prepareStatement(sqlMonit);
				ps.setObject(1, ordInfo.getOrdLnDlyPk());
				rs = ps.executeQuery();
				if (rs.next()) {
					gxPropS.setMonitorPoint(rs.getString("MONITOR_POINT"));
				}
				rs.close();
				ps.close();

				ProducHelperBean helperBean = new ProducHelperBean();
				int gxProSeq = helperBean.getMaxGxProSeq(con,
						gxPropS.getPlineCode(), "H", ordInfo.getRollMonth());

				// 耗时=需坯支*小时能力
				if (capacity <= 0) {
					hrCapcty = 0;
				} else {
					hrCapcty = Integer.parseInt(gxPropS.getGroupBlNumS()) * 60
							/ capacity;
				}
				// 3.1计划开始时间(这个时间应该从数据库里查)
				String planTimeB = helperBean.getMaxPlanTimeE(con,
						gxPropS.getPlineCode(), "H", ordInfo.getRollMonth(),
						gxProSeq);
				if (planTimeB.equals("")) {
					message = "无接箍开始时间。";
					return message;
				}
				TimeEntity TimeEntiy;
				try {
					TimeEntiy = helperBean.calculateTime(
							Integer.parseInt(gxPropS.getGroupNumS()), capacity,
							planTimeB);

					// 计划结束时间
					String planTimeE = TimeEntiy.getEndTime();
					// 修改序号
					helperBean.seqIncrease(con, gxPropS.getPlineCode(),
							gxProSeq, -1, 1, "H");
					// 时间自动调整
					helperBean.timeIncrease(con, gxProSeq,
							gxPropS.getPlineCode(), planTimeE, "H");
					gxPropS.setPlanUsetime(String.valueOf(hrCapcty));
					gxPropS.setPlanTimeB(planTimeB);
					gxPropS.setPlanTimeE(planTimeE);
					gxPropS.setGxProSeq(String.valueOf(gxProSeq));
					gxPropS.setOrderSource("备料需求");
					gxPropS.setDecision(decision);
					String sqlAdd = EntityTool.fillSqlByEntity(
							CoreXmlSqlParsersFactory
									.getSql("CorePlnOrderJgS.addPlnOrderJg"),
							gxPropS);
					ps = con.prepareStatement(sqlAdd);
					ps.executeUpdate();
					ps.close();

				} catch (ParseException e) {
					e.printStackTrace();
					message = e.getMessage();
					return message;
				}
			} else {
				// 修改接箍排产个,支
				String update = CoreXmlSqlParsersFactory
						.getSql("CorePlnOrderJgS.updatePlnOrderJg");
				ps = con.prepareStatement(update);
				ps.setObject(1, numJgS);
				ps.setObject(2, wtJgS);
				ps.setObject(3, blNumS);
				ps.setObject(4, blWtS);
				ps.setObject(5, proPlanId);
				ps.executeUpdate();
				ps.close();
			}
			// 修改接箍合同排产订单号
			String updateOrder = " UPDATE PLN_ORDER_JG T SET T.PRO_PLAN_ID_S=? WHERE T.JG_ORDERNO=?";
			ps = con.prepareStatement(updateOrder);
			ps.setObject(1, proPlanId);
			ps.setObject(2, plnJg.getJgOrderno());
			ps.executeUpdate();
			ps.close();
		}
		// 生成接箍订单从表信息
		message = addOrderJgC(con, ordInfo);
		// 查询备料需求
		return message;
	}

	// 新增接箍排产订单从表（订单主表下发生成）
	public static String addOrderJgC(Connection con, PlnOrdInfo ordInfo)
			throws SQLException {
		String message = "";
		PlnGxJgPropCEntity gxPropC = new PlnGxJgPropCEntity();
		PreparedStatement ps = null;
		ResultSet rs = null;
		// 查询同备料合同下有几条接箍订单
		List<PlnGxJgPropSEntity> jgPlanId = getPlnJgPropS(con,
				ordInfo.getOrdLnDlyPk(), ordInfo.getPlnDivideId());
		if (jgPlanId == null) {
			message = "合同号:" + ordInfo.getOrdLnDlyPk() + ",未找到接箍排产订单信息!";
			return message;
		}
		for (PlnGxJgPropSEntity jgPlan : jgPlanId) {

			double jgWtAll = Double.parseDouble(jgPlan.getGroupWtS());// 总接箍吨
			int jgNumAll = Integer.parseInt(jgPlan.getGroupNumS());// 总接箍个

			double plnWt = Double.parseDouble(jgPlan.getGroupBlWtS());// 总备料重量
			int plnNum = Integer.parseInt(jgPlan.getGroupBlNumS());// 总备料支数

			int i = 0;
			int num = 0;
			String demandDetection = "";// 探伤
			String demandBonderizing = "";// 磷化、镀铜
			String demandBlast = "";// 喷砂
			int countC = 0;
			String codeJg = "";// 接箍码
			String demandCopper = "";// 压印.喷标
			String prdcrNo = "";// 监制编号
			int numJgS = 0;// 从表接箍个数
			double wtJgS = 0;// 从表接箍吨
			int blNumS = 0;
			double blWtS = 0;
			int singleCutNun = Integer.parseInt(jgPlan.getSingleCutNun());
			// 通过接箍备料查询接箍合同
			List<PlnOrderJgEntity> orderJg = getPlnOrderJg(con,
					jgPlan.getOrdLnDlyPk(), jgPlan.getProPlanId());
			if (orderJg == null) {
				message = "未找到接箍合同!";
				return message;
			}
			int seq = 1;
			String key = "";
			double wt = 0;
			// 总系数
			double plnCoeff = plnWt / plnNum;
			for (PlnOrderJgEntity plnJg : orderJg) {
				double ordNumJg = Double.parseDouble(plnJg.getOrdernum());// 接箍合同个数
				double orderNum = 0;
				// 查询接箍订单对应接箍合同总个数
				String orderJgNum = "SELECT SUM(T.ORDERNUM) ORDERNUM FROM PLN_ORDER_JG T WHERE T.PRO_PLAN_ID=?";
				ps = con.prepareStatement(orderJgNum);
				ps.setObject(1, plnJg.getProPlanId());
				rs = ps.executeQuery();
				if (rs.next()) {
					orderNum = rs.getDouble("ORDERNUM");
				}
				rs.close();
				ps.close();

				numJgS = (int) Math.round((ordNumJg / orderNum) * jgNumAll);
				wtJgS = numJgS * jgWtAll / jgNumAll;
				num += numJgS;
				i++;
				blNumS = (int) Math.round((double) numJgS
						/ (double) singleCutNun);
				blWtS = blNumS * plnCoeff;
				wt += blWtS;
				i++;
				if (orderJg.size() == i) {
					if (jgNumAll != num) {
						numJgS = jgNumAll - num + numJgS;
						wtJgS = numJgS * jgWtAll / jgNumAll;
					}
					if (wt - plnWt < 0) {
						blNumS = plnNum - num / singleCutNun + blNumS;
						blWtS = blNumS * plnCoeff;
					}
				}
				// 判断接箍码相同
				if (codeJg.equals("") || codeJg.equals(plnJg.getCodeJg())) {

					// 判断压印相同
					if (demandCopper.equals("")
							|| demandCopper.equals(plnJg.getDemandCopper())) {

						// 判断监制相同
						if (prdcrNo.equals("")
								|| prdcrNo.equals(plnJg.getPrdcrNo())) {
							// 生成接箍排产订单从表条件
							// 判断探伤相同
							if (demandDetection.equals("")
									|| demandDetection.equals(plnJg
											.getDemandDetection())) {
								// 判定磷化、镀铜相同
								if (demandBonderizing.equals("")
										|| demandBonderizing.equals(plnJg
												.getDemandBonderizing())) {

									// 判定喷砂相同
									if (demandBlast.equals("")
											|| demandBlast.equals(plnJg
													.getDemandBlast())) {
										countC++;
									}
								}
							}
						}
					}
				}
				codeJg = plnJg.getCodeJg();
				prdcrNo = plnJg.getPrdcrNo();
				demandCopper = plnJg.getDemandCopper();
				demandDetection = plnJg.getDemandDetection();
				demandBonderizing = plnJg.getDemandBonderizing();
				demandBlast = plnJg.getDemandBlast();
				if (countC <= 1) {
					key = jgPlan.getProPlanId() + String.format("%02d", seq);
					gxPropC.setKey(key);
					gxPropC.setProPlanId(jgPlan.getProPlanId());
					gxPropC.setPlineCode(jgPlan.getPlineCode());
					gxPropC.setPlineName(jgPlan.getPlineName());
					gxPropC.setGroupNumS(String.valueOf(numJgS));
					gxPropC.setGroupWtS(String.valueOf(wtJgS));
					gxPropC.setCodeJg(codeJg);
					gxPropC.setDemandBlast(plnJg.getDemandBlast());
					gxPropC.setDemandBonderizing(plnJg.getDemandBonderizing());
					gxPropC.setDemandCopper(plnJg.getDemandCopper());
					gxPropC.setDemandDetection(plnJg.getDemandDetection());
					gxPropC.setIfMonitorJg(plnJg.getIfMonitorJg());
					gxPropC.setSingleCutNun(jgPlan.getSingleCutNun());
					gxPropC.setPlanNum(String.valueOf(blNumS));
					gxPropC.setPlanWt(String.valueOf(blWtS));
					String addSql = EntityTool.fillSqlByEntity(
							CoreXmlSqlParsersFactory
									.getSql("CorePlnOrderJgS.addPlnOrderJgC"),
							gxPropC);
					ps = con.prepareStatement(addSql);
					ps.executeUpdate();
					ps.close();
					seq++;

				} else {
					// 修改接箍排产个,支
					String update = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrderJgS.updatePlnOrderJgC");
					ps = con.prepareStatement(update);
					ps.setObject(1, numJgS);
					ps.setObject(2, wtJgS);
					ps.setObject(3, blNumS);
					ps.setObject(4, blWtS);
					ps.setObject(5, key);
					ps.executeUpdate();
					ps.close();
				}
				// 修改接箍合同排产订单号从表
				String updateOrder = " UPDATE PLN_ORDER_JG T SET T.KEY=? WHERE T.JG_ORDERNO=? AND T.PRO_PLAN_ID_S=?";
				ps = con.prepareStatement(updateOrder);
				ps.setObject(1, key);
				ps.setObject(2, plnJg.getJgOrderno());
				ps.setObject(3, jgPlan.getProPlanId());
				ps.executeUpdate();
				ps.close();

			}
		}
		return message;
	}

	/**
	 * 单支备料管切默认切管个数
	 * 
	 * @throws SQLException
	 */
	public static String[] getSinBlToJgNum(Connection conn, String codeJg,
			int numJg, String codeBl) throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		String[] param = new String[4];
		double lengthJg = 0;// 接箍长度
		double aimLenBl = 0;// 备料目标长度
		int numJgSinBl = 0;
		double outdimeter = 0;
		double wallthick = 0;
		double wtOfMiBl = 0;// 备料米单重
		double sinBlWt = 0;// 备料单支重量
		// int numJg = Integer.parseInt(propEntiy.getGroupNum());
		int numBl = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.selectComMscJg");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, codeJg);
		rs = ps.executeQuery();
		if (!rs.next()) {
			return param;
		}
		if (rs.getString("LENGTH_JG") != null) {
			lengthJg = Double.parseDouble(rs.getString("LENGTH_JG"));
		}
		rs.close();
		ps.close();

		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.selectComMscBl");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, codeBl);
		rs = ps.executeQuery();
		if (!rs.next()) {
			return param;
		}
		outdimeter = rs.getDouble("MIN_D");
		wallthick = rs.getDouble("MIN_H");
		aimLenBl = rs.getDouble("AIM_LEN");
		if (lengthJg == 0) {
			return param;
		}
		numJgSinBl = (int) Math.floor(aimLenBl / (lengthJg + 10));
		wtOfMiBl = CoreOrdBase.WeightOfMi(outdimeter, wallthick);
		sinBlWt = aimLenBl * wtOfMiBl / 1000;
		if (numJgSinBl > 0) {
			numBl = (int) Math.ceil(numJg
					/ Double.parseDouble(String.valueOf(numJgSinBl)));
		}
		rs.close();
		ps.close();
		param[0] = String.valueOf(numBl);// 备料支数
		param[1] = String.valueOf(numBl * sinBlWt);// 备料重量
		param[2] = String.valueOf(aimLenBl / 1000);// 接箍料目标长度
		param[3] = String.valueOf(numJgSinBl);// 切管个数
		return param;
	}

	/**
	 * 查询接箍合同信息
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static List<PlnOrderJgEntity> getPlnOrderJg(Connection con,
			String reqBlId, String proPlanId) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrderJgS.QueryPlnOrderJg");
		if (!proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID_S='" + proPlanId + "'";
		}
		sql += "  ORDER BY T.CODE_JG,T.PRDCR_NO,T.DEMAND_COPPER ASC";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, reqBlId);
		ResultSet rs = ps.executeQuery();
		List<PlnOrderJgEntity> list = EntityTool.getEntitysByRs(rs,
				PlnOrderJgEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnOrderJgEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 查询接箍订单
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static List<PlnGxJgPropSEntity> getPlnJgPropS(Connection con,
			String ordLnDlyPk, String plnDivideId) throws SQLException {
		int str = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrderJgS.QueryPlnJgPropS");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ps.setObject(2, plnDivideId);
		ResultSet rs = ps.executeQuery();
		List<PlnGxJgPropSEntity> list = EntityTool.getEntitysByRs(rs,
				PlnGxJgPropSEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnGxJgPropSEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}
}