package com.steering.pss.ydm.pipemanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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

import com.ibm.ws.webservices.xml.waswebservices.value;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.stove.PlanFurnProduction;
import com.steering.pss.ydm.entity.QcmZgJugdeApplyEntity;


public class FrmHandleJuge extends DBCommon{

	
	/***
	 * 查询申判数据
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @param valueFlag
	 * @return
	 */
	public CoreReturnObject doQueryApplyJudge(ArrayList<String> list,String startTim,String endTim,String valueFlag)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmHandleJuge.doQueryApplyJudge");
		if(!list.get(0).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+list.get(0)+"'";
		}
		if(!startTim.equals(""))
		{
			sql += " AND T.JUDGE_RESULT_TIME >= TO_DATE('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND T.JUDGE_RESULT_TIME <= TO_DATE('"+endTim+"','YYYY-MM-DD HH24:MI:SS') ";
		}
		if(!valueFlag.equals("")&&!valueFlag.equals("2"))
		{
			sql += " AND T.INSTOCK_FLAG = '"+valueFlag+"'";
		}
		sql += " GROUP BY T.JUGDE_APPLY_CODE,T.JUDGE_STOVE_NO,T.PROCESS_CODE,T.PLINE_CODE,T.LOT_NO,T.PLINE_NAME,T.PRO_ORDER_NO";
		return this.getData(sql);
	}
	
	
	/***
	 * 查询详细炉批信息
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @param valueFlag
	 * @return
	 */
	public CoreReturnObject doQueryApplyData(ArrayList<String> list)
	{
		return this.getDataBySqlID("FrmHandleJuge.doQueryApplyData", list.toArray());
	}
	
	/***
	 * 进行手工判定
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doHandleApply(ArrayList<String> parm,String userName)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		int acount = 0;
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parm)
			{
				QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool.getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
				qcmZgJugdeApplyTity.setJudgeResultCode("40740701");
				qcmZgJugdeApplyTity.setJudgeResult("合同内合格");
				qcmZgJugdeApplyTity.setBalYearMonth(yearMoth);
				qcmZgJugdeApplyTity.setJudgeMaker(userName);
				qcmZgJugdeApplyTity.setValidflag("00");
				sql = CoreXmlSqlParsersFactory.getSql("doHandleApply.SelectApplyData");//查询否已判或者申判信息是否还在
				sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(acount > 0)
				{
					cro.setV_errMsg("该炉信息已不是申判初始状态/或者已撤销申请判定，不能进行手工判定!");
					return cro;
				}
				String updateApplyResultSql = CoreXmlSqlParsersFactory.getSql("FrmHandleJuge.doHandleApply");//手工判定
				updateApplyResultSql = EntityTool.fillSqlByEntity(updateApplyResultSql, qcmZgJugdeApplyTity);
				sqllist.add(updateApplyResultSql);
				
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("判定成功!");
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	/***
	 * 撤销手工判定
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doCancelHandleApply(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String valueFlag = "";
		String instorageFlag = "";
		int acount = 0;
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parm)
			{
				QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool.getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
				
				qcmZgJugdeApplyTity.setValidflag("20");
				sql = CoreXmlSqlParsersFactory.getSql("doHandleApply.SelectApplyData");//查询否已判或者申判信息是否还在
				sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(acount > 0)
				{
					cro.setV_errMsg("该炉信息不是已判定状态，不能进行判定撤销!");
					return cro;
				}
				
				sql = CoreXmlSqlParsersFactory.getSql("doHandleApply.SelectInStorageFlag");//查询该炉信息是否已确认
				sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(acount >0)
				{
					cro.setV_errMsg("该炉信息已有确认，不能进行判定撤销!");
					return cro;
				}
				String updateCancelApplyResultSql = CoreXmlSqlParsersFactory.getSql("FrmHandleJuge.doCancelHandleApply");//撤销手工判定
				updateCancelApplyResultSql = EntityTool.fillSqlByEntity(updateCancelApplyResultSql, qcmZgJugdeApplyTity);
				sqllist.add(updateCancelApplyResultSql);
				
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("撤销判定成功!");
			}
			
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	/***
	 * 对判定信息进行确认
	 * @param parm
	 * @return
	 * @throws ParseException 
	 */
	public CoreReturnObject doComfirm(ArrayList<String> parm,String userName) throws ParseException
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String valueFlag = "";
		String valueFlag1 = "";
		String inListDoc = "";
		int acount = 0;
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			inListDoc = getInListDoc(connection, "800709");
			for(String str : parm)
			{
				QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool.getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("doHandleApply.SelectBathYear");//查询否已判或者申判信息是否还在
				sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					valueFlag1 = rs.getString("VALIDFLAG");
					yearMoth = rs.getString("BAL_YEAR_MONTH");
				}
				rs.close();
				psta.close();
				if(!valueFlag1.equals("20"))
				{
					cro.setV_errMsg("该炉信息未进行判定， 不能进行缴库确认!");
					return cro;
				}
				
				sql = CoreXmlSqlParsersFactory.getSql("doHandleApply.SelectComfirmApplyData");//查询否已判或者申判信息是否还在
				sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					valueFlag = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(valueFlag)>0)
				{
					cro.setV_errMsg("该炉信息已进行了缴库确认，不能进行重复缴库确认!");
					return cro;
				}
				qcmZgJugdeApplyTity.setApplyName(userName);
				qcmZgJugdeApplyTity.setBalYearMonth(yearMoth);
				qcmZgJugdeApplyTity.setApplyTime(systime);
				qcmZgJugdeApplyTity.setInstockDoc(inListDoc);
				String updateComfirmApplyResultSql = CoreXmlSqlParsersFactory.getSql("FrmHandleJuge.doComfirm");//确认判定信息
				updateComfirmApplyResultSql = EntityTool.fillSqlByEntity(updateComfirmApplyResultSql, qcmZgJugdeApplyTity);
				sqllist.add(updateComfirmApplyResultSql);

			}
			if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("D"))
			{
				PlanFurnProduction.addPlnZyC(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());
			}else if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("E"))
			{
				PlanFurnProduction.addPlnZyCD(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());//镦拔扩判定
			}else if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("F"))
			{
				PlanFurnProduction.addPlnZyCF(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());
			}else if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("G"))
			{
				PlanFurnProduction.addPlnZyCG(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("确认成功!");
			}	
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	/***
	 * 获取出库单号
	 * 
	 * @param conn
	 * @param strInbound
	 * @return
	 * @throws SQLException
	 */
	public String getInListDoc(Connection conn, String strInbound)
			throws SQLException {
		String inListDoc = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmHandleJuge.getInListDoc");
		PreparedStatement psta = conn.prepareStatement(sql);
		psta.setString(1, strInbound);
		psta.setString(2, strInbound);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDoc = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return inListDoc;
	}
	
	/***
	 * 对判定信息取消确认
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doCancelComfirm(ArrayList<String> parm,String userName)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String valueFlag = "";
		int acount = 0;
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parm)
			{
				QcmZgJugdeApplyEntity qcmZgJugdeApplyTity = EntityTool.getEntityByJSONStr(str, QcmZgJugdeApplyEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("doHandleApply.SelectComfirmApplyData");//查询否已判或者申判信息是否还在
				sql = EntityTool.fillSqlByEntity(sql, qcmZgJugdeApplyTity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(acount == 0)
				{
					cro.setV_errMsg("该炉信息还未进行缴库确认，不能进行缴库确认撤销!");
					return cro;
				}
				
				String updateCancelComfirmApplyResultSql = CoreXmlSqlParsersFactory.getSql("FrmHandleJuge.doCancelComfirm");//取消判定信息的确认
				updateCancelComfirmApplyResultSql = EntityTool.fillSqlByEntity(updateCancelComfirmApplyResultSql, qcmZgJugdeApplyTity);
				sqllist.add(updateCancelComfirmApplyResultSql);
			}
			
			if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("D"))
			{
				PlanFurnProduction.deletePlnC(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());
			}else if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("E"))
			{
				PlanFurnProduction.deletePlnCD(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());//镦拔扩判定
			}else if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("F"))
			{
				PlanFurnProduction.deletePlnCF(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());
			}else if(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getProcessCode().equals("G"))
			{
				PlanFurnProduction.deletePlnCG(EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJugdeApplyCode(), EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getJudgeStoveNo(), userName, connection,EntityTool.getEntityByJSONStr(parm.get(0), QcmZgJugdeApplyEntity.class).getBatchNo());
			}
			
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("撤销成功!");
			}
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
