package com.steering.pss.plnsaleord.jggxpln;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.base.CorePlnComm;
import com.steering.pss.plnsaleord.jggxpln.model.PlnGxJgMEntity;
import com.steering.pss.plnsaleord.jggxpln.model.PlnGxJgPropEntity;
import com.steering.pss.plnsaleord.order.CorePlnOrdJgDjDeal;
import com.steering.pss.plnsaleord.order.baseMethod.CoreOrdBase;

/**
 * 接箍订单
 * 
 * @author xiaohai
 */
public class CorePlnJgOrdToDd extends DBCommon {
	/**
	 * 查询接箍合同号
	 * 
	 * @return
	 */
	public CoreReturnObject queryJgOrdNo() {
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CorePlnJgOrdToDd.queryJgOrdNo");
		return cro;
	}

	/**
	 * 查询工序订单订单号
	 * 
	 * @return
	 */
	public CoreReturnObject queryJgGxMNo() {
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CorePlnJgOrdToDd.queryJgGxMNo");
		return cro;
	}

	/**
	 * 查询监制工序点单位
	 * 
	 * @param prdcrNo
	 * @return
	 */
	public CoreReturnObject selectPrdStation(String prdcrNo) {
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CorePlnJgOrdToDd.selectPrdStation",
				new Object[] { prdcrNo });
		return cro;
	}

	/**
	 * 查询未组订单的接箍合同
	 * 
	 * @param jgOrdNo
	 * @return
	 */
	public CoreReturnObject queryJgOrd(String jgOrdNo) {
		CoreReturnObject cro = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.queryJgOrd");
		cro = this.getData(sql, new Object[] { jgOrdNo });
		return cro;
	}

	/**
	 * 查询接箍工序订单
	 * 
	 * @param jgDdNo
	 * @return
	 */
	public CoreReturnObject queryJgGxM(String jgDdNo, String strShowBlDeal) {
		CoreReturnObject cro = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.queryJgGxM");

		if (strShowBlDeal.equals("0")) {
			sql += " and nvl(t.MATCHNUM,0)=0 and nvl(t.BUYPLANNUM,0)=0 and nvl(t.JGWT_BLREQ,0)=0 ";
		}
		cro = this.getData(sql, new Object[] { jgDdNo });
		return cro;
	}

	/**
	 * 查询接箍工序订单属性表
	 * 
	 * @param jgDdNo
	 * @return
	 */
	public CoreReturnObject queryJgGxProp(String jgDdNo) {
		CoreReturnObject cro = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.queryJgGxProp");
		cro = this.getData(sql, new Object[] { jgDdNo });
		return cro;
	}

	/**
	 * 查询接箍订单下的合同
	 * 
	 * @param jgDdNo
	 * @return
	 */
	public CoreReturnObject queryGxJgOrd(String jgDdNo) {
		CoreReturnObject cro = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.queryGxJgOrd");
		cro = this.getData(sql, new Object[] { jgDdNo });
		return cro;
	}

	/**
	 * 查询接箍合同
	 * 
	 * @param jgOrdNo
	 * @return
	 */
	public CoreReturnObject queryJgOrdStatus(String jgOrdNo) {
		CoreReturnObject cro = new CoreReturnObject();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.queryJgOrdStatus");
		cro = this.getData(sql, new Object[] { jgOrdNo });
		return cro;
	}

	/**
	 * 新增接箍订单
	 * 
	 * @param list
	 * @param codeJg
	 * @param alphaCode
	 * @param custAlphaNo
	 * @param prdcrNo
	 * @param createName
	 * @return
	 */
	public CoreReturnObject addJgGxM(ArrayList<String> list, String beginProcStr) {
		CoreReturnObject cro = new CoreReturnObject();
		JSONObject json = JSONObject.fromObject(beginProcStr);
		PlnGxJgMEntity jgGxM = (PlnGxJgMEntity) JSONObject.toBean(json,
				PlnGxJgMEntity.class);
		String codeJg = jgGxM.getCodeJg();// 接箍码
		String prdcrNo = jgGxM.getPrdcrNo();// 监制编号
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.getGxJgMOrdNo");
			String sqlAddM = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.addGxJgM");
			String sqlAddP = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.addGxJgProp");
			String sqlUpdate = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updatePlnJgOrd");
			String sqlAddC = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.addGxJgC");
			int planNum = 0;
			ArrayList<PlnGxJgPropEntity> jgGxProp = new ArrayList<PlnGxJgPropEntity>();
			String proPlanId = "";// 接箍工序订单编号
			int jgGroupId = 0;// 属性分组号
			String codeBl = "";

			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				if (rs.getString("PLAN_ID") != null) {
					proPlanId = rs.getString("PLAN_ID");
				}
			}
			if (proPlanId.equals("")) {
				cro.setResult("未能生产订单编号！");
				return cro;
			}
			rs.close();
			ps.close();

			// /接箍合同四项要求
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.queryJgOrdDemand");
			if (list.size() >= 1) {
				sql += " WHERE T.JG_ORDERNO = '" + list.get(0) + "'";
			} else {
				cro.setResult("未选择要组单的合同。");
				return cro;
			}
			for (int i = 1; i < list.size(); i++) {
				sql += " OR T.JG_ORDERNO = '" + list.get(i) + "'";
			}
			sql += " GROUP BY T.DEMAND_DETECTION, T.DEMAND_BONDERIZING, T.DEMAND_COPPER, T.DEMAND_BLAST";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				PlnGxJgPropEntity propEntiy = new PlnGxJgPropEntity();
				jgGroupId++;
				if (rs.getString("DEMAND_DETECTION") != null) {
					propEntiy.setDemandDetection(rs
							.getString("DEMAND_DETECTION"));
				}
				if (rs.getString("DEMAND_BONDERIZING") != null) {
					propEntiy.setDemandBonderizing(rs
							.getString("DEMAND_BONDERIZING"));
				}
				if (rs.getString("DEMAND_COPPER") != null) {
					propEntiy.setDemandCopper(rs.getString("DEMAND_COPPER"));
				}
				if (rs.getString("DEMAND_BLAST") != null) {
					propEntiy.setDemandBlast(rs.getString("DEMAND_BLAST"));
				}
				propEntiy.setProPlanId(proPlanId);
				propEntiy.setJgGroupId(CorePlnComm.padding(String
						.valueOf(jgGroupId), '0', 2));
				propEntiy.setGroupNum("0");
				jgGxProp.add(propEntiy);
			}
			rs.close();
			ps.close();

			for (String jgOrdNo : list) {
				ps = conn.prepareStatement(CoreXmlSqlParsersFactory
						.getSql("CorePlnJgOrdToDd.queryJgOrdStatus"));
				ps.setObject(1, jgOrdNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					if (rs.getString("PRO_PLAN_ID") != null) {
						conn.rollback();
						cro.setResult("接箍合同：" + jgOrdNo
								+ "已组订单，不能再组到其它订单当中，请刷新数据！");
						return cro;
					}
					if (rs.getString("JGORDERSTATUS") != null
							&& !rs.getString("JGORDERSTATUS").trim()
									.equals("0")) {
						conn.rollback();
						cro.setResult("接箍合同：" + jgOrdNo + "不是计划状态。");
						return cro;
					}
					int num = rs.getInt("ORDERNUM");
					planNum += num;

					// 获取四项值
					PreparedStatement pst = null;
					String groupId = "";
					String demandDetection = "";
					String demandBonderizing = "";
					String demandCopper = "";
					String demandBlast = "";
					if (rs.getString("DEMAND_DETECTION") != null) {
						demandDetection = rs.getString("DEMAND_DETECTION");
					}
					if (rs.getString("DEMAND_BONDERIZING") != null) {
						demandBonderizing = rs.getString("DEMAND_BONDERIZING");
					}
					if (rs.getString("DEMAND_COPPER") != null) {
						demandCopper = rs.getString("DEMAND_COPPER");
					}
					if (rs.getString("DEMAND_BLAST") != null) {
						demandBlast = rs.getString("DEMAND_BLAST");
					}
					// 合并到属性表
					for (PlnGxJgPropEntity propEntiy : jgGxProp) {
						if (propEntiy.getDemandDetection().equals(
								demandDetection)
								&& propEntiy.getDemandBonderizing().equals(
										demandBonderizing)
								&& propEntiy.getDemandCopper().equals(
										demandCopper)
								&& propEntiy.getDemandBlast().equals(
										demandBlast)) {
							int propNum = Integer.parseInt(propEntiy
									.getGroupNum());
							propNum += num;
							propEntiy.setGroupNum(String.valueOf(propNum));
							groupId = propEntiy.getJgGroupId();
							break;
						}
					}

					// 更新接箍合同状态，回写订单号
					pst = conn.prepareStatement(sqlUpdate);
					pst.setObject(1, proPlanId);
					pst.setObject(2, "1");
					pst.setObject(3, groupId);
					pst.setObject(4, jgOrdNo);
					pst.executeUpdate();
					pst.close();
				}
				rs.close();
				ps.close();
			}

			// 生产订单
			if (!prdcrNo.trim().equals("")) {
				// 监制信息
				jgGxM.setPrdcrNo(prdcrNo);
				String sqlPrd = CoreXmlSqlParsersFactory
						.getSql("CorePlnJgOrdToDd.queryJgPrdCr");
				sqlPrd += " AND T.CODE_BL IS NULL    AND ROWNUM<=1";
				ps = conn.prepareStatement(sqlPrd);
				ps.setObject(1, prdcrNo);
				ps.setObject(2, codeJg);
				rs = ps.executeQuery();
				if (rs.next()) {
					jgGxM.setMonitordemand("接箍监制。");
					jgGxM.setIfMonitorJg("1");
				}else{
					jgGxM.setIfMonitorJg("0");
				}
				rs.close();
				ps.close();

				sqlPrd = CoreXmlSqlParsersFactory
						.getSql("CorePlnJgOrdToDd.queryJgPrdCr");
				sqlPrd += " AND T.CODE_BL IS NOT NULL  AND ROWNUM<=1";
				ps = conn.prepareStatement(sqlPrd);
				ps.setObject(1, prdcrNo);
				ps.setObject(2, codeJg);
				rs = ps.executeQuery();
				if (rs.next()) {
					if (!jgGxM.getIfMonitorJg().trim().equals("1")) {
						jgGxM.setMonitordemand("接箍料监制。");
					} else {
						jgGxM.setMonitordemand("接箍、接箍料监制。");
					}
					jgGxM.setIfMonitorJg("1");
				}else{
					jgGxM.setIfMonitorJg("0");
				}
				rs.close();
				ps.close();
//				if (jgGxM.getIfMonitorJg().equals("")) {
//					cro.setResult("有监制编号未找到接箍码监制要求。");
//					return cro;
//				}
			}

			if (jgGxM.getCustAlphaNo().trim().equals("")) {
				jgGxM.setCustAlphaNo("/");
			}
			String jgHtH = "";
			if (list.size() >= 1) {
				jgHtH = list.get(0);
			} else {
				cro.setResult("未选择要组单的合同。");
				return cro;
			}
			double wasteRate = 0;
			// 获取可选备料码，默认备料码
			String allCodeBl = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.selectJgRBl");
			ps = conn.prepareStatement(sql);
			ps.setObject(1, jgHtH);
			ps.setObject(2, jgHtH);
			ps.setObject(3, jgHtH);
			// ps.setObject(1, jgGxM.getKeyJg());
			// ps.setObject(2, jgGxM.getCustAlphaNo());
			// ps.setObject(3, jgGxM.getAlphaCode());
			rs = ps.executeQuery();
			while (rs.next()) {
				if (rs.getString("PSC_BL") != null) {
					codeBl = rs.getString("PSC_BL");
					if (jgGxM.getCodeJgBl().trim().equals("")) {
						String[] blMaterial = null;
						PreparedStatement pst = null;
						ResultSet rst = null;
						String outMaterIdesc = "";
						sql = CoreXmlSqlParsersFactory
								.getSql("CorePlnJgOrdToDd.selectJgMaterialPl");
						sql += " ORDER BY T.PRD_RATE DESC, T.WASTE_RATE ASC";
						pst = conn.prepareStatement(sql);
						pst.setObject(1, codeBl);
						pst.setObject(2, codeJg);
						rst = pst.executeQuery();
						if (rst.next()) {
							jgGxM.setPlineCode(rst.getString("PLINE_CODE"));
							jgGxM.setPlineName(rst.getString("PLINE_NAME"));
							outMaterIdesc = rst.getString("OUT_MATERIAL_DESC");
							wasteRate = rst.getDouble("WASTE_RATE") / 10000;
						}
						rst.close();
						pst.close();
						if (outMaterIdesc == null || outMaterIdesc.equals("")) {
							cro.setResult("未找到接箍加工产出物料。");
							return cro;
						}
						jgGxM.setCodeJgBl(codeBl);

						blMaterial = CorePlnOrdJgDjDeal.getBlMaterial(conn,
								codeBl);
						if (blMaterial != null) {
							jgGxM.setBlWlId(blMaterial[0]);
							jgGxM.setBlWlDesc(blMaterial[1]);
						}
					}

					if (jgGxM.getAllCodeBl().equals("")) {
						allCodeBl = codeBl;
					} else {
						allCodeBl += ("," + codeBl);
					}

				}

				// 新增接箍订单从表
				String sqlUpC = EntityTool.fillSqlByEntity(sqlAddC, jgGxM);
				ps = conn.prepareStatement(sqlUpC);
				ps.setObject(1, proPlanId);
				ps.setObject(2, codeBl);
				ps.executeUpdate();
				ps.close();
			}
			rs.close();
			ps.close();
			if (allCodeBl.equals("")) {
				cro.setResult("未取到备料码,请核对工艺评审结果是否有备料码。");
				return cro;
			}
			jgGxM.setAllCodeBl(allCodeBl);
			planNum = (int) Math.ceil(planNum / (1 - wasteRate));
			// 生产属性表
			for (PlnGxJgPropEntity propEntiy : jgGxProp) {
				String sqlUp = "";
				propEntiy.setCodeJgBl(codeBl);
				getSinBlToJgNum(conn, codeJg, propEntiy);

				sqlUp = EntityTool.fillSqlByEntity(sqlAddP, propEntiy);
				ps = conn.prepareStatement(sqlUp);
				ps.setObject(1, codeJg);
				ps.executeUpdate();
				ps.close();
			}

			jgGxM.setProPlanId(proPlanId);
			jgGxM.setPlanNum(String.valueOf(planNum));
			String sqlUp = EntityTool.fillSqlByEntity(sqlAddM, jgGxM);
			ps = conn.prepareStatement(sqlUp);
			ps.executeUpdate();
			ps.close();

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

	/**
	 * 加入接箍订单
	 * 
	 * @param list
	 * @param planId
	 * @param prdcrNo
	 * @param updateName
	 * @return
	 */
	public CoreReturnObject joinJgM(ArrayList<String> list, String planId,
			String prdcrNo, String updateName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);

			PreparedStatement ps = null;
			String sqlUpdateM = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updateGxJgMNum");
			String sqlUpdateP = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updateGxJgPropNum");
			String sqlAddP = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.addGxJgProp");
			String sqlUpdate = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updatePlnJgOrd");
			int confirmNum = 0;
			String errMessage = "";
			int planNum = 0;
			for (String jgOrdNo : list) {
				ps = conn.prepareStatement(CoreXmlSqlParsersFactory
						.getSql("CorePlnJgOrdToDd.queryJgOrdStatus"));
				ps.setObject(1, jgOrdNo);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					int num = rs.getInt("ORDERNUM");
					String groupId = "";// 属性分组号
					String codeJg = "";
					String demandDetection = "";
					String demandBonderizing = "";
					String demandCopper = "";
					String demandBlast = "";
					String codeBl = "";
					PreparedStatement pst = null;
					if (rs.getString("CODE_JG") != null) {
						codeJg = rs.getString("CODE_JG");
					} else {
						errMessage = "未找到接箍合同：" + jgOrdNo + "的接箍码。";
						continue;
					}
					if (rs.getString("PRO_PLAN_ID") != null) {
						errMessage = "接箍合同：" + jgOrdNo + "已组订单，不能再组到其它订单当中。";
						continue;
					}
					if (rs.getString("JGORDERSTATUS") != null
							&& !rs.getString("JGORDERSTATUS").trim()
									.equals("0")) {
						errMessage = "接箍合同：" + jgOrdNo + "不是计划状态。";
						continue;
					}

					if (rs.getString("CODE_JG_BL") != null) {
						codeBl = rs.getString("CODE_JG_BL");
					}

					if (rs.getString("DEMAND_DETECTION") != null) {
						demandDetection = rs.getString("DEMAND_DETECTION");
					}
					if (rs.getString("DEMAND_BONDERIZING") != null) {
						demandBonderizing = rs.getString("DEMAND_BONDERIZING");
					}
					if (rs.getString("DEMAND_COPPER") != null) {
						demandCopper = rs.getString("DEMAND_COPPER");
					}
					if (rs.getString("DEMAND_BLAST") != null) {
						demandBlast = rs.getString("DEMAND_BLAST");
					}

					pst = conn.prepareStatement(CoreXmlSqlParsersFactory
							.getSql("CorePlnJgOrdToDd.selectJgOrdPropdeal"));
					pst.setObject(1, jgOrdNo);
					pst.setObject(2, planId);
					ResultSet rst = pst.executeQuery();
					if (rst.next()) {
						// 存在现有属性记录
						PreparedStatement pst1 = null;
						int oldNumJg = rst.getInt("GROUP_NUM");
						int oldNumBl = rst.getInt("GROUP_BL_NUM");
						double oldWtBl = rst.getInt("GROUP_BL_WT");
						int addBlNum = 0;
						double addBlWt = 0;

						if (oldNumJg > 0) {
							addBlNum = (int) Math.ceil(oldNumBl
									* num
									/ Double.parseDouble(String
											.valueOf(oldNumJg)));
						}
						if (oldNumBl > 0) {
							addBlWt = addBlNum * oldWtBl / oldNumBl;
						}

						groupId = rst.getString("JG_GROUP_ID");
						pst1 = conn.prepareStatement(sqlUpdateP);
						pst1.setObject(1, num);
						pst1.setObject(2, addBlNum);
						pst1.setObject(3, addBlWt);
						pst1.setObject(4, planId);
						pst1.setObject(5, groupId);
						pst1.executeUpdate();
						pst1.close();
					} else {
						// 不存在现有属性记录，新增
						String sqlUp = "";
						PlnGxJgPropEntity propEntiy = new PlnGxJgPropEntity();
						PreparedStatement pst1 = conn
								.prepareStatement(CoreXmlSqlParsersFactory
										.getSql("CorePlnJgOrdToDd.getGxJgPropId"));
						pst1.setObject(1, planId);
						ResultSet rst1 = pst1.executeQuery();
						if (rst1.next()) {
							if (rst1.getString("GROUP_ID") == null) {
								conn.rollback();
								cro.setV_errCode(-1);
								cro.setV_errMsg("生产属性分组号出错。");
								return cro;
							}
							groupId = rst1.getString("GROUP_ID");
						}
						rst1.close();
						pst1.close();

						propEntiy.setProPlanId(planId);
						propEntiy.setJgGroupId(groupId);
						propEntiy.setGroupNum(String.valueOf(num));
						propEntiy.setDemandDetection(demandDetection);
						propEntiy.setDemandBonderizing(demandBonderizing);
						propEntiy.setDemandCopper(demandCopper);
						propEntiy.setDemandBlast(demandBlast);
						propEntiy.setCodeJgBl(codeBl);

						getSinBlToJgNum(conn, codeJg, propEntiy);

						sqlUp = EntityTool.fillSqlByEntity(sqlAddP, propEntiy);
						pst1 = conn.prepareStatement(sqlUp);
						pst1.setObject(1, codeJg);
						pst1.executeUpdate();
						pst1.close();
					}
					rst.close();
					pst.close();

					// 更新接箍合同状态，回写订单号
					pst = conn.prepareStatement(sqlUpdate);
					pst.setObject(1, planId);
					pst.setObject(2, "1");
					pst.setObject(3, groupId);
					pst.setObject(4, jgOrdNo);
					pst.executeUpdate();
					pst.close();
					planNum += num;
				}
				rs.close();
				ps.close();
				confirmNum++;
			}
			String plineCode = "";
			String inMaterialNo = "";
			String outMaterialNo = "";
			String sqlQuery = "SELECT T.PRO_PLAN_ID,T.PLINE_CODE,T.BL_WL_ID,T.CODE_JG FROM PLN_GX_JG_M T WHERE T.PRO_PLAN_ID = ?";
			ps = conn.prepareStatement(sqlQuery);
			ps.setObject(1, planId);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				plineCode = rs.getString("PLINE_CODE");
				inMaterialNo = rs.getString("BL_WL_ID");
				outMaterialNo = rs.getString("CODE_JG");
			}
			rs.close();
			ps.close();

			// 查询投入产出物料对应小时能力表万只废品数
			double wasteRate = 0;
			String sqldesc = "SELECT NVL(T.WASTE_RATE,0) WASTE_RATE  FROM SLM_BASE_MATERIAL_H_PL T  WHERE T.IN_MATERIAL_NO=? AND T.OUT_MATERIAL_NO=? AND T.PLINE_CODE=?";
			ps = conn.prepareStatement(sqldesc);
			ps.setObject(1, inMaterialNo);
			ps.setObject(2, outMaterialNo);
			ps.setObject(3, plineCode);
			rs = ps.executeQuery();
			if (rs.next()) {
				wasteRate = rs.getDouble("WASTE_RATE") / 10000;
			}
			rs.close();
			ps.close();
			planNum = (int) Math.ceil(planNum / (1 - wasteRate));
			// 更新接箍订单接箍个数
			ps = conn.prepareStatement(sqlUpdateM);
			ps.setObject(1, planNum);
			ps.setObject(2, planId);
			ps.executeUpdate();
			ps.close();

			conn.commit();
			if (confirmNum == list.size()) {
				cro.setResult(confirmNum + "条加入成功。");
			} else {
				cro.setResult(confirmNum + "条加入成功，"
						+ (list.size() - confirmNum) + "条失败。\n" + errMessage);
			}
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} catch (Exception sqle) {
			if (conn != null) {
				try {
					conn.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 删除订单
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject deleteGxJgM(ArrayList<String> list) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);

			int confirmNum = 0;
			PreparedStatement ps = null;
			String sqlDeleteM = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.deleteGxJgM");
			String sqlDeleteC = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.deleteGxJgC");
			String sqlDeleteProp = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.deleteGxJgPropByDd");
			String sqlUpdate = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updatePlnJgOrd");
			String sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.queryGxJgOrd");
			for (String proPlanId : list) {
				// 删除条件判断
				String sqlDelInfo = CoreXmlSqlParsersFactory
						.getSql("CorePlnJgOrdToDd.queryJgGxMInfo");
				ps = conn.prepareStatement(sqlDelInfo);
				ps.setObject(1, proPlanId);
				ResultSet rsDel = ps.executeQuery();
				while (rsDel.next()) {
					String strPLANSTATUS = rsDel.getString("PLANSTATUS");
					int numMATCHNUM = rsDel.getInt("MATCHNUM");
					int numBUYPLANNUM = rsDel.getInt("BUYPLANNUM");
					int numJGWT_BLREQ = rsDel.getInt("JGWT_BLREQ");
					if (!strPLANSTATUS.equals("0")) {
						cro.setV_errCode(-1);
						cro.setV_errMsg("接箍订单" + proPlanId + "不是计划状态，不能删除");
						return cro;
					}
					if (numMATCHNUM != 0) {
						cro.setV_errCode(-1);
						cro.setV_errMsg("接箍订单" + proPlanId + "有利库记录，不能删除");
						return cro;
					}
					if (numBUYPLANNUM != 0) {
						cro.setV_errCode(-1);
						cro.setV_errMsg("接箍订单" + proPlanId + "有外购计划，不能删除");
						return cro;
					}
					if (numJGWT_BLREQ != 0) {
						cro.setV_errCode(-1);
						cro.setV_errMsg("接箍订单" + proPlanId + "已做备料需求，不能删除");
						return cro;
					}
				}
				rsDel.close();
				ps.close();
				String planstttus = "";
				// 查询成品合同排产状态
				String sqlCpOrder = "SELECT MAX(T.PLANSTATUS) PLANSTATUS"
						+ "  FROM PLN_SALEORD_PROC T, PLN_ORDER_JG T1, PLN_GX_JG_M T2"
						+ " WHERE T.ORD_LN_DLY_PK = T1.ORD_LN_DLY_PK  "
						+ " AND T1.PRO_PLAN_ID = T2.PRO_PLAN_ID  AND T2.PRO_PLAN_ID=?";
				ps = conn.prepareStatement(sqlCpOrder);
				ps.setObject(1, proPlanId);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					planstttus = rs.getString("PLANSTATUS");
				}
				rs.close();
				ps.close();
				if (planstttus.equals("")) {
					cro.setV_errCode(-1);
					cro.setV_errMsg("接箍订单" + proPlanId + "无对应成品合同，不能删除");
					return cro;
				}
				if (!planstttus.equals("1")) {
					cro.setV_errCode(-1);
					cro.setV_errMsg("接箍订单" + proPlanId + "对应排产成品合同不是已接收状态，不能删除");
					return cro;
				}
				ps = conn.prepareStatement(sql);
				ps.setObject(1, proPlanId);
				rs = ps.executeQuery();
				while (rs.next()) {
					String jgOrdNo = rs.getString("JG_ORDERNO");
					// 更新接箍合同状态，回写订单号
					PreparedStatement pst = conn.prepareStatement(sqlUpdate);
					pst.setObject(1, "");
					pst.setObject(2, "0");
					pst.setObject(3, "");
					pst.setObject(4, jgOrdNo);
					pst.executeUpdate();
					pst.close();
				}
				rs.close();
				ps.close();

				// 删除订单
				ps = conn.prepareStatement(sqlDeleteM);
				ps.setObject(1, proPlanId);
				ps.executeUpdate();
				ps.close();
				// 删除订单从表
				ps = conn.prepareStatement(sqlDeleteC);
				ps.setObject(1, proPlanId);
				ps.executeUpdate();
				ps.close();
				// 删除分组
				ps = conn.prepareStatement(sqlDeleteProp);
				ps.setObject(1, proPlanId);
				ps.executeUpdate();
				ps.close();

				conn.commit();
				confirmNum++;
			}
			cro.setResult(confirmNum + "条订单删除成功。");
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} catch (Exception sqle) {
			if (conn != null) {
				try {
					conn.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 删除订单明细
	 * 
	 * @param list
	 * @param proPlanId
	 * @return
	 */
	public CoreReturnObject deleteGxJgOrd(ArrayList<String> list) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			int confirmNum = 0;
			PreparedStatement ps = null;
			String sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.selectGxJgOrd");
			String sqlDeleteM = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.deleteGxJgM");
			String sqlDeleteProp = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.deleteGxJgProp");
			String sqlUpdate = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updatePlnJgOrd");
			String sqlUpdateM = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updateGxJgMNum");
			String sqlUpdateP = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.updateGxJgPropNum");
			String sqlQ = CoreXmlSqlParsersFactory
					.getSql("CorePlnJgOrdToDd.selectGxJgProp");
			for (String jgOrderNo : list) {
				ps = conn.prepareStatement(sql);
				ps.setObject(1, jgOrderNo);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					String proPlanId = rs.getString("PRO_PLAN_ID");
					String groupId = rs.getString("JG_GROUP_ID");
					int planNum = rs.getInt("ORDERNUM");

					int oldNumJg = rs.getInt("GROUP_NUM");
					int oldNumBl = rs.getInt("GROUP_BL_NUM");
					double oldWtBl = rs.getInt("GROUP_BL_WT");
					int addBlNum = 0;
					double addBlWt = 0;

					if (oldNumJg > 0) {
						addBlNum = (int) Math.ceil(oldNumBl * planNum
								/ Double.parseDouble(String.valueOf(oldNumJg)));
					}
					if (oldNumBl > 0) {
						addBlWt = addBlNum * oldWtBl / oldNumBl;
					}

					// 更新分组个数
					PreparedStatement pst = conn.prepareStatement(sqlUpdateP);
					pst.setObject(1, -planNum);
					pst.setObject(2, -addBlNum);
					pst.setObject(3, -addBlWt);
					pst.setObject(4, proPlanId);
					pst.setObject(5, groupId);
					pst.executeUpdate();
					pst.close();

					// 更新接箍订单接箍个数
					pst = conn.prepareStatement(sqlUpdateM);
					pst.setObject(1, -planNum);
					pst.setObject(2, proPlanId);
					pst.executeUpdate();
					pst.close();

					// 更新接箍合同状态，回写订单号
					pst = conn.prepareStatement(sqlUpdate);
					pst.setObject(1, "");
					pst.setObject(2, "0");
					pst.setObject(3, "");
					pst.setObject(4, jgOrderNo);
					pst.executeUpdate();
					pst.close();

					// 查询订单剩余
					pst = conn.prepareStatement(sqlQ);
					pst.setObject(1, proPlanId);
					pst.setObject(2, groupId);
					ResultSet rst = pst.executeQuery();
					if (rst.next()) {
						int groupNum = rst.getInt("GROUP_NUM");
						int planNumM = rst.getInt("PLAN_NUM");
						if (groupNum == 0) {
							// 删除分组
							PreparedStatement pst1 = conn
									.prepareStatement(sqlDeleteProp);
							pst1.setObject(1, proPlanId);
							pst1.setObject(2, groupId);
							pst1.executeUpdate();
							pst1.close();
						}

						if (planNumM == 0) { // 删除订单
							PreparedStatement pst1 = conn
									.prepareStatement(sqlDeleteM);
							pst1.setObject(1, proPlanId);
							pst1.executeUpdate();
							pst1.close();
						}

					}
					rst.close();
					pst.close();
				}
				rs.close();
				ps.close();

				conn.commit();
				confirmNum++;
			}
			cro.setResult(confirmNum + "条订单明细移除成功。");
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} catch (Exception sqle) {
			if (conn != null) {
				try {
					conn.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 查询接箍利库
	 * 
	 * @param codeJg
	 * @param custAlphaNo
	 * @param alphaCode
	 * @return
	 */
	public CoreReturnObject queryMatJg(String codeJg, String custAlphaNo,
			String alphaCode) {
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CorePlnJgOrdToDd.queryMatJg", new Object[] {
				codeJg, custAlphaNo, alphaCode });
		return cro;
	}

	/**
	 * 单支备料管切默认接箍个数
	 * 
	 * @throws SQLException
	 */
	public static boolean getSinBlToJgNum(Connection conn, String codeJg,
			PlnGxJgPropEntity propEntiy) throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		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 codeBl = propEntiy.getCodeJgBl();
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnJgOrdToDd.selectComMscJg");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, codeJg);
		rs = ps.executeQuery();
		if (!rs.next()) {
			return false;
		}
		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 false;
		}
		outdimeter = rs.getDouble("MIN_D");
		wallthick = rs.getDouble("MIN_H");
		aimLenBl = rs.getDouble("AIM_LEN");
		if (lengthJg == 0) {
			return false;
		}
		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)));
		}

		propEntiy.setGroupBlNum(String.valueOf(numBl));
		propEntiy.setGroupBlWt(String.valueOf(numBl * sinBlWt));
		propEntiy.setAimlengthBl(String.valueOf(aimLenBl / 1000));
		propEntiy.setSingleCutNun(String.valueOf(numJgSinBl));
		rs.close();
		ps.close();

		return true;
	}

	/**
	 * 新增接箍排产订单
	 * 
	 * @param con
	 * @param jgMEntity
	 * @param propEntiy
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	// public void addPlnGxJGProps(Connection con, PlnGxJgMEntity jgMEntity,
	// PlnGxJgPropEntity propEntiy) throws IllegalAccessException,
	// InvocationTargetException {
	// PlnGxJgPropSEntity entity = new PlnGxJgPropSEntity();
	// BeanUtils.copyProperties(entity, jgMEntity);
	// BeanUtils.copyProperties(entity, propEntiy);
	// ProducHelperBean helperBean = new ProducHelperBean();
	// //
	// }

}