package com.steering.pss.judge.Bll;

import java.sql.Connection;
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.QcmJudgeLockEntity;

public class BllQcmJudgeLock extends DBCommon{
	
	public CoreReturnObject query(String judgeStoveNo, String orderMngNo){
		Connection con = null;
		CoreReturnObject cro = new CoreReturnObject();
		try{
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			String orderNo = "";
			String orderSeq = "";
			String[] orderMngNos = orderMngNo.split("/");
			if(orderMngNos.length >= 2){
				orderNo = orderMngNos[0];
				orderSeq = orderMngNos[1];
			}else if(orderMngNos.length >= 1){
				orderNo = orderMngNos[0];
			}
			SqlBuilder b = null;
//			ArrayList<QcmZgJugdeApplyEntity> judgeStoveNos = null;
//			if(!judgeStoveNo.equals("")){
//				b = new SqlBuilder("JdgQcmJudgeLock.QueryJudgeStoveNo");
//				b.Fill(new String[]{ judgeStoveNo });
//				judgeStoveNos = EntityTool.getEntitysBySqlNoClsCon(
//						b.toString(), con, QcmZgJugdeApplyEntity.class);
//			}else{
//				b = new SqlBuilder("JdgQcmJudgeLock.QueryJudgeStoveNoByOrderNo");
//				b.Fill(new String[]{ orderNo, orderSeq });
//				judgeStoveNos = EntityTool.getEntitysBySqlNoClsCon(
//						b.toString(), con, QcmZgJugdeApplyEntity.class);
//			}
			
			//if(judgeStoveNos.get)
			
			ArrayList<QcmJudgeLockEntity> locks = new ArrayList<QcmJudgeLockEntity>();
				//String judgeStoveNo = applyJudgeStoveNo.getJudgeStoveNo();
				//炼钢
				b = new SqlBuilder("JdgQcmJudgeLock.QueryLg");
				b.Fill(new String[]{ judgeStoveNo, orderNo, orderSeq });
				ArrayList<QcmJudgeLockEntity> tmplocks = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJudgeLockEntity.class);
				for(QcmJudgeLockEntity tmpLock : tmplocks){
					locks.add(tmpLock);
				}
				
				//轧管
				b = new SqlBuilder("JdgQcmJudgeLock.QueryZg");
				b.Fill(new String[]{ judgeStoveNo, orderNo, orderSeq });
				tmplocks = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJudgeLockEntity.class);
				
				for(QcmJudgeLockEntity tmpLock : tmplocks){
					locks.add(tmpLock);
				}
				
				//镦拔扩
				b = new SqlBuilder("JdgQcmJudgeLock.QueryDbk");
				b.Fill(new String[]{ judgeStoveNo, orderNo, orderSeq });
				tmplocks = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJudgeLockEntity.class);
				
				for(QcmJudgeLockEntity tmpLock : tmplocks){
					locks.add(tmpLock);
				}
				
				//热处理
				b = new SqlBuilder("JdgQcmJudgeLock.QueryRcl");
				b.Fill(new String[]{ judgeStoveNo, orderNo, orderSeq });
				tmplocks = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJudgeLockEntity.class);
				
				for(QcmJudgeLockEntity tmpLock : tmplocks){
					locks.add(tmpLock);
				}
				
				//加工线
				b = new SqlBuilder("JdgQcmJudgeLock.QueryJgx");
				b.Fill(new String[]{ judgeStoveNo, orderNo, orderSeq });
				tmplocks = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJudgeLockEntity.class);
				
				for(QcmJudgeLockEntity tmpLock : tmplocks){
					locks.add(tmpLock);
				}
				
				//接箍
				b = new SqlBuilder("JdgQcmJudgeLock.QueryJg");
				b.Fill(new String[]{ judgeStoveNo, orderNo, orderSeq });
				tmplocks = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJudgeLockEntity.class);
				
				for(QcmJudgeLockEntity tmpLock : tmplocks){
					locks.add(tmpLock);
				}
				
				//成品
				b = new SqlBuilder("JdgQcmJudgeLock.QueryZc");
				b.Fill(new String[]{ judgeStoveNo, orderNo, orderSeq });
				tmplocks = EntityTool.getEntitysBySqlNoClsCon(
						b.toString(), con, QcmJudgeLockEntity.class);
				
				for(QcmJudgeLockEntity tmpLock : tmplocks){
					locks.add(tmpLock);
				}
			
			cro.setResult(EntityTool.getGsonByEntity(locks));
			 
			return cro;
		}catch(Exception ex){
			cro.setV_errMsg("程序异常: " + ex.getMessage());
			return cro;
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 锁定
	 * @param jsons
	 * @return
	 */
	public CoreReturnObject lock(ArrayList<String> jsons, String reason, String userId){
		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 json : jsons){
				QcmJudgeLockEntity lock = EntityTool.getEntityByGson(json, QcmJudgeLockEntity.class);
				lock.setLockReason(reason);
				lock.setLockUserId(userId);
				if(lock.getLockId().equals("")){
					String insert = CoreXmlSqlParsersFactory.getSql("JdgQcmJudgeLock.insert");
					insert = EntityTool.fillSqlByEntity(insert, lock);
					st.executeUpdate(insert);
				}else{
					String sqlLock = CoreXmlSqlParsersFactory.getSql("JdgQcmJudgeLock.Lock");
					sqlLock = EntityTool.fillSqlByEntity(sqlLock, lock);
					st.executeUpdate(sqlLock);
				}
			}
			
			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{
			try {
				st.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 解锁
	 * @param jsons
	 * @return
	 */
	public CoreReturnObject unlock(ArrayList<String> jsons, String userId){
		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 json : jsons){
				QcmJudgeLockEntity lock = EntityTool.getEntityByGson(json, QcmJudgeLockEntity.class);
				lock.setUnlockUserId(userId);
				String sqlUnlock = CoreXmlSqlParsersFactory.getSql("JdgQcmJudgeLock.unlock");
				sqlUnlock = EntityTool.fillSqlByEntity(sqlUnlock, lock);
				st.executeUpdate(sqlUnlock);
			}
			
			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{
			try {
				st.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
