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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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.common.StringTool;
import com.steering.mes.zgmil.entity.MilControlLogEntity;

/**
 * 连轧脱管业务类
 * 
 * @author Administrator
 * 
 */
public class FrmRollingRPipeResult extends DBCommon {

	/**
	 * 查询简单作业计划
	 * 
	 * @param MilPlan
	 *            作业计划实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getSingleMainPlanForHeatNo(
			com.steering.mes.zgmil.entity.MilRollingRackEntity rs) {
		return this.getDataBySqlID("FrmRollingRPipeResult.PlanQueryForHeatNo",
				new Object[] { rs.getJudgeStoveNo() });
	}

	/***
	 * 查询连轧跟踪表信息
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject rackQueryForHeatNo(String HeatNo, String proPlanId,
			String gxPlanNo) {
		return this.getDataBySqlID("FrmRollingRPipeResult.RackQueryForHeatNo",
				new Object[] { HeatNo, proPlanId, gxPlanNo });
	}

	/***
	 * 查询单倍重、烧损率
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject getDBHeightBRate(String HeatNo, String ResultNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("FrmRollingRPipeResult.getDBHeightBRate",
				new Object[] { HeatNo, ResultNo, proPlanId, gxPlanNo });
	}

	/**
	 * 修改主计划状态开始连轧 新增连轧实绩
	 */

	public CoreReturnObject insertResultForMainPlan(
			com.steering.mes.zgmil.entity.MilRollingResultEntity rs) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		PreparedStatement psta = null;
		String sql = "";
		int acount = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			String sql1 = CoreXmlSqlParsersFactory
					.getSql("FrmRollingRPipeResult.updateMailPlanStatus");
			String sql2 = CoreXmlSqlParsersFactory
					.getSql("FrmRollingRPipeResult.insertResultForMainPlan");
			String sql3 = CoreXmlSqlParsersFactory
					.getSql("FrmRollingRPipeResult.UpdataResultPROCDATE");//
			sql1 = EntityTool.fillSqlByEntity(sql1, rs);
			sql2 = EntityTool.fillSqlByEntity(sql2, rs);
			sql3 = EntityTool.fillSqlByEntity(sql3, rs);

			PreparedStatement updateFurnaceReslut = connection
					.prepareStatement(sql1);

			PreparedStatement updateStateBack = connection
					.prepareStatement(sql2);

			PreparedStatement deletePortMil = connection.prepareStatement(sql3);

			updateFurnaceReslut.addBatch();
			updateStateBack.addBatch();
			deletePortMil.addBatch();
			updateFurnaceReslut.executeBatch();
			updateStateBack.executeBatch();
			deletePortMil.executeBatch();

			// 更新运输需求起运启时间
			/*
			 * updateTmsDemandMStartTime(connection, connection2, cro,
			 * rs.getJudgeStoveNo(), rs.getResultNo()); if
			 * (cro.getV_errMsg().equals("")) { cro.setV_errMsg("开始成功！"); }
			 */
			cro.setV_errMsg("开始成功！");
			connection.commit();
			// 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;

	}

	/**
	 * 插入连轧调度日志
	 * 
	 */

	public CoreReturnObject insertContralLog(String JudgeStoveNo, String LotNo,
			String Shift, String Group, String UserID, String UserName,
			String ChangeShiftFlag, String proPlanId, String gxPlanNo) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		PreparedStatement psta1 = null;
		PreparedStatement psta2 = null;
		try {
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);
			ResultSet rs = null;

			String STOVE_NO = "";
			String CONTRACT_NO = "";
			String IMP_PROCESS = "";
			String PLINE_CODE = "";
			String PRODUCER = "";

			String FINISH_USER = "";
			String TARGET_LIABRARY = "";
			String VARIETIES = "";
			String PIPE_SIZE = "";
			String BUCKLE_TYPE = "";
			String LENGTH_RANGE = "";
			String ROLL_STANDRAD = "";
			String PIPE_SECTION = "";
			String STEELCODE = "";
			String STEELNAME = "";
			String GRADECODE = "";
			String GRADENAME = "";
			String STEEL_STANDRAD = "";

			String sql = CoreXmlSqlParsersFactory
					.getSql("HotCheck.MainPlanForJudgeStoveNo");
			psta = conn.prepareStatement(sql);
			psta.setString(1, JudgeStoveNo);
			psta.setString(2, proPlanId);
			psta.setString(3, gxPlanNo);
			System.out.print(sql);
			rs = psta.executeQuery();
			while (rs.next()) {
				STOVE_NO = rs.getString(2);
				CONTRACT_NO = rs.getString(5);
				IMP_PROCESS = rs.getString(6);
				PLINE_CODE = rs.getString(7);
				PRODUCER = rs.getString(8);

				FINISH_USER = rs.getString(15);
				TARGET_LIABRARY = rs.getString(16);
				VARIETIES = rs.getString(17);
				PIPE_SIZE = rs.getString(18);
				BUCKLE_TYPE = rs.getString(19);
				LENGTH_RANGE = rs.getString(20);
				ROLL_STANDRAD = rs.getString(21);
				PIPE_SECTION = rs.getString(22);
				STEELCODE = rs.getString(23);
				STEELNAME = rs.getString(24);
				GRADECODE = rs.getString(25);
				GRADENAME = rs.getString(26);
				STEEL_STANDRAD = rs.getString(27);

			}
			String sql1 = " INSERT INTO MIL_CONTROL_LOG(LOGS_BEG,PLINE_CODE,STOVE_NO,JUDGE_STOVE_NO,LOT_NO,CONTRACT_NO,IMP_PROCESS,PRODUCER,FINISH_USER ";
			sql1 += " ,TARGET_LIABRARY,VARIETIES,PIPE_SIZE,BUCKLE_TYPE,LENGTH_RANGE,";
			sql1 += " ROLL_STANDRAD, PIPE_SECTION,STEELCODE,STEELNAME,GRADECODE,GRADENAME,STEEL_STANDRAD,PRO_TYPE,USER_DATE,";
			sql1 += " PRO_BC,PRO_BZ,USER_ID,USER_NAME,PRO_PLAN_ID,GX_PLAN_NO,CHANGE_SHIFT_FLAG )";

			sql1 += " VALUES( to_char(sysdate,'YYYYMMDDhh24mi'),'" + PLINE_CODE
					+ "','" + STOVE_NO + "','" + JudgeStoveNo + "','" + LotNo
					+ "','" + CONTRACT_NO + "','" + IMP_PROCESS + "','"
					+ PRODUCER + "',";
			sql1 += " '" + FINISH_USER + "','" + TARGET_LIABRARY + "','"
					+ VARIETIES + "','" + PIPE_SIZE + "','" + BUCKLE_TYPE
					+ "','" + LENGTH_RANGE + "',";
			sql1 += " '" + ROLL_STANDRAD + "','" + PIPE_SECTION + "','"
					+ STEELCODE + "','" + STEELNAME + "','" + GRADECODE + "','"
					+ GRADENAME + "','" + STEEL_STANDRAD + "','1',sysdate,";
			sql1 += "'" + Shift + "','" + Group + "','" + UserID + "','"
					+ UserName + "','" + proPlanId + "','" + gxPlanNo + "','"
					+ ChangeShiftFlag + "')";

			psta1 = conn.prepareStatement(sql1);
			psta1.executeUpdate();
			psta1.close();

			
			Date now = new Date();
			SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
			String yearMoth = df.format(now);
			yearMoth = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(PLINE_CODE)); // 改25号结转
			String strSql = CoreXmlSqlParsersFactory
					.getSql("FrmRollingRPipeResult.UpdateMilControlLogDate");
			strSql += " WHERE JUDGE_STOVE_NO = '" + JudgeStoveNo
					+ "' AND NVL(PRO_PLAN_ID,'/') = NVL('" + proPlanId
					+ "','/') AND NVL(GX_PLAN_NO,'/') = NVL('" + gxPlanNo
					+ "','/')";

			psta2 = conn.prepareStatement(strSql);
			psta2.executeUpdate();
			psta2.close();

			conn.commit();
			rs.close();
			psta.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;
	}

	/**
	 * 结炉修改连轧调度日志
	 * 
	 */

	public CoreReturnObject nodeUpdateContralLog(
			MilControlLogEntity MilControlLogEntity) {
		String JudgeStoveNo = MilControlLogEntity.getJudgeStoveNo();
		String LotNo = MilControlLogEntity.getLotNo();
		String Shift = MilControlLogEntity.getProBc();
		String Group = MilControlLogEntity.getProBz();
		String UserID = MilControlLogEntity.getUserId();
		String UserName = MilControlLogEntity.getUserName();
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		PreparedStatement psta1 = null;
		try {
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);
			ResultSet rs = null;

			String QaNum = "";
			String QaTon = "";
			String ScrapNum = "";
			String ScrapTon = "";

			String sql = "SELECT QA_TON,QUALIFIED_NUM,SCRAP_NUM FROM MIL_ROLLING_RESULT WHERE JUDGE_STOVE_NO = '"
					+ JudgeStoveNo + "' and LOT_NO  = '" + LotNo + "'";
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			while (rs.next()) {

				QaTon = rs.getString(1);
				QaNum = rs.getString(2);
				ScrapNum = rs.getString(3);
				String sql1 = " UPDATE MIL_CONTROL_LOG set LOGS_END = to_char(sysdate,'YYYYMMDDhh24mi'),PRO_BC = '"
						+ Shift
						+ "',PRO_BZ='"
						+ Group
						+ "',USER_ID = '"
						+ UserID
						+ "',USER_NAME = '"
						+ UserName
						+ "',QUALIFIED_NUM ='"
						+ QaNum
						+ "',QUALIFIED_WT = '"
						+ QaTon + "',SCRAP_NUM = '" + ScrapNum + "'  ";
				sql1 += "  where JUDGE_STOVE_NO = '" + JudgeStoveNo
						+ "'  and LOT_NO  = '" + LotNo + "'";

				psta1 = conn.prepareStatement(sql1);
				psta1.executeUpdate();
			}
			psta1.close();
			conn.commit();
			rs.close();
			psta.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 rs
	 * @return
	 */
	public CoreReturnObject changeGroupUpdateContralLog(
			MilControlLogEntity MilControlLogEntity) {
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.changeGroupUpdateContralLog");
		sql = EntityTool.fillSqlByEntity(sql, MilControlLogEntity);
		parmlist.add(sql);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/**
	 * 结炉修改连轧调度日志表
	 * 
	 * @param MilControlLogEntity
	 * @return
	 */
	public CoreReturnObject nodeChange(MilControlLogEntity MilControlLogEntity) {
		setNodeFlag(MilControlLogEntity.getJudgeStoveNo(), "1");
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.nodeChange");
		sql = EntityTool.fillSqlByEntity(sql, MilControlLogEntity);
		parmlist.add(sql);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/***
	 * 开始与结炉的班次班组不同时修改连轧调度日志表
	 * 
	 * @param MilControlLogEntity
	 * @return
	 */
	public CoreReturnObject changeShiftGroupUpdateContralLog(
			MilControlLogEntity MilControlLogEntity) {
		setNodeFlag(MilControlLogEntity.getJudgeStoveNo(), "1");
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.changeShiftGroupUpdateContralLog");
		sql = EntityTool.fillSqlByEntity(sql, MilControlLogEntity);
		parmlist.add(sql);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/**
	 * 修改调度结炉标识
	 * 
	 * @param JudgeStoveNo
	 * @param flag
	 *            (0.未结炉，1.结炉)
	 */
	public void setNodeFlag(String JudgeStoveNo, String flag) {
		this.setDataBySqlID("FrmRollingRPipeResult.setNodeFlag", new Object[] {
				flag, JudgeStoveNo });

	}

	/**
	 * 接班取消修改连轧调度日志表
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject changeGroupCancelUpdateContralLog(String Shift,
			String Group, String ShiftA, String GroupA, String JudgeStoveNo,
			String LotNo) {
		ArrayList<String> parmlist = new ArrayList<String>();
		// 1- 删除下道班次调度日志
		String sql = "DELETE MIL_CONTROL_LOG WHERE LOT_NO = '" + LotNo
				+ "' AND JUDGE_STOVE_NO = '" + JudgeStoveNo
				+ "' and LOGS_END is null";
		// 2- 清空本到班次日志 结束时间 合废支吨
		String sql1 = "UPDATE MIL_CONTROL_LOG SET LOGS_END = '',QUALIFIED_NUM ='',QUALIFIED_WT='',SCRAP_NUM='',SCRAP_WT ='',CHANGE_SHIFT_FLAG='0' WHERE LOGS_END =  ";
		sql1 += "( SELECT MAX (LOGS_END) FROM MIL_CONTROL_LOG  WHERE LOT_NO = '"
				+ LotNo
				+ "' AND JUDGE_STOVE_NO = '"
				+ JudgeStoveNo
				+ "' and PRO_BC = '"
				+ Shift
				+ "' and PRO_BZ = '"
				+ Group
				+ "' ) and ";
		sql1 += " LOT_NO = '" + LotNo + "' AND JUDGE_STOVE_NO = '"
				+ JudgeStoveNo + "' and PRO_BC = '" + Shift
				+ "' and PRO_BZ = '" + Group + "'";
		parmlist.add(sql);
		parmlist.add(sql1);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	public CoreReturnObject getControlStatus(String heatno, String lotno,
			String shift, String group, String proPlanId, String gxPlanNo) {
		String sql = "Select t.CONFIRM_FLAG   from MIL_CONTROL_LOG t WHERE  t.judge_stove_no=  '"
				+ heatno
				+ "' and t.LOT_NO = '"
				+ lotno
				+ "' and t.PRO_BC = '"
				+ shift
				+ "' and t.PRO_BZ = '"
				+ group
				+ "' AND NVL(PRO_PLAN_ID,'/') = NVL('"
				+ proPlanId
				+ "','/') AND NVL(GX_PLAN_NO,'/') = NVL('"
				+ gxPlanNo
				+ "','/')";

		return this.getData(sql);
	}

	/***
	 * 查询连轧实绩信息
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject resultQuery(String heatNo, String proPlanId,
			String gxPlanNo) {
		return this.getDataBySqlID("FrmRollingRPipeResult.resultQuery",
				new Object[] { heatNo, proPlanId, gxPlanNo });
	}

	/***
	 * 查询最大有效管序号
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject getMaxSeq(String heatNo, String ResultNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("FrmRollingRPipeResult.getMaxSeq",
				new Object[] { heatNo, ResultNo, proPlanId, gxPlanNo });
	}

	/**
	 * 保存连轧实绩
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject resultSave(
			com.steering.mes.zgmil.entity.MilRollingResultEntity rs, String Flag) {
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = "";
		String sql1 = "";
		String sql2 = "";
		String factoryId = rs.getPlineCode();
		if (factoryId.equals("0")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmRollingRPipeResult.resultSave");
		} else {
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmRollingRPipeResult.resultSave1");
		}
		sql = EntityTool.fillSqlByEntity(sql, rs);
		sql1 = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.SaveRackEndTime");
		sql1 = EntityTool.fillSqlByEntity(sql1, rs);
		parmlist.add(sql);
		parmlist.add(sql1);
		if (Flag.equals("0")) {
			// 保存连轧调度开始结束时间
			sql2 = CoreXmlSqlParsersFactory
					.getSql("FrmRollingRPipeResult.SaveContralLogTime");
			sql2 = EntityTool.fillSqlByEntity(sql2, rs);
			parmlist.add(sql2);
		}

		// 修改生产年月
		String sql3 = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.UpdataResultPROCDATE");
		sql3 = EntityTool.fillSqlByEntity(sql3, rs);
		parmlist.add(sql3);

		String sql4 = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.UpdataControlPROCDATE");
		sql4 = EntityTool.fillSqlByEntity(sql4, rs);
		parmlist.add(sql4);

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

	/***
	 * 查询实绩表轧废支数
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject scrapNumQuery(
			com.steering.mes.zgmil.entity.MilRollingResultEntity rs) {
		return this.getDataBySqlID("FrmRollingRPipeResult.scrapNumQuery",
				new Object[] { rs.getJudgeStoveNo() });
	}

	// 连轧开始回退 删除调度日志 Flag 0- 开始回退 1-结炉回退
	public CoreReturnObject DeleteControlLog(String JudgeStoveNo, String LotNo,
			Integer Flag, String proPlanId, String gxPlanNo) {
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = "";

		if (Flag.equals(0)) {
			sql = "delete MIL_CONTROL_LOG where JUDGE_STOVE_NO = '"
					+ JudgeStoveNo + "' and LOT_NO = '" + LotNo
					+ "' AND NVL(PRO_PLAN_ID,'/') = NVL('" + proPlanId
					+ "','/') AND NVL(GX_PLAN_NO,'/') = NVL('" + gxPlanNo
					+ "','/')";
			sql += " AND Confirm_Flag = '0'";
		}
		// 连轧结炉回退 清空调度结炉信息
		else {
			setNodeFlag(JudgeStoveNo, "0");
			sql = "UPDATE MIL_CONTROL_LOG set LOGS_END = '',QUALIFIED_NUM ='',QUALIFIED_WT = '',SCRAP_NUM = ''  ";
			sql += "  where JUDGE_STOVE_NO = '"
					+ JudgeStoveNo
					+ "' AND NVL(PRO_PLAN_ID,'/') = NVL('"
					+ proPlanId
					+ "','/') AND NVL(GX_PLAN_NO,'/') = NVL('"
					+ gxPlanNo
					+ "','/') "
					+ " AND NVL(LOGS_END,'1')=(SELECT MAX(NVL(LOGS_END,'1')) FROM MIL_CONTROL_LOG WHERE JUDGE_STOVE_NO ='"
					+ JudgeStoveNo + "' AND NVL(PRO_PLAN_ID,'/') = NVL('"
					+ proPlanId + "','/') AND NVL(GX_PLAN_NO,'/') = NVL('"
					+ gxPlanNo + "','/'))";
		}
		parmlist.add(sql);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/**
	 * 剔除毛管 修改轧废支数
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject rackDelete(
			com.steering.mes.zgmil.entity.MilRollingRackEntity rs) {
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = "";
		String strSql = "";

		sql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.rackDelete");
		strSql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.resultScrapNumUpdate");
		sql = EntityTool.fillSqlByEntity(sql, rs);
		strSql = EntityTool.fillSqlByEntity(strSql, rs);
		parmlist.add(sql);
		parmlist.add(strSql);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/**
	 * 连轧结炉
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject endStoveForMainPlan(
			com.steering.mes.zgmil.entity.MilRollingResultEntity rs,
			MilControlLogEntity MilControlLogEntity) {
		setNodeFlag(MilControlLogEntity.getJudgeStoveNo(), "1");
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = "";
		String strSql = "";
		String strSql1 = "";
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.endStoveForMainPlan");
		strSql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.resultSaveForNode");
		/*
		 * String factoryId = rs.getPlineCode(); if (factoryId.equals("0")) {
		 * strSql = CoreXmlSqlParsersFactory
		 * .getSql("FrmRollingRPipeResult.resultSaveForNode"); } else { strSql =
		 * CoreXmlSqlParsersFactory
		 * .getSql("FrmRollingRPipeResult.resultSaveForNode1"); }
		 */
		strSql1 = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.endStoveForResult");
		String strSql2 = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.endStoveForRack");
		// 交接班时上个班的结束时间为空时，补齐时间
		String strSql3 = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.endStoveForRackEndTime");
		// 修改连轧调度日志表
		String strSql4 = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.changeShiftGroupUpdateContralLog");
		// 更新进程表mil_processes数据
		String updateProcess = CoreXmlSqlParsersFactory
				.getSql("FrmInOnlineStore.insertMilProcess2");
		
		Date now = new Date();
		String yearMoth = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(rs.getPlineCode()));// 改25号结转
		MilControlLogEntity.setBalYearMonth(yearMoth);// 改25号结转

		sql = EntityTool.fillSqlByEntity(sql, rs);
		strSql = EntityTool.fillSqlByEntity(strSql, rs);
		strSql1 = EntityTool.fillSqlByEntity(strSql1, rs);
		strSql2 = EntityTool.fillSqlByEntity(strSql2, rs);
		strSql3 = EntityTool.fillSqlByEntity(strSql3, rs);
		strSql4 = EntityTool.fillSqlByEntity(strSql4, MilControlLogEntity);
		updateProcess = EntityTool.fillSqlByEntity(updateProcess, rs);
		parmlist.add(sql);
		parmlist.add(strSql);
		parmlist.add(strSql1);
		parmlist.add(strSql2);
		parmlist.add(strSql3);
		parmlist.add(strSql4);
		parmlist.add(updateProcess);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/***
	 * 新增管列剔除表信息
	 * 
	 * @param scrapSeq
	 * @return
	 */
	public CoreReturnObject insertScrapSlabSeq(
			com.steering.mes.zgmil.entity.MilSlabScrapSeqEntity slabScrapSeq) {
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = "";
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmRollingRPipeResult.insertScrapSlabSeq");
		sql = EntityTool.fillSqlByEntity(sql, slabScrapSeq);
		parmlist.add(sql);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/***
	 * 查询序号剔除表的序号
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject deleteStatusQuery(
			com.steering.mes.zgmil.entity.MilSlabScrapSeqEntity rs) {
		return this.getDataBySqlID("FrmRollingRPipeResult.deleteStatusQuery",
				new Object[] { rs.getJudgeStoveNo() });
	}

	/***
	 * 查询穿孔实绩信息是否结炉
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject perForationResultQuery(String JudgeStoveNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID(
				"FrmRollingRPipeResult.perForationResultQuery", new Object[] {
						JudgeStoveNo, proPlanId, gxPlanNo });
	}

	/***
	 * 查询穿孔跟踪表中毛管是 否被剔除
	 * 
	 * @return
	 */
	public CoreReturnObject perForationResultStaus(String JudgeStoveNo,
			String seq) {
		return this.getDataBySqlID(
				"FrmRollingRPipeResult.perForationResultStaus", new Object[] {
						JudgeStoveNo, seq });
	}

	/***
	 * 查询穿孔跟踪表中指定序号范围内剔除管支数
	 * 
	 * @return
	 */
	public CoreReturnObject getScrapNum(String JudgeStoveNo, String ResultNo,
			Integer StartSeq, Integer EndSeq, String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("FrmRollingRPipeResult.getScrapNum",
				new Object[] { JudgeStoveNo, ResultNo, StartSeq, EndSeq,
						proPlanId, gxPlanNo });
	}

	/***
     * 
     */
	public CoreReturnObject GetRollResultNodeQuery(String PliceCode,
			String Shift, String Group, String StartTime, String EndTime) {
		return this.getDataBySqlID(
				"FrmRollingRPipeResult.GetRollResultNodeQuery", new Object[] {
						PliceCode, Shift, Group, StartTime, EndTime });

	}

	/**
	 * 查询工器具更换实绩
	 * 
	 * @param ProcessNo
	 *            工序号 2-穿孔 3-连轧 5-定径
	 * @return
	 */
	public CoreReturnObject toolChangeQuery(String PliceCode, String Shift,
			String Group, String StartTime, String EndTime) {

		return this.getDataBySqlID("FeedResult.RollToolQuery", new Object[] {
				PliceCode, Shift, Group, StartTime, EndTime });

	}

	/***
	 * 查询交班最大序号的班次班组
	 * 
	 * @param jugeNo
	 * @param resultNo
	 * @return
	 */
	public CoreReturnObject doQueryRackGroup(String jugeNo, String resultNo) {
		return this.getDataBySqlID("FrmRollingRPipeResult.doQueryRackGroup",
				new Object[] { resultNo, jugeNo });
	}

	/**
	 * 查询实绩表中一炉开始的班次班组
	 * 
	 * @param jugeNo
	 * @param resultNo
	 * @return
	 */
	public CoreReturnObject doQueryShiftGroup(String jugeNo, String resultNo) {
		return this.getDataBySqlID("FrmRollingRPipeResult.doQueryShiftGroup",
				new Object[] { resultNo, jugeNo });
	}

	/**
	 * 查询跟踪表待交班管的班次班组
	 * 
	 * @param jugeNo
	 * @param resultNo
	 * @return
	 */
	public CoreReturnObject doQueryRollingRackShiftGroup(String jugeNo,
			String resultNo) {
		/*
		 * String sql = ""; sql =
		 * "select t.col_shift,t.col_group from  MIL_ROLLING_RACK t where t.judge_stove_no = '"
		 * + jugeNo + "' and t.result_no = '" + resultNo +
		 * "' AND T.CHANGE_FLAG = '2'"; return this.getData(sql);
		 */
		return this.getDataBySqlID(
				"FrmRollingRPipeResult.doQueryRollingRackShiftGroup",
				new Object[] { resultNo, jugeNo });
	}

	/**
	 * 设置交班的班次班组
	 * 
	 * @param jugeNo
	 * @param resultNo
	 * @param Seq
	 *            点击交班的序号
	 * @param BeforSeq
	 *            上个班交班点
	 * @param plineCode
	 * @param shift
	 * @param group
	 * @param user
	 * @return
	 */
	public CoreReturnObject doSetShiftGroup(String jugeNo, String resultNo,
			Integer Seq, Integer BeforSeq, String plineCode, String shift,
			String group, String user) {
		ArrayList<String> sqllist = new ArrayList<String>();

		String sql = "";// 交班前清空班次 班组
		String sql1 = "";// 设置交班的班次班组

		sqllist.clear();
		sql = "update MIL_ROLLING_RACK  set COL_SHIFT = ''  , COL_GROUP = ''  ,COL_USER='', END_ROLLING_TIME = '',CHANGE_FLAG = 0 ";
		sql += "where JUDGE_STOVE_NO = '" + jugeNo + "' and RESULT_NO = '"
				+ resultNo + "' and seq != " + Seq + " and seq > " + (BeforSeq)
				+ " ";

		sql1 = "update MIL_ROLLING_RACK t set t.col_shift = '" + shift
				+ "' , t.col_group = '" + group + "',t.COL_USER ='" + user
				+ "'";
		sql1 += " where t.judge_stove_no = '" + jugeNo
				+ "' and t.result_no = '" + resultNo + "'";
		sql1 += " and t.pline_code = '" + plineCode
				+ "' and t.change_flag = '2'";

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

	/**
	 * 交班 1- 修改实绩表交接班支数 2-修改跟踪表班次班组 3-修改跟踪表交班标识
	 * 
	 * @param Seq
	 *            点击交班的序号
	 * @param BeforSeq
	 *            上个班交班点
	 * @param BeforSeqOutFur
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject changeNum(String JudgeStoveNo, String ResultNo,
			Integer Seq, Integer BeforSeq, String PLineCode, String Shift,
			String Class, String User) {

		ArrayList<String> sqllist = new ArrayList<String>();
		// ArrayList<String> sqllist1 = new ArrayList<String>();
		String sql = ""; // 交班前清空班次 班组
		String sql1 = ""; // 修改实绩表交接班支数
		String sql2 = ""; // 修改跟踪表班次班组
		String sql3 = ""; // 修改跟踪表交班标识
		String sql4 = ""; // 修改管号队列开始时间

		sqllist.clear();
		sql = "update MIL_ROLLING_RACK  set COL_SHIFT = ''  , COL_GROUP = ''  ,COL_USER='', END_ROLLING_TIME = '',CHANGE_FLAG = 0 ";
		sql += "where JUDGE_STOVE_NO = '" + JudgeStoveNo
				+ "' and RESULT_NO = '" + ResultNo + "' and seq != " + Seq
				+ " and seq > " + (BeforSeq) + " ";
		// 修改连轧交接班支数
		sql1 = "update  MIL_ROLLING_RESULT  set  CHANGECLASSNUM = " + Seq
				+ " where ";
		sql1 += "JUDGE_STOVE_NO = '" + JudgeStoveNo + "' and RESULT_NO = '"
				+ ResultNo + "'";

		// 修改连轧跟踪表班次班组
		sql2 = "update MIL_ROLLING_RACK  set (COL_SHIFT, COL_GROUP, COL_USER,END_ROLLING_TIME) = ";
		sql2 += "(select COL_SHIFT, COL_GROUP, COL_USER,sysdate from MIL_ROLLING_RACK where CHANGE_FLAG='2' AND JUDGE_STOVE_NO = '"
				+ JudgeStoveNo + "') ";
		sql2 += " where JUDGE_STOVE_NO = '" + JudgeStoveNo
				+ "' and RESULT_NO = '" + ResultNo + "'  and  seq between  "
				+ (BeforSeq + 1) + " and " + Seq + "";

		sql3 = "update MIL_ROLLING_RACK set  CHANGE_FLAG = '1' where JUDGE_STOVE_NO = '"
				+ JudgeStoveNo
				+ "' and RESULT_NO = '"
				+ ResultNo
				+ "'  and seq =" + Seq;

		// 修改跟踪表开始时间
		sql4 = " update MIL_ROLLING_RACK set START_ROLLING_TIME = sysdate where JUDGE_STOVE_NO = '"
				+ JudgeStoveNo
				+ "' and RESULT_NO = '"
				+ ResultNo
				+ "'  and seq >" + Seq;

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

	/**
	 * 
	 * @param JudgeStoveNo
	 * @param ResultNo
	 * @param PLineCode
	 * @param Shift
	 * @param Group
	 * @return
	 */
	public CoreReturnObject findRollingRack(String JudgeStoveNo,
			String ResultNo, String PLineCode, String Shift, String Group) {
		String sql = "";
		sql = "select t.judge_stove_no from MIL_ROLLING_RACK t where t.judge_stove_no = '"
				+ JudgeStoveNo
				+ "' and t.result_no ='"
				+ ResultNo
				+ "' and t.col_shift ='"
				+ Shift
				+ "' and t.col_group = '"
				+ Group + "' and t.change_flag = '1'";
		return this.getData(sql);
	}

	/**
	 * 交班取消
	 * 
	 * @param JudgeStoveNo
	 * @param ResultNo
	 * @param PLineCode
	 * @param Shift
	 * @param Group
	 * @param process
	 * @param colno
	 * @return
	 */
	public CoreReturnObject ChangBackelse(String JudgeStoveNo, String ResultNo,
			String PLineCode, String Shift, String Group) {
		String sql = "";
		sql = " UPDATE MIL_ROLLING_RACK SET ";
		sql += " CHANGE_FLAG='0',COL_SHIFT = ''  , COL_GROUP = ''  ,COL_USER='',END_ROLLING_TIME='' ";
		sql += "  WHERE JUDGE_STOVE_NO='" + JudgeStoveNo + "' AND RESULT_NO='"
				+ ResultNo + "'";
		if ((Shift.equals("") || Shift == "")
				&& (Group == "" || Group.equals(""))) {
			sql += "  AND CHANGE_FLAG='2'";
		} else {
			sql += " AND COL_SHIFT = '" + Shift + "' AND COL_GROUP = '" + Group
					+ "'";
		}
		return this.setData(sql);
	}

	/**
	 * 更新运输需求起运启时间
	 * 
	 * @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("FrmRollingRPipeResult.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;
	}

}
