package com.steering.pss.ydm.pipeleavefactory;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.ydm.entity.YdmZcLoadvehicleMatEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 装车作业.
 * @author Administrator TJG 2015-05-13
 */
public class FrmPipeEntruck extends DBCommon {
    /**
     * 查询理货单信息.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallyInfo(ArrayList<String> list,
            String loadType, String[] storager, String storag) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.QueryTallyInfo");
        if (loadType.equals("")) {
            sql += " AND T.LOADVEHICLE_FLAG IN ('0','1','2')";
        }
        else if (loadType.equals("2")) {
            sql += " AND T.LOADVEHICLE_FLAG='2'";
        }
        else {
            sql += " AND T.LOADVEHICLE_FLAG IN ('0','1')";
        }
        if (storag.equals("")) {
            sql += CoreConverter.toSqlConditionString("T.OUT_STOCK_CODE",
                    storager);
        }
        else {
            sql += " AND T.OUT_STOCK_CODE='" + storag + "'";
        }
        sql += " GROUP BY T.TALLYSHEET_NO,T.BILL_NO,T.CREATE_TIME,T.END_TIME,T.ACCBATCH_RECY,T.LOADVEHICLE_FLAG,M.SUPPLY_UNIT  ORDER BY T.CREATE_TIME DESC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询理货材料表每支材料重量
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryMatTallyC(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeEntruck.queryMatTallyC", list
                .toArray());
    }

    /**
     * 查询理货材料信息（GROUP）.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallyInfoMat(ArrayList<String> list,
            String loadType) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.QueryTallyInfoMat");

        sql += " GROUP BY T.TALLYSHEET_NO, T.JUDGE_STOVE_NO, T.BATCH_NO,T.GROUP_NO, T.ORDER_NO, T.ORDER_SEQ,T.LOCATION_NO, "
                + "T.DELIVERY_NO, T.STOVE_NO, T.MATERIAL_NAME, T.PRODUCNAME, T.STD_NAME, "
                + "T.GRADENAME, T.STEELNAME, T.STD_STYLE_DESC, T.SPEC_NAME, T.MODEL_DESC, "
                + "T.PROCESS_DESC, T.PLINE_NAME, T.ACT_DIMATER, T.ACT_HEIGHT, T.ACT_LEN_MIN, "
                + "T.ACT_LEN_MAX, T.JUDGE_RESULT_CODE, T.STORAGE_NO,T.VSTEL_DESC, T.CREATE_NAME, "
                + "T.ORD_PK, T.ORD_LN_PK, T.ORD_LN_DLY_PK, T.BILL_NO,T.MEMO ORDER BY T.ORDER_NO,T.ORDER_SEQ,T.DELIVERY_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.GROUP_NO ASC) T1";
        if (loadType.equals("0")) {
            sql += " WHERE T1.PREP_COUNT != 0";
        }
        return this.getData(sql, list.toArray());
    }

    /**
     *查询提单表状态
     * @param
     * @return obj
     */
    public CoreReturnObject queryBillType(String billType) {
        return this.getDataBySqlID("FrmPipeEntruck.QueryBillType",
                new Object[] { billType });
    }

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

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

    /**
     * 查询发货实绩表信息
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryDelivery(ArrayList<String> list,
            String[] storager, String storag) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.queryDelivery");
        if (storag.equals("")) {
            sql += CoreConverter.toSqlConditionString("T.OUT_STOCK_CODE",
                    storager);
        }
        else {
            sql += " AND T.OUT_STOCK_CODE='" + storag + "'";
        }
        // sql += " AND T.LOADVEHICLE_NO || '&' LIKE ('%' || '" + load
        // + "' || '%') AND T.AFFIRM_FLAG || '&' LIKE ('%' || '" + affirm
        // + "' || '%')";
        sql += " ORDER BY T.CREATE_TIME DESC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询车号对应的装车单号
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryCarLoad(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeEntruck.queryCarLoad", list
                .toArray());
    }

    /**
     * 查询发货实绩从表信息
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryMativery(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.queryMativery");
        sql += "GROUP BY T.JUDGE_STOVE_NO, T.BATCH_NO, T.GROUP_NO, T.ORDER_NO, T.ORDER_SEQ, "
                + "T.ORDER_SEQ_NO, T.LOADVEHICLE_NO, T.BILL_NO, T.TALLYSHEET_NO, T.VEHICLE_NO, "
                + "T.MATERIAL_NO, T.PRODUCNAME, T.GRADENAME, T.STEELNAME, T.SPEC_NAME, "
                + "T.MODEL_DESC, T.STORAGE_NO,T.VSTEL_DESC, T.STD_NAME, T.STD_STYLE_DESC, T.CREATE_NAME,T.ACT_LEN_MIN,T.ACT_LEN_MAX,T.ORD_PK,"
                + " T.ORD_LN_PK,T.ORD_LN_DLY_PK  ORDER BY T.JUDGE_STOVE_NO, T.BATCH_NO ASC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询确认标志
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryAppFlag(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeEntruck.queryAppFlag", list
                .toArray());
    }

    /**
     * 查询承运单位
     * @param
     * @return obj
     */
    public CoreReturnObject queryTallyCar(String tally, String vehi) {
        return this.getDataBySqlID("FrmPipeEntruck.QueryTallyCar",
                new Object[] { tally, vehi });
    }

    /**
     * 查询确认标志
     * @param strBill 传递参数
     * @return obj
     */
    public CoreReturnObject queryCar(String strBill, String strTally,
            String strCar) {
        return this.getDataBySqlID("FrmPipeEntruck.queryCar", new Object[] {
                strBill, strTally, strCar });
    }

    /**
     * 查询理货主表装车状态
     * @param strBill 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallType(String strBill, String strTally) {
        return this.getDataBySqlID("FrmPipeEntruck.queryTallType",
                new Object[] { strBill, strTally });
    }

    public CoreReturnObject queryCarTally(String strTally) {
        return this.getDataBySqlID("FrmPipeEntruck.queryCarTaly",
                new Object[] { strTally });
    }

    /**
     * 每个装车单号对应的装车序号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public static String querySendSl(Connection conn, String strlist)
            throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.QuerySendSl");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString(1);
        }
        rs.close();
        psta.close();
        return seq;
    }

    /**
     *自动生成装车单号
     * @param conn
     * @param strSend 传递参数
     * @return
     * @throws Exception 异常处理
     */
    public String querySendLoad(Connection conn, String strSend)
            throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.QuerySendList");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strSend);
        psta.setString(2, strSend);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString(1);
        }
        rs.close();
        psta.close();
        return seq;
//        if (rs.next()) {
//            String res = rs.getString(1);
//            if (res == null) {
//                res = strSend + "0001";
//            }
//            else {
//                seq = Integer.parseInt(res.substring(res.length() - 4, res
//                        .length()));
//            }
//        }
//        rs.close();
//        psta.close();
//        return strSend + StringTool.formatDecimal(++seq, "0000");
    }

    /**
     * 每支材料对应的倒库倒库单号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public static String querySendOut(Connection conn, String strlist)
            throws Exception {
        int seq = 0;
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.queryOutListNew");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, "OH" + matter1.format(dt));
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = "OH" + matter1.format(dt) + "00001";
            }
            else {
                seq = Integer.parseInt(res.substring(res.length() - 5, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return "OH" + matter1.format(dt)
                + StringTool.formatDecimal(++seq, "00000");
    }

    /**
     * 每支材料对应的倒库（出库）流水号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public static String querySendOutList(Connection conn, String strlist,
            String strType) throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.queryOut");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, matter1.format(dt));
       //psta.setString(3, strType);
       // psta.setString(4, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString(1);
        }
        rs.close();
        psta.close();
        return seq;
    }

    /**
     * 每支材料对应的倒库倒库单号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public static String querySendOutXs(Connection conn, String strlist)
            throws Exception {
        int seq = 0;
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.queryOutListNewXs");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, "OH" + matter1.format(dt));
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = "OH" + matter1.format(dt) + "00001";
            }
            else {
                seq = Integer.parseInt(res.substring(res.length() - 5, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return "OH" + matter1.format(dt)
                + StringTool.formatDecimal(++seq, "00000");
    }

    /**
     * 每支材料对应的倒库（出库）流水号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public static String querySendOutListXs(Connection conn, String strlist,
            String strType) throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.queryOutXs");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, matter1.format(dt));
        psta.setString(3, strType);
        psta.setString(4, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString(1);
        }
        rs.close();
        psta.close();
        return seq;
    }

    /***
     * 查询吊车车牌
     * @return
     */

    /*public CoreReturnObject doQueryCrane()
    {
        return  this.getDataBySqlID("TmsDao","FrmPipeEntruck.doQueryCrane");
    }注释生产物流  2024/4/24 肖沅累*/

    /***
     * 查询司机信息
     * @return
     */
    /*public CoreReturnObject doQueryDrive()
    {
        return  this.getDataBySqlID("TmsDao","FrmPipeEntruck.doQueryDrive");
    }注释生产物流  2024/4/24 肖沅累*/
    /**
     * 新增装车
     * @param pram 装车材料表参数
     * @param pram1 装车主表参数
     * @param strSend 理货单号
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject addTallyInfo(ArrayList<Object> pram,
            ArrayList<String> pram1, String strSend, String loadNoMat,ArrayList<String> craneList) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        //Connection tmsCon = this.getDao("TmsDao").getConnection();注释生产物流  2024/4/24 肖沅累
        PreparedStatement psta = null;
        Date nowTime = new Date();
        SimpleDateFormat dfT = new SimpleDateFormat("yyyyMM");
        String strNowTime = dfT.format(nowTime);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            ArrayList<String> billMats = new ArrayList<String>(); // 保存理货材料号
            ArrayList<String> loadMats = new ArrayList<String>(); // 保存理货材料表主键
            // 数组长度等于装车支数和
            String sqlquery = "";
            String sqlupdate = "";
            int strcount = 0;// 支数
            double strWeight = 0;// 重量
            double strLen = 0;// 长度
            DecimalFormat df = new DecimalFormat("0.000");
            DecimalFormat dflen = new DecimalFormat("0.00");

            String insertLoadC = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.AddLoadMat"); // 新增到装车材料表
            PreparedStatement pstaLoadC = connection
                    .prepareStatement(insertLoadC);
            String insertLoadM = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.AddLoadPerf"); // 新增到装车实绩表
            PreparedStatement pstaLoadM = connection
                    .prepareStatement(insertLoadM);
            String updateTallM1 = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.updateTallM1"); // 修改理货主表装车状态
            PreparedStatement pstaTallM1 = connection
                    .prepareStatement(updateTallM1);
            String updateTallM2 = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.updateTallM2"); // 修改理货主表装车状态
            PreparedStatement pstaTallM2 = connection
                    .prepareStatement(updateTallM2);
            String updateBillC = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.updateBillC"); // 修改提单材料表装车状态
            PreparedStatement pstabillC = connection
                    .prepareStatement(updateBillC);
            String updateTallC = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.updateTallC"); // 修改理货材料表装车标志
            PreparedStatement pstatallC = connection
                    .prepareStatement(updateTallC);
            String LoadNo = "";
            String tallNo = "";
            if (loadNoMat.equals("")) {
                tallNo = querySendLoad(connection, strSend);
            }
            else {
                tallNo = loadNoMat;
            }



            List<YdmZcLoadvehicleMatEntity> lvMats = new ArrayList<YdmZcLoadvehicleMatEntity>();
            for (int j = 0; j < pram.size(); j++) {
                billMats.clear();
                loadMats.clear();
                lvMats.clear();
                ArrayList<String> list = (ArrayList<String>) pram.get(j);
                int count = Integer.parseInt(list.get(0));
                int countW = 0;
                int billcount = 0;// 反写提单主表支数
                double billWeight = 0;// 反写提单主表重量
                double billLen = 0;// 反写提单主表长度
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.queryAmount");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(1));
                psta.setString(2, list.get(2));
                psta.setString(3, list.get(3)+list.get(4));
                psta.setString(4, list.get(6));
                psta.setString(5, list.get(7));
                psta.setString(6, list.get(21));
                psta.setString(7, list.get(26));
                psta.setString(8,list.get(27));
                psta.setString(9,list.get(28));
                ResultSet rsnum = psta.executeQuery();
                if (rsnum.next()) {
                    countW = Integer.parseInt(rsnum.getString(1));
                }
                rsnum.close();
                psta.close();

                if (count > countW) {
                    cro.setV_errMsg("您输入的支数在[" + list.get(6) + "]该炉、["
                            + list.get(7) + "]该批号下已不足" + count + "支,请重新输入！");
                    cro.setV_errCode(-1);
                    return cro;
                }
                sqlquery = CoreXmlSqlParsersFactory.getSql(
                        "FrmPipeEntruck.QueryBillSql").replace("#rumCont#",
                        String.valueOf(count + 1));
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(1));
                psta.setString(2, list.get(2));
                psta.setString(3, list.get(6));
                psta.setString(4, list.get(7));
                psta.setString(5, list.get(21));
                psta.setString(6, list.get(3)+list.get(4));
                psta.setString(7, list.get(26));
                psta.setString(8,list.get(27));
                psta.setString(9,list.get(28));
                ResultSet rs1 = psta.executeQuery();
                lvMats = EntityTool.getEntitysByRs(rs1,
                        YdmZcLoadvehicleMatEntity.class);
                rs1.close();
                psta.close();

                String CarLoadType = "";
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.queryCarLoad"); // 查询车号对应的装车单号
                PreparedStatement pstaCar = connection
                        .prepareStatement(sqlquery);
                pstaCar.setObject(1, list.get(1));
                pstaCar.setObject(2, list.get(2));
                pstaCar.setObject(3, list.get(8));
                ResultSet rscar = pstaCar.executeQuery();
                if (rscar.next()) {
                    CarLoadType = rscar.getString(1);
                }
                rscar.close();
                pstaCar.close();

                if (!list.get(9).toString().equals(CarLoadType)
                        && CarLoadType.length() != 0) {
                    cro.setV_errMsg("同一提单，理货单在同一车下只允许存在一个装车单号！");
                    cro.setV_errCode(-1);
                    return cro;
                }

                // 获取装车序号
                LoadNo = querySendSl(connection, tallNo);
                int loadNo = Integer.parseInt(LoadNo.substring(
                        LoadNo.length() - 6, LoadNo.length()));
                for (YdmZcLoadvehicleMatEntity lvMat : lvMats) {

                    strcount += Double.parseDouble(lvMat.getActCount());
                    strWeight += Double.parseDouble(lvMat.getActWeight());
                    strLen += Double.parseDouble(lvMat.getActLen());

                    billcount += Double.parseDouble(lvMat.getActCount());
                    billWeight += Double.parseDouble(lvMat.getActWeight());
                    billLen += Double.parseDouble(lvMat.getActLen());

                    // 新增到装车材料表
                    pstaLoadC.setString(1, tallNo
                            + StringTool.formatDecimal(loadNo, "000000"));
                    pstaLoadC.setString(2, tallNo);
                    pstaLoadC.setString(3, list.get(1));
                    pstaLoadC.setString(4, list.get(2));
                    pstaLoadC.setString(5, list.get(8));
                    pstaLoadC.setString(6, lvMat.getOrderNo());
                    pstaLoadC.setString(7, lvMat.getOrderSeq());
                    pstaLoadC.setString(8, lvMat.getOrderSeqNo());
                    pstaLoadC.setString(9, lvMat.getProOrderNo());
                    pstaLoadC.setString(10, lvMat.getMatNo());
                    pstaLoadC.setString(11, lvMat.getLotNo());
                    pstaLoadC.setString(12, list.get(6));
                    pstaLoadC.setString(13, list.get(7));
                    pstaLoadC.setString(14, lvMat.getGroupNo());
                    pstaLoadC.setString(15, lvMat.getStoveNo());
                    pstaLoadC.setString(16, lvMat.getMaterialNo());
                    pstaLoadC.setString(17, lvMat.getMaterialName());
                    pstaLoadC.setString(18, lvMat.getProduccode());
                    pstaLoadC.setString(19, lvMat.getProducname());
                    pstaLoadC.setString(20, lvMat.getStdCode());
                    pstaLoadC.setString(21, lvMat.getStdName().trim());
                    pstaLoadC.setString(22, lvMat.getGradecode());
                    pstaLoadC.setString(23, lvMat.getGradename());
                    pstaLoadC.setString(24, lvMat.getSteelcode());
                    pstaLoadC.setString(25, lvMat.getSteelname());
                    pstaLoadC.setString(26, lvMat.getStdStyle());
                    pstaLoadC.setString(27, lvMat.getStdStyleDesc());
                    pstaLoadC.setString(28, lvMat.getSpecCode());
                    pstaLoadC.setString(29, lvMat.getSpecName());
                    pstaLoadC.setString(30, lvMat.getModelCode());
                    pstaLoadC.setString(31, lvMat.getModelDesc());
                    pstaLoadC.setString(32, lvMat.getProcessCdoe());
                    pstaLoadC.setString(33, lvMat.getProcessDesc());
                    pstaLoadC.setString(34, lvMat.getPlineCode());
                    pstaLoadC.setString(35, lvMat.getPlineName());
                    pstaLoadC.setString(36, lvMat.getActDimater());
                    pstaLoadC.setString(37, lvMat.getActHeight());
                    pstaLoadC.setString(38, lvMat.getActLen());
                    pstaLoadC.setString(39, lvMat.getActLenMin());
                    pstaLoadC.setString(40, lvMat.getActLenMax());
                    pstaLoadC.setString(41, lvMat.getActCount());
                    pstaLoadC.setString(42, lvMat.getActWeight());
                    pstaLoadC.setString(43, lvMat.getActTheoryWeight());
                    pstaLoadC.setString(44, lvMat.getJudgeResultCode());
                    pstaLoadC.setString(45, lvMat.getProBc());
                    pstaLoadC.setString(46, lvMat.getProBz());
                    if (lvMat.getRpoTime() == "") {
                        pstaLoadC.setString(47, "");
                    }
                    else {
                        pstaLoadC.setString(47, lvMat.getRpoTime().substring(0,
                                19));
                    }
                    pstaLoadC.setString(48, list.get(11));
                    pstaLoadC.setString(49, list.get(12));
                    pstaLoadC.setString(50, lvMat.getSaleOrg());
                    pstaLoadC.setString(51, lvMat.getSaleOrgDesc());
                    pstaLoadC.setString(52, lvMat.getSaleArea());
                    pstaLoadC.setString(53, lvMat.getStorageTypeNo());
                    pstaLoadC.setString(54, lvMat.getStorageTypeName());
                    pstaLoadC.setString(55, list.get(22));
                    pstaLoadC.setString(56, list.get(23));
                    pstaLoadC.setString(57, lvMat.getStorageNo());
                    pstaLoadC.setString(58, lvMat.getLocationNo());
                    pstaLoadC.setString(59, list.get(10));
                    pstaLoadC.setString(60, list.get(15));
                    pstaLoadC.setString(61, list.get(15));
                    pstaLoadC.setString(62, lvMat.getOrdPk());
                    pstaLoadC.setString(63, lvMat.getOrdLnPk());
                    pstaLoadC.setString(64, lvMat.getOrdLnDlyPk());
                    pstaLoadC.setString(65, list.get(16));
                    pstaLoadC.setString(66, list.get(17));
                    pstaLoadC.setString(67, lvMat.getTransType());
                    pstaLoadC.setString(68, lvMat.getReceivNo());
                    pstaLoadC.setString(69, lvMat.getReceivNm());
                    pstaLoadC.setString(70, lvMat.getStationNo());
                    pstaLoadC.setString(71, lvMat.getStationNm());
                    pstaLoadC.setString(72, lvMat.getSpclLnNo());
                    pstaLoadC.setString(73, lvMat.getSpclLnNm());
                    pstaLoadC.setString(74, lvMat.getPortNo());
                    pstaLoadC.setString(75, lvMat.getVstel());
                    pstaLoadC.setString(76, lvMat.getVstelDesc());
                    pstaLoadC.setString(77, lvMat.getDelivyPlaceCode());
                    pstaLoadC.setString(78, lvMat.getDelivyPlaceName());
                    pstaLoadC.setString(79, list.get(24));
                    pstaLoadC.setString(80, list.get(25));
                    pstaLoadC.setString(81, "1");
                    pstaLoadC.setString(82, "120504".equals(lvMat.getSupplyUnitCode())? ComBaseInfo.GetBalYear_2416(nowTime): ComBaseInfo.GetBalYear(nowTime));
                    pstaLoadC.setString(83, lvMat.getPortDesc());
                    pstaLoadC.setString(84, lvMat.getCreditNo());
                    pstaLoadC.setString(85, String.valueOf(lvMat.getSendNum()));
                    pstaLoadC.setString(86, String.valueOf(lvMat.getActFixNum()));
                    pstaLoadC.setString(87, String.valueOf(lvMat.getActFixLen()));
                    pstaLoadC.setString(88, lvMat.getFinishproMold());
                    pstaLoadC.setString(89, lvMat.getInMold());
                    pstaLoadC.setString(90, lvMat.getJudgeStdName());
                    pstaLoadC.setString(91, lvMat.getJudgeStdCode());
                    pstaLoadC.setString(92, lvMat.getMeasuringSeq());
                    pstaLoadC.setString(93, list.get(28));
                    pstaLoadC.setString(94, lvMat.getInType());
                    pstaLoadC.addBatch();
                    loadNo++;
                }

                // pstabillC.setObject(1, lvMat.getMatNo());
                pstabillC.setObject(1, list.get(1));
                pstabillC.setObject(2, list.get(2));
                pstabillC.setObject(3, list.get(6));
                pstabillC.setObject(4, list.get(7));
                pstabillC.setObject(5, list.get(21));
                pstabillC.setObject(6, list.get(3)+list.get(4));
                pstabillC.setObject(7, list.get(26));
                pstabillC.setObject(8, list.get(27));
                pstabillC.setObject(9, list.get(28));
                pstabillC.setObject(10, String.valueOf(count + 1));
                pstabillC.addBatch();

                // pstatallC.setObject(1, lvMat.getMatNo());
                pstatallC.setObject(1, list.get(1));
                pstatallC.setObject(2, list.get(2));
                pstatallC.setObject(3, list.get(6));
                pstatallC.setObject(4, list.get(7));
                pstatallC.setObject(5, list.get(21));
                pstatallC.setObject(6, list.get(3)+list.get(4));
                pstatallC.setObject(7, list.get(26));
                pstatallC.setObject(8, list.get(27));
                pstatallC.setObject(9, list.get(28));
                pstatallC.setObject(10, String.valueOf(count + 1));
                pstatallC.addBatch();

                pstaLoadC.executeBatch();
                pstabillC.executeBatch();
                pstatallC.executeBatch();
//                sqlupdate = "UPDATE YDM_ZC_BILL_M SET ACT_NUM = TO_NUMBER(NVL(ACT_NUM,'0')) + TO_NUMBER('"
//                        + billcount
//                        + "'),"
//                        + " ACT_WT = TO_NUMBER(NVL(ACT_WT,'0')) + TO_NUMBER('"
//                        + df.format(billWeight)
//                        + "'),ACT_TOTL_LEN=TO_NUMBER(NVL(ACT_TOTL_LEN,'0'))+TO_NUMBER('"
//                        + dflen.format(billLen)
//                        + "')  WHERE BILL_NO='"
//                        + list.get(1)
//                        + "' AND  ORD_LN_DLY_PK='"
//                        + list.get(20)
//                        + "' AND VALIDFLAG='1'"; // 修改提单主表装车标志
//                Statement sta1 = connection.createStatement();
//                sta1.executeUpdate(sqlupdate);
//                sta1.close();
            }
            sqlquery = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.QueryAddSend"); // 查詢出要新增的数据
            psta = connection.prepareStatement(sqlquery);
            psta.setString(1, pram1.get(4));
            psta.setString(2, pram1.get(5));
            ResultSet rs = psta.executeQuery();
            FrmLoadVehicleMat lvMat = new FrmLoadVehicleMat();
            if (rs.next()) {
                lvMat.setManageno(rs.getString(1));
                lvMat.setManagename(rs.getString(2));
                lvMat.setLoadtype(rs.getString(3));
                lvMat.setStrTranstpe(rs.getString(4));
                lvMat.setStrReceivno(rs.getString(5));
                lvMat.setStrReceivnm(rs.getString(6));
                lvMat.setStrStationno(rs.getString(7));
                lvMat.setStrStationnm(rs.getString(8));
                lvMat.setStrSpclno(rs.getString(9));
                lvMat.setStrSpclname(rs.getString(10));
                lvMat.setStrShipno(rs.getString(11));
                lvMat.setStrPlant(rs.getString(12));
                lvMat.setStrPlantname(rs.getString(13));
                lvMat.setStrVstel(rs.getString(14));
                lvMat.setStrVsteldesc(rs.getString(15));
                lvMat.setStrCarryno(rs.getString(16));
                lvMat.setStrCarrycode(rs.getString(17));
                lvMat.setStrCarryname(rs.getString(18));
                lvMat.setStrDelivycode(rs.getString(19));
                lvMat.setStrDelivyname(rs.getString(20));
                lvMat.setStrUnitno(rs.getString(21));
                lvMat.setStrUnitname(rs.getString(22));
                lvMat.setStorage(rs.getString(23));
                lvMat.setPrdCls(rs.getString(24));
            }
            rs.close();
            psta.close();

            sqlquery = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.queryTalyBill"); // 查询是否存在记录
            PreparedStatement psta1 = connection.prepareStatement(sqlquery);
            psta1.setString(1, tallNo);
            psta1.setString(2, pram1.get(8));
            psta1.setString(3, pram1.get(4));
            psta1.setString(4, pram1.get(5));
            ResultSet rs2 = psta1.executeQuery();
            if (rs2.next()) {
                sqlupdate = "UPDATE YDM_ZC_LOADVEHICLE SET STACKING_WT= TO_NUMBER('"
                        + df.format(strWeight)
                        + "')+TO_NUMBER(NVL(STACKING_WT,'0')),STACKING_PIECE= TO_NUMBER('"
                        + strcount
                        + "') +TO_NUMBER(NVL(STACKING_PIECE,'0')), STACKING_TOTL_LEN =TO_NUMBER( '"
                        + dflen.format(strLen)
                        + "' )+TO_NUMBER(NVL(STACKING_TOTL_LEN,'0')) WHERE LOADVEHICLE_NO = '"
                        + tallNo
                        + "'"
                        + " AND VEHICLE_NO = '"
                        + pram1.get(8)
                        + "' AND BILL_NO = '"
                        + pram1.get(4)
                        + "' AND TALLYSHEET_NO = '" + pram1.get(5) + "'"; //
                Statement sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();
            }
            else {
                // 新增到装车实绩表
                pstaLoadM.setString(1, tallNo);
                pstaLoadM.setString(2, pram1.get(4));
                pstaLoadM.setString(3, pram1.get(5));
                pstaLoadM.setString(4, lvMat.getLoadtype());
                pstaLoadM.setString(5, pram1.get(8));
                pstaLoadM.setString(6, lvMat.getStrTranstpe());
                pstaLoadM.setString(7, lvMat.getStrReceivno());
                pstaLoadM.setString(8, lvMat.getStrReceivnm());
                pstaLoadM.setString(9, lvMat.getStrStationno());
                pstaLoadM.setString(10, lvMat.getStrStationnm());
                pstaLoadM.setString(11, lvMat.getStrSpclno());
                pstaLoadM.setString(12, lvMat.getStrSpclname());
                pstaLoadM.setString(13, lvMat.getStrShipno());
                pstaLoadM.setString(14, lvMat.getStrPlant());
                pstaLoadM.setString(15, lvMat.getStrPlantname());
                pstaLoadM.setString(16, lvMat.getStrVstel());
                pstaLoadM.setString(17, lvMat.getStrVsteldesc());
                pstaLoadM.setString(18, pram1.get(0));
                pstaLoadM.setString(19, pram1.get(1));
                pstaLoadM.setString(20, df.format(strWeight));
                pstaLoadM.setInt(21, strcount);
                pstaLoadM.setString(22, dflen.format(strLen));
                pstaLoadM.setString(23, lvMat.getStrDelivycode());
                pstaLoadM.setString(24, lvMat.getStrDelivyname());
                pstaLoadM.setString(25, lvMat.getStrUnitno());
                pstaLoadM.setString(26, lvMat.getStrUnitname());
                pstaLoadM.setString(27, pram1.get(13));
                pstaLoadM.setString(28, pram1.get(14));
                pstaLoadM.setString(29, pram1.get(9));
                pstaLoadM.setString(30, pram1.get(12));
                pstaLoadM.setString(31, pram1.get(12));
                pstaLoadM.setString(32, lvMat.getStorage());
                pstaLoadM.setString(33, pram1.get(15));
                pstaLoadM.setString(34, pram1.get(16));
                pstaLoadM.setString(35, "0");
                pstaLoadM.setString(36, "0");
                pstaLoadM.setString(37, lvMat.getPrdCls());

                pstaLoadM.setString(38,craneList.get(0));
                pstaLoadM.setString(39,craneList.get(1));
                pstaLoadM.setString(40,craneList.get(2));
                pstaLoadM.setString(41,craneList.get(3));
                pstaLoadM.setString(42,craneList.get(4));
                pstaLoadM.addBatch();
            }
            rs2.close();
            psta1.close();

            // 查询理货主表对应的子表信息是否全部装车完毕
            sqlquery = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.QueryBillC");
            psta = connection.prepareStatement(sqlquery);
            psta.setString(1, pram1.get(4));
            psta.setString(2, pram1.get(5));
            ResultSet rs3 = psta.executeQuery();
            if (!rs3.next()) {
                pstaTallM1.setObject(1, pram1.get(9));
                pstaTallM1.setObject(2, pram1.get(4));
                pstaTallM1.setObject(3, pram1.get(5));
                pstaTallM1.addBatch();
            }
            else {
                pstaTallM2.setObject(1, pram1.get(9));
                pstaTallM2.setObject(2, pram1.get(4));
                pstaTallM2.setObject(3, pram1.get(5));
                pstaTallM2.addBatch();
            }
            rs3.close();
            psta.close();

            cro.setResult(1);
            pstaLoadM.executeBatch();
            pstaTallM1.executeBatch();
            pstaTallM2.executeBatch();
            cro.setV_errMsg("装车成功！");
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            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 queryOutList1(ArrayList<String> list) {
    // return this.getDataBySqlID("FrmPipeEntruck.QueryOutList1", list
    // .toArray());
    // }

    /**
     * 撤销
     * @param pram 修改参数
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject upDateInfo(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Statement sta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            // 数组长度等于装车支数和
            String sqlquery = "";
            String sqlupdate = "";
//            String updateTallC1 = CoreXmlSqlParsersFactory
//                    .getSql("FrmPipeEntruck.updateTallC1"); // 修改理货材料表装车标志
//            PreparedStatement pstatallC1 = connection
//                    .prepareStatement(updateTallC1);
//            String updateBillC1 = CoreXmlSqlParsersFactory
//                    .getSql("FrmPipeEntruck.updateBillC1"); // 修改提单材料表装车标志
//            PreparedStatement pstabillC1 = connection
//                    .prepareStatement(updateBillC1);
            String updateSql = CoreXmlSqlParsersFactory.getSql("FrmPipeEntruck.cancelLoadVie");
            PreparedStatement ps = connection.prepareStatement(updateSql);
            ArrayList<String> billMats = new ArrayList<String>(); // 保存材料表主键
            List<YdmZcLoadvehicleMatEntity> tdmMatLoad = new ArrayList<YdmZcLoadvehicleMatEntity>();
            for (int i = 0; i < pram.size(); i++) {
                tdmMatLoad.clear();
                billMats.clear();

                ArrayList<String> list = (ArrayList<String>) pram.get(i);

                // // 按炉批行和提单号查询装车材料主键
                // sqlquery = CoreXmlSqlParsersFactory
                // .getSql("FrmPipeEntruck.queryLoadvehMat");
                // psta = connection.prepareStatement(sqlquery);
                // psta.setString(1, list.get(0));
                // psta.setString(2, list.get(1));
                // psta.setString(3, list.get(2));
                // psta.setString(4, list.get(3));
                // ResultSet rs = psta.executeQuery();
                // while (rs.next()) {
                // billMats.add(rs.getString(2));
                // }
                // rs.close();
                // psta.close();

//                sqlquery = CoreXmlSqlParsersFactory
//                        .getSql("FrmPipeEntruck.queryLoadvehMatCount");
//                psta = connection.prepareStatement(sqlquery);
//                psta.setString(1, list.get(0));
//                psta.setString(2, list.get(1));
//                psta.setString(3, list.get(2));
//                psta.setString(4, list.get(3));
//                ResultSet rs1 = psta.executeQuery();
//                while (rs1.next()) {
//                    int billcount = 0;// 支数
//                    double billWeight = 0;// 重量
//                    double billLen = 0;// 长度
//                    DecimalFormat df = new DecimalFormat("0.000");
//                    DecimalFormat dflen = new DecimalFormat("0.00");
//                    billcount = rs1.getInt(4);
//                    billWeight = Double.parseDouble(rs1.getString(5));
//                    billLen = Double.parseDouble(rs1.getString(6));
//                    String ordesqeno = rs1.getString(3);

//                    pstabillM.setObject(1, billcount);
//                    pstabillM.setObject(2, df.format(billWeight));
//                    pstabillM.setObject(3, dflen.format(billLen));
//                    pstabillM.setObject(4, list.get(1));
//                    pstabillM.setObject(5, ordesqeno);
//                    pstabillM.addBatch();

//                }
//                rs1.close();
//                psta.close();

//                pstatallC1.setObject(1, list.get(0));
//                pstatallC1.setObject(2, list.get(1));
//                pstatallC1.setObject(3, list.get(2));
//                pstatallC1.setObject(4, list.get(3));
//                pstatallC1.addBatch();
//
//                pstabillC1.setObject(1, list.get(0));
//                pstabillC1.setObject(2, list.get(1));
//                pstabillC1.setObject(3, list.get(2));
//                pstabillC1.setObject(4, list.get(3));
//                pstabillC1.addBatch();
//
//                pstatallC1.executeBatch();
//                pstabillC1.executeBatch();
                ps.setString(1,list.get(1));
                ps.setString(2,list.get(2));
                ps.setString(3,list.get(0));
                ps.setString(4,list.get(3));
                ps.executeUpdate();

                sqlupdate = "DELETE YDM_ZC_LOADVEHICLE_MAT  WHERE LOADVEHICLE_NO = '"
                        + list.get(0)
                        + "' AND BILL_NO = '"
                        + list.get(1)
                        + "' AND TALLYSHEET_NO = '"
                        + list.get(2)
                        + "' AND VEHICLE_NO = '" + list.get(3) + "'"; // 删除装车材料表
                sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();

                sqlupdate = "DELETE YDM_ZC_LOADVEHICLE  WHERE LOADVEHICLE_NO ='"
                        + list.get(0) + "'";// 删除装车实绩表
                sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();

                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.QueryYDMzcLoad");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(1));
                psta.setString(2, list.get(2));
                ResultSet rs2 = psta.executeQuery();
                if (rs2.next()) {
                    sqlupdate = "UPDATE YDM_ZC_TALLYSHEET_M SET LOADVEHICLE_FLAG = '1',END_TIME=''"
                            + " WHERE  BILL_NO="
                            + "'"
                            + list.get(1)
                            + "'"
                            + " AND TALLYSHEET_NO="
                            + "'"
                            + list.get(2)
                            + "'"
                            + ""; // 修改理货主表装车标志
                    sta = connection.createStatement();
                    sta.executeUpdate(sqlupdate);
                    sta.close();
                }
                else {
                    sqlupdate = "UPDATE YDM_ZC_TALLYSHEET_M SET LOADVEHICLE_FLAG = '0',END_TIME=''"
                            + " WHERE  BILL_NO="
                            + "'"
                            + list.get(1)
                            + "'"
                            + " AND TALLYSHEET_NO="
                            + "'"
                            + list.get(2)
                            + "'"
                            + ""; // 修改理货主表装车标志
                    sta = connection.createStatement();
                    sta.executeUpdate(sqlupdate);
                    sta.close();
                }
                rs2.close();
                psta.close();
                cro.setV_errMsg("撤销装车！LOADVEHICLE_NO："+list.get(0)+" BILL_NO:"+list.get(1)+"TALLYSHEET_NO:"+list.get(2)
                		+"VEHICLE_NO:"+list.get(3)+"UserName"+list.get(4));
            }
            System.out.print(new Date());
//            pstabillM.executeBatch();

            System.out.print(new Date());
            cro.setV_errMsg("撤销成功！");
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /**
     * 理货单关闭
     * @param pram 修改参数
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject tallyClose(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Statement sta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            // 数组长度等于装车支数和
            String sqlquery = "";
            String sqlupdate = "";

            String deleteTallC = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.deleteTallC"); // 删除理货材料表
            PreparedStatement pstatallc = connection
                    .prepareStatement(deleteTallC);

            String updateBillC3 = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.updateBillC3"); // 修改提单材料表状态
            PreparedStatement pstabillC3 = connection
                    .prepareStatement(updateBillC3);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String tallNo = list.get(0);
                String billNo = list.get(1);
                String judgeNo = list.get(2);
                String batchNO = list.get(3);
                String groupNo = list.get(4);
                // String ordPkNo = list.get(5);
                // String ordSenPkNo = list.get(6);
                String ordDelPkNo = list.get(7);

                int billcount = 0;// 支数
                double billWeight = 0;// 重量
                double billLen = 0;// 长度
                DecimalFormat df = new DecimalFormat("0.000");
                DecimalFormat dflen = new DecimalFormat("0.00");
                ArrayList<String> loadMats = new ArrayList<String>(); // 保存理货材料表主键
                ArrayList<String> tallMats = new ArrayList<String>(); // 保存理货材料表材料主键
                // 理货材料表材料，理货主键
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.queryTallyC");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, tallNo);
                psta.setObject(2, billNo);
                psta.setObject(3, judgeNo);
                psta.setObject(4, batchNO);
                psta.setObject(5, groupNo);
                psta.setObject(6, ordDelPkNo);
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    loadMats.add(rs.getString(1));
                    tallMats.add(rs.getString(2));
                    billcount += rs.getInt(3);
                    billWeight += Double.parseDouble(rs.getString(4));
                    billLen += Double.parseDouble(rs.getString(5));
                }
                rs.close();
                psta.close();

                // 理货是否未装车
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.queryTallyCLoadTypeALL");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, tallNo);
                psta.setObject(2, billNo);
                ResultSet rs2 = psta.executeQuery();
                if (rs2.next()) {
                    if (rs2.getInt(1) == 0) {
                        cro.setV_errMsg("该理货单未装车不能进行理货关闭,请到理货编制界面撤消理货信息！");
                        cro.setV_errCode(-1);
                        return cro;
                    }
                }
                rs2.close();
                psta.close();

                pstatallc.setObject(1, tallNo);
                pstatallc.setObject(2, billNo);
                pstatallc.setObject(3, judgeNo);
                pstatallc.setObject(4, batchNO);
                pstatallc.setObject(5, groupNo);
                pstatallc.setObject(6, ordDelPkNo);
                pstatallc.addBatch();

                sqlupdate = "UPDATE YDM_ZC_TALLYSHEET_M  SET STACKING_WT = TO_NUMBER(NVL(STACKING_WT,0)) - TO_NUMBER('"
                        + df.format(billWeight)
                        + "'),"
                        + "STACKING_PIECE    = TO_NUMBER(NVL(STACKING_PIECE,0)) - TO_NUMBER('"
                        + billcount
                        + "'),"
                        + "STACKING_TOTL_LEN = TO_NUMBER(NVL(STACKING_TOTL_LEN,0)) - TO_NUMBER('"
                        + dflen.format(billLen)
                        + "')"
                        + "WHERE BILL_NO = '"
                        + billNo
                        + "'   AND TALLYSHEET_NO = '"
                        + tallNo
                        + "'  AND ORD_LN_DLY_PK = '" + ordDelPkNo + "'"; // 释放理货主表量
                sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();

                // 理货是否还有未装车数据
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.queryTallyCLoadType");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, tallNo);
                psta.setObject(2, billNo);
                ResultSet rs1 = psta.executeQuery();
                if (rs1.next()) {
                    if (!rs1.getString(1).equals("0")) {
                        sqlupdate = "UPDATE YDM_ZC_TALLYSHEET_M  SET LOADVEHICLE_FLAG='2',END_TIME=SYSDATE WHERE BILL_NO = '"
                                + billNo
                                + "' AND TALLYSHEET_NO = '"
                                + tallNo
                                + "'"; // 修改理货单主表装车状态（理货关闭）
                        sta = connection.createStatement();
                        sta.executeUpdate(sqlupdate);
                        sta.close();
                    }
                }
                rs1.close();
                psta.close();

                pstabillC3.setObject(1, billNo);
                pstabillC3.setObject(2, ordDelPkNo);
                pstabillC3.setObject(3, judgeNo);
                pstabillC3.setObject(4, batchNO);
                pstabillC3.setObject(5, groupNo);
                pstabillC3.addBatch();
            }
            cro.setResult(1);
            pstatallc.executeBatch();
            pstabillC3.executeBatch();
            cro.setV_errMsg("关闭成功！");
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            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
     */
    public CoreReturnObject addTallCar(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlquery = "";

            String insterCar = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.addTallCar"); // 新增车号
            PreparedStatement pstaCar = connection.prepareStatement(insterCar);

            String updateAskDay60 = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateAskDay60");// 修改日请车使用数
            PreparedStatement updateDay60 = connection
                    .prepareStatement(updateAskDay60);
            String updateAskDay70 = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateAskDay70");// 修改日请车使用数
            PreparedStatement updateDay70 = connection
                    .prepareStatement(updateAskDay70);

            String tallNo = list.get(1);
            String billNo = list.get(0);
            String car = list.get(2);
            String carrValuse = list.get(3);
            String carrtext = list.get(4);
            String sendPlan = list.get(5);
            String CarType = list.get(6);

            // 查询是否有相同车号（同一提单同一理货单下）
            String strCar = "";
            sqlquery = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.QueryTallCar");
            psta = connection.prepareStatement(sqlquery);
            psta.setObject(1, tallNo);
            psta.setObject(2, car);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                strCar = rs.getString("VEHICLE_NO");
            }
            rs.close();
            psta.close();

            if (!strCar.equals("")) {
                cro.setV_errMsg("同一提单同一理货单下不能有重复车号,请重新输入车号！");
                cro.setV_errCode(-1);
                return cro;
            }

            if (!CarType.equals("")) {
                // 查询请车数
                double vday = 0;
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.QueryTallDay");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, tallNo);
                psta.setObject(2, sendPlan);
                rs = psta.executeQuery();
                if (rs.next()) {
                    vday = Double.parseDouble(rs.getString("VADAY"));
                }
                rs.close();
                psta.close();
                if (vday + 1 > Double.parseDouble(list.get(7))) {
                    cro.setV_errMsg("输入的车皮数超过日请车批复数！");
                    cro.setV_errCode(-1);
                    return cro;
                }
            }
            pstaCar.setObject(1, billNo);
            pstaCar.setObject(2, tallNo);
            pstaCar.setObject(3, car);
            pstaCar.setObject(4, carrValuse);
            pstaCar.setObject(5, carrtext);
            pstaCar.setObject(6, sendPlan);
            pstaCar.setObject(7, CarType);
            pstaCar.addBatch();
            pstaCar.executeBatch();

            if (CarType.equals("60吨车皮")) {
                updateDay60.setObject(1, sendPlan);
                updateDay60.addBatch();
                updateDay60.executeBatch();
            }
            else if (CarType.equals("70吨车皮")) {
                updateDay70.setObject(1, sendPlan);
                updateDay70.addBatch();
                updateDay70.executeBatch();
            }
            cro.setResult(1);
            cro.setV_errMsg("追加车号成功！");
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;

    }

}
