package com.steering.pss.plnsaleord.steelMarkingPlan;

import java.lang.reflect.InvocationTargetException;
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.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.lenthCal.CoreIsRounded;
import com.steering.pss.plnsaleord.processOrder.base.BaseHelper;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.ComBaseWaterGroupEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnBaseLzSpecCutEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnBasePlineLgEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnBasePlineLzEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnOrderCastSEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforOrdEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforcastEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforfurnaceEntity;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

/**
 * 炉次/浇次计划
 * 
 * @author Administrator
 */
public class FrmSteelMakingPlanChange extends DBCommon {
	private static double Pi = 3.1415926; // 圆周率

	/**
	 * 查询最大炉数
	 * 
	 * @param strArrList
	 * @param plineCode
	 * @return
	 */
	@SuppressWarnings("null")
	public CoreReturnObject getCastMaxNum(ArrayList<String[]> strArrList,
			String plineCode) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getCastNum");
		try {
			int num = 0;
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			for (String[] strArr : strArrList) {
				ps = con.prepareStatement(sql);
				ps.setObject(1, strArr[0]);
				ps.setObject(2, strArr[1]);
				ps.setObject(3, plineCode);
				rs = ps.executeQuery();
				if (rs.next()) {
					if (rs.getString("stove_cnt") != null
							&& !rs.getString("stove_cnt").equals("")) {
						String stoveCnt = rs.getString("stove_cnt");
						if (num < Integer.parseInt(stoveCnt)) {
							num = Integer.parseInt(stoveCnt);
						}
					}
				}
			}
			core.setResult(num);
			return core;
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return core;
	}

	/**
	 * 通过钢种集合查询连浇群组
	 * 
	 * @param con
	 * @param gradeList
	 * @return
	 * @throws SQLException
	 */
	public static List<ComBaseWaterGroupEntity> getCastGroupByGradeList(
			Connection con, List<String> gradeList) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getCastGroupByGradeList");
		for (String gradecode : gradeList) {
			sql += "  and instr(t1.gradecode,'" + gradecode + "')>0";
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<ComBaseWaterGroupEntity> list = EntityTool.getEntitysByRs(rs,
				ComBaseWaterGroupEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			return list;
		}
	}

	/**
	 * 验证连浇群组
	 * 
	 * @param gradecodeList
	 * @param gradenameList
	 * @return
	 */
	public CoreReturnObject getCastInPlaceGroup(
			ArrayList<String> gradecodeList, ArrayList<String> gradenameList) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		// 0 提示信息 1 连浇群组号 2连浇群组名 3　钢种排序
		String[] messageArr = new String[] { "", "", "", "" };
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			// 查询包含所有钢种的连浇群组
			List<ComBaseWaterGroupEntity> groupList = FrmSteelMakingPlanChange
					.getCastGroupByGradeList(con, gradecodeList);
			if (groupList == null) {
				messageArr[0] = "不存在钢种(" + gradenameList.toString() + ")的连浇群组!";
				core.setResult(messageArr);
				return core;
			}
			String[] paramArr = new String[gradecodeList.size()];
			for (int i = 0; i < gradecodeList.size(); i++) {
				paramArr[i] = gradecodeList.get(i);
			}
			// 把多个钢种任意组合成字符串集合
			ArrayList<String> gradeStrList = new ArrayList<String>();
			gradeStrList = SteelHelper.permutation(paramArr, 0, gradeStrList);
			for (ComBaseWaterGroupEntity groupEntity : groupList) {
				String gradecodeArr = groupEntity.getGradecode();
				for (String gradeStr : gradeStrList) {
					if (gradecodeArr.contains(gradeStr)) {
						messageArr[1] = groupEntity.getWaterGroupCode();
						messageArr[2] = groupEntity.getWaterGroup();
						messageArr[3] = gradeStr;
						core.setResult(messageArr);
						return core;
					}
				}
			}
		} catch (Exception e) {
			try {
				if (con != null) {
					con.rollback();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		messageArr[0] = "不存在钢种(" + gradenameList.toString() + ")连续的连浇群组!";
		core.setResult(messageArr);
		return core;
	}

	/**
	 * 查询炼钢数据
	 * 
	 * @param plinecode
	 * @return
	 */
	public CoreReturnObject getBasePlineLg(String plinecode) {
		Object[] params = new Object[] { plinecode };
		return this.getDataBySqlID("FrmSteelMakingPlanChange.getBasePlineLg",
				params);
	}

	/**
	 * 查询所选铸造订单索引码个数
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject queryCastCicCount(String[] proPlanIdall) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.queryCastCicCount");
		sql += CoreConverter.toSqlConditionString(
				"T.PRO_PLAN_ID||'/'||T.GX_PLAN_NO", proPlanIdall);
		sql += " GROUP BY CIC) ";
		return this.getData(sql);
	}

	/**
	 * 查询配料方案
	 * 
	 * @param plinecode
	 * @return
	 */
	public CoreReturnObject queryBatch(String plinecode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.queryBatch");
		if (!plinecode.equals("")) {
			sql += " AND T.PLINE_CODE='" + plinecode + "'";
		}
		return this.getData(sql);
	}

	/**
	 * 查询炉次订单信息是否有订单材
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject getPlnsteelforOrd(String furnaceNo) {
		Object[] params = new Object[] { furnaceNo };
		return this.getDataBySqlID(
				"FrmSteelMakingPlanChange.getPlnsteelforOrd", params);
	}

	/**
	 * 查询铸造数据
	 * 
	 * @param plinecode
	 * @return
	 */
	public CoreReturnObject getBasePlineLz(String plinecode) {
		Object[] params = new Object[] { plinecode };
		return this.getDataBySqlID("FrmSteelMakingPlanChange.getBasePlineLz",
				params);
	}

	/**
	 * 查询最大浇次号
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	String getMaxCastno(Connection con, String nowTime, String plineCode)
			throws SQLException {
		String castno = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getMaxCastno");
		PreparedStatement ps = con.prepareStatement(sql);
		String pineJc = "";
		if (plineCode.equals("C002")) {
			pineJc = "JC1";
		} else if (plineCode.equals("C004")) {
			pineJc = "JC2";
		} else if (plineCode.equals("C007")) {
			pineJc = "JC3";
		} else {
			ps.setObject(1, "JC");
		}
		ps.setObject(1, pineJc);
		ps.setObject(2, nowTime);
		ps.setObject(3, pineJc);
		ps.setObject(4, nowTime);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			castno = rs.getString("CAST_NO");
		}
		rs.close();
		ps.close();
		return castno;
	}

	/**
	 * 查询最大浇次号
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	String getMaxFurnaceNo(Connection con, String castNo) throws SQLException {
		String furnaceNo = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getMaxFurnaceNo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, castNo);
		ps.setObject(2, castNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			furnaceNo = rs.getString("FURNACENO");
		}
		rs.close();
		ps.close();
		return furnaceNo;
	}

	/**
	 * 查询浇次同一铸造产线最大浇次号顺序号
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	int getMaxCastSeqno(Connection con, String plineCode) throws SQLException {
		int castSeqno = 1;
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getMaxCastSeqno");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			castSeqno = rs.getInt("CAST_SEQ") + 1;
		}
		rs.close();
		ps.close();
		return castSeqno;
	}

	/**
	 * 查询炉次同一铸造产线最大炉号顺序号
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	int getMaxFurnSeqno(Connection con, String plineCode) throws SQLException {
		int furnSeqno = 1;
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getMaxFurnSeqno");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			furnSeqno = rs.getInt("FURNACENO_SEQ") + 1;
		}
		rs.close();
		ps.close();
		return furnSeqno;
	}

	/**
	 * 查询炉次订单同一铸造产线最大炉号顺序号
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	static int getMaxCcmSeqno(Connection con, String plineCode)
			throws SQLException {
		int furnSeqno = 1;
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getMaxCcmSeqno");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			furnSeqno = rs.getInt("CCM_SEQ") + 1;
		}
		rs.close();
		ps.close();
		return furnSeqno;
	}

	/**
	 * 获取数据库当前时间
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static String getSystemTime(Connection con) throws SQLException {
		String time = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getSystemTime");
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			time = rs.getString("DATETIME");
		}
		rs.close();
		ps.close();
		return time;
	}

	/**
	 * 获取炉次最大结束时间
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public String getCastTime(Connection con, String plineCode,
			String systemTime) throws SQLException {
		String time = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getCastTime");
		// sql+=" AND T.PLINE_CODE = ?";
		sql += " AND T.EF_PLINE_CODE = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			time = rs.getString("ENDTIME");
		}
		rs.close();
		ps.close();
		if (time == null || time.equals("")) {
			time = systemTime;
		}
		return time;
	}

	/**
	 * 新增浇次信息
	 * 
	 * @param con
	 * @param entity
	 * @throws SQLException
	 */
	void addCast(Connection con, PlnSteelforcastEntity entity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.addCast");
		sql = EntityTool.fillSqlByEntity(sql, entity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 新增炉次
	 * 
	 * @param con
	 * @param entity
	 * @throws SQLException
	 */
	void addFurnace(Connection con, PlnSteelforfurnaceEntity entity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.addFurnace");
		sql = EntityTool.fillSqlByEntity(sql, entity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 新增炉内计划
	 * 
	 * @param con
	 * @param entity
	 * @throws SQLException
	 */
	void addFurnaceOrd(Connection con, PlnSteelforOrdEntity entity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.addFurnaceOrd");
		sql = EntityTool.fillSqlByEntity(sql, entity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询电炉基础表数据
	 * 
	 * @param con
	 * @param plineCode
	 *            电炉
	 * @return
	 * @throws SQLException
	 */
	static PlnBasePlineLgEntity getPlineLgEntity(Connection con,
			String plineCode) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getBasePlineLg");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		List<PlnBasePlineLgEntity> list = EntityTool.getEntitysByRs(rs,
				PlnBasePlineLgEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() != 1) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/**
	 * 获取出钢量
	 * 
	 * @param con
	 * @param plineCode
	 * @return
	 * @throws SQLException
	 */
	public static double[] getPlineLgParams(Connection con, String plineCode)
			throws SQLException {
		PlnBasePlineLgEntity entity = FrmSteelMakingPlanChange
				.getPlineLgEntity(con, plineCode);
		if (entity == null) {
			return null;
		}
		double standSteel = 0;
		if (!entity.getStdOutWt().equals("")) {
			standSteel = Double.parseDouble(entity.getStdOutWt());
		}
		double minSteel = 0;
		if (!entity.getOutWtMin().equals("")) {
			minSteel = Double.parseDouble(entity.getOutWtMin());
		}
		double maxSteel = 0;
		if (!entity.getOutWtMax().equals("")) {
			maxSteel = Double.parseDouble(entity.getOutWtMax());
		}
		double[] paramArr = new double[] { standSteel, minSteel, maxSteel };
		return paramArr;
	}

	/**
	 * 获取模铸出钢量
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static double[] getMoildOutGrad(Connection con, String mould,
			String gradeName) throws SQLException {
		double minSteel = 0;
		double maxSteel = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.queryMould");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, mould);
		ps.setObject(2, gradeName);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			minSteel = rs.getDouble("OUT_WT_MIN");
			maxSteel = rs.getDouble("OUT_WT_MAX");
		}
		rs.close();
		ps.close();
		if (minSteel <= 0) {
			ps = con.prepareStatement(sql);
			ps.setObject(1, mould);
			ps.setObject(2, "20控Cr");
			rs = ps.executeQuery();
			if (rs.next()) {
				minSteel = rs.getDouble("OUT_WT_MIN");
				maxSteel = rs.getDouble("OUT_WT_MAX");
			}
			rs.close();
			ps.close();
		}
		double[] paramArr = new double[] { minSteel, maxSteel };
		return paramArr;
	}

	/**
	 * 获取配料方案
	 * 
	 * @param con
	 * @param plineCode
	 * @return
	 * @throws SQLException
	 */
	public static String[] getBatchingCode(Connection con, String plineCode)
			throws SQLException {
		String[] paramArr = null;
		String batchingCode = "";
		String batchingName = "";
		String materiel = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getBatchingCode");
		sql += " AND T.PLINE_CODE = ? AND T.DEF_VAL='1'";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			batchingCode = rs.getString("BATCHING_CODE");
			batchingName = rs.getString("BATCHING_NAME");
			materiel = rs.getString("MATERIEL");
			paramArr = new String[] { batchingCode, batchingName, materiel };
		}
		rs.close();
		ps.close();

		return paramArr;
	}

	/**
	 * 查询铸机基础表数据
	 * 
	 * @param con
	 * @param plineCode
	 * @return
	 * @throws SQLException
	 */
	public PlnBasePlineLzEntity getPlineLzEntity(Connection con,
			String plineCode) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getBasePlineLz");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, plineCode);
		ResultSet rs = ps.executeQuery();
		List<PlnBasePlineLzEntity> list = EntityTool.getEntitysByRs(rs,
				PlnBasePlineLzEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() != 1) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/**
	 * 获取铸机基础参数
	 * 
	 * @param entity
	 * @param castMode
	 * @return
	 */
	public double[] getPlineLzParams(PlnBasePlineLzEntity entity,
			String castMode) {
		double[] paramArr = new double[] { 0, 0, 0 };
		// 首炉切头长（m）
		double cutLenBegin = 0;
		if (!entity.getCutLenBegin().equals("")) {
			cutLenBegin = Double.parseDouble(entity.getCutLenBegin());
		}
		// 尾炉切头长(m)
		double coutLenEnd = 0;
		if (castMode.equals("0")) {
			// 不换中包方式
			if (!entity.getCutLenEndUnch().equals("")) {
				coutLenEnd = Double.parseDouble(entity.getCutLenEndUnch());
			}
		} else {
			if (!entity.getCutLenEndCh().equals("")) {
				coutLenEnd = Double.parseDouble(entity.getCutLenEndCh());
			}
		}
		// 中包铸余
		double zbLeftWgt = 0;
		if (!entity.getZbLeftWt().equals("")) {
			coutLenEnd = Double.parseDouble(entity.getZbLeftWt());
		}
		paramArr[0] = cutLenBegin;
		paramArr[1] = coutLenEnd;
		paramArr[2] = zbLeftWgt;
		return paramArr;
	}

	/**
	 * 查询连铸规格切头尾参数表
	 * 
	 * @param con
	 * @param dimater
	 *            直径
	 * @return
	 * @throws SQLException
	 */
	public static PlnBaseLzSpecCutEntity getBaseLzSpecCut(Connection con,
			String dimater) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.getBaseLzSpecCut");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dimater);
		ResultSet rs = ps.executeQuery();
		List<PlnBaseLzSpecCutEntity> list = EntityTool.getEntitysByRs(rs,
				PlnBaseLzSpecCutEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/**
	 * 查询连铸规格切头尾参数
	 * 
	 * @param con
	 * @param dimater
	 * @return 0 切头 1不换中包切尾长（m）2换中包切尾长（m）
	 * @throws SQLException
	 */
	public static double[] getBaseLzSpecParams(Connection con, String dimater,
			String castMode) throws SQLException {
		PlnBaseLzSpecCutEntity entity = FrmSteelMakingPlanChange
				.getBaseLzSpecCut(con, dimater);
		if (entity == null) {
			return null;
		}
		double cutLenBegin = 0;
		if (!entity.getCutLenBegin().equals("")) {
			cutLenBegin = Double.parseDouble(entity.getCutLenBegin());
		}
		double cutLenEndUnch = 0;
		if (!entity.getCutLenEndUnch().equals("")) {
			cutLenEndUnch = Double.parseDouble(entity.getCutLenEndUnch());
		}
		double cutLenEndCh = 0;
		if (!entity.getCutLenEndCh().equals("")) {
			cutLenEndCh = Double.parseDouble(entity.getCutLenEndCh());
		}
		double[] param = new double[2];
		param[0] = cutLenBegin;
		if (castMode.equals("1")) {
			param[1] = cutLenEndCh;
		} else {
			param[1] = cutLenEndUnch;
		}
		return param;
	}

	/**
	 * 查询合同号，用户，去向，钢级，品名
	 * 
	 * @param con
	 * @param castEntity
	 * @throws SQLException
	 */
	public static String[] getOrderUser(Connection con,
			PlnOrderCastSEntity castEntity) throws SQLException {
		String[] param = new String[10];
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.queryOrderUser");
		sql = EntityTool.fillSqlByEntity(sql, castEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			param[0] = rs.getString("ORDER_NO");
			param[1] = rs.getString("FINAL_USER_DESC");
			param[2] = rs.getString("PLINE_CODE");
			param[3] = rs.getString("PLINE_NAME");
			param[4] = rs.getString("STEELCODE");
			param[5] = rs.getString("STEELNAME");
			param[6] = rs.getString("PRODUCCODE");
			param[7] = rs.getString("PRODUCNAME");
			param[8] = rs.getString("PDEPT_NAME");
			param[9] = rs.getString("PRDCR_MAN");
		}
		rs.close();
		ps.close();
		return param;
	}

	/**
	 * 产线对应余材合同号，CIC
	 * 
	 * @param con
	 * @param
	 * @throws SQLException
	 * @throws ParseException
	 */
	public static String getBlOrder(Connection con, String plineCode,
			String proMonth) throws SQLException, ParseException {
		String orderNo = "";
		// SimpleDateFormat dFormat = new SimpleDateFormat("yyMM");
		// SimpleDateFormat dFormat1 = new SimpleDateFormat("yyyyMM");
		// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// String dateTime = getSystemTime(con);
		String orderSeq = "";
		String yearM = proMonth.substring(2, 6);
		String proMatch = proMonth.substring(0, 6);
		if (plineCode.equals("C001")) {// 一炼
			orderSeq = "ZZBY-AY" + yearM + "/";
		} else if (plineCode.equals("C003")) {// 二炼
			orderSeq = "ZZBY-AE" + yearM + "/";
		} else if (plineCode.equals("C006")) {// 三炼
			orderSeq = "ZZBY-AS" + yearM + "/";
		}
		// 查询最大余材合同号
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.queryPlineOrderBl");
		sql += " and T.ORDER_NO LIKE '%" + orderSeq + "%'";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, orderSeq);
		ps.setObject(2, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			orderNo = rs.getString("ORDER_NO");
		}
		rs.close();
		ps.close();
		return orderNo;
	}

	/**
	 * 修改已组炉量(自动拆分铸造订单)
	 * 
	 * @param con
	 * @param entity
	 * @throws SQLException
	 */
	void updateOrderCastsData(Connection con, PlnOrderCastSEntity entity)
			throws SQLException {
		//
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMakingPlanChange.updateOrderCastsData");
		sql = EntityTool.fillSqlByEntity(sql, entity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 设定炉信息
	 * 
	 * @param castEntity
	 *            订单实体类
	 * @param castno
	 *            浇次号
	 * @param castSeq
	 *            浇次内顺序号
	 * @param furnaceno
	 *            　炉号
	 * @param
	 * @param wgt
	 *            　炉重量
	 * @param
	 * @param
	 * @return
	 */
	static PlnSteelforfurnaceEntity generationFurnace(
			PlnOrderCastSEntity castEntity, String castno, String castSeq,
			String furnaceno, String standSteel, String wgt, String num,
			String surplusFlag, String username, String pathGy) {
		PlnSteelforfurnaceEntity entity = new PlnSteelforfurnaceEntity();
		entity.setFurnaceno(furnaceno);
		entity.setCastStoveSeq(castSeq);
		entity.setStatus("00");
		entity.setCastingtype(castEntity.getCastingtype());
		entity.setEfPlineCode(castEntity.getPlineCodeA());
		entity.setEfPlineName(castEntity.getPlineNameA());
		entity.setEfAge("");
		entity.setPlineCode(castEntity.getPlineCode());
		entity.setPlineName(castEntity.getPlineName());
		entity.setCcmid(castEntity.getPlineName());
		entity.setWlidIn("A");
		entity.setWldescIn("电炉投入方案");
		entity.setMaterialNo(castEntity.getWlidIn());
		entity.setMaterialName(castEntity.getWldescIn());
		entity.setGradecode(castEntity.getGradecode());
		entity.setGradename(castEntity.getGradename());
		entity.setCic(castEntity.getCic());
		entity.setQuantity(num);
		entity.setWeight(wgt);
		entity.setAimSteelQty(standSteel);
		entity.setPlanRoute(pathGy);
		entity.setSurplusflag(surplusFlag);
		entity.setIfMonitorLg(castEntity.getIfMonitorLg());
		entity.setPrdcrNo(castEntity.getPrdcrNo());
		entity.setPlanremark("");
		entity.setRemark("");
		entity.setMakeman(username);
		entity.setCraftNo(castEntity.getCraftNo());
		entity.setIfConfirm(castEntity.getIfConfirm());
		return entity;
	}

	/**
	 * 设定炉内订单信息
	 * 
	 * @param
	 * @param
	 * @param
	 * @param
	 * @param
	 * @param num
	 *            　炉支数
	 * @param
	 * @return
	 * @throws SQLException
	 */
	static PlnSteelforOrdEntity generationSteelforOrd(
			PlnOrderCastSEntity castEntity, String castno, String castSeq,
			String furnaceno, String furnaceSeq, String wgt, String num,
			String surplusFlag, String surplusType, String username,
			double singleWeight, Connection con, int ccmSeq)
			throws IllegalAccessException, InvocationTargetException,
			SQLException {

		PlnSteelforOrdEntity entity = new PlnSteelforOrdEntity();
		BeanUtils.copyProperties(entity, castEntity);
		entity.setFurnaceno(furnaceno);
		entity.setSequenceno(furnaceSeq);
		entity.setCutNum(furnaceSeq);
		entity.setCastStoveSeq(castSeq);
		entity.setCcmSeq(String.valueOf(ccmSeq));
		entity.setStatus("00");// 状态
		entity.setGptype(castEntity.getGptype());
		entity.setCastingtype(castEntity.getCastingtype());
		entity.setPlineCode(castEntity.getPlineCode());
		entity.setPlineName(castEntity.getPlineName());
		entity.setCcmid(castEntity.getPlineName());
		entity.setFlowno("");
		entity.setEfPlineCode(castEntity.getPlineCodeA());
		entity.setEfPlineName(castEntity.getPlineNameA());
		entity.setWldescIn(castEntity.getWldescIn());
		entity.setWlidIn(castEntity.getWlidIn());
		entity.setMaterialNo(castEntity.getMaterialNo());
		entity.setMaterialName(castEntity.getMaterialName());
		entity.setGradecode(castEntity.getGradecode());
		entity.setGradename(castEntity.getGradename());
		entity.setDiameter(castEntity.getDiameterLz());// 管坯直径
		entity.setLenGpSingle(castEntity.getLenGpSingle());
		entity.setCutNumGp(castEntity.getCutNumGp());
		entity.setLengthM(castEntity.getLengthLz());
		entity.setWeightGpOne(String.valueOf(singleWeight));
		//2020.6.9修改
		if(castEntity.getProPlanId().contains("GP"))
		{
			entity.setWeightM(castEntity.getWtCastOne());
		}else
		{
			entity.setWeightM(String.valueOf(Double.parseDouble(castEntity.getWtCastOne()) * Integer.parseInt(castEntity.getCutNumGp())));
		}

		entity.setTotMumM(num);// 支数
		entity.setTotWeightM(wgt);// 重量
		entity.setCic(castEntity.getCic());
		if (castEntity.getGptype().equals("1")) {
			entity.setPic("");
		} else {
			entity.setPic(castEntity.getPic());
		}
		entity.setSic(castEntity.getSic());
		entity.setDic(castEntity.getDic());
		entity.setSurplusflag(surplusFlag);
		entity.setSurplusType(surplusType);
		entity.setMakeman(username);
		entity.setMemo("");
		entity.setIfSurplusAlloted("");
		if (!surplusType.equals("")) {
			if (surplusType.equals("1")) {
				entity.setProPlanId(castEntity.getProPlanId());
				entity.setGxPlanNo(castEntity.getGxPlanNo());
				// String order = getBlOrder(con, castEntity.getPlineCode());
				entity.setOrderNo("");
				entity.setFinalUserDesc("备料");
				entity.setSteelcode("");
				entity.setSteelname("");
				entity.setProduccode("");
				entity.setProducname("");
				entity.setGowhereCode("");
				entity.setGowhereName("离线库");
				entity.setIfMonitorLg("/");
				entity.setPrdcrNo("");
			} else {
				entity.setProPlanId("");
				entity.setGxPlanNo("");
				entity.setOrderNo("");
				entity.setFinalUserDesc("");
				entity.setSteelcode("");
				entity.setSteelname("");
				entity.setProduccode("");
				entity.setProducname("");
				entity.setGowhereCode("");
				entity.setGowhereName("");
				entity.setIfMonitorLg("/");
				entity.setPrdcrNo("");
			}
		} else {
			String[] pram = getOrderUser(con, castEntity);
			entity.setProPlanId(castEntity.getProPlanId());
			entity.setGxPlanNo(castEntity.getGxPlanNo());
			entity.setOrderNo(pram[0]);
			entity.setFinalUserDesc(pram[1]);
			entity.setSteelcode(pram[4]);
			entity.setSteelname(pram[5]);
			entity.setProduccode(pram[6]);
			entity.setProducname(pram[7]);
			if (castEntity.getCastingtype().equals("2")) {
				entity.setGowhereCode("");
				entity.setGowhereName("快锻");
			} else {
				entity.setGowhereCode(pram[2]);
				entity.setGowhereName(pram[3]);
			}
			if (castEntity.getIfMonitorLg().equals("1")) {
				entity.setIfMonitorLg("是");
			} else {
				entity.setIfMonitorLg("/");
			}

			entity.setPrdcrNo(castEntity.getPrdcrNo());
			entity.setPdeptName(pram[8]);
			entity.setPrdcrMan(pram[9]);
		}
		entity.setIsendgx("0");
		return entity;
	}

	/**
	 * 生成炉
	 * 
	 * @param castList
	 *            订单集合
	 * @param flow
	 *            　流数
	 * @param castMode
	 *            　中包方式
	 * @param
	 * @param
	 * @param
	 * @param
	 * @param
	 * @param
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SQLException
	 */
	ReturnEntity generationFurnace(Connection con,
			List<PlnOrderCastSEntity> castList, double flow, String castMode,
			double zbLeftWgt, String castno, String castSeq, String furnaceno,
			int maxCastNum, String username, double maxSteel, double minSteel,
			double standSteel, int ccmSeq, double wtYlCount, double tailLoss,
			double smeltingWt, String luNumFalg, double smeltingWt1,
			double wtCast) throws IllegalAccessException,
			InvocationTargetException, SQLException {
		ReturnEntity returnEntity = new ReturnEntity();
		boolean lastFlag = false;
		PlnOrderCastSEntity beginEntity = null;
		// 是否有余材(0没有)
		String surplusFlag = "0";
		// 炉内顺序号
		int furnaceSeq = 1;
		// 首炉加量(不是首炉，这个量为0，不影响后面计算)
		double beginAddWgt = 0;
		// 尾炉加量(首炉有开始钢种与首炉加量)
		double endAddWgt = 0;
		// 开始钢种
		String beignGradename = "";
		String endGradename = "";
		int beginAddNum = 0;
		double fristStandSteel = 0;

		if (castSeq.equals("1")) {
			/**
			 * 1 生成首炉加量的炉内计划
			 */
			beginEntity = castList.get(0);
			beignGradename = beginEntity.getGradename();
			double cutLenBegin = 0;
			if (beginEntity.getCastingtype().equals("1")) {
				// 通过直径查询对应切头(待处理)
				double[] cutParams = FrmSteelMakingPlanChange
						.getBaseLzSpecParams(con, beginEntity.getDiameterLz(),
								castMode);
				if (cutParams == null) {
					returnEntity.setMessage("直径(" + beginEntity.getDiameterLz()
							+ ")没有维护连铸规格切头尾数据!");
					return returnEntity;
				}
				cutLenBegin = cutParams[0];

				// 管坯直径
				double dimater = Double
						.parseDouble(beginEntity.getDiameterLz());
				// 计算米单重
				double mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
				// 单倍坯重
				double singleWeight = mSingleWeight
						* (Double.parseDouble(beginEntity.getLenGpSingle()) / 1000);
				// 倍尺坯重
				double wtCastOne = mSingleWeight
						* (Double.parseDouble(beginEntity.getLengthLz()) / 1000);
				// 首炉加量
				beginAddWgt = new BigDecimal(mSingleWeight * flow * cutLenBegin)
						.setScale(3, RoundingMode.HALF_UP).doubleValue();
				beginAddNum = (int) Math.ceil(beginAddWgt
						/ Double.parseDouble(beginEntity.getPlanWtS())
						* Integer.parseInt(beginEntity.getPlanNumS()));
				beginAddWgt = beginAddNum * wtCastOne;
				furnaceSeq = 0;
				// 首炉加量生成炉次订单
				PlnSteelforOrdEntity ordEntity = FrmSteelMakingPlanChange
						.generationSteelforOrd(beginEntity, castno, castSeq,
								furnaceno, String.valueOf(furnaceSeq), String
										.valueOf(beginAddWgt), String
										.valueOf(beginAddNum), "1", "2",
								username, singleWeight, con, ccmSeq);
				//ordEntity.setWeightM(String.valueOf(wtCastOne));
				returnEntity.getOrdEntityList().add(ordEntity);
				furnaceSeq++;
				ccmSeq++;// 连铸顺序号
				surplusFlag = "1";
			}
		}
		// 炉可组炉重量
		double sumPlanWgt = beginAddWgt;
		// 炉可组炉支数
		int sumPlanNum = beginAddNum;
		// 已排产重量
		double sumplanedWgt = 0;
		int sumplanedNum = 0;
		// String cic = "";
		// double smeltingWt1 = 0;// 总的待冶炼量
		String pathGy = "";// 工艺路径
		double castWt = 0;

		for (int i = 0; i < castList.size(); i++) {
			PlnOrderCastSEntity entity = castList.get(i);
			pathGy = entity.getStationDesc();

			// 已组炉量
			double planedWgt = 0;
			if (!entity.getPlanedWt().equals("")) {
				planedWgt = Double.parseDouble(entity.getPlanedWt());
			}
			// 已组炉支数
			int planedNum = 0;
			if (!entity.getPlanedNum().equals("")) {
				planedNum = Integer.parseInt(entity.getPlanedNum());
			}
			// 利库量
			double planWtSold = Double.parseDouble(entity.getPlanWtSold());
			int planNumSold = Integer.parseInt(entity.getPlanNumSold());
			// 排产量
			double planWgt = Double.parseDouble(entity.getPlanWtS())
					- planWtSold;
			// 排产支数
			int planNum = Integer.parseInt(entity.getPlanNumS()) - planNumSold;
			// 可组炉
			sumplanedWgt = planWgt - planedWgt;
			sumplanedNum = planNum - planedNum;
			// if(wtYlCount-sumplanedWgt<0.000002){
			// sumplanedWgt=wtYlCount;
			// }
			// smeltingWt1 += sumplanedWgt;
			// 如果该订单排产量-已组炉等于0，则循环订单
			if (sumplanedWgt <= 0.00002) {
				continue;
			}
			// 管坯直径
			double dimater = 0;
			double mSingleWeight = 0;
			double singleWeight = 0;
			double cutLenEnd = 0;
			double wtCastOne = 0;
			if (entity.getCastingtype().equals("1")) {
				dimater = Double.parseDouble(entity.getDiameterLz());
				// 计算米单重
				mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
				// 单倍坯重
				singleWeight = mSingleWeight
						* (Double.parseDouble(entity.getLenGpSingle()) / 1000);
				// 倍尺坯重
				wtCastOne = mSingleWeight
						* (Double.parseDouble(entity.getLengthLz()) / 1000);
				double[] cutParams = FrmSteelMakingPlanChange
						.getBaseLzSpecParams(con, entity.getDiameterLz(),
								castMode);
				if (cutParams == null) {
					returnEntity.setMessage("直径(" + entity.getDiameterLz()
							+ ")没有维护连铸规格切头尾数据!");
					return returnEntity;
				}
				cutLenEnd = cutParams[1];
				/**
				 * 查询对应直径的切尾
				 */

				if (cutLenEnd <= 0) {
					String message = "[换中包]";
					if (castMode.equals("0")) {
						message = "[不换中包]";
					}
					returnEntity.setMessage("直径(" + entity.getDiameterLz()
							+ ")连铸规格切头尾为0!" + message);
					return returnEntity;
				}
			} else {
				wtCastOne = Double.parseDouble(entity.getWtCastOne());
				String sqldx = CoreXmlSqlParsersFactory
						.getSql("FrmSteelMakingPlanChange.getMscCastRForg");
				PreparedStatement ps1 = con.prepareStatement(sqldx);
				ps1.setObject(1, entity.getDiameterLz());
				ResultSet rs1 = ps1.executeQuery();
				if (rs1.next()) {
					singleWeight = rs1.getDouble("ACTUAL_WEIGHT");
				}
				rs1.close();
				ps1.close();
				if (singleWeight == 0) {
					returnEntity.setMessage("模铸订单(" + entity.getDiameterLz()
							+ ")未取到实重!");
					return returnEntity;
				}
				// 查询模铸出钢上下限
				// double[] steelArrMz = getMoildOutGrad(con, entity
				// .getDiameterLz(), entity.getGradename());
				// minSteel = steelArrMz[0];
				// maxSteel = steelArrMz[1];
				// if (wtYlCount < minSteel) {
				// wtYlCount = minSteel;
				// }
				// if (wtYlCount > maxSteel) {
				// wtYlCount = maxSteel;
				// }
			}
			// 如果不是首炉，则这个订单为炉的开始订单
			if (beginEntity == null) {
				beginEntity = castList.get(i);
			}
			if (fristStandSteel <= 0) {
				fristStandSteel = standSteel;
			}
			/**
			 * 1.当前标准出钢量>可组炉总量
			 */
			double wt = 0;
			if (luNumFalg.equals("0")) {
				if (maxCastNum == 1) {
					wt = wtYlCount + beginAddWgt + tailLoss;
				} else {
					// 第一炉
					if (Integer.parseInt(castSeq) == 1) {
						// 如果可组炉量>没炉冶炼吨
						// if (sumplanedWgt > wtYlCount) {
						// sumplanedWgt = wtYlCount;
						// }
						wt = wtYlCount + beginAddWgt;
					}
					// 最后一炉
					else if (smeltingWt1 != 0
							&& smeltingWt - smeltingWt1 - wtYlCount < 0.00002) {
						// 如果可组炉量>没炉冶炼吨
						// if (sumplanedWgt > wtYlCount) {
						// sumplanedWgt = wtYlCount;
						// }
						wt = tailLoss + wtYlCount;
					} else {
						wt = wtYlCount;
					}
				}
			} else {
				if (maxCastNum == 1) {
					wt = wtYlCount + beginAddWgt + tailLoss;
				} else {
					// 第一炉
					if (Integer.parseInt(castSeq) == 1) {
						// 如果可组炉量>没炉冶炼吨
						// if (sumplanedWgt > wtYlCount) {
						// sumplanedWgt = wtYlCount;
						// }
						wt = wtYlCount + beginAddWgt;
					} // 最后一炉
					else if (Integer.parseInt(castSeq) == maxCastNum) {
						// 如果可组炉量>没炉冶炼吨
						// if (sumplanedWgt > wtYlCount) {
						// sumplanedWgt = wtYlCount;
						// }
						wt = tailLoss + wtYlCount;
					} else {
						wt = wtYlCount;
					}
				}
			}
			// if (entity.getCastingtype().equals("1")) {
			// if (sumplanedWgt >= 90 && sumplanedWgt <= 110) {
			// wt = sumPlanWgt + sumplanedWgt;
			// }
			// // else {
			// // wt = standSteel;
			// // }
			// }
			// else {
			// wt = standSteel;
			// }

			if (wt > sumPlanWgt + sumplanedWgt) {
				PlnSteelforOrdEntity ordEntity = FrmSteelMakingPlanChange
						.generationSteelforOrd(entity, castno, castSeq,
								furnaceno, String.valueOf(furnaceSeq), String
										.valueOf(sumplanedWgt), String
										.valueOf(sumplanedNum), "0", "",
								username, singleWeight, con, ccmSeq);
				castWt += sumplanedWgt;
				furnaceSeq++;
				ccmSeq++;
				/**
				 * 1.1 可以找到下一个订单，循环不到下一个订单说明已经所有订单已经执行完
				 */
				if (i + 1 < castList.size()) {
					sumPlanWgt = sumPlanWgt + sumplanedWgt;
					sumPlanNum = sumPlanNum + sumplanedNum;
					// 回填已组炉量
					entity
							.setPlanedWt(String.valueOf(sumplanedWgt
									+ planedWgt));
					entity.setPlanedNum(String
							.valueOf(planedNum + sumplanedNum));
					PlnOrderCastSEntity entity1 = castList.get(i + 1);
					double wtCastOne1 = 0;
					if (entity1.getCastingtype().equals("1")) {
						wtCastOne1 = mSingleWeight
								* (Double.parseDouble(entity1.getLengthLz()) / 1000);
					} else {
						wtCastOne1 = Double.parseDouble(entity1.getWtCastOne());
					}
					// 判定工艺路径
					if (!pathGy.contains(entity1.getStationDesc())) {
						pathGy = entity1.getStationDesc();
					}
					// 判断两个索引码是否存在交集
					// if (!entity.getCic().equals(entity1.getCic())) {
					// cic = queryPlnCastCicAll(con, entity.getCic() + ","
					// + entity1.getCic());
					// }
					// else {
					// cic = entity1.getCic();
					// }
					// 没有交集则不能组在一炉(则不能组在同一炉里)
					if (!entity.getCic().equals(entity1.getCic())
							|| !entity1.getGradecode().equals(
									entity.getGradecode())) {
						returnEntity.getOrdEntityList().add(ordEntity);
						// 如果出钢量下限-前面可组炉总量<0,则用标准出钢量
						double noPlanWgt = minSteel - sumPlanWgt;
						if (noPlanWgt <= 0) {
							noPlanWgt = wt - sumPlanWgt;
						}
						int noPlanNum = (int) Math.ceil(noPlanWgt / planWgt
								* planNum);
						noPlanWgt = noPlanNum * wtCastOne1;
						// if ((wtCast + sumPlanWgt + noPlanWgt) - 2 * wt >
						// 0.00002) {
						// noPlanNum = noPlanNum - 1;
						// noPlanWgt = noPlanNum
						// * Double
						// .parseDouble(entity1.getWtCastOne());
						// }
						if (Integer.parseInt(castSeq) % 2 == 0) {
							noPlanNum = noPlanNum - 1;
							noPlanWgt = noPlanNum * wtCastOne1;
						}
						if (noPlanWgt > 0) {
							// 则形成非计划加量
							PlnSteelforOrdEntity noPlanEntity = FrmSteelMakingPlanChange
									.generationSteelforOrd(entity, castno,
											castSeq, furnaceno, String
													.valueOf(furnaceSeq),
											String.valueOf(noPlanWgt), String
													.valueOf(noPlanNum), "1",
											"1", username, singleWeight, con,
											ccmSeq);
							sumPlanWgt = sumPlanWgt + noPlanWgt;
							sumPlanNum = sumPlanNum + noPlanNum;
							returnEntity.getOrdEntityList().add(noPlanEntity);
							surplusFlag = "1";
							furnaceSeq++;
							ccmSeq++;
						}
						// 跳出循环
						break;
					} else {
						// 不做处理
						returnEntity.getOrdEntityList().add(ordEntity);
						continue;
					}
				} else {
					/**
					 * 1.2 已经到最后一个订单(即尾炉加量)并且 标准出钢量-排产总量不满足一炉
					 */
					sumPlanWgt = sumPlanWgt + sumplanedWgt;
					sumPlanNum = sumPlanNum + sumplanedNum;
					lastFlag = true;
					// 计算尾炉加量
					double addEnd = 0;
					// 尾炉加量支数
					int addEndNum = 0;
					if (entity.getCastingtype().equals("1")) {// 连铸
						// 计算尾炉加量
						addEnd = new BigDecimal(FrmSteelMakingPlanChange
								.CalculateEndAdd(cutLenEnd, Double
										.parseDouble(entity.getDiameterLz()),
										flow, zbLeftWgt)).setScale(3,
								RoundingMode.HALF_UP).doubleValue();
						addEndNum = (int) Math.ceil(addEnd / planWgt * planNum);
					}
					sumPlanWgt = new BigDecimal(sumPlanWgt + addEnd).setScale(
							3, RoundingMode.HALF_UP).doubleValue();
					sumPlanNum = sumPlanNum + addEndNum;
					// 存在以下几种情况　１.尾炉加量加前面的重量不够一炉(小于出钢量下限)／２.够一炉

					if (sumPlanWgt - addEnd < minSteel) {
						// 如果小于出钢量下限，则按出钢量下限计算

						// 非计划加量
						double noPlanWgt = minSteel - sumPlanWgt + addEnd;
						int noPlanNum = (int) Math
								.ceil((minSteel - sumPlanWgt + addEnd)
										/ planWgt * planNum);
						noPlanWgt = noPlanNum * wtCastOne;

						if (Integer.parseInt(castSeq) % 2 == 0) {
							noPlanNum = noPlanNum - 1;
							noPlanWgt = noPlanNum * wtCastOne;
						}
						// if (entity.getCastingtype().equals("1")) {
						if (noPlanWgt > 0) {
							PlnSteelforOrdEntity noPlanEntity = FrmSteelMakingPlanChange
									.generationSteelforOrd(entity, castno,
											castSeq, furnaceno, String
													.valueOf(furnaceSeq),
											String.valueOf(noPlanWgt), String
													.valueOf(noPlanNum), "1",
											"1", username, singleWeight, con,
											ccmSeq);
							returnEntity.getOrdEntityList().add(noPlanEntity);
							sumPlanWgt = sumPlanWgt + noPlanWgt;
							sumPlanNum = sumPlanNum + noPlanNum;
							furnaceSeq++;
							ccmSeq++;
						}
						// }
						// else {
						// // 模铸修改量，不产生余材
						// PlnSteelforOrdEntity noPlanEntity =
						// FrmSteelMakingPlanChange
						// .generationSteelforOrd(entity, castno,
						// castSeq, furnaceno, String
						// .valueOf(furnaceSeq-1),
						// String.valueOf(noPlanWgt), String
						// .valueOf(noPlanNum), "0",
						// "", username, singleWeight, con,
						// ccmSeq-1);
						// returnEntity.getOrdEntityList().add(noPlanEntity);
						// sumPlanWgt = sumPlanWgt + noPlanWgt;
						// sumPlanNum = sumPlanNum + noPlanNum;
						// furnaceSeq++;
						// ccmSeq++;
						// }
					} else {
						// 如果总量小于标准出钢量，并且大于最小出钢量,则还需要生成非计划加量炉次订单
						// if (sumPlanWgt < wt) {
						// double noPlanWgt = wt - sumPlanWgt;
						// int noPlanNum = (int) Math
						// .ceil((wt - sumPlanWgt) / planWgt
						// * planNum);
						// PlnSteelforOrdEntity noPlanEntity =
						// FrmSteelMakingPlanChange
						// .generationSteelforOrd(entity, castno,
						// castSeq, furnaceno, String
						// .valueOf(furnaceSeq),
						// String.valueOf(noPlanWgt), String
						// .valueOf(noPlanNum), "1",
						// "1", username, singleWeight, con);
						// returnEntity.getOrdEntityList().add(noPlanEntity);
						// sumPlanWgt = sumPlanWgt + noPlanWgt;
						// sumPlanNum = sumPlanNum + noPlanNum;
						// furnaceSeq++;
						// }
						// else {
						// // 则不需要生成非计划加量,并且前炉次订单要减量
						// double canWgt = wt - sumPlanWgt;
						// int canNum = (int) Math.ceil(canWgt / planWgt
						// * planNum);
						// ordEntity.setTotMumM(String.valueOf(planNum
						// + canNum));// 支数
						// ordEntity.setTotWeightM(String.valueOf(planWgt
						// + canWgt));// 重量
						// sumPlanWgt = sumPlanWgt + canWgt;
						// sumPlanNum = sumPlanNum + canNum;
						// planWgt = planWgt + canWgt;
						// planNum = planNum + canNum;
						// }
					}

					if (entity.getCastingtype().equals("1")) {
						// 生成尾炉加量炉次订单
						furnaceSeq = 99;
						PlnSteelforOrdEntity lastaddEntity = FrmSteelMakingPlanChange
								.generationSteelforOrd(entity, castno, castSeq,
										furnaceno, String.valueOf(furnaceSeq),
										String.valueOf(addEnd), String
												.valueOf(addEndNum), "1", "3",
										username, singleWeight, con, ccmSeq);
						furnaceSeq++;
						ccmSeq++;
						surplusFlag = "1";
						returnEntity.getOrdEntityList().add(lastaddEntity);

						// 返回值(结束钢种与尾炉加量,用于浇次使用)
						endAddWgt = addEnd;

					}
					endGradename = entity.getGradename();
					// 把已组炉量存回此订单
					entity.setPlanedWt(String.valueOf(planWgt));
					entity.setPlanedNum(String.valueOf(planNum));
					returnEntity.getOrdEntityList().add(ordEntity);
					break;
				}
			} else {
				/**
				 * 2.组炉量填充(.当前标准出钢量<可组炉总量,可组炉量有剩余)
				 */
				// 最后一炉(//计算尾炉加量)如果是尾炉，则要加尾炉加量炉次订单
				double addEnd = 0;
				int addEndNum = 0;
				String castS = "0";
				if (luNumFalg.equals("1")) {
 					if (Integer.parseInt(castSeq) == maxCastNum) {
						castS = "1";
					}
				} else {
					if (smeltingWt1 != 0
							&& smeltingWt - smeltingWt1 - wtYlCount < 0.0000002) {
						castS = "1";
					}
				}
				if (castS.equals("1") && entity.getCastingtype().equals("1")) {
					addEnd = FrmSteelMakingPlanChange.CalculateEndAdd(
							cutLenEnd, Double.parseDouble(entity
									.getDiameterLz()), flow, zbLeftWgt);
					addEndNum = (int) Math.ceil(addEnd / planWgt * planNum);
					addEnd = addEndNum * wtCastOne;
					// wt = wt + addEnd;
				}
				// if(wt-wt)
				// 生成炉次订单
				double canWgt = wt - addEnd - sumPlanWgt;
				int canNum = (int) Math.ceil((canWgt) / wtCastOne);
				canWgt = canNum * wtCastOne;
				if (sumplanedWgt - canWgt < 0.000002
						|| sumplanedNum - canNum <= 0) {
					canWgt = sumplanedWgt;
					canNum = sumplanedNum;
				}
				// do {
				// if ((wtCast + addEnd + sumPlanWgt + canWgt) - 2 * wt >
				// 0.00002) {
				// canNum = canNum - 1;
				// canWgt = canNum
				// * Double.parseDouble(entity.getWtCastOne());
				// if (sumplanedWgt - canWgt < 0.000002) {
				// canWgt = sumplanedWgt;
				// }
				// }
				// }
				// while ((wtCast + addEnd + sumPlanWgt + canWgt) - 2 * wt >
				// 0.00002);

				if (Integer.parseInt(castSeq) % 2 == 0) {
					canNum = canNum - 1;
					canWgt = canNum * wtCastOne;
					if (sumplanedWgt - canWgt < 0.000002
							|| sumplanedNum - canNum <= 0) {
						canWgt = sumplanedWgt;
						canNum = sumplanedNum;
					}
				}
				// if (luNumFalg.equals("1")) {
				// if (Integer.parseInt(castSeq) != maxCastNum) {
				// if (Integer.parseInt(castSeq) % 2 == 0) {
				// do {
				// if (Integer.parseInt(castSeq) * wtYlCount
				// - (smeltingWt1 + canWgt) < 0.0000002) {
				// canNum = canNum - 1;
				// canWgt = canNum
				// * Double.parseDouble(entity
				// .getWtCastOne());
				// }
				//
				// }
				// while (Integer.parseInt(castSeq) * wtYlCount
				// - (smeltingWt1 + canWgt) < 0.0000002);
				// }
				// }
				// }else{
				// if (smeltingWt - smeltingWt1 - wtYlCount >=1) {
				// if (Integer.parseInt(castSeq) % 2 == 0) {
				// do {
				// if (Integer.parseInt(castSeq) * wtYlCount
				// - (smeltingWt1 + canWgt) < 0.0000002) {
				// canNum = canNum - 1;
				// canWgt = canNum
				// * Double.parseDouble(entity
				// .getWtCastOne());
				// }
				//
				// }
				// while (Integer.parseInt(castSeq) * wtYlCount
				// - (smeltingWt1 + canWgt) < 0.0000002);
				// }
				// }else{
				//
				// }
				// }
				if (canWgt > 0) {
					PlnSteelforOrdEntity ordEntity = FrmSteelMakingPlanChange
							.generationSteelforOrd(entity, castno, castSeq,
									furnaceno, String.valueOf(furnaceSeq),
									String.valueOf(canWgt), String
											.valueOf(canNum), "0", "",
									username, singleWeight, con, ccmSeq);
					castWt += canWgt;
					returnEntity.getOrdEntityList().add(ordEntity);

					// 炉内顺序加1
					furnaceSeq++;
					ccmSeq++;
					// 回填订单的已组炉重量
					entity.setPlanedWt(String.valueOf(canWgt + planedWgt));
					entity.setPlanedNum(String.valueOf(canNum + planedNum));
					//
					sumPlanWgt = sumPlanWgt + canWgt;
					sumPlanNum = sumPlanNum + canNum;
				}
				//7if(planWgt-canWgt-planedWgt==0){
				//	addEnd = FrmSteelMakingPlanChange
				//			.CalculateEndAdd(cutLenEnd, Double
				//					.parseDouble(entity
				//							.getDiameterLz()),
				//					flow, zbLeftWgt);
				//	addEndNum = (int) Math.ceil(addEnd / planWgt
				//			* planNum);
				//	addEnd = addEndNum * wtCastOne;
				// = true;
				//}
				if (luNumFalg.equals("0")) {
					if (maxCastNum > 1) {
						if (smeltingWt1 != 0
								&& smeltingWt - smeltingWt1 - castWt < 0.0000002) {
							lastFlag = true;
							if (entity.getCastingtype().equals("1")) {
								addEnd = FrmSteelMakingPlanChange
										.CalculateEndAdd(cutLenEnd, Double
												.parseDouble(entity
														.getDiameterLz()),
												flow, zbLeftWgt);
								addEndNum = (int) Math.ceil(addEnd / planWgt
										* planNum);
								addEnd = addEndNum * wtCastOne;
							}
						}
					} else {
						lastFlag = true;
					}
				}
				//
				// 生成尾炉加量炉次订单
				if (addEnd > 0) {
					furnaceSeq = 99;
					PlnSteelforOrdEntity lastaddEntity = FrmSteelMakingPlanChange
							.generationSteelforOrd(entity, castno, castSeq,
									furnaceno, String.valueOf(furnaceSeq),
									String.valueOf(addEnd), String
											.valueOf(addEndNum), "1", "3",
									username, singleWeight, con, ccmSeq);
					returnEntity.getOrdEntityList().add(lastaddEntity);
					sumPlanWgt = sumPlanWgt + addEnd;
					sumPlanNum = sumPlanNum + addEndNum;
					surplusFlag = "1";
					// 返回值(结束钢种与尾炉加量,用于浇次使用)
					endAddWgt = addEnd;
					furnaceSeq++;
					ccmSeq++;
				}
				endGradename = entity.getGradename();
				break;
			}
		}

		String[] batc = getBatchingCode(con, beginEntity.getPlineCodeA());
		if (batc == null) {
			returnEntity.setMessage("电炉(" + beginEntity.getPlineNameA()
					+ ")没有配料方案,请维护基础数据!");
			return returnEntity;
		}
		// if (!cic.equals("")) {
		// beginEntity.setCic(cic);
		// }
		// 生成炉信息
		PlnSteelforfurnaceEntity furnaceEntity = FrmSteelMakingPlanChange
				.generationFurnace(beginEntity, castno, castSeq, furnaceno,
						String.valueOf(standSteel), String.valueOf(sumPlanWgt),
						String.valueOf(sumPlanNum), surplusFlag, username,
						pathGy);
		returnEntity.setBeginAddWgt(beginAddWgt);
		returnEntity.setEndAddWgt(endAddWgt);
		returnEntity.setBeginGradename(beignGradename);
		returnEntity.setEndGradename(endGradename);
		returnEntity.setFlag(lastFlag);
		returnEntity.setFurEntity(furnaceEntity);
		returnEntity.setCcmSeq(ccmSeq);
		returnEntity.setCastWt(castWt);
		return returnEntity;
	}

	/**
	 * 组浇
	 * 
	 * @param jsonList
	 *            json字符串集合
	 * 
	 * @param params
	 *            参数数组
	 * @param username
	 *            操作人
	 * @param flag
	 *            标识(0 预算 1组浇) 预算不把数据写入数据库
	 * @return
	 */
	public CoreReturnObject CastGoup(ArrayList<String> jsonList,
			String[] params, String username, String flag, String strCount,
			String countNotMan, String luNumFalg) {
		CoreReturnObject core = new CoreReturnObject();
		core.setResult(false);
		// 连铸机号
		String plineCode = params[0];
		String plineName = params[1];
		int flow = Integer.parseInt(params[2]); // 流数
		double zbLeftWgt = Double.parseDouble(params[3]); // 中包铸余
		int maxCastNum = Integer.parseInt(params[4]); // 最大炉数
		String waterGroupCode = params[5]; // 连浇群组号
		String castMode = params[6];// 换包方式 1换中包 2不换中包
		String memo = params[7]; // 备注
		double steelMax = Double.parseDouble(params[8]);// 出钢量上限
		double steelMin = Double.parseDouble(params[9]);// 出钢量下限
		String memoNum = params[10];// 自定义排序
		// SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Connection con = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			// 获取数据库当前时间
			String dateTime = getSystemTime(con);
			FrmSteelMakingPlanChange frmSteelMakingPlanChange = new FrmSteelMakingPlanChange();
			// json转化为实体类
			List<PlnOrderCastSEntity> castList = SteelHelper.jsonsToEntityList(
					PlnOrderCastSEntity.class, jsonList);
			// Collections.sort(castList);
			// 实时数据
			List<PlnOrderCastSEntity> castList1 = new ArrayList<PlnOrderCastSEntity>();
			// 验证各订单数据
			double ylWgtCount = 0;// 选定订单冶炼吨合计
			String diameterLz = "";
			String castingtype = "";
			String plineCodeB = "";
			String castype = "";
			String dimer = "";
			String plineCodeA = "";
			String plineNameA = "";
			String wldescIn = "";
			String materialNo = "";
			// String cic = "";
			// String firstPlanId = "";
			// String lastPlanId = "";

			for (PlnOrderCastSEntity entity : castList) {
				PlnOrderCastSEntity entity1 = ControlOrderPlan
						.getOrderCastById(con, entity.getProPlanId(), entity
								.getGxPlanNo());
				if (entity1 == null) {
					core.setV_errMsg("数据异常,请刷新数据!");
					return core;
				}
				if (Double.parseDouble(entity1.getCanWgt()) <= 0) {
					core.setV_errMsg("可组炉重量为0,请刷新数据!");
					return core;
				}
				if (Integer.parseInt(entity1.getExeStatus()) > 1) {
					core.setV_errMsg("铸造订单(" + entity1.getProPlanId() + "/"
							+ entity1.getGxPlanNo() + "),不是计划和生产中的状态!");
					return core;
				}
				wldescIn = entity1.getWlidIn();
				materialNo = entity1.getMaterialNo();
				if (plineCodeB.equals("")) {
					plineCodeB = entity1.getPlineCode();
				}
				if (!plineCodeB.equals(entity1.getPlineCode())) {
					core.setV_errMsg("请选择同一铸造产线!");
					return core;
				}
				if (plineCodeA.equals("")) {
					plineCodeA = entity1.getPlineCodeA();
				}
				if (!plineCodeA.equals(entity1.getPlineCodeA())) {
					core.setV_errMsg("请选择同一电炉产线!");
					return core;
				}
				plineNameA = entity1.getPlineNameA();
				if (castype.equals("")) {
					castype = entity.getCastingtype();
				}
				if (!castype.equals(entity.getCastingtype())) {
					core.setV_errMsg("请选择同一铸造类型!");
					return core;
				}
				if (dimer.equals("")) {
					dimer = entity.getDiameterLz();
				}
				if (!dimer.equals(entity.getDiameterLz())) {
					core.setV_errMsg("请选择同一连铸直径!");
					return core;
				}
				ylWgtCount += Double.parseDouble(entity1.getCanWgt());
				diameterLz = entity1.getDiameterLz();
				castingtype = entity1.getCastingtype();
				entity1.setStationDesc(entity.getStationDesc());
				entity1.setManualNum(entity.getManualNum());
				// if (entity.getGradeSort().equals("1")) {
				// firstPlanId = entity.getProPlanId() + "/"
				// + entity.getGxPlanNo();
				// }
				// if (entity.getGradeSort().) {
				// lastPlanId = entity.getProPlanId() + "/" +
				// entity.getGxPlanNo();
				// }
				// 验证状态
				castList1.add(entity1);
			}
			// 获取索引码个数
			// int cicCount = Integer.parseInt(strCount);
			// 获取炉次开始时间(同电炉)
			String castStartTime = getCastTime(con, plineCodeA, dateTime);
			/**
			 * 2. 炉信息
			 */
			List<PlnSteelforfurnaceEntity> furnaceList = new ArrayList<PlnSteelforfurnaceEntity>();// 炉次集合
			List<PlnSteelforOrdEntity> steelordList = new ArrayList<PlnSteelforOrdEntity>();// 炉次订单集合
			// 1.浇次号
			SimpleDateFormat sdf = new SimpleDateFormat("yyMM");
			String nowTime = sdf.format(new Date());
			String castno = frmSteelMakingPlanChange.getMaxCastno(con, nowTime,
					plineCode);// 查询最大浇次号
			int castSeqNo = getMaxCastSeqno(con, plineCode);// 查询最大浇次号顺序号

			// 首炉加量
			double addWtBegin = 0;
			// 尾炉加量
			double addWtEnd = 0;
			// 开始钢种
			String beginGrade = "";
			// 结束钢种
			String endGrade = "";
			// 2.生成炉
			double headLoss = 0;// 头部损失
			double tailLoss = 0;// 尾部损失
			double cutLenEnd = 0;// 切头
			double cutLenBegin = 0;// 切尾
			double mSingleWeight = 0;
			// 管坯直径
			double dimater = 0;
			if (castingtype.equals("1")) {
				double[] cutParams = FrmSteelMakingPlanChange
						.getBaseLzSpecParams(con, diameterLz, castMode);
				if (cutParams == null) {
					core.setV_errMsg("直径(" + diameterLz + ")没有维护连铸规格切头尾数据!");
					return core;
				}
				cutLenEnd = cutParams[1];
				cutLenBegin = cutParams[0];
				// 查询对应直径的切尾
				if (cutLenEnd <= 0) {
					String message = "[换中包]";
					if (castMode.equals("0")) {
						message = "[不换中包]";
					}
					core.setV_errMsg("直径(" + diameterLz + ")连铸规格切头尾为0!"
							+ message);
					return core;
				}
				dimater = Double.parseDouble(diameterLz);
				// 计算米单重
				mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
				// 头部损失
				headLoss = mSingleWeight * flow * cutLenBegin;
				tailLoss = mSingleWeight * cutLenEnd * flow + zbLeftWgt;
			}
			/**
			 * 查询电炉的标准出钢量与出钢量下限
			 */
			double[] steelArr = FrmSteelMakingPlanChange.getPlineLgParams(con,
					plineCodeA);
			if (steelArr == null) {
				core.setV_errMsg("电炉(" + plineNameA + ")没有排产基础数据!");
				return core;
			}
			double standSteel = steelArr[0];// 标准出钢量
			// double minSteel = steelArr[1];// 出钢量下限
			// double maxSteel = steelMax;// 出刚量上限
			// double minSteel = steelMin;
			// if (standSteel <= 0 || minSteel <= 0) {
			// returnEntity.setMessage("电炉(" + entity.getPlineNameA()
			// + ")没有标准出钢量!");
			// return returnEntity;
			// }

			double castWtg = 0;
			// 组浇支数
			int castNum = 0;
			// 用于预算
			double noPlanWgt = 0;// 非计划加量
			double ordWgt = 0;// 订单使用量
			String plnStarttime = "";
			String mess = "";
			double averageWt = standSteel;
			double wtYlCount = 0;
			if (Integer.parseInt(countNotMan) == 1) {
				mess = "手工组浇";
				int maxNum = 0;
				for (int j = 0; j < castList1.size(); j++) {
					PlnOrderCastSEntity entity1 = castList1.get(j);
					if (maxNum < Integer.parseInt(entity1.getManualNum())) {
						maxNum = Integer.parseInt(entity1.getManualNum());
					}
				}
				// 单浇次头尾损失
				double loss = headLoss + tailLoss;
				// 平均单炉出钢量=圈定浇次排产订单冶炼量/冶炼炉数
				averageWt = new BigDecimal((ylWgtCount + loss) / maxNum)
						.setScale(3, RoundingMode.HALF_UP).doubleValue();
				// 查询同一铸造产线炉次订单顺序号
				int ccmSeq = getMaxCcmSeqno(con, plineCode);
				for (int castSeq = 1; castSeq > 0; castSeq++) {
					// String furnaceno = getMaxFurnaceNo(con, castno);//
					// 查询最大炉次号
					String furnaceno = castno + String.format("%02d", castSeq);
					ReturnEntity returnEntity = frmSteelMakingPlanChange
							.generationManualFurnace(con, castList1, flow,
									castMode, zbLeftWgt, castno, String
											.valueOf(castSeq), furnaceno,
									username, steelMax, steelMin, ccmSeq,
									maxNum, averageWt);
					ccmSeq = returnEntity.getCcmSeq();
					// 返回信息
					if (returnEntity.getMessage().length() > 0) {
						core.setV_errMsg(returnEntity.getMessage());
						return core;
					}
					furnaceList.add(returnEntity.getFurEntity());
					List<PlnSteelforOrdEntity> list = returnEntity
							.getOrdEntityList();
					for (PlnSteelforOrdEntity entity : list) {
						steelordList.add(entity);
					}
					// 首炉
					if (castSeq == 1) {
						addWtBegin = returnEntity.getBeginAddWgt();
						beginGrade = returnEntity.getBeginGradename();
					}
					// 是否最后一个订单,如果到最后一个订单则不再循环
					if (returnEntity.isFlag()) {
						addWtEnd = returnEntity.getEndAddWgt();
						endGrade = returnEntity.getEndGradename();
						break;
					}
					// 如果生成的炉号大于最大炉数
					if (castSeq >= maxNum) {
						addWtEnd = returnEntity.getEndAddWgt();
						endGrade = returnEntity.getEndGradename();
						break;
					}
				}
			} else {
				mess = "自动组浇";

				// 单浇次头尾损失
				double loss = headLoss + tailLoss;
				// if (cicCount == 1) {
				// 单浇次冶炼订单量max=最大炉数*出钢量上限-单浇次头尾损失
				double singleCastMax = maxCastNum * steelMax - loss;

				//  圈定浇次排产订单冶炼量
				double smeltingWt = 0;
				if (ylWgtCount >= singleCastMax) {
					smeltingWt = singleCastMax;
				} else {
					smeltingWt = ylWgtCount;
				}

				// 计算单浇次冶炼炉数
				double smeltingNumMin = (int)((smeltingWt+loss) / steelMax);// MIN=Int（（圈定浇次排产订单冶炼量/出钢上限）
				double smeltingNumMax = (int) Math
						.ceil((smeltingWt+loss) / steelMin); // MAX=Int（（圈定浇次排产订单冶炼量/出钢下限）
				int num = 1;
				// 获取最小整数
				int min = CoreIsRounded.isRounded(smeltingNumMin,
						smeltingNumMax);

				if (min != 0) {
					if (min * steelMax >= smeltingWt) {
						num = min;
					} else {
						num = min + 1;
					}
				}
				// 最大炉数与冶炼炉数比，冶炼炉数小于最大炉数，取冶炼炉数 反之，取最大炉数
				if (maxCastNum >= num) {
					maxCastNum = num;
				}
				// maxCastNum = 10;
				// 平均单炉出钢量=圈定浇次排产订单冶炼量/冶炼炉数
				averageWt = Math.ceil((smeltingWt + loss) / maxCastNum);

				// 每一炉冶炼吨
				wtYlCount = new BigDecimal((smeltingWt) / maxCastNum).setScale(
						3, RoundingMode.HALF_UP).doubleValue();
				// 平均出单炉出钢量与出钢量上限比
				if (averageWt > steelMax) {
					// core.setV_errMsg("平均出单炉出钢量(" + averageWt + ")大于出钢量上限!");
					// return core;
					averageWt = steelMax;
				}
				if (averageWt < steelMin) {
					averageWt = steelMin;
				}
				// 平均出单炉出钢量与出钢量下限比
				if (wtYlCount < steelMin) {
					// core.setV_errMsg("平均单炉出钢量("+averageWt+")小于出钢量下限!");
					// return core;
					wtYlCount = steelMin;
				}
				if (wtYlCount > steelMax) {
					wtYlCount = steelMax;
				}
				// 查询同一铸造产线炉次订单顺序号
				int ccmSeq = getMaxCcmSeqno(con, plineCode);
				double smeltingWt1 = 0;
				double wtCast = 0;
				int countSeq = 1;
				for (int castSeq = 1; castSeq > 0; castSeq++) {		
					String furnaceno = castno + String.format("%02d", countSeq);
					int countF = 0;
					do {
						if (countF > 0) {
							furnaceno = castno
									+ String.format("%02d", countSeq);
						}
						// 查询是否存在虚拟炉次号
						String sqlFurnNo = "SELECT COUNT(*) COUNT FROM PLN_STEELFORFURNACE T WHERE T.FURNACENO = ?";
						PreparedStatement ps = con.prepareStatement(sqlFurnNo);
						ps.setObject(1, furnaceno);
						ResultSet rs = ps.executeQuery();
						if (rs.next()) {
							countF = rs.getInt("COUNT");
						}
						rs.close();
						ps.close();
						countSeq++;
					} while (countF > 0);

					ReturnEntity returnEntity = frmSteelMakingPlanChange
							.generationFurnace(con, castList1, flow, castMode,
									zbLeftWgt, castno, String.valueOf(castSeq),
									furnaceno, maxCastNum, username, steelMax,
									steelMin, averageWt, ccmSeq, wtYlCount,
									tailLoss, smeltingWt, luNumFalg,
									smeltingWt1, wtCast);
					smeltingWt1 += returnEntity.getCastWt();
					ccmSeq = returnEntity.getCcmSeq();
					wtCast = returnEntity.getCastWt();
					// 返回信息
					if (returnEntity.getMessage().length() > 0) {
						core.setV_errMsg(returnEntity.getMessage());
						return core;
					}
					furnaceList.add(returnEntity.getFurEntity());
					List<PlnSteelforOrdEntity> list = returnEntity
							.getOrdEntityList();
					for (PlnSteelforOrdEntity entity : list) {
						steelordList.add(entity);
					}
					// 首炉
					if (castSeq == 1) {
						addWtBegin = returnEntity.getBeginAddWgt();
						beginGrade = returnEntity.getBeginGradename();
					}
					// 是否最后一个订单,如果到最后一个订单则不再循环
					if (returnEntity.isFlag()) {
						addWtEnd = returnEntity.getEndAddWgt();
						endGrade = returnEntity.getEndGradename();
						break;
					}
					if (luNumFalg.equals("1")) {
						// 如果生成的炉号大于最大炉数
						if (castSeq >= maxCastNum) {
							addWtEnd = returnEntity.getEndAddWgt();
							endGrade = returnEntity.getEndGradename();
							break;
						}
					}
				}
			}
			/**
			 * 保存炉次
			 */
			// 组浇重量
			plnStarttime = castStartTime;
			if (flag.equals("1")) {
				// 炉内计划
				for (PlnSteelforOrdEntity entity1 : steelordList) {
					// entity1.setFurnacenoSeq(String.valueOf(furnSeqNo));
					entity1.setCastNo(castno);
					entity1.setCastSeq(String.valueOf(castSeqNo));
					entity1.setMaketime(dateTime);
					// entity1.setCcmSeq(String.valueOf(ccmSeq));
					// entity1.setProMonth(proMonth);
					frmSteelMakingPlanChange.addFurnaceOrd(con, entity1);
					// ccmSeq++;
				}

				for (PlnSteelforfurnaceEntity entity : furnaceList) {
					// 查询电炉产线，钢种对应投入产出小时能力
					double capacity = FrmSteeSerialPublic.seqCapacityFurn(con,
							entity.getEfPlineCode(), entity.getWlidIn(), entity
									.getMaterialNo());

					// 查询同一产线炉次顺序号
					int furnSeqNo = getMaxFurnSeqno(con, entity
							.getEfPlineCode());
					castWtg += Double.parseDouble(entity.getWeight());
					castNum += Integer.parseInt(entity.getQuantity());
					String[] batc = getBatchingCode(con, entity
							.getEfPlineCode());
					if (batc == null) {
						core.setV_errMsg("电炉(" + entity.getEfPlineName()
								+ ")没有配料方案,请维护基础数据!");
						return core;
					}
					String batchingCode = batc[0];
					String batchingName = batc[1];
					double materiel = Double.parseDouble(batc[2]);
					double ironNum = Double.parseDouble(entity.getWeight())
							* 0.3 - materiel;
					double steelscrapNum = Double.parseDouble(entity
							.getWeight())
							- ironNum;
					entity.setBatchingCode(batchingCode);
					entity.setBatchingName(batchingName);
					entity.setHotmetalNum(batc[2]);
					entity.setIronNum(String.valueOf(ironNum));
					entity.setSteelscrapNum(String.valueOf(steelscrapNum));

					Date date = df.parse(plnStarttime);
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					cal.add(Calendar.HOUR, 3);// 24小时制
					date = cal.getTime();
					String proMonth = df.format(date).substring(0, 10).replace(
							"-", "");
					String plnEndtime = df.format(BaseHelper.dateAddSecond(df
							.parse(plnStarttime),
							(int) Math.ceil(capacity) * 60));
					entity.setPlnStarttime(plnStarttime);
					entity.setPlnEndtime(plnEndtime);
					entity.setFurnacenoSeq(String.valueOf(furnSeqNo));
					entity.setCastNo(castno);
					entity.setCastSeq(String.valueOf(castSeqNo));
					entity.setMaketime(dateTime);
					entity.setProMonth(proMonth);
					// String craftNo = queryFurnNoCraftNo(con, entity.getCic(),
					// entity.getEfPlineCode(), entity.getGradecode());
					// entity.setCraftNo(craftNo);
					frmSteelMakingPlanChange.addFurnace(con, entity);

					// 更新炉次订单炉次顺序号
					String updateOrdSql = " Update PLN_STEELFOR_ORD T SET T.FURNACENO_SEQ=?,T.PRO_MONTH=? WHERE T.FURNACENO=?";
					PreparedStatement ps = con.prepareStatement(updateOrdSql);
					ps.setObject(1, furnSeqNo);
					ps.setObject(2, proMonth);
					ps.setObject(3, entity.getFurnaceno());
					ps.executeUpdate();
					ps.close();

					plnStarttime = plnEndtime;
				}

				// 修改铸机订单已组炉量
				for (PlnOrderCastSEntity entity2 : castList1) {
					double planWtS = Double.parseDouble(entity2.getPlanWtS())
							- Double.parseDouble(entity2.getPlanWtSold());
					int planNumS = Integer.parseInt(entity2.getPlanNumS())
							- Integer.parseInt(entity2.getPlanNumSold());
					if (Double.parseDouble(entity2.getPlanedWt()) - planWtS > 0.00002) {
						entity2.setPlanedNum(String.valueOf(planNumS));
						entity2.setPlanedWt(String.valueOf(planWtS));
					}
					String castnoZz = castno;
					if (!entity2.getCastNoAll().equals("")) {
						castnoZz = entity2.getCastNoAll() + "," + castno;
					}
					entity2.setCastNoAll(castnoZz);
					frmSteelMakingPlanChange.updateOrderCastsData(con, entity2);
					PreparedStatement ps = null;
					// 修改管坯需求状态
					String sql = CoreXmlSqlParsersFactory
							.getSql("FrmSteelMakingPlanChange.updateReqinfGp");
					ps = con.prepareStatement(sql);
					ps.setObject(1, "2");
					ps.setObject(2, entity2.getProPlanId());
					ps.executeUpdate();
					ps.close();
				}
			} else {
				for (PlnSteelforfurnaceEntity entity : furnaceList) {
					castWtg += Double.parseDouble(entity.getWeight());
					castNum += Integer.parseInt(entity.getQuantity());
				}
				for (PlnSteelforOrdEntity entity : steelordList) {
					if (entity.getSurplusflag().equals("1")) {
						noPlanWgt += Double.parseDouble(entity.getTotWeightM());
					} else {
						ordWgt += Double.parseDouble(entity.getTotWeightM());
					}
				}
			}
			// }
			/**
			 * 浇次
			 */
			// 炉数
			int furnaceCout = furnaceList.size();
			// 新增浇次
			if (flag.equals("1")) {
				Date date = df.parse(castStartTime);
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				cal.add(Calendar.HOUR, 3);// 24小时制
				date = cal.getTime();
				String proMonth = df.format(date).substring(0, 10).replace("-",
						"");
				// 查询铸造产线，钢种对应投入产出小时能力
				double capacityZz = FrmSteeSerialPublic.seqCapacityCast(con,
						plineCode, wldescIn, materialNo);

				// 浇次计划结束时间
				String endTime = df
						.format(BaseHelper.dateAddSecond(
								df.parse(plnStarttime), (int) Math
										.ceil(capacityZz) * 60));

				int ifconfirm = 0;
				// 查询该铸造产线下的浇次是否确认
				String sqlIfConfi = "SELECT COUNT(*) COUNT FROM PLN_STEELFORCAST T WHERE T.PLINE_CODE='"
						+ plineCode + "' AND T.IF_CONFIRM='0'";
				PreparedStatement ps = con.prepareStatement(sqlIfConfi);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					ifconfirm = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (ifconfirm > 0) {
					core.setV_errMsg("铸造产线为:" + plineName + ",下有未确认的浇次,请先确认!");
					return core;
				}
				// 浇次数据
				PlnSteelforcastEntity castEntity = new PlnSteelforcastEntity();

				castEntity.setCastNo(castno);
				castEntity.setCastSeq(String.valueOf(castSeqNo));
				castEntity.setStatus("00");
				// 设定炉数
				castEntity.setFurnacenumber(String.valueOf(furnaceCout));
				castEntity.setTotWt(String.format("%.3f", castWtg));
				castEntity.setTotNum(String.valueOf(castNum));
				// 连铸机号
				castEntity.setCcmid(plineName);
				castEntity.setStarttime(castStartTime);
				castEntity.setEndtime(endTime);
				// 制定人
				castEntity.setMakeman(username);
				castEntity.setMaketime(dateTime);
				castEntity.setFlownumber(String.valueOf(flow));
				// 备注
				castEntity.setMemo(memo);
				// 连浇群组号
				castEntity.setWaterGroupCode(waterGroupCode);
				// 首炉加量
				castEntity.setAddWtBegin(String.valueOf(addWtBegin));
				// 尾炉加量
				castEntity.setAddWtEnd(String.valueOf(addWtEnd));
				// 开始钢种
				castEntity.setStartGradecode(beginGrade);
				castEntity.setMemoNum(memoNum);

				// 结束钢种
				castEntity.setEndGradecode(endGrade);
				castEntity.setPlineCode(plineCode);
				castEntity.setPlineName(plineName);
				castEntity.setProMonth(proMonth);
				castEntity.setDiameter(diameterLz);
				castEntity.setAimSteelQty(String.valueOf(averageWt));
				castEntity.setAverageQty(String.valueOf(wtYlCount));
				frmSteelMakingPlanChange.addCast(con, castEntity);
			}
			con.commit();
			if (flag.equals("0")) {
				String message = "预算结果("
						+ mess
						+ ") \r\n浇次重量:"
						+ String.format("%.3f", castWtg)
						+ "  浇次支数:"
						+ castNum
						+ "  开始钢种:"
						+ beginGrade
						+ "  结束钢种:"
						+ endGrade
						+ "\r\n订单使用量:"
						+ String.format("%.3f", ordWgt)
						+ "  炉数:"
						+ furnaceCout
						+ "\r\n生产加量:"
						+ new BigDecimal(noPlanWgt - addWtBegin - addWtEnd)
								.setScale(3, RoundingMode.HALF_UP)
								.doubleValue() + "   首炉加量:"
						+ String.format("%.3f", addWtBegin) + "   尾炉加量:"
						+ String.format("%.3f", addWtEnd);

				core.setV_errMsg(message);
				core.setResult(true);
				return core;
			}
			core.setV_errMsg("组浇成功!");
			core.setResult(true);
			return core;
		} catch (Exception e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			core.setV_errMsg(e.getMessage());
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				core.setV_errMsg(e.getMessage());
			}
		}
		return core;
	}

	/**
	 * 手工组浇
	 * 
	 * @param castList
	 *            订单集合
	 * @param flow
	 *            　流数
	 * @param castMode
	 *            　中包方式
	 * @param zbLeftWgt中包铸余
	 * @param castno浇次
	 * @param castSeq浇次内顺序号
	 * @param furnaceno炉次
	 * @param maxCastNum最大炉数
	 * @param username操作人
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SQLException
	 */
	ReturnEntity generationManualFurnace(Connection con,
			List<PlnOrderCastSEntity> castList, double flow, String castMode,
			double zbLeftWgt, String castno, String castSeq, String furnaceno,
			String username, double maxSteel, double minSteel, int ccmSeq,
			int maxCastNum, double standSteel) throws IllegalAccessException,
			InvocationTargetException, SQLException {
		ReturnEntity returnEntity = new ReturnEntity();
		boolean lastFlag = false;
		// 是否有余材(0没有)
		String surplusFlag = "0";
		// 炉内顺序号
		int furnaceSeq = 1;
		// 首炉加量(不是首炉，这个量为0，不影响后面计算)
		double beginAddWgt = 0;
		// 尾炉加量(首炉有开始钢种与首炉加量)
		double endAddWgt = 0;
		// 开始钢种
		String beignGradename = "";
		String endGradename = "";
		int beginAddNum = 0;
		// 炉次重量
		double sumPlanWgt = 0;
		// 炉次支数
		int sumPlanNum = 0;
		// 已排产重量
		double sumplanedWgt = 0;
		int sumplanedNum = 0;

		PlnOrderCastSEntity beginEntity = null;
		int firstNum = Integer.parseInt(castSeq);
		String cic = "";
		String pathGy = "";
		for (int i = 0; i < castList.size(); i++) {
			PlnOrderCastSEntity entity = castList.get(i);

			if (!pathGy.contains(entity.getStationDesc())) {
				pathGy = entity.getStationDesc();
			}
			if (Integer.parseInt(entity.getManualNum()) == Integer
					.parseInt(castSeq)) {
				beginEntity = entity;

				// 判定是否是首炉
				if (firstNum == 1) {
					/**
					 * 1 生成首炉加量的炉内计划
					 */
					beignGradename = entity.getGradename();
					double cutLenBegin = 0;
					if (entity.getCastingtype().equals("1")) {
						// 通过直径查询对应切头(待处理)
						double[] cutParams = FrmSteelMakingPlanChange
								.getBaseLzSpecParams(con, entity
										.getDiameterLz(), castMode);
						if (cutParams == null) {
							returnEntity
									.setMessage("直径(" + entity.getDiameterLz()
											+ ")没有维护连铸规格切头数据!");
							return returnEntity;
						}
						cutLenBegin = cutParams[0];

						// 管坯直径
						double dimater = Double.parseDouble(entity
								.getDiameterLz());
						// 计算米单重
						double mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
						// 单倍坯重
						double singleWeight = mSingleWeight
								* (Double.parseDouble(entity.getLenGpSingle()) / 1000);

						// 首炉加量
						beginAddWgt = new BigDecimal(mSingleWeight * flow
								* cutLenBegin)
								.setScale(3, RoundingMode.HALF_UP)
								.doubleValue();
						beginAddNum = (int) Math.ceil(beginAddWgt
								/ Double.parseDouble(entity.getPlanWtS())
								* Integer.parseInt(entity.getPlanNumS()));
						furnaceSeq = 0;
						// 首炉加量生成炉次订单
						PlnSteelforOrdEntity ordEntity = FrmSteelMakingPlanChange
								.generationSteelforOrd(entity, castno, castSeq,
										furnaceno, String.valueOf(furnaceSeq),
										String.valueOf(beginAddWgt), String
												.valueOf(beginAddNum), "1",
										"2", username, singleWeight, con,
										ccmSeq);
						returnEntity.getOrdEntityList().add(ordEntity);
						furnaceSeq++;
						ccmSeq++;// 连铸顺序号
						surplusFlag = "1";
						firstNum++;
					}
					sumPlanWgt = beginAddWgt;
					sumPlanNum = beginAddNum;
				}

				// 排产量
				double planWgt = Double.parseDouble(entity.getPlanWtS());
				// 排产支数
				int planNum = Integer.parseInt(entity.getPlanNumS());
				// 已组炉量
				double planedWgt = 0;
				if (!entity.getPlanedWt().equals("")) {
					planedWgt = Double.parseDouble(entity.getPlanedWt());
				}
				// 已组炉支数
				int planedNum = 0;
				if (!entity.getPlanedNum().equals("")) {
					planedNum = Integer.parseInt(entity.getPlanedNum());
				}
				// 可组炉
				sumplanedWgt = planWgt - planedWgt;
				sumplanedNum = planNum - planedNum;
				// 如果该订单排产量-已组炉等于0，则循环订单
				if (sumplanedWgt <= 0) {
					continue;
				}
				// 管坯直径
				double dimater = 0;
				double mSingleWeight = 0;
				double singleWeight = 0;
				if (entity.getCastingtype().equals("1")) {
					dimater = Double.parseDouble(entity.getDiameterLz());
					// 计算米单重
					mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
					// 单倍坯重
					singleWeight = mSingleWeight
							* (Double.parseDouble(entity.getLenGpSingle()) / 1000);
				} else {
					String sqldx = CoreXmlSqlParsersFactory
							.getSql("FrmSteelMakingPlanChange.getMscCastRForg");
					PreparedStatement ps1 = con.prepareStatement(sqldx);
					ps1.setObject(1, entity.getDiameterLz());
					ResultSet rs1 = ps1.executeQuery();
					if (rs1.next()) {
						singleWeight = rs1.getDouble("ACTUAL_WEIGHT");
					}
					rs1.close();
					ps1.close();
					if (singleWeight == 0) {
						returnEntity.setMessage("模铸订单(" + dimater + ")未取到实重!");
						return returnEntity;
					}
				}
				sumPlanWgt = sumPlanWgt + sumplanedWgt;
				sumPlanNum = sumPlanNum + sumplanedNum;

				PlnSteelforOrdEntity ordEntity = FrmSteelMakingPlanChange
						.generationSteelforOrd(entity, castno, castSeq,
								furnaceno, String.valueOf(furnaceSeq), String
										.valueOf(sumplanedWgt), String
										.valueOf(sumplanedNum), "0", "",
								username, singleWeight, con, ccmSeq);
				
				returnEntity.getOrdEntityList().add(ordEntity);
				furnaceSeq++;
				ccmSeq++;

				// 回填订单的已组炉重量
				entity.setPlanedWt(String.valueOf(sumplanedWgt));
				entity.setPlanedNum(String.valueOf(sumplanedNum));
				if (cic.equals("")) {
					cic = entity.getCic();
				}
				if (!cic.equals(entity.getCic())) {
					cic = cic + "," + entity.getCic();
				}
				endGradename = entity.getGradename();
			}
		}
		// 最后一炉(//计算尾炉加量)如果是尾炉，则要加尾炉加量炉次订单
		double addEnd = 0;
		int addEndNum = 0;
		if (Integer.parseInt(castSeq) == maxCastNum
				&& beginEntity.getCastingtype().equals("1")) {
			double cutLenEnd = 0;
			double[] cutParams = FrmSteelMakingPlanChange.getBaseLzSpecParams(
					con, beginEntity.getDiameterLz(), castMode);
			if (cutParams == null) {
				returnEntity.setMessage("直径(" + beginEntity.getDiameterLz()
						+ ")没有维护连铸规格切尾数据!");
				return returnEntity;
			}
			cutLenEnd = cutParams[1];
			/**
			 * 查询对应直径的切尾
			 */

			if (cutLenEnd <= 0) {
				String message = "[换中包]";
				if (castMode.equals("0")) {
					message = "[不换中包]";
				}
				returnEntity.setMessage("直径(" + beginEntity.getDiameterLz()
						+ ")连铸规格切头尾为0!" + message);
				return returnEntity;
			}
			// 排产量
			double planWgt1 = Double.parseDouble(beginEntity.getPlanWtS());
			// 排产支数
			int planNum1 = Integer.parseInt(beginEntity.getPlanNumS());
			addEnd = FrmSteelMakingPlanChange.CalculateEndAdd(cutLenEnd, Double
					.parseDouble(beginEntity.getDiameterLz()), flow, zbLeftWgt);
			addEndNum = (int) Math.ceil(addEnd / planWgt1 * planNum1);
			// 管坯直径
			double dimater = 0;
			double mSingleWeight = 0;
			double singleWeight = 0;
			dimater = Double.parseDouble(beginEntity.getDiameterLz());
			// 计算米单重
			mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
			// 单倍坯重
			singleWeight = mSingleWeight
					* (Double.parseDouble(beginEntity.getLenGpSingle()) / 1000);

			// 生成尾炉加量炉次订单
			if (addEnd > 0) {
				furnaceSeq = 99;
				PlnSteelforOrdEntity lastaddEntity = FrmSteelMakingPlanChange
						.generationSteelforOrd(beginEntity, castno, castSeq,
								furnaceno, String.valueOf(furnaceSeq), String
										.valueOf(addEnd), String
										.valueOf(addEndNum), "1", "3",
								username, singleWeight, con, ccmSeq);
				returnEntity.getOrdEntityList().add(lastaddEntity);
				sumPlanWgt = sumPlanWgt + addEnd;
				sumPlanNum = sumPlanNum + addEndNum;
				surplusFlag = "1";
				// 返回值(结束钢种与尾炉加量,用于浇次使用)
				endAddWgt = addEnd;
				furnaceSeq++;
				ccmSeq++;
			}
		}
		/**
		 * 1.判定可组炉量+已组炉量是否大于上限或小于下限
		 */
		// if (maxSteel < sumPlanWgt - beginAddWgt - addEnd) {
		// returnEntity.setMessage("炉次(" + castSeq + ")炉次重量(" + sumPlanWgt
		// + ")大于出钢量上限!");
		// return returnEntity;
		// }
		// if (minSteel > sumPlanWgt - beginAddWgt - addEnd) {
		// returnEntity.setMessage("炉次(" + castSeq + ")炉次重量(" + sumPlanWgt
		// + ")小于出钢量下限!");
		// return returnEntity;
		// }
		String[] batc = getBatchingCode(con, beginEntity.getPlineCodeA());
		if (batc == null) {
			returnEntity.setMessage("电炉(" + beginEntity.getPlineNameA()
					+ ")没有配料方案,请维护基础数据!");
			return returnEntity;
		}
		String[] cicAll = cic.split(",");
		// Arrays.sort(cicAll);
		TreeSet<String> prodCodeSet = new TreeSet<String>();
		prodCodeSet.addAll(Arrays.asList(cicAll));
		cicAll = prodCodeSet.toArray(new String[] {});
		cic = StringUtils.join(cicAll, ",");
		beginEntity.setCic(cic);
		// if (cicAll.length > 1) {
		// // 查询是否存在CIC交集
		// String cicJj = queryPlnCastCicAll(con, cic);
		// if (cicJj.equals("")) {
		// returnEntity.setMessage("炉次(" + castSeq + ")钢种索引码(CIC)(" + cic
		// + ")不存在交集!");
		// return returnEntity;
		// }
		// }
		// 生成炉信息（sumPlanWgt - beginAddWgt - addEnd）
		PlnSteelforfurnaceEntity furnaceEntity = FrmSteelMakingPlanChange
				.generationFurnace(beginEntity, castno, castSeq, furnaceno,
						String.valueOf(standSteel), String.valueOf(sumPlanWgt),
						String.valueOf(sumPlanNum), surplusFlag, username,
						pathGy);
		returnEntity.setBeginAddWgt(beginAddWgt);
		returnEntity.setEndAddWgt(endAddWgt);
		returnEntity.setBeginGradename(beignGradename);
		returnEntity.setEndGradename(endGradename);
		returnEntity.setFlag(lastFlag);
		returnEntity.setFurEntity(furnaceEntity);
		returnEntity.setCcmSeq(ccmSeq);
		return returnEntity;
	}

	/**
	 * 通过索引码查询是否存在交集
	 * 
	 * @param conn
	 * @param proPlanId
	 * @return
	 * @throws SQLException
	 */
	public static String queryPlnCastCicAll(Connection conn, String cic)
			throws SQLException {
		String cicAll = "";
		String sql = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		sql = "SELECT DISTINCT CIC FROM PLN_STEELFORFURNACE_CIC T WHERE T.CIC LIKE '%'||?||'%'";
		ps = conn.prepareStatement(sql);
		ps.setObject(1, cic);
		rs = ps.executeQuery();
		if (rs.next()) {
			cicAll = rs.getString("CIC");
		}
		rs.close();
		ps.close();
		return cicAll;
	}

	/**
	 * 获取索引码个数
	 * 
	 * @param con
	 * @param proPlanId
	 * @return
	 * @throws SQLException
	 */
	public static int queryPlnCastCicCount(Connection conn, String proPlanId)
			throws SQLException {
		int count = 1;
		String sql = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		sql = "SELECT COUNT(*) COUNT  FROM (SELECT CIC FROM PLN_ORDER_CAST_S T  WHERE T.PRO_PLAN_ID || '/' || T.GX_PLAN_NO IN ('')  ";
		sql += " GROUP BY T.CIC)";
		ps = conn.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 获取炼钢工艺文件
	 * 
	 * @param con
	 * @param cic
	 * @param plineCode
	 * @param gradeCode
	 * @return
	 * @throws SQLException
	 */
	public static String queryFurnNoCraftNo(Connection conn, String cic,
			String plineCode, String gradeCode, String planRoute)
			throws SQLException {
		String craftNo = "";
		int count = 0;
		String sql = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		// 查询是否存在同钢种，同炼钢产线，同CIC 的工艺文件
		/*
		 * sql =
		 * CoreXmlSqlParsersFactory.getSql("FrmSteelMerge.getQueryCraftLgM"); ps
		 * = conn.prepareStatement(sql); ps.setObject(1, cic); ps.setObject(2,
		 * gradeCode); ps.setObject(3, plineCode); ps.setObject(4, planRoute);
		 * rs = ps.executeQuery(); if (rs.next()) { count = rs.getInt("COUNT");
		 * } rs.close(); ps.close(); if (count == 1) {
		 */
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmSteelMerge.getQueryCraftLgMNo");
		ps = conn.prepareStatement(sql);
		ps.setObject(1, cic);
		ps.setObject(2, gradeCode);
		ps.setObject(3, plineCode);
		ps.setObject(4, planRoute);
		rs = ps.executeQuery();
		if (rs.next()) {
			craftNo = rs.getString("CRAFT_NO");
		}
		rs.close();
		ps.close();
		/* } */
		return craftNo;
	}

	/**
	 * 计算尾加量　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　
	 * 
	 * @param cutLenEnd切尾
	 * @param dimater
	 *            　直径　
	 * @param flow
	 *            　流数
	 * @param zbLeftWgt
	 *            中包铸余
	 * @return
	 */
	static double CalculateEndAdd(double cutLenEnd, double dimater,
			double flow, double zbLeftWgt) {
		// 计算米单重
		double mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;
		// 计算尾炉加量
		double endAddWgt = mSingleWeight * cutLenEnd * flow + zbLeftWgt;
		return endAddWgt;
	}
}

/**
 * 内部类
 * 
 * @author Administrator
 */
class ReturnEntity {
	private PlnSteelforfurnaceEntity furEntity;
	private List<PlnSteelforOrdEntity> ordEntityList = new ArrayList<PlnSteelforOrdEntity>();
	private double beginAddWgt = 0;
	private double endAddWgt = 0;
	private String beginGradename = "";
	private String endGradename = "";
	boolean flag = false;
	private int ccmSeq = 0;
	private double castWt = 0;

	/**
	 * 炉次订单重量
	 * 
	 * @return
	 */
	public double getCastWt() {
		return castWt;
	}

	/**
	 * 炉次订单重量
	 * 
	 * @return
	 */
	public void setCastWt(double castWt) {
		this.castWt = castWt;
	}

	public int getCcmSeq() {
		return ccmSeq;
	}

	public void setCcmSeq(int ccmSeq) {
		this.ccmSeq = ccmSeq;
	}

	/**
	 * 首炉加量
	 * 
	 * @param beginAddWgt
	 */
	public double getBeginAddWgt() {
		return beginAddWgt;
	}

	/**
	 * 首炉加量
	 * 
	 * @param beginAddWgt
	 */
	public void setBeginAddWgt(double beginAddWgt) {
		this.beginAddWgt = beginAddWgt;
	}

	/**
	 * 尾炉加量
	 * 
	 * @param beginAddWgt
	 */
	public double getEndAddWgt() {
		return endAddWgt;
	}

	/**
	 * 尾炉加量
	 * 
	 * @param beginAddWgt
	 */
	public void setEndAddWgt(double endAddWgt) {
		this.endAddWgt = endAddWgt;
	}

	/**
	 * 开始钢种
	 * 
	 * @param beginGradename
	 */
	public String getBeginGradename() {
		return beginGradename;
	}

	/**
	 * 开始钢种
	 * 
	 * @param beginGradename
	 */
	public void setBeginGradename(String beginGradename) {
		this.beginGradename = beginGradename;
	}

	/**
	 * 结束钢种
	 * 
	 * @param beginGradename
	 */
	public String getEndGradename() {
		return endGradename;
	}

	/**
	 * 结束钢种
	 * 
	 * @param beginGradename
	 */
	public void setEndGradename(String endGradename) {
		this.endGradename = endGradename;
	}

	String message = "";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public PlnSteelforfurnaceEntity getFurEntity() {
		return furEntity;
	}

	public void setFurEntity(PlnSteelforfurnaceEntity furEntity) {
		this.furEntity = furEntity;
	}

	public List<PlnSteelforOrdEntity> getOrdEntityList() {
		return ordEntityList;
	}

	public void setOrdEntityList(List<PlnSteelforOrdEntity> ordEntityList) {
		this.ordEntityList = ordEntityList;
	}

}