package com.steering.ydm.bc;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.YdmBcOutlistEntity;
import com.steering.mes.mcp.entity.YdmGpOutlistEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

/**
 * 接箍出厂确认
 */
public class FrmGateFrim1 extends DBCommon {

    /**
     * 读卡查询接箍出库信息
     * @param outdoc
     * @return
     */
    public CoreReturnObject doQueryOutList(String outdoc){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim1.doQueryOutList");
        sql += " AND OUTSTOCK_DOC = '"+outdoc+"'";
        return this.getData(sql);
    }

    /**
     * 查询接箍出库未确认出厂信息
     * @param list
     * @return
     */
    public CoreReturnObject QueryBefore(ArrayList list){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim1.doQueryOutList");

        if ((list.get(0) != null && !list.get(0).equals(""))&&(list.get(1) != null && !list.get(1).equals(""))){
            sql += " AND T.CREATE_TIME BETWEEN to_date('"+list.get(0)+"','YYYY-MM-DD HH24:mi:ss')" +
                    " AND to_date('"+list.get(1)+"'，'YYYY-MM-DD HH24:mi:ss')";
        }

        if(list.get(2) != null && !list.get(2).equals("")){
            sql += " AND T.JUDGE_STOVE_NO = '"+list.get(2)+"'";
        }

        if(list.get(3) != null && !list.get(3).equals("")){
            sql += " AND T.DEAL_ORDE = '"+list.get(3)+"'";
        }

        sql += " AND T.DEAL_TYPE_CODE ='800803'";
        sql += " AND nvl(T.LEAVE_FACTORY_FLAG,0) = 0";
        sql += " AND T.TAR_REC_FLAG='0' ORDER BY T.CREATE_TIME desc";
        return this.getData(sql);
    }


    /**
     * 查询接箍出库已确认出厂信息
     * @param list
     * @return
     */
    public CoreReturnObject QueryDone(ArrayList list){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim1.doQueryOutList");

        if ((list.get(0) != null && !list.get(0).equals(""))&&(list.get(1) != null && !list.get(1).equals(""))){
            sql += " AND T.CREATE_TIME BETWEEN to_date('"+list.get(0)+"','YYYY-MM-DD HH24:mi:ss')" +
                    " AND to_date('"+list.get(1)+"'，'YYYY-MM-DD HH24:mi:ss')";
        }

        if(list.get(2) != null && !list.get(2).equals("")){
            sql += " AND T.JUDGE_STOVE_NO = '"+list.get(2)+"'";
        }

        if(list.get(3) != null && !list.get(3).equals("")){
            sql += " AND T.DEAL_ORDE = '"+list.get(3)+"'";
        }

        sql += " AND T.DEAL_TYPE_CODE ='800803'";
        sql += " AND nvl(T.LEAVE_FACTORY_FLAG,0) = 1";
        sql += " AND T.TAR_REC_FLAG='0' ORDER BY T.CREATE_TIME desc";
        return this.getData(sql);
    }

    /**
     * 确认出厂
     * @param dealOrde 接箍交易单号
     * @param username 操作人
     * @return
     */
    public CoreReturnObject Confirm(String dealOrde , String username){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim1.Confirm");
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1,username);
            ps.setObject(2,dealOrde);
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("确认出厂成功!");
        }
        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 dealOrde 接箍交易单号
     * @return
     */
    public CoreReturnObject CancelConfirm(String dealOrde){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory.getSql("FrmGateFrim1.CancelConfirm");
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1,dealOrde);
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("撤销确认出厂成功!");
        }
        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;
    }


}
