/**
 * 
 */
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.util.ArrayList;

import javax.script.ScriptException;

import antlr.collections.List;

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.MilEtResultEntity;
import com.steering.mes.zgmil.entity.MilMtResultEntity;
import com.steering.mes.zgmil.entity.MilOfflineReslutEntity;
import com.steering.mes.zgmil.entity.MilPlanEntity;
import com.steering.mes.zgmil.entity.MilQaRackEntity;
import com.steering.mes.zgmil.entity.MilStraighRackEntity;
import com.steering.mes.zgmil.entity.MilStraighteningResultEntity;
import com.steering.mes.zgmil.entity.MilTestingRackEntity;
import com.steering.mes.zgmil.entity.MilUlResulEntity;

/**
 * @author ZJAY 返线操作方法类
 * 
 */
public class FrmBackLine extends DBCommon {

	/**
	 * 查询下线实绩
	 * 
	 */
	public CoreReturnObject GetOfflineResult( String node) {
		String sql="";
		if(node.equals("0"))
		{
			sql=CoreXmlSqlParsersFactory
			.getSql("BackLine.GetOfflineResult0");
		}
		if(node.equals("1"))
		{
			sql=CoreXmlSqlParsersFactory
			.getSql("BackLine.GetOfflineResult1");
		}
		return this.getData(sql);
	}

	/**
	 * 返线操作
	 * 1.修改下线实绩，2.修改主表下线状态3，修改对应 实绩表返线支数4,，修改对应跟踪记录表下线状态
	 * @param heatno
	 * @param Node工序
	 *            0-矫直 1-探伤
	 * @param test
	 *            0-矫直，1-ET，2-MT，3-UT
	 * @param BackLineNum
	 *            返线支数
	 * @param BackLineNumAll
	 *            可返线支数
	 * @param DelNums
	 *            剔除总支数
	 * @return
	 */
	public CoreReturnObject BackLine(String heatno, String Node, String test,
			Integer BackLineNum, Integer BackLineNumAll, Integer DelNums,String colno) {
		
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement pstaResult = null;//更新实绩
		PreparedStatement pstaRack = null;//更新跟踪记录表
		PreparedStatement pstaplan = null;//更新主计划
		PreparedStatement pstaoffline=null;//更新下线支数
		PreparedStatement pstats=null;
		PreparedStatement pstaNum = null;
		PreparedStatement pstaqa=null;
		
		ResultSet rsNum = null;
		
		String sqlResult = "";
		String sqlRack = "";
		String sqlplan = "";
		String sqloffline="";
		String sqlts="";
		String sqlNum = "";
		String sqlqa="";

		int AddScrapSlabCount1 = 0;
		int AddScrapSlabCount2 = 0;
		int DelNum=0;
		
		try
		{
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);
			
			if (Node.equals("0"))// 矫直工序
			{	
				sqlRack=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateRack0");
				sqlts=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateRack1");
				if(test.equals("1"))//1#台
				{
					sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult01");
					sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult01");
				}
				if(test.equals("2"))//2#台
				{
					sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult02");
					sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult02");
				}
				if(test.equals("3"))//3#台
				{
					sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult03");
					sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult03");
				}
				
				pstaoffline=conn.prepareStatement(sqloffline);
				pstaoffline.setInt(1, BackLineNum);
				pstaoffline.setInt(2, BackLineNum);
				if(BackLineNumAll.equals(BackLineNum))
				{
					pstaoffline.setString(3, "1");
				}
				else
				{
					pstaoffline.setString(3, "0");
				}
				pstaoffline.setString(4, heatno);
				pstaoffline.executeUpdate();
				//pstaResult=conn.prepareStatement(sqlResult);
				
				pstaRack=conn.prepareStatement(sqlRack);
				int last=BackLineNum;
				pstaRack.setString(1, heatno);
				pstaRack.setString(2, test);
				pstaRack.setInt(3, last);
				AddScrapSlabCount1 = pstaRack.executeUpdate();
				
				pstats=conn.prepareStatement(sqlts);
				int last1=BackLineNum;
				pstats.setString(1, heatno);
				pstats.setString(2, test);
				pstats.setInt(3, last1);
				AddScrapSlabCount1 = pstats.executeUpdate();
				
				pstaResult=conn.prepareStatement(sqlResult);
				pstaResult.setInt(1, BackLineNum);
				pstaResult.setInt(2, BackLineNum);
				pstaResult.setString(3, heatno);
				pstaResult.executeUpdate();
				
				sqlNum="SELECT count(*) FROM MIL_STRAIGH_RACK t WHERE t.JUDGE_STOVE_NO='";
				sqlNum+=heatno;
				sqlNum+="' AND t.status=1 AND t.SEQ BETWEEN ";
				sqlNum+=BackLineNumAll - BackLineNum + 1+DelNums;
				sqlNum+=" AND ";
				sqlNum+=BackLineNumAll+DelNums;
				
				pstaNum=conn.prepareStatement(sqlNum);
				rsNum=pstaNum.executeQuery();
				while(rsNum.next())
				{
					DelNum=rsNum.getInt(1);
				}
			}
			
			
			if (Node.equals("1"))// 探伤工序
			{
				
				sqlRack=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateRack1");
				if(test.equals("1"))//ET
				{
					if(colno.equals("1"))
					{  
						sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult11");
						sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult111");
					}
					if(colno.equals("2"))
					{
						sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult12");
						sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult112");
					}
					if(colno.equals("3"))
					{
						sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult13");
						sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult113");
					}
				}
				if(test.equals("2"))//MT
				{
					if(colno.equals("1"))
					{
						sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult11");
						sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult121");
					}
					if(colno.equals("1"))
					{
						sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult12");
						sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult122");
					}
				}
				if(test.equals("3"))//UT
				{
					if(colno.equals("1"))
					{
						sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult11");
						sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult131");
					}
					if(colno.equals("2"))
					{
						sqloffline=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateOfflineResult12");
						sqlResult = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateResult132");
					}
				}
				
				pstaoffline=conn.prepareStatement(sqloffline);
				pstaoffline.setInt(1, BackLineNum);
				pstaoffline.setInt(2, BackLineNum);
				if(BackLineNumAll.equals(BackLineNum))
				{
					pstaoffline.setString(3, "1");
				}
				else
				{
					pstaoffline.setString(3, "0");
				}
				pstaoffline.setString(4, heatno);
				pstaoffline.executeUpdate();
				
				pstaResult=conn.prepareStatement(sqlResult);
				pstaResult.setInt(1, BackLineNum);
				pstaResult.setInt(2, BackLineNum);
				pstaResult.setString(3, heatno);
				pstaResult.executeUpdate();
				
				pstaRack=conn.prepareStatement(sqlRack);
				int last=BackLineNum;
				pstaRack.setString(1, heatno);
				pstaRack.setString(2, colno);
				pstaRack.setInt(3, last);
				AddScrapSlabCount1 = pstaRack.executeUpdate();
				
				sqlNum="SELECT count(*) FROM MIL_TESTING_RACK t WHERE t.JUDGE_STOVE_NO='";
				sqlNum+=heatno;
				sqlNum+="' AND t.status=1 AND t.SEQ BETWEEN ";
				sqlNum+=BackLineNumAll - BackLineNum + 1+DelNums;
				sqlNum+=" AND ";
				sqlNum+=BackLineNumAll+DelNums;
				
				pstaNum=conn.prepareStatement(sqlNum);
				rsNum=pstaNum.executeQuery();
				while(rsNum.next())
				{
					DelNum=rsNum.getInt(1);
				}
			}
			//------------更新跟踪记录表方法  旧 （有问题）
			//Update MIL_TESTING_RACK t
			//set t.OFFLINE_FLAG ='2'
			//WHERE
			//t.Judge_Stove_No=?
			//AND t.STATUS=0
			//AND t.SEQ between ? and ?
			//pstaRack=conn.prepareStatement(sqlRack);
			
			//int first=0;
			//if(BackLineNumAll.equals(BackLineNum))
			//{
			// first = 1;   //ScrapNumBefore - OfflineNum + 1+DelNums
			//}
			//else
			//{
			//	first=BackLineNumAll - BackLineNum+1+DelNums;
			//	if(DelNum>0)
			//	{
			//		first=first-DelNum;
			//	}
			//}
	
			//pstaRack.setString(1, heatno);
			//pstaRack.setInt(2, first);
			//pstaRack.setInt(3, BackLineNumAll+DelNums);
			//AddScrapSlabCount1 = pstaRack.executeUpdate();
		////-------矫直部分跟踪记录表修改
			sqlqa=CoreXmlSqlParsersFactory.getSql("BackLine.UpdateQARack");
			pstaqa = conn.prepareStatement(sqlqa);
			pstaqa.setString(1, heatno);
			pstaqa.setInt(2, BackLineNum);
			AddScrapSlabCount1 = pstaqa.executeUpdate();
			pstaqa.close();

			//-----------更新主表下线状态
			sqlplan = CoreXmlSqlParsersFactory.getSql("BackLine.UpdateMILPLAN");
			pstaplan = conn.prepareStatement(sqlplan);
			if(Node.equals("0"))
			{
				pstaplan.setString(1, "70");
			}
			if(Node.equals("1"))
			{
				pstaplan.setString(1, "80");
			}
			pstaplan.setString(2, heatno);
			AddScrapSlabCount2 = pstaplan.executeUpdate();
			pstaplan.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(AddScrapSlabCount1);
		return cro;
	}
	public CoreReturnObject GetBackPlan(String heatno)
	{
		String sql="select * from MIL_PLAN t where t.judge_stove_no='";
		sql+=heatno;
		sql+="'";
		return this.getData(sql);
	}
	
	public CoreReturnObject GetEndPlan(String  node,String  PlineCod )
	{
		String status="";
		String nodestatus="";
		if(node.equals("0"))
		{
			status="75";
			nodestatus="80";
		}
		if(node.equals("1"))
		{
			status="85";
			nodestatus="90";
		}
		String sql="select  t.feed_time ,t.start_time from MIL_PLAN t WHERE t.pline_code='";
		sql+=PlineCod;
		sql+="' AND t.node_status='";
		sql+=nodestatus;
		sql+="' AND t.status='";
		sql+=status;
		sql+="' AND t.feed_time= (select  max(t1.feed_time) from MIL_PLAN t1 where t1.pline_code='";
		sql+= PlineCod;
		sql+="' AND t1.node_status='";
		sql+=nodestatus;
		sql+="' AND t1.status='";
		sql+=status;
		sql+="' )";
		return this.getData(sql);
	}
	/***
	 * 查询当前正在开始探伤的炉计划时间
	 * @param heatNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryStartTime(String plineCode)
	{
		return this.getDataBySqlID("FrmBackLine.doQueryStartTime", new Object[]{plineCode});
	}
	
	public CoreReturnObject GetBackResult(String heatno,String node,String test)
	{
		
		String sql="select * from ";
		if(node.equals("0"))
		{
			sql+=" MIL_STRAIGHTENING_RESULT t ";
		}
		if(node.equals("1"))
		{
			if(test.equals("1"))
			{
				sql+=" MIL_ET_RESULT t ";
			}
			if(test.equals("2"))
			{
				sql+=" MIL_MT_RESULT t ";
			}
			if(test.equals("3"))
			{
				sql+=" MIL_UL_RESUL t ";
			}
		}
		sql+=" where t.judge_stove_no='";
		sql+=heatno;
		sql+="'";
		return this.getData(sql);
	}
	
	public CoreReturnObject GetBackRack(String heatno,String node,String test,String colno,Integer backlinenum)
	{
		String TableName="";
		//String col="";
		if(node.equals("0"))
		{
			TableName="MIL_STRAIGH_RACK";
			//col=test;
		}
		if(node.equals("1"))
		{
			TableName="MIL_TESTING_RACK";
			//col=colno;
		}
		if(node.equals("2"))
		{
			TableName="MIL_QA_RACK";
		}
		
		String sql="select t.* from ";
		sql+=TableName;
		sql+=" t where  t.judge_stove_no='"+heatno+"' AND  t.seq in (select SEQ  from (select a.SEQ AS SEQ from ";
		sql+=TableName;
		sql+=" a WHERE a.STATUS = 0 AND a.offline_flag = 1 AND a.judge_stove_no ='";
		sql+=heatno;
		//sql+="' AND a.col_no='";
		//sql+=col;
		sql+="' order by a.seq desc) WHERE rownum between 1 and ";
		sql+=backlinenum;
		sql+=" )";
		return this.getData(sql);
	}
	
	/**
	 * 1.新增下线计划，2.新增管号队列，4.插入实绩表3.修改原管号队列4.修改下线实绩
	 * @param arr
	 * @param heatno
	 * @param Node
	 * @param test
	 * @return
	 */
	public CoreReturnObject BackLineNEW(ArrayList arr, String heatno,
			String Node, String test ,String  process) {
		MilPlanEntity plan = (MilPlanEntity) arr.get(0);
		MilStraighteningResultEntity milstra = (MilStraighteningResultEntity) arr.get(1);
		MilEtResultEntity milet = (MilEtResultEntity) arr.get(2);
		MilMtResultEntity milmt = (MilMtResultEntity) arr.get(3);
		MilUlResulEntity milut = (MilUlResulEntity) arr.get(4);
		ArrayList<MilStraighRackEntity> liststraight = (ArrayList<MilStraighRackEntity>) arr
				.get(5);
		ArrayList<MilTestingRackEntity> listtest = (ArrayList<MilTestingRackEntity>) arr
				.get(6);
		ArrayList<MilQaRackEntity> listqa = (ArrayList<MilQaRackEntity>) arr
				.get(7);
		Integer BackLineNum = Integer.valueOf(String.valueOf(arr.get(8)));
		Integer BackLineNum1 = Integer.valueOf(String.valueOf(arr.get(9)));
		Integer BackLineNum2 = Integer.valueOf(String.valueOf(arr.get(10)));
		Integer BackLineNum3 = Integer.valueOf(String.valueOf(arr.get(11)));
		// 1.新增下线计划，2.新增管号队列，4.插入实绩表3.修改原管号队列4.修改下线实绩5.修改原实绩记录返线实绩6.
		ArrayList<String> sqllist = new ArrayList<String>();
		String sqladdplan = "";
		String sqladdStraightRack = "";
		String sqladdTestRack = "";
		String sqladdQaRack = "";
		String sqladdStraightResult = "";
		String sqladdTestResult = "";
		String sqlUpdateresult="";
		String sqlUpdaterackjz = "";
		String sqlUpdaterackts = "";
		String sqlUpdaterackqa = "";
		String sqlUpdateoffline = "";
		
		if(isInsert("3",test,plan.getJudgeStoveNo()))
		{
			sqladdplan=" UPDATE MIL_PLAN t SET t.branch= t.branch +"+BackLineNum;
			sqladdplan+=" WHERE t.judge_stove_no='";
			sqladdplan+=plan.getJudgeStoveNo();
			sqladdplan+="'";
		}
		else
		{
		sqladdplan = CoreXmlSqlParsersFactory.getSql("BackLine.AddMILPLAN");
		sqladdplan = EntityTool.fillSqlByEntity(sqladdplan, plan);
		}
		sqllist.add(sqladdplan);
		
		if(isInsert("0",test,milstra.getJudgeStoveNo()))
		{
			sqladdStraightResult="UPDATE MIL_STRAIGHTENING_RESULT t SET t.backline_num_total =t.backline_num_total +";
			sqladdStraightResult+=BackLineNum+ " , t.backline_num1= t.backline_num1+"+BackLineNum1+ " , t.backline_num2= t.backline_num2+";
			sqladdStraightResult+=BackLineNum2+ " , t.backline_num3=t.backline_num3 +" +BackLineNum3;
			sqladdStraightResult+=" WHERE t.judge_stove_no='"+milstra.getJudgeStoveNo()+"'";		
		}
		else
		{
		sqladdStraightResult = CoreXmlSqlParsersFactory
				.getSql("BackLine.AddStraightResult");
		sqladdStraightResult = EntityTool.fillSqlByEntity(sqladdStraightResult,
				milstra);
		}
		sqllist.add(sqladdStraightResult);

		if (Node.equals("1"))// 矫直
		{
			if(isInsert("1",test,milstra.getJudgeStoveNo()))
			{
				if(test.equals("1"))
				{
					sqladdTestResult="Update MIL_ET_RESULT t SET t.backline_num3=t.backline_num3+";
					sqladdTestResult+=BackLineNum3+" ,";
				}
				if(test.equals("2"))
				{
					sqladdTestResult="Update MIL_MT_RESULT t SET  ";
				}
				if(test.equals("3"))
				{
					sqladdTestResult="Update MIL_UL_RESUL t SET  ";
				}
				sqladdTestResult+=" t.backline_num=t.backline_num+ "+BackLineNum+" , t.backline_num1=t.backline_num1+ "+BackLineNum1+" , t.backline_num2=t.backline_num1+ "+BackLineNum2;
				sqladdTestResult+=" WHERE t.judge_stove_no='"+milstra.getJudgeStoveNo()+"'";
				
			}
			else
			{
			if (test.endsWith("1")) {
				sqladdTestResult = CoreXmlSqlParsersFactory
						.getSql("BackLine.AddETResult");
				sqladdTestResult = EntityTool.fillSqlByEntity(sqladdTestResult,
						milet);
			}
			if (test.endsWith("2")) {
				sqladdTestResult = CoreXmlSqlParsersFactory
						.getSql("BackLine.AddMTResult");
				sqladdTestResult = EntityTool.fillSqlByEntity(sqladdTestResult,
						milmt);
			}
			if (test.endsWith("3")) {
				sqladdTestResult = CoreXmlSqlParsersFactory
						.getSql("BackLine.AddUTResult");
				sqladdTestResult = EntityTool.fillSqlByEntity(sqladdTestResult,
						milut);
			}
			}
			sqllist.add(sqladdTestResult);
		}
		for(int i=0;i<liststraight.size();i++)
		{
			MilStraighRackEntity milstraight=new MilStraighRackEntity();
			milstraight=liststraight.get(i);
			sqladdStraightRack=CoreXmlSqlParsersFactory
			.getSql("BackLine.AddStraightRack");
			sqladdStraightRack = EntityTool.fillSqlByEntity(sqladdStraightRack,
					milstraight);
			sqllist.add(sqladdStraightRack);
		}
		for(int i=0;i<listtest.size();i++)
		{
			MilTestingRackEntity testrack=new MilTestingRackEntity();
			testrack=listtest.get(i);
			sqladdTestRack=CoreXmlSqlParsersFactory
			.getSql("BackLine.AddTestRack");
			sqladdTestRack = EntityTool.fillSqlByEntity(sqladdTestRack,
					testrack);
			sqllist.add(sqladdTestRack);
		}
		for(int i=0;i<listqa.size();i++)
		{
			MilQaRackEntity qa=new MilQaRackEntity();
			qa=listqa.get(i);
			sqladdQaRack=CoreXmlSqlParsersFactory
			.getSql("BackLine.AddQaRack");
			sqladdQaRack = EntityTool.fillSqlByEntity(sqladdQaRack,
					qa);
			sqllist.add(sqladdQaRack);
		}
		
		if(Node.equals("0"))
		{
			sqlUpdateresult="Update MIL_STRAIGHTENING_RESULT  t SET t.backline_num_total= t.backline_num_total+"+BackLineNum;
			sqlUpdateresult+= " , t.backline_num1= t.backline_num1+"+BackLineNum1+" , t.backline_num2=t.backline_num2+ "+BackLineNum2+" , t.backline_num3=t.backline_num3+";
			sqlUpdateresult+=BackLineNum3 +" WHERE t.judge_stove_no='" +heatno+"'";
			
			sqlUpdaterackjz = this.GetRackSql(heatno, "0",BackLineNum);
			sqlUpdaterackts = this.GetRackSql(heatno, "1",BackLineNum);
			sqlUpdaterackqa = this.GetRackSql(heatno, "2",BackLineNum);
			sqllist.add(sqlUpdateresult);
			sqllist.add(sqlUpdaterackjz);
			sqllist.add(sqlUpdaterackts);
			sqllist.add(sqlUpdaterackqa);
		}
		if(Node.equals("1"))
		{
			if(test.equals("1"))
			{
				sqlUpdateresult="Update MIL_ET_RESULT t SET t.backline_num3=t.backline_num3+";
				sqlUpdateresult+=BackLineNum3+" ,";
			}
			if(test.equals("2"))
			{
				sqlUpdateresult="Update MIL_MT_RESULT t SET  ";
			}
			if(test.equals("3"))
			{
				sqlUpdateresult="Update MIL_UL_RESUL t SET  ";
			}
			sqlUpdateresult+=" t.backline_num= t.backline_num+"+BackLineNum+" , t.backline_num1= t.backline_num1+"+BackLineNum1+" , t.backline_num2= t.backline_num2+"+BackLineNum2;
			sqlUpdateresult+=" WHERE t.judge_stove_no='"+heatno+"'";
			sqlUpdaterackts = this.GetRackSql(heatno, "1",BackLineNum);
			sqlUpdaterackqa = this.GetRackSql(heatno, "2",BackLineNum);
			
			sqllist.add(sqlUpdateresult);
			sqllist.add(sqlUpdaterackts);
			sqllist.add(sqlUpdaterackqa);
		}
		
		sqlUpdateoffline="Update MIL_OFFLINE_RESLUT t SET t.backline_num= t.backline_num+"+BackLineNum;
		sqlUpdateoffline+=" ,t.BACKLINE_NUM1= t.BACKLINE_NUM1+"+BackLineNum1+" , t.BACKLINE_NUM2= t.BACKLINE_NUM2+"+ BackLineNum2 +" , t.BACKLINE_NUM3=t.BACKLINE_NUM3+"+BackLineNum3;
		sqlUpdateoffline+=" WHERE t.judge_stove_no ='" +heatno +"' AND t.OFFLINE_PRO='"+process+"'";
		sqllist.add(sqlUpdateoffline);
		
		if(Node.equals("1"))
		{
			String sql = "UPDATE MIL_TESTING_RACK T SET T.COL_NO='' WHERE T.JUDGE_STOVE_NO = '"
				+ plan.getJudgeStoveNo() + "'  AND T.RESULT_NO = '" + plan.getId() + "' ";
			sqllist.add(sql);
		}
		 return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);	
	}
	
	public CoreReturnObject BackLineNEWBack(String heatnonew, String heatno,String process
			,String node,String test,Integer totalbefor,Integer backlinenumafter1,
			Integer backlinenumafter2,Integer backlinenumafter3,Integer backlinenumBack1,Integer backlinenumBack2,Integer backlinenumBack3)
	{
		ArrayList<String> sqllist=new ArrayList<String> ();
		int totalafter=backlinenumafter1+backlinenumafter2+backlinenumafter3;
		int totalback=backlinenumBack1+backlinenumBack2+backlinenumBack3;
	    String sqlplan=" UPDATE MIL_PLAN t SET t.branch= "+totalafter;
	    sqlplan+=" WHERE t.judge_stove_no='";
	    sqlplan+=heatnonew;
	    sqlplan+="'";
		sqllist.add(sqlplan);
		
		String sqlStraightResult="";
		sqlStraightResult="UPDATE MIL_STRAIGHTENING_RESULT t SET t.backline_num_total =";
		sqlStraightResult+=totalafter+ " , t.backline_num1= "+backlinenumafter1+ " , t.backline_num2= ";
		sqlStraightResult+=backlinenumafter2+ " , t.backline_num3= " +backlinenumafter3;
		sqlStraightResult+=" WHERE t.judge_stove_no='"+heatnonew+"'";
		sqllist.add(sqlStraightResult);
		
		if(node.equals("1"))
		{
			String sqlTestResult="";
			if(test.equals("1"))
			{
				sqlTestResult="Update MIL_ET_RESULT t SET t.backline_num3=";
				sqlTestResult+=backlinenumafter3+" ,";
			}
			if(test.equals("2"))
			{
				sqlTestResult="Update MIL_MT_RESULT t SET  ";
			}
			if(test.equals("3"))
			{
				sqlTestResult="Update MIL_UL_RESUL t SET  ";
			}
			sqlTestResult+=" t.backline_num=  "+totalafter+" , t.backline_num1=  "+backlinenumafter1+" , t.backline_num2=  "+backlinenumafter2;
			sqlTestResult+=" WHERE t.judge_stove_no='"+heatnonew+"'";
			sqllist.add(sqlTestResult);
		}
		String sqlstraRack1="UPDATE MIL_STRAIGH_RACK T SET T.COL_NO='1' WHERE T.SEQ BETWEEN  1 AND "+backlinenumafter1+" AND t.judge_stove_no='"+heatnonew+"'";
		String sqlstraRack2="UPDATE MIL_STRAIGH_RACK T SET T.COL_NO='2' WHERE T.SEQ BETWEEN "+ (backlinenumafter1+1)+"  AND "+(backlinenumafter2+backlinenumafter1)+" AND t.judge_stove_no='"+heatnonew+"'";;
		String sqltestRack1="UPDATE MIL_TESTING_RACK T SET T.COL_NO='1' WHERE T.SEQ BETWEEN  1 AND "+backlinenumafter1+" AND t.judge_stove_no='"+heatnonew+"'";;
		String sqltestRack2="UPDATE MIL_TESTING_RACK T SET T.COL_NO='2' WHERE T.SEQ BETWEEN "+ (backlinenumafter1+1)+"  AND "+(backlinenumafter2+backlinenumafter1)+" AND t.judge_stove_no='"+heatnonew+"'";;;
		String sqlqaRack1="UPDATE MIL_QA_RACK T SET T.COL_NO='1' WHERE T.SEQ BETWEEN  1 AND "+backlinenumafter1 +" AND t.judge_stove_no='"+heatnonew+"'";;;
		String sqlqaRack2="UPDATE MIL_QA_RACK T SET T.COL_NO='2' WHERE T.SEQ BETWEEN "+ (backlinenumafter1+1)+"  AND "+(backlinenumafter2+backlinenumafter1) +" AND t.judge_stove_no='"+heatnonew+"'";;;
		sqllist.add(sqlstraRack1);
		sqllist.add(sqlstraRack2);
		sqllist.add(sqltestRack1);
		sqllist.add(sqltestRack2);
		//sqllist.add(sqlqaRack1);
		//sqllist.add(sqlqaRack2);
		if(test.equals("1"))
		{
		String sqlstraRack3="UPDATE MIL_STRAIGH_RACK T SET T.COL_NO='3' WHERE T.SEQ BETWEEN  "+(backlinenumafter2+backlinenumafter1+1)+" AND "+totalafter +" AND t.judge_stove_no='"+heatnonew+"'";;;
		String sqltestRack3="UPDATE MIL_TESTING_RACK T SET T.COL_NO='3' WHERE T.SEQ BETWEEN  "+(backlinenumafter2+backlinenumafter1+1)+" AND "+totalafter +" AND t.judge_stove_no='"+heatnonew+"'";;;
		String sqlqaRack3="UPDATE MIL_QA_RACK T SET T.COL_NO='3' WHERE T.SEQ BETWEEN  "+(backlinenumafter2+backlinenumafter1+1)+" AND "+totalafter +" AND t.judge_stove_no='"+heatnonew+"'";;;
		sqllist.add(sqlstraRack3);
		sqllist.add(sqltestRack3);
		//sqllist.add(sqlqaRack3);
		}
		
		String sqlstraRackdel=" DELETE FROM  MIL_STRAIGH_RACK T  WHERE T.SEQ BETWEEN "+ (totalafter+1) +" AND "+ totalbefor +" AND T.JUDGE_STOVE_NO='" +heatnonew+"'";
		String sqltestRackdel=" DELETE FROM  MIL_TESTING_RACK T  WHERE T.SEQ BETWEEN "+ (totalafter+1) +" AND "+ totalbefor +" AND T.JUDGE_STOVE_NO='" +heatnonew+"'";
		String sqlqaRackdel=" DELETE FROM  MIL_QA_RACK T  WHERE T.SEQ BETWEEN "+ (totalafter+1) +" AND "+ totalbefor +" AND T.JUDGE_STOVE_NO='" +heatnonew+"'";
		sqllist.add(sqlstraRackdel);
		sqllist.add(sqltestRackdel);
		sqllist.add(sqlqaRackdel);
		
		if(node.equals("0"))
		{
			String sqlUpdateresult="Update MIL_STRAIGHTENING_RESULT  t SET t.backline_num_total= t.backline_num_total-"+totalback;
			sqlUpdateresult+= " , t.backline_num1= t.backline_num1-"+backlinenumBack1+" , t.backline_num2=t.backline_num2- "+backlinenumBack1+" , t.backline_num3=t.backline_num3-";
			sqlUpdateresult+=backlinenumBack3 +" WHERE t.judge_stove_no='" +heatno+"'";
			
			String sqlUpdaterackjz = this.GetRackSqlBack(heatno, "0",totalback);
			String sqlUpdaterackts = this.GetRackSqlBack(heatno, "1",totalback);
			String sqlUpdaterackqa = this.GetRackSqlBack(heatno, "2",totalback);
			sqllist.add(sqlUpdateresult);
			sqllist.add(sqlUpdaterackjz);
			sqllist.add(sqlUpdaterackts);
			sqllist.add(sqlUpdaterackqa);
			
		}
		if(node.equals("1"))
		{
			String sqlUpdateresult="";
			if(test.equals("1"))
			{
			    sqlUpdateresult="Update MIL_ET_RESULT t SET t.backline_num3=t.backline_num3-";
				sqlUpdateresult+=backlinenumBack3+" ,";
			}
			if(test.equals("2"))
			{
				sqlUpdateresult="Update MIL_MT_RESULT t SET  ";
			}
			if(test.equals("3"))
			{
				sqlUpdateresult="Update MIL_UL_RESUL t SET  ";
			}
			sqlUpdateresult+=" t.backline_num= t.backline_num-"+totalback+" , t.backline_num1= t.backline_num1-"+backlinenumBack1+" , t.backline_num2= t.backline_num2-"+backlinenumBack2;
			sqlUpdateresult+=" WHERE t.judge_stove_no='"+heatno+"'";
			String sqlUpdaterackts = this.GetRackSqlBack(heatno, "1",totalback);
			String sqlUpdaterackqa = this.GetRackSqlBack(heatno, "2",totalback);
			sqllist.add(sqlUpdateresult);
			sqllist.add(sqlUpdaterackts);
			sqllist.add(sqlUpdaterackqa);
		}
		String sqlUpdateoffline="Update MIL_OFFLINE_RESLUT t SET t.backline_num= "+totalafter;
		sqlUpdateoffline+=" ,t.BACKLINE_NUM1= "+backlinenumafter1+" , t.BACKLINE_NUM2= "+ backlinenumafter2 +" , t.BACKLINE_NUM3="+backlinenumafter3;
		sqlUpdateoffline+=" WHERE t.judge_stove_no ='" +heatno +"' AND t.OFFLINE_PRO='"+process+"'";
		sqllist.add(sqlUpdateoffline);
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);	
	}
	
	public CoreReturnObject AfterBackLineNEWBack( String heatnonew,String heatno,String process,String node,String test )
	{
		ArrayList<String> sqllist=new ArrayList<String>();
		String sqlplan=" delete from MIL_PLAN T WHERE T.JUDGE_STOVE_NO='"+heatnonew+"'"; 
		String sqlstraight=" delete from MIL_STRAIGHTENING_RESULT T WHERE T.JUDGE_STOVE_NO='"+heatnonew+"'"; 
		if(node.equals("0"))
		{
			String sqltestresult=" delete from ";
			if(test.equals("1"))
			{
				sqltestresult+=" MIL_ET_RESULT T";
			}
			if(test.equals("2"))
			{
				sqltestresult+=" MIL_MT_RESULT T";
			}
			if(test.equals("3"))
			{
				sqltestresult+=" MIL_UL_RESUL T";
			}
			sqltestresult+=" WHERE T.JUDGE_STOVE_NO='"+heatnonew+"'"; 
			sqllist.add(sqltestresult);
		}
		String sqlUpdateoffline="Update MIL_OFFLINE_RESLUT t SET t.backline_num= 0 ";
		sqlUpdateoffline+=" ,t.BACKLINE_NUM1= 0 , t.BACKLINE_NUM2= 0 , t.BACKLINE_NUM3= 0 ";
		sqlUpdateoffline+=" WHERE t.judge_stove_no ='" +heatno +"' AND t.OFFLINE_PRO='"+process+"'";
		sqllist.add(sqlplan);
		sqllist.add(sqlstraight);
		sqllist.add(sqlUpdateoffline);	
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	public CoreReturnObject isInsertBack(String node,String test,String heatno)
	{
		String sql=" Select * From";
		if(node.equals("0"))
		{
			sql+=" MIL_STRAIGHTENING_RESULT t";
		}
		if(node.equals("1"))
		{
			if(test.equals("1"))
			{
			sql+=" MIL_ET_RESULT t";
			}
			if(test.equals("2"))
			{
			sql+=" MIL_MT_RESULT t";
			}
			if(test.equals("3"))
			{
			sql+=" MIL_UL_RESUL t";
			}
		}
		if(node.equals("3"))
		{
			sql+=" MIL_PLAN t";
		}
		sql+=" WHERE t.judge_stove_no='";
		sql+=heatno;
		sql+="'";
		return this.getData(sql);
	}
	public boolean isInsert(String node,String test,String heatno)
	{
		Connection conn = null;
		PreparedStatement psta = null;
		ResultSet re = null;
		String sql=" Select * From ";
		boolean result=false; 
		try
		{
			conn = this.getDao("TgDao").getConnection();
			conn.setAutoCommit(false);
			
			if(node.equals("0"))
			{
				sql+=" MIL_STRAIGHTENING_RESULT t";
			}
			if(node.equals("1"))
			{
				if(test.equals("1"))
				{
				sql+=" MIL_ET_RESULT t";
				}
				if(test.equals("2"))
				{
				sql+=" MIL_MT_RESULT t";
				}
				if(test.equals("3"))
				{
				sql+=" MIL_UL_RESUL t";
				}
			}
			if(node.equals("3"))
			{
				sql+=" MIL_PLAN t";
			}
			sql+=" WHERE t.judge_stove_no='";
			sql+=heatno;
			sql+="'";
			psta=conn.prepareStatement(sql);
			re=psta.executeQuery();
			while(re.next())
			{
				result=true;
			}
		}
		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 result;
	}
	public String GetRackSqlBack(String heatno,String node,Integer backlinenum)
	{
		String TableName="";
		if(node.equals("0"))
		{
			TableName="MIL_STRAIGH_RACK";
		}
		if(node.equals("1"))
		{
			TableName="MIL_TESTING_RACK";
		}
		if( node.equals("2"))
		{
			TableName="MIL_QA_RACK";
		}
		
		String sql="Update ";
		sql+=TableName;
		sql+=" t SET t.offline_flag = 1 ";
		sql+="  where t.judge_stove_no='"+heatno+"' AND t.seq in (select SEQ  from (select a.SEQ AS SEQ from ";
		sql+=TableName;
		sql+=" a WHERE a.STATUS = 0 AND a.offline_flag = 2 AND a.judge_stove_no ='";
		sql+=heatno;
		//sql+="' AND a.col_no='";
		//sql+=col;
		sql+="' order by a.seq ) WHERE rownum between 1 and ";
		sql+=backlinenum;
		sql+=" )";
		return sql;
	}

	public String GetRackSql(String heatno,String node,Integer backlinenum)
	{
		String TableName="";
		if(node.equals("0"))
		{
			TableName="MIL_STRAIGH_RACK";
		}
		if(node.equals("1"))
		{
			TableName="MIL_TESTING_RACK";
		}
		if( node.equals("2"))
		{
			TableName="MIL_QA_RACK";
		}
		
		String sql="Update ";
		sql+=TableName;
		sql+=" t SET t.offline_flag = 2 ";
		sql+="  where t.judge_stove_no='"+heatno+"' AND t.seq in (select SEQ  from (select a.SEQ AS SEQ from ";
		sql+=TableName;
		sql+=" a WHERE a.STATUS = 0 AND a.offline_flag = 1 AND a.judge_stove_no ='";
		sql+=heatno;
		//sql+="' AND a.col_no='";
		//sql+=col;
		sql+="' order by a.seq desc) WHERE rownum between 1 and ";
		sql+=backlinenum;
		sql+=" )";
		return sql;
	}
	/**
	 * 查询已返线支数
	 * @param heatno
	 * @param node
	 * @return
	 */
	public CoreReturnObject GetCountHAS(String heatno,String node,String test,String colno)
	{
		String sql="";
		if(node.equals("0"))
		{
			sql="select count(*) as counts from MIL_STRAIGH_RACK t WHERE t.Judge_Stove_No='";
			sql+=heatno;
			sql+="' AND t.status=0 AND t.offline_flag =2 ";
			sql+=" AND t.col_no=";
			sql+=test;
		}
		if(node.equals("1"))
		{
			sql="select count(*) as counts from MIL_TESTING_RACK t WHERE t.Judge_Stove_No='";
			sql+=heatno;
			sql+="' AND t.status=0 AND t.offline_flag =2";
			sql+=" AND t.col_no=";
			sql+=colno;
		}
		return this.getData(sql);
	}

}
