package com.steering.mes.mcp.CollarLibrary;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

/**
 * 接箍收料
 * @author TJG 2016/04/26
 * @version v1.0
 */
public class FrmCollarInBound extends DBCommon {

    /**
     * 查询交易表倒库实绩
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryOutPerfo(ArrayList<String> list, String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCollarInBound.queryOutPerfo");
        // sql += "  AND TAR_KEP_NO LIKE '%' || '" + list.get(1) + "' || '%' ";
        if (!list.get(0).equals("")) {
            sql += " AND TAR_STORAGE_NO ='" + list.get(0) + "'";
        }else {
            sql += CoreConverter.toSqlConditionString("TAR_STORAGE_NO", arr);
        }
        if (!list.get(2).equals("")) {
            sql += " AND judge_stove_no LIKE '%' || '" + list.get(2)
                    + "' || '%'";
        }
        sql += " AND DECODE('" + list.get(3)
                + "', NULL, '1', create_time) BETWEEN DECODE('" + list.get(4)
                + "', NULL, '1', to_date('" + list.get(5)
                + "', 'YYYY-MM-DD HH24:MI:SS'))";
        sql += " AND DECODE('" + list.get(6) + "', NULL, '1', to_date('"
                + list.get(7) + "', 'YYYY-MM-DD HH24:MI:SS')) ";
        sql += " AND DEAL_TYPE_CODE ='800803'";
        sql += " AND TAR_REC_FLAG='0'  ORDER BY CREATE_TIME desc";
        return this.getData(sql);
    }


    /**
     * 查询交易表倒库实绩
     * @param outdoc 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryOutPerfo(String outdoc) {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmCollarInBound.queryOutPerfo");
        sql += " AND OUTSTOCK_DOC = '"+outdoc+"'";
        sql += " AND DEAL_TYPE_CODE ='800803'";
        sql += " AND TAR_REC_FLAG='0'  ORDER BY CREATE_TIME desc";
        return this.getData(sql);
    }

    /**
     * 查询交易表入库实绩
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryInPerfo(ArrayList<String> list, String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCollarInBound.queryInPerfo");

//        sql += "  AND TAR_KEP_NO LIKE '%' || '" + list.get(1) + "' || '%' ";
        if (!list.get(0).equals("")) {
            sql += " AND TAR_STORAGE_NO ='" + list.get(0) + "'";
        }else {
            sql += CoreConverter.toSqlConditionString("TAR_STORAGE_NO", arr);
        }
        sql += " AND judge_stove_no LIKE '%' || '" + list.get(2) + "' || '%'";
        sql += " AND DECODE('" + list.get(3)
                + "', NULL, '1', create_time) BETWEEN DECODE('" + list.get(4)
                + "', NULL, '1', to_date('" + list.get(5)
                + "', 'YYYY-MM-DD HH24:MI:SS'))";
        sql += " AND DECODE('" + list.get(6) + "', NULL, '1', to_date('"
                + list.get(7) + "', 'YYYY-MM-DD HH24:MI:SS')) ";
        sql += " AND DEAL_TYPE_CODE ='800703'";
        sql += "  AND TAR_REC_FLAG='1'  ORDER BY CREATE_TIME desc";
        return this.getData(sql);
    }

    /**
     * 新增
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject addOutLibResult(ArrayList<ArrayList<String>> list1) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAddDeal = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarInBound.AddDealResult");// 新增交易信息
            PreparedStatement patAddDeal = connection
                    .prepareStatement(sqlAddDeal);
            String sqlupdateButter = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarInBound.updateButter");// 修改位置表
            PreparedStatement patupdateButter = connection
                    .prepareStatement(sqlupdateButter);

            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            String systime = dateFormat.format(now);

            for (int i = 0; i < list1.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) list1.get(i);
                // String relutNo = list.get(0);
                // String planNo = list.get(1);
                String matNo = list.get(2);
//                String mBkenNo = list.get(3);
                String mbStorage = list.get(4);
                String dealOrde = list.get(5);
                String name = list.get(6);
                String strOutNo = list.get(7);
                String strOutname = list.get(8);

                String recFlag = "";
                String sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmCollarInBound.queryRecFalg");
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, dealOrde);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    recFlag = rs.getString("TAR_REC_FLAG");
                }
                rs.close();
                psta.close();
                if (recFlag.equals("1")) {
                    cro.setV_errMsg("该记录已收料！");
                    return cro;
                }
                // 获取交易单号
                String dealOrdeNo = FrmCollarOutBound.doAddDealOrde("IC", "800703", connection);
                // 新增交易表
                patAddDeal.setObject(1, strOutNo);
                patAddDeal.setObject(2, strOutname);
                patAddDeal.setObject(3, systime);
                patAddDeal.setObject(4, name);
                patAddDeal.setObject(5, systime);
                patAddDeal.setObject(6, list.get(9));
                patAddDeal.setObject(7, dealOrdeNo);
                patAddDeal.setObject(8, dealOrde);
                patAddDeal.setObject(9, name);
                patAddDeal.setObject(10, systime);
                patAddDeal.setObject(11, dealOrde);
                patAddDeal.addBatch();

                // 修改位置表标志
                // patupdateButter.setObject(1, "1");
                patupdateButter.setObject(1, name);
                patupdateButter.setObject(2, systime);
                patupdateButter.setObject(3, mbStorage);
                patupdateButter.setObject(4, matNo);
                patupdateButter.addBatch();
            }
            patAddDeal.executeBatch();
            patupdateButter.executeBatch();

            cro.setV_errMsg("收料成功！");
            connection.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            cro.setV_errMsg(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 list 传递参数
     * @return obj
     */
    public CoreReturnObject deleteOutLibResult(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAddDeal = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarInBound.deleteDealResult");// 删除交易信息
            PreparedStatement patAddDeal = connection
                    .prepareStatement(sqlAddDeal);
            String sqlupdateButter = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarInBound.updateButter");// 修改位置表
            PreparedStatement patupdateButter = connection
                    .prepareStatement(sqlupdateButter);

            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            String systime = dateFormat.format(now);
            for (int i = 0; i < list.size(); i++) {
                ArrayList<String> pram = (ArrayList<String>) list.get(i);
//                String relutNo = pram.get(0);
//                String planNo = pram.get(1);
                String matNo = pram.get(2);
//                String mBkenNo = pram.get(3);
                String mbStorage = pram.get(4);
                String name = pram.get(5);
                String dealNo = pram.get(6);

                String recFlag = "";
                String sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmCollarInBound.queryDeleteRecFalg");
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, mbStorage);
                psta.setObject(2, matNo);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    recFlag = rs.getString("TAR_REC_FLAG");
                }
                rs.close();
                psta.close();
                if (recFlag.equals("0")) {
                    cro.setV_errMsg("该记录已撤销收料！");
                    return cro;
                }

                // 删除交易信息
                patAddDeal.setObject(1, dealNo);
                patAddDeal.setObject(2, name);
                patAddDeal.setObject(3, systime);
                patAddDeal.setObject(4, mbStorage);
                patAddDeal.setObject(5, matNo);
                patAddDeal.addBatch();

                // patupdateButter.setObject(1, "0");
                patupdateButter.setObject(1, name);
                patupdateButter.setObject(2, systime);
                patupdateButter.setObject(3, mbStorage);
                patupdateButter.setObject(4, matNo);
                patupdateButter.addBatch();
            }
            patAddDeal.executeBatch();
            patupdateButter.executeBatch();
            cro.setV_errMsg("撤销收料成功！");
            connection.commit();

        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            cro.setV_errMsg(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 dealOrde
     * @return
     */
    public CoreReturnObject doQueryCount(String dealOrde)
    {
        return this.getDataBySqlID("FrmCollarInBound.doQueryCount", new Object[]{dealOrde});
    }

}
