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

/**
 * @author ZJAY 组批
 */
public class FrmZPResult extends DBCommon {

	/**
	 * 查询 下线管实绩
	 * 
	 * @param MilOffLineResult 下线管实体类
	 * @return
	 */
	public CoreReturnObject offLineResultQuery(com.steering.mes.zgmil.entity.MilOfflineReslutEntity MilOffLineResult, String EndTime) {
		return this.getDataBySqlID("FrmZPResult.SingleQuery", new Object[] {MilOffLineResult.getLotNo(),MilOffLineResult.getJudgeStoveNo(),
				MilOffLineResult.getOfflineTime(),EndTime
			 });
	}
	
	/**
	 * 查询 组批实绩
	 * 
	 * @param MilGroupResult 组批信息实体类
	 * @return
	 */
	public CoreReturnObject GroupResultQuery(com.steering.mes.zgmil.entity.MilOfflineGroupEntity MilGroupResult, String EndTime) {
		return this.getDataBySqlID("FrmZPResult.GroupResultQuery", new Object[] {MilGroupResult.getZpLotNo(),MilGroupResult.getZpJudgeStoveNo(),EndTime
			 });
	}
	
	/**
	 * 查询 组批实绩履历
	 * 
	 * @param MilGroupResult 组批信息实体类
	 * @return
	 */
	public CoreReturnObject GroupResumeQuery(com.steering.mes.zgmil.entity.MilOfflineGroupResumeEntity GroupingDetail, String EndTime) {
		return this.getDataBySqlID("FrmZPResult.GroupResultQueryForZS", new Object[] {EndTime
			 });
	}
	/**
	 * 查询 下线管性能批
	 * 
	 * @param MilOffLineResult
	 * @return
	 */
	public CoreReturnObject simpleResultQuery(String JudgeStoveNo) {
		return this.getDataBySqlID("FrmZPResult.SimpleQuery", new Object[] {JudgeStoveNo});
	}
	
	/**
	 * 查询 下线管性能批分组
	 * 
	 * @param MilOffLineResult
	 * @return
	 */
	public CoreReturnObject simpleDetailResultQuery(String JudgeStoveNo) {
		return this.getDataBySqlID("FrmZPResult.SimpleDetailQuery", new Object[] {JudgeStoveNo});
	}
	/**
	 * 查询 待组批下线管性能批分组
	 * 
	 * @param MilOffLineResult
	 * @return
	 */
	public CoreReturnObject waitZPQuery(String JudgeStoveNo) {
		return this.getDataBySqlID("FrmZPResult.waitZPQuery", new Object[] {JudgeStoveNo});
	}
	
	
	/**
	 * 修改性能批分组状态
	 * 
	 * @param MilOffLineResult
	 * @return
	 */
	public CoreReturnObject updateSimpleGropuStatus(String JudgeStoveNo,String SimpleNo,Integer GroupNo,String Status) {
		return this.getDataBySqlID("FrmZPResult.UpdateSimpleGroupStatus", new Object[] {Status,JudgeStoveNo,SimpleNo,GroupNo});
	}
	
	
	/**
	 * 组批
	 * 
	 * @param MilOffLineResult
	 * @return
	 */
	public CoreReturnObject zp(ArrayList GroupEntityList,com.steering.mes.zgmil.entity.MilOfflineGroupEntity GroupingEntity) {
		//组炉操作
		//1-新增组炉实绩
		//2-新增组炉履历
		//3-修改性能批分组状态
		  ArrayList<String> sqllist = new ArrayList<String>();
		  
		  String sql = "";
		  String sql1 = "";
		  for(int i=0;i<GroupEntityList.size();i++)
		  {
			  //新增组炉履历
			  sql =  CoreXmlSqlParsersFactory.getSql("FrmZPResult.InsertGroupResume");
			  sql = EntityTool.fillSqlByEntity(sql, GroupEntityList.get(i));
			  sqllist.add(sql);
			  //修改性能批分组状态
			  sql1 =  CoreXmlSqlParsersFactory.getSql("FrmZPResult.UpdateSimpleGroupStatusForEntity");
			  sql1 = EntityTool.fillSqlByEntity(sql1, GroupEntityList.get(i));
			  sqllist.add(sql1);
		  }
		  //新增组炉实绩
		  String sql2 = CoreXmlSqlParsersFactory.getSql("FrmZPResult.InsertGroup");
		  sql2 =  EntityTool.fillSqlByEntity(sql2, GroupingEntity);
		  sqllist.add(sql2);
		  return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);	
	
	}
	
	
	/**
	 * 性能批分组  
	 * 1-新增分组
	 * 2-修改分组支数
	 * @param MilOffLineResult
	 * @return
	 */
	public CoreReturnObject grouping(String JudgeStoveNo,String SimpleNo,Integer GroupNo,Integer GroupNum,String User,String Shift,String Group) {
		 CoreReturnObject cro = new CoreReturnObject();
		   Connection conn = null;
		   PreparedStatement psta = null;
		   String sql = "";
		   try
		   {
			   conn = this.getDao("TgDao").getConnection();
			   ResultSet rs = null;
			   int MaxGroupNo = 0;
			   conn.setAutoCommit(false);
			   sql = "select max(group_no) from MIL_SIMPLE_DETAIL where JUDGE_STOVE_NO ='"+ JudgeStoveNo  +"' and SIMPLE_LOT_NO = '"+ SimpleNo +"'";
			   psta = conn.prepareStatement(sql);
			   rs = psta.executeQuery();
			   while(rs.next())
			   {
				   MaxGroupNo = rs.getInt(1);  //查询当前性能批最大组号
			   }
			   rs.close();
			   psta.close();
			   //新增分组
			   sql = "insert into MIL_SIMPLE_DETAIL(JUDGE_STOVE_NO,SIMPLE_LOT_NO,GROUP_NO,COUNT,COL_USER,COL_SHIFT,COL_GROUP)";
			   sql += "  values ('"+JudgeStoveNo+"','"+SimpleNo+"',"+ MaxGroupNo+1 +",'"+ User+"','"+Shift+"','"+Group+"')";
			   psta = conn.prepareStatement(sql);
			   psta.executeUpdate();
			   psta.close();
			   //修改当前分组支数
			   sql = "update set COUNT = COUNT - "+GroupNum+" where JUDGE_STOVE_NO = '" +JudgeStoveNo +"' and SIMPLE_LOT_NO = '" + SimpleNo+"' and GROUP_NO = "+GroupNo +"";
			   psta = conn.prepareStatement(sql);
			   psta.executeUpdate();
			   psta.close();
			   
			   conn.commit();
			   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;

		
	}
}
