package com.steering.mes.zgmil.coup;

import java.util.ArrayList;
import com.steering.comm.db.DBCommon;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.common.EntityTool;
import com.steering.mes.zgmil.entity.MilSlabScrapSeqEntity;

/**
 * 定径业务类
 * 
 * @author lr 2015/05/28
 * @version v1.0
 */
public class SizingResult extends DBCommon {
	/**
	 * 查询
	 * 
	 * @param JudgeStoveNo
	 *            炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getSizingResult(String JudgeStoveNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("SizingResult.SingleSizingResultQuery",
				new Object[] { JudgeStoveNo, proPlanId, gxPlanNo });
	}

	/**
	 * 查询
	 * 
	 * @param JudgeStoveNo
	 *            炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getReFurResult(String JudgeStoveNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("SizingResult.getReFurResult", new Object[] {
				JudgeStoveNo, proPlanId, gxPlanNo });
	}

	/**
	 * 查询
	 * 
	 * @param JudgeStoveNo
	 *            炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getReFurResult1(String JudgeStoveNo,
			String proPlanId, String gxPlanNo) {
		return this.getDataBySqlID("SizingResult.getReFurResult1",
				new Object[] { JudgeStoveNo, proPlanId, gxPlanNo });
	}

	/**
	 * 查询管号队列
	 * 
	 * @param JudgeStoveNo
	 *            --炉号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getSizingRackSeq(String JudgeStoveNo) {
		return this.getDataBySqlID("SizingResult.SizingRackSeqQuery",
				new Object[] { JudgeStoveNo });
	}

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

		String sql = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateMainPlanStatue");
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.AddSizingResult");
		String sql2 = "Update MIL_SIZING_RACK SET START_SIZING_TIME = SYSDATE WHERE JUDGE_STOVE_NO = '"
				+ MilSizingResultEntity.getJudgeStoveNo()
				+ "' AND RESULT_NO = '"
				+ MilSizingResultEntity.getResultNo()
				+ "'";
		sql1 = EntityTool.fillSqlByEntity(sql1, MilSizingResultEntity);
		sql = EntityTool.fillSqlByEntity(sql, MilSizingResultEntity);
		sqllist.add(sql);
		sqllist.add(sql1);
		// sqllist.add(sql2);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}

	/**
	 * 保存定径实绩信息
	 * 
	 * @param MilSizingResultEntity
	 *            定径实绩实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject saveSizingResult(
			com.steering.mes.zgmil.entity.MilSizingResultEntity MilSizingResultEntity) {
		ArrayList<String> sqllist = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateSizingResult");
		sql = EntityTool.fillSqlByEntity(sql, MilSizingResultEntity);
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.SaveRackEndTime");
		sql1 = EntityTool.fillSqlByEntity(sql1, MilSizingResultEntity);
		//修改判定表终轧温度
		String sql2 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.SaveQcmExitTemp");
		sql2 = EntityTool.fillSqlByEntity(sql2, MilSizingResultEntity);
		//修改质保书终轧温度
		String sql3 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.SaveZbsExitTemp");
		sql3 = EntityTool.fillSqlByEntity(sql3, MilSizingResultEntity);
		sqllist.add(sql);
		sqllist.add(sql1);
		sqllist.add(sql2);
		sqllist.add(sql3);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);

	}

	/**
	 * 结炉
	 * 
	 * @param MilSizingResultEntity
	 *            定径实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject nodeResult(
			com.steering.mes.zgmil.entity.MilSizingResultEntity MilSizingResultEntity) {

		ArrayList<String> sqllist = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateSizingResultForNode");
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateMainPlanNodeStatue");
		String sql2 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateSizingResultEndTime");
		String sql3 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateSizingRack");
		// 交接班时上个班的结束时间为空时，补齐时间
		String sql4 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateSizingRackEndTime");
		// 更新进程表mil_processes数据
		String updateProcess = CoreXmlSqlParsersFactory
				.getSql("FrmInOnlineStore.insertMilProcess2");
		sql = EntityTool.fillSqlByEntity(sql, MilSizingResultEntity);
		sql1 = EntityTool.fillSqlByEntity(sql1, MilSizingResultEntity);
		sql2 = EntityTool.fillSqlByEntity(sql2, MilSizingResultEntity);
		sql3 = EntityTool.fillSqlByEntity(sql3, MilSizingResultEntity);
		sql4 = EntityTool.fillSqlByEntity(sql4, MilSizingResultEntity);
		updateProcess = EntityTool.fillSqlByEntity(updateProcess,
				MilSizingResultEntity);
		sqllist.add(sql);
		sqllist.add(sql1);
		sqllist.add(sql2);
		sqllist.add(sql3);
		sqllist.add(sql4);
		sqllist.add(updateProcess);
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);

	}

	/**
	 * 剔除
	 * 
	 * @param MilScrapSlab
	 *            管坯剔除实体类
	 * @return CoreReturnObject
	 */
	public CoreReturnObject scrapMat(MilSlabScrapSeqEntity MilScrapSlab) {
		ArrayList<String> sqllist = new ArrayList<String>();
		// 新增剔除管号序列
		String sql = CoreXmlSqlParsersFactory
				.getSql("FeedResult.AddScrapSlabSeq");
		// 修改定径表物料状态
		String sql1 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateSizingStatue");
		// 修改定径实绩表剔除支数
		String sql2 = CoreXmlSqlParsersFactory
				.getSql("SizingResult.UpdateSizingScrapNum");

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

	/**
	 * j结炉实绩查询
	 */
	public CoreReturnObject GetSizingResultNodeQuery(String PliceCode,
			String Shift, String Group, String StartTime, String EndTime) {
		return this.getDataBySqlID("SizingResult.GetSizingResultNodeQuery",
				new Object[] { PliceCode, Shift, Group, StartTime, EndTime });
	}

	/**
	 * 交班 1- 修改实绩表交接班支数 2-修改跟踪表班次班组
	 * 
	 * @param ProcessNo
	 *            1-锯切 2-加热炉 5-再加热炉 6-定径
	 * @return CoreReturnObject
	 */
	public CoreReturnObject changeNum(String JudgeStoveNo, String ResultNo,
			Integer Seq, String PLineCode, String Shift, String Class,
			String ProcessNo) {
		ArrayList<String> sqllist = new ArrayList<String>();
		String sql1 = "";
		String sql2 = "";
		if (ProcessNo.equals(6)) {
			// 无再加热炉 修改穿孔、连轧、定径交班支数
			if (PLineCode.equals("C009") || PLineCode.equals("C012")
					|| PLineCode.equals("C017")) {
				String[] TableResultList = new String[] {
						"MIL_PERFORATION_RESULT", "MIL_ROLLING_RESULT",
						"MIL_REFUR_RESULT", "MIL_SIZING_RESULT" };
				String[] TableRackList = new String[] { "mil_perforation_rack",
						"MIL_ROLLING_RACK", "mil_refur_rack", "mil_sizing_rack" };
				String[] ChangeNumList = new String[] { "JZB_NUM",
						"CHANGECLASSNUM", "CHANGECLASSNUM", "CHANGE_NUM" };

				for (int i = 0; i < 3; i++) {
					// 修改交接班支数
					sql1 = "update " + TableResultList[i] + " set  "
							+ ChangeNumList[i] + " = " + Seq + " where ";
					sql1 += "JUDGE_STOVE_NO = '" + JudgeStoveNo
							+ "' and RESULT_NO = '" + ResultNo + "'";

					sql2 = "update " + TableRackList[i] + " set COL_SHIFT = '"
							+ Shift + "'  AND COL_GROUP = '" + Class
							+ "'  where ";
					sql2 += "JUDGE_STOVE_NO = '" + JudgeStoveNo
							+ "' and RESULT_NO = '" + ResultNo + "'  and seq <"
							+ (Seq + 1) + "";

					sqllist.add(sql1);
					sqllist.add(sql2);
				}

			}
			// 有再加热炉
			else {
				sql1 = "update MIL_SIZING_RESULT set  CHANGE_NUM = " + Seq
						+ " where ";
				sql1 += "JUDGE_STOVE_NO = '" + JudgeStoveNo
						+ "' and RESULT_NO = '" + ResultNo + "'";

				sql2 = "update mil_sizing_rack set COL_SHIFT = '" + Shift
						+ "'  AND COL_GROUP = '" + Class + "'  where ";
				sql2 += "JUDGE_STOVE_NO = '" + JudgeStoveNo
						+ "' and RESULT_NO = '" + ResultNo + "'  and seq <"
						+ (Seq + 1) + "";

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

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

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

	}

}
