package com.steering.pss.judge.Bll;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.judge.model.QcmBcPassApplyEntity;
import com.steering.pss.judge.model.QcmBcPassConfigEntity;
import com.steering.pss.plnsaleord.stove.PlanFurnProduction;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

public class BllZcColligatePassApply  extends DBCommon {
	
	/**
	 * 审核
	 * @param passApplyPks
	 * @param user
	 * @param auditResult
	 * @param memo
	 * @return
	 */
	public CoreReturnObject Audite(ArrayList<String[]> parms, String userId, String user,
			String auditResult, String memo){
		Connection con = null;
		Statement st = null;
		Date now = new Date();
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			SqlBuilder b;
			ArrayList<String> judgeStoveNos = new ArrayList<String>();
			for(String[] parm : parms){
				String passApplyNo = parm[0];
				String auditStep = parm[1];
				b = new SqlBuilder("JdgQcmZcPassApplyDAL.queryByPassApplyNo");
				b.Fill(new String[]{ passApplyNo });
				QcmBcPassApplyEntity passApply = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmBcPassApplyEntity.class);
				b = new SqlBuilder("JdgQcmZcPassFlow.getMaxAuditStep");
                b.Fill(new String[]{ passApply.getPassApplyNo() });
                QcmBcPassConfigEntity maxAuditStep = EntityTool.getEntityBySqlNoClsCon(
                		b.toString(), con, QcmBcPassConfigEntity.class);
				if(passApply.getType().equals("1")){
					//新增炉计划
					if(auditResult.equals("1") && auditStep.equals(maxAuditStep.getAuditStep())){
						//*********************更新材料表
//						b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryByBatchNo");
//		        		b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(),
//		        				passApply.getBatchNo(), passApply.getProcessCode() });
//		        		List<QcmZgJugdeApplyEntity> zgJudgeApplys = EntityTool.getEntitysBySqlNoClsCon(
//		        				b.toString(), con, QcmZgJugdeApplyEntity.class);
//		                b = new SqlBuilder("JdgMatBcM.updateInstancy");
//		                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(),
//		                		passApply.getBatchNo(), passApply.getProcessCode(), passApply.getType() });
//		                st.executeUpdate(b.toString());
		                b = new SqlBuilder("JdgMatBcM.updateBelong");
		                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(),
		                		passApply.getBatchNo(), "002001001", "生产计划部" });
		                st.executeUpdate(b.toString());
		                
		                //更新材料历史表
//		                b = new SqlBuilder("JdgMatBcBackup.updateInstancy");
//		                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(),
//		                		passApply.getBatchNo(), passApply.getProcessCode(), passApply.getType() });
//		                st.executeUpdate(b.toString());
		                b = new SqlBuilder("JdgMatBcBackup.updateBelong");
		                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(),
		                		passApply.getBatchNo(), "002001001", "生产计划部" });
		                st.executeUpdate(b.toString());
						
		            	PlanFurnProduction.addPlnZyC(passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(),
		            			user, con, passApply.getBatchNo());
		            	
		            	//更新管坯申请表的紧急状态
//		            	b = new SqlBuilder("QcmGpJugdeApplyDAL.setInstancy");
//		            	b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
//		            			passApply.getJudgeStoveNo(), "1"});
//		            	st.executeUpdate(b.toString());
		            	//修改申请判定表的结转年月
		                b = new SqlBuilder("QcmZgJugdeApplyDAL.setBalYearMonth");
		                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(), 
		                		passApply.getBatchNo(), passApply.getProcessCode(),
								"C072,C091,C092,C098,C099,C100,C102,C103,C104".contains(passApply.getPlineCode())?ComBaseInfo.GetBalYear_2416(now):ComBaseInfo.GetBalYear(now)});
		                st.executeUpdate(b.toString());
						
						//新增入库表
//		        		FrmGpIqInList gpIqInList = new FrmGpIqInList();
//		        		for(QcmGpJudgeApplyEntity gpJudgeApplySub : gpJudgeApplys){
//		        			String gpJudgeApplyJson = EntityTool.getGsonByEntity(gpJudgeApplySub);
//		                    ArrayList<String> gpJudgeApplyJsons = new ArrayList<String>();
//		                    gpJudgeApplyJsons.add(gpJudgeApplyJson);
//		                    gpIqInList.insertGpInList(con, gpJudgeApplyJsons, user);
//		        		}
					}
				}
				
				//修改放行审核流程表。
//				b = new SqlBuilder("JdgQcmBcPassFlow.getAuditCnt");
//				b.Fill(new String[]{ passApply.getPassApplyNo() });
//				GenericEntity auditCnt = EntityTool.getEntityBySqlNoClsCon(
//						b.toString(), con, GenericEntity.class);
				
				if(auditResult.equals("")){
					if(auditStep.equals(maxAuditStep.getAuditStep())){
	                	b = new SqlBuilder("JdgQcmZcPassFlow.clearAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, "1" });
	    				st.executeUpdate(b.toString());
	                }else{
	                	b = new SqlBuilder("JdgQcmZcPassFlow.clearAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, "0" });
	    				st.executeUpdate(b.toString());
	    				b = new SqlBuilder("JdgQcmZcPassFlow.setCrtAuditStep");
	    				String nextAuditStep = String.valueOf(Integer.parseInt(auditStep) + 1);
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), nextAuditStep, "1"});
	    				st.executeUpdate(b.toString());
	                }
				}else{
					if(auditStep.equals(maxAuditStep.getAuditStep())){
	                	b = new SqlBuilder("JdgQcmZcPassFlow.setAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, 
	    						auditResult, user, memo, "1", userId });
	    				st.executeUpdate(b.toString());
	                }else{
	                	b = new SqlBuilder("JdgQcmZcPassFlow.setAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, 
	    						auditResult, user, memo, "0", userId });
	    				st.executeUpdate(b.toString());
	    				b = new SqlBuilder("JdgQcmZcPassFlow.setCrtAuditStep");
	    				String nextAuditStep = String.valueOf(Integer.parseInt(auditStep) + 1);
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), nextAuditStep, "1"});
	    				st.executeUpdate(b.toString());
	                }
				}

				//如果是最后一步审核步骤
//				b = new SqlBuilder("JdgQcmBcPassFlow.getCrtAuditStep");
//				b.Fill(new String[]{ passApply.getPassApplyNo() });
//                QcmBcPassConfigEntity crtAuditStep = EntityTool.getEntityBySqlNoClsCon(
//                		b.toString(), con, QcmBcPassConfigEntity.class);
                
                if(auditStep.equals(maxAuditStep.getAuditStep())){
                	b = new SqlBuilder("JdgQcmZcPassApplyDAL.updateAudite");
    				b.Fill(new String[]{ passApplyNo, user, auditResult, memo });
    				st.executeUpdate(b.toString());
    				
					b = new SqlBuilder("QcmZgJugdeApplyDAL.setAuditResultZc");
					b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJudgeStoveNo(),
							passApply.getBatchNo(), passApply.getProcessCode(), auditResult, memo,
							passApply.getType() });
					st.executeUpdate(b.toString());
                }
				
				judgeStoveNos.add(passApply.getJudgeStoveNo());
			}
			
			con.commit();
			return cro;
		}catch(Exception ex){
			try {
				con.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		finally{
			if(st != null)
			{
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(con != null)
			{
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 删除申请放行记录
	 * @param passApplyNos
	 * @param user
	 * @return
	 */
	public CoreReturnObject delete(ArrayList<String> passApplyNos, String user){
		Connection con = null;
		Statement st = null;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			for(String passApplyNo : passApplyNos){
				SqlBuilder b = new SqlBuilder("JdgQcmZcPassApplyDAL.updateValidflag");
				b.Fill(new String[]{ passApplyNo, "0", user });
				st.executeUpdate(b.toString());
				
				//修改送判表紧急放行标志
				b = new SqlBuilder("JdgQcmZcPassApplyDAL.queryByPassApplyNo");
				b.Fill(new String[]{ passApplyNo });
				QcmBcPassApplyEntity pass = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmBcPassApplyEntity.class);
				b = new SqlBuilder("QcmZgJugdeApplyDAL.setTolInstancyFlag");
                b.Fill(new String[]{ pass.getJugdeApplyCode(), pass.getJudgeStoveNo(),
                		pass.getBatchNo(), pass.getProcessCode(), "0" });
                st.executeUpdate(b.toString());
			}
			
			con.commit();
			return cro;
		}catch(Exception ex){
			try {
				con.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}finally{
			if(st != null){
				try {
					st.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
