package com.steering.pss.ydm.acasleavefactory;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
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.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.entity.YdmZcBillCEntity;
import com.steering.pss.ydm.entity.YdmZcBillMEntity;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 坯子直接出库（针对腾飞库，安利库，宝仓库，润德库直接出库）
 * @author Administrator TJG 2015-03-10
 */
public class FrmCasOutAutomatic extends DBCommon {

    /**
     * 每个装车单号对应的装车序号
     * @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 {
        int seq = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipeEntruck.QuerySendList");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strSend);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strSend + "01";
            }
            else {
                seq = Integer.parseInt(res.substring(res.length() - 2, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return strSend + StringTool.formatDecimal(++seq, "00");
    }

    /**
     * 每支材料对应的倒库（出库）单号
     * @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");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmCasOutAutomatic.queryOutList");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, matter.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("FrmCasOutAutomatic.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 con
     * @param sendPlanNo
     * @return
     * @throws SQLException
     */
    public static YdmZcBillMEntity getBillM(Connection con, String billNo,
            String sendPlanNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCasOutAutomatic.getqueryBillM");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, billNo);
        ps.setObject(2, sendPlanNo);
        ResultSet rs = ps.executeQuery();
        List<YdmZcBillMEntity> ydmbillM = EntityTool.getEntitysByRs(rs,
                YdmZcBillMEntity.class);
        rs.close();
        ps.close();
        if (ydmbillM == null || ydmbillM.size() == 0) {
            return null;
        }
        else {
            for (YdmZcBillMEntity ydmZcBillMEntity : ydmbillM) {
                QualityDesignTool.nullConverNullString(ydmZcBillMEntity);
            }
            return ydmbillM.get(0);
        }
    }

    /**
     * 自动生成理货单号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendTally(Connection conn, String strSendList,
            String storage) throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmTallYsheet.QuerySendList");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, storage);
        psta.setString(2, matter1.format(dt));
        psta.setString(3, matter.format(dt));
        psta.setString(4, storage);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString(1);
        }
        rs.close();
        psta.close();
        return seq;
    }

    /**
     * 查询提单从表信息
     * @param con 连接字符串
     * @param matInfoEntity 资源类
     * @return List<MatZcMInfoEntity>
     * @throws SQLException
     */
    public static List<YdmZcBillCEntity> getBillC(Connection con, String billNo)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePipeCarbill.getInventoryCanInfo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, billNo);
        ResultSet rs = ps.executeQuery();
        List<YdmZcBillCEntity> ydmBillc = EntityTool.getEntitysByRs(rs,
                YdmZcBillCEntity.class);
        ps.close();
        rs.close();
        if (ydmBillc == null || ydmBillc.size() == 0) {
            return null;
        }
        else {
            // for (YdmZcBillCEntity matBillC : ydmBillc) {
            // QualityDesignTool.nullConverNullString(matBillC);
            // }
            return ydmBillc;
        }
    }

    /**
     * 撤销装车
     * @param pram 装车材料表参数
     * @param pram1 装车主表参数
     * @param strSend 理货单号
     * @return obj
     */
    public CoreReturnObject deleteTallyInfo(ArrayList<String[]> pram,
            String Name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date nowTime = new Date();
        SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strNowTime = dfT.format(nowTime);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlquery = "";

            String insterButtr = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.AddButtress"); // 新增位置表
            PreparedStatement pstaButtr = connection
                    .prepareStatement(insterButtr);

            String updateMatNo1 = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.updateMatNo1"); // 修改材料表材料状态
            PreparedStatement pstaMatNo1 = connection
                    .prepareStatement(updateMatNo1);
            String updatebillM = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.updateBillM1"); // 修改提单主表材料状态
            PreparedStatement pstabillM = connection
                    .prepareStatement(updatebillM);
            String updateBillC1 = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.updateBillC1"); // 修改提单材料表装车标志
            PreparedStatement pstabillC1 = connection
                    .prepareStatement(updateBillC1);
            String deleteOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.deleteOut"); // 删除出库表销售出库对应记录
            PreparedStatement pstaOut = connection.prepareStatement(deleteOut);

            List<YdmZcBillCEntity> ydmBillc = new ArrayList<YdmZcBillCEntity>();
            for (String[] billNos : pram) {
                ydmBillc.clear();
                String billNo = billNos[0];
                String managNo = billNos[2];
                String managName = billNos[3];
                String depid = billNos[4];
                String depName = billNos[5];

                // 判断可撤销提单

                String stata = "";
                String zoutFalg = "";
                String billTic = "";
                String outTic = "";
                String traTic = "";
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasOutAutomatic.QueryBillMNo");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                ResultSet rs1 = psta.executeQuery();
                if (rs1.next()) {
                    stata = rs1.getString("BILL_STATUS");
                    billTic = rs1.getString("BILL_TICKET_STATUS");
                    outTic = rs1.getString("OUTSTOCK_TICKET_STATUS");
                    traTic = rs1.getString("TRANSMNY_TICKET_STATUS");
                    zoutFalg = rs1.getString("ZOUT_FLAG");
                }
                rs1.close();
                psta.close();

                if (zoutFalg.equals("0")) {
                    cro.setV_errMsg("选择的提单不是直接出库提单，不能撤销！");
                    return cro;
                }
                if (Integer.parseInt(stata) >= 4) {
                    cro.setV_errMsg("提单不是执行状态,不能撤销！");
                    return cro;
                }
                if (!billTic.equals("0") && !outTic.equals("0")
                        && !traTic.equals("0")) {
                    cro.setV_errMsg("提单货款对齐,出库费,运费有未撤销,不能撤销！");
                    return cro;
                }
                // 查询提单从表材料号
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasOutAutomatic.QueryAddSendMat");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                rs1 = psta.executeQuery();
                ydmBillc = EntityTool.getEntitysByRs(rs1,
                        YdmZcBillCEntity.class);
                rs1.close();
                psta.close();
                for (YdmZcBillCEntity ydmBills : ydmBillc) {
                    String layMax = "";
                    String colMax = "";
                    String attrNo = "";
                    String attraName = "";
                    String typeNo = "";
                    String typeName = "";
                    sqlquery = CoreXmlSqlParsersFactory
                            .getSql("FrmPipeEntruck.queryTager");// 查询要新增的目标库号信息
                    PreparedStatement psta3 = connection
                            .prepareStatement(sqlquery);
                    psta3.setString(1, ydmBills.getStorageNo());
                    ResultSet rsTag = psta3.executeQuery();
                    if (rsTag.next()) {
                        layMax = rsTag.getString(2);
                        colMax = rsTag.getString(3);
                        attrNo = rsTag.getString(4);
                        attraName = rsTag.getString(5);
                        typeNo = rsTag.getString(6);
                        typeName = rsTag.getString(7);
                    }
                    rsTag.close();
                    psta3.close();

                    pstaButtr.setString(1, ydmBills.getMatNo());
                    pstaButtr.setString(2, ydmBills.getJudgeStoveNo());
                    pstaButtr.setString(3, ydmBills.getBatchNo());
                    pstaButtr.setString(4, ydmBills.getGroupNo());
                    pstaButtr.setString(5, ydmBills.getStorageNo());
                    pstaButtr.setString(6, ydmBills.getLocationNo());
                    pstaButtr.setObject(7, layMax);
                    pstaButtr.setObject(8, colMax);
                    pstaButtr.setString(9, attrNo);
                    pstaButtr.setString(10, attraName);
                    pstaButtr.setString(11, typeNo);
                    pstaButtr.setString(12, typeName);
                    pstaButtr.setString(13, managNo);
                    pstaButtr.setString(14, managName);
                    pstaButtr.setString(15, depid);
                    pstaButtr.setString(16, depName);
                    pstaButtr.setString(17, Name);
                    pstaButtr.setString(18, strNowTime);
                    pstaButtr.addBatch();

                    // pstaMatNo1.setObject(1, ydmBills.getMatNo());
                    // pstaMatNo1.addBatch();

                }
                pstaMatNo1.setObject(1, billNo);
                pstaMatNo1.addBatch();

                pstaOut.setObject(1, billNo);
                pstaOut.addBatch();

                String sqlupdate = "DELETE YDM_ZC_LOADVEHICLE_MAT  WHERE  BILL_NO = '"
                        + billNo + "'"; // 删除装车材料表
                Statement sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();
                sqlupdate = "DELETE YDM_ZC_LOADVEHICLE  WHERE  BILL_NO = '"
                        + billNo + "'";// 删除装车实绩表
                sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();

                pstabillC1.setObject(1, billNo);
                pstabillC1.addBatch();

                pstabillM.setObject(1, billNo);
                pstabillM.addBatch();
            }
            pstaButtr.executeBatch();
            pstaMatNo1.executeBatch();
            pstabillC1.executeBatch();
            pstaOut.executeBatch();
            pstabillM.executeBatch();

            cro.setResult(1);
            cro.setV_errMsg("撤销出库成功!");
            connection.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            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 billNo 提单号
     * @return CoreReturnObject
     */
    public CoreReturnObject queryBillC(String billNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCasOutAutomatic.QueryBillC");
        return this.getData(sql, new Object[] { billNo });
    }

    /**
     * 新增装车
     * @param pram 装车材料表参数
     * @param pram1 装车主表参数
     * @param strSend 理货单号
     * @return obj
     */
    public CoreReturnObject addTallyInfo(ArrayList<String[]> pram, String Name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date nowTime = new Date();
        SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strNowTime = dfT.format(nowTime);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String jsTime = df.format(nowTime);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            // 数组长度等于装车支数和
            String sqlquery = "";
            String insertLoadC = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.AddLoadMat"); // 新增到装车材料表
            PreparedStatement pstaLoadC = connection
                    .prepareStatement(insertLoadC);
            String insertLoadM = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeEntruck.AddLoadPerf"); // 新增到装车实绩表
            PreparedStatement pstaLoadM = connection
                    .prepareStatement(insertLoadM);
            String insertOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.insertOutList"); // 新增到出库表
            PreparedStatement pstaOut = connection.prepareStatement(insertOut);
            String deleteButtress = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.deleteButtress"); // 删除位置表
            PreparedStatement pstaButtress = connection
                    .prepareStatement(deleteButtress);
            String updateBillC = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.updateBillC"); // 修改提单材料表装车状态
            PreparedStatement pstabillC = connection
                    .prepareStatement(updateBillC);
            String updateMatNo = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.updateMatNo"); // 修改材料表材料状态
            PreparedStatement pstaMatNo = connection
                    .prepareStatement(updateMatNo);
            String updateBillM = CoreXmlSqlParsersFactory
                    .getSql("FrmCasOutAutomatic.updateBillM"); // 修改提单主表状态
            PreparedStatement pstabillM = connection
                    .prepareStatement(updateBillM);
            String butessMatNo = "";
            String LoadNo = "";
            List<YdmZcBillCEntity> ydmBillc = new ArrayList<YdmZcBillCEntity>();
            for (String[] billNos : pram) {
                ydmBillc.clear();
                String billNo = billNos[0];
                String sendPlanNo = billNos[1];
                String managNo = billNos[2];
                String managName = billNos[3];
                String depid = billNos[4];
                String depName = billNos[5];
                String saleorg = billNos[6];
                String saleorgdesc = billNos[7];
                String tranType = billNos[8];
                String receivNo = billNos[9];
                String receivNm = billNos[10];
                // String stlNuitNo = billNos[11];
                // String stlNuitNm = billNos[12];
                String statioNo = billNos[13];
                String statioNm = billNos[14];
                String spclLnNo = billNos[15];
                String spclLnNm = billNos[16];
                String bc = billNos[18];
                String bz = billNos[17];

                String storsge = "";// 出库地点
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasOutAutomatic.QueryBillC");// 查询提单从表信息
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, billNo);
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    storsge = rs.getString("STORAGE_NO");
                }
                rs.close();
                psta.close();

                String dumyFlag = "";// 是否虚拟库
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasOutAutomatic.QueryStorageNo");// 查询仓库是否虚拟库
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, storsge);
                rs = psta.executeQuery();
                if (rs.next()) {
                    dumyFlag = rs.getString("DUMMY_FLAG");
                }
                rs.close();
                psta.close();

                YdmZcBillMEntity ydmbillM = FrmCasOutAutomatic.getBillM(
                        connection, billNo, sendPlanNo);
                if (ydmbillM == null) {
                    cro.setV_errMsg("提单" + billNo + "信息已改变,请重新获取数据!");
                    return cro;
                }
                if (!ydmbillM.getBillStatus().equals("1")) {
                    cro.setV_errMsg("只有下发状态的提单可以直接出库！");
                    return cro;
                }
                if (ydmbillM.getLoadvehicleType().equals("2")) {
                    if (ydmbillM.getIspreclose().equals("0")) {
                        cro.setV_errMsg("虚单未一次关闭,不能直接出库！");
                        return cro;
                    }
                }

                if (dumyFlag.equals("0")) {
                    if (!ydmbillM.getDelvryAddr().equals("客户库-腾飞")
                            && !ydmbillM.getDelvryAddr().equals("客户库-宝仓")
                            && !ydmbillM.getDelvryAddr().equals("客户库-润德")
                            && !ydmbillM.getDelvryAddr().equals("客户库-安利")
                            && !ydmbillM.getDelvryAddr().equals("客户库-思创")
                            && !ydmbillM.getDelvryAddr().equals("客户库-滨城")
                            && !storsge.equals("LSY1")) {
                        cro.setV_errMsg("该提单合同交货地址不是客户库且出库地点不是虚拟库,不能直接出库！");
                        return cro;
                    }
                }

                // 获取理货单号
                String tallNO = querySendTally(connection, billNo, storsge);
                int tallNo = Integer.parseInt(tallNO.substring(
                        tallNO.length() - 4, tallNO.length()));

                // 获取装车单号
                String strloadNo = querySendLoad(connection, tallNO.substring(
                        0, 12)
                        + StringTool.formatDecimal(tallNo, "0000"));

                pstaLoadM.setString(1, strloadNo);
                pstaLoadM.setString(2, billNo);
                pstaLoadM.setString(3, tallNO);
                pstaLoadM.setString(4, ydmbillM.getLoadvehicleType());
                pstaLoadM.setString(5, "*");
                pstaLoadM.setString(6, ydmbillM.getPlanTransType());
                pstaLoadM.setString(7, ydmbillM.getReceivNo());
                pstaLoadM.setString(8, ydmbillM.getReceivNm());
                pstaLoadM.setString(9, ydmbillM.getStationNo());
                pstaLoadM.setString(10, ydmbillM.getStationNm());
                pstaLoadM.setString(11, ydmbillM.getSpclLnNo());
                pstaLoadM.setString(12, ydmbillM.getSpclLnNm());
                pstaLoadM.setString(13, "");
                pstaLoadM.setString(14, "");
                pstaLoadM.setString(15, "");
                pstaLoadM.setString(16, ydmbillM.getVstelCode());
                pstaLoadM.setString(17, ydmbillM.getVstelDesc());
                pstaLoadM.setString(18, "");
                pstaLoadM.setString(19, "");
                pstaLoadM.setString(20, String.valueOf(ydmbillM.getPlanWt()));
                pstaLoadM.setString(21, String.valueOf(ydmbillM.getPlanNum()));
                pstaLoadM.setString(22, String.valueOf(ydmbillM
                        .getPlanTotlLen()));
                pstaLoadM.setString(23, "");
                pstaLoadM.setString(24, "");
                pstaLoadM.setString(25, ydmbillM.getStlmtUnitNo());
                pstaLoadM.setString(26, ydmbillM.getStlmtUnitNm());
                pstaLoadM.setString(27, managNo);
                pstaLoadM.setString(28, managName);
                pstaLoadM.setString(29, Name);
                pstaLoadM.setString(30, strNowTime);
                pstaLoadM.setString(31, strNowTime);
                pstaLoadM.setString(32, storsge);
                pstaLoadM.setString(33, depid);
                pstaLoadM.setString(34, depName);
                pstaLoadM.setString(35, "1");
                pstaLoadM.setString(36, "1");
                pstaLoadM.setString(37, ydmbillM.getPrdCls());
                pstaLoadM.addBatch();
                tallNo++;
                pstaLoadM.executeBatch();
                // 查询提单从表材料号
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasOutAutomatic.QueryAddSendMat");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                ResultSet rs1 = psta.executeQuery();
                ydmBillc = EntityTool.getEntitysByRs(rs1,
                        YdmZcBillCEntity.class);
                rs1.close();
                psta.close();

                String sendOut = querySendOut(connection, "800805");
                // 获取出库流水号
                butessMatNo = querySendOutList(connection, sendOut, "800805");
                int outNo = Integer.parseInt(butessMatNo.substring(butessMatNo
                        .length() - 4, butessMatNo.length()));

                // 获取装车序号
                LoadNo = querySendSl(connection, strloadNo);
                int loadNo = Integer.parseInt(LoadNo.substring(
                        LoadNo.length() - 6, LoadNo.length()));

                String layMax = "";
                String colMax = "";
                String attrNo = "";
                String attraName = "";
                String typeNo = "";
                String typeName = "";
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeEntruck.queryTager");// 查询要新增的目标库号信息
                PreparedStatement psta3 = connection.prepareStatement(sqlquery);
                psta3.setString(1, storsge);
                ResultSet rsTag = psta3.executeQuery();
                if (rsTag.next()) {
                    layMax = rsTag.getString(2);
                    colMax = rsTag.getString(3);
                    attrNo = rsTag.getString(4);
                    attraName = rsTag.getString(5);
                    typeNo = rsTag.getString(6);
                    typeName = rsTag.getString(7);
                }
                rsTag.close();
                psta3.close();

                for (YdmZcBillCEntity ydmBills : ydmBillc) {
                    if (ydmBills.getPrtFlag().equals("1")) {
                        cro.setV_errMsg("提单有过磅，不能直接出库！");
                        return cro;
                    }

                    // 新增到装车材料表
                    pstaLoadC.setString(1, strloadNo
                            + StringTool.formatDecimal(loadNo, "000000"));
                    pstaLoadC.setString(2, strloadNo);
                    pstaLoadC.setString(3, billNo);
                    pstaLoadC.setString(4, tallNO);
                    pstaLoadC.setString(5, "*");
                    pstaLoadC.setString(6, ydmBills.getOrderNo());
                    pstaLoadC.setString(7, ydmBills.getOrderSeq());
                    pstaLoadC.setString(8, ydmBills.getDeliveryNo());
                    pstaLoadC.setString(9, "");
                    pstaLoadC.setString(10, ydmBills.getMatNo());
                    pstaLoadC.setString(11, ydmBills.getLotNo());
                    pstaLoadC.setString(12, ydmBills.getJudgeStoveNo());
                    pstaLoadC.setString(13, ydmBills.getBatchNo());
                    pstaLoadC.setString(14, ydmBills.getGroupNo());
                    pstaLoadC.setString(15, ydmBills.getStoveNo());
                    pstaLoadC.setString(16, ydmBills.getMaterialNo());
                    pstaLoadC.setString(17, ydmBills.getMaterialName());
                    pstaLoadC.setString(18, ydmBills.getProduccode());
                    pstaLoadC.setString(19, ydmBills.getProducname());
                    pstaLoadC.setString(20, ydmBills.getStdCode());
                    pstaLoadC.setString(21, ydmBills.getStdName());
                    pstaLoadC.setString(22, ydmBills.getGradecode());
                    pstaLoadC.setString(23, ydmBills.getGradename());
                    pstaLoadC.setString(24, ydmBills.getSteelcode());
                    pstaLoadC.setString(25, ydmBills.getSteelname());
                    pstaLoadC.setString(26, ydmBills.getStdStyle());
                    pstaLoadC.setString(27, ydmBills.getStdStyleDesc());
                    pstaLoadC.setString(28, ydmBills.getSpecCode());
                    pstaLoadC.setString(29, ydmBills.getSpecName());
                    pstaLoadC.setString(30, ydmBills.getModelCode());
                    pstaLoadC.setString(31, ydmBills.getModelDesc());
                    pstaLoadC.setString(32, "");
                    pstaLoadC.setString(33, "");
                    pstaLoadC.setString(34, ydmBills.getPlineCode());
                    pstaLoadC.setString(35, ydmBills.getPlineName());
                    pstaLoadC.setString(36, ydmBills.getActDimater());
                    pstaLoadC.setString(37, ydmBills.getActHeight());
                    pstaLoadC.setString(38, String
                            .valueOf(ydmBills.getActLen()));
                    pstaLoadC.setString(39, String.valueOf(ydmBills
                            .getActLenMin()));
                    pstaLoadC.setString(40, String.valueOf(ydmBills
                            .getActLenMax()));
                    pstaLoadC.setString(41, ydmBills.getActCount());
                    pstaLoadC.setString(42, String.valueOf(ydmBills
                            .getActWeight()));
                    pstaLoadC.setString(43, String.valueOf(ydmBills
                            .getActTheoryWeight()));
                    pstaLoadC.setString(44, ydmBills.getJudgeResultCode());
                    pstaLoadC.setString(45, ydmBills.getProBc());
                    pstaLoadC.setString(46, ydmBills.getProBz());
                    if (ydmBills.getRpoTime().equals("")) {
                        pstaLoadC.setString(47, "");
                    }
                    else {
                        pstaLoadC.setString(47, ydmBills.getRpoTime()
                                .substring(0, 19));
                    }
                    pstaLoadC.setString(48, bc);// 班次
                    pstaLoadC.setString(49, bz);// 班组
                    pstaLoadC.setString(50, saleorg);
                    pstaLoadC.setString(51, saleorgdesc);
                    pstaLoadC.setString(52, "");
                    pstaLoadC.setString(53, "");
                    pstaLoadC.setString(54, "");
                    pstaLoadC.setString(55, managNo);
                    pstaLoadC.setString(56, managName);
                    pstaLoadC.setString(57, ydmBills.getStorageNo());
                    pstaLoadC.setString(58, ydmBills.getLocationNo());
                    pstaLoadC.setString(59, Name);
                    pstaLoadC.setString(60, strNowTime);
                    pstaLoadC.setString(61, strNowTime);
                    pstaLoadC.setString(62, ydmBills.getOrdPk());
                    pstaLoadC.setString(63, ydmBills.getOrdLnPk());
                    pstaLoadC.setString(64, ydmBills.getOrdLnDlyPk());
                    pstaLoadC.setString(65, "");
                    pstaLoadC.setString(66, "");
                    pstaLoadC.setString(67, tranType);
                    pstaLoadC.setString(68, receivNo);
                    pstaLoadC.setString(69, receivNm);
                    pstaLoadC.setString(70, statioNo);
                    pstaLoadC.setString(71, statioNm);
                    pstaLoadC.setString(72, spclLnNo);
                    pstaLoadC.setString(73, spclLnNm);
                    pstaLoadC.setString(74, ydmBills.getPortNo());
                    pstaLoadC.setString(75, ydmBills.getVstel());
                    pstaLoadC.setString(76, ydmBills.getVstelDesc());
                    pstaLoadC.setString(77, "");
                    pstaLoadC.setString(78, "");
                    pstaLoadC.setString(79, depid);
                    pstaLoadC.setString(80, depName);
                    pstaLoadC.setString(81, "2");
                    pstaLoadC.setString(82,"120504".equals(ydmbillM.getSupplyUnit())? ComBaseInfo.GetBalYear_2416(nowTime): ComBaseInfo.GetBalYear(nowTime));
                    pstaLoadC.setString(83, ydmBills.getPortDesc());
                    pstaLoadC.setString(84, ydmBills.getCreditNo());
                    pstaLoadC.setString(85, String.valueOf(ydmBills
                            .getSendNum()));
                    pstaLoadC.setString(86, String.valueOf(ydmBills
                            .getActFixNum()));
                    pstaLoadC.setString(87, String.valueOf(ydmBills
                            .getActFixLen()));
                    pstaLoadC.addBatch();
                    loadNo++;

                    // 新增出库表
                    pstaOut.setString(1, sendOut
                            + StringTool.formatDecimal(outNo, "0000"));
                    pstaOut.setString(2, sendOut);
                    pstaOut.setString(3, "800805");
                    pstaOut.setString(4, "销售出库");
                    pstaOut.setString(5, "*");
                    pstaOut.setString(6, ydmBills.getMatNo());
                    pstaOut.setString(7, ydmBills.getLotNo());
                    pstaOut.setString(8, ydmBills.getStoveNo());
                    pstaOut.setString(9, ydmBills.getJudgeStoveNo());
                    pstaOut.setString(10, ydmBills.getBatchNo());
                    pstaOut.setString(11, ydmBills.getGroupNo());
                    pstaOut.setString(12, ydmBills.getMaterialNo());
                    pstaOut.setString(13, ydmBills.getMaterialName());
                    pstaOut.setString(14, ydmBills.getProduccode());
                    pstaOut.setString(15, ydmBills.getProducname());
                    pstaOut.setString(16, ydmBills.getStdCode());
                    pstaOut.setString(17, ydmBills.getStdName());
                    pstaOut.setString(18, ydmBills.getGradecode());
                    pstaOut.setString(19, ydmBills.getGradename());
                    pstaOut.setString(20, ydmBills.getSpecCode());
                    pstaOut.setString(21, ydmBills.getSpecName());
                    pstaOut.setString(22, ydmBills.getPlineCode());
                    pstaOut.setString(23, ydmBills.getPlineName());
                    pstaOut.setString(24, ydmBills.getActDimater());
                    pstaOut.setString(25, ydmBills.getActHeight());
                    pstaOut.setString(26, String.valueOf(ydmBills.getActLen()));
                    pstaOut.setString(27, String.valueOf(ydmBills
                            .getActLenMin()));
                    pstaOut.setString(28, String.valueOf(ydmBills
                            .getActLenMax()));
                    pstaOut.setString(29, ydmBills.getActCount());
                    pstaOut.setString(30, String.valueOf(ydmBills
                            .getActWeight()));
                    pstaOut.setString(31, String.valueOf(ydmBills
                            .getActTheoryWeight()));
                    pstaOut.setString(32, ydmBills.getStorageNo());
                    pstaOut.setString(33, ydmBills.getLocationNo());
                    pstaOut.setString(34, layMax);
                    pstaOut.setString(35, colMax);
                    pstaOut.setString(36, attrNo);
                    pstaOut.setString(37, attraName);
                    pstaOut.setString(38, typeNo);
                    pstaOut.setString(39, typeName);
                    pstaOut.setString(40, managNo);
                    pstaOut.setString(41, managName);
                    if (ydmBills.getRpoTime().equals("")) {
                        pstaOut.setString(42, "");
                    }
                    else {
                        pstaOut.setString(42, ydmBills.getRpoTime().substring(
                                0, 19));
                    }
                    pstaOut.setString(43, ydmBills.getProBc());
                    pstaOut.setString(44, ydmBills.getProBz());
                    pstaOut.setString(45, strNowTime);
                    pstaOut.setString(46, bc);// 班次
                    pstaOut.setString(47, bz);// 班组
                    pstaOut.setString(48, Name);
                    pstaOut.setString(49, strNowTime);
                    pstaOut.setString(50, ydmBills.getOrdPk());
                    pstaOut.setString(51, ydmBills.getOrdLnPk());
                    pstaOut.setString(52, ydmBills.getOrdLnDlyPk());
                    pstaOut.setString(53, ydmBills.getOrderNo());
                    pstaOut.setString(54, ydmBills.getOrderSeq());
                    pstaOut.setString(55, ydmBills.getDeliveryNo());
                    pstaOut.setString(56, depid);
                    pstaOut.setString(57, depName);
                    pstaOut.setString(58, saleorg);
                    pstaOut.setString(59, saleorgdesc);
                    pstaOut.setString(60, jsTime);
                    pstaOut.setString(61, String.valueOf(ydmBills
                            .getActFixNum()));
                    pstaOut.setString(62, String.valueOf(ydmBills
                            .getActFixLen()));
                    pstaOut.setString(63, String.valueOf(ydmBills
                            .getSendNum()));
                    pstaOut.setString(64, ydmBills.getVstel());
                    pstaOut.setString(65, ydmBills.getVstelDesc());
                    pstaOut.addBatch();
                    outNo++;

                    pstabillM.setObject(1, ydmBills.getActCount());
                    pstabillM.setObject(2, ydmBills.getActWeight());
                    pstabillM.setObject(3, ydmBills.getActLen());
                    pstabillM.setObject(4, billNo);
                    pstabillM.setObject(5, ydmBills.getOrdLnDlyPk());
                    pstabillM.addBatch();
                }
                pstaLoadC.executeBatch();
                pstaOut.executeBatch();
                pstabillM.executeBatch();

                pstaMatNo.setObject(1, billNo);
                pstaMatNo.addBatch();

                pstaButtress.setObject(1, billNo);
                pstaButtress.addBatch();

                pstabillC.setObject(1, billNo);
                pstabillC.addBatch();

            }
            pstabillC.executeBatch();
            pstaMatNo.executeBatch();
            pstaButtress.executeBatch();
            cro.setResult(1);
            cro.setV_errMsg("出库成功!");
            connection.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            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;
    }
}
