package com.steering.mes.zgmil.coup;

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

import javax.script.ScriptException;

import com.steering.comm.db.DBCommon;

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

import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.mes.zgmil.entity.FurResultEntity;
import com.steering.mes.zgmil.entity.MilPlan;
import com.steering.mes.zgmil.entity.FeedRowPlan;
import com.steering.mes.zgmil.entity.FeedRack;
import com.steering.mes.zgmil.entity.MilScrapSlab;
import com.steering.mes.zgmil.entity.FeedSawResultEntity;
import com.steering.mes.zgmil.entity.MilSlabScrapSeqEntity;

/**
 * 上料锯切实绩业务类
 * 
 * @author lr 2015/05/28
 * @version v1.0
 */
public class FurResult extends DBCommon {
	/**
	 * 查询
	 * 
	 * @param JudgeStoveNo
	 *            炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getFurResult(String JudgeStoveNo, String proPlanId,
			String gxPlanNo) {
		return this.getDataBySqlID("FurResult.SingleFurResultQuery",
				new Object[] { JudgeStoveNo, proPlanId, gxPlanNo });
	}

	/**
	 * 查询待装炉计划
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getINFurPlan(String PlineCode, String LotNo,
			String JudgeStoveNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FurResult.INFurPlanQuery");
		String sql1 = "";
		if (!LotNo.equals("")) {
			sql1 += " AND t.lot_no LIKE '%" + LotNo + "%'";
		}
		if (!JudgeStoveNo.equals("")) {
			sql1 += " AND t.judge_stove_no LIKE '%" + JudgeStoveNo + "%'";
		}
		sql = sql.replace("#something1#", sql1).replace("#PLINE_CODE#",
				PlineCode);
		return this.getData(sql);
	}

	/**
	 * 查询炉计划状态
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getIsStartStats(String JudgeStoveNo,
			String ResultNo, String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("FurResult.getIsStartStats", new Object[] {
				JudgeStoveNo, ResultNo, proPlanId, gxPlanNo });
	}

	/**
	 * 查询待出炉计划
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getINFurOUTPlan(String PlineCode, String LotNo,
			String JudgeStoveNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FurResult.INFurOUTPlanQuery");
		String sql1 = "";
		if (!LotNo.equals("")) {
			sql1 += " AND a.lot_no LIKE '%" + LotNo + "%'";
		}
		if (!JudgeStoveNo.equals("")) {
			sql1 += " AND a.judge_stove_no LIKE '%" + JudgeStoveNo + "%'";
		}
		sql = sql.replace("#something1#", sql1).replace("#PLINE_CODE#",
				PlineCode);
		return this.getData(sql);
	}

	/**
	 * 查询出炉计划
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getOUTFurPlan(String PlineCode, String LotNo,
			String JudgeStoveNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FurResult.OUTFurPlanQuery");
		String sql1 = "";
		if (!LotNo.equals("")) {
			sql1 += " AND a.lot_no LIKE '%" + LotNo + "%'";
		}
		if (!JudgeStoveNo.equals("")) {
			sql1 += " AND a.judge_stove_no LIKE '%" + JudgeStoveNo + "%'";
		}
		sql = sql.replace("#something1#", sql1).replace("#PLINE_CODE#",
				PlineCode);
		return this.getData(sql);
	}

	/**
	 * 查询炉次最后一支入炉时间
	 * 
	 * @param JudgeStoveNo
	 *            炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getInFurTime(String JudgeStoveNo) {
		return this.getDataBySqlID("FurResult.InFurTimeQuery",
				new Object[] { JudgeStoveNo });
	}

	/**
	 * 查询炉内未装炉完成计划
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getINFurNonEndIN(String Flag, String PlineCode) {
		return this.getDataBySqlID("FurResult.InFurNonEndIn", new Object[] {
				Flag, PlineCode });
	}

	/**
	 * 查询炉次炉内状态
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getInFurStatus(String JudgeStoveNo) {
		return this.getDataBySqlID("FurResult.InFurStatus",
				new Object[] { JudgeStoveNo });
	}

	/**
	 * 查询上料管号队列
	 * 
	 * @param JudgeStoveNo
	 *            --炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getFurRackSeq(String JudgeStoveNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("FurResult.FurRackSeqQuery", new Object[] {
				JudgeStoveNo, proPlanId, gxPlanNo });
	}

	/**
	 * 烧损率
	 * 
	 * @param PlineCode
	 *            --产线
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBurnningRate(String PlineCode,String ResultNo) {
		return this.getDataBySqlID("FurResult.getBurnningRate",
				new Object[] { PlineCode,ResultNo,ResultNo, PlineCode});
	}

	/**
	 * 开始入炉 - 修改主计划状态 ，新增加热炉实绩信息
	 * 
	 * @param FurResultEntity
	 *            加热炉实绩实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject startInFur(
			com.steering.mes.zgmil.entity.FurResultEntity FurResultEntity) {
		ArrayList<String> sqllist = new ArrayList<String>();

		String sql = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateMainPlanStatue");
		String sql1 = CoreXmlSqlParsersFactory.getSql("FurResult.AddFurResult");
		sql1 = EntityTool.fillSqlByEntity(sql1, FurResultEntity);
		sql = EntityTool.fillSqlByEntity(sql, FurResultEntity);
		sqllist.add(sql);
		sqllist.add(sql1);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}

	/**
	 * 保存加热炉实绩信息
	 * 
	 * @param FurResultEntity
	 *            加热炉实 绩实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject saveFurResult(
			com.steering.mes.zgmil.entity.FurResultEntity FurResultEntity) {
		ArrayList<String> sqllist = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateFurResult");
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateEndTimeForRack");
		sql = EntityTool.fillSqlByEntity(sql, FurResultEntity);
		sql1 = EntityTool.fillSqlByEntity(sql1, FurResultEntity);
		sqllist.add(sql);
		sqllist.add(sql1);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}

	/**
	 * 结炉
	 * 
	 * @param FurResultEntity
	 *            加热炉实绩实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject nodeFurResult(
			com.steering.mes.zgmil.entity.FurResultEntity FurResultEntity) {

		ArrayList<String> sqllist = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateFurResultForNode");
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateMainPlanNodeStatue");
		String sql2 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateFurResultEndTime");
		String sql3 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateInChangeShit");
		String sql4 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateOutChangeShit");
		String sql5 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateEndTime");
		String updateProcess = CoreXmlSqlParsersFactory
				.getSql("FrmInOnlineStore.insertMilProcess2");// 更新进程表mil_processes数据

		sql = EntityTool.fillSqlByEntity(sql, FurResultEntity);
		sql1 = EntityTool.fillSqlByEntity(sql1, FurResultEntity);
		sql2 = EntityTool.fillSqlByEntity(sql2, FurResultEntity);
		sql3 = EntityTool.fillSqlByEntity(sql3, FurResultEntity);
		sql4 = EntityTool.fillSqlByEntity(sql4, FurResultEntity);
		sql5 = EntityTool.fillSqlByEntity(sql5, FurResultEntity);
		updateProcess = EntityTool.fillSqlByEntity(updateProcess, FurResultEntity);

		sqllist.add(sql);
		sqllist.add(sql1);
		sqllist.add(sql2);
		sqllist.add(sql3);
		sqllist.add(sql4);
		// sqllist.add(sql5);
		sqllist.add(updateProcess);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);

	}

	/***
	 * 查询样管信息
	 * 
	 * @return
	 */
	public CoreReturnObject selectPortBacthSlample(String matNo) {
		return this.getDataBySqlID("FurResult.selectPortBacthSlample",
				new Object[] { matNo });
	}

	/***
	 * 结炉赋值材料号到剔废表中
	 * 
	 * @param JudgeStoveNo
	 * @param ProcessNo
	 * @return
	 */
	public CoreReturnObject updateMilSlab(String JudgeStoveNo,
			String ProcessNo, String proPlanId, String gxPlanNo) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		// String sql = "";
		// ResultSet rs = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateMilSlabSql = CoreXmlSqlParsersFactory
					.getSql("FurResult.updateMilSlab");
			if (ProcessNo.equals("3")) {
				updateMilSlabSql = updateMilSlabSql.replace("#RACK#",
						"MIL_PERFORATION_RACK");
			} else if (ProcessNo.equals("4")) {
				updateMilSlabSql = updateMilSlabSql.replace("#RACK#",
						"MIL_ROLLING_RACK");
			} else if (ProcessNo.equals("5")) {
				updateMilSlabSql = updateMilSlabSql.replace("#RACK#",
						"MIL_REFUR_RACK");
			} else if (ProcessNo.equals("6")) {
				updateMilSlabSql = updateMilSlabSql.replace("#RACK#",
						"MIL_SIZING_RACK");
			} else if (ProcessNo.equals("2")) {
				updateMilSlabSql = updateMilSlabSql.replace("#RACK#",
						"MIL_FUR_RACK").replace("T.STATUS = '1'",
						"T.STATUS IN ('0','1','2','3')");
			}
			PreparedStatement updateMilSlabPsta = connection
					.prepareStatement(updateMilSlabSql);
			updateMilSlabPsta.setString(1, JudgeStoveNo);
			updateMilSlabPsta.setString(2, proPlanId);
			updateMilSlabPsta.setString(3, gxPlanNo);
			updateMilSlabPsta.setString(4, ProcessNo);
			updateMilSlabPsta.addBatch();

			updateMilSlabPsta.executeBatch();
			cro.setV_errMsg("修改成功!");
			connection.commit();
		} 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 JudgeStoveNo
	 * @param ProcessNo
	 * @param opProcessNo
	 * @return
	 */
	public CoreReturnObject updateProcess(String JudgeStoveNo,
			String ProcessNo, String opProcessNo, String proPlanId,
			String gxPlanNo, String lotNo, String plineCode) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		String sql = "";
		ResultSet rs = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String m_matNo = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			sql = CoreXmlSqlParsersFactory.getSql("updateProcess.selectScrap");
			psta = connection.prepareStatement(sql);
			psta.setString(1, JudgeStoveNo);
			psta.setString(2, ProcessNo);
			psta.setString(3, proPlanId);
			psta.setString(4, gxPlanNo);
			rs = psta.executeQuery();
			List<MilSlabScrapSeqEntity> list = EntityTool.getEntitysByRs(rs,
					MilSlabScrapSeqEntity.class);

			rs.close();
			psta.close();
			for (int i = 0; i < list.size(); i++) {
				MilSlabScrapSeqEntity milSlabSeq = list.get(i);
				String updateProcessSql = CoreXmlSqlParsersFactory
						.getSql("updateProcess.UpdateFlag");// 结炉剔除进程表中数据
				updateProcessSql = updateProcessSql.replace("#OP_PROCESS#",
						opProcessNo);
				updateProcessSql = EntityTool.fillSqlByEntity(updateProcessSql,
						milSlabSeq);
				PreparedStatement updateProcessPsta = connection
						.prepareStatement(updateProcessSql);
				updateProcessPsta.executeUpdate();
				updateProcessPsta.close();
				// updateProcessPsta.setString(1, milSlabSeq.getJudgeStoveNo());
				// updateProcessPsta.setString(2, opProcessNo);
				// updateProcessPsta.setString(3, milSlabSeq.getSeq());
				// updateProcessPsta.setString(4, opProcessNo);
				// updateProcessPsta.setString(5, milSlabSeq.getJudgeStoveNo());
				// updateProcessPsta.addBatch();

			}
			// updateProcessPsta.executeBatch();

			cro.setV_errMsg("修改成功!");
			connection.commit();

		} 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 MilScrapSlab
	 *            管坯剔除实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject scrapFurMat(MilSlabScrapSeqEntity MilScrapSlab) {
		ArrayList<String> sqllist = new ArrayList<String>();
		// 新增剔除管号序列
		String sql = CoreXmlSqlParsersFactory
				.getSql("FeedResult.AddScrapSlabSeq");
		// 修改加热炉表物料状态
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateFurStatue");
		// 修改加热炉实绩表剔除支数
		String sql2 = CoreXmlSqlParsersFactory
				.getSql("FurResult.UpdateFurScrapNum");

		sql = EntityTool.fillSqlByEntity(sql, MilScrapSlab);
		sql1 = EntityTool.fillSqlByEntity(sql1, MilScrapSlab);
		sql2 = EntityTool.fillSqlByEntity(sql2, MilScrapSlab);
		sqllist.add(sql);
		sqllist.add(sql1);
		sqllist.add(sql2);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}

	/**
	 * 修改出入炉时间
	 * 
	 * @param FurResultEntity
	 *            加热炉实绩实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject updateFurTime(String JudgeStoveNo, String Shift,
			String Group, String temp, Integer Flag) {

		switch (Flag) {
		case 0:
			return this.setDataBySqlID("FurResult.UpdateFirstInFurTime",
					new Object[] { JudgeStoveNo });

		case 1:
			return this.setDataBySqlID("FurResult.UpdateLastInFurTime",
					new Object[] { Shift, Group, JudgeStoveNo });

		case 2:
			return this.setDataBySqlID("FurResult.UpdateFirstOutFurTime",
					new Object[] { Shift, Group, temp, JudgeStoveNo });

		default:
			return this.setDataBySqlID("FurResult.UpdateLastOutFurTime",
					new Object[] { JudgeStoveNo });

		}

	}

	/**
	 * 最后一直入炉时添加班次班组入炉时间
	 * 
	 * @param
	 * @return CoreReturnObject
	 */

	public CoreReturnObject SetInFur(String JudgeStvoeNo, String ResultNo,
			String Shift, String Group, String User) {
		return this.setDataBySqlID("FurResult.UpdateInFurShift", new Object[] {
				Shift, Group, User, JudgeStvoeNo, ResultNo });

	}

	/**
	 * 查询已出炉实绩
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetSingleFurNodeResult(String PlineCode,
			String Shift, String Group, String StartTime, String EndTime) {
		return this.getDataBySqlID("FurResult.SingleFurNodeResult",
				new Object[] { PlineCode, Shift, Group, StartTime, EndTime });
	}

	/**
	 * 结炉后清空材料号为空的班次班组
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject ClearShitAfterNode(String JudgeStoveNo,
			String ResultNo, String proPlanId, String gxPlanNo) {
		ArrayList<String> sqllist = new ArrayList<String>();

		String sql = "UPDATE MIL_FUR_Rack SET IN_SHIFT = '',IN_GROUP = '',IN_USER = '',IN_FUR_TIME = '',COL_SHIFT = '',COL_GROUP = '',COL_USER = '',OUT_FUR_TIME = ''";
		sql += "WHERE	JUDGE_STOVE_NO = '" + JudgeStoveNo
				+ "' 	AND RESULT_NO = '" + ResultNo
				+ "' AND NVL(PRO_PLAN_ID,'/') = NVL('" + proPlanId
				+ "','/') AND NVL(GX_PLAN_NO,'/') = NVL('" + gxPlanNo
				+ "','/')" + "	AND MAT_NO IS NULL";

		sqllist.add(sql);

		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}

	/**
	 * 结炉时剔除剔料序号表对应管坯
	 * 
	 * @param JudgeStoveNo
	 *            上料计划实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject scrapNodeMat(String JudgeStoveNo) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		PreparedStatement psta1 = null;
		try {
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);

			String sql = "";

			ResultSet rs = null;
			ResultSet rs1 = null;
			ResultSet rs3 = null;
			ArrayList<int[]> seqlist = new ArrayList<int[]>();
			sql = CoreXmlSqlParsersFactory.getSql("FeedResult.SinglePlanQuery");
			psta = conn.prepareStatement(sql);
			psta.setString(1, JudgeStoveNo);
			rs = psta.executeQuery();
			while (rs.next()) {
				int SawNum = rs.getInt(3); // 分切数
				int DBNum = rs.getInt(8); // 倍尺数
				sql = CoreXmlSqlParsersFactory
						.getSql("FeedResult.ScrapSlabSeqQuery");
				psta1 = conn.prepareStatement(sql);
				psta1.setString(1, JudgeStoveNo);
				psta1.setString(2, "2"); // 加热炉
				rs1 = psta1.executeQuery();

				int Seq = 0;
				String ScrapTime = "";
				String Remark = "";
				String ScrapUser = "";
				String ScrapGroup = "";
				String ScrapShift = "";
				String TableName = "";
				String MatNo = "";
				String[] TableNameList = new String[] {
						"MIL_PERFORATION_HISTORY", "MIL_ROLLING_HISTORY",
						"MIL_REFUR_HISTORY", "MIL_SIZING_HISTORY",
						"MIL_SAW_HISTORY", "MIL_STRAIGH_HISTORY",
						"MIL_TESTING_HISTORY" };
				// 切前剔除
				while (rs1.next()) {
					Seq = rs1.getInt(1);
					ScrapTime = rs1.getString(2);
					Remark = rs1.getString(4);
					ScrapUser = rs1.getString(5);
					ScrapShift = rs1.getString(6);
					ScrapGroup = rs1.getString(7);
					// 查询剔除序号对应的管号
					sql = " SELECT MAT_NO FROM "
							+ " (SELECT ROWNUM AS ROW_NO ,SEQ,MAT_NO FROM MIL_FUR_HISTORY T WHERE DELETE_FLAG ='0' "
							+ " AND T.JUDGE_STOVE_NO = '" + JudgeStoveNo
							+ "' ORDER BY SEQ)WHERE row_no = " + Seq;
					psta = conn.prepareStatement(sql);
					rs3 = psta.executeQuery();
					while (rs3.next()) {
						MatNo = rs3.getString(1);
					}
					psta.close();

					// 修改上料历史表对应序号的管坯 为 1-剔除
					sql = CoreXmlSqlParsersFactory
							.getSql("FurResult.UpdateFurHistory");
					psta = conn.prepareStatement(sql);
					psta.setInt(1, Seq);
					psta.setString(2, JudgeStoveNo);
					int UpdateFeedHistoryCount = psta.executeUpdate();
					psta.close();

					// 新增坯料剔除表
					sql = CoreXmlSqlParsersFactory
							.getSql("FeedResult.AddScrapSlab");
					psta = conn.prepareStatement(sql);
					psta.setString(1, JudgeStoveNo);
					psta.setInt(2, Seq);
					psta.setString(3, "2");
					psta.setString(4, MatNo);
					psta.setString(5, "0");
					psta.setString(6, ScrapUser);
					psta.setString(7, ScrapTime);
					psta.setString(8, ScrapShift);
					psta.setString(9, ScrapGroup);
					int AddScrapSlabCount = psta.executeUpdate();
					psta.close();

					// 修改历史表对应序号管坯状态为 1-剔除

					for (int i = 0; i < TableNameList.length; i++) {
						TableName = TableNameList[i];

						sql = "UPDATE "
								+ TableName
								+ " SET STATUS = '1'  WHERE JUDGE_STOVE_NO ='"
								+ JudgeStoveNo
								+ "'AND "
								+ "SEQ IN (SELECT SEQ FROM (SELECT ROWNUM AS ROW_NO , SEQ FROM "
								+ TableName + " T WHERE  T.DELETE_FLAG = '0'"
								+ "  AND T.JUDGE_STOVE_NO ='" + JudgeStoveNo
								+ "' ORDER BY SEQ )WHERE ";

						String sqllistRollUpdate = " ROW_NO in (";

						String sqlWhere = "";
						if (i < 5) {
							sqlWhere = Seq + "" + "))";
							sqllistRollUpdate = sqllistRollUpdate + sqlWhere;
							sql = sql + sqllistRollUpdate;
						}

						else {
							for (int j = 1; j < SawNum; j++) {
								sqlWhere += ((Seq - 1) * SawNum + j) + "" + ",";

							}
							sqlWhere = sqlWhere + ((Seq * SawNum) + "" + "))");
							sqllistRollUpdate = sqllistRollUpdate + sqlWhere;
							sql = sql + sqllistRollUpdate;

						}

						psta = conn.prepareStatement(sql);

						psta.executeUpdate(sql);
						psta.close();
					}

				}

				// 修改上料之后所有工序历史表status=1的管 delete_flag = 1
				for (int i = 0; i < TableNameList.length; i++) {
					TableName = TableNameList[i];
					sql = "update  " + TableName
							+ "  set delete_flag = '1' where status = '1'";
					psta = conn.prepareStatement(sql);
					psta.executeUpdate(sql);
					psta.close();
				}

			}
			// 修改上料历史表状态为0 的坯料状态为2
			conn.commit();
			rs3.close();
			rs1.close();
			psta1.close();
			rs.close();

			conn.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		cro.setResult(0);
		return cro;
	}
	
	/**
	 * 保存第一支出炉温度 时间并更新运输需求起运启时间
	 * @param JudgeStoveNo
	 * @param Shift
	 * @param Group
	 * @param temp
	 * @param Flag
	 * @param id
	 * @return
	 */
	public CoreReturnObject updateFurTimeTms(
			String JudgeStoveNo, String Shift,
			String Group, String temp, Integer Flag, String id) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			
			String sql1 = CoreXmlSqlParsersFactory
					.getSql("FurResult.UpdateFirstOutFurTime");
			PreparedStatement updateFurTimeReslut = connection
					.prepareStatement(sql1);
			updateFurTimeReslut.setString(1, Shift);
			updateFurTimeReslut.setString(2, Group);
			updateFurTimeReslut.setString(3, temp);
			updateFurTimeReslut.setString(4, JudgeStoveNo);
			updateFurTimeReslut.addBatch();
			updateFurTimeReslut.executeBatch();
			

			// 更新运输需求起运启时间
//			TZH 24.4.24 注销物流接口
//			updateTmsDemandMStartTime(connection, connection2, cro,
//					JudgeStoveNo, id);
			if (cro.getV_errMsg().equals("")) {
				cro.setV_errMsg("第一支出炉成功！");
			}
			connection.commit();
//			TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
//					TZH 24.4.24 注销物流接口
//					connection2.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
//			TZH 24.4.24 注销物流接口
//			if (connection2 != null) {
//				try {
//					connection.rollback(); // 回滚事务
//					connection2.rollback(); // 回滚事务
//				} catch (Exception e2) {
//				}
//			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
//				TZH 24.4.24 注销物流接口
//				if (connection2 != null && !connection2.isClosed()) {
//					connection2.close();
//				}
			} catch (Exception e2) {
			}
		}

		return cro;
		
	}
	
	/**
	 * 更新运输需求起运启时间
	 * @param connection
	 * @param connection2
	 * @param core
	 * @param JudgeStoveNo
	 * @param HeatPlanNo
	 * @return
	 * @throws SQLException
	 */
	public CoreReturnObject updateTmsDemandMStartTime(Connection connection,
			Connection connection2, CoreReturnObject core, String JudgeStoveNo,
			String HeatPlanNo) throws SQLException {
		PreparedStatement psta = null;
		ResultSet rs = null;
		String sql = "";
		String startTime = "";// 开始时间
		sql = CoreXmlSqlParsersFactory
				.getSql("FurResult.selectStartTime");// 查询第一支出炉时间
		psta = connection.prepareStatement(sql);
		psta.setString(1, JudgeStoveNo.replaceFirst("-.*", ""));
		psta.setString(2, HeatPlanNo);
		rs = psta.executeQuery();
		if (rs.next()) {
			startTime = rs.getString("START_TIME");
		}
		rs.close();
		psta.close();
		String updateOff = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.UpdateTmsDemandMStartTime");// 更新运输需求起运启时间
		PreparedStatement pstaupdateOff = connection2
				.prepareStatement(updateOff);
		pstaupdateOff.setString(1, JudgeStoveNo);
		pstaupdateOff.setString(2, HeatPlanNo);
		pstaupdateOff.setString(3, startTime);
		pstaupdateOff.addBatch();
		pstaupdateOff.executeBatch();
		return core;
	}

}
