package com.steering.pss.plnsaleord.order;

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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
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.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.order.model.PlnOrdInfo;
import com.steering.pss.plnsaleord.stove.entity.PlnOrderDbkSEntity;
import com.steering.pss.plnsaleord.stove.entity.PlnOrderJgxSEntity;
import com.steering.pss.plnsaleord.stove.entity.PlnOrderRclSEntity;

/**
 * @author tjg 2018.05.4
 */
public class FrmProdSchProcAmoun extends DBCommon {
	/**
	 * 查询排产信息
	 * 
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @return object
	 */
	public CoreReturnObject query(String[] plineCode, String proPlanId,
			String orderNo, String[] statusArray, String comPlineCode,
			String processCode) {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryDbk");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryRcl");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryJgx");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryJg");

		}
		if (!comPlineCode.equals("")) {
			sql += " AND T.PLINE_CODE='" + comPlineCode + "' ";
		} else {
			sql += CoreConverter.toSqlConditionString("PLINE_CODE", plineCode);
		}
		if (!proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID LIKE '%'||'" + proPlanId + "'||'%'";
		}
		if (!orderNo.equals("")) {
			if (processCode.equals("H")) {
				sql += " AND T.REQ_BL_ID LIKE '%'||'" + orderNo + "'||'%'";
			} else {
				sql += " AND T.ORDER_NO||'/'||T.ORDER_SEQ LIKE '%'||'"
						+ orderNo + "'||'%'";
			}

		}
		if (statusArray.length > 0) {
			sql += CoreConverter.toSqlConditionString("T.EXE_STATUS",
					statusArray);
		}
		sql += " AND T.ORDER_SOURCE IN ('销售合同','补量需求','备料需求','补量需求A')";
		sql += "  ORDER BY  T.PLINE_CODE,TO_NUMBER(T.GX_PRO_SEQ)";
		return this.getData(sql);
	}

	/**
	 * 查询补量信息
	 * 
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @return object
	 */
	public CoreReturnObject queryBl(String[] plineCode, String proPlanId,
			String orderNo, String[] statusArray, String comPlineCode,
			String processCode) {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryDbk");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryRcl");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryJgx");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryJg");

		}
		if (!comPlineCode.equals("")) {
			sql += " AND T.PLINE_CODE='" + comPlineCode + "' ";
		} else {
			sql += CoreConverter.toSqlConditionString("PLINE_CODE", plineCode);
		}
		if (!proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID LIKE '%'||'" + proPlanId + "'||'%'";
		}
		if (!orderNo.equals("")) {
			if (processCode.equals("H")) {
				sql += " AND T.REQ_BL_ID LIKE '%'||'" + orderNo + "'||'%'";
			} else {
				sql += " AND T.ORDER_NO||'/'||T.ORDER_SEQ LIKE '%'||'"
						+ orderNo + "'||'%'";
			}
		}
		if (statusArray.length > 0) {
			sql += CoreConverter.toSqlConditionString("T.EXE_STATUS",
					statusArray);
		}
		sql += " AND T.ORDER_SOURCE ='排产补量'";
		sql += "  ORDER BY  T.PLINE_CODE,TO_NUMBER(T.GX_PRO_SEQ)";
		return this.getData(sql);
	}

	/**
	 * 查询排产补量信息
	 * 
	 * @param ordLnDlyPk
	 * @param plnDivideId
	 * @return object
	 */
	public CoreReturnObject queryPlnBl(String proPlanId, String processCode) {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryDbk");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryRcl");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryJgx");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryJg");

		}
		sql += " AND T.PRO_PLAN_ID ='" + proPlanId + "'";
		sql += " AND T.ORDER_SOURCE ='排产补量'";
		sql += "  ORDER BY  T.PLINE_CODE,TO_NUMBER(T.GX_PRO_SEQ)";
		return this.getData(sql);
	}

	/**
	 * 新增补量(镦拔扩，热处理，加工线,接箍)
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject addAmount(ArrayList<ArrayList<String>> list,
			String processCode) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DateFormat df1 = new SimpleDateFormat("yyyy-MM");
			Date time = new Date();
			String nowTime = df.format(time);
			String sqlOrderP = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.querySaleProcStatus");// 查询排产分解信息
			for (int i = 0; i < list.size(); i++) {
				// String amountName = list.get(i).get(0);
				double amountWet = Double.parseDouble(list.get(i).get(2));// 补量需求吨
				int amountNum = Integer.parseInt(list.get(i).get(1));// 补量需求支
				String proPlanId = list.get(i).get(3);// 订单编号
				String gxPlanNo = list.get(i).get(4);// 排产序号
				PlnOrdInfo ordInfo = new PlnOrdInfo();
				String sql = "";
				if (processCode.equals("E")) {
					PlnOrderDbkSEntity proEntity = getPlanOrderS(conn,
							proPlanId, gxPlanNo, PlnOrderDbkSEntity.class, "E");
					if (proEntity == null) {
						cro.setV_errMsg("订单信息发生改变,请重新获取数据!");
						return cro;
					}
					if (!proEntity.getExeStatus().equals("2")
							&& !proEntity.getExeStatus().equals("3")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq()
								+ ")不是已生产及关闭状态,不能新增补量!");
						return cro;
					}
					String mess = FrmProdSchAmount.canAfterProcess(
							conn,
							proEntity.getOrdLnDlyPk(),
							proEntity.getPlnDivideId(),
							"E",
							proEntity.getWholeBacklogDesc(),
							proEntity.getOrderNo() + "/"
									+ proEntity.getOrderSeq());
					if (!mess.equals("")) {
						cro.setV_errMsg(mess);
						return cro;
					}
					proEntity.setProMonth(df1.format(time));// 赋值当前年月
					ProducHelperBean helperBean = new ProducHelperBean();
					// 1.2验证产线能力
					double[] capacity = helperBean.getPlineCapacity(conn,
							proEntity.getInwlId(), proEntity.getOutwlId(),
							proEntity.getPlineCode(), "E");
					if (capacity[0] <= 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),产线("
								+ proEntity.getPlineName() + ")没有维护产线能力!");
						return cro;
					}

					double oldWeMin = 0;
					if (proEntity.getWeigthSMin() == null
							|| proEntity.getWeigthSMin().equals("")) {
						oldWeMin = 0;
					} else {
						oldWeMin = Double
								.parseDouble(proEntity.getWeigthSMin());
					}
					double weigthSMin = amountWet * oldWeMin
							/ Double.parseDouble(proEntity.getWeigthS());// 排产剩余重量MIN
					double wastRate = 0;// 万只废品
					int addNum = 0;
					double addWt = 0;
					// 查询万只废品支
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmProdSchProcAmoun.queryBaseMaterialEPl");
					ps = conn.prepareStatement(sql);
					ps.setString(1, proEntity.getInwlId());
					ps.setString(2, proEntity.getOutwlId());
					ps.setString(3, proEntity.getPlineCode());
					rs = ps.executeQuery();
					if (rs.next()) {
						wastRate = rs.getInt("WASTE_RATE") / 10000;
					}
					rs.close();
					ps.close();
					addNum = (int) (amountNum * wastRate);
					addWt = amountWet * wastRate;
					// 查询合同信息
					ps = conn.prepareStatement(sqlOrderP);
					ps.setString(1, proEntity.getOrdLnDlyPk());
					ps.setString(2, proEntity.getPlnDivideId());
					rs = ps.executeQuery();
					if (rs.next()) {
						ordInfo.setOrdLnPk(rs.getString("ORD_LN_PK"));
						ordInfo.setDesginKey(rs.getString("DESGIN_KEY"));
						ordInfo.setBom(rs.getString("BOM"));
						ordInfo.setLenCutendCbk(rs.getString("LEN_CUTEND_DBK"));
						ordInfo.setLenCutheadDbk(rs
								.getString("LEN_CUTHEAD_DBK"));
						if (rs.getString("OUTS_STL_PIPE_FL") != null) {
							ordInfo.setOutsStlPipeFl(rs
									.getString("OUTS_STL_PIPE_FL"));
						}
					} else {
						cro.setV_errMsg("未找到合同信息。");
						return cro;
					}
					rs.close();
					ps.close();
					// 获取最新工艺 CIC
					// 2.3查找工艺
					String[] craftInfo = PlnHelper.getArray(conn,
							"CorePlnSaleOrder.queryOrdCraftOrdDesign",
							" AND T.VALIDFLAG = '1'",
							new String[] { ordInfo.getOrdLnPk() },
							new String[] { "CRAFT_SEQ", "DESGIN_KEY",
									"CRAFT_NAME", "CRAFT_NO", "CRAFT_NO_V" });
					if (craftInfo == null || craftInfo.length == 0) {
						cro.setV_errMsg("合同行：" + proEntity.getOrderNo() + "/"
								+ proEntity.getOrderSeq() + "未找到有效的工艺评审！");
						return cro;
					}
					proEntity.setLenCutend(ordInfo.getLenCutendCbk());
					proEntity.setLenCuthead(ordInfo.getLenCutheadDbk());
					proEntity.setCraftSeq(craftInfo[0]);
					proEntity.setCraftFileMan(craftInfo[2]);
					proEntity.setCraftFileNo(craftInfo[3]);
					// 镦拔扩
					String sqlZgCic = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrdProUseTime.queryDbkPineHrCapcty");
					ps = conn.prepareStatement(sqlZgCic);
					ps.setObject(1, proPlanId);
					rs = ps.executeQuery();
					if (rs.next()) {
						if (rs.getString("CIC") != null) {
							proEntity.setCic(rs.getString("CIC"));
						}
						if (rs.getString("PIC") != null) {
							proEntity.setPic(rs.getString("PIC"));
						}
						if (rs.getString("SIC") != null) {
							proEntity.setSic(rs.getString("SIC"));
						}
						if (rs.getString("DIC") != null) {
							proEntity.setDic(rs.getString("DIC"));
						}
						if (rs.getString("WIC") != null) {
							proEntity.setWic(rs.getString("WIC"));
						}
						if (rs.getString("GIC") != null) {
							proEntity.setGic(rs.getString("GIC"));
						}
						if (rs.getString("STATION_ROAD") != null) {
							proEntity.setStationRoad(rs
									.getString("STATION_ROAD"));
						}
					}
					rs.close();
					ps.close();
					// 排产序号
					String gxPlanNo1 = helperBean.getMaxGxPlanNo(conn,
							proEntity.getProPlanId(), "E");
					// 生产顺序
					int gxProSeq = helperBean.getMaxGxProSeq(conn,
							proEntity.getPlineCode(), "E",
							proEntity.getProMonth());
					// 3.1计划开始时间(这个时间应该从数据库里查)
					String planTimeB = helperBean.getMaxPlanTimeE(conn,
							proEntity.getPlineCode(), "E",
							proEntity.getProMonth(), gxProSeq);
					if (planTimeB.equals("")) {
						cro.setV_errMsg("计划开始时间为空,补量失败!");
						return cro;

					}
					TimeEntity TimeEntiy = helperBean.calculateTime(amountNum,
							capacity[0], planTimeB);
					// 计划结束时间
					String planTimeE = TimeEntiy.getEndTime();
					String planUserTime = TimeEntiy.getUserTime();

					helperBean.seqIncrease(conn, proEntity.getPlineCode(),
							gxProSeq, -1, 1, "E");
					// 3.2新增新记录
					proEntity.setGxPlanNo(gxPlanNo1);
					proEntity.setGxProSeq(String.valueOf(gxProSeq));
					proEntity.setWeigthS(String.valueOf(amountWet));
					proEntity.setNumS(String.valueOf(amountNum));
					proEntity.setAddNumIn(String.valueOf(addNum));
					proEntity.setAddWtIn(String.valueOf(addWt));
					proEntity.setInNumS(String.valueOf(amountNum - addNum));
					proEntity.setInWeightS(String.valueOf(amountWet - addWt));
					proEntity.setPlanTimeB(planTimeB);
					proEntity.setPlanTimeE(planTimeE);
					proEntity.setPlanUsetime(planUserTime);
					proEntity.setWeigthSMin(String.valueOf(weigthSMin));
					proEntity.setCreateTime(nowTime);
					proEntity.setOrderSource("排产补量");
					proEntity.setZyBatchId("");
					// 新增镦拔扩排产补量
					String sqlUpAddPln = EntityTool
							.fillSqlByEntity(
									CoreXmlSqlParsersFactory
											.getSql("FrmProdSchProcAmoun.queryAddPlnDbkAmount"),
									proEntity);
					ps = conn.prepareStatement(sqlUpAddPln);
					ps.executeUpdate();
					ps.close();

					// 时间自动调整
					helperBean.timeIncrease(conn,
							Integer.parseInt(proEntity.getGxProSeq()),
							proEntity.getPlineCode(), planTimeE, "E");

				} else if (processCode.equals("F")) {
					PlnOrderRclSEntity proEntity = getPlanOrderS(conn,
							proPlanId, gxPlanNo, PlnOrderRclSEntity.class, "F");
					if (proEntity == null) {
						cro.setV_errMsg("订单信息发生改变,请重新获取数据!");
						return cro;
					}
					if (!proEntity.getExeStatus().equals("2")
							&& !proEntity.getExeStatus().equals("3")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq()
								+ ")不是已生产及关闭状态,不能新增补量!");
						return cro;
					}
					String mess = FrmProdSchAmount.canAfterProcess(
							conn,
							proEntity.getOrdLnDlyPk(),
							proEntity.getPlnDivideId(),
							"F",
							proEntity.getWholeBacklogDesc(),
							proEntity.getOrderNo() + "/"
									+ proEntity.getOrderSeq());
					if (!mess.equals("")) {
						cro.setV_errMsg(mess);
						return cro;
					}
					proEntity.setProMonth(df1.format(time));// 赋值当前年月
					ProducHelperBean helperBean = new ProducHelperBean();
					// 1.2验证产线能力
					double[] capacity = helperBean.getPlineCapacity(conn,
							proEntity.getInwlId(), proEntity.getOutwlId(),
							proEntity.getPlineCode(), "F");
					if (capacity[0] <= 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),产线("
								+ proEntity.getPlineName() + ")没有维护产线能力!");
						return cro;
					}

					double oldWeMin = 0;
					if (proEntity.getWeigthSMin() == null
							|| proEntity.getWeigthSMin().equals("")) {
						oldWeMin = 0;
					} else {
						oldWeMin = Double
								.parseDouble(proEntity.getWeigthSMin());
					}
					double weigthSMin = amountWet * oldWeMin
							/ Double.parseDouble(proEntity.getWeigthS());// 排产剩余重量MIN
					double wastRate = 0;// 万只废品
					int addNum = 0;
					double addWt = 0;
					// 查询万只废品支
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmProdSchProcAmoun.queryBaseMaterialFPl");
					ps = conn.prepareStatement(sql);
					ps.setString(1, proEntity.getInwlId());
					ps.setString(2, proEntity.getOutwlId());
					ps.setString(3, proEntity.getPlineCode());
					rs = ps.executeQuery();
					if (rs.next()) {
						wastRate = rs.getInt("WASTE_RATE") / 10000;
					}
					rs.close();
					ps.close();
					addNum = (int) (amountNum * wastRate);
					addWt = amountWet * wastRate;
					// 查询合同信息
					ps = conn.prepareStatement(sqlOrderP);
					ps.setString(1, proEntity.getOrdLnDlyPk());
					ps.setString(2, proEntity.getPlnDivideId());
					rs = ps.executeQuery();
					if (rs.next()) {
						ordInfo.setOrdLnPk(rs.getString("ORD_LN_PK"));
						ordInfo.setDesginKey(rs.getString("DESGIN_KEY"));
						ordInfo.setBom(rs.getString("BOM"));
						if (rs.getString("OUTS_STL_PIPE_FL") != null) {
							ordInfo.setOutsStlPipeFl(rs
									.getString("OUTS_STL_PIPE_FL"));
						}
					} else {
						cro.setV_errMsg("未找到合同信息。");
						return cro;
					}
					rs.close();
					ps.close();
					// 获取最新工艺 CIC
					// 2.3查找工艺
					String[] craftInfo = PlnHelper.getArray(conn,
							"CorePlnSaleOrder.queryOrdCraftOrdDesign",
							" AND T.VALIDFLAG = '1'",
							new String[] { ordInfo.getOrdLnPk() },
							new String[] { "CRAFT_SEQ", "DESGIN_KEY",
									"CRAFT_NAME", "CRAFT_NO", "CRAFT_NO_V" });
					if (craftInfo == null || craftInfo.length == 0) {
						cro.setV_errMsg("合同行：" + proEntity.getOrderNo() + "/"
								+ proEntity.getOrderSeq() + "未找到有效的工艺评审！");
						return cro;
					}
					proEntity.setCraftSeq(craftInfo[0]);
					proEntity.setCraftFileMan(craftInfo[2]);
					proEntity.setCraftFileNo(craftInfo[3]);
					// 镦拔扩
					String sqlZgCic = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrdProUseTime.queryRclPineHrCapcty");
					ps = conn.prepareStatement(sqlZgCic);
					ps.setObject(1, proPlanId);
					rs = ps.executeQuery();
					if (rs.next()) {
						if (rs.getString("CIC") != null) {
							proEntity.setCic(rs.getString("CIC"));
						}
						if (rs.getString("PIC") != null) {
							proEntity.setPic(rs.getString("PIC"));
						}
						if (rs.getString("SIC") != null) {
							proEntity.setSic(rs.getString("SIC"));
						}
						if (rs.getString("DIC") != null) {
							proEntity.setDic(rs.getString("DIC"));
						}
						if (rs.getString("WIC") != null) {
							proEntity.setWic(rs.getString("WIC"));
						}
						if (rs.getString("GIC") != null) {
							proEntity.setGic(rs.getString("GIC"));
						}
						if (rs.getString("STATION_ROAD") != null) {
							proEntity.setStationRoad(rs
									.getString("STATION_ROAD"));
						}
					}
					rs.close();
					ps.close();
					// 排产序号
					String gxPlanNo1 = helperBean.getMaxGxPlanNo(conn,
							proEntity.getProPlanId(), "F");
					// 生产顺序
					int gxProSeq = helperBean.getMaxGxProSeq(conn,
							proEntity.getPlineCode(), "F",
							proEntity.getProMonth());
					// 3.1计划开始时间(这个时间应该从数据库里查)
					String planTimeB = helperBean.getMaxPlanTimeE(conn,
							proEntity.getPlineCode(), "F",
							proEntity.getProMonth(), gxProSeq);
					if (planTimeB.equals("")) {
						cro.setV_errMsg("计划开始时间为空,补量失败!");
						return cro;

					}
					TimeEntity TimeEntiy = helperBean.calculateTime(amountNum,
							capacity[0], planTimeB);
					// 计划结束时间
					String planTimeE = TimeEntiy.getEndTime();
					String planUserTime = TimeEntiy.getUserTime();

					helperBean.seqIncrease(conn, proEntity.getPlineCode(),
							gxProSeq, -1, 1, "F");
					// 3.2新增新记录
					proEntity.setGxPlanNo(gxPlanNo1);
					proEntity.setGxProSeq(String.valueOf(gxProSeq));
					proEntity.setWeigthS(String.valueOf(amountWet));
					proEntity.setNumS(String.valueOf(amountNum));
					proEntity.setAddNumIn(String.valueOf(addNum));
					proEntity.setAddWtIn(String.valueOf(addWt));
					proEntity.setInNumS(String.valueOf(amountNum - addNum));
					proEntity.setInWeightS(String.valueOf(amountWet - addWt));
					proEntity.setPlanTimeB(planTimeB);
					proEntity.setPlanTimeE(planTimeE);
					proEntity.setPlanUsetime(planUserTime);
					proEntity.setWeigthSMin(String.valueOf(weigthSMin));
					proEntity.setCreateTime(nowTime);
					proEntity.setOrderSource("排产补量");
					proEntity.setZyBatchId("");
					// 新增热处理排产补量
					String sqlUpAddPln = EntityTool
							.fillSqlByEntity(
									CoreXmlSqlParsersFactory
											.getSql("FrmProdSchProcAmoun.queryAddPlnRclAmount"),
									proEntity);
					ps = conn.prepareStatement(sqlUpAddPln);
					ps.executeUpdate();
					ps.close();

					// 时间自动调整
					helperBean.timeIncrease(conn,
							Integer.parseInt(proEntity.getGxProSeq()),
							proEntity.getPlineCode(), planTimeE, "F");
				} else if (processCode.equals("G")) {
					PlnOrderJgxSEntity proEntity = getPlanOrderS(conn,
							proPlanId, gxPlanNo, PlnOrderJgxSEntity.class, "G");
					if (proEntity == null) {
						cro.setV_errMsg("订单信息发生改变,请重新获取数据!");
						return cro;
					}
					if (!proEntity.getExeStatus().equals("2")
							&& !proEntity.getExeStatus().equals("3")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq()
								+ ")不是已生产及关闭状态,不能新增补量!");
						return cro;
					}
					proEntity.setProMonth(df1.format(time));// 赋值当前年月
					ProducHelperBean helperBean = new ProducHelperBean();
					// 1.2验证产线能力
					double[] capacity = helperBean.getPlineCapacity(conn,
							proEntity.getInwlId(), proEntity.getOutwlId(),
							proEntity.getPlineCode(), "G");
					if (capacity[0] <= 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),产线("
								+ proEntity.getPlineName() + ")没有维护产线能力!");
						return cro;
					}

					// double oldWeMin=0;
					// if(proEntity.getWeigthSMin()==null||proEntity.getWeigthSMin().equals("")){
					// oldWeMin=0;
					// }else{
					// oldWeMin=Double.parseDouble(proEntity.getWeigthSMin());
					// }
					// double weigthSMin = amountWet
					// * oldWeMin
					// / Double.parseDouble(proEntity.getWeigthS());// 排产剩余重量MIN
					double wastRate = 0;// 万只废品
					int addNum = 0;
					double addWt = 0;
					// 查询万只废品支
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmProdSchProcAmoun.queryBaseMaterialGPl");
					ps = conn.prepareStatement(sql);
					ps.setString(1, proEntity.getInwlId());
					ps.setString(2, proEntity.getOutwlId());
					ps.setString(3, proEntity.getPlineCode());
					rs = ps.executeQuery();
					if (rs.next()) {
						wastRate = rs.getInt("WASTE_RATE") / 10000;
					}
					rs.close();
					ps.close();
					addNum = (int) (amountNum * wastRate);
					addWt = amountWet * wastRate;
					// 查询合同信息
					ps = conn.prepareStatement(sqlOrderP);
					ps.setString(1, proEntity.getOrdLnDlyPk());
					ps.setString(2, proEntity.getPlnDivideId());
					rs = ps.executeQuery();
					if (rs.next()) {
						ordInfo.setOrdLnPk(rs.getString("ORD_LN_PK"));
						ordInfo.setDesginKey(rs.getString("DESGIN_KEY"));
						ordInfo.setBom(rs.getString("BOM"));
						// ordInfo.setLenCutendCbk(rs.getString("LEN_CUTEND_DBK"));
						// ordInfo.setLenCutheadDbk(rs
						// .getString("LEN_CUTHEAD_DBK"));
						if (rs.getString("OUTS_STL_PIPE_FL") != null) {
							ordInfo.setOutsStlPipeFl(rs
									.getString("OUTS_STL_PIPE_FL"));
						}
					} else {
						cro.setV_errMsg("未找到合同信息。");
						return cro;
					}
					rs.close();
					ps.close();
					// 获取最新工艺 CIC
					// 2.3查找工艺
					String[] craftInfo = PlnHelper.getArray(conn,
							"CorePlnSaleOrder.queryOrdCraftOrdDesign",
							" AND T.VALIDFLAG = '1'",
							new String[] { ordInfo.getOrdLnPk() },
							new String[] { "CRAFT_SEQ", "DESGIN_KEY",
									"CRAFT_NAME", "CRAFT_NO", "CRAFT_NO_V" });
					if (craftInfo == null || craftInfo.length == 0) {
						cro.setV_errMsg("合同行：" + proEntity.getOrderNo() + "/"
								+ proEntity.getOrderSeq() + "未找到有效的工艺评审！");
						return cro;
					}
					// proEntity.setLenCutend(ordInfo.getLenCutendCbk());
					// proEntity.setLenCuthead(ordInfo.getLenCutheadDbk());
					proEntity.setCraftSeq(craftInfo[0]);
					proEntity.setCraftFileMan(craftInfo[2]);
					proEntity.setCraftFileNo(craftInfo[3]);
					// 镦拔扩
					String sqlZgCic = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrdProUseTime.queryJgxPineHrCapcty");
					ps = conn.prepareStatement(sqlZgCic);
					ps.setObject(1, proPlanId);
					rs = ps.executeQuery();
					if (rs.next()) {
						if (rs.getString("CIC") != null) {
							proEntity.setCic(rs.getString("CIC"));
						}
						if (rs.getString("PIC") != null) {
							proEntity.setPic(rs.getString("PIC"));
						}
						if (rs.getString("SIC") != null) {
							proEntity.setSic(rs.getString("SIC"));
						}
						if (rs.getString("DIC") != null) {
							proEntity.setDic(rs.getString("DIC"));
						}
						if (rs.getString("WIC") != null) {
							proEntity.setWic(rs.getString("WIC"));
						}
						if (rs.getString("GIC") != null) {
							proEntity.setGic(rs.getString("GIC"));
						}
						if (rs.getString("STATION_ROAD") != null) {
							proEntity.setStationRoad(rs
									.getString("STATION_ROAD"));
						}
					}
					rs.close();
					ps.close();
					// 排产序号
					String gxPlanNo1 = helperBean.getMaxGxPlanNo(conn,
							proEntity.getProPlanId(), "G");
					// 生产顺序
					int gxProSeq = helperBean.getMaxGxProSeq(conn,
							proEntity.getPlineCode(), "G",
							proEntity.getProMonth());
					// 3.1计划开始时间(这个时间应该从数据库里查)
					String planTimeB = helperBean.getMaxPlanTimeE(conn,
							proEntity.getPlineCode(), "G",
							proEntity.getProMonth(), gxProSeq);
					if (planTimeB.equals("")) {
						cro.setV_errMsg("计划开始时间为空,补量失败!");
						return cro;

					}
					TimeEntity TimeEntiy = helperBean.calculateTime(amountNum,
							capacity[0], planTimeB);
					// 计划结束时间
					String planTimeE = TimeEntiy.getEndTime();
					String planUserTime = TimeEntiy.getUserTime();

					helperBean.seqIncrease(conn, proEntity.getPlineCode(),
							gxProSeq, -1, 1, "G");
					// 3.2新增新记录
					proEntity.setGxPlanNo(gxPlanNo1);
					proEntity.setGxProSeq(String.valueOf(gxProSeq));
					proEntity.setWeigthS(String.valueOf(amountWet));
					proEntity.setNumS(String.valueOf(amountNum));
					proEntity.setAddNumIn(String.valueOf(addNum));
					proEntity.setAddWtIn(String.valueOf(addWt));
					proEntity.setInNumS(String.valueOf(amountNum - addNum));
					proEntity.setInWeightS(String.valueOf(amountWet - addWt));
					proEntity.setPlanTimeB(planTimeB);
					proEntity.setPlanTimeE(planTimeE);
					proEntity.setPlanUsetime(planUserTime);
					// proEntity.setWeigthSMin(String.valueOf(weigthSMin));
					proEntity.setCreateTime(nowTime);
					proEntity.setOrderSource("排产补量");
					proEntity.setZyBatchId("");
					if (proEntity.getBeginTimeZg() != null
							&& !proEntity.getBeginTimeZg().equals("")) {
						proEntity
								.setBeginTimeZg(proEntity.getBeginTimeZg()
										.substring(
												0,
												proEntity.getBeginTimeZg()
														.length() - 2));
					}
					if (proEntity.getDelvryEdate() != null
							&& !proEntity.getDelvryEdate().equals("")) {
						proEntity
								.setDelvryEdate(proEntity.getDelvryEdate()
										.substring(
												0,
												proEntity.getDelvryEdate()
														.length() - 2));
					}

					// 新增加工线排产补量
					String sqlUpAddPln = EntityTool
							.fillSqlByEntity(
									CoreXmlSqlParsersFactory
											.getSql("FrmProdSchProcAmoun.queryAddPlnJgxAmount"),
									proEntity);
					ps = conn.prepareStatement(sqlUpAddPln);
					ps.executeUpdate();
					ps.close();

					// 时间自动调整
					helperBean.timeIncrease(conn,
							Integer.parseInt(proEntity.getGxProSeq()),
							proEntity.getPlineCode(), planTimeE, "G");
				} else if (processCode.equals("H")) {
					PlnGxJgPropSEntity gxPropS = getPlanOrderS(conn, proPlanId,
							gxPlanNo, PlnGxJgPropSEntity.class, "H");
					if (gxPropS == null) {
						cro.setV_errMsg("订单信息发生改变,请重新获取数据!");
						return cro;
					}
					if (!gxPropS.getExeStatus().equals("2")
							&& !gxPropS.getExeStatus().equals("3")) {
						cro.setV_errMsg("备料合同号(" + gxPropS.getOrdLnDlyPk()
								+ ")不是已生产或关闭状态,不能新增补量!");
						return cro;
					}
					ProducHelperBean helperBean = new ProducHelperBean();
					// 排产序号
					String gxPlanNo1 = helperBean.getMaxGxPlanNo(conn,
							proPlanId, "H");
					// 生产顺序
					int gxProSeq = helperBean.getMaxGxProSeq(conn,
							gxPropS.getPlineCode(), "H", gxPropS.getProMonth());
					gxPropS.setGxPlanNo(gxPlanNo1);
					gxPropS.setGroupNumS(String.valueOf(amountNum
							* Integer.parseInt(gxPropS.getSingleCutNun())));
					gxPropS.setGroupWtS(String.valueOf(amountWet));

					gxPropS.setGroupBlNumS(String.valueOf(amountNum));
					gxPropS.setGroupBlWtS(String.valueOf(amountWet));
					gxPropS.setProMonth(ordInfo.getRollMonth());
					// 查询排产分解表工艺文件
					String sqlPlnS = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrderJgS.queryPlnSarCarft");
					ps = conn.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"));
					}
					rs.close();
					ps.close();
					double wasteRate = 0;
					String outwlDesc = "";
					String outwlId = "";
					double capacity = 0;// 小时能力
					// 查询产出物料
					String sqlwl = CoreXmlSqlParsersFactory
							.getSql("CorePlnOrderJgS.selectJgMaterialPl");
					sqlwl += " ORDER BY T.PRD_RATE DESC, T.WASTE_RATE ASC";
					ps = conn.prepareStatement(sqlwl);
					ps.setObject(1, gxPropS.getCodeJgBl());// 备料码
					ps.setObject(2, gxPropS.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("")) {
						cro.setV_errMsg("合同号:" + ordInfo.getOrdLnDlyPk()
								+ ",未找到接箍加工产出物料。");
						return cro;
					}
					gxPropS.setOutwlDesc(outwlDesc);
					gxPropS.setOutwlId(outwlId);
					double hrCapcty = 0;// 耗时
					// 耗时=需坯支*小时能力
					if (capacity <= 0) {
						hrCapcty = 0;
					} else {
						hrCapcty = Integer.parseInt(gxPropS.getGroupBlNumS())
								* 60 / capacity;
					}
					// 3.1计划开始时间(这个时间应该从数据库里查)
					String planTimeB = helperBean.getMaxPlanTimeE(conn,
							gxPropS.getPlineCode(), "H",
							ordInfo.getRollMonth(), gxProSeq);
					if (planTimeB.equals("")) {
						cro.setV_errMsg("无接箍开始时间。");
						return cro;
					}
					TimeEntity TimeEntiy;
					try {
						TimeEntiy = helperBean.calculateTime(
								Integer.parseInt(gxPropS.getGroupNumS()),
								capacity, planTimeB);

						// 计划结束时间
						String planTimeE = TimeEntiy.getEndTime();
						// 修改序号
						helperBean.seqIncrease(conn, gxPropS.getPlineCode(),
								gxProSeq, -1, 1, "H");
						// 时间自动调整
						helperBean.timeIncrease(conn, gxProSeq,
								gxPropS.getPlineCode(), planTimeE, "H");
						gxPropS.setPlanUsetime(String.valueOf(hrCapcty));
						gxPropS.setPlanTimeB(planTimeB);
						gxPropS.setPlanTimeE(planTimeE);
						gxPropS.setGxProSeq(String.valueOf(gxProSeq));
						gxPropS.setOrderSource("排产补量");
						// gxPropS.setIfMonitorBl(ifMonitorBl);
						String sqlAdd = EntityTool
								.fillSqlByEntity(
										CoreXmlSqlParsersFactory
												.getSql("CorePlnOrderJgS.addPlnOrderJg"),
										gxPropS);
						ps = conn.prepareStatement(sqlAdd);
						ps.executeUpdate();
						ps.close();
					} catch (ParseException e) {
						e.printStackTrace();
						cro.setV_errMsg(e.getMessage());
						return cro;
					}

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

	/**
	 * 查询排产信息
	 * 
	 * @param con
	 * @param proPlanId
	 *            订单编号
	 * @param gxPlanNo
	 *            排产序号
	 * @throws SQLException
	 */
	public static <T> T getPlanOrderS(Connection con, String proPlanId,
			String gxPlanNo, Class<T> cls, String processCode)
			throws SQLException {
		String sql = "";
		if (processCode.equals("E")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryAddPlnDbkStrat");
		} else if (processCode.equals("F")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryAddPlnRclStrat");
		} else if (processCode.equals("G")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryAddPlnJgxStrat");
		} else if (processCode.equals("H")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmProdSchProcAmoun.queryAddPlnJgStrat");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ps.setObject(2, gxPlanNo);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 删除补量(镦拔扩，热处理，加工线)
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject deleteAmount(ArrayList<ArrayList<String>> list,
			String processCode) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			String sqlZy = "";
			for (int i = 0; i < list.size(); i++) {
				String proPlanId = list.get(i).get(0);// 订单编号
				String gxPlanNo = list.get(i).get(1);// 排产序号
				if (processCode.equals("E")) {
					PlnOrderDbkSEntity proEntity = getPlanOrderS(conn,
							proPlanId, gxPlanNo, PlnOrderDbkSEntity.class, "E");
					if (proEntity == null) {
						cro.setV_errMsg("排产补量信息发生改变,请重新获取数据!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("2")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已生产完成,不能删除!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("3")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已关闭,不能删除!");
						return cro;
					}
					// 查询是否存在炉计划
					int countNum = 0;
					sqlZy = "SELECT COUNT(*) COUNT_NUM FROM PLN_ZY_DBK_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
					ps = conn.prepareStatement(sqlZy);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					rs = ps.executeQuery();
					if (rs.next()) {
						countNum = rs.getInt("COUNT_NUM");
					}
					rs.close();
					ps.close();

					if (countNum > 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已存在炉计划,不能删除!");
						return cro;
					}
					ProducHelperBean helperBean = new ProducHelperBean();
					// 1.2验证产线能力
					double[] capacity = helperBean.getPlineCapacity(conn,
							proEntity.getInwlId(), proEntity.getOutwlId(),
							proEntity.getPlineCode(), "E");
					if (capacity[0] <= 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),产线("
								+ proEntity.getPlineName() + ")没有维护产线能力!");
						return cro;
					}
					String planTimeB = proEntity.getPlanTimeB();
					// 删除镦拔扩排产补量
					String sqlUpDelete = "DELETE PLN_ORDER_DBK_S WHERE PRO_PLAN_ID = ? AND GX_PLAN_NO = ?";
					ps = conn.prepareStatement(sqlUpDelete);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					ps.executeUpdate();
					ps.close();
					helperBean.seqIncrease(conn, proEntity.getPlineCode(),
							Integer.parseInt(proEntity.getGxProSeq()), -1, -1,
							"E");
					// 时间自动调整
					helperBean.timeIncrease(conn,
							Integer.parseInt(proEntity.getGxProSeq()) - 1,
							proEntity.getPlineCode(), planTimeB, "E");
				} else if (processCode.equals("F")) {
					PlnOrderRclSEntity proEntity = getPlanOrderS(conn,
							proPlanId, gxPlanNo, PlnOrderRclSEntity.class, "F");
					if (proEntity == null) {
						cro.setV_errMsg("排产补量信息发生改变,请重新获取数据!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("2")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已生产完成,不能删除!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("3")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已关闭,不能删除!");
						return cro;
					}
					// 查询是否存在炉计划
					int countNum = 0;
					sqlZy = "SELECT COUNT(*) COUNT_NUM FROM PLN_ZY_RCL_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
					ps = conn.prepareStatement(sqlZy);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					rs = ps.executeQuery();
					if (rs.next()) {
						countNum = rs.getInt("COUNT_NUM");
					}
					rs.close();
					ps.close();

					if (countNum > 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已存在炉计划,不能删除!");
						return cro;
					}
					ProducHelperBean helperBean = new ProducHelperBean();
					// 1.2验证产线能力
					double[] capacity = helperBean.getPlineCapacity(conn,
							proEntity.getInwlId(), proEntity.getOutwlId(),
							proEntity.getPlineCode(), "F");
					if (capacity[0] <= 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),产线("
								+ proEntity.getPlineName() + ")没有维护产线能力!");
						return cro;
					}
					String planTimeB = proEntity.getPlanTimeB();
					// 删除热处理排产补量
					String sqlUpDelete = "DELETE PLN_ORDER_RCL_S WHERE PRO_PLAN_ID = ? AND GX_PLAN_NO = ?";
					ps = conn.prepareStatement(sqlUpDelete);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					ps.executeUpdate();
					ps.close();
					helperBean.seqIncrease(conn, proEntity.getPlineCode(),
							Integer.parseInt(proEntity.getGxProSeq()), -1, -1,
							"F");
					// 时间自动调整
					helperBean.timeIncrease(conn,
							Integer.parseInt(proEntity.getGxProSeq()) - 1,
							proEntity.getPlineCode(), planTimeB, "F");
				} else if (processCode.equals("G")) {
					PlnOrderJgxSEntity proEntity = getPlanOrderS(conn,
							proPlanId, gxPlanNo, PlnOrderJgxSEntity.class, "G");
					if (proEntity == null) {
						cro.setV_errMsg("排产补量信息发生改变,请重新获取数据!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("2")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已生产完成,不能删除!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("3")) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已关闭,不能删除!");
						return cro;
					}
					// 查询是否存在炉计划
					int countNum = 0;
					sqlZy = "SELECT COUNT(*) COUNT_NUM FROM PLN_ZY_JGX_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
					ps = conn.prepareStatement(sqlZy);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					rs = ps.executeQuery();
					if (rs.next()) {
						countNum = rs.getInt("COUNT_NUM");
					}
					rs.close();
					ps.close();

					if (countNum > 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),已存在炉计划,不能删除!");
						return cro;
					}
					ProducHelperBean helperBean = new ProducHelperBean();
					// 1.2验证产线能力
					double[] capacity = helperBean.getPlineCapacity(conn,
							proEntity.getInwlId(), proEntity.getOutwlId(),
							proEntity.getPlineCode(), "G");
					if (capacity[0] <= 0) {
						cro.setV_errMsg("合同号(" + proEntity.getOrderNo() + "/)"
								+ proEntity.getOrderSeq() + "),产线("
								+ proEntity.getPlineName() + ")没有维护产线能力!");
						return cro;
					}
					String planTimeB = proEntity.getPlanTimeB();
					// 删除加工线排产补量
					String sqlUpDelete = "DELETE PLN_ORDER_JGX_S WHERE PRO_PLAN_ID = ? AND GX_PLAN_NO = ?";
					ps = conn.prepareStatement(sqlUpDelete);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					ps.executeUpdate();
					ps.close();
					helperBean.seqIncrease(conn, proEntity.getPlineCode(),
							Integer.parseInt(proEntity.getGxProSeq()), -1, -1,
							"G");
					// 时间自动调整
					helperBean.timeIncrease(conn,
							Integer.parseInt(proEntity.getGxProSeq()) - 1,
							proEntity.getPlineCode(), planTimeB, "G");
				} else if (processCode.equals("H")) {
					PlnGxJgPropSEntity proEntity = getPlanOrderS(conn,
							proPlanId, gxPlanNo, PlnGxJgPropSEntity.class, "H");
					if (proEntity == null) {
						cro.setV_errMsg("排产补量信息发生改变,请重新获取数据!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("2")) {
						cro.setV_errMsg("备料合同号(" + proEntity.getOrdLnDlyPk()
								+ "),已生产完成,不能删除!");
						return cro;
					}
					if (proEntity.getExeStatus().equals("3")) {
						cro.setV_errMsg("备料合同号(" + proEntity.getOrdLnDlyPk()
								+ "),已关闭,不能删除!");
						return cro;
					}
					// 查询是否存在炉计划
					int countNum = 0;
					sqlZy = "SELECT COUNT(*) COUNT_NUM FROM PLN_ZY_JG_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
					ps = conn.prepareStatement(sqlZy);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					rs = ps.executeQuery();
					if (rs.next()) {
						countNum = rs.getInt("COUNT_NUM");
					}
					rs.close();
					ps.close();

					if (countNum > 0) {
						cro.setV_errMsg("备料合同号(" + proEntity.getOrdLnDlyPk()
								+ "),已存在炉计划,不能删除!");
						return cro;
					}
					ProducHelperBean helperBean = new ProducHelperBean();
					// 1.2验证产线能力
					double[] capacity = helperBean.getPlineCapacity(conn,
							proEntity.getBlWlId(), proEntity.getOutwlId(),
							proEntity.getPlineCode(), "H");
					if (capacity[0] <= 0) {
						cro.setV_errMsg("备料合同号(" + proEntity.getOrdLnDlyPk()
								+ "),产线(" + proEntity.getPlineName()
								+ ")没有维护产线能力!");
						return cro;
					}
					String planTimeB = proEntity.getPlanTimeB();
					// 删除加工线排产补量
					String sqlUpDelete = "DELETE PLN_GX_JG_PROP_S WHERE PRO_PLAN_ID = ? AND GX_PLAN_NO = ?";
					ps = conn.prepareStatement(sqlUpDelete);
					ps.setObject(1, proPlanId);
					ps.setObject(2, gxPlanNo);
					ps.executeUpdate();
					ps.close();
					helperBean.seqIncrease(conn, proEntity.getPlineCode(),
							Integer.parseInt(proEntity.getGxProSeq()), -1, -1,
							"H");
					// 时间自动调整
					helperBean.timeIncrease(conn,
							Integer.parseInt(proEntity.getGxProSeq()) - 1,
							proEntity.getPlineCode(), planTimeB, "H");
				}
			}
			cro.setV_errMsg("删除成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			if (conn != null) {
				try {
					conn.rollback();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				}
			}
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}
}