package com.steering.pss.plnsaleord.processOrder;

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

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.GpOptimize.model.PlnOrderCastSEntity;
import com.steering.pss.plnsaleord.GpOptimize.model.PlnOrderDzSEntity;
import com.steering.pss.plnsaleord.GpOptimize.model.PlnReqinfGpEntity;
import com.steering.pss.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.order.baseMethod.CoreOrdBase;
import com.steering.pss.plnsaleord.order.model.PlnOrdInfo;
import com.steering.pss.plnsaleord.order.model.PlnOrderDbkEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderDbkSEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderJgxEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderJgxSEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderRclEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderRclSEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderZgEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderZgSEntity;
import com.steering.pss.plnsaleord.processOrder.base.ProducHelper;
import com.steering.pss.plnsaleord.processOrder.base.ProducHelperBean;
import com.steering.pss.plnsaleord.processOrder.entity.TimeEntity;

/**
 * 工序耗时
 * 
 * @author xiaohai
 */
public class CorePlnOrdProUseTime extends DBCommon {
	private static ProducHelperBean helperBean = new ProducHelperBean();

	/**
	 * 新增铸造工序排产
	 * 
	 * @param conn
	 * @param proPlanId
	 * @param creatName
	 * @throws SQLException
	 */
	public boolean addOrderCastS(Connection conn, String proPlanId,
			String creatName, String rollMonth, String createTime,
			String plineCodeZz, String plineNameZz, String gpWtLongMin,
			String gowhereCode, String gowhereName) throws SQLException {
		PlnOrderCastSEntity castSEntity = new PlnOrderCastSEntity();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Date nowTime = new Date();
		double useTime = 0;
		int plnNUM = 0;// 订单支
		double capacity = 0;// 产线能力
		SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM");
		String gradeCode = "";
		String sqlAdd = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.addOrderCastS");
		String sqlUp = "";

		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.queryCastPineHrCapcty");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		if (rs.next()) {
			useTime = rs.getDouble("USE_TIME");
			if (rs.getString("GRADECODE") != null) {
				gradeCode = rs.getString("GRADECODE");
			}
			plnNUM = rs.getInt("PLAN_WT");
			capacity = rs.getDouble("HR_CAPCTY");
		}
		rs.close();
		ps.close();

		// 电炉产线(取基础数据)
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.queryPlnBasePlineA");
		ps = conn.prepareStatement(sql);
		// ps.setObject(1, gradeCode);
		ps.setObject(1, plineCodeZz);
		rs = ps.executeQuery();
		if (rs.next()) {
			castSEntity.setPlineCodeA(rs.getString("PLINE_CODE_A"));
			castSEntity.setPlineNameA(rs.getString("PLINE_NAME_A"));
		}
		rs.close();
		ps.close();
		if (castSEntity.getPlineCodeA().equals("")) {
			verrMsg = "无对应电炉产线,请维护连铸排产基础数据铸机对应电炉产线默认。";
			return false;
		}
		castSEntity.setProPlanId(proPlanId);
		castSEntity.setProMonth(dFormat.format(nowTime));
		castSEntity.setPlanUsetime(String.valueOf(useTime));
		castSEntity.setCreateName(creatName);
		castSEntity.setCreateTime(createTime);
		// String[] plineArr = PlnHelper.getArray(conn,
		// "CorePlnOrdProUseTime.getPlineCodeB",
		// new String[] { proPlanId }, new String[] { "PLINE_CODE",
		// "PLINE_NAME" });
		// if (plineArr == null || plineArr[0] == null
		// || plineArr[0].length() == 0) {
		// verrMsg = "无轧管产线。";
		// return false;
		// }
		ProducHelperBean helperBean = new ProducHelperBean();
		int gxProSeq = helperBean.getMaxGxProSeq(conn, plineCodeZz, "B",
				rollMonth);

		castSEntity.setPlineCode(plineCodeZz);
		castSEntity.setPlineName(plineNameZz);
		castSEntity.setGxProSeq(String.valueOf(gxProSeq));
		TimeEntity TimeEntiy;
		try { // 3.1计划开始时间(这个时间应该从数据库里查)
			String planTimeB = helperBean.getMaxPlanTimeE(conn,
					castSEntity.getPlineCode(), "B", rollMonth, gxProSeq);
			if (planTimeB.equals("")) {
				verrMsg = "无开始时间。";
				return false;
			}
			TimeEntiy = helperBean.calculateTime(plnNUM, capacity, planTimeB);
			// 计划结束时间
			String planTimeE = TimeEntiy.getEndTime();
			// 修改序号
			helperBean.seqIncrease(conn, castSEntity.getPlineCode(), gxProSeq,
					-1, 1, "B");
			// 时间自动调整
			helperBean.timeIncrease(conn, gxProSeq, castSEntity.getPlineCode(),
					planTimeE, "B");
			castSEntity.setPlanTimeB(planTimeB);
			castSEntity.setPlanTimeE(planTimeE);
			sqlUp = EntityTool.fillSqlByEntity(sqlAdd, castSEntity);
			ps = conn.prepareStatement(sqlUp);
			ps.setObject(1, gpWtLongMin);
			ps.setObject(2, gowhereCode);
			ps.setObject(3, gowhereName);
			ps.executeUpdate();
			ps.close();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 新增锻造工序排产
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 */
	public static void addOrderDzS(Connection conn, String proPlanId,
			PlnReqinfGpEntity reqinfGpEntity, String createName,
			String createTime) throws SQLException {
		PlnOrderDzSEntity dzSEntity = new PlnOrderDzSEntity();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Date nowTime = new Date();
		double useTime = 0;
		int plnNUM = 0;// 订单支
		double capacity = 0;// 产线能力
		String rollMonth = reqinfGpEntity.getProMonth();
		SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM");
		String sqlAdd = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.addOrderDzS");
		String sqlUp = "";

		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.queryDzPineHrCapcty");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		if (rs.next()) {
			useTime = rs.getDouble("USE_TIME");
			plnNUM = rs.getInt("OUT_WT_TOT");
			capacity = rs.getDouble("HR_CAPCTY");
		}
		rs.close();
		ps.close();

		dzSEntity.setProPlanId(proPlanId);
		dzSEntity.setProMonth(dFormat.format(nowTime));
		dzSEntity.setPlanUsetime(String.valueOf(useTime));

		String[] plineArr = PlnHelper.getArray(conn,
				"CorePlnOrdProUseTime.getPlineCodeC",
				new String[] { proPlanId }, new String[] { "PLINE_CODE",
						"PLINE_NAME" });
		if (plineArr == null || plineArr[0] == null
				|| plineArr[0].length() == 0) {
			return;
		}
		ProducHelperBean helperBean = new ProducHelperBean();
		int gxProSeq = helperBean.getMaxGxProSeq(conn, plineArr[0], "C",
				rollMonth);
		dzSEntity.setPlineCode(plineArr[0]);
		dzSEntity.setPlineName(plineArr[1]);
		dzSEntity.setGxProSeq(String.valueOf(gxProSeq));
		dzSEntity.setDzBurnLose(reqinfGpEntity.getDzBurnLose());
		dzSEntity.setDzCutHeadEnd(reqinfGpEntity.getDzCutHeadEnd());
		dzSEntity.setDzLoseAdd(reqinfGpEntity.getDzLoseAdd());
		dzSEntity.setCreateName(createName);
		dzSEntity.setCreateTime(createTime);
		TimeEntity TimeEntiy;
		try { // 3.1计划开始时间(这个时间应该从数据库里查)
			String planTimeB = helperBean.getMaxPlanTimeE(conn,
					dzSEntity.getPlineCode(), "C", rollMonth, gxProSeq);
			if (planTimeB.equals("")) {
				return;
			}
			TimeEntiy = helperBean.calculateTime(plnNUM, capacity, planTimeB);
			// 计划结束时间
			String planTimeE = TimeEntiy.getEndTime();
			// 修改序号
			helperBean.seqIncrease(conn, dzSEntity.getPlineCode(), gxProSeq,
					-1, 1, "C");
			// 时间自动调整
			helperBean.timeIncrease(conn, gxProSeq, dzSEntity.getPlineCode(),
					planTimeE, "C");
			dzSEntity.setPlanTimeB(planTimeB);
			dzSEntity.setPlanTimeE(planTimeE);
			sqlUp = EntityTool.fillSqlByEntity(sqlAdd, dzSEntity);
			ps = conn.prepareStatement(sqlUp);
			ps.executeUpdate();
			ps.close();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 新增加工耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 */
	public static void addOrderJgxS(Connection conn, String proPlanId,
			String versionNo, PlnOrdInfo ordInfo, PlnOrderJgxEntity jgxEntity)
			throws SQLException {
		double hrCapcty = 0;
		int wasteNum = 0;// 万支废品数
		double inWt = 0;
		int inNum = 0;
		int ordWasteNum = 0;// 订单加投支
		double ordWasteWt = 0;// 订单加投吨
		int plnNUM = 0;// 订单支
		double capacity = 0;// 产线能力
		PlnOrderJgxSEntity jgxSEntity = new PlnOrderJgxSEntity();
		jgxSEntity.setVersionNo(versionNo);
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.queryJgxPineHrCapcty");
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 0;
			wasteNum = rs.getInt("WASTE_RATE");
			inWt = rs.getDouble("IN_WEIGHT");
			inNum = rs.getInt("IN_NUM");
			hrCapcty = rs.getDouble("USE_TIME");
			rate = wasteNum / 10000;
			ordWasteNum = (int) (inNum * rate);
			ordWasteWt = inWt * rate;
			plnNUM = rs.getInt("PLANNUM");
			capacity = rs.getDouble("HR_CAPCTY");
			if (rs.getString("CIC") != null) {
				jgxSEntity.setCic(rs.getString("CIC"));
			}
			if (rs.getString("PIC") != null) {
				jgxSEntity.setPic(rs.getString("PIC"));
			}
			if (rs.getString("SIC") != null) {
				jgxSEntity.setSic(rs.getString("SIC"));
			}
			if (rs.getString("DIC") != null) {
				jgxSEntity.setDic(rs.getString("DIC"));
			}
			if (rs.getString("WIC") != null) {
				jgxSEntity.setWic(rs.getString("WIC"));
			}
			if (rs.getString("GIC") != null) {
				jgxSEntity.setGic(rs.getString("GIC"));
			}
			if (rs.getString("STATION_ROAD") != null) {
				jgxSEntity.setStationRoad(rs.getString("STATION_ROAD"));
			}
		}
		rs.close();
		ps.close();

		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.getPlnGxJgxM");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("IF_EXPORT") != null) {
				jgxSEntity.setIfExport(rs.getString("IF_EXPORT"));
			}
			if (rs.getString("ABRVITION") != null) {
				jgxSEntity.setFinalUserDesc(rs.getString("ABRVITION"));
			}
			if (rs.getString("D_PLINE_NAME") != null) {
				jgxSEntity.setGroupRollName(rs.getString("D_PLINE_NAME"));
			}
			if (rs.getString("PRDCR_ADVC_DAY") != null) {
				jgxSEntity.setPrdcrAdvcDay(rs.getString("PRDCR_ADVC_DAY"));
			}
			if (rs.getString("PRIORITY") != null) {
				jgxSEntity.setPriority(rs.getString("PRIORITY"));
			}
			if (rs.getString("PRIORITY_MEMO") != null) {
				jgxSEntity.setPriorityMemo(rs.getString("PRIORITY_MEMO"));
			}
			if (rs.getString("DELVRY_EDATE") != null) {
				jgxSEntity.setDelvryEdate(rs.getString("DELVRY_EDATE"));
			}
			if (rs.getString("STATION_NM") != null) {
				jgxSEntity.setStationNm(rs.getString("STATION_NM"));
			}
		}
		rs.close();
		ps.close();

		// 查询制程
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.getPlnGxJgxMPl");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		while (rs.next()) {
			if (rs.getString("PROCESS_CODE") != null
					&& rs.getString("PROCESS_CODE").trim().equals("D")) {
				jgxSEntity.setGroupRollName(rs.getString("PROCESS_DESC_C"));
			}
			// if (rs.getString("WHOLE_BACKLOG_DESC") != null
			// && jgxSEntity.getWholeBacklogDesc().equals("")) {
			// jgxSEntity.setWholeBacklogDesc(rs
			// .getString("WHOLE_BACKLOG_DESC"));
			// }
		}
		rs.close();
		ps.close();
		String decision = "A";
		if (ordInfo.getOrderSource().equals("备料需求")) {
			decision = "B";
		}
		jgxSEntity.setDecision(decision);
		jgxSEntity.setProPlanId(proPlanId);
		jgxSEntity.setPlanUsetime(String.valueOf(hrCapcty));
		jgxSEntity.setAddWtIn(jgxEntity.getAddNumIn());
		jgxSEntity.setAddNumIn(jgxEntity.getAddWtIn());
		jgxSEntity.setInWeightS(String.valueOf(inWt
				- Double.parseDouble(jgxEntity.getAddWtIn())));
		jgxSEntity.setInNumS(String.valueOf(inNum
				- Integer.valueOf(jgxEntity.getAddNumIn()).intValue()));
		String[] plineArr = PlnHelper.getArray(conn,
				"CorePlnOrdProUseTime.getPlineCodeG",
				new String[] { proPlanId }, new String[] { "PLINE_CODE",
						"PLINE_NAME" });
		if (plineArr == null || plineArr[0] == null
				|| plineArr[0].length() == 0) {
			return;
		}
		ProducHelperBean helperBean = new ProducHelperBean();
		int gxProSeq = helperBean.getMaxGxProSeq(conn, plineArr[0], "G",
				ordInfo.getRollMonth());
		jgxSEntity.setPlineCode(plineArr[0]);
		jgxSEntity.setPlineName(plineArr[1]);
		jgxSEntity.setGxProSeq(String.valueOf(gxProSeq));
		jgxSEntity.setMsc(ordInfo.getMsc());
		jgxSEntity.setIndexSeq(ordInfo.getIndexSeq());
		jgxSEntity.setBom(ordInfo.getBom());
		jgxSEntity.setWholeBacklogDesc(ordInfo.getWholeBacklogDesc());

		TimeEntity TimeEntiy;
		try { // 3.1计划开始时间(这个时间应该从数据库里查)
			String planTimeB = helperBean.getMaxPlanTimeE(conn,
					jgxSEntity.getPlineCode(), "G", ordInfo.getRollMonth(),
					gxProSeq);
			if (planTimeB.equals("")) {
				return;
			}
			TimeEntiy = helperBean.calculateTime(plnNUM, capacity, planTimeB);
			// 计划结束时间
			String planTimeE = TimeEntiy.getEndTime();

			// 修改序号
			helperBean.seqIncrease(conn, jgxSEntity.getPlineCode(), gxProSeq,
					-1, 1, "G");
			// 时间自动调整
			helperBean.timeIncrease(conn, gxProSeq, jgxSEntity.getPlineCode(),
					planTimeE, "G");
			jgxSEntity.setPlanTimeB(planTimeB);
			jgxSEntity.setPlanTimeE(planTimeE);
			jgxSEntity.setOutnumCut(String.valueOf(ordInfo.getProCut()));
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnOrdProUseTime.addOrderJgxS");
			String sqlUp = EntityTool.fillSqlByEntity(sql, jgxSEntity);
			ps = conn.prepareStatement(sqlUp);
			ps.setObject(1, hrCapcty);
			// ps.setObject(2, hrCapcty);
			ps.executeUpdate();
			ps.close();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 新增热处理耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 */
	public static void addOrderRclS(Connection conn, String proPlanId,
			String versionNo, PlnOrdInfo ordInfo, PlnOrderRclEntity rclEntity)
			throws SQLException {
		double hrCapcty = 0;
		int wasteNum = 0;// 万支废品数
		double inWt = 0;
		int inNum = 0;
		int ordWasteNum = 0;// 订单加投支
		double ordWasteWt = 0;// 订单加投吨
		int plnNUM = 0;// 订单支
		double capacity = 0;// 产线能力
		PlnOrderRclSEntity rclSEntity = new PlnOrderRclSEntity();
		rclSEntity.setVersionNo(versionNo);
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.queryRclPineHrCapcty");
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 0;
			wasteNum = rs.getInt("WASTE_RATE");
			inWt = rs.getDouble("IN_WEIGHT");
			inNum = rs.getInt("IN_NUM");
			hrCapcty = rs.getDouble("USE_TIME");
			rate = wasteNum / 10000;
			ordWasteNum = (int) (inNum * rate);
			ordWasteWt = inWt * rate;
			plnNUM = rs.getInt("PLANNUM");
			capacity = rs.getDouble("HR_CAPCTY");
			if (rs.getString("LENGTHDISC") != null) {
				rclSEntity.setLengthdisc(rs.getString("LENGTHDISC"));
			}
			if (rs.getString("GRADECODE") != null) {
				rclSEntity.setGradecode(rs.getString("GRADECODE"));
			}
			if (rs.getString("GRADENAME") != null) {
				rclSEntity.setGradename(rs.getString("GRADENAME"));
			}
			if (rs.getString("CIC") != null) {
				rclSEntity.setCic(rs.getString("CIC"));
			}
			if (rs.getString("PIC") != null) {
				rclSEntity.setPic(rs.getString("PIC"));
			}
			if (rs.getString("SIC") != null) {
				rclSEntity.setSic(rs.getString("SIC"));
			}
			if (rs.getString("DIC") != null) {
				rclSEntity.setDic(rs.getString("DIC"));
			}
			if (rs.getString("WIC") != null) {
				rclSEntity.setWic(rs.getString("WIC"));
			}
			if (rs.getString("GIC") != null) {
				rclSEntity.setGic(rs.getString("GIC"));
			}
			if (rs.getString("STATION_ROAD") != null) {
				rclSEntity.setStationRoad(rs.getString("STATION_ROAD"));
			}
		}
		rs.close();
		ps.close();

		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.getPlnGxRclM");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("PRDCR_ADVC_DAY") != null) {
				rclSEntity.setPrdcrAdvcDay(rs.getString("PRDCR_ADVC_DAY"));
			}
		}
		rs.close();
		ps.close();
		String decision = ProducHelper.getDecision(ordInfo.getWholeBacklog(),
				ordInfo.getWholeBacklogDesc(), "F", ordInfo.getOrderSource());

		if (decision.equals("A")) {
			rclSEntity.setIsendgx("1");
		} else {
			rclSEntity.setIsendgx("0");
		}
		rclSEntity.setDecision(decision);
		rclSEntity.setProPlanId(proPlanId);
		rclSEntity.setPlanUsetime(String.valueOf(hrCapcty));
		rclSEntity.setAddWtIn(String.valueOf(rclEntity.getAddNumIn()));
		rclSEntity.setAddNumIn(String.valueOf(rclEntity.getAddWtIn()));
		rclSEntity.setInWeightS(String.valueOf(inWt
				- Double.parseDouble(rclEntity.getAddWtIn())));
		rclSEntity.setInNumS(String.valueOf(inNum - rclEntity.getAddNumIn()));
		rclSEntity.setWeigthSMin(rclEntity.getWeigthSMin());
		String[] plineArr = PlnHelper.getArray(conn,
				"CorePlnOrdProUseTime.getPlineCodeF",
				new String[] { proPlanId }, new String[] { "PLINE_CODE",
						"PLINE_NAME" });
		if (plineArr == null || plineArr[0] == null
				|| plineArr[0].length() == 0) {
			return;
		}
		ProducHelperBean helperBean = new ProducHelperBean();
		int gxProSeq = helperBean.getMaxGxProSeq(conn, plineArr[0], "F",
				ordInfo.getRollMonth());
		rclSEntity.setPlineCode(plineArr[0]);
		rclSEntity.setPlineName(plineArr[1]);
		rclSEntity.setGxProSeq(String.valueOf(gxProSeq));
		rclSEntity.setMsc(ordInfo.getMsc());
		rclSEntity.setIndexSeq(ordInfo.getIndexSeq());
		rclSEntity.setBom(ordInfo.getBom());
		rclSEntity.setWholeBacklogDesc(ordInfo.getWholeBacklogDesc());
		TimeEntity TimeEntiy;
		try { // 3.1计划开始时间(这个时间应该从数据库里查)
			String planTimeB = helperBean.getMaxPlanTimeE(conn,
					rclSEntity.getPlineCode(), "F", ordInfo.getRollMonth(),
					gxProSeq);
			if (planTimeB.equals("")) {
				return;
			}
			TimeEntiy = helperBean.calculateTime(plnNUM, capacity, planTimeB);
			// 计划结束时间
			String planTimeE = TimeEntiy.getEndTime();
			// 修改序号
			helperBean.seqIncrease(conn, rclSEntity.getPlineCode(), gxProSeq,
					-1, 1, "F");
			// 时间自动调整
			helperBean.timeIncrease(conn, gxProSeq, rclSEntity.getPlineCode(),
					planTimeE, "F");
			rclSEntity.setPlanTimeB(planTimeB);
			rclSEntity.setPlanTimeE(planTimeE);
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnOrdProUseTime.addOrderRclS");
			String sqlUp = EntityTool.fillSqlByEntity(sql, rclSEntity);
			ps = conn.prepareStatement(sqlUp);
			ps.setObject(1, hrCapcty);
			// ps.setObject(2, hrCapcty);
			ps.executeUpdate();
			ps.close();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 新增镦拔扩耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 */
	public static void addOrderDbkS(Connection conn, String proPlanId,
			String versionNo, PlnOrdInfo ordInfo, PlnOrderDbkEntity dbkEntity)
			throws SQLException {
		double hrCapcty = 0;
		int wasteNum = 0;// 万支废品数
		double inWt = 0;
		int inNum = 0;
		int ordWasteNum = 0;// 订单加投支
		double ordWasteWt = 0;// 订单加投吨
		int plnNUM = 0;// 订单支
		double capacity = 0;// 产线能力
		PlnOrderDbkSEntity dbkSEntity = new PlnOrderDbkSEntity();
		dbkSEntity.setVersionNo(versionNo);
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.queryDbkPineHrCapcty");
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 0;
			wasteNum = rs.getInt("WASTE_RATE");
			inWt = rs.getDouble("IN_WEIGHT");
			inNum = rs.getInt("IN_NUM");
			hrCapcty = rs.getDouble("USE_TIME");
			rate = wasteNum / 10000;
			ordWasteNum = (int) (inNum * rate);
			ordWasteWt = inWt * rate;
			plnNUM = rs.getInt("PLANNUM");
			capacity = rs.getDouble("HR_CAPCTY");
			// dbkSEntity.setLenCuthead(String.valueOf(rs.getDouble("CROP_HEAD")));
			// dbkSEntity.setLenCutend(String.valueOf(rs.getDouble("CROP_END")));
			if (rs.getString("CIC") != null) {
				dbkSEntity.setCic(rs.getString("CIC"));
			}
			if (rs.getString("PIC") != null) {
				dbkSEntity.setPic(rs.getString("PIC"));
			}
			if (rs.getString("SIC") != null) {
				dbkSEntity.setSic(rs.getString("SIC"));
			}
			if (rs.getString("DIC") != null) {
				dbkSEntity.setDic(rs.getString("DIC"));
			}
			if (rs.getString("WIC") != null) {
				dbkSEntity.setWic(rs.getString("WIC"));
			}
			if (rs.getString("GIC") != null) {
				dbkSEntity.setGic(rs.getString("GIC"));
			}
			if (rs.getString("STATION_ROAD") != null) {
				dbkSEntity.setStationRoad(rs.getString("STATION_ROAD"));
			}
		}
		rs.close();
		ps.close();
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.getPlnGxDbkM");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("IF_EXPORT") != null) {
				dbkSEntity.setIfExport(rs.getString("IF_EXPORT"));
			}
			if (rs.getString("ABRVITION") != null) {
				dbkSEntity.setFinalUserDesc(rs.getString("ABRVITION"));
			}
			if (rs.getString("PRIORITY") != null) {
				dbkSEntity.setPriority(rs.getString("PRIORITY"));
			}
			if (rs.getString("PRIORITY_MEMO") != null) {
				dbkSEntity.setPriorityMemo(rs.getString("PRIORITY_MEMO"));
			}
		}
		rs.close();
		ps.close();
		dbkSEntity.setPassCode("");
		dbkSEntity.setPassName("");
		String decision = ProducHelper.getDecision(ordInfo.getWholeBacklog(),
				ordInfo.getWholeBacklogDesc(), "E", ordInfo.getOrderSource());
		if (decision.equals("A")) {
			dbkSEntity.setIsendgx("1");
		} else {
			dbkSEntity.setIsendgx("0");
		}
		dbkSEntity.setDecision(decision);
		dbkSEntity.setLenCutend(ordInfo.getLenCutendCbk());
		dbkSEntity.setLenCuthead(ordInfo.getLenCutheadDbk());
		dbkSEntity.setProPlanId(proPlanId);
		dbkSEntity.setPlanUsetime(String.valueOf(hrCapcty));
		dbkSEntity.setAddWtIn(String.valueOf(dbkEntity.getAddNumIn()));
		dbkSEntity.setAddNumIn(String.valueOf(dbkEntity.getAddWtIn()));
		dbkSEntity.setInWeightS(String.valueOf(inWt
				- Double.parseDouble(dbkEntity.getAddWtIn())));
		dbkSEntity.setInNumS(String.valueOf(inNum
				- Integer.valueOf(dbkEntity.getAddNumIn()).intValue()));
		String[] plineArr = PlnHelper.getArray(conn,
				"CorePlnOrdProUseTime.getPlineCodeE",
				new String[] { proPlanId }, new String[] { "PLINE_CODE",
						"PLINE_NAME" });
		if (plineArr == null || plineArr[0] == null
				|| plineArr[0].length() == 0) {
			return;
		}
		ProducHelperBean helperBean = new ProducHelperBean();
		int gxProSeq = helperBean.getMaxGxProSeq(conn, plineArr[0], "E",
				ordInfo.getRollMonth());
		dbkSEntity.setPlineCode(plineArr[0]);
		dbkSEntity.setPlineName(plineArr[1]);
		dbkSEntity.setGxProSeq(String.valueOf(gxProSeq));
		dbkSEntity.setMsc(ordInfo.getMsc());
		dbkSEntity.setIndexSeq(ordInfo.getIndexSeq());
		dbkSEntity.setBom(ordInfo.getBom());
		dbkSEntity.setWholeBacklogDesc(ordInfo.getWholeBacklogDesc());
		dbkSEntity.setWeigthSMin(dbkEntity.getWeigthSMin());
		TimeEntity TimeEntiy;
		try { // 3.1计划开始时间(这个时间应该从数据库里查)
			String planTimeB = helperBean.getMaxPlanTimeE(conn,
					dbkSEntity.getPlineCode(), "E", ordInfo.getRollMonth(),
					gxProSeq);
			if (planTimeB.equals("")) {
				return;
			}
			TimeEntiy = helperBean.calculateTime(plnNUM, capacity, planTimeB);
			// 计划结束时间
			String planTimeE = TimeEntiy.getEndTime();
			// 修改序号
			helperBean.seqIncrease(conn, dbkSEntity.getPlineCode(), gxProSeq,
					-1, 1, "E");
			// 时间自动调整
			helperBean.timeIncrease(conn, gxProSeq, dbkSEntity.getPlineCode(),
					planTimeE, "E");
			dbkSEntity.setPlanTimeB(planTimeB);
			dbkSEntity.setPlanTimeE(planTimeE);
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnOrdProUseTime.addOrderDbkS");
			String sqlUp = EntityTool.fillSqlByEntity(sql, dbkSEntity);
			ps = conn.prepareStatement(sqlUp);
			ps.setObject(1, hrCapcty);
			// ps.setObject(2, hrCapcty);
			ps.executeUpdate();
			ps.close();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 错误信息
	 */
	private String verrMsg = "";

	/**
	 * 错误信息
	 */
	public String getVerrMsg() {
		return verrMsg;
	}

	/**
	 * 错误信息
	 */
	public void setVerrMsg(String verrMsg) {
		this.verrMsg = verrMsg;
	}

	/**
	 * 新增轧管耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @return
	 * @throws SQLException
	 */
	public boolean addOrderZgS(Connection conn, String proPlanId,
			String versionNo, PlnOrdInfo ordInfo, PlnOrderZgEntity zgEntity,
			String aimoutdiameter) throws SQLException {
		double hrCapcty = 0;
		int wasteNum = 0;// 万支废品数
		double inWt = 0;
		int inNum = 0;
		int ordWasteNum = 0;// 订单加投支
		double ordWasteWt = 0;// 订单加投吨
		int plnNUM = 0;// 订单支
		double capacity = 0;// 产线能力
		PlnOrderZgSEntity zgSEntity = new PlnOrderZgSEntity();
		zgSEntity.setVersionNo(versionNo);
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.queryZgPineHrCapcty");
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 0;
			int cutNum = rs.getInt("OUTNUM_SINGLE");
			wasteNum = rs.getInt("WASTE_RATE");
			inWt = rs.getDouble("IN_WT");
			inNum = rs.getInt("IN_NUM");
			// hrCapcty = rs.getDouble("USE_TIME");
			rate = wasteNum / 10000;
			plnNUM = rs.getInt("IN_NUM") + rs.getInt("ADD_NUM_IN");
			capacity = rs.getDouble("HR_CAPCTY");
			if (cutNum != 0) {
				ordWasteNum = (int) (inNum * rate / cutNum);
				ordWasteWt = inWt * rate / cutNum;
			}

			// zgSEntity.setDiameterMandrel(String.valueOf(rs
			// .getDouble("MANDREL_SIZE")));
			// zgSEntity.setLenCuthead(String.valueOf(rs.getDouble("CROP_HEAD")));
			// zgSEntity.setLenCutend(String.valueOf(rs.getDouble("CROP_END")));
			// if (rs.getString("PASS_CODE") != null) {
			// zgSEntity.setPassCode(rs.getString("PASS_CODE"));
			// }
			// if (rs.getString("PASS_NAME") != null) {
			// zgSEntity.setPassName(rs.getString("PASS_NAME"));
			// }
			if (rs.getString("CIC") != null) {
				zgSEntity.setCic(rs.getString("CIC"));
			}
			if (rs.getString("PIC") != null) {
				zgSEntity.setPic(rs.getString("PIC"));
			}
			if (rs.getString("SIC") != null) {
				zgSEntity.setSic(rs.getString("SIC"));
			}
			if (rs.getString("DIC") != null) {
				zgSEntity.setDic(rs.getString("DIC"));
			}
			if (rs.getString("WIC") != null) {
				zgSEntity.setWic(rs.getString("WIC"));
			}
			if (rs.getString("GIC") != null) {
				zgSEntity.setGic(rs.getString("GIC"));
			}
			if (rs.getString("STATION_ROAD") != null) {
				zgSEntity.setStationRoad(rs.getString("STATION_ROAD"));
			}
		}
		rs.close();
		ps.close();
		// if (capacity == 0) {
		// verrMsg = "合同号:" + zgEntity.getOrderNo() + "/"
		// + zgEntity.getOrderSeq() + "小时能力为零或为空。";
		// return false;
		// }

		String groupCic = "";
		String gradeCode = "";
		String groupWic = "";// 水压
		String groupPic = "";// 材质
		String groupSic = "";// 公差
		String groupDic = "";// 探伤
		String groupGic = "";// 加工
		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.selectGpIcList");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		while (rs.next()) {
			if (rs.getString("CIC") != null) {
				if (!rs.getString("CIC").equals("")) {
					groupCic += rs.getString("CIC") + ",";
				}
			}
			if (rs.getString("GRADECODE") != null) {
				gradeCode = rs.getString("GRADECODE");
			}
			if (rs.getString("PIC") != null) {
				if (!rs.getString("PIC").equals("")) {
					groupPic += rs.getString("PIC") + ",";
				}
			}
			if (rs.getString("SIC") != null) {
				if (!rs.getString("SIC").equals("")) {
					groupSic += rs.getString("SIC") + ",";
				}
			}
			if (rs.getString("DIC") != null) {
				if (!rs.getString("DIC").equals("")) {
					groupDic += rs.getString("DIC") + ",";

				}
			}
			if (rs.getString("WIC") != null) {
				if (!rs.getString("WIC").equals("")) {
					groupWic += rs.getString("WIC") + ",";
				}
			}
			if (rs.getString("GIC") != null) {
				if (!rs.getString("GIC").equals("")) {
					groupGic += rs.getString("GIC") + ",";
				}
			}
		}
		rs.close();
		ps.close();
		if (!groupDic.equals("") && groupDic.contains(",")) {
			groupDic = groupDic.substring(0, groupDic.length() - 1);
		}
		zgSEntity.setDicGroupGp(groupDic);
		zgSEntity.setDicGp(groupDic.split(",")[0]);

		if (!groupPic.equals("") && groupPic.contains(",")) {
			groupPic = groupPic.substring(0, groupPic.length() - 1);
		}
		if (ordInfo.getWholeBacklogDesc().contains("锻造")) {
			zgSEntity.setPicGroupGp(groupPic);
			zgSEntity.setPicGp(groupPic.split(",")[0]);
		}

		if (!groupSic.equals("") && groupSic.contains(",")) {
			groupSic = groupSic.substring(0, groupSic.length() - 1);
		}
		zgSEntity.setSicGroupGp(groupSic);
		zgSEntity.setSicGp(groupSic.split(",")[0]);

		if (!groupWic.equals("") && groupWic.contains(",")) {
			groupWic = groupWic.substring(0, groupWic.length() - 1);
		}
		zgSEntity.setWicGroupGp(groupWic);
		zgSEntity.setWicGp(groupWic.split(",")[0]);
		if (!groupGic.equals("") && groupGic.contains(",")) {
			groupGic = groupGic.substring(0, groupGic.length() - 1);
		}
		zgSEntity.setGicGroupGp(groupGic);
		zgSEntity.setGicGp(groupGic.split(",")[0]);

		if (!groupCic.equals("") && groupCic.contains(",")) {
			groupCic = groupCic.substring(0, groupCic.length() - 1);
		}
		String[] groupCic1 = null;
		String cicGroupYl = "";
		// if (!ordInfo.getOutsStlPipeFl().equals("121902")
		// && !ordInfo.getOutsStlPipeFl().equals("121903")) {
		// 冶炼索引码CIC
		String[] gradCodeYl = null;
		if(ordInfo.getGradecodeAll() !=null)
		{
			gradCodeYl = ordInfo.getGradecodeAll().split(",");
		}

		sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.selectGpIcListlk");
		sql += CoreConverter.toSqlConditionString("D.GRADENAME", gradCodeYl);
		sql += ")";
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		if (rs.next()) {
			cicGroupYl = rs.getString("CIC");
		}
		rs.close();
		ps.close();
		if (cicGroupYl == null) {
			verrMsg = "无钢种索引码,请联系工艺人员。";
			return false;
		}
		groupCic1 = cicGroupYl.split(",");

		// String[] groupCic1 = cicGroupYl.split(",");
		zgSEntity.setCicGroupGp(cicGroupYl);

		// 去除数组中的空格
		List<String> tmp = new ArrayList<String>();
		for (String str : groupCic1) {
			if (str != null && str.length() != 0) {
				tmp.add(str.trim());
			}
		}
		groupCic1 = tmp.toArray(new String[0]);
		// }
		String cicGroup = "";
		if (groupCic1 != null && groupCic1.length > 0) {
			// String sqlc = CoreXmlSqlParsersFactory
			// .getSql("CorePlnZgMStoGp.screenCicByGrade");
			// String outsBillet = "外购";
			// if (ordInfo.getOutsStlPipeFl().equals("121902")
			// || ordInfo.getOutsStlPipeFl().equals("121903")) {
			// outsBillet = "自炼";
			// }

			String sqlc = CoreXmlSqlParsersFactory
					.getSql("FrmBomConfirm.getQueryGrade");
			sqlc += " AND INSTR(T.GRADECODE,'" + gradeCode + "') > 0";
			sqlc += CoreConverter.toSqlConditionString("T.CIC", groupCic1);
			if (!ordInfo.getOutsStlPipeFl().equals("121902")
					&& !ordInfo.getOutsStlPipeFl().equals("121903")) {
				sqlc += " AND T1.PURCHASE_STD='" + ordInfo.getOutsBilletFl()
						+ "' ";
			}
			PreparedStatement pst = conn.prepareStatement(sqlc);
			// pst.setObject(1, gradeCode);
			ResultSet rst = pst.executeQuery();
			if (rst.next()) {
				if (rst.getString("CIC") != null) {
					cicGroup += rst.getString("CIC") + ",";
				}
				if (ordInfo.getOutsStlPipeFl().equals("121902")
						|| ordInfo.getOutsStlPipeFl().equals("121903")) {
					if (rst.getString("ALLOW_BUY").equals("外购")) {
						zgSEntity.setCicGp(rst.getString("CIC"));
					}
				}

				// if (rst.getString("STD_NAME_NK") != null) {
				// stdNameNk += rst.getString("STD_NAME_NK")+ ",";
				// }
			} else {
				if (ordInfo.getOutsStlPipeFl().equals("")
						|| ordInfo.getOutsStlPipeFl().equals("121901")) {
					if (ordInfo.getOutsBilletFl().equals("1")) {
						verrMsg = "无外购钢种索引码,请联系工艺人员。";
						return false;
					} else {
						verrMsg = "无自炼钢种索引码,请联系工艺人员。";
						return false;
					}
				}
			}
			rst.close();
			pst.close();
			if (!cicGroup.equals("") && cicGroup.contains(",")) {
				cicGroup = cicGroup.substring(0, cicGroup.length() - 1);
			}
			if (zgSEntity.getCicGp() == null || zgSEntity.getCicGp().equals("")) {
				zgSEntity.setCicGp(cicGroup.split(",")[0]);
			}
		}
		zgSEntity.setProPlanId(proPlanId);
		zgSEntity.setPlanUsetime(String.valueOf(hrCapcty));
		zgSEntity.setAddWtIn(String.valueOf(zgEntity.getAddWtIn()));
		zgSEntity.setAddNumIn(String.valueOf(zgEntity.getAddNumIn()));
		zgSEntity.setInWeightS(String.valueOf(inWt
				- Double.parseDouble(zgEntity.getAddWtIn())));
		if(zgEntity.getAddNumIn().equals(""))
		{
			zgEntity.setAddNumIn("0");
		}
		zgSEntity.setInNumS(String.valueOf(inNum
				- Integer.valueOf(zgEntity.getAddNumIn()).intValue()));
		String[] plineArr = PlnHelper.getArray(conn,
				"CorePlnOrdProUseTime.getPlineCodeD",
				new String[] { proPlanId }, new String[] { "PLINE_CODE",
						"PLINE_NAME" });
		if (plineArr == null || plineArr[0] == null
				|| plineArr[0].length() == 0) {
			verrMsg = "无轧管产线。";
			return false;
		}
		// 查询铸造投入产出
		String materInB = "";
		String materOutB = "";
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("CorePlnProcessOrd.queryDesginBom");
		PreparedStatement ps1 = conn.prepareStatement(sql1);
		ps1.setString(1, ordInfo.getBom());
		ResultSet rs1 = ps1.executeQuery();
		if (rs1.next()) {
			materInB = rs1.getString("B_IN_MATERIAL_NO");
			materOutB = rs1.getString("B_OUT_MATERIAL_NO");
		}
		rs1.close();
		ps1.close();
		String plinCodeB = "";
		String plinNameB = "";
		if (!plineArr[0].equals("C008")) {
			// 获取铸造产线
			sql1 = CoreXmlSqlParsersFactory
					.getSql("CorePlnProcessOrd.queryBaseMaterialBPl");
			// if (plineArr[0].equals("C008")) {
			// sql += " AND B.PLINE_CODE = 'C002'";
			// }
			// sql += " ) ORDER BY T.PLINE_LEVEL ASC, T.PRD_RATE DESC";
			ps1 = conn.prepareStatement(sql1);
			ps1.setString(1, materInB);
			ps1.setString(2, materOutB);
			ps1.setString(3, ordInfo.getDesginKey());
			ps1.setString(4, ordInfo.getIndexSeq());
			rs1 = ps1.executeQuery();
			if (rs1.next()) {
				plinCodeB = rs1.getString("PLINE_CODE");
				plinNameB = rs1.getString("PLINE_NAME");
			}
			rs1.close();
			ps1.close();
		} else {
			plinCodeB = "C002";
			plinNameB = "一炼铸机";
		}
		String gradCodeArr = "";
		String gradNameArr = "";
		String cicGroupLk = "";
		String[] gradArr = PlnHelper.getArray(conn,
				"CorePlnProcessOrd.queryDesginPl",
				new String[] { ordInfo.getDesginKey(), ordInfo.getDesginKey(),
						ordInfo.getDesginKey() }, new String[] { "GRADECODE",
						"GRADENAME" });
		if (gradArr == null || gradArr[0] == null || gradArr[0].length() == 0) {

		} else {
			gradCodeArr = gradArr[0];
			gradNameArr = gradArr[1];
			String[] gradCode = gradCodeArr.split(",");
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnOrdProUseTime.selectGpIcListlk");
			sql += CoreConverter.toSqlConditionString("D.GRADECODE", gradCode);
			sql += ")";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, proPlanId);
			rs = ps.executeQuery();
			if (rs.next()) {
				cicGroupLk = rs.getString("CIC");
			}
			rs.close();
			ps.close();
		}

		zgSEntity.setGradecodeLk(gradCodeArr);
		zgSEntity.setGradenameLk(gradNameArr);
		zgSEntity.setCicGroupLk(cicGroupLk);
		ProducHelperBean helperBean = new ProducHelperBean();
		int gxProSeq = helperBean.getMaxGxProSeq(conn, plineArr[0], "D",
				ordInfo.getRollMonth());
		zgSEntity.setPlineCode(plineArr[0]);
		zgSEntity.setPlineName(plineArr[1]);
		zgSEntity.setGxProSeq(String.valueOf(gxProSeq));
		zgSEntity.setMsc(ordInfo.getMsc());
		zgSEntity.setIndexSeq(ordInfo.getIndexSeq());
		zgSEntity.setBom(ordInfo.getBom());
		zgSEntity.setWholeBacklogDesc(ordInfo.getWholeBacklogDesc());
		zgSEntity.setDiameterMandrel(String.valueOf(ordInfo
				.getDiameterMandrel()));
		zgSEntity.setOutdiameterZg(zgEntity.getOutdiameterZg());
		zgSEntity.setWallthickZg(zgEntity.getWallthickZg());
		zgSEntity.setAimoutdiameter(String.valueOf(aimoutdiameter));

		zgSEntity.setLenCuthead(zgEntity.getLenCuthead());
		zgSEntity.setLenCutend(zgEntity.getLenCutend());
		zgSEntity.setPassCode(zgEntity.getPassCode());
		zgSEntity.setPassName(zgEntity.getPassName());
		zgSEntity.setVerify1(zgEntity.getVerify1());
		zgSEntity.setVerify2(zgEntity.getVerify2());
		zgSEntity.setVerify3(zgEntity.getVerify3());
		zgSEntity.setGpreqNum(zgEntity.getGpreqNum());
		zgSEntity.setGpreqWeight(new BigDecimal(zgEntity.getGpreqWeight())
				.setScale(3, RoundingMode.HALF_UP).doubleValue());
		zgSEntity
				.setGpreqWeightMin(new BigDecimal(zgEntity.getGpreqWeightMin())
						.setScale(3, RoundingMode.HALF_UP).doubleValue());
		zgSEntity.setTubeEndRatio(zgEntity.getTubeEndRatio());
		zgSEntity.setWeigthSMin(zgEntity.getWeigthSMin());
		zgSEntity.setThicknessWall(zgEntity.getThicknessWall());
		zgSEntity.setDecision(zgEntity.getDecision());
		zgSEntity.setProdStandard(zgEntity.getProdStandard());
		zgSEntity.setOutputStandard(zgEntity.getOutputStandard());
		zgSEntity.setOutsBilletFl(ordInfo.getOutsBilletFl());
		if (ordInfo.getOrderUnit().equals("米")
				|| ordInfo.getOrderUnit().equals("英尺")
				|| ordInfo.getTotlength() > 0) {
			zgSEntity.setIfLenControl("1");
		} else {
			zgSEntity.setIfLenControl("0");
		}
		if (ordInfo.getOrderUnit().equals("米")
				|| ordInfo.getOrderUnit().equals("英尺")
				|| ordInfo.getWeighType() != null
				&& ordInfo.getWeighType().contains("理")) {
			zgSEntity.setIfMinusSicRoll("1");
		} else {
			zgSEntity.setIfMinusSicRoll("0");
		}
		String applyNo = "";
		String applyStatus = "";
		String issuedMemo = "";
		// 判定是否是补量合同
//		if (ordInfo.getOrderSource().equals("补量需求")) {

			// 查询改合同是否有推荐长度申请
			sql1 = CoreXmlSqlParsersFactory
					.getSql("CorePlnOrdProUseTime.queryZgProApplyNo");
			ps1 = conn.prepareStatement(sql1);
			ps1.setString(1, ordInfo.getOrderNo());
			ps1.setString(2, ordInfo.getOrderSeq());
			rs1 = ps1.executeQuery();
			if (rs1.next()) {
				applyNo = rs1.getString("APPLY_NO");
				applyStatus = rs1.getString("APPLY_STATUS");
				issuedMemo = rs1.getString("ISSUED_MEMO");
			}
			rs1.close();
			ps1.close();
//		}
		if (zgEntity.getDecision().equals("A")) {
			zgSEntity.setIsendgx("1");
		} else {
			zgSEntity.setIsendgx("0");
		}
		zgSEntity.setPlansNum(zgEntity.getPlansNum());
		// 耗时=需坯支*小时能力
		if (capacity <= 0) {
			hrCapcty = 0;
		} else {
			hrCapcty = zgEntity.getGpreqNum() * 60 / capacity;
		}
		TimeEntity TimeEntiy;
		try { // 3.1计划开始时间(这个时间应该从数据库里查)
			String planTimeB = helperBean.getMaxPlanTimeE(conn,
					zgSEntity.getPlineCode(), "D", ordInfo.getRollMonth(),
					gxProSeq);
			if (planTimeB.equals("")) {
				verrMsg = "无轧管开始时间。";
				return false;
			}
			TimeEntiy = helperBean.calculateTime(zgEntity.getGpreqNum(),
					capacity, planTimeB);
			// 计划结束时间
			String planTimeE = TimeEntiy.getEndTime();
			// 修改序号
			helperBean.seqIncrease(conn, zgSEntity.getPlineCode(), gxProSeq,
					-1, 1, "D");
			// 时间自动调整
			helperBean.timeIncrease(conn, gxProSeq, zgSEntity.getPlineCode(),
					planTimeE, "D");
			zgSEntity.setPlanTimeB(planTimeB);
			zgSEntity.setPlanTimeE(planTimeE);
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePlnOrdProUseTime.addOrderZgS");
			String sqlUp = EntityTool.fillSqlByEntity(sql, zgSEntity);
			ps = conn.prepareStatement(sqlUp);
			ps.setObject(1, hrCapcty);
			// ps.setObject(2, hrCapcty);
			ps.setObject(2, plinCodeB);
			ps.setObject(3, plinNameB);
			ps.setObject(4, applyNo);
			ps.setObject(5, applyStatus);
			ps.setObject(6, issuedMemo);
			ps.executeUpdate();
			ps.close();
			// helperBean.generateZyBatchId(conn, zgSEntity.getPlineCode(),
			// "D");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 删除铸造工序排产
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static void deleteOrderCastS(Connection conn, String proPlanId)
			throws SQLException, ParseException {
		List<PlnOrderCastSEntity> entityList = PlnHelper.getEntityDatas(conn,
				new String[] { proPlanId }, "ProducHelperBean.getPlanOrderB",
				"and t.pro_plan_id=?", PlnOrderCastSEntity.class);
		if (entityList == null || entityList.size() == 0) {
			return;
		}
		int i = 0;
		for (PlnOrderCastSEntity entity : entityList) {
			// 修改计划时间
			helperBean.timeIncrease(conn,
					Integer.parseInt(entity.getGxProSeq()),
					entity.getPlineCode(), entity.getPlanTimeE(), "B");
			// 修改顺序序号
			helperBean.seqIncrease(conn, entity.getPlineCode(),
					Integer.parseInt(entity.getGxProSeq()) - i, -1, -1, "B");
			i++;
			PlnHelper.deleteOrUpdate(conn,
					"CorePlnOrdProUseTime.deleteOrderCastS", new String[] {
							entity.getProPlanId(), entity.getGxPlanNo() });
		}
	}

	/**
	 * 删除锻造工序排产
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static void deleteOrderDzS(Connection conn, String proPlanId)
			throws SQLException, ParseException {

		List<PlnOrderDzSEntity> entityList = PlnHelper.getEntityDatas(conn,
				new String[] { proPlanId }, "ProducHelperBean.getPlanOrderC",
				"and t.pro_plan_id=?", PlnOrderDzSEntity.class);
		if (entityList == null || entityList.size() == 0) {
			return;
		}
		int i = 0;
		for (PlnOrderDzSEntity entity : entityList) {
			// 修改计划时间
			helperBean.timeIncrease(conn,
					Integer.parseInt(entity.getGxProSeq()),
					entity.getPlineCode(), entity.getPlanTimeE(), "C");
			// 修改顺序序号
			helperBean.seqIncrease(conn, entity.getPlineCode(),
					Integer.parseInt(entity.getGxProSeq()) - i, -1, -1, "C");
			i++;
			PlnHelper.deleteOrUpdate(conn,
					"CorePlnOrdProUseTime.deleteOrderDzS", new String[] {
							entity.getProPlanId(), entity.getGxPlanNo() });
		}
	}

	/**
	 * 删除加工耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static void deleteOrderJgxS(Connection conn, String proPlanId)
			throws SQLException, ParseException {
		List<PlnOrderJgxSEntity> entityList = PlnHelper.getEntityDatas(conn,
				new String[] { proPlanId },
				"CorePlnOrdProUseTime.getPlanOrderG", "and t.pro_plan_id=?",
				PlnOrderJgxSEntity.class);
		if (entityList == null || entityList.size() == 0) {
			return;
		}
		int i = 0;
		for (PlnOrderJgxSEntity entity : entityList) {
			// 修改计划时间
			helperBean.timeIncrease(conn,
					Integer.parseInt(entity.getGxProSeq()),
					entity.getPlineCode(), entity.getPlanTimeE(), "G");
			// 修改顺序序号
			helperBean.seqIncrease(conn, entity.getPlineCode(),
					Integer.parseInt(entity.getGxProSeq()) - i, -1, -1, "G");
			i++;
			PlnHelper.deleteOrUpdate(conn,
					"CorePlnOrdProUseTime.deleteOrderJgxS", new String[] {
							entity.getProPlanId(), entity.getGxPlanNo() });
		}
	}

	/**
	 * 删除热处理耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static void deleteOrderRclS(Connection conn, String proPlanId)
			throws SQLException, ParseException {
		List<PlnOrderRclSEntity> entityList = PlnHelper.getEntityDatas(conn,
				new String[] { proPlanId },
				"CorePlnOrdProUseTime.getPlanOrderF", "and t.pro_plan_id=?",
				PlnOrderRclSEntity.class);
		if (entityList == null || entityList.size() == 0) {
			return;
		}
		int i = 0;
		for (PlnOrderRclSEntity entity : entityList) {
			// 修改计划时间
			helperBean.timeIncrease(conn,
					Integer.parseInt(entity.getGxProSeq()),
					entity.getPlineCode(), entity.getPlanTimeE(), "F");
			// 修改顺序序号
			helperBean.seqIncrease(conn, entity.getPlineCode(),
					Integer.parseInt(entity.getGxProSeq()) - i, -1, -1, "F");
			i++;
			PlnHelper.deleteOrUpdate(conn,
					"CorePlnOrdProUseTime.deleteOrderRclS", new String[] {
							entity.getProPlanId(), entity.getGxPlanNo() });
		}

	}

	/**
	 * 删除镦拔扩耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static void deleteOrderDbkS(Connection conn, String proPlanId)
			throws SQLException, ParseException {
		List<PlnOrderDbkSEntity> entityList = PlnHelper.getEntityDatas(conn,
				new String[] { proPlanId },
				"CorePlnOrdProUseTime.getPlanOrderE", "and t.pro_plan_id=?",
				PlnOrderDbkSEntity.class);
		if (entityList == null || entityList.size() == 0) {
			return;
		}
		int i = 0;
		for (PlnOrderDbkSEntity entity : entityList) {
			// 修改计划时间
			helperBean.timeIncrease(conn,
					Integer.parseInt(entity.getGxProSeq()),
					entity.getPlineCode(), entity.getPlanTimeE(), "E");
			// 修改顺序序号
			helperBean.seqIncrease(conn, entity.getPlineCode(),
					Integer.parseInt(entity.getGxProSeq()) - i, -1, -1, "E");
			i++;
			PlnHelper.deleteOrUpdate(conn,
					"CorePlnOrdProUseTime.deleteOrderDbkS", new String[] {
							entity.getProPlanId(), entity.getGxPlanNo() });
		}
	}

	/**
	 * 删除轧管耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static void deleteOrderZgS(Connection conn, String proPlanId)
			throws SQLException, ParseException {
		List<PlnOrderZgSEntity> entityList = PlnHelper.getEntityDatas(conn,
				new String[] { proPlanId },
				"CorePlnOrdProUseTime.getPlanOrderD", "and t.pro_plan_id=?",
				PlnOrderZgSEntity.class);
		if (entityList == null || entityList.size() == 0) {
			return;
		}
		int i = 0;
		for (PlnOrderZgSEntity entity : entityList) {
			// 修改计划时间
			helperBean.timeIncrease(conn,
					Integer.parseInt(entity.getGxProSeq()),
					entity.getPlineCode(), entity.getPlanTimeE(), "D");
			// 修改顺序序号
			helperBean.seqIncrease(conn, entity.getPlineCode(),
					Integer.parseInt(entity.getGxProSeq()) - i, -1, -1, "D");
			i++;
			PlnHelper.deleteOrUpdate(conn,
					"CorePlnOrdProUseTime.deleteOrderZgS", new String[] {
							entity.getProPlanId(), entity.getGxPlanNo() });
		}
	}

	/**
	 * 更新加工耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @param planWt
	 * @param planNum
	 * @throws SQLException
	 */
	public static void updateOrderJgxS(Connection conn, String proPlanId,
			double planWt, int planNum) throws SQLException {
		int ordMNum = 0;// 工序订单支数
		String sqlQ = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.selectPlnGxJgxM");
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.updateOrderJgxS");
		PreparedStatement ps = conn.prepareStatement(sqlQ);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 1;
			ordMNum = rs.getInt("PLANNUM");
			if (ordMNum > 0) {
				rate = planNum / ordMNum;
			}
			sql = sql.replaceAll("'#RATE#'", String.valueOf(rate));
			PreparedStatement psta = conn.prepareStatement(sql);
			psta.setObject(1, proPlanId);
			psta.executeUpdate();
			psta.close();
		}
		rs.close();
		ps.close();
	}

	/**
	 * 更新热处理耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @param planWt
	 * @param planNum
	 * @throws SQLException
	 */
	public static void updateOrderRclS(Connection conn, String proPlanId,
			double planWt, int planNum) throws SQLException {
		int ordMNum = 0;// 工序订单支数
		String sqlQ = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.selectPlnGxRclM");
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.updateOrderRclS");
		PreparedStatement ps = conn.prepareStatement(sqlQ);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 1;
			ordMNum = rs.getInt("PLANNUM");
			if (ordMNum > 0) {
				rate = planNum / ordMNum;
			}
			sql = sql.replaceAll("'#RATE#'", String.valueOf(rate));
			PreparedStatement psta = conn.prepareStatement(sql);
			psta.setObject(1, proPlanId);
			psta.executeUpdate();
			psta.close();
		}
		rs.close();
		ps.close();
	}

	/**
	 * 更新镦拔扩耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @param planWt
	 * @param planNum
	 * @throws SQLException
	 */
	public static void updateOrderDbkS(Connection conn, String proPlanId,
			double planWt, int planNum) throws SQLException {
		int ordMNum = 0;// 工序订单支数
		String sqlQ = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.selectPlnGxDbkM");
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.updateOrderDbkS");
		PreparedStatement ps = conn.prepareStatement(sqlQ);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 1;
			ordMNum = rs.getInt("PLANNUM");
			if (ordMNum > 0) {
				rate = planNum / ordMNum;
			}
			sql = sql.replaceAll("'#RATE#'", String.valueOf(rate));
			PreparedStatement psta = conn.prepareStatement(sql);
			psta.setObject(1, proPlanId);
			psta.executeUpdate();
			psta.close();
		}
		rs.close();
		ps.close();
	}

	/**
	 * 更新轧管耗时工序
	 * 
	 * @param conn
	 * @param proPlanId
	 * @param planWt
	 * @param planNum
	 * @throws SQLException
	 */
	public static void updateOrderZgS(Connection conn, String proPlanId,
			double planWt, int planNum) throws SQLException {
		int ordMNum = 0;// 工序订单支数
		String sqlQ = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.selectPlnGxZgM");
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePlnOrdProUseTime.updateOrderZgS");
		PreparedStatement ps = conn.prepareStatement(sqlQ);
		ps.setObject(1, proPlanId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			double rate = 1;
			ordMNum = rs.getInt("PLAN_NUM");
			if (ordMNum > 0) {
				rate = planNum / ordMNum;
			}
			sql = sql.replaceAll("'#RATE#'", String.valueOf(rate));
			PreparedStatement psta = conn.prepareStatement(sql);
			psta.setObject(1, proPlanId);
			psta.executeUpdate();
			psta.close();
		}
		rs.close();
		ps.close();
	}

}