package com.steering.pss.plnsaleord.planStove;

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.List;

import java.util.*;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;
import com.steering.pss.plnsaleord.stove.ComPlanFurnace;
import com.steering.pss.plnsaleord.stove.PlanFurnProduction;
import com.steering.pss.plnsaleord.stove.entity.PlnZyZgCEntity;
import com.steering.pss.plnsaleord.stove.entity.PlnZyZgMEntity;
import com.steering.pss.plnsaleord.planStove.entity.PlnZyZgMOEntity;
import com.steering.pss.plnsaleord.planStove.entity.MilPlanEntity;
import com.steering.pss.plnsaleord.planStove.entity.MilFeedPlanEntity;
import com.steering.pss.plnsaleord.processOrder.entity.PlnOrderZgSEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

public class FrmPlanStoveD extends DBCommon {
	// private static final boolean PlnZlForcx = false;
	private static double Pi = 3.1415926; // 圆周率

	/**
	 * 查询轧管虚拟炉计划
	 * 
	 * @param timeBegin
	 * @param timeEnd
	 * @param plineCode
	 * @param orderNo
	 * @param proPlanId
	 * @param heatId
	 * @param statusArr
	 * @return
	 */
	public CoreReturnObject queryPlanStoveD(String timeBegin, String timeEnd,
			String plineCode, String orderNo, String proPlanId, String heatId,
			String[] statusArr, String judgeNo, String feedHeatNo,String lotNo,String lotNo1,String lotNo2,String feedHeatNo2) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanStoveD.queryPlanStoveD");
		List<String> param = new ArrayList<String>();
		if (!timeBegin.equals("") && !timeEnd.equals("")) {
			sql += " AND TO_DATE(T.PRO_DAY, 'YYYY-MM-DD') BETWEEN TO_DATE(?,'YYYY-MM-DD') AND TO_DATE(?,'YYYY-MM-DD')";
			param.add(timeBegin);
			param.add(timeEnd);
		}
		if (!plineCode.equals("")) {
			sql += " AND T.PLINE_CODE=? ";
			param.add(plineCode);
		}
		if (!orderNo.equals("")) {
			sql += " AND T.ORDER_NO||'/'||T.ORDER_SEQ LIKE '%'||?||'%'";
			param.add(orderNo);
		}
		if (!proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID||'/'||T.GX_PLAN_NO LIKE '%'||?||'%'";
			param.add(proPlanId);
		}
		if (!heatId.equals("")) {
			sql += " AND T.HEAT_PLAN_NO LIKE '%'||?||'%'";
			param.add(heatId);
		}
		if (!feedHeatNo.equals("")) {
			sql += " AND T.FEED_HEAT_NO ='" + feedHeatNo + "'";
		}
		if(!lotNo.equals(""))
		{
			sql += " AND T.ZY_BATCH_ID LIKE '%'||'"+lotNo+"'||'%'";
		}
		sql += CoreConverter.toSqlConditionString("T.PLANSTATUS", statusArr);
		if (!judgeNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO='" + judgeNo + "'";
		}
		if(!lotNo1.equals(""))
		{
			sql += " AND T.ZY_BATCH_ID >= '"+lotNo1+"' AND T.ZY_BATCH_ID <= '"+lotNo2+"'";
		}
		
        String sql2="";
		if (!feedHeatNo2.equals("")) {
			sql2 += " AND T2.STOVE_NO ='" + feedHeatNo2 + "'";
		}
		
		sql += " ) T2 WHERE 1=1 "+sql2;// ORDER BY T.GX_PRO_SEQ ASC
		sql += " ORDER BY T2.ZY_BATCH_ID,T2.GX_PRO_SEQ ASC ";//

		// , T2.GX_PRO_SEQ ASC
		return this.getData(sql, param.toArray());
	}

	/**
	 * 主从表一起查
	 * 
	 * @param timeBegin
	 * @param timeEnd
	 * @param plineCode
	 * @param orderNo
	 * @param proPlanId
	 * @param heatId
	 * @param statusArr
	 * @param judgeNo
	 * @return
	 */
	public CoreReturnObject queryPlanStoveM(String timeBegin, String timeEnd,
			String plineCode, String orderNo, String proPlanId, String heatId,
			String[] statusArr, String judgeNo, String feedHeatNo,String lotNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanStoveD.queryPlanStoveM");
		List<String> param = new ArrayList<String>();
		if (!timeBegin.equals("") && !timeEnd.equals("")) {
			sql += " AND TO_DATE(T.PRO_DAY, 'YYYY-MM-DD') BETWEEN TO_DATE(?,'YYYY-MM-DD') AND TO_DATE(?,'YYYY-MM-DD')";
			param.add(timeBegin);
			param.add(timeEnd);
		}
		if (!plineCode.equals("")) {
			sql += " AND T.PLINE_CODE=? ";
			param.add(plineCode);
		}
		if (!orderNo.equals("")) {
			sql += " AND T.ORDER_NO||'/'||T.ORDER_SEQ LIKE '%'||?||'%'";
			param.add(orderNo);
		}
		if (!proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID||'/'||T.GX_PLAN_NO LIKE '%'||?||'%'";
			param.add(proPlanId);
		}
		if (!heatId.equals("")) {
			sql += " AND T.HEAT_PLAN_NO LIKE '%'||?||'%'";
			param.add(heatId);
		}
		if (!feedHeatNo.equals("")) {
			sql += " AND T.FEED_HEAT_NO ='" + feedHeatNo + "'";
		}
		if(!lotNo.equals(""))
		{
			sql += " AND T.ZY_BATCH_ID LIKE '%'||'"+lotNo+"'||'%'";
		}
		sql += CoreConverter.toSqlConditionString("T.PLANSTATUS", statusArr);
		sql += " ORDER BY T.GX_PRO_SEQ ASC) T2 ";
		if (!judgeNo.equals("")) {
			sql += " WHERE  T2.JUDGE_STOVE_NO LIKE '%'||'" + judgeNo
					+ "' ||'%'";
		}

		sql += " ORDER BY T2.GX_PRO_SEQ_PC ASC , T2.GX_PRO_SEQ ASC";
		return this.getData(sql, param.toArray());
	}

	public CoreReturnObject queryPlanStoveRow(String heatPlanNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanStoveD.queryPlanStoveM");
		sql += "  AND T.HEAT_PLAN_NO ='" + heatPlanNo + "') t2";
		return this.getData(sql);
	}

	/**
	 * 查询轧管实际炉计划
	 * 
	 * @param plineCode
	 * @param proPlanId
	 * @param heatId
	 * @return
	 */
	public CoreReturnObject queryPlanStoveDc(String plineCode,
			String proPlanId, String heatId, String judgeNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanStoveD.queryPlanStoveDc");
		if (!plineCode.equals("")) {
			sql += " AND T.PLINE_CODE='" + plineCode + "' ";
		}
		if (!proPlanId.equals("")) {
			sql += " AND T.PRO_PLAN_ID LIKE '%'||'" + proPlanId + "' ||'%'";
		}
		if (!heatId.equals("")) {
			sql += " AND T.HEAT_PLAN_NO LIKE '%'||'" + heatId + "' ||'%'";
		}
		if (!judgeNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO LIKE '%'||'" + judgeNo + "' ||'%'";
		}

		sql += " ORDER BY T.HEAT_PLAN_SQE ASC";
		return this.getData(sql);
	}

	// 同步查询
	public CoreReturnObject queryPlanStoveD(String judgeNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanStoveD.queryPlanStoveD");
		sql += " ORDER BY T.GX_PRO_SEQ ASC) T2 WHERE 1=1";
		if (!judgeNo.equals("")) {
			sql += " AND T2.JUDGE_STOVE_NO LIKE '%'||'" + judgeNo + "' ||'%'";
		}
		// sql += " AND T2.STOVE_NO IS NULL ";
		return this.getData(sql);
	}

	// 同步查询
	public CoreReturnObject queryPlanStoveDc(String judgeNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanStoveD.queryPlanStoveDc");
		if (!judgeNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO LIKE '%'||'" + judgeNo + "' ||'%'";
		}
		// sql += " AND T.STOVE_NO IS NULL ";
		sql += " ORDER BY T.HEAT_PLAN_SQE ASC";
		return this.getData(sql);
	}

	/**
	 * 查询轧管实际炉计划
	 * 
	 * @param con
	 * @param
	 *
	 * @throws SQLException
	 */
	public static List<PlnZyZgMOEntity> getQureyZyZgM(Connection con,
			String heatPlanNo) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("StoveHelper.getPlanZyMC");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, heatPlanNo);
		ResultSet rs = ps.executeQuery();
		List<PlnZyZgMOEntity> list = EntityTool.getEntitysByRs(rs,
				PlnZyZgMOEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (PlnZyZgMOEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 查询最大上料生产顺序
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static String getMaxFeedSeq(Connection con, String date,
			String plineCode) throws SQLException {
		String feedSeqNo = "";
		String sql = "SELECT ? ||  LPAD(SUBSTR(NVL(MAX(T.FEED_SEQ),'000'),-3,3) + 1, 3, '0') "
				+ " AS FEED_SEQ FROM MIL_PLAN  T WHERE T.FEED_SEQ LIKE '%'||?||'%' AND T.PLINE_CODE=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, date + plineCode);
		ps.setObject(2, date + plineCode);
		ps.setObject(3, plineCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			feedSeqNo = rs.getString("FEED_SEQ");
		}
		rs.close();
		ps.close();
		return feedSeqNo;
	}

	/**
	 * 查询流动卡流水号
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static String getMaxLotSeq(Connection con, String proPlnIdNo,
			String zyBatchId) throws SQLException {
		String lotSeq = "";
		String sql = "SELECT ? ||  LPAD(SUBSTR(NVL(MAX(T.LOT_SEQ),'000'),-3,3) + 1, 3, '0') "
				+ " AS LOT_SEQ FROM MIL_PLAN  T WHERE T.SEQ=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, zyBatchId);
		ps.setObject(2, proPlnIdNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			lotSeq = rs.getString("LOT_SEQ");
		}
		rs.close();
		ps.close();
		return lotSeq;
	}

	/**
	 * 轧管炉计划下发
	 * 
	 * @param jslist
	 * @param userName
	 *            操作人
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doFurnacePlan(ArrayList<ArrayList<String>> jslist,
			String userName, String flag) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
			List<String> juList = new ArrayList<String>();
			// 循环虚拟炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String heatPlanNo = list.get(0);
				String judgeStoveNo = list.get(1);
				String stoveNo = list.get(2);
				String zyBatchId = list.get(3);
				String planstatus = "";
				// 查询炉计划信息
				List<PlnZyZgMOEntity> listZgC = getQureyZyZgM(conn, heatPlanNo);
				if (listZgC == null) {
					cro.setV_errMsg("炉计划已发生变化,请重新查询!");
					return cro;
				}
				List<PlnZyZgCEntity> listC = StoveHelper.getQureyZyZgC(conn,
						heatPlanNo);
				int countM = 0;
				String feedHeatNo = "";
				String proPlanId = "";
				String gxPlanNo = "";
				String plnDivideId = "";

				String gowhereName = "";
				String gowhereCode = "";
				String ifProduct = "0";
				PlnOrderZgSEntity plnOrderS = new PlnOrderZgSEntity();
				for (PlnZyZgMOEntity plnZgC : listZgC) {
					if (plnZgC.getJudgeStoveNo().equals("")) {
						cro.setV_errMsg("炼钢计划未下发,轧管炉计划不能下发!");
						return cro;
					}
					countM++;
					feedHeatNo = plnZgC.getFeedHeatNo();
					proPlanId = plnZgC.getProPlanId();
					gxPlanNo = plnZgC.getGxPlanNo();
					plnDivideId = plnZgC.getPlnDivideId();
					if (flag.equals("0")
							&& !plnZgC.getPlanstatus().equals("00")) {
						cro.setV_errMsg("选择的轧管炉计划状态不是计划状态,不能下发!");
						return cro;
					}
					if (flag.equals("1")
							&& !plnZgC.getPlanstatus().equals("10")) {
						cro.setV_errMsg("选择的轧管炉计划不是下发状态,不能撤销下发!");
						return cro;
					}

					plnOrderS = FrmPlanMaterialSelection.getPlanOrderZg(conn,
							proPlanId, gxPlanNo, PlnOrderZgSEntity.class);
					if (plnOrderS == null) {
						cro.setV_errMsg("未找到订单(" + proPlanId + "/" + gxPlanNo
								+ ")信息!");
						return cro;
					}
					if (flag.equals("1")) {// 撤销下发
						planstatus = "00";
						// 查询炉次接收表状态
						String queryMilPln = "SELECT T.STATUS,T.IS_LOAD FROM MIL_PLAN T WHERE T.ID=? ";
						ps = conn.prepareStatement(queryMilPln);
						ps.setObject(1, heatPlanNo);
						// ps.setObject(2, plnZgC.getProPlanId() + "-"
						// + plnZgC.getGxPlanNo());
						// ps.setObject(3, plnZgC.getFeedHeatNo() + "-" + "01");
						rs = ps.executeQuery();
						if (rs.next()) {
							if (!rs.getString("STATUS").equals("00")) {
								cro.setV_errMsg("轧管已开始锯切,不能撤销!");
								return cro;
							}
							if (rs.getString("IS_LOAD").equals("1")) {
								cro.setV_errMsg("轧管已上料,不能撤销!");
								return cro;
							}
						}
						rs.close();
						ps.close();

						// 删除炉计划接收表
						String deleteSqlPln = "DELETE MIL_PLAN T WHERE T.ID=?";
						ps = conn.prepareStatement(deleteSqlPln);
						ps.setObject(1, heatPlanNo);
						// ps.setObject(2, plnZgC.getProPlanId() + "-"
						// + plnZgC.getGxPlanNo());
						// ps.setObject(3, plnZgC.getFeedHeatNo() + "-" + "01");
						ps.executeUpdate();
						ps.close();
						if (listC != null) {
							for (PlnZyZgCEntity plnC : listC) {
								// 删除炉计划接收表
								String deleteSqlPls = "DELETE MIL_FEED_PLAN  WHERE FEED_ROW_NO = ? ";
								ps = conn.prepareStatement(deleteSqlPls);
								ps.setObject(1, plnC.getHeatPlanSqe());
								// ps.setObject(2, plnC.getFeedHeatNo() + "-"
								// + "01");
								ps.executeUpdate();
								ps.close();
							}
						}
					} else {
						if (plnOrderS.getIfStoveIssued().equals("0")) {
							cro.setV_errMsg("订单(" + proPlanId + "/" + gxPlanNo
									+ "),未下发,请先下发订单锁定生产批次!");
							return cro;
						}
						String gowWhere = ComPlanFurnace.getPlineCodeGroup(
								conn, plnZgC.getPlineCode(),
								plnZgC.getProPlanId(), plnZgC.getGxPlanNo());
						if (gowWhere != null) {
							// 查询去向
							String sqlGroup = "SELECT T.STORAGE_NO,T.STORAGE_NAME FROM COM_BASE_PLINE_R_GROUP T WHERE T.PLINE_CODE_M=? AND T.PLINE_NAME_C=?";
							ps = conn.prepareStatement(sqlGroup);
							ps.setObject(1, plnZgC.getPlineCode());
							ps.setObject(2, gowWhere);
							rs = ps.executeQuery();
							if (rs.next()) {
								gowhereName = rs.getString("STORAGE_NAME");
								gowhereCode = rs.getString("STORAGE_NO");
							}
							rs.close();
							ps.close();
						}
						int counnum = StoveHelper.getQueryStation(conn,
								proPlanId, gxPlanNo, "D");
						if (counnum > 0) {
							ifProduct = "1";
						}
						// 判断炼钢判定炉号是否重复下发
						int countZm = 0;
						String queryMjud = CoreXmlSqlParsersFactory
								.getSql("FrmPlanStoveD.QueryPlnZYStatus");
						ps = conn.prepareStatement(queryMjud);
						ps.setObject(1, plnZgC.getProPlanId());
						ps.setObject(2, plnZgC.getGxPlanNo());
						ps.setObject(3, plnZgC.getJudgeStoveNo());
						rs = ps.executeQuery();
						if (rs.next()) {
							if (!rs.getString("PLANSTATUS").equals("00")) {
								countZm = rs.getInt("COUNT");
								cro.setV_errMsg("轧管订单(" + plnZgC.getProPlanId()
										+ "/" + plnZgC.getGxPlanNo()
										+ ")对应上料炉号(" + plnZgC.getJudgeStoveNo()
										+ ")已下发,请先撤销该炉下发!");
								return cro;
							}
						}
						rs.close();
						ps.close();
						if (countZm > 0) {
							cro.setV_errMsg("炼钢上料炉号在同一轧管订单/排产序号下存在多个,请确认!");
							return cro;
						}
						int countGz = 0;
						String gradenameM = "";
						// 查询轧管订单对应钢种
						String sqlZgS = "SELECT T.GRADENAME FROM PLN_ZY_ZG_M T WHERE T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
						ps = conn.prepareStatement(sqlZgS);
						ps.setObject(1, plnZgC.getProPlanId());
						ps.setObject(2, plnZgC.getGxPlanNo());
						rs = ps.executeQuery();
						while (rs.next()) {
							if (gradenameM.equals("")) {
								gradenameM = rs.getString("GRADENAME");
							}
							if (!gradenameM.equals(rs.getString("GRADENAME"))) {
								countGz++;
							}
						}
						rs.close();
						ps.close();
						if (countGz > 0) {
							cro.setV_errMsg("轧管订单(" + plnZgC.getProPlanId()
									+ "/" + plnZgC.getGxPlanNo() + ")对应上料炉号("
									+ plnZgC.getJudgeStoveNo()
									+ ")钢种不同,请先拆分轧管订单!");
							return cro;
						}
						planstatus = "10";
						if (feedHeatNo.equals(""))// &&
						// !juList.contains(judgeStoveNo)
						{
							juList.add(judgeStoveNo);
							String maxFeed = "";
							String sqljend = "2000"; //2023年轧管系统用分炉坯:232000~239999
							//2022年分炉炉号
							/*if(plnZgC.getPlineCode().equals("C008")){
								sqljend = "4000";//250机组分炉炉号范围224000－225999
							}else if(plnZgC.getPlineCode().equals("C009")){
								sqljend = "6000";//258机组分炉炉号范围226000－227999
							}else if(plnZgC.getPlineCode().equals("C010")){
								sqljend = "7500";//168机组分炉炉号范围222000－223999,7500借500个炉号
							}else if(plnZgC.getPlineCode().equals("C017")){
								sqljend = "8000";//460机组分炉炉号范围228000－229999
							}else{
								sqljend = "0000";
							}*/
							String sql = "SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.SORTCODE='5007'";
							ps = conn.prepareStatement(sql);
							rs = ps.executeQuery();
							if (rs.next()) {
								maxFeed = rs.getString("BASENAME") + sqljend;
							} else {
								cro.setV_errMsg("轧管起始判定炉号不能为空！");
								return cro;
							}
							rs.close();
							ps.close();
							int feedMaxNum = Integer.parseInt(maxFeed);
							// 查询当前所要用的上料炉号280000-289999区间最大
							String judMatQ = CoreXmlSqlParsersFactory
									.getSql("FrmPlanStoveD.QueryfeedJut");
							ps = conn.prepareStatement(judMatQ);
							ps.setObject(1, maxFeed);
							ps.setObject(2, maxFeed.substring(0, 2));
							//ps.setObject(3, plnZgC.getPlineCode());
							ps.setObject(3, maxFeed.substring(0, 2));
							rs = ps.executeQuery();
							if (rs.next()) {
								feedMaxNum = rs.getInt("FEED_HEAT_NO");
							}
							rs.close();
							ps.close();
							List<String> listFeed1 = new ArrayList<String>();
							for (int j = Integer.parseInt(maxFeed); j <= feedMaxNum; j++) {
								listFeed1.add(String.valueOf(j));
							}
							List<String> listFeed2 = new ArrayList<String>();
							// 查询已用上料炉号数组
							judMatQ = CoreXmlSqlParsersFactory
									.getSql("FrmPlanStoveD.QueryFeedAll");
							ps = conn.prepareStatement(judMatQ);
							ps.setObject(1, maxFeed.substring(0, 2));
							ps.setObject(2, judgeStoveNo);
							ps.setObject(3, maxFeed.substring(0, 2));
							ps.setObject(4, judgeStoveNo);
							rs = ps.executeQuery();
							while (rs.next()) {
								listFeed2.add(rs.getString("FEED_HEAT_NO"));
							}
							rs.close();
							ps.close();

							// 下发时生成上料炉号
							int countSlcod = 0;
							// 1.查询该炉是否存在多个订单
							String sqlJueCount = CoreXmlSqlParsersFactory
									.getSql("FrmPlanStoveD.QueryHeatPlanNo");
							ps = conn.prepareStatement(sqlJueCount);
							ps.setObject(1, judgeStoveNo);
							ps.setObject(2, judgeStoveNo);
							rs = ps.executeQuery();
							while (rs.next()) {
								// 判定是否存在 上料炉号
								String fesNo = rs.getString("FEED_HEAT_NO");
								if (fesNo != null && fesNo.equals(judgeStoveNo)) {
									countSlcod++;
									break;
								}
							}
							rs.close();
							ps.close();

							String feedMax = "";
							PreparedStatement psU = null;

							if (countSlcod == 0) {
								feedMax = judgeStoveNo;
							} else {
								List<String> listMap = compareArry(listFeed1,
										listFeed2);
								if (null != listMap && listMap.size() > 0) {
									feedMaxNum = Integer.parseInt(listMap
											.get(0));
								}
								// 查询当前所要用的上料炉号280000-289999区间最大
								feedMax = String.valueOf(feedMaxNum);
								listFeed2.add(String.valueOf(feedMaxNum));
								feedMaxNum++;
							}
							// 修改轧管炉计划上料炉号
							String update = "UPDATE PLN_ZY_ZG_M T SET T.FEED_HEAT_NO=? WHERE T.HEAT_PLAN_NO=?";
							psU = conn.prepareStatement(update);
							psU.setObject(1, feedMax);
							psU.setObject(2, heatPlanNo);
							psU.executeUpdate();
							psU.close();
							// 修改轧管实际炉计划上料炉号
							update = "UPDATE PLN_ZY_ZG_C T SET T.FEED_HEAT_NO=? WHERE T.HEAT_PLAN_NO=?";
							psU = conn.prepareStatement(update);
							psU.setObject(1, feedMax);
							psU.setObject(2, heatPlanNo);
							psU.executeUpdate();
							psU.close();

							feedHeatNo = feedMax;

						}
						// 查询判定是否已生成接收表信息
						String sqlMil = "SELECT COUNT(*) COUNT  FROM MIL_PLAN T WHERE T.JUDGE_STOVE_NO=(SELECT T1.FEED_HEAT_NO||'-01' FROM PLN_ZY_ZG_M T1 WHERE T1.HEAT_PLAN_NO=?)";
						ps = conn.prepareStatement(sqlMil);
						ps.setObject(1, plnZgC.getHeatPlanNo());
						rs = ps.executeQuery();
						if (rs.next()) {
							if (rs.getInt("COUNT") > 0) {
								cro.setV_errMsg("该轧管判定炉号:" + feedHeatNo
										+ ",已生成装炉信息!");
								return cro;
							}
						}
						rs.close();
						ps.close();
						String feedSeq = getMaxFeedSeq(conn,
								dFormat.format(dFormat.parse(dateTime)),
								plnZgC.getPlineCode());
						String lotSeq = getMaxLotSeq(
								conn,
								plnZgC.getProPlanId() + "-"
										+ plnZgC.getGxPlanNo(), zyBatchId);
						MilPlanEntity milPln = new MilPlanEntity();
						milPln.setPrdcrNo(plnZgC.getPrdcrNo());
						milPln.setIfMonitor(plnZgC.getIfMonitor());
						milPln.setIfProduct(ifProduct);
						milPln.setCraftSeq(plnOrderS.getCraftSeq());
						milPln.setCic(plnZgC.getCic());
						milPln.setIndexSeq(plnOrderS.getIndexSeq());
						milPln.setPic(plnZgC.getPic());
						milPln.setId(plnZgC.getHeatPlanNo());
						milPln.setSeq(plnZgC.getProPlanId() + "-"
								+ plnZgC.getGxPlanNo());
						milPln.setStoveNo(stoveNo);
						milPln.setJudgeStoveNo(feedHeatNo + "-" + "01");
						milPln.setLotNo(zyBatchId);
						milPln.setContractNo(plnZgC.getOrderNo() + "/"
								+ plnZgC.getOrderSeq());
						milPln.setImpProcess(plnOrderS.getCraftFileNo());
						milPln.setPlineCode(plnZgC.getPlineCode());
						milPln.setProducer(plnZgC.getMonitordemand());
						milPln.setProcessStatus(plnZgC.getProcessDescC());
						milPln.setDbLength(plnZgC.getLenGpSingle());
						milPln.setDbHeight(plnZgC.getWtGpSingle());
						milPln.setRollLength(plnZgC.getRollLength());
						milPln.setSawNum(plnZgC.getOutnumCut());
						milPln.setTargetLength(plnZgC.getAimlength());
						milPln.setFinishUser(plnZgC.getFinalUserDesc());
						milPln.setTargetLiabrary(gowhereName);
						milPln.setVarieties(plnZgC.getProducname());
						milPln.setVarietiesCode(plnZgC.getProduccode());
						String outdiameZg = new BigDecimal(
								plnZgC.getOutdiameterZg()).setScale(2,
								RoundingMode.HALF_UP).toString();
						String wallthZg = new BigDecimal(
								plnZgC.getWallthickZg()).setScale(2,
								RoundingMode.HALF_UP).toString();
						milPln.setPipeSize(outdiameZg + "*" + wallthZg);
						// milPln.setPipeSize(plnZgC.getSpecName());
						String sql = "SELECT T.Spec_Code FROM COM_BASE_SPEC T WHERE T.SPEC_NAME=? AND T.VALIDFLAG='1'";
						ps = conn.prepareStatement(sql);
						ps.setObject(1, outdiameZg + "x" + wallthZg);
						rs = ps.executeQuery();
						if (rs.next()) {
							milPln.setPipeSizeCode(rs.getString("Spec_Code"));
						}
						rs.close();
						ps.close();
						milPln.setBuckleType(plnZgC.getModelDesc());
						milPln.setBuckleTypeCode(plnZgC.getModelCode());
						milPln.setLengthRange(plnZgC.getLenRollMin() + "-"
								+ plnZgC.getLenRollMax());
						milPln.setRollStandrad(plnZgC.getStdName());
						milPln.setPipeSection(plnZgC.getDiameter());
						milPln.setSteelcode(plnZgC.getSteelcode());
						milPln.setSteelname(plnZgC.getSteelname());
						milPln.setGradecode(plnZgC.getGradecode());
						milPln.setGradename(plnZgC.getGradename());
						String steelStandrad = "";
						if (ifProduct.equals("1")) {
							steelStandrad = plnZgC.getCic() + "+"
									+ plnZgC.getPic() + "+" + plnZgC.getSic()
									+ "+" + plnZgC.getDic();
						} else {
							steelStandrad = plnZgC.getPic() + "+"
									+ plnZgC.getSic() + "+" + plnZgC.getDic();
						}

						if (steelStandrad.replace("+", "").equals("")) {
							steelStandrad = plnZgC.getCraftFileNo();
						}
						milPln.setSteelStandrad(steelStandrad);
						milPln.setTarget(plnZgC.getDecision());
						milPln.setBranch(plnZgC.getPlnInsingleNum());
						milPln.setTon(plnZgC.getPlnInsingleWt());
						milPln.setDbNum(plnZgC.getPlnInsingleNum());
						milPln.setTom(dateTime);
						milPln.setCutHeadLengthp(plnZgC.getLenCuthead());
						milPln.setCutTrailLengthp(plnZgC.getLenCutend());
						milPln.setControlThickp(plnZgC.getAimwallthick());
						milPln.setProduceStandrad(plnZgC.getOutputStandard());
						milPln.setFeedSeq(feedSeq);
						milPln.setLotSeq(lotSeq);
						milPln.setFeedHeatNo(judgeStoveNo);
						milPln.setLengthcp(plnZgC.getLengthdisc());
						milPln.setCodeSampleFlag("0");
						milPln.setRollStandradCode(plnZgC.getStdCode());
						milPln.setStdStyle(plnZgC.getStdStyle());
						milPln.setStdStyleDesc(plnZgC.getStdStyleDesc());
						milPln.setCheckNo("");
						milPln.setOrdLnDlyPk(plnZgC.getOrdLnDlyPk() + "-"
								+ plnZgC.getPlnDivideId());
						if (plnZgC.getDecision().contains("A")) {
							milPln.setPrtFlag("1");
						} else {
							milPln.setPrtFlag("0");
						}
						milPln.setProPlanId(plnZgC.getProPlanId());
						milPln.setGxPlanNo(plnZgC.getGxPlanNo());
						String gpType = "铸坯";
						if (plnZgC.getGptype().equals("2")) {
							gpType = "锻坯";
						}
						milPln.setTubeType(gpType);
						String tubSour = "自炼";
						if (plnZgC.getOutsBilletFl().equals("1")) {
							tubSour = "外购";
						}
						milPln.setTubeSuorce(tubSour);
						milPln.setType(plnZgC.getPlnType());
						milPln.setStatus("00");
						milPln.setNodeStatus("00");
						// 新增炉计划接收表
						String addSqlPln = EntityTool.fillSqlByEntity(
								CoreXmlSqlParsersFactory
										.getSql("FrmPlanStoveD.AddMilPlan"),
								milPln);
						ps = conn.prepareStatement(addSqlPln);
						ps.executeUpdate();
						ps.close();

						if (listC != null) {
							for (PlnZyZgCEntity plnC : listC) {// 存在实际炉计划新增上料行
								double feedLength = Double.parseDouble(plnC
										.getLengthM())
										+ Double.parseDouble(plnC
												.getCutLength());
								int dimater = Integer.parseInt(plnC
										.getDiameter());
								if (dimater == 251) {
									dimater = 250;
								}
								double mSingleWeight = (7.8 * Pi / 4 * dimater * dimater) / 1000 / 1000;// 米单重
								// 单倍坯重
								// double weightGpOne = mSingleWeight *
								// (Double.parseDouble(plnZgC.getLenGpSingle())
								// /
								// 1000);
								// 倍尺坯单支重=坯米单重*倍尺坯长/1000
								double weightM = mSingleWeight
										* (feedLength / 1000);
								// double wt = weightM
								// * Integer.parseInt(plnZgC
								// .getPlanIndoubleNum());
								String cutNumGp = plnC.getCutNumGp();
								if (cutNumGp.equals("")) {
									cro.setV_errMsg("倍尺数不能为空!");
									return cro;
								}
								MilFeedPlanEntity milFeed = new MilFeedPlanEntity();
								milFeed.setFeedRowNo(plnC.getHeatPlanSqe());
								milFeed.setSeq("");
								milFeed.setLotNo(zyBatchId);
								milFeed.setStoveNo(stoveNo);
								milFeed.setJudgeStoveNo(feedHeatNo + "-" + "01");
								milFeed.setPlineCode(plnC.getPlineCode());
								milFeed.setDbNum(plnC.getCutNumGp());
								milFeed.setMatHeight(String.valueOf(weightM));
								milFeed.setMatTotalHeight(plnC
										.getPlanIndoubleWt());
								milFeed.setFeedNum(plnC.getPlanIndoubleNum());
								milFeed.setFeedLength(plnC.getLengthMOld());
								milFeed.setUseLength(plnC.getLengthM());
								milFeed.setCutTarget(plnC.getCutTarget());
								milFeed.setCutLength(plnC.getCutLength());
								milFeed.setCutWeight(plnC.getCutWeight());
								milFeed.setRemark("");
								milFeed.setDbLength(plnC.getLenGpSingle());
								milFeed.setNonUseLength(plnC.getCutLength());
								milFeed.setPipeSection(plnC.getDiameter());
								milFeed.setDbHeight(plnC.getWtGpSingle());
								milFeed.setProPlanId(plnC.getProPlanId());
								milFeed.setGxPlanNo(plnC.getGxPlanNo());
								milFeed.setFeedNumPlan(plnC
										.getPlanIndoubleNum());
								milFeed.setCreateName(userName);
								milFeed.setCreateTime(dateTime);
								// 新增上料行表
								String addSqlPls = EntityTool
										.fillSqlByEntity(
												CoreXmlSqlParsersFactory
														.getSql("FrmPlanStoveD.AddMilFeedPlan"),
												milFeed);
								ps = conn.prepareStatement(addSqlPls);
								ps.executeUpdate();
								ps.close();
							}
						}
					}
				}
				// 修改轧管虚拟炉计划
				String deleteZgM = "UPDATE PLN_ZY_ZG_M T SET T.ZY_BATCH_ID=?,T.PLANSTATUS=?,T.GOWHERE_CODE=?,T.GOWHERE_NAME=?,"
						+ "T.DOWNMAN=?,T.DOWNTIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS'),T.CRAFT_FILE_NO=?,T.CRAFT_FILE_MAN=?,"
						+ "T.CRAFT_SEQ=?,t.IF_PRODUCT=? WHERE T.HEAT_PLAN_NO=?";
				ps = conn.prepareStatement(deleteZgM);
				ps.setObject(1, zyBatchId);
				ps.setObject(2, planstatus);
				ps.setObject(3, gowhereCode);
				ps.setObject(4, gowhereName);
				ps.setObject(5, userName);
				ps.setObject(6, dateTime);
				ps.setObject(7, plnOrderS.getCraftFileNo());
				ps.setObject(8, plnOrderS.getCraftFileMan());
				ps.setObject(9, plnOrderS.getCraftSeq());
				ps.setObject(10, ifProduct);
				ps.setObject(11, heatPlanNo);
				ps.executeUpdate();
				ps.close();
				// 修改轧管实际炉计划
				deleteZgM = "UPDATE PLN_ZY_ZG_C T SET T.ZY_BATCH_ID=? WHERE T.HEAT_PLAN_NO=?";
				ps = conn.prepareStatement(deleteZgM);
				ps.setObject(1, zyBatchId);
				ps.setObject(2, heatPlanNo);
				ps.executeUpdate();
				ps.close();

				if (flag.equals("1")) {
					// 撤销下工序虚拟炉计划
					String messeGx = PlanFurnProduction.deletePlnFurn(
							proPlanId, gxPlanNo, userName, dateTime,
							heatPlanNo, plnDivideId, conn);
					if (!messeGx.equals("")) {
						cro.setV_errMsg(messeGx);
						return cro;
					}
				} else {
					// 新增下工序虚拟炉计划
					String messeGx = PlanFurnProduction.addPlnFurn(proPlanId,
							gxPlanNo, userName, dateTime, heatPlanNo,
							plnDivideId, conn);
					if (!messeGx.equals("")) {
						cro.setV_errMsg(messeGx);
						return cro;
					}
				}
			}
			if (flag.equals("1")) {
				cro.setV_errMsg("撤销成功!");
			} else {
				cro.setV_errMsg("下发成功!");
			}
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 比较2个数组，获取相对于数组1相同的元素、相对于数组2相同的元素，2个数组相同的元素
	 * 
	 * @param t1
	 * @param t2
	 * @return
	 */
	public static List<String> compareArry(List<String> t1, List<String> t2) {
		/**
		 * 集合A (新的数据有，旧的数据没有，即为要添加的数据)
		 */
		List<String> listA = new ArrayList<String>();
		for (String str1 : t1) {// 遍历新的数组元素
			if (!t2.contains(str1)) {// 旧的数组中不包含新的元素（即为要添加的元素）
				if (!listA.contains(str1)) {
					listA.add(String.valueOf(str1));
				}
			}
		}
		Collections.sort(listA);
		return listA;
	}

	/**
	 * 轧管炉计划下发后获取上料行
	 * 
	 * @param jslist
	 * @param userName
	 *            操作人
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doAddFeedLien(ArrayList<ArrayList<String>> jslist,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			// 获取数据库当前时间
			// String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环虚拟炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String heatPlanNo = list.get(0);

				List<PlnZyZgMOEntity> listZgC = getQureyZyZgM(conn, heatPlanNo);
				if (listZgC == null) {
					cro.setV_errMsg("未找到轧管实际炉计划或炉计划已发生变化,请重新查询!");
					return cro;
				}
				for (PlnZyZgMOEntity plnZgC : listZgC) {

					if (!plnZgC.getPlanstatus().equals("10")) {
						cro.setV_errMsg("选择的轧管炉计划状态不是下发状态,不能获取!");
						return cro;
					}
					// 查询是否生成上料行记录
					String feedNo = "";
					String sqlQ = "SELECT COUNT(*) COUNT FROM PLN_ZY_ZG_C T1, MIL_FEED_PLAN F  WHERE T1.HEAT_PLAN_SQE = F.FEED_ROW_NO AND T1.HEAT_PLAN_SQE = ?";
					ps = conn.prepareStatement(sqlQ);
					ps.setObject(1, plnZgC.getHeatPlanSqe());
					rs = ps.executeQuery();
					if (rs.next()) {
						feedNo = "1";
					}
					rs.close();
					ps.close();
					if (!feedNo.equals("")) {
						cro.setV_errMsg("选择的轧管炉计划已生成上料行,不能重新获取!");
						return cro;
					}
					MilFeedPlanEntity milFeed = new MilFeedPlanEntity();
					milFeed.setFeedRowNo(plnZgC.getHeatPlanSqe());
					milFeed.setSeq("");
					milFeed.setLotNo(plnZgC.getZyBatchId());
					milFeed.setStoveNo(plnZgC.getStoveNo());
					milFeed.setJudgeStoveNo(plnZgC.getFeedHeatNo() + "-" + "01");
					milFeed.setPlineCode(plnZgC.getPlineCode());
					milFeed.setDbNum(plnZgC.getCutNumGp());
					milFeed.setMatHeight(plnZgC.getWeightM());
					milFeed.setMatTotalHeight(plnZgC.getPlanIndoubleWt());
					milFeed.setFeedNum(plnZgC.getPlanIndoubleNum());
					milFeed.setFeedLength(plnZgC.getLengthM());
					milFeed.setUseLength(plnZgC.getLengthM());
					milFeed.setCutTarget(plnZgC.getCutTarget());
					milFeed.setCutLength(plnZgC.getCutLength());
					milFeed.setCutWeight(plnZgC.getCutWeight());
					milFeed.setRemark("");
					milFeed.setDbLength(plnZgC.getLenGpSingle());
					milFeed.setNonUseLength(plnZgC.getCutLength());
					milFeed.setPipeSection(plnZgC.getDiameter());
					milFeed.setDbHeight(plnZgC.getWtGpSingle());
					milFeed.setProPlanId(plnZgC.getProPlanId());
					milFeed.setGxPlanNo(plnZgC.getGxPlanNo());
					milFeed.setFeedNumPlan(plnZgC.getPlanIndoubleNum());
					// 新增上料行表
					String addSqlPls = EntityTool.fillSqlByEntity(
							CoreXmlSqlParsersFactory
									.getSql("FrmPlanStoveD.AddMilFeedPlan"),
							milFeed);
					ps = conn.prepareStatement(addSqlPls);
					ps.executeUpdate();
					ps.close();
				}
			}
			cro.setV_errMsg("获取成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 轧管炉计划修改备注
	 * 
	 * @param jslist
	 * @param userName
	 *            操作人
	 * @throws ParseException
	 */
	public CoreReturnObject doUpdateMemo(ArrayList<ArrayList<String>> jslist,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环虚拟炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String memo = list.get(0);
				String heatPlanNo = list.get(1);
				// 查询炉计划状态
				// String querySql =
				// "SELECT T.PLANSTATUS FROM PLN_ZY_ZG_M T WHERE T.HEAT_PLAN_NO = ?";
				// ps = conn.prepareStatement(querySql);
				// ps.setObject(1, heatPlanNo);
				// rs = ps.executeQuery();
				// if (rs.next()) {
				// if (!rs.getString("PLANSTATUS").equals("00")) {
				// cro.setV_errMsg("选中轧管炉计划已不是计划状态,不能修改!");
				// return cro;
				// }
				// }
				// else {
				// cro.setV_errMsg("炉计划信息已发生改变,请重新查询数据!");
				// return cro;
				// }
				// rs.close();
				// ps.close();
				// 修改轧管虚拟炉计划
				String deleteZgM = "UPDATE PLN_ZY_ZG_M T SET T.MEMO=?,T.UPDATE_NAME=?,T.UPDATE_TIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS') WHERE T.HEAT_PLAN_NO=?";
				ps = conn.prepareStatement(deleteZgM);
				ps.setObject(1, memo);
				ps.setObject(2, userName);
				ps.setObject(3, dateTime);
				ps.setObject(4, heatPlanNo);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("修改成功!");

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

	/**
	 * 轧管炉计划修改轧管判定炉号
	 * 
	 * @param jslist
	 * @param userName
	 *            操作人
	 * @throws ParseException
	 */
	public CoreReturnObject doUpdateFeedHeattNo(
			ArrayList<ArrayList<String>> jslist, String userName)
			throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环虚拟炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				// String zyBatchId = list.get(0);
				String heatPlanNo = list.get(0);
				String feedHeatNo = list.get(1);
				// 查询炉计划状态
				String querySql = "SELECT T.PLANSTATUS FROM PLN_ZY_ZG_M T WHERE T.HEAT_PLAN_NO = ?";
				ps = conn.prepareStatement(querySql);
				ps.setObject(1, heatPlanNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					if (!rs.getString("PLANSTATUS").equals("00")) {
						cro.setV_errMsg("选中轧管炉计划已不是计划状态,不能修改!");
						return cro;
					}
				} else {
					cro.setV_errMsg("炉计划信息已发生改变,请重新查询数据!");
					return cro;
				}
				rs.close();
				ps.close();
				// 修改轧管虚拟炉计划
				String deleteZgM = "UPDATE PLN_ZY_ZG_M T SET T.FEED_HEAT_NO=?,T.UPDATE_NAME=?,T.UPDATE_TIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS') WHERE T.HEAT_PLAN_NO=?";
				ps = conn.prepareStatement(deleteZgM);
				// ps.setObject(1, zyBatchId);
				ps.setObject(1, feedHeatNo);
				ps.setObject(2, userName);
				ps.setObject(3, dateTime);
				ps.setObject(4, heatPlanNo);
				ps.executeUpdate();
				ps.close();
				// 修改轧管实际炉计划
				deleteZgM = "UPDATE PLN_ZY_ZG_C T SET T.FEED_HEAT_NO=?,T.UPDATE_NAME=?,T.UPDATE_TIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS') WHERE T.HEAT_PLAN_NO=?";
				ps = conn.prepareStatement(deleteZgM);
				// ps.setObject(1, zyBatchId);
				ps.setObject(1, feedHeatNo);
				ps.setObject(2, userName);
				ps.setObject(3, dateTime);
				ps.setObject(4, heatPlanNo);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("修改成功!");

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

	/**
	 * 实际炉计划查询炉次订单连铸信息
	 * 
	 * @param con
	 * @param
	 *
	 * @param
	 *
	 * @throws SQLException
	 */
	public static <T> T getPlanZgS(Connection con, String proPlanId,
			String gxPlanNo, Class<T> cls) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("ComPlanFurnace.queryPlnOrderZgS");
		sql += " AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proPlanId);
		ps.setObject(2, gxPlanNo);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 删除剩余炉计划
	 * 
	 * @param jslist
	 * @param userName
	 *            操作人
	 * @throws ParseException
	 */
	public CoreReturnObject doDeleteStatr(ArrayList<ArrayList<String>> jslist,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			// 循环虚拟炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String heatPlanNo = list.get(0);
				String heatPlanSqe = list.get(1);
				int countNum = 0;// 可删除剩余支
				int actNum = 0;// 库存剩余支
				String feedHeatNo = "";
				String judgeStoveNo = "";
				String proPlanId = "";
				String gxPlanNo = "";
				String lengthMOld = "";// 倍尺坯长
				String lenGpSingleOld = "";// 单倍坯长
				String cutNumOld = "";// 倍尺数
				String jugdeApplyCode = "";
				// 查询炉计划可删除支
				String querySql = CoreXmlSqlParsersFactory
						.getSql("FrmPlanStoveD.QueryRemaZg");
				ps = conn.prepareStatement(querySql);
				ps.setObject(1, heatPlanNo);
				ps.setObject(2, heatPlanSqe);
				rs = ps.executeQuery();
				if (rs.next()) {
					actNum = rs.getInt("ACT_COUNT");
					countNum = rs.getInt("COUNTNUM");
					feedHeatNo = rs.getString("FEED_HEAT_NO");
					judgeStoveNo = rs.getString("JUDGE_STOVE_NO");
					proPlanId = rs.getString("PRO_PLAN_ID");
					gxPlanNo = rs.getString("GX_PLAN_NO");
					lengthMOld = rs.getString("LENGTH_M_OLD");
					lenGpSingleOld = rs.getString("LEN_GP_SINGLE_OLD");
					cutNumOld = rs.getString("CUT_NUM_OLD");
					jugdeApplyCode = rs.getString("JUGDE_APPLY_CODE");
					if (rs.getInt("PLANSTATUS") < 20) {
						cro.setV_errMsg("判定炉号(" + feedHeatNo + ")未上料,不能进行次操作!");
						return cro;
					}
				} else {
					cro.setV_errMsg("炉计划信息已发生改变,请重新查询数据!");
					return cro;
				}
				rs.close();
				ps.close();
				if (countNum <= 0 || actNum == 0) {
					cro.setV_errMsg("上料炉号(" + feedHeatNo + ")已全部上料!");
					return cro;
				}
				// if (countNum != actNum) {
				// cro.setV_errMsg("判定炉号已全部上料!");
				// return cro;
				// }
				// 修改库存释放余材
				String updateSql = CoreXmlSqlParsersFactory
						.getSql("FrmPlanStoveD.updateMatYc");
				ps = conn.prepareStatement(updateSql);
				ps.setObject(1, judgeStoveNo);
				ps.setObject(2, jugdeApplyCode);
				ps.setObject(3, proPlanId);
				ps.setObject(4, gxPlanNo);
				ps.setObject(5, lengthMOld);
				ps.setObject(6, lenGpSingleOld);
				ps.setObject(7, cutNumOld);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("删除成功!");

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

	/**
	 * 手动关闭炉计划
	 */
	public CoreReturnObject doLuClose(ArrayList<ArrayList<String>> jslist,
			String userName) throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// 循环炉计划信息
			for (int i = 0; i < jslist.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String heatPlanNo = list.get(0);
				// 查询炉计信息
				PlnZyZgMEntity listM = FrmPlanStoveE.getQureyZyM(conn,
						heatPlanNo, "D", PlnZyZgMEntity.class);
				if (listM == null) {
					cro.setV_errMsg("炉计划已发生变化,请重新查询!");
					return cro;
				}
				if (listM.getPlanstatus().equals("40")) {
					cro.setV_errMsg("选择的轧管炉计划状态是关闭状态,不能重复关闭!");
					return cro;
				}

				// 关闭炉计划
				String deleteZgC = "UPDATE PLN_ZY_ZG_M T SET T.IS_CLOSE='1',T.PLANSTATUS='40' WHERE HEAT_PLAN_NO = ?";
				ps = conn.prepareStatement(deleteZgC);
				ps.setObject(1, heatPlanNo);
				ps.executeUpdate();
				ps.close();
			}
			cro.setV_errMsg("关闭成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/***
	 * 查询所有仓库
	 * @return
	 */
	public CoreReturnObject doQueryStorageNo()
	{
            return this.getDataBySqlID("FrmPlanStoveD.doQueryStorageNo");
	}

	/***
	 * 修改物流去向
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject updateGoWhere(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str: parmList)
			{
				PlnZyZgMEntity plnZyZgEntity = EntityTool.getEntityByJSONStr(str,PlnZyZgMEntity.class);
				String updateGoWhereSql = CoreXmlSqlParsersFactory.getSql("FrmPlanStoveD.updateGoWhere");
				updateGoWhereSql = EntityTool.fillSqlByEntity(updateGoWhereSql,plnZyZgEntity);
				sqllist.add(updateGoWhereSql);
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("修改成功!");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	/**
	 * 获取天津喷印炉号
	 */
	public CoreReturnObject updateFlagHeatNo(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str: parmList)
			{
				PlnZyZgMEntity plnZyZgEntity = EntityTool.getEntityByJSONStr(str,PlnZyZgMEntity.class);
				String updateGoWhereSql = CoreXmlSqlParsersFactory.getSql("FrmPlanStoveD.updateFlagHeatNo");
				updateGoWhereSql = EntityTool.fillSqlByEntity(updateGoWhereSql,plnZyZgEntity);
				sqllist.add(updateGoWhereSql);
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("获取成功!");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	/**
	 * 撤销喷印炉号
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject cancleFlagHeatNo(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str: parmList)
			{
				PlnZyZgMEntity plnZyZgEntity = EntityTool.getEntityByJSONStr(str,PlnZyZgMEntity.class);
				String updateGoWhereSql = CoreXmlSqlParsersFactory.getSql("FrmPlanStoveD.cancleFlagHeatNo");
				updateGoWhereSql = EntityTool.fillSqlByEntity(updateGoWhereSql,plnZyZgEntity);
				sqllist.add(updateGoWhereSql);
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("撤销成功!");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
