package com.steering.pss.judge.Bll;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

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.judge.model.QcmZcCheckconsignDetailEntity;
import com.steering.pss.judge.model.QcmZgJugdeApplyEntity;
import com.steering.pss.plnsaleord.stove.PlanFurnProduction;
import com.steering.pss.ydm.pipemanage.FrmBcIqInList;

public class BllRolledTubeColligatePassApply  extends DBCommon {
	
    /**
     * 试样组质量放行申请
     * @param parms
     * @param processCode
     * @param user
     * @param unit
     * @param memo
     * @param passType
     * @return
     */
    public CoreReturnObject passApply(ArrayList<String[]> parms, String processCode,
    		String user, String unit, String unitDesc, String memo){
    	Connection con = null;
    	Statement st = null;
    	CoreReturnObject cro = new CoreReturnObject();
    	try{
        	SqlBuilder b;
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        	con.setAutoCommit(false);
        	st = con.createStatement();
    		ResultSet rs = st.executeQuery(CoreXmlSqlParsersFactory.getSql(
    				"JdgQcmBcPassApplyDAL.getPhyMaxPk"));
    		String passApplyNo = "";
    		if(rs.next()){
    			passApplyNo = rs.getString(1);
    			rs.close();
    		}
        	for(String[]  parm : parms){
        		String checkNo = parm[0];
        		String sampleNo = parm[1];
        		String phyCodeMin = parm[2];
        		String batchNo = parm[3];
        		
        		b = new SqlBuilder("JdgQcmBcPassApplyDAL.QueryDetailIsPass");
        		b.Fill(new String[]{ checkNo, sampleNo,phyCodeMin, batchNo });
        		
        		QcmZcCheckconsignDetailEntity tmpConsignDetails = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmZcCheckconsignDetailEntity.class);
        		if(!tmpConsignDetails.getNkResult().equals("0") && !tmpConsignDetails.getJfResult().equals("0")){
        			cro.setV_errMsg("只有判定不合格的记录，才能申请质量放行！");
        			return cro;
        		}	
        		b = new SqlBuilder("JdgQcmBcPassApplyDAL.GetPhyApplyCnt");
        		b.Fill(new String[]{ checkNo, sampleNo,phyCodeMin, batchNo });
        		ResultSet rsBcApplyCnt = st.executeQuery(b.toString());
        		int gpApplyCnt = 0;
        		if(rsBcApplyCnt.next())
        		{
        			gpApplyCnt = rsBcApplyCnt.getInt(1);
        		}
        		if(gpApplyCnt > 0) //如果有放行申请单，则不重复生成。
        		{
        			continue;
        		}
                

        		String sql = CoreXmlSqlParsersFactory.getSql("JdgQcmBcPassApplyDAL.InsertPhy");
        		PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, passApplyNo);
                ps.setObject(2, checkNo);
                ps.setObject(3, sampleNo);
                ps.setObject(4, phyCodeMin);

                ps.setObject(5, batchNo);
                ps.setObject(6, user);
                ps.setObject(7, unit);
                ps.setObject(8, unitDesc);
                ps.setObject(9, memo);
                ps.setObject(10, processCode);
                ps.executeUpdate();
                ps.close();
        		
        	}
        	b = new SqlBuilder("JdgQcmBcPassConfig.getJhyConfigInfo");
			b.Fill(new String[]{ processCode  });
			ArrayList<QcmBcPassConfigEntity> passConfigs = EntityTool.getEntitysBySqlNoClsCon(
					b.toString(), con, QcmBcPassConfigEntity.class);
			if(passConfigs.size() == 0){
				con.rollback();
				cro.setV_errMsg("没有找到可用的评审流程，请配置评审流程！");
    			return cro;
			}
			int passFlowCnt = 0;
			for(QcmBcPassConfigEntity passConfig : passConfigs){
				b = new SqlBuilder("JdgQcmBcPassFlow.insertPhy");
				String ctrAuditStep = "0";
				if(passFlowCnt == 0){
					ctrAuditStep = "1";
				}
				b.Fill(new String[]{ passApplyNo, passConfig.getAuditStep(),
						passConfig.getAuditUnit(), ctrAuditStep });
				st.executeUpdate(b.toString());
				passFlowCnt++;
			}
        	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();
				}
    		}
    		try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
    	}
    }
	
	/**
	 * 审核
	 * @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;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			st = con.createStatement();
			SqlBuilder b;
			for(String[] parm : parms){
				String passApplyNo = parm[0];
				String auditStep = parm[1];
				b = new SqlBuilder("JdgQcmBcPassApplyDAL.queryPhyPassApplyNo");
				b.Fill(new String[]{ passApplyNo });
				QcmBcPassApplyEntity passApply = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmBcPassApplyEntity.class);
				b = new SqlBuilder("JdgQcmBcPassFlow.getPhyMaxAuditStep");
                b.Fill(new String[]{ passApply.getPassApplyNo() });
                QcmBcPassConfigEntity maxAuditStep = EntityTool.getEntityBySqlNoClsCon(
                		b.toString(), con, QcmBcPassConfigEntity.class);

				if(auditResult.equals("")){
					if(auditStep.equals(maxAuditStep.getAuditStep())){
	                	b = new SqlBuilder("JdgQcmBcPassFlow.clearAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, "1"});
	    				st.executeUpdate(b.toString());
	                }else{
	                	b = new SqlBuilder("JdgQcmBcPassFlow.clearAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, "0"});
	    				st.executeUpdate(b.toString());
	    				b = new SqlBuilder("JdgQcmBcPassFlow.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("JdgQcmBcPassFlow.setPhyAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, 
	    						auditResult, user, memo, "1", userId});
	    				st.executeUpdate(b.toString());
	                }else{
	                	b = new SqlBuilder("JdgQcmBcPassFlow.setPhyAuditResult");
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), auditStep, 
	    						auditResult, user, memo, "0", userId });
	    				st.executeUpdate(b.toString());
	    				b = new SqlBuilder("JdgQcmBcPassFlow.setPhyAuditStep");
	    				String nextAuditStep = String.valueOf(Integer.parseInt(auditStep) + 1);
	    				b.Fill(new String[]{ passApply.getPassApplyNo(), nextAuditStep, "1"});
	    				st.executeUpdate(b.toString());
	                }
				}
                
                if(auditStep.equals(maxAuditStep.getAuditStep())){
                	b = new SqlBuilder("JdgQcmBcPassApplyDAL.updatePhyAudite");
    				b.Fill(new String[]{ passApplyNo, user, auditResult, memo });
    				st.executeUpdate(b.toString());
    				
					b = new SqlBuilder("JdgQcmBcPassApplyDAL.updatePhyDetailByPassNo");
					b.Fill(new String[]{ passApplyNo});
					st.executeUpdate(b.toString());
                }
				
			}
			
			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();
				}
			}
		}
	}
	
}
