package com.steering.mes.zgmil.coup;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;

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

public class FrmReFurResult extends DBCommon {
	/**
	 * 查询简单作业计划
	 * 
	 * @param MilPlan
	 *            作业计划实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getRefurResult(String JudgeStoveNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("FrmReFurResult.getRefurResult",
				new Object[] { JudgeStoveNo, proPlanId, gxPlanNo });

	}

	/**
	 * 查询待装炉计划
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getINFurPlan(String PlineCode, String LotNo,
			String JudgeStoveNo) {
		return this.getDataBySqlID("FrmReFurResult.INFurPlanQuery",
				new Object[] { PlineCode, LotNo, JudgeStoveNo });
	}

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

	/**
	 * 查询待出炉计划
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getINFurOUTPlan(String PlineCode, String LotNo,
			String JudgeStoveNo) {
		return this.getDataBySqlID("FrmReFurResult.INFurOUTPlanQuery",
				new Object[] { PlineCode, LotNo, JudgeStoveNo });
	}

	/**
	 * 查询出炉计划
	 * 
	 * @param
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getOUTFurPlan(String PlineCode, String LotNo,
			String JudgeStoveNo) {
		return this.getDataBySqlID("FrmReFurResult.OUTFurPlanQuery",
				new Object[] { PlineCode, LotNo, JudgeStoveNo });
	}

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

	/**
	 * 修改主计划状态开始入炉 新增再入炉实绩
	 */

	public CoreReturnObject insertResultForMainPlan(
			com.steering.mes.zgmil.entity.MilRefurResultEntity rs) {
		ArrayList<String> parmlist = new ArrayList<String>();

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReFurResult.updateMailPlanStatus");
		String strSql = CoreXmlSqlParsersFactory
				.getSql("FrmReFurResult.insertResultForMainPlan");
		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 rackQueryForHeatNo(
			com.steering.mes.zgmil.entity.MilRefurRackEntity rs) {
		return this.getDataBySqlID("FrmReFurResult.RackQueryForHeatNo",
				new Object[] { rs.getJudgeStoveNo(), rs.getProPlanId(),
						rs.getGxPlanNo() });
	}

	/***
	 * 查询实绩表 再入加热炉剔除支数
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject scrapNumQuery(
			com.steering.mes.zgmil.entity.MilRefurResultEntity rs) {
		return this.getDataBySqlID("FrmReFurResult.scrapNumQuery",
				new Object[] { rs.getJudgeStoveNo(), rs.getProPlanId(),
						rs.getGxPlanNo() });
	}

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

		sql = CoreXmlSqlParsersFactory.getSql("FrmReFurResult.rackDelete");
		strSql = CoreXmlSqlParsersFactory
				.getSql("FrmReFurResult.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 JudgeStoveNo
	 *            炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getInFurTime(String JudgeStoveNo) {
		return this.getDataBySqlID("FurResult.InFurTimeQuery",
				new Object[] { JudgeStoveNo });
	}

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

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

		String sql = "";
		sql += "UPDATE MIL_REFUR_RESULT SET ";
		if (Flag.equals(0)) {
			sql += " FIRST_IN_TIME = sysdate ,FIRST_IN_SHIFT = '" + Shift
					+ "' , FIRST_IN_GROUP = '" + Group + "',FIRST_IN_TEMP = '"
					+ temp + "' ";
		}
		if (Flag.equals(1)) {
			sql += " LAST_IN_TIME = sysdate ,LAST_IN_SHIFT = '" + Shift
					+ "' , LAST_IN_GROUP = '" + Group + "',LAST_IN_TEMP = '"
					+ temp + "' , IS_START = 1 ";
		}
		if (Flag.equals(2)) {
			sql += " FIRST_OUT_TIME = sysdate ,FIRST_OUT_SHIFT = '" + Shift
					+ "' , FIRST_OUT_GROUP = '" + Group
					+ "',FIRST_OUT_TEMP = '" + temp + "' , IS_START = 2";
		}
		// if (Flag.equals(3)) {
		// sql += " LAST_OUT_TIME = sysdate ";
		// }
		sql += "  WHERE JUDGE_STOVE_NO = '";
		sql += JudgeStoveNo;
		sql += "'";
		return this.setData(sql);
		/**
		 * switch(Flag) { case 0: return
		 * this.setDataBySqlID("FrmReFurResult.UpdateFirstInFurTime", new
		 * Object[]{JudgeStoveNo});
		 * 
		 * case 1: return
		 * this.setDataBySqlID("FrmReFurResult.UpdateLastInFurTime", new
		 * Object[]{JudgeStoveNo});
		 * 
		 * case 2: return
		 * this.setDataBySqlID("FrmReFurResult.UpdateFirstOutFurTime", new
		 * Object[]{JudgeStoveNo});
		 * 
		 * case 3: return
		 * this.setDataBySqlID("FrmReFurResult.UpdateLastOutFurTime", new
		 * Object[]{JudgeStoveNo});
		 * 
		 * 
		 * }
		 */
	}

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

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

	}

	/***
	 * 保存再加热炉实绩
	 * 
	 * @param scrapSeq
	 * @return
	 */
	public CoreReturnObject resultSave(
			com.steering.mes.zgmil.entity.MilRefurResultEntity RefurResult) {
		ArrayList<String> parmlist = new ArrayList<String>();
		String sql = "";
		String sql1 = "";
		sql = CoreXmlSqlParsersFactory.getSql("FrmReFurResult.resultSave");
		sql = EntityTool.fillSqlByEntity(sql, RefurResult);
		sql1 = CoreXmlSqlParsersFactory
				.getSql("FrmReFurResult.UpdateEndTimeForRack");
		sql1 = EntityTool.fillSqlByEntity(sql1, RefurResult);
		parmlist.add(sql);
		parmlist.add(sql1);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				parmlist);
	}

	/**
	 * 再入加热炉结炉
	 * 
	 * @param rs
	 * @return
	 */
	/*
	 * public CoreReturnObject endStoveForMainPlan(
	 * com.steering.mes.zgmil.entity.MilRefurResultEntity rs, String justNo,
	 * String plineCode, String user, String shift, String group) {
	 * ArrayList<String> parmlist = new ArrayList<String>(); String sql = "";
	 * String strSql = ""; String strSql1 = ""; String strSql2 = ""; String
	 * strSql3 = ""; sql = CoreXmlSqlParsersFactory
	 * .getSql("FrmReFurResult.endStoveForMainPlan"); strSql =
	 * CoreXmlSqlParsersFactory .getSql("FrmReFurResult.endStoveForResult");
	 * strSql1 = CoreXmlSqlParsersFactory
	 * .getSql("FrmReFurResult.UpdateNodeRackOutShift"); strSql2 =
	 * CoreXmlSqlParsersFactory .getSql("FrmReFurResult.UpdateNodeRackInShift");
	 * strSql3 = CoreXmlSqlParsersFactory
	 * .getSql("FrmReFurResult.UpdateNodeRackEndTime");
	 * 
	 * 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); parmlist.add(sql);
	 * parmlist.add(strSql); parmlist.add(strSql1); parmlist.add(strSql2);
	 * parmlist.add(strSql3); return
	 * this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction( parmlist);
	 * 
	 * }
	 */

	/***
	 * 再入加热炉结炉
	 * 
	 * @param rs
	 * @param justNo
	 * @param plineCode
	 * @param user
	 * @param shift
	 * @param group
	 * @return
	 */
	public CoreReturnObject endStoveForMainPlan(
			com.steering.mes.zgmil.entity.MilRefurResultEntity rs,
			String justNo, String plineCode, String user, String shift,
			String group) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		try {
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);

			String sql = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.endStoveForMainPlan");
			sql = EntityTool.fillSqlByEntity(sql, rs);
			PreparedStatement endStoveForMainPlan = conn.prepareStatement(sql);

			String strSql = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.endStoveForResult");
			strSql = EntityTool.fillSqlByEntity(strSql, rs);
			PreparedStatement endStoveForResult = conn.prepareStatement(strSql);

			String strSql1 = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.UpdateNodeRackOutShift");
			strSql1 = EntityTool.fillSqlByEntity(strSql1, rs);
			PreparedStatement UpdateNodeRackOutShift = conn
					.prepareStatement(strSql1);

			String strSql2 = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.UpdateNodeRackInShift");
			strSql2 = EntityTool.fillSqlByEntity(strSql2, rs);
			PreparedStatement UpdateNodeRackInShift = conn
					.prepareStatement(strSql2);

			String strSql3 = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.UpdateNodeRackEndTime");
			strSql3 = EntityTool.fillSqlByEntity(strSql3, rs);
			PreparedStatement UpdateNodeRackEndTime = conn
					.prepareStatement(strSql3);

			String sql4 = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.insertFurnacesReslut");
			psta = conn.prepareStatement(sql4);

			// 更新进程表mil_processes数据
			String updateProcess = CoreXmlSqlParsersFactory
					.getSql("FrmInOnlineStore.insertMilProcess2");
			updateProcess = EntityTool.fillSqlByEntity(updateProcess, rs);
			PreparedStatement updateMilProcess = conn
					.prepareStatement(updateProcess);

			endStoveForMainPlan.addBatch();
			endStoveForResult.addBatch();
			UpdateNodeRackOutShift.addBatch();
			UpdateNodeRackInShift.addBatch();
			UpdateNodeRackEndTime.addBatch();
			updateMilProcess.addBatch();

			psta.setString(1, justNo);
			psta.setString(2, plineCode);
			psta.setString(3, rs.getProPlanId());
			psta.setString(4, rs.getGxPlanNo());
			psta.setString(5, user);
			psta.setString(6, shift);
			psta.setString(7, group);
			psta.addBatch();

			endStoveForMainPlan.executeBatch();
			endStoveForResult.executeBatch();
			UpdateNodeRackOutShift.executeBatch();
			UpdateNodeRackInShift.executeBatch();
			UpdateNodeRackEndTime.executeBatch();
			psta.executeBatch();
			updateMilProcess.executeBatch();
			psta.close();
			cro.setResult(1);
			conn.commit();

		} 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();
			}
		}

		return cro;
	}

	/***
	 * 查询连轧该毛管是否被剔除
	 * 
	 * @param JudgeStoveNo
	 * @param seq
	 * @return
	 */
	public CoreReturnObject getRollingRackSeq(String JudgeStoveNo, String seq) {
		return this.getDataBySqlID("FrmReFurResult.getRollingRackSeq",
				new Object[] { JudgeStoveNo, seq });
	}

	/**
	 * 查询结炉实绩
	 * 
	 * @param PliceCode
	 * @param Shift
	 * @param Group
	 * @param StartTime
	 * @param EndTime
	 * @return
	 */
	public CoreReturnObject GetReFurResultNodeQuery(String PliceCode,
			String Shift, String Group, String StartTime, String EndTime) {
		// ArrayList<String> parmlist = new ArrayList<String>();
		// String sql =
		// "SELECT  a.BURNINGRATE,a.OUT_FUR_NUM, a.RESULT_NO,a.LOT_NO,a.STOVE_NO, a.JUDGE_STOVE_NO,a.PLINE_CODE,  ";
		// sql+=
		// "a.IN_FUR_NUM, a.SCRAP_NUM,TO_CHAR(a.START_TIME, 'YYYY-MM-DD HH24:MI:SS') START_TIME,TO_CHAR(a.END_TIME, 'YYYY-MM-DD HH24:MI:SS') END_TIME,";
		// sql+=
		// "b.DB_HEIGHT,c.BURNINGRATE, a.qa_ton,a.remark, a.col_shift,a.col_group, a.CHANGECLASSNUM, a.OUT_CHANGE_NUM,";
		// sql+=
		// "a.col_user FROM MIL_REFUR_RESULT a, mil_plan b,mil_refur_result c WHERE 1=1 AND b.PLINE_CODE='"+PliceCode+"'";
		// sql+= " AND a.COL_SHIFT='"+Shift+"'";
		// sql+= " AND a.COL_GROUP='"+Group+"'";
		// sql+=
		// " AND a.END_TIME BETWEEN TO_DATE('"+StartTime+"','YYYY-MM-DD HH24:MI:SS') AND TO_DATE('"+EndTime+"','YYYY-MM-DD HH24:MI:SS')";
		// sql+= " AND a.NODE_FUR_FLAG='1'";
		// sql+= " AND a.JUDGE_STOVE_NO = b.JUDGE_STOVE_NO";
		// sql+= " and b.JUDGE_STOVE_NO = c.JUDGE_STOVE_NO";
		// sql+= " order by a.end_time";
		// parmlist.add(sql);
		// return
		// this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(parmlist);
		return this.getDataBySqlID("FrmReFurResult.GetReFurResultNodeQuery",
				new Object[] { PliceCode, Shift, Group, StartTime, EndTime });
	}

	/**
	 * 插入荒管剔除待处理实绩表
	 * 
	 * @param rs
	 * @return
	 */
	public CoreReturnObject insertFurnacesReslut(String justNo,
			String plineCode, String user, String shift, String group) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		String sql = "";
		try {
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.insertFurnacesReslut");
			psta = conn.prepareStatement(sql);
			psta.setString(1, justNo);
			psta.setString(2, plineCode);
			psta.setString(3, user);
			psta.setString(4, shift);
			psta.setString(5, group);
			psta.addBatch();

			psta.executeBatch();
			psta.close();
			conn.commit();

		} 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 justNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject deleteFurnacesReslut(String justNo,
			String plineCode, String proPlanId, String gxPlanNo) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		ResultSet rs = null;
		int status = 0;// 组炉状态
		String sql = "";
		try {
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);

			sql = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.queryFurnacesReslut");
			psta = conn.prepareStatement(sql);
			psta.setString(1, justNo);
			psta.setString(2, plineCode);
			psta.setString(3, proPlanId);
			psta.setString(4, gxPlanNo);
			rs = psta.executeQuery();
			if (rs.next()) {
				status = Integer.parseInt(rs.getString("ZP_FLAG"));
			}
			rs.close();
			psta.close();
			if (status > 0) {
				cro.setV_errMsg("此炉号已组炉,不可以回退!");
				return cro;
			}

			sql = CoreXmlSqlParsersFactory
					.getSql("FrmReFurResult.deleteFurnacesReslut");
			psta = conn.prepareStatement(sql);
			psta.setString(1, justNo);
			psta.setString(2, plineCode);
			psta.setString(3, proPlanId);
			psta.setString(4, gxPlanNo);
			psta.addBatch();

			psta.executeBatch();
			psta.close();
			cro.setV_errMsg("荒管剔除成功!");
			conn.commit();

		} 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(1);
		return cro;
	}

	/**
	 * 根据炉号查返线计划标识（BACK_PLAN_FLAG）
	 * 
	 * @param judgeStoveNo
	 * @return
	 */
	public CoreReturnObject getMilPlanFalg(String judgeStoveNo,
			String plineCode, String proPlanId, String gxPlanNo) {
		String sql = "select t.BACK_PLAN_FLAG from MIL_PLAN t where t.judge_stove_no = '"
				+ judgeStoveNo
				+ "' AND NVL(PRO_PLAN_ID,'/') = NVL('"
				+ proPlanId
				+ "','/') AND NVL(GX_PLAN_NO,'/') = NVL('"
				+ gxPlanNo
				+ "','/')"
				+ " and t.pline_code = '"
				+ plineCode
				+ "'";
		return this.getData(sql);
	}

}
