package com.steering.mes.signature;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
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.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.mes.zgmil.entity.MilEntrustedBillEntity;

public class FrmEntrustedCheck extends DBCommon{
	
	public CoreReturnObject doQuerySubContact(ArrayList<String> list,String startTim,String endTim,String plineCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmEntrustedCheck.doQuerySubcontract");
		if(!startTim.equals(""))
		{
		   sql += " AND T.ENTRUSTED_TIME >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND T.ENTRUSTED_TIME <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!plineCode.equals(""))
		{
		   sql += " AND T.PLINE_CODE = '"+plineCode+"'";
		}
		return this.getData(sql, list.toArray());
	}

	public CoreReturnObject doQueryPline()
	{
		return this.getDataBySqlID("FrmEntrustedCheck.doQueryPline");
	}
	/***
	 * 审核通过
	 * @param parm
	 * @return
	 */
	public CoreReturnObject checkPass(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        String imProcess = "";//工艺号
        String enTrustFlag = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Statement st = connection.createStatement();
			String updateFlagSql = CoreXmlSqlParsersFactory.getSql("FrmEntrustedCheck.checkPass");//审核通过
			PreparedStatement updateFlagPsta = connection.prepareStatement(updateFlagSql);
			for(String str : parm)
			{
				MilEntrustedBillEntity milEntrustTity = EntityTool.getEntityByJSONStr(str, MilEntrustedBillEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmEntrustedCheck.selectEntrustFlag");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milEntrustTity.getSubcontractNoSeq());
				psta.setString(2, milEntrustTity.getJudgeStoveNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					enTrustFlag = rs.getString("ENTRUSTED_FLAG");
				}
				rs.close();
				psta.close();
				if(!enTrustFlag.equals("0"))
				{
					cro.setV_errMsg("已审核数据,不允许重复审核!");
					return cro;
				}
				updateFlagPsta.setString(1, milEntrustTity.getEntrustedPlineCode());
				updateFlagPsta.setString(2, milEntrustTity.getEntrustedPlineName());
				updateFlagPsta.setString(3, milEntrustTity.getEntrustedRemark());
				updateFlagPsta.setString(4, milEntrustTity.getSubcontractNoSeq());
				updateFlagPsta.setString(5, milEntrustTity.getJudgeStoveNo());
				updateFlagPsta.addBatch();
			}
			updateFlagPsta.executeBatch();
			cro.setV_errMsg("审核成功!");
			connection.commit();
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	
	/***
	 * 审核不通过
	 * @param parm
	 * @return
	 */
	public CoreReturnObject noCheckPass(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        String enTrustFlag = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Statement st = connection.createStatement();
			String updateFlagSql = CoreXmlSqlParsersFactory.getSql("FrmEntrustedCheck.noCheckPass");//审核通过
			PreparedStatement updateFlagPsta = connection.prepareStatement(updateFlagSql);
			for(String str : parm)
			{
				MilEntrustedBillEntity milEntrustTity = EntityTool.getEntityByJSONStr(str, MilEntrustedBillEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmEntrustedCheck.selectEntrustFlag");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milEntrustTity.getSubcontractNoSeq());
				psta.setString(2, milEntrustTity.getJudgeStoveNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					enTrustFlag = rs.getString("ENTRUSTED_FLAG");
				}
				rs.close();
				psta.close();
				if(!enTrustFlag.equals("0"))
				{
					cro.setV_errMsg("已审核数据,不允许重复审核!");
					return cro;
				}
				updateFlagPsta.setString(1, milEntrustTity.getEntrustedRemark());
				updateFlagPsta.setString(2, milEntrustTity.getSubcontractNoSeq());
				updateFlagPsta.setString(3, milEntrustTity.getJudgeStoveNo());
				updateFlagPsta.addBatch();
			}
			updateFlagPsta.executeBatch();
			cro.setV_errMsg("审核成功!");
			connection.commit();
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	
	/***
	 * 撤销审核
	 * @param parm
	 * @return
	 */
	public CoreReturnObject cancelCheckPass(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        String enTrustFlag = "";
        String flag = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			Statement st = connection.createStatement();
			String updateFlagSql = CoreXmlSqlParsersFactory.getSql("FrmEntrustedCheck.cancelCheckPass");//审核通过
			PreparedStatement updateFlagPsta = connection.prepareStatement(updateFlagSql);
			for(String str : parm)
			{
				MilEntrustedBillEntity milEntrustTity = EntityTool.getEntityByJSONStr(str, MilEntrustedBillEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmEntrustedCheck.selectEntrustFlag");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milEntrustTity.getSubcontractNoSeq());
				psta.setString(2, milEntrustTity.getJudgeStoveNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					enTrustFlag = rs.getString("ENTRUSTED_FLAG");
				}
				rs.close();
				psta.close();
				if(enTrustFlag.equals("0"))
				{
					cro.setV_errMsg("未审核数据,不允许做撤销审核操作!");
					return cro;
				}
				sql = CoreXmlSqlParsersFactory.getSql("FrmDownlineManage.SelectSubcontractComplete");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milEntrustTity.getSubcontractNoSeq());
				psta.setString(2, milEntrustTity.getJudgeStoveNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					flag = rs.getString("SUBCONTRACT_FLAG");
				}
				rs.close();
				psta.close();
				if(flag.equals("1"))
				{
					cro.setV_errMsg("委外实绩已完成,不允许撤销审核!");
					return cro;
				}
				updateFlagPsta.setString(1, milEntrustTity.getSubcontractNoSeq());
				updateFlagPsta.setString(2, milEntrustTity.getJudgeStoveNo());
				updateFlagPsta.addBatch();
			}
			updateFlagPsta.executeBatch();
			cro.setV_errMsg("撤销成功!");
			connection.commit();
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
}
