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

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.signature.MeanValueCalculation;
import com.steering.mes.zgmil.entity.MilHotCheckEntity;
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.MilPlan;
import com.steering.mes.zgmil.entity.MilScrapSlab;
import com.steering.mes.zgmil.entity.FeedSawResultEntity;
import com.steering.mes.zgmil.entity.MilSlabScrapSeqEntity;
import com.steering.mes.zgmil.entity.MilHotCheckDetailEntity;
import com.steering.mes.zgmil.entity.MilHotCheckResultEntity;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

public class FrmHotCheck extends DBCommon {
	/**
	 * 查询主表记录
	 * 
	 * @param heatno
	 * @return
	 */
	public CoreReturnObject getHotPlan(String heatno) {
		return this.getDataBySqlID("HotCheck.getHotPlan",
				new Object[] { heatno });
	}

	public CoreReturnObject CheckHotCheck(String heatno) {
		return this.getDataBySqlID("HotCheck.CheckHotCheck",
				new Object[] { heatno });
	}

	// 查询热取样详细信息
	public CoreReturnObject CheckHotCheckDetail(String heatno, String ResultNo,
			String Shift, String Group) {
		return this.getDataBySqlID("HotCheck.CheckHotCheckDetail",
				new Object[] { heatno, ResultNo, Shift, Group });
	}

	// 查询热取样主实绩信息
	public CoreReturnObject GetHotCheckMainResult(String heatno, String ResultNo) {
		return this.getDataBySqlID("HotCheck.CheckHotCheck", new Object[] {
				heatno, ResultNo });
	}

	/**
	 * 查询简单作业计划
	 * 
	 * @param MilPlan
	 *            作业计划实体类
	 * @param ProcessNo
	 *            工序号 1-锯切 2-环形加热炉 3-穿孔 4-连轧 5-再加热炉 6-定径 7-管排锯 8-矫直 9-ET探伤
	 *            10-人工质检 11-涡流探伤
	 * @return CoreReturnObject
	 * @Return 0-未开始 1-工序已开始
	 */
	public CoreReturnObject getSingleMainPlan(
			com.steering.mes.zgmil.entity.MilPlan MilPlan, Integer ProcessNo,
			Integer HotFlag) {
		String[] TableNameList = new String[] { "MIL_FEEDSAW_RESULT",
				"MIL_FUR_Result", "MIL_PERFORATION_Result",
				"MIL_ROLLING_Result", "MIL_REFUR_Result", "MIL_SIZING_Result",
				"MIL_SAW_Result", "MIL_STRAIGHTENING_RESULT", "MIL_ET_RESULT",
				"MIL_SURFACE_RESULT", "MIL_MT_RESULT", "MIL_UL_RESUL" };
		String TableName = TableNameList[ProcessNo - 1];
		// String Judge_Stove_No = "Judge_Stove_No";
		String BackFlag = "0,1";
		if (ProcessNo.equals(8)) {
			BackFlag = "0,1,7";
		} else if (ProcessNo > 8) {
			BackFlag = "0,1,7,8";
		}
		if (ProcessNo.equals(10)) {
			// Judge_Stove_No = "HEAT_NO";
		}
		String Sql = "SELECT b.CUT_NUM,a.ID,a.SEQ,a.STOVE_NO,a.JUDGE_STOVE_NO,a.LOT_NO,a.CONTRACT_NO,a.IMP_PROCESS,a.PLINE_CODE,case a.HOT_CHECK_FLAG";
		Sql += " WHEN '0' THEN '未完成抽检' WHEN '1' THEN '已完成抽检' END AS HOT_CHECK_FLAG,";
		Sql += " REGEXP_REPLACE(a.JUDGE_STOVE_NO, '-.*', '') as JUDGE_NO,CASE a.STATUS WHEN ";
		Sql += "'00' THEN '上料' WHEN '05' THEN '开始锯切' WHEN  '10'	THEN '锯切完成' WHEN  '15' THEN '入炉' WHEN '20'	THEN '出炉' WHEN '25' THEN '开始穿孔' WHEN '30'	THEN '穿孔完成' WHEN ";
		Sql += " '35' THEN '开始连轧' WHEN  '40'	THEN '连轧完成' WHEN '45' THEN '入再加热炉' WHEN '50'	THEN '出再加热炉' WHEN '55' THEN '开始定径' WHEN '60'	THEN '定径完成' WHEN ";
		Sql += " '65' THEN '开始分切' WHEN '70'	THEN '分切完成' WHEN '75' THEN '开始矫直' WHEN '80'	THEN '矫直完成 ' WHEN '85' THEN '开始探伤' WHEN '90'	THEN '探伤完成' WHEN	 '95' THEN '人工质检开始' WHEN ";
		Sql += "  '99'	THEN '入库' END AS STATUS,";
		Sql += " a.PRODUCER, a.PROCESS_STATUS, a.DB_LENGTH, a.DB_HEIGHT, a.ROLL_LENGTH, a.SAW_NUM, a.TARGET_LENGTH, a.FINISH_USER, a.TARGET_LIABRARY, a.VARIETIES, a.PIPE_SIZE, ";
		Sql += " a.BUCKLE_TYPE, a.LENGTH_RANGE, a.ROLL_STANDRAD,a.PIPE_SECTION, a.STEELCODE, a.STEELNAME, a.GRADECODE,  a.GRADENAME, a.STEEL_STANDRAD, a.TARGET, BRANCH,";
		Sql += "a.TON, a.CUT_TARGET,a.CUT_LENGTH, a.CUT_WEIGHT, a.DB_NUM, CASE a.NODE_STATUS WHEN  '00' THEN '' WHEN '10'	THEN '锯切完成' WHEN '15' THEN '入炉' WHEN '20'	THEN '出炉' WHEN ";
		Sql += " '25' THEN '开始穿孔' WHEN '30'	THEN '穿孔完成' WHEN '35' THEN '开始连轧' WHEN '40'	THEN '连轧完成' WHEN '45' THEN '入再加热炉' WHEN '50'	THEN '出再加热炉' WHEN '55' THEN '开始定径' WHEN ";
		Sql += " '60'	THEN '定径完成' WHEN '65' THEN '开始分切' WHEN '70'	THEN '分切完成' WHEN '75' THEN '开始矫直' WHEN '80'	THEN '矫直完成 ' WHEN '85' THEN '开始探伤' WHEN '90'	THEN '探伤完成' WHEN	 '96' THEN '人工质检完成' WHEN '99' THEN '交库'";
		Sql += " END AS NODE_STATUS,a.PRO_PLAN_ID,a.GX_PLAN_NO,";
		if (ProcessNo.equals(2) || ProcessNo.equals(5)) {
			Sql += " Nvl((select b.IS_START FROM "
					+ TableName
					+ " b where b.judge_stove_no = a.judge_Stove_no) ,4)AS IS_START, ";
		} else {
			Sql += " Nvl((select  b.NODE_FUR_FLAG from "
					+ TableName
					+ " b where b.judge_stove_no = a.judge_Stove_no) ,3)AS IS_START ,";
		}
		Sql += " 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, TO_CHAR(a.TOM, 'YYYY-MM-DD HH24:MI:SS') TOM, TO_CHAR(a.TOC, 'YYYY-MM-DD HH24:MI:SS') TOC ";
		Sql += "   FROM MIL_PLAN   a  ,MIL_FEEDSAW_RESULT b";
		Sql += " WHERE A.COL_TIME > TO_DATE('201903','YYYY-MM')  ";
		String a = MilPlan.getLotNo();
		String b = MilPlan.getJudgeStoveNo();
		if (!MilPlan.getLotNo().trim().equals("")) {
			Sql += " AND a.LOT_NO || '&' LIKE '%' || '" + MilPlan.getLotNo()
					+ "' || '%' ";
		}
		if (!MilPlan.getJudgeStoveNo().trim().equals("")) {
			Sql += "  AND a.JUDGE_STOVE_NO || '&' LIKE '%' || '"
					+ MilPlan.getJudgeStoveNo() + "' || '%' ";
		}
		Sql += " and  a.NODE_STATUS between " + MilPlan.getStartStatus()
				+ " and " + MilPlan.getStatus() + " ";
		Sql += " AND a.PLINE_CODE = '" + MilPlan.getPlineCode() + "'";
		Sql += " AND a.HOT_CHECK_FLAG = '" + HotFlag + "'";
		Sql += " AND a.rolling_status = '0'  and a.BACK_PLAN_FLAG in ( "
				+ BackFlag + ")";
		Sql += " and a.judge_stove_no = b.judge_stove_no(+)";
		Sql += " and a.id = b.Result_NO(+)";
		if (ProcessNo > 1) {
			Sql += " AND STATUS>'00'   ";
		}
		Sql += "  order by   a.FEED_TIME  ,a.START_TIME";
		return this.getData(Sql);
	}

	/**
	 * 保存抽样送检详细数据
	 * 
	 * @return
	 */
	public CoreReturnObject saveHotCheckDetail(ArrayList ListEntity,
			String JudgeStoveNo, String ResultNo, String Shift, String Group) {
		ArrayList<String> sqllist = new ArrayList<String>();
		String sql1 = " DELETE MIL_HOT_CHECK_DETAIL WHERE JUDGE_STOVE_NO='"
				+ JudgeStoveNo + "' and RESULT_NO= '" + ResultNo
				+ "' and COL_SHIFT = '" + Shift + "' AND COL_GROUP = '" + Group
				+ "'";
		sqllist.add(sql1);
		String sql = "";
		String date = "";
		for (int i = 0; i < ListEntity.size(); i++) {

			MilHotCheckDetailEntity MilHotCheckDetailEntity = (MilHotCheckDetailEntity) ListEntity
					.get(i);
			sql = CoreXmlSqlParsersFactory
					.getSql("HotCheck.InsertHotCheckDetail");
			sql = EntityTool.fillSqlByEntity(sql, ListEntity.get(i));
			if (MilHotCheckDetailEntity.getColTime().equals("sysdate")) {
				date = "sysdate";
			} else {
				date = "to_date('" + MilHotCheckDetailEntity.getColTime()
						+ "','yyyy-mm-dd hh24:mi:ss') ";
			}
			sql += date + " )";
			sqllist.add(sql);
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist);
	}

	/**
	 * 保存抽样送检主实绩数据
	 * 
	 * @return
	 */
	public CoreReturnObject saveHotCheck(
			MilHotCheckResultEntity MilHotCheckEntity, String CutHead,
			String CutTrail, String OrderLength) {
		String date = "";
		if (MilHotCheckEntity.getCheckDate().equals("sysdate")) {
			date = "sysdate";
		} else {
			date = "to_date('" + MilHotCheckEntity.getCheckDate()
					+ "','yyyy-mm-dd hh24:mi:ss') ";
		}
		ArrayList<String> sqllist = new ArrayList<String>();
		String sql1 = " DELETE MIL_HOT_CHECK WHERE JUDGE_STOVE_NO='"
				+ MilHotCheckEntity.getJudgeStoveNo() + "' ";
		String sql2 = " Insert into MIL_HOT_CHECK (PLINE_CODE,JUDGE_STOVE_NO,RESULT_NO,COL_SHIFT,COL_GROUP,INSPECTOR,CHECK_DATE,REMARK,PASS_SERIES1,PASS_SERIES2,PASS_SERIES3,PASS_SERIES4,DOUBLE_B1,DOUBLE_B2,DOUBLE_B3,DOUBLE_B4,OUTDIAMETER,WALLTHICK,START_TEMP,END_TEMP) values  ";
		sql2 += "   ('" + MilHotCheckEntity.getPlineCode() + "','"
				+ MilHotCheckEntity.getJudgeStoveNo() + "','"
				+ MilHotCheckEntity.getResultNo() + "','"
				+ MilHotCheckEntity.getColShift() + "','"
				+ MilHotCheckEntity.getColGroup() + "','"
				+ MilHotCheckEntity.getInspector() + "'," + date + ",'"
				+ MilHotCheckEntity.getRemark() + "', ";
		sql2 += "  '" + MilHotCheckEntity.getPassSeries1() + "','"
				+ MilHotCheckEntity.getPassSeries2() + "','"
				+ MilHotCheckEntity.getPassSeries3() + "','"
				+ MilHotCheckEntity.getPassSeries4() + "','"
				+ MilHotCheckEntity.getDoubleB1() + "','"
				+ MilHotCheckEntity.getDoubleB2() + "','"
				+ MilHotCheckEntity.getDoubleB3() + "','"
				+ MilHotCheckEntity.getDoubleB4() + "','"
				+ MilHotCheckEntity.getOutdiameter() + "','"
				+ MilHotCheckEntity.getWallthick() + "','"
				+ MilHotCheckEntity.getStartTemp() + "','"
				+ MilHotCheckEntity.getEndTemp() + "') ";

		// 修改主表实绩
		String sql3 = "  update mil_plan set CUT_HEAD_LENGTHA ='" + CutHead
				+ "' ,CUT_TRAIL_LENGTHA ='" + CutTrail + "' ,ORDER_LENGTHA='"
				+ OrderLength + "' where JUDGE_STOVE_NO='"
				+ MilHotCheckEntity.getJudgeStoveNo() + "' and ID= '"
				+ MilHotCheckEntity.getResultNo() + "'";
		sqllist.add(sql1);
		sqllist.add(sql2);
		sqllist.add(sql3);

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

	/**
	 * 修改取样标识
	 * 
	 * @return
	 */
	public CoreReturnObject updateCheckFlag(String JudgeStoveNo,
			String ResultNo, String CheckFlag) {
		ArrayList<String> sqllist = new ArrayList<String>();
		String sql1 = " update  mil_plan set HOT_CHECK_FLAG = '" + CheckFlag
				+ "' WHERE JUDGE_STOVE_NO='" + JudgeStoveNo + "' and ID= '"
				+ ResultNo + "'";
		sqllist.add(sql1);

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

	/**
	 * 查询简单作业计划
	 */
	public CoreReturnObject getSingleMainPlanNodeStatus(String JudgeStoveNo,
			String ResultNo) {
		String Sql = " SELECT JUDGE_STOVE_NO,NODE_STATUS FROM MIL_PLAN WHERE JUDGE_STOVE_NO = '"
				+ JudgeStoveNo + "' AND ID = '" + ResultNo + "' ";
		return this.getData(Sql);
	}

	/**
	 * 查询快速分切是否开始
	 */
	public CoreReturnObject getSawCutIsStart(String JudgeStoveNo,
			String ResultNo) {
		String Sql = " SELECT JUDGE_STOVE_NO,CUT_HEAD_LENGTH FROM MIL_SAW_RESULT WHERE JUDGE_STOVE_NO = '"
				+ JudgeStoveNo + "' AND RESULT_NO = '" + ResultNo + "' ";
		return this.getData(Sql);
	}

	/**
	 * 回炉坯合同号查询
	 * 
	 * @param heatno
	 * @return
	 */
	public CoreReturnObject getContractNo(String SteelName, String Length,
			String Section, String PlineCode, String date) {
		return this.getDataBySqlID("HotCheck.getBackSlab", new Object[] {
				PlineCode, SteelName, Length, Section, date });
	}

	/**
	 * 合同号查询回炉坯
	 * 
	 * @param heatno
	 * @return
	 */
	public CoreReturnObject getBackSlabForContractNo(String SteelName,
			String Length, String Section, String PlineCode) {
		return this.getDataBySqlID("HotCheck.getBackSlabForContractNo",
				new Object[] { SteelName, Length, Section, PlineCode });
	}

//	TZH 24.4.24 注销物流接口
//	/**
//	 * 从中间表装炉计划中查询是否存在该批号
//	 * 
//	 * @param LotNo
//	 * @return
//	 */
//	public CoreReturnObject getLotNoInFurPlan(String LotNo) {
//		return this.getDataBySqlID("MSSQL1Dao", "HotCheck.getLotNoInFurPlan",
//				new Object[] { LotNo });
//	}

	/**
	 * 
	 * 已组批炉号查询回炉坯
	 * 
	 * @param JudgeStoveNo
	 * @return
	 */
	public CoreReturnObject getBackSlabForJudgeStoveNo(String JudgeStoveNo) {
		return this.getDataBySqlID("HotCheck.getBackSlabForJudgeStoveNo",
				new Object[] { JudgeStoveNo });
	}

	/**
	 * 查询已组回炉坯计划
	 * 
	 * @param heatno000
	 * 
	 * @return
	 */
	public CoreReturnObject getBackSlabPlan(String JudgeStoveNo, String LotNo,
			String ContractNo, String Flag, String PlineCode) {
		if (Flag.equals("0")) {
			return this
					.getDataBySqlID("HotCheck.getBackSlabPlan", new Object[] {
							PlineCode, JudgeStoveNo, LotNo, ContractNo });
		} else {
			return this
					.getDataBySqlID("HotCheck.getBackSlabPlanStart",
							new Object[] { PlineCode, JudgeStoveNo, LotNo,
									ContractNo });
		}
	}

	/**
	 * 回炉坯组炉
	 * 
	 * @param JudgeStoveNo
	 * @param BackJudgeStoveNo
	 * @param BackLotNo
	 * @param DBnum
	 * @param MatNoList
	 * @param User
	 * @param Shift
	 * @param Group
	 * @param ContractNo
	 * @param RollLength
	 * @return
	 * @throws Exception
	 */
	public CoreReturnObject BackSlabGroup(String JudgeStoveNo,
			String BackJudgeStoveNo, String BackLotNo, Integer DBnum,
			String MatNoList, String User, String Shift, String Group,
			String ContractNo, String RollLength, String proPlanId,
			String gxPlanNo, FeedRowPlan FeedRowPlan1) throws Exception {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		PreparedStatement psta1 = null;
		// String id = "substr(SYS_GUID(),0,20)";
		String Seq = "0001";
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		int seq = 1;
		double inPutWgt = 0;// 上料量
		int inPutNum = 0;// 总支数
		int m = 0;

		try {
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);
			ResultSet rs = null;
			ResultSet rs1 = null;
			ResultSet rs2 = null;

			String ID = "";
			String CONTRACT_NO = "";
			String IMP_PROCESS = "";
			String PLINE_CODE = "";
			String PRODUCER = "";
			String PROCESS_STATUS = "";
			String DB_LENGTH = "";
			String DB_HEIGHT = "";
			String ROLL_LENGTH = "";
			String SAW_NUM = "";
			String TARGET_LENGTH = "";
			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 TARGET = "";
			String CUT_HEAD_LENGTHP = "";
			String CUT_TRAIL_LENGTHP = "";
			String CONTROL_THICKP = "";
			String PRODUCE_STANDRAD = "";
			String CUT_TYPE = "";
			String FEED_HEAT_NO = "";
			String LENGTHCP = "";
			String ORD_LN_DLY_PK = "";
			String PRT_FLAG = "";
			String PRO_PLAN_ID = "";
			String GX_PLAN_NO = "";
			String RESULT_NO = "";

			String CRAFT_SEQ = "";
			String CIC = "";
			String INDEX_SEQ = "";
			String PIC = "";
			String IF_PRODUCT = "";
			String PRDCR_NO = "";
			String IF_MONITOR = "";

			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()) {
				CONTRACT_NO = rs.getString(5);
				IMP_PROCESS = rs.getString(6);
				PLINE_CODE = rs.getString(7);
				PRODUCER = rs.getString(8);
				if (PRODUCER == null) {
					PRODUCER = "";
				}
				PROCESS_STATUS = rs.getString(9);
				DB_LENGTH = rs.getString(10);
				DB_HEIGHT = rs.getString(11);
				ROLL_LENGTH = rs.getString(12);
				SAW_NUM = rs.getString(13);
				TARGET_LENGTH = rs.getString(14);
				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);
				TARGET = rs.getString(28);
				CUT_HEAD_LENGTHP = rs.getString(29);
				CUT_TRAIL_LENGTHP = rs.getString(30);
				CONTROL_THICKP = rs.getString(31);
				PRODUCE_STANDRAD = rs.getString(32);
				CUT_TYPE = rs.getString(33);
				FEED_HEAT_NO = rs.getString(34);
				LENGTHCP = rs.getString(35);
				ORD_LN_DLY_PK = rs.getString(36);
				PRT_FLAG = rs.getString(37);
				PRO_PLAN_ID = rs.getString(38);
				GX_PLAN_NO = rs.getString(39);

				CRAFT_SEQ = rs.getString(40);
				if (CRAFT_SEQ == null) {
					CRAFT_SEQ = "";
				}
				CIC = rs.getString(41);
				if (CIC == null) {
					CIC = "";
				}
				INDEX_SEQ = rs.getString(42);
				if (INDEX_SEQ == null) {
					INDEX_SEQ = "";
				}
				PIC = rs.getString(43);
				if (PIC == null) {
					PIC = "";
				}
				IF_PRODUCT = rs.getString(44);
				if (IF_PRODUCT == null) {
					IF_PRODUCT = "";
				}
				PRDCR_NO = rs.getString(45);
				if (PRDCR_NO == null) {
					PRDCR_NO = "";
				}
				IF_MONITOR = rs.getString(46);
				if (IF_MONITOR == null) {
					IF_MONITOR = "";
				}
				ID = rs.getString(47);

				// 组炉组批修改合同号、用户、规格
				if (!ContractNo.trim().equals("")) {
					String sql2 = CoreXmlSqlParsersFactory
							.getSql("HotCheck.MainPlanForContactNo");
					psta = conn.prepareStatement(sql2);
					psta.setString(1, ContractNo);
					System.out.print(sql2);
					rs2 = psta.executeQuery();
					while (rs2.next()) {
						CONTRACT_NO = ContractNo;
						FINISH_USER = rs2.getString(2);
						PIPE_SIZE = rs2.getString(3);
						IMP_PROCESS = rs2.getString(4);
						VARIETIES = rs2.getString(5);
						BUCKLE_TYPE = rs2.getString(6);
						LENGTH_RANGE = rs2.getString(7);
						ROLL_STANDRAD = rs2.getString(8);
						CONTROL_THICKP = rs2.getString(9);
						PROCESS_STATUS = rs2.getString(10);
						SAW_NUM = rs2.getString(11);
						ROLL_STANDRAD = rs2.getString(12);
						TARGET = rs2.getString(13);
						if (rs2.getString(14).length() > 0) {
							PRO_PLAN_ID = rs2.getString(14);
						}
						if (rs2.getString(15).length() > 0) {
							GX_PLAN_NO = rs2.getString(15);
						}
					}
				}

				// 组炉组批修改轧制长度
				if (!RollLength.trim().equals("")) {
					ROLL_LENGTH = RollLength;
				}

				int result = FeedSawResult.comPareTime(systime,
						new SimpleDateFormat("yyyy-MM-dd").format(now)
								+ " 21:00:00");
				if (result > 0) {
					yearMoth = getBalYearMoth(systime);

				}

				// 插入Mil_Plan
                String sql1 = CoreXmlSqlParsersFactory
                            .getSql("HotCheck.insertMilPlan");
                psta1 = conn.prepareStatement(sql1);
                psta1.setString(1, JudgeStoveNo);
                psta1.setString(2, proPlanId);
                psta1.setString(3, gxPlanNo);
                psta1.setString(4, BackJudgeStoveNo);
                psta1.setString(5, BackLotNo);
                psta1.setInt(6, DBnum);
                psta1.executeUpdate();
                psta1.close();


				String TotalHeight = "";

				// 插入Mil_Feed_Plan
				String sql2 = CoreXmlSqlParsersFactory
						.getSql("HotCheck.InsertBackFeedPlan");
				psta1 = conn.prepareStatement(sql2);
				psta1.setString(1, "0001");
				psta1.setString(2, "001");
				psta1.setString(3, BackLotNo);
				psta1.setString(4, BackJudgeStoveNo + "-01");
				psta1.setString(5, PLINE_CODE);
				psta1.setInt(6, 1);
				psta1.setString(7, DB_HEIGHT);
				psta1.setString(8, TotalHeight);
				psta1.setInt(9, DBnum);
				psta1.setString(10, DB_LENGTH);
				psta1.setString(11, DB_LENGTH);
				psta1.setString(12, DB_LENGTH);
				psta1.setString(13, PIPE_SECTION);
				psta1.setString(14, DB_HEIGHT);
				psta1.setInt(15, DBnum);
				psta1.setInt(16, DBnum);
				psta1.setString(17, proPlanId);
				psta1.setString(18, gxPlanNo);

				psta1.executeUpdate();
				psta1.close();

				// 修改回炉坯 坯料状态
				String sql3 = "UPDATE MIL_SLAB_SCRAP SET GROUP_FLAG = '1',NEW_HEAT_NO = '"
						+ BackJudgeStoveNo
						+ "-01',FLAG = '1', "
						+ " COL_TIME = sysdate ,COL_USER = '"
						+ User
						+ "',COL_SHIFT = '"
						+ Shift
						+ "',COL_GROUP = '"
						+ Group
						+ "', NEW_LOT_NO = '"
						+ BackLotNo
						+ "'  where MAT_NO IN " + MatNoList + "";
				psta1 = conn.prepareStatement(sql3);
				psta1.executeUpdate();
				psta1.close();

				// 修改进程表组炉标识
				String sql4 = CoreXmlSqlParsersFactory
						.getSql("HotCheck.UpdateGroupFlag");
				psta1 = conn.prepareStatement(sql4);
				psta1.setString(1, BackJudgeStoveNo);

				psta1.executeUpdate();
				psta1.close();

				// 回写回炉坯原材料的重量
				String sqlTotalHeight = CoreXmlSqlParsersFactory
						.getSql("HotCheck.sqlTotalHeight");
				psta1 = conn.prepareStatement(sqlTotalHeight);
				psta1.setString(1, BackJudgeStoveNo + "-01");
				psta1.setString(2, PLINE_CODE);

				psta1.executeUpdate();
				psta1.close();
			}

			String insertProcessSql = CoreXmlSqlParsersFactory
					.getSql("FeedResult.insertProcess");// 上料生成进程管号队列
			PreparedStatement insertProcessPsta = conn
					.prepareStatement(insertProcessSql);

			// 上料生成进程管号队列
			sql = CoreXmlSqlParsersFactory.getSql("HotCheck.SelectFeedNum");
			sql = EntityTool.fillSqlByEntity(sql, FeedRowPlan1);
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			List<FeedRowPlan> list = EntityTool.getEntitysByRs(rs,
					FeedRowPlan.class);

			rs.close();
			psta.close();
			sql = CoreXmlSqlParsersFactory
					.getSql("FeedResult.SelectFeedToalWgt");
			sql = EntityTool.fillSqlByEntity(sql, FeedRowPlan1);
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				inPutWgt = Double.parseDouble(rs.getString("MAT_TOTAL_HEIGHT"));
			}
			rs.close();
			psta.close();

			sql = CoreXmlSqlParsersFactory
					.getSql("FeedResult.SelectFeedToalNum");
			sql = EntityTool.fillSqlByEntity(sql, FeedRowPlan1);
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				inPutNum = Integer.parseInt(rs.getString("ACT_COUNT"));
			}
			rs.close();
			psta.close();

			sql = CoreXmlSqlParsersFactory.getSql("HotCheck.SelectRESULT_NO"); // 查询ID
			psta = conn.prepareStatement(sql);
			psta.setString(1, BackJudgeStoveNo + "-01");
			psta.setString(2, BackLotNo);
			psta.setString(3, PLINE_CODE);
			rs = psta.executeQuery();
			if (rs.next()) {
				RESULT_NO = rs.getString("id");
			}
			rs.close();
			psta.close();

			ArrayList<String> wgtList = new ArrayList<String>();
			List<double[]> lenList = MeanValueCalculation.MeanCalculation(
					inPutWgt, inPutNum);
			for (int k = 0; k < lenList.size(); k++) {
				double[] conLen = lenList.get(k);
				if ((int) conLen[0] > 0) {
					for (int x = 0; x < (int) conLen[0]; x++) {
						DecimalFormat dt = new DecimalFormat("0.000");
						wgtList.add(dt.format(conLen[1]));
					}
				}
			}

			String mMatNo = queryMatNoM(conn, FeedRowPlan1.getJudgeStoveNo(),
					FeedRowPlan1.getLotNo(), FeedRowPlan1.getResultNo()); // 母坯材料号
			int g = Integer.parseInt(mMatNo.substring(mMatNo.length() - 3,
					mMatNo.length()));
			for (int k = 0; k < list.size(); k++) {
				FeedRowPlan feedPlan = list.get(k);
				for (int s = 0; s < Integer.parseInt(feedPlan.getFeedNum()); s++) {

					for (int i = 1; i < Integer.parseInt(feedPlan.getDbNum()) + 1; i++) {
						for (int j = 1; j < Integer.parseInt(feedPlan
								.getSawNum()) + 1; j++) {
							// 上料生成进程管号队列
							insertProcessPsta.setString(1, RESULT_NO); // ID
							insertProcessPsta.setString(2, FeedRowPlan1
									.getLotNo());
							insertProcessPsta.setString(3, FeedRowPlan1
									.getStoveNo());
							insertProcessPsta.setString(4, FeedRowPlan1
									.getJudgeStoveNo());
							insertProcessPsta.setString(5, FeedRowPlan1
									.getPlineCode());
							insertProcessPsta.setString(6, String.valueOf(seq));
							insertProcessPsta.setString(7, (FeedRowPlan1
									.getJudgeStoveNo().replaceFirst("-.*", ""))
									+ StringTool.formatDecimal(g, "000")
									+ i
									+ j);// 子管材料号
							insertProcessPsta.setString(8, "");// 定尺长度
							// new DecimalFormat("0.000").format(strWgt)
							insertProcessPsta.setString(9, "");// 重量
							insertProcessPsta.setString(10, systime);
							insertProcessPsta.setString(11, (FeedRowPlan1
									.getJudgeStoveNo().replaceFirst("-.*", ""))
									+ StringTool.formatDecimal(g, "000") + i);// 母管材料号
							insertProcessPsta.setString(12, (FeedRowPlan1
									.getJudgeStoveNo().replaceFirst("-.*", ""))
									+ StringTool.formatDecimal(g, "000"));// 母坯材料号
							insertProcessPsta.setString(13, String
									.valueOf(wgtList.get(m)));// 投料量
							insertProcessPsta.setString(14, yearMoth);
							insertProcessPsta.setString(15, FeedRowPlan1
									.getProPlanId()); // 轧管订单编号
							insertProcessPsta.setString(16, FeedRowPlan1
									.getGxPlanNo()); // 工序排产序号

							insertProcessPsta.addBatch();
							seq++;
							m++;
						}
					}
					g++;
				}
			}
			insertProcessPsta.executeBatch();
			conn.commit();
			rs.close();
			// rs2.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(1);
		return cro;
	}

	/**
	 * 回炉坯回退
	 * 
	 * @return
	 */
	public CoreReturnObject BackFurSlab(String JudgeStoveNo, String ResultNo,
			String MatNoList, String proPlanId, String gxPlanNo) {
		ArrayList<String> sqllist = new ArrayList<String>();
		String sql1 = " DELETE  mil_plan  WHERE JUDGE_STOVE_NO='"
				+ JudgeStoveNo + "' AND NVL(PRO_PLAN_ID,'/') = NVL('"
				+ proPlanId + "','/') AND NVL(GX_PLAN_NO,'/') = NVL('"
				+ gxPlanNo + "','/')";
		String sql2 = " DELETE  mil_feed_plan  WHERE JUDGE_STOVE_NO='"
				+ JudgeStoveNo + "' AND NVL(PRO_PLAN_ID,'/') = NVL('"
				+ proPlanId + "','/') AND NVL(GX_PLAN_NO,'/') = NVL('"
				+ gxPlanNo + "','/')";
		String sql3 = "update MIL_SLAB_SCRAP SET GROUP_FLAG = '0',NEW_HEAT_NO = '',FLAG = '0',COL_TIME = '' ,COL_USER = '',COL_SHIFT = '',COL_GROUP = '' where MAT_NO IN "
				+ MatNoList + "";
		String sql4 = " UPDATE PORT_MIL_BATCH_SAMPLE_RESULT T SET T.GROUP_FLAG = '0' where t.m_mat_no in "
				+ MatNoList + "";
		String sql5 = " DELETE  PORT_MIL_BATCH_SAMPLE_RESULT  WHERE JUDGE_STOVE_NO='"
				+ JudgeStoveNo
				+ "' AND NVL(PRO_PLAN_ID,'/') = NVL('"
				+ proPlanId
				+ "','/') AND NVL(GX_PLAN_NO,'/') = NVL('"
				+ gxPlanNo + "','/')";

		sqllist.add(sql1);
		sqllist.add(sql2);
		sqllist.add(sql3);
		sqllist.add(sql4);
		sqllist.add(sql5);

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

	/**
	 * 查询该炉号是否已经组炉
	 * 
	 */
	public CoreReturnObject IsUsedJudgeStoveNo(String JudgeStoveNo) {
		String Sql = " SELECT JUDGE_STOVE_NO FROM MIL_PLAN WHERE JUDGE_STOVE_NO = '"
				+ JudgeStoveNo + "'  ";
		return this.getData(Sql);
	}

	/***
	 * 查询该炉号之前是不是回炉坯炉号
	 * 
	 * @param JudgeStoveNo
	 * @return
	 */
	public CoreReturnObject IsZpJudgeStoveNo(String JudgeStoveNo) {
		String Sql = " SELECT JUDGE_STOVE_NO FROM MIL_PLAN T  WHERE T.BACK_SLAB_FLAG = 1 AND T. JUDGE_STOVE_NO = '"
				+ JudgeStoveNo + "'  ";
		return this.getData(Sql);
	}

	/**
	 * 查询MIL_PLAN表中全部合同号
	 * 
	 * @return
	 */
	public CoreReturnObject getContractNo(String plineCode) {
		String sql = "select DISTINCT t.contract_no from MIL_PLAN t where t.pline_code = '"
				+ plineCode + "' ";
		return this.getData(sql);
	}

	/**
	 * 日期加一天获取下一月(年月)
	 * 
	 * @param yearMoth
	 * @return
	 */
	private String getBalYearMoth(String systime) {
		String year = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfS = new SimpleDateFormat("yyyyMM");
		try {
			Date date = sdf.parse(systime);
			Calendar calender = Calendar.getInstance();
			calender.setTime(date);
			calender.add(Calendar.DATE, 1);
			year = sdfS.format(calender.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return year;

	}

	/**
	 * 根据炉号、批号自动生成材料号
	 * 
	 * @param conn
	 *            c
	 * @param strLcode
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryMatNoM(Connection conn, String strLcode, String lotNo,
			String resultNo) throws Exception {
		int count = 0;
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FeedResult.queryMatNoM");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strLcode);
		psta.setString(2, lotNo);
		psta.setString(3, resultNo);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			String res = rs.getString(1);
			if (res == null) {
				res = strLcode.replaceFirst("-.*", "") + "001";
			} else {
				count = Integer.parseInt(res.substring(res.length() - 3, res
						.length()));
			}
		}
		rs.close();
		psta.close();
		return (strLcode.replaceFirst("-.*", ""))
				+ StringTool.formatDecimal(++count, "000");
	}

	/**
	 * 查询当月炉号数 并生成炉号 炉号为“D XX XX XXXX”.
	 * @return
	 */
	public CoreReturnObject queryCountJudgeStoveNo() {
		String sql = "SELECT 'D'||TO_CHAR(SYSDATE, 'YYMM')|| LPAD(NVL(SUBSTR(MAX(NUM),5, 9),0) + 1, 4, 0) AS NUM" +
				"FROM (" +
				"  SELECT SUBSTR(T.JUDGE_STOVE_NO, 1, 9) AS NUM" +
				"  FROM MIL_PLAN T" +
				"  WHERE T.PLINE_CODE = 'C072'" +
				"    AND T.JUDGE_STOVE_NO LIKE 'D'||TO_CHAR(SYSDATE, 'YYMM')||'%'" +
				"  UNION" +
				"  SELECT T.FEED_HEAT_NO AS NUM" +
				"  FROM PLN_ZY_ZG_M T" +
				"  WHERE T.PLINE_CODE = 'C072'" +
				"    AND T.FEED_HEAT_NO LIKE 'D'||TO_CHAR(SYSDATE, 'YYMM')||'%'" +
				")";
		return this.getData(sql);
	}
}
