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.QcmGpJudgeApplyEntity;
import com.steering.pss.judge.model.QcmGpPassApplyEntity;
import com.steering.pss.plnsaleord.stove.ComPlanFurnace;
import com.steering.pss.ydm.entity.YdmGpInlistEntity;
import com.steering.pss.ydm.pipemanage.FrmGpIqInList;

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

public class BllSteelMakingPassApply extends DBCommon{
	
	/**
	 * 删除申请放行记录
	 * @param passApplyNos
	 * @param user
	 * @return
	 */
	public CoreReturnObject delete(ArrayList<String> passApplyNos, String user){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String passApplyNo : passApplyNos){
			SqlBuilder b = new SqlBuilder("QcmGpPassApplyDAL.updateValidflag");
			b.Fill(new String[]{ passApplyNo, "0", user });
			sqllist.add(b.toString());
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 审核
	 * @param passApplyPks
	 * @param user
	 * @param auditResult
	 * @param memo
	 * @return
	 */
	public CoreReturnObject Audite(ArrayList<String> passApplyNos, 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;
			ArrayList<String> judgeStoveNos = new ArrayList<String>();
			Date now = new Date();
			for(String passApplyNo : passApplyNos){
				b = new SqlBuilder("QcmGpPassApplyDAL.queryByPassApplyNo");
				b.Fill(new String[]{ passApplyNo });
				QcmGpPassApplyEntity passApply = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmGpPassApplyEntity.class);
				if(passApply==null)
					continue;
				//验证申请判定单号是否还存在
				b = new SqlBuilder("QcmGpJugdeApplyDAL.queryGpApplyByJudgeStoveNo");
				b.Fill(new String[]{ passApply.getJugdeApplyCode(),passApply.getJugdeApplySqe(),passApply.getJudgeStoveNo() });
	            QcmGpJudgeApplyEntity gpJudgeApply = EntityTool.getEntityBySqlNoClsCon(
	            		b.toString(), con, QcmGpJudgeApplyEntity.class);
	            if(gpJudgeApply==null)
	            {
	            	cro.setV_errMsg("未找到对应送判记录，无法审核！");
	            	return cro;
	            }
	            
				if(passApply.getType().equals("1")){
					if(gpJudgeApply.getJudgeResultCode()!="")
		            {
		            	cro.setV_errMsg("已判定，无法审核！");
		            	return cro;
		            }
					
					//新增炉计划
					//if(auditResult.equals("1") && !judgeStoveNos.contains(passApply.getJudgeStoveNo())){
					if(auditResult.equals("1")){
						//*********************更新材料表
						b = new SqlBuilder("QcmGpJugdeApplyDAL.queryGpApplyByJudgeStoveNo");
		        		b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
		        				passApply.getJudgeStoveNo() });
		        		List<QcmGpJudgeApplyEntity> gpJudgeApplys = EntityTool.getEntitysBySqlNoClsCon(
		        				b.toString(), con, QcmGpJudgeApplyEntity.class);
		                b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpMInstancy");
		                b.Fill(new String[]{ gpJudgeApplys.get(0).getJugdeApplyCode() + gpJudgeApplys.get(0).getJugdeApplySqe(),
		                		passApply.getType() });
		                st.executeUpdate(b.toString());
		                b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpMBelong");
		                if(gpJudgeApplys.get(0).getSupplyUnitCode().equals("120504")){
		                	b.Fill(new String[]{ gpJudgeApplys.get(0).getJugdeApplyCode() + gpJudgeApplys.get(0).getJugdeApplySqe(),
			                		"002023005", "产品制造部" });
		                }else{
		                	b.Fill(new String[]{ gpJudgeApplys.get(0).getJugdeApplyCode() + gpJudgeApplys.get(0).getJugdeApplySqe(),
			                		"002001001", "生产计划部" });
		                }
		                
		                st.executeUpdate(b.toString());
		                
		                //更新材料历史表
		                b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpMHistoryInstancy");
		                b.Fill(new String[]{ gpJudgeApplys.get(0).getJugdeApplyCode() + gpJudgeApplys.get(0).getJugdeApplySqe(),
		                		 passApply.getType() });
		                st.executeUpdate(b.toString());
		                b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpMHistoryBelong");
		                if(gpJudgeApplys.get(0).getSupplyUnitCode().equals("120504")){
		                	b.Fill(new String[]{ gpJudgeApplys.get(0).getJugdeApplyCode() + gpJudgeApplys.get(0).getJugdeApplySqe(),
			                		 "002023005", "产品制造部" });
		                }else{
		                	b.Fill(new String[]{ gpJudgeApplys.get(0).getJugdeApplyCode() + gpJudgeApplys.get(0).getJugdeApplySqe(),
			                		 "002001001", "生产计划部" });
		                }
		                
		                st.executeUpdate(b.toString());
						
						ArrayList<String> parm = new ArrayList<String>();
		            	parm.add(passApply.getJugdeApplyCode());
		            	parm.add(passApply.getJugdeApplySqe());
		            	parm.add(passApply.getJudgeStoveNo());
		            	ArrayList<ArrayList<String>> parms = new ArrayList<ArrayList<String>>();
		            	parms.add(parm);
		            	ComPlanFurnace.addPlnZyC(parms, user, con);
		            	
		            	//更新管坯申请表的紧急状态
		            	b = new SqlBuilder("QcmGpJugdeApplyDAL.setInstancy");
		            	b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
		            			passApply.getJudgeStoveNo(), "1"});
		            	st.executeUpdate(b.toString());
		            	//修改申请判定表的结转年月
		                b = new SqlBuilder("QcmGpJugdeApplyDAL.setBalYearMonth");
		                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(), passApply.getJudgeStoveNo(),
								"C072,C091,C092,C098,C099,C100,C102,C103,C104".contains(passApply.getPlineCode())?ComBaseInfo.GetBalYear_2416(now):ComBaseInfo.GetBalYear(now)});
		                st.executeUpdate(b.toString());
		            	
		            	b = new SqlBuilder("QcmGpPassApplyDAL.updateAudite2");
		            	if(gpJudgeApplys.get(0).getSupplyUnitCode().equals("120504")){
		                	b.Fill(new String[]{ passApplyNo, "产品制造部", auditResult, memo });
		                }else{
		                	b.Fill(new String[]{ passApplyNo, "生产计划部", auditResult, memo });
		                }
						
						st.executeUpdate(b.toString());
						
						//新增入库表
		        		FrmGpIqInList gpIqInList = new FrmGpIqInList();
		        		for(QcmGpJudgeApplyEntity gpJudgeApplySub : gpJudgeApplys){
							b = new SqlBuilder("FrmGpIqInList.SelectGpInlist");
							b.Fill(new String[]{ gpJudgeApplySub.getJugdeApplyCode()+gpJudgeApplySub.getJugdeApplySqe()+gpJudgeApplySub.getJugdeApplySequeno(),
									gpJudgeApplySub.getJudgeStoveNo() });
							YdmGpInlistEntity ydmGpInlistEntity = EntityTool.getEntityBySqlNoClsCon(
									b.toString(), con, YdmGpInlistEntity.class);
							if(ydmGpInlistEntity!=null){
								cro.setV_errMsg("产权入库数据已存在，无法审核！");
								return cro;
							}
		        			String gpJudgeApplyJson = EntityTool.getGsonByEntity(gpJudgeApplySub);
		                    ArrayList<String> gpJudgeApplyJsons = new ArrayList<String>();
		                    gpJudgeApplyJsons.add(gpJudgeApplyJson);
		                    gpIqInList.insertGpInList(con, gpJudgeApplyJsons, user);
		        		}
					}
				}
				
				b = new SqlBuilder("QcmGpPassApplyDAL.updateAudite");
				b.Fill(new String[]{ passApplyNo, user, auditResult, memo });
				st.executeUpdate(b.toString());
				
				//修改管坯申请判定表中的审核结果。
				if(passApply.getType().equals("0")){
					b = new SqlBuilder("QcmGpJugdeApplyDAL.setAuditResult");
					b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
							passApply.getJudgeStoveNo(), auditResult, memo });
					st.executeUpdate(b.toString());
				}
				
				judgeStoveNos.add(passApply.getJudgeStoveNo());
			}
//			con.rollback();
			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();
				}
			}
		}
	}
	
	/**
	 * 审核2
	 * @param passApplyPks
	 * @param user
	 * @param auditResult
	 * @param memo
	 * @return
	 */
	public CoreReturnObject Audite2(ArrayList<String> passApplyNos, 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();
			ArrayList<String> judgeStoveNos = new ArrayList<String>();
			for(String passApplyNo : passApplyNos){
				SqlBuilder b = new SqlBuilder("QcmGpPassApplyDAL.updateAudite2");
				b.Fill(new String[]{ passApplyNo, user, auditResult, memo });
				st.executeUpdate(b.toString());
				b = new SqlBuilder("QcmGpPassApplyDAL.queryByPassApplyNo");
				b.Fill(new String[]{ passApplyNo });
				QcmGpPassApplyEntity passApply = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, QcmGpPassApplyEntity.class);
				
				//修改管坯申请判定表中的审核结果。
				if(passApply.getType().equals("0")){
					b = new SqlBuilder("QcmGpJugdeApplyDAL.setAuditResult2");
					b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
							passApply.getJudgeStoveNo(), auditResult, memo });
					st.executeUpdate(b.toString());
				}
				
				//回写管坯申请判定表
//        		b = new SqlBuilder("QcmLgJudgeDAL.queryByJudgeStoveNo");
//        		b.Fill(new String[]{ passApply.getJudgeStoveNo(), "B" });
//        		QcmLgJudgeEntity lgJudgeEntity = EntityTool.getEntityBySqlNoClsCon(
//        				b.toString(), con, QcmLgJudgeEntity.class);
//                b = new SqlBuilder("QcmGpJugdeApplyDAL.setJudgeResultAudit2");
//                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
//                		passApply.getJudgeStoveNo(), "40740706", lgJudgeEntity.getCreateName(),
//                		passApply.getFaceResultCode(), passApply.getPhyResultCode(), 
//                		passApply.getDetectResultCode(), passApply.getChemResultCode(), "20" });
//                st.executeUpdate(b.toString());
				
				//新增炉计划
//				if(auditResult.equals("1") && !judgeStoveNos.contains(passApply.getJudgeStoveNo())){
//	            	ArrayList<String> parm = new ArrayList<String>();
//	            	parm.add(passApply.getJugdeApplyCode());
//	            	parm.add(passApply.getJugdeApplySqe());
//	            	parm.add(passApply.getJudgeStoveNo());
//	            	ArrayList<ArrayList<String>> parms = new ArrayList<ArrayList<String>>();
//	            	parms.add(parm);
//	            	ComPlanFurnace.addPlnZyC(parms, user, con);
//	            	
//	            	//新增入库表
//					b = new SqlBuilder("QcmGpJugdeApplyDAL.queryGpApplyByJudgeStoveNo");
//	        		b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
//	        				passApply.getJudgeStoveNo() });
//	        		List<QcmGpJudgeApplyEntity> gpJudgeApplys = EntityTool.getEntitysBySqlNoClsCon(
//	        				b.toString(), con, QcmGpJudgeApplyEntity.class);
//	        		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("QcmLgJudgeDAL.queryByJudgeStoveNo");
//	        		b.Fill(new String[]{ passApply.getJudgeStoveNo(), "B" });
//	        		QcmLgJudgeEntity lgJudgeEntity = EntityTool.getEntityBySqlNoClsCon(
//	        				b.toString(), con, QcmLgJudgeEntity.class);
//	                b = new SqlBuilder("QcmGpJugdeApplyDAL.setJudgeResultAudit2");
//	                b.Fill(new String[]{ passApply.getJugdeApplyCode(), passApply.getJugdeApplySqe(),
//	                		passApply.getJudgeStoveNo(), "40740706", lgJudgeEntity.getCreateName(),
//	                		passApply.getFaceResultCode(), passApply.getPhyResultCode(), 
//	                		passApply.getDetectResultCode(), passApply.getChemResultCode(), "20" });
//	                st.executeUpdate(b.toString());
//	                
//	               //回写炼钢、锻造生产物料主表  更新条件应该是 末工序就是成品否者就是在制品。
//	               //查询成分判定记录
//	                QcmLgChemjudgeEntity lgChemJudge = EntityTool.getEntityBySqlNoClsCon(
//	                		new SqlBuilder("QcmLgJudgeDAL.getChemJudge").Fill(
//	                				new String[]{ passApply.getJudgeStoveNo(), "B" }).toString(),
//	                		con, QcmLgChemjudgeEntity.class);
//	                if(lgChemJudge == null){
//	                	cro.setV_errMsg("没有找到成分判定记录，请先进行成分判定！");
//	                	return cro;
//	                }
//	                
//	                //查询理化
//	                b = new SqlBuilder("QcmZgPhyjudgeDAL.queryByJudgeStoveNo");
//	                b.Fill(new String[]{ passApply.getJudgeStoveNo(), "B" });
//	                QcmZgPhyjudgeEntity zgPhyJudge = EntityTool.getEntityBySqlNoClsCon(
//	                		b.toString(), con, QcmZgPhyjudgeEntity.class); 
//	                String phyJudgeId = "";
//	                String phyResultId = "";
//	                if(zgPhyJudge != null){
//	                	phyJudgeId = zgPhyJudge.getPhyJudgeId();
//	                	phyResultId = zgPhyJudge.getPhyResultId();
//	                }
//	                
//	                String matStatus = "";
//	                if(gpJudgeApplys.size() > 0){
//	                	if(gpJudgeApplys.get(0).getIsendgx().equals("1")){
//	                    	matStatus = "80150202";
//	                    }else{
//	                    	matStatus = "80150102";
//	                    }
//	                	
//	                	//更新材料表
//	                	b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpM");
//	                    b.Fill(new String[]{ passApply.getJugdeApplyCode() + passApply.getJugdeApplySqe(),
//	                    		"40740706", user, "1", "1", matStatus,
//	                    		gpJudgeApplys.get(0).getFaceResultCode(),
//	                    		user, passApply.getChemResultCode(), lgChemJudge.getCreateTime(), lgChemJudge.getCreateName(),
//	                    		passApply.getPhyResultCode(), user, lgChemJudge.getChemJudgeId(), phyJudgeId, phyResultId });
//	                    st.executeUpdate(b.toString());
//	                    
//	                    //更新材料历史表
//	                    b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpMHistory");
//	                    b.Fill(new String[]{ passApply.getJugdeApplyCode() + passApply.getJugdeApplySqe(),
//	                    		"40740706", user, "1", "1", matStatus, 
//	                    		gpJudgeApplys.get(0).getFaceResultCode(),
//	                    		user, passApply.getChemResultCode(), lgChemJudge.getCreateTime(), lgChemJudge.getCreateName(),
//	                    		passApply.getPhyResultCode(), user, lgChemJudge.getChemJudgeId(), phyJudgeId, phyResultId });
//	                    
//	                    b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpMBelong");
//	                    b.Fill(new String[]{ passApply.getJugdeApplyCode() + passApply.getJugdeApplySqe(), "002001001", "生产计划部" });
//	                    st.executeUpdate(b.toString());
//	                    b = new SqlBuilder("QcmLgJudgeDAL.updateMatGpMHistoryBelong");
//	                    b.Fill(new String[]{ passApply.getJugdeApplyCode() + passApply.getJugdeApplySqe(), "002001001", "生产计划部" });
//	                    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();
				}
			}
		}
	}
}
