package com.steering.pss.ydm.pipeleavefactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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.StringTool;

/**
 * 装车作业.
 * @author Administrator TJG 2015-03-10
 */
public class FrmPipeSendList extends DBCommon {
    /**
     * 查询客户信息.
     * @return obj
     */
    public CoreReturnObject queryCustomer() {
        // 通过sqlID查询数据
        return this.getDataBySqlID("FrmPipeSendList.QueryCustomer");
    }

    /**
     * 查询品名信息.
     * @return obj
     */
    public CoreReturnObject queryProductName() {
        return this.getDataBySqlID("FrmPipeSendList.QueryProductName");
    }

    /**
     * 查询专用线信息.
     * @return obj
     */
    public CoreReturnObject queryPrivateSid() {
        return this.getDataBySqlID("FrmPipeSendList.QueryPrivateSid");
    }

    /**
     * 查询到站信息.
     * @return obj
     */
    public CoreReturnObject queryArriveStation() {
        return this.getDataBySqlID("FrmPipeSendList.QueryArriveStation");
    }

    /**
     * 查询理货信息是否有装车序号.
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSql(Connection conn, String strlist) throws Exception {
        int seq = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeSendList.QuerySendSql");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strlist + "000001";
            }
            else {
                seq = Integer.parseInt(res.substring(14, 20));
            }
        }
        rs.close();
        psta.close();
        return strlist + StringTool.formatDecimal(++seq, "000000");
    }

    /**
     * 查询理货单号.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject querySendList(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.QuerySendList", list
                .toArray());
    }

    /**
     * 查询提单信息.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryBillInfo(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.QueryBillInfo", list
                .toArray());
    }

    /**
     * 查询物料信息.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryMatInfo(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.QueryMatInfo", list
                .toArray());
    }

    /**
     * 根据订单号、炉、批、组号查询库存支数.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryMatInfo1(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.QueryMatInfo1", list
                .toArray());
    }

    /**
     * 查询理货信息（GROUP）.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallyInfo(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.QueryTallyInfo", list
                .toArray());
    }

    /**
     * 查询理货信息（GROUP）.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallyInfo2(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.QueryTallyInfo2", list
                .toArray());
    }

    /**
     * 根据提单号查询装车实绩信息
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryPerformance(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.queryPerformance", list
                .toArray());
    }

    /**
     * 根据理货单号查询装车实绩是否有对应的理货单信息
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryPerforInfo(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.queryPerforInfo", list
                .toArray());
    }

    /**
     * 根据理货单号查询装车材料表支数、重量、长度信息
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryMaterial(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.queryMaterial", list
                .toArray());
    }

    /**
     * 新增理貨信息.
     * @param list1 新增參數
     * @return obj
     */
    public CoreReturnObject addTallyInfo(ArrayList<String> list1) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            int abc = Integer.parseInt(list1.get(16));
            int[] piecesList = new int[] { (int) abc }; // 支数
            // int piecesList=Integer.parseInt(list1.get(4));
            ArrayList<String> billMats = new ArrayList<String>(); // 保存提单材料表主键
            // 数组长度等于装车支数和
            String sqlquery = "";
            String sqlupdate = "";
            String sqlinsert = "";

            for (int i = 0; i < piecesList.length; i++) {
                // 按炉批行和提单号查询提单材料主键
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeSendList.QueryBillSql");
                // select pk from table where stove_no = ? and lot_no =
                // ?,bill_no = ?,group_no = ? and rownum < piecesList[i]+1
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list1.get(1));
                psta.setString(2, list1.get(13));
                psta.setString(3, list1.get(14));
                psta.setString(4, list1.get(15));
                psta.setInt(5, piecesList[i] + 1);
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    billMats.add(rs.getString(1));
                }
                rs.close();
                psta.close();
            }
            String args = "";
            for (int i = 0; i < billMats.size(); i++) {
                args += "'" + billMats.get(i).toString() + "',";
            }
            args = args.substring(0, args.length() - 1);

            sqlupdate = "UPDATE YDM_ZC_BILL_C SET EDIT_FLAG = '1'"
                    + " WHERE BILL_SQE IN(" + args + ")"; // 修改提单材料表
            Statement sta = connection.createStatement();
            sta.executeUpdate(sqlupdate);
            sta.close();

            for (int i = 0; i < billMats.size(); i++) {
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeSendList.QueryAddSend"); // 查詢出要新增的数据
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list1.get(1));
                psta.setString(2, billMats.get(i));
                ResultSet rs = psta.executeQuery();
                FrmLoadVehicleMat lvMat = new FrmLoadVehicleMat();
                if (rs.next()) {
                    lvMat.setStrOrdno(rs.getString(1));
                    lvMat.setStrOrdsqe(rs.getString(2));
                    lvMat.setStrOrdsqe(rs.getString(3));
                    lvMat.setStrMatno(rs.getString(4));
                    lvMat.setStrLotno(rs.getString(5));
                    lvMat.setStrJudgeno(rs.getString(6));
                    lvMat.setStrBatchno(rs.getString(7));
                    lvMat.setStrGroupno(rs.getString(8));
                    lvMat.setStrStoveno(rs.getString(9));
                    lvMat.setStrMaterialno(rs.getString(10));
                    lvMat.setStrMaterialname(rs.getString(11));
                    lvMat.setStrpProduccode(rs.getString(12));
                    lvMat.setStrProducname(rs.getString(13));
                    lvMat.setStrStdcode(rs.getString(14));
                    lvMat.setStrStdname(rs.getString(15));
                    lvMat.setStrGradecode(rs.getString(16));
                    lvMat.setStrGradename(rs.getString(17));
                    lvMat.setStrSteelcode(rs.getString(18));
                    lvMat.setStrSteelname(rs.getString(19));
                    lvMat.setStrSidstyle(rs.getString(20));
                    lvMat.setStrStdstyledesc(rs.getString(21));
                    lvMat.setStrSpecode(rs.getString(22));
                    lvMat.setStrSpecname(rs.getString(23));
                    lvMat.setStrModelcode(rs.getString(24));
                    lvMat.setStrModeldesc(rs.getString(25));
                    lvMat.setStrPlinecode(rs.getString(26));
                    lvMat.setStrPlinename(rs.getString(27));
                    lvMat.setStrActdimater(rs.getString(28));
                    lvMat.setStrActheight(rs.getString(29));
                    lvMat.setStrActlenmin(rs.getString(30));
                    lvMat.setStrActlenmax(rs.getString(31));
                    lvMat.setStrActlen(rs.getString(32));
                    lvMat.setStrActcount(rs.getString(33));
                    lvMat.setStrActweight(rs.getString(34));
                    lvMat.setStrTheoryweight(rs.getString(35));
                    lvMat.setStrResultode(rs.getString(36));
                    lvMat.setStrProbc(rs.getString(37));
                    lvMat.setStrProbz(rs.getString(38));
                    lvMat.setStrProtime(rs.getString(39));
                    lvMat.setStrSaleorg(rs.getString(40));
                    lvMat.setStrSaleorgdesc(rs.getString(41));
                    lvMat.setStrTranstpe(rs.getString(42));
                    lvMat.setStrReceivno(rs.getString(43));
                    lvMat.setStrReceivnm(rs.getString(44));
                    lvMat.setStrStationno(rs.getString(45));
                    lvMat.setStrStationnm(rs.getString(46));
                    lvMat.setStrSpclno(rs.getString(47));
                    lvMat.setStrSpclname(rs.getString(48));
                    lvMat.setStrPort(rs.getString(49));
                    lvMat.setStrVstel(rs.getString(50));
                    lvMat.setStrVsteldesc(rs.getString(51));
                    lvMat.setStrCarrycode(rs.getString(52));
                    lvMat.setStrCarryname(rs.getString(53));
                    lvMat.setStrDelivycode(rs.getString(54));
                    lvMat.setStrDelivyname(rs.getString(55));
                    lvMat.setStrUnitno(rs.getString(56));
                    lvMat.setStrUnitname(rs.getString(57));

                    sqlinsert = CoreXmlSqlParsersFactory
                            .getSql("FrmPipeSendList.AddTallyInfo"); // 新增到发货材料表
                    psta = connection.prepareStatement(sqlinsert);
                    psta.setString(1, querySendSql(connection, list1.get(7)));
                    psta.setString(2, list1.get(7));
                    psta.setString(3, list1.get(1));
                    psta.setString(4, list1.get(8));
                    psta.setString(5, lvMat.getStrOrdno());
                    psta.setString(6, lvMat.getStrOrdsqe());
                    psta.setString(7, lvMat.getStrOrdsqeno());
                    psta.setString(8, lvMat.getStrMatno());
                    psta.setString(9, lvMat.getStrLotno());
                    psta.setString(10, list1.get(13));
                    psta.setString(11, list1.get(14));
                    psta.setString(12, list1.get(15));
                    psta.setString(13, lvMat.getStrStoveno());
                    psta.setString(14, lvMat.getStrMaterialno());
                    psta.setString(15, lvMat.getStrMaterialname());
                    psta.setString(16, lvMat.getStrpProduccode());
                    psta.setString(17, lvMat.getStrProducname());
                    psta.setString(18, lvMat.getStrStdcode());
                    psta.setString(19, lvMat.getStrStdname());
                    psta.setString(20, lvMat.getStrGradecode());
                    psta.setString(21, lvMat.getStrGradename());
                    psta.setString(22, lvMat.getStrSteelcode());
                    psta.setString(23, lvMat.getStrSteelname());
                    psta.setString(24, lvMat.getStrSidstyle());
                    psta.setString(25, lvMat.getStrStdstyledesc());
                    psta.setString(26, lvMat.getStrSpecode());
                    psta.setString(27, lvMat.getStrSpecname());
                    psta.setString(28, lvMat.getStrModelcode());
                    psta.setString(29, lvMat.getStrModeldesc());
                    psta.setString(30, lvMat.getStrPlinecode());
                    psta.setString(31, lvMat.getStrPlinename());
                    psta.setString(32, lvMat.getStrActdimater());
                    psta.setString(33, lvMat.getStrActheight());
                    psta.setString(34, lvMat.getStrActlenmin());
                    psta.setString(35, lvMat.getStrActlenmax());
                    psta.setString(36, lvMat.getStrActlen());
                    psta.setString(37, lvMat.getStrActcount());
                    psta.setString(38, lvMat.getStrActweight());
                    psta.setString(39, lvMat.getStrTheoryweight());
                    psta.setString(40, lvMat.getStrResultode());
                    psta.setString(41, lvMat.getStrProbc());
                    psta.setString(42, lvMat.getStrProbz());
                    psta.setString(43, lvMat.getStrProtime());
                    psta.setString(44, lvMat.getStrSaleorg());
                    psta.setString(45, lvMat.getStrSaleorgdesc());
                    psta.setString(46, list1.get(2));
                    psta.setString(47, list1.get(6));
                    psta.setString(48, list1.get(5));
                    psta.setString(49, list1.get(4));
                    psta.setString(50, list1.get(3));
                    psta.setString(51, lvMat.getStrSpclno());
                    psta.setString(52, lvMat.getStrSpclname());
                    psta.setString(53, lvMat.getStrPort());
                    psta.setString(54, lvMat.getStrVstel());
                    psta.setString(55, lvMat.getStrVsteldesc());
                    psta.setString(56, list1.get(10));
                    psta.setString(57, list1.get(9));
                    psta.setString(58, lvMat.getStrDelivycode());
                    psta.setString(59, lvMat.getStrDelivyname());
                    psta.setString(60, lvMat.getStrUnitno());
                    psta.setString(61, lvMat.getStrUnitname());
                    psta.setString(62, list1.get(11));
                    psta.setString(63, list1.get(12));
                    psta.executeUpdate();
                    psta.close();
                }
            }
            cro.setResult(billMats.size());
            connection.commit();
        }
        catch (Exception 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 list 传递参数
     * @return obj
     */
    public CoreReturnObject addPerformance(ArrayList<String> list) {
        return this.setDataBySqlID("FrmPipeSendList.addPerformance", list
                .toArray());
    }

    /**
     * 修改装车材料表信息
     * @param list 修改参数
     * @return obj
     */
    public CoreReturnObject upDateInfo(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            int sunUpdate = Integer.parseInt(list.get(3));
            int[] piecesList = new int[] { (int) sunUpdate }; // 支数
            ArrayList<String> loadMats = new ArrayList<String>(); // 保存装车材料表主键
            ArrayList<String> billMats = new ArrayList<String>(); // 保存发货材料表主键
            // 数组长度等于装车支数和
            String sqlquery = "";
            String sqlupdate = "";

            for (int i = 0; i < piecesList.length; i++) {
                // 按炉批行和提单号查询装车材料主键
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeSendList.QueryLoadSql");
                // select pk from table where stove_no = ? and lot_no =
                // ?,bill_no = ?,group_no = ? and rownum < piecesList[i]+1
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(4));
                psta.setString(2, list.get(0));
                psta.setString(3, list.get(1));
                psta.setString(4, list.get(2));
                psta.setInt(5, piecesList[i] + 1);
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    loadMats.add(rs.getString(1));
                }
                rs.close();
                psta.close();
            }
            String args = ""; // 装车材料主键
            for (int i = 0; i < loadMats.size(); i++) {
                args += "'" + loadMats.get(i).toString() + "',";
            }
            args = args.substring(0, args.length() - 1);

            sqlupdate = "UPDATE YDM_ZC_LOADVEHICLE_MAT SET VALIDFLAG = '0',"
                    + "DELETE_NAME=" + "'" + list.get(5) + "'" + ","
                    + "DELETE_TIME=SYSDATE " + " WHERE LOADVEHICLE_SQE IN("
                    + args + ")"; // 修改装车材料表有效标志
            Statement sta = connection.createStatement();
            sta.executeUpdate(sqlupdate);
            sta.close();

            for (int i = 0; i < piecesList.length; i++) {
                // 按炉批行和提单号查询未理货的发货材料主键
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeSendList.QuerySqlBill");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(4));
                psta.setString(2, list.get(0));
                psta.setString(3, list.get(1));
                psta.setString(4, list.get(2));
                psta.setInt(5, piecesList[i] + 1);
                ResultSet rs1 = psta.executeQuery();
                while (rs1.next()) {
                    billMats.add(rs1.getString(1));
                }
                rs1.close();
                psta.close();

            }
            String args1 = ""; // 发车材料主键
            for (int i = 0; i < billMats.size(); i++) {
                args1 += "'" + billMats.get(i).toString() + "',";
            }
            args1 = args1.substring(0, args1.length() - 1);

            sqlupdate = "UPDATE YDM_ZC_BILL_C SET EDIT_FLAG = '0'"
                    + " WHERE BILL_SQE IN(" + args1 + ")"; // 修改发车材料表有效标志
            Statement staload = connection.createStatement();
            int count = staload.executeUpdate(sqlupdate);
            staload.close();
            connection.commit();
            cro.setResult(count);
        }
        catch (Exception 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 list 传递参数
     * @return obj
     */
    public CoreReturnObject updatePerformance(ArrayList<String> list) {
        return this.setDataBySqlID("FrmPipeSendList.updatePerformance", list
                .toArray());
    }

    /**
     * 修改装车实绩表状态
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject updatePerfor(ArrayList<String> list) {
        return this.setDataBySqlID("FrmPipeSendList.updatePerfor", list
                .toArray());
    }

    /**
     * 修改装车材料表状态
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject updateMaterial(ArrayList<String> list) {
        return this.setDataBySqlID("FrmPipeSendList.updateMaterial", list
                .toArray());
    }

    /**
     * 查询理货物料
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject querySendAll(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeSendList.querySendAll", list
                .toArray());
    }

    /**
     * 修改订单材料表状态
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject updateMatAll(ArrayList<String> list) {
        return this.setDataBySqlID("FrmPipeSendList.updateMatAll", list
                .toArray());
    }
}
