package com.steering.pss.ydm.acasleavefactory;

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

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.BillOrderInfoEntity;
import com.steering.pss.ydm.entity.PipeMatGpMEntity;
import com.steering.pss.ydm.entity.MatGpMEntity1;
import com.steering.pss.ydm.entity.YdmZcBillMEntity;
import com.steering.pss.ydm.entity.YdmZcSendplanCEntity;
import com.steering.pss.ydm.entity.YdmZcSendplanMEntity;
import com.steering.pss.ydm.pipeleavefactory.CorePipeCarBill;

/**
 * 汽运提单编制(坯子)
 * @author Administrator
 */
public class CoreCasCarBill extends DBCommon {

    /**
     * 查询仓库可编制信息
     * @param ordPk 合同主键
     * @param ordLnPk 行主键
     * @param ordLnDlyPK 交货行主键
     * @return CoreReturnObject
     */
    public CoreReturnObject queryPipeMat(String ordLnDlyPK) {
        Object[] params = new Object[] { ordLnDlyPK };
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.queryPipeMat");
        return this.getData(sql, params);
    }

    /**
     * 验证重量
     * @param matInfoEntity
     * @return
     */
    public CoreReturnObject checkMatWeight(String strMatNo) {
        JSONObject json = JSONObject.fromObject(strMatNo);
        PipeMatGpMEntity matInfoEntity = (PipeMatGpMEntity) JSONObject.toBean(
                json, PipeMatGpMEntity.class);
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.checkMatWeight");
        sql = EntityTool.fillSqlByEntity(sql, matInfoEntity);
        Object[] param = new Object[] { Integer.parseInt(matInfoEntity
                .getActCount()) + 1 };
        return this.getData(sql, param);
    }

    /**
     * 查询挂提单资源量
     * @param con
     * @param matInfoEntity
     * @return
     * @throws SQLException
     */
    public static double[] getInventoryCanWgt(Connection con,
            PipeMatGpMEntity matInfoEntity) throws SQLException {
        double[] arr = new double[3];
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.getInventoryCanWgt");
        sql = EntityTool.fillSqlByEntity(sql, matInfoEntity);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, Integer.parseInt(matInfoEntity.getActCount()) + 1);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            arr[0] = rs.getDouble("SUM_COUNT");
            arr[1] = rs.getDouble("SUM_WGT");
            arr[2] = rs.getDouble("SUM_LEN");
        }
        rs.close();
        ps.close();
        return arr;
    }

    /**
     * 查询合同信息对应材料表信息
     * @param list 合同数据
     * @return obj
     */
    public CoreReturnObject queryMatInfo(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.QueryMatInfo");
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询提单主表主键
     * @param list 参数
     * @return obj
     */
    public CoreReturnObject getBillNoSeq(ArrayList<String> list) {
        return this.getDataBySqlID("CoreCasCarBill.GetBillNoSeq", list
                .toArray());
    }

    /**
     * 查询提单材料表主键
     * @param list 参数
     * @return obj
     */
    public CoreReturnObject getBillSeq(ArrayList<String> list) {
        return this.getDataBySqlID("CoreCasCarBill.GetBillSeq", list.toArray());
    }

    /**
     * 查询提单材料表主键
     * @param list 参数
     * @return obj
     */
    public CoreReturnObject queryBillMat(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.QueryBillMat");
        return this.getData(sql, list.toArray());
    }

    /**
     * 废除提单信息
     * @param billSeqNO 提单主表数据
     * @param parm 提单材料数据
     * @param matNO 库存材料数据
     * @return obj
     */
    public CoreReturnObject updateBill(ArrayList<String> billNos,
            String userName, String deptId, String deptName) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "";
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            // 验证提单号
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.getBillInfoByBillNoSeq");
            for (String billNo : billNos) {
                ps = con.prepareStatement(sql);
                ps.setObject(1, billNo);
                rs = ps.executeQuery();
                List<YdmZcBillMEntity> billMEntitys = EntityTool
                        .getEntitysByRs(rs, YdmZcBillMEntity.class);
                rs.close();
                ps.close();
                if (billMEntitys == null || billMEntitys.size() == 0) {
                    core.setV_errMsg("提单信息发生变化,请重新获取数据!");
                    return core;
                }
                for (YdmZcBillMEntity billMEntity : billMEntitys) {
                    QualityDesignTool.nullConverNullString(billMEntity);
                    String sqlBillC = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.DeleteBillC");
                    String sqlBillM = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.DeleteBillM");
                    String sqlMat = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.DeleteMat");
                    String sqlSendM = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.DeleteSendM");
                    String sqlSendC = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.DeleteSendC");
                    String sqlBillNo = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.DeleteBillNo");
                    String sqlDele = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.DeleteTRANSIT1");
                    if (billMEntity.getBillStatus().equals("0")) {
                        ps = con.prepareStatement(sqlBillM);
                        ps.setObject(1, billNo);
                        ps.executeUpdate();
                        ps.close();

                        ps = con.prepareStatement(sqlMat);
                        ps.setObject(1, userName);
                        ps.setObject(2, billMEntity.getBillNo());
                        ps.setObject(3, billMEntity.getOrdLnDlyPk());
                        ps.executeUpdate();
                        ps.close();

                        ps = con.prepareStatement(sqlBillC);
                        ps.setObject(1, billMEntity.getBillNo());
                        ps.setObject(2, billMEntity.getOrdLnPk());
                        ps.setObject(3, billMEntity.getOrdLnDlyPk());
                        ps.executeUpdate();
                        ps.close();

                        ps = con.prepareStatement(sqlSendM);
                        ps.setObject(1, billMEntity.getPlanWt());
                        ps.setObject(2, billMEntity.getSendPlanNo());
                        ps.executeUpdate();
                        ps.close();

                        ps = con.prepareStatement(sqlSendC);
                        ps.setObject(1, billMEntity.getPlanWt());
                        ps.setObject(2, billMEntity.getSendPlanNo());
                        ps.setObject(3, billMEntity.getOrdLnDlyPk());
                        ps.executeUpdate();
                        ps.close();

                        ps = con.prepareStatement(sqlBillNo);
                        ps.setObject(1, billMEntity.getBillNo());
                        ps.executeUpdate();
                        ps.close();
                        // 删除提单运输方式
                        ps = con.prepareStatement(sqlDele);
                        ps.setObject(1, billMEntity.getBillNo());
                        ps.executeUpdate();
                        ps.close();

                    }
                    else {
                        core.setV_errMsg("提单号:" + billMEntity.getBillNo()
                                + "不是提单编制状态,不能撤销提单!");
                        return core;
                    }

                }

            }
            core.setV_errMsg("提单撤销成功!");
            con.commit();

        }
        catch (SQLException e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return core;
    }

    /**
     * 查询交货行量
     * @param billNo 提单号
     * @return CoreReturnObject
     */
    public CoreReturnObject queryOrdLnDlyPkWT(String ordLnDlyPk) {
        Object[] params = new Object[] { ordLnDlyPk };
        return this.getDataBySqlID("CoreCasCarBill.queryOrdLnDlyPkWT", params);
    }

    /**
     * 查询该合同提单量
     * @param billNo 提单号
     * @return CoreReturnObject
     */
    public CoreReturnObject queryBillWeight(String ordLnDlyPk) {
        Object[] params = new Object[] { ordLnDlyPk };
        return this.getDataBySqlID("CoreCasCarBill.queryBillWeight", params);
    }

    /**
     * 查询计划信息
     * @param con
     * @param sendPlanNo
     * @return
     * @throws SQLException
     */
    public static YdmZcSendplanMEntity getSendPlanInfo(Connection con,
            String sendPlanNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.getsendPlanInfoBySendPlanNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, sendPlanNo);
        ResultSet rs = ps.executeQuery();
        List<YdmZcSendplanMEntity> sendPlanMEntitys = EntityTool
                .getEntitysByRs(rs, YdmZcSendplanMEntity.class);
        rs.close();
        ps.close();
        if (sendPlanMEntitys == null || sendPlanMEntitys.size() == 0) {
            return null;
        }
        else {
            for (YdmZcSendplanMEntity ydmZcSendplanMEntity : sendPlanMEntitys) {
                QualityDesignTool.nullConverNullString(ydmZcSendplanMEntity);
            }
            return sendPlanMEntitys.get(0);
        }
    }

    /**
     * 查询计划从表信息
     * @param con
     * @param sendPlanNo
     * @return
     * @throws SQLException
     */
    public static YdmZcSendplanCEntity getSendPlanCInfo(Connection con,
            String sendPlanNo, String ordPk, String ordLnPk, String ordLnDlyPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.getPlanCInfoByordLnPkAndSPNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, sendPlanNo);
        ps.setObject(2, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        List<YdmZcSendplanCEntity> sendPlanCEntitys = EntityTool
                .getEntitysByRs(rs, YdmZcSendplanCEntity.class);
        rs.close();
        ps.close();
        if (sendPlanCEntitys == null || sendPlanCEntitys.size() == 0) {
            return null;
        }
        else {
            for (YdmZcSendplanCEntity sendPlanCEntity : sendPlanCEntitys) {
                QualityDesignTool.nullConverNullString(sendPlanCEntity);
            }
            return sendPlanCEntitys.get(0);
        }
    }

    /**
     * 生成提单号
     * @param con
     * @param salg
     * @return
     * @throws SQLException
     */
    public static String createBillNo(Connection con, String salg)
            throws SQLException {
        String billNo = "";
        String flag = "";
        if (salg.equals("100101")) {
            flag = "X";
        }
        else if (salg.equals("100102")) {
            flag = "M";
        }
        else if (salg.equals("100103")) {
            flag = "G";
        }
        else {
            return "S";
        }
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.createBillNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, flag);
        ps.setObject(2, flag);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            billNo = rs.getString("BILL_NO");
        }
        rs.close();
        ps.close();
        return billNo;
    }

    /**
     * 插入提单号并返回提单号
     * @param con
     * @param salg
     * @return
     * @throws SQLException
     */
    public static String insertBillNo(Connection con, String salg)
            throws SQLException {
        String billNo = "";
        String flag = "";
        if (salg.equals("100101")) {
            flag = "X";
        }
        else if (salg.equals("100102")) {
            flag = "M";
        }
        else if (salg.equals("100103")) {
            flag = "G";
        }
        else {
            return "S";
        }
        String sql = CoreXmlSqlParsersFactory.getSql(
                "CoreCasCarBill.createBillNo").replace("#BILLNO#", "TH" + flag);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, flag);
        ps.setObject(2, flag);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            billNo = rs.getString("BILL_NO");
        }
        rs.close();
        ps.close();
        return billNo;
    }

    /**
     * 验证提单编号
     * @param con 连接字符串
     * @param billNo 提单编号
     * @return String
     * @throws SQLException
     */
    public static String[] checkBillStatus(Connection con, String billNo)
            throws SQLException {
        String[] arrStr = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.checkBillStatus");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, billNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            arrStr = new String[3];
            arrStr[0] = rs.getString("BILL_STATUS");
            arrStr[1] = rs.getString("SEND_PLAN_NO");
            arrStr[2] = rs.getString("VSTEL_CODE");
        }
        rs.close();
        ps.close();
        return arrStr;
    }

    /**
     * 验证新增或者修改提单主表
     * @param con
     * @param billNo
     * @param sendPlanNo
     * @param sendDayPlanNo
     * @param ordPk
     * @param ordLnPk
     * @param ordLnDlyPk
     * @return
     * @throws SQLException
     */
    public static boolean checkIsCommon(Connection con, String billNo,
            String sendPlanNo, String ordLnDlyPk) throws SQLException {
        boolean flag = true;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.checkIsCommon");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, billNo);
        ps.setObject(2, sendPlanNo);
        ps.setObject(3, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt("COUNT");
        }
        rs.close();
        ps.close();
        if (count != 0) {
            flag = false;
        }
        return flag;
    }

    /**
     * 新增提单(汽运)
     * @param param 参数集合
     * @param billEntitys 资源信息集合
     * @return CoreReturnObject
     */
    public CoreReturnObject DoAdd(ArrayList<String> param,
            ArrayList<PipeMatGpMEntity> matInfoEntitys, String strPraFlag,
            String orderNo, String ordersSeqNo, String delvNo) {
        CoreReturnObject core = new CoreReturnObject();
        String billNo = param.get(0);
        String sendPlanNo = param.get(1);
        String ordPk = param.get(2);
        String ordLnPk = param.get(3);
        String ordLnDlyPk = param.get(4);
        String virtualFlag = param.get(5);
        String userName = param.get(6);
        String beginTime = param.get(9);
        String endTime = param.get(10);
        String credNo = param.get(11);
        String pordNo = param.get(12);
        String pordName = param.get(13);
        Date nowTime = new Date();
        SimpleDateFormat dfT = new SimpleDateFormat("   yyyy-MM-dd HH:mm:ss");
        String strNowTime = dfT.format(nowTime);
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 验证计划信息
            YdmZcSendplanMEntity sendPlanMEntity = CoreCasCarBill
                    .getSendPlanInfo(con, sendPlanNo);
            if (sendPlanMEntity == null) {
                core.setV_errMsg("计划" + sendPlanNo + "信息已改变,请重新获取数据!");
                return core;
            }
            if (!(sendPlanMEntity.getSendPlanStatus().equals("1") || sendPlanMEntity
                    .getSendPlanStatus().equals("2"))) {
                core.setV_errMsg("发运计划号:" + sendPlanNo + "不是执行/下发状态,不能生成提单!");
                return core;
            }
            // 生成修改或者新增提单号标识
            boolean flag = false;
            if (!billNo.equals("")) {
                String[] billInfo = CoreCasCarBill.checkBillStatus(con, billNo);
                if (billInfo == null) {
                    core.setV_errMsg("提单号发生变化,请重新获取数据!");
                    return core;
                }
                String billStatus = billInfo[0];
                String beSendPlanNo = billInfo[1];
                if (!(billStatus.equals("0"))) {
                    core.setV_errMsg("提单号:" + billNo + "不是编制,不能追加提单信息!");
                    return core;
                }
                if (!beSendPlanNo.equals(sendPlanNo)) {
                    core.setV_errMsg("不能在计划号:" + beSendPlanNo + "的提单号:"
                            + billNo + "下追加计划号:" + sendPlanNo + "信息!");
                    return core;
                }
                // 如果是同一个计划号同一个交货行号向下就修改提单
                flag = CoreCasCarBill.checkIsCommon(con, billNo, sendPlanNo,
                        ordLnDlyPk);
            }
            else {
                // 生成提单号
                flag = true;
                billNo = CoreCasCarBill.insertBillNo(con, sendPlanMEntity
                        .getSaleOrg());
                String sqlInsertBill = "INSERT INTO YDM_ZC_BILL_NO (BILL_NO) values ('"
                        + billNo + "')";
                Statement sta1 = con.createStatement();
                sta1.executeUpdate(sqlInsertBill);
                sta1.close();
            }

            // 获取合同信息(发运计划子信息)
            YdmZcSendplanCEntity planCEntity = CoreCasCarBill.getSendPlanCInfo(
                    con, sendPlanNo, ordPk, ordLnPk, ordLnDlyPk);
            if (planCEntity == null) {
                core.setV_errMsg("合同信息已改变,请重新获取数据!");
                return core;
            }
            // 操作数据
            DecimalFormat df = new DecimalFormat("0000");
            String sqlUpMat = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateMatZcMByMatNo");
            PreparedStatement psMat = con.prepareStatement(sqlUpMat);

            String sqlAddBillC = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.addBillCInfo");
            PreparedStatement ps1 = con.prepareStatement(sqlAddBillC);

            String sqlAddBillM = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.addBillMInfo");
            String sqlupdateBillM = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateBillByBillNo");
            String sqlupdateSendM = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateSendM");
            PreparedStatement psSendM = con.prepareStatement(sqlupdateSendM);
            String sqlupdateSendC = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateSendC");
            PreparedStatement psSendC = con.prepareStatement(sqlupdateSendC);
            // 插入到运输表
            String sqlupdateDeryL = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.InsertDeryL");
            PreparedStatement psDeryL = con.prepareStatement(sqlupdateDeryL);

            for (PipeMatGpMEntity matInfoEntity : matInfoEntitys) {
                // 分炉标识
                String prtFlag = strPraFlag;

                // 可编辑信息
                List<MatGpMEntity1> matZcMInfos = CoreCasCarBill
                        .getInventoryCanInfo(con, matInfoEntity);

                List<double[]> lenList = MeanValueCalculation.MeanCalculation(
                        Double.parseDouble(matInfoEntity.getSendNum()), Integer
                                .parseInt(String.valueOf(matInfoEntity
                                        .getActCount())));
                List<String> len = new ArrayList<String>();// 计算每只长度
                for (int j = 0; j < lenList.size(); j++) {
                    double[] conLen = lenList.get(j);
                    if ((int) conLen[0] >= 0) {
                        for (int z = 0; z < (int) conLen[0]; z++) {
                            len.add(String.valueOf(conLen[1]));
                        }
                    }
                }
                int w = 0;
                for (MatGpMEntity1 matZcMInfoEntity : matZcMInfos) {
                    String matNo = "";
                    String strBillNo = "";
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.queryBillMatNoC");
                    PreparedStatement ps2 = con.prepareStatement(sql);
                    ps2.setObject(1, matZcMInfoEntity.getMatNo());
                    ResultSet rs = ps2.executeQuery();
                    if (rs.next()) {
                        matNo = rs.getString("MAT_NO");
                        strBillNo = rs.getString("BILL_NO");
                    }
                    rs.close();
                    ps2.close();

                    if (!matNo.equals("")) {
                        core.setV_errMsg("该炉号："
                                + matZcMInfoEntity.getJudgeStoveNo() + "、批号："
                                + matZcMInfoEntity.getBatchNo() + "、组号："
                                + matZcMInfoEntity.getGroudNo()
                                + ",已生成提单！提单号为:" + strBillNo);
                        return core;
                    }
                    String gllockFlag = "";
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.queryLockMatNo");
                    ps2 = con.prepareStatement(sql);
                    ps2.setObject(1, matZcMInfoEntity.getMatNo());
                    rs = ps2.executeQuery();
                    if (rs.next()) {
                        gllockFlag = rs.getString("LOCK_FLAG");
                    }
                    rs.close();
                    ps2.close();

                    if (gllockFlag.equals("1")) {
                        core.setV_errMsg("已封锁,不能生成提单！");
                        return core;
                    }
                    // 修改库存状态
                    psMat.setObject(1, matZcMInfoEntity.getMatNo());
                    psMat.addBatch();

                    // 新增提单从表信息
                    String billNoSeq = matZcMInfoEntity.getMatNo()
                            + df.format(1);
                    ps1.setObject(1, prtFlag);
                    ps1.setObject(2, matZcMInfoEntity.getOrdPk());
                    ps1.setObject(3, matZcMInfoEntity.getOrdLnPk());
                    ps1.setObject(4, matZcMInfoEntity.getOrdLnDlyPk());
                    ps1.setObject(5, billNoSeq);
                    ps1.setObject(6, billNo);
                    ps1.setObject(7, orderNo);
                    ps1.setObject(8, ordersSeqNo);
                    ps1.setObject(9, matZcMInfoEntity.getMatNo());
                    ps1.setObject(10, matZcMInfoEntity.getLotNo());
                    ps1.setObject(11, matZcMInfoEntity.getJudgeStoveNo());
                    ps1.setObject(12, matZcMInfoEntity.getBatchNo());
                    ps1.setObject(13, matZcMInfoEntity.getGroudNo());
                    ps1.setObject(14, matZcMInfoEntity.getStoveNo());
                    ps1.setObject(15, matZcMInfoEntity.getMaterialNo());
                    ps1.setObject(16, matZcMInfoEntity.getMaterialName());
                    ps1.setObject(17, matZcMInfoEntity.getProduccode());
                    ps1.setObject(18, matZcMInfoEntity.getProducname());
                    ps1.setObject(19, matZcMInfoEntity.getJudgeStdCode());
                    ps1.setObject(20, matZcMInfoEntity.getJudgeStdName());
                    ps1.setObject(21, matZcMInfoEntity.getJudgeGradecode());
                    ps1.setObject(22, matZcMInfoEntity.getJudgeGradename());
                    ps1.setObject(23, "");
                    ps1.setObject(24, "");
                    ps1.setObject(25, "");
                    ps1.setObject(26, "");
                    ps1.setObject(27, matZcMInfoEntity.getSpecCode());
                    ps1.setObject(28, matZcMInfoEntity.getSpecName());
                    ps1.setObject(29, "");
                    ps1.setObject(30, "");
                    ps1.setObject(31, matZcMInfoEntity.getPlineCode());
                    ps1.setObject(32, matZcMInfoEntity.getPlineName());
                    ps1.setObject(33, matZcMInfoEntity.getActDimater());
                    ps1.setObject(34, matZcMInfoEntity.getActHeight());
                    ps1.setObject(35, String.valueOf(matZcMInfoEntity
                            .getActLen()));
                    ps1.setObject(36, matZcMInfoEntity.getActLenMin());
                    ps1.setObject(37, matZcMInfoEntity.getActLenMax());
                    ps1.setObject(38, String.valueOf(matZcMInfoEntity
                            .getActCount()));
                    ps1.setObject(39, String.valueOf(matZcMInfoEntity
                            .getActWeight()));
                    ps1.setObject(40, String.valueOf(matZcMInfoEntity
                            .getActTheoryWeight()));
                    ps1.setObject(41, "");
                    ps1.setObject(42, matZcMInfoEntity.getProBc());
                    ps1.setObject(43, matZcMInfoEntity.getProBz());
                    ps1.setObject(44, matZcMInfoEntity.getRpoTime());
                    ps1.setObject(45, matZcMInfoEntity.getMatNo());
                    ps1.setObject(46, matZcMInfoEntity.getMatNo());
                    ps1.setObject(47, userName);
                    ps1.setObject(48, strNowTime);
                    ps1.setObject(49, delvNo);
                    ps1.setObject(50, matInfoEntity.getVstel());
                    ps1.setObject(51, matInfoEntity.getVstelDesc());
                    ps1.setObject(52, String.valueOf(len.get(w)));
                    ps1.setObject(53, matZcMInfoEntity.getActFixNum());
                    ps1.setObject(54, matZcMInfoEntity.getActFixLen());
                    ps1.setObject(55, matInfoEntity.getActFixLen());
                    ps1.addBatch();
                    w++;
                }

            }
            // psMatlen.executeBatch();
            // 查询库存可编制量
            double sumWgtCan = 0;
            int sumCount = 0;
            double sumLen = 0;
            for (PipeMatGpMEntity matInfoEntity : matInfoEntitys) {
                double[] amount = CoreCasCarBill.getInventoryCanWgt(con,
                        matInfoEntity);
                int amountCount = (int) amount[0];
                double amountWgt = amount[1];
                double amountLen = amount[2];
                if (amountCount != Integer
                        .parseInt(matInfoEntity.getActCount())
                        || Math.abs(amountWgt
                                - Double.parseDouble(matInfoEntity
                                        .getActWeight())) > 0.0000000001) {
                    core.setV_errMsg("库存可编制量信息发生改变,请重新获取数据!");
                    return core;
                }
                sumWgtCan += amountWgt;
                sumCount += amountCount;
                sumLen += amountLen;
            }
            // 新增提单主表信息
            if (flag) {

                YdmZcBillMEntity billMEntity = new YdmZcBillMEntity();
                BeanUtils.copyProperties(billMEntity, sendPlanMEntity);
                // 查询提单主表最大序号
                int indexMaxM = CoreCasCarBill.getBillMMaxSeq(con, billNo);
                String billNoSeq = billNo + df.format(indexMaxM);
                BillOrderInfoEntity orderLineEntity = CoreCasCarBill
                        .getBillOrderInfo(con, ordLnDlyPk);
                BeanUtils.copyProperties(billMEntity, planCEntity);
                billMEntity.setStdCode(orderLineEntity.getStdCode());
                billMEntity.setStdName(orderLineEntity.getStdName());
                billMEntity.setSteelcode(orderLineEntity.getSteelcode());
                billMEntity.setSteelname(orderLineEntity.getSteelname());
                billMEntity.setStlmtUnitNo(orderLineEntity.getStlmtUnitNo());
                billMEntity.setStlmtUnitNm(orderLineEntity.getStlmtUnitNm());
                billMEntity.setReceivAddr(orderLineEntity.getReceivAddr());
                billMEntity.setDelvryAddr(orderLineEntity.getDelvryAddr());
                billMEntity
                        .setFstDraweeUnit(orderLineEntity.getFstDraweeUnit());
                billMEntity.setAgencyFl(orderLineEntity.getAgencyFl());
                billMEntity.setPickupFlag(orderLineEntity.getPickupFlag());
                billMEntity.setPlanTransType("110501");
                billMEntity.setBillNo(billNo);
                billMEntity.setBillNoSeq(billNoSeq);
                billMEntity.setPlanNum(String.valueOf(sumCount));
                billMEntity.setPlanWt(String.valueOf(sumWgtCan));
                billMEntity.setPlanTotlLen(String.valueOf(sumLen));
                billMEntity.setCreateName(userName);
                billMEntity.setBillStatus("0");
                billMEntity.setLoadvehicleType(virtualFlag);
                billMEntity.setActStartTime(beginTime);
                billMEntity.setActEndTime(endTime);
                billMEntity.setMemo("");
                billMEntity.setCreateTime(strNowTime);
                billMEntity.setPortNo(pordNo);
                billMEntity.setPortDesc(pordName);
                billMEntity.setCreditNo(credNo);
                billMEntity.setExtraFlag(orderLineEntity.getExtraFlag());
                String sqlAddBillMStr = EntityTool.fillSqlByEntity(sqlAddBillM,
                        billMEntity);
                ps = con.prepareStatement(sqlAddBillMStr);
                ps.executeUpdate();
                ps.close();

                psDeryL.setObject(1, userName);
                psDeryL.setObject(2, strNowTime);
                psDeryL.setObject(3, billNo);
                psDeryL.setObject(4, ordLnDlyPk);
                psDeryL.addBatch();
            }
            else {
                // 修改提单主表信息
                ps = con.prepareStatement(sqlupdateBillM);
                ps.setObject(1, String.valueOf(sumCount));
                ps.setObject(2, String.valueOf(sumWgtCan));
                ps.setObject(3, String.valueOf(sumLen));
                ps.setObject(4, billNo);
                ps.setObject(5, sendPlanNo);
                ps.setObject(6, ordLnDlyPk);
                ps.executeUpdate();
                ps.close();

                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("CoreCasCarBill.queryDeryL");
                PreparedStatement psd = con.prepareStatement(sqlquery);
                psd.setObject(1, ordLnDlyPk);
                psd.setObject(2, billNo);
                ResultSet rsd = psd.executeQuery();
                if (!rsd.next()) {
                    psDeryL.setObject(1, userName);
                    psDeryL.setObject(2, strNowTime);
                    psDeryL.setObject(3, billNo);
                    psDeryL.setObject(4, ordLnDlyPk);
                    psDeryL.addBatch();
                }

            }
            psMat.executeBatch();
            psMat.close();
            ps1.executeBatch();
            ps1.close();

            psSendM.setObject(1, String.valueOf(sumWgtCan));
            psSendM.setObject(2, userName);
            psSendM.setObject(3, strNowTime);
            psSendM.setObject(4, sendPlanNo);
            psSendM.addBatch();
            psSendC.setObject(1, String.valueOf(sumWgtCan));
            psSendC.setObject(2, userName);
            psSendC.setObject(3, strNowTime);
            psSendC.setObject(4, sendPlanNo);
            psSendC.setObject(5, ordLnDlyPk);
            psSendC.addBatch();

            if (flag) {
                core.setV_errMsg("新增提单成功!");
            }
            else {
                core.setV_errMsg("修改提单成功!");
            }
            psSendM.executeBatch();
            psSendC.executeBatch();
            psDeryL.executeBatch();
            con.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return core;
    }

    /**
     * 新增提单(铁运)
     * @param param 参数集合
     * @param billEntitys 资源信息集合
     * @return CoreReturnObject
     */
    public CoreReturnObject doAddBillInfo(ArrayList<String> param,
            ArrayList<PipeMatGpMEntity> matInfoEntitys, String orderNo,
            String ordersSeqNo, String delvNo) {
        CoreReturnObject core = new CoreReturnObject();
        String billNo = param.get(0);
        String sendPlanNo = param.get(1);
        String ordPk = param.get(2);
        String ordLnPk = param.get(3);
        String ordLnDlyPk = param.get(4);
        String userName = param.get(5);
        String virtualFlag = param.get(8);
        String beginTime = param.get(9);
        String endTime = param.get(10);
        Date nowTime = new Date();
        SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strNowTime = dfT.format(nowTime);
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 验证计划信息
            YdmZcSendplanMEntity sendPlanMEntity = CorePipeCarBill
                    .getSendPlanInfo(con, sendPlanNo);
            if (sendPlanMEntity == null) {
                core.setV_errMsg("计划" + sendPlanNo + "信息已改变,请重新获取数据!");
                return core;
            }
            if (!(sendPlanMEntity.getSendPlanStatus().equals("1") || sendPlanMEntity
                    .getSendPlanStatus().equals("2"))) {
                core.setV_errMsg("发运计划号:" + sendPlanNo + "不是编制/下发状态,不能生成提单!");
                return core;
            }
            // 生成修改或者新增提单号标识
            boolean flag = false;
            if (!billNo.equals("")) {
                String[] billInfo = CorePipeCarBill
                        .checkBillStatus(con, billNo);
                if (billInfo == null) {
                    core.setV_errMsg("提单号发生变化,请重新获取数据!");
                    return core;
                }
                String billStatus = billInfo[0];
                String beSendPlanNo = billInfo[1];
                if (!(billStatus.equals("0"))) {
                    core.setV_errMsg("提单号:" + billNo + "不是编制,不能追加提单信息!");
                    return core;
                }
                if (!beSendPlanNo.equals(sendPlanNo)) {
                    core.setV_errMsg("不能在计划号:" + beSendPlanNo + "的提单号:"
                            + billNo + "下追加计划号:" + sendPlanNo + "信息!");
                    return core;
                }
                // 如果是同一个计划号同一个交货行号向下就修改提单
                flag = CorePipeCarBill.checkIsCommon(con, billNo, sendPlanNo,
                        ordPk, ordLnPk, ordLnDlyPk);
            }
            else {
                // 生成提单号
                flag = true;
                billNo = CoreCasCarBill.insertBillNo(con, sendPlanMEntity
                        .getSaleOrg());
                String sqlInsertBill = "INSERT INTO YDM_ZC_BILL_NO (BILL_NO) values ('"
                        + billNo + "')";
                Statement sta1 = con.createStatement();
                sta1.executeUpdate(sqlInsertBill);
                sta1.close();
            }

            // 获取合同信息(发运计划子信息)
            YdmZcSendplanCEntity planCEntity = CoreCasCarBill.getSendPlanCInfo(
                    con, sendPlanNo, ordPk, ordLnPk, ordLnDlyPk);
            if (planCEntity == null) {
                core.setV_errMsg("合同信息已改变,请重新获取数据!");
                return core;
            }
            DecimalFormat df = new DecimalFormat("0000");
            String sqlUpMat = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateMatZcMByMatNo");// 修改库存材料状态
            PreparedStatement psMat = con.prepareStatement(sqlUpMat);
            String sqlAddBillC = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.addBillCInfo");
            PreparedStatement ps1 = con.prepareStatement(sqlAddBillC);// 新增提单从表信息
            String sqlAddBillM = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.addBillMInfo");
            String sqlupdateBillM = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateBillByBillNo");
            String sqlupdateSendM = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateSendM");
            PreparedStatement psSendM = con.prepareStatement(sqlupdateSendM);
            String sqlupdateSendC = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.updateSendC");
            PreparedStatement psSendC = con.prepareStatement(sqlupdateSendC);
            // 插入到运输表
            String sqlupdateDeryL = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.InsertDeryL");
            PreparedStatement psDeryL = con.prepareStatement(sqlupdateDeryL);

            for (PipeMatGpMEntity matInfoEntity : matInfoEntitys) {
                // 分炉标识
                String prtFlag = CoreCasCarBill.checkIsPrtByJustNo(con,
                        matInfoEntity.getJudgeStoveNo(), matInfoEntity
                                .getBatchNo(), matInfoEntity.getGroudNo(),
                        matInfoEntity.getActCount(), "110502");
                // 可编辑信息
                List<MatGpMEntity1> matZcMInfos = CoreCasCarBill
                        .getInventoryCanInfo(con, matInfoEntity);

                List<double[]> lenList = MeanValueCalculation.MeanCalculation(
                        Double.parseDouble(matInfoEntity.getSendNum()), Integer
                                .parseInt(String.valueOf(matInfoEntity
                                        .getActCount())));
                List<String> len = new ArrayList<String>();// 计算每只长度
                for (int j = 0; j < lenList.size(); j++) {
                    double[] conLen = lenList.get(j);
                    if ((int) conLen[0] >= 0) {
                        for (int z = 0; z < (int) conLen[0]; z++) {
                            len.add(String.valueOf(conLen[1]));
                        }
                    }
                }
                int w = 0;

                for (MatGpMEntity1 matZcMInfoEntity : matZcMInfos) {
                    String matNo = "";
                    String strBillNo = "";
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.queryBillMatNoC");
                    PreparedStatement ps2 = con.prepareStatement(sql);
                    ps2.setObject(1, matZcMInfoEntity.getMatNo());
                    ResultSet rs = ps2.executeQuery();
                    if (rs.next()) {
                        matNo = rs.getString("MAT_NO");
                        strBillNo = rs.getString("BILL_NO");
                    }
                    rs.close();
                    ps2.close();

                    if (!matNo.equals("")) {
                        core.setV_errMsg("该炉号："
                                + matZcMInfoEntity.getJudgeStoveNo() + "、批号："
                                + matZcMInfoEntity.getBatchNo() + "、组号："
                                + matZcMInfoEntity.getGroudNo()
                                + ",已生成提单！提单号为:" + strBillNo);
                        return core;
                    }
                    String gllockFlag = "";
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CoreCasCarBill.queryLockMatNo");
                    ps2 = con.prepareStatement(sql);
                    ps2.setObject(1, matZcMInfoEntity.getMatNo());
                    rs = ps2.executeQuery();
                    if (rs.next()) {
                        gllockFlag = rs.getString("LOCK_FLAG");
                    }
                    rs.close();
                    ps2.close();

                    if (gllockFlag.equals("1")) {
                        core.setV_errMsg("已封锁,不能生成提单！");
                        return core;
                    }
                    // 修改库存状态
                    psMat.setObject(1, matZcMInfoEntity.getMatNo());
                    psMat.addBatch();

                    // 新增提单从表信息
                    int billCIndex = CoreCasCarBill.getBillCMaxSeq(con,
                            matZcMInfoEntity.getMatNo());
                    String billNoSeq = matZcMInfoEntity.getMatNo()
                            + df.format(billCIndex + 1);
                    ps1.setObject(1, prtFlag);
                    ps1.setObject(2, matZcMInfoEntity.getOrdPk());
                    ps1.setObject(3, matZcMInfoEntity.getOrdLnPk());
                    ps1.setObject(4, matZcMInfoEntity.getOrdLnDlyPk());
                    ps1.setObject(5, billNoSeq);
                    ps1.setObject(6, billNo);
                    ps1.setObject(7, orderNo);
                    ps1.setObject(8, ordersSeqNo);
                    ps1.setObject(9, matZcMInfoEntity.getMatNo());
                    ps1.setObject(10, matZcMInfoEntity.getLotNo());
                    ps1.setObject(11, matZcMInfoEntity.getJudgeStoveNo());
                    ps1.setObject(12, matZcMInfoEntity.getBatchNo());
                    ps1.setObject(13, matZcMInfoEntity.getGroudNo());
                    ps1.setObject(14, matZcMInfoEntity.getStoveNo());
                    ps1.setObject(15, matZcMInfoEntity.getMaterialNo());
                    ps1.setObject(16, matZcMInfoEntity.getMaterialName());
                    ps1.setObject(17, matZcMInfoEntity.getProduccode());
                    ps1.setObject(18, matZcMInfoEntity.getProducname());
                    ps1.setObject(19, matZcMInfoEntity.getJudgeStdCode());
                    ps1.setObject(20, matZcMInfoEntity.getJudgeStdName());
                    ps1.setObject(21, matZcMInfoEntity.getJudgeGradecode());
                    ps1.setObject(22, matZcMInfoEntity.getJudgeGradename());
                    ps1.setObject(23, "");
                    ps1.setObject(24, "");
                    ps1.setObject(25, "");
                    ps1.setObject(26, "");
                    ps1.setObject(27, matZcMInfoEntity.getSpecCode());
                    ps1.setObject(28, matZcMInfoEntity.getSpecName());
                    ps1.setObject(29, "");
                    ps1.setObject(30, "");
                    ps1.setObject(31, matZcMInfoEntity.getPlineCode());
                    ps1.setObject(32, matZcMInfoEntity.getPlineName());
                    ps1.setObject(33, matZcMInfoEntity.getActDimater());
                    ps1.setObject(34, matZcMInfoEntity.getActHeight());
                    ps1.setObject(35, String.valueOf(matZcMInfoEntity
                            .getActLen()));
                    ps1.setObject(36, matZcMInfoEntity.getActLenMin());
                    ps1.setObject(37, matZcMInfoEntity.getActLenMax());
                    ps1.setObject(38, String.valueOf(matZcMInfoEntity
                            .getActCount()));
                    ps1.setObject(39, String.valueOf(matZcMInfoEntity
                            .getActWeight()));
                    ps1.setObject(40, String.valueOf(matZcMInfoEntity
                            .getActTheoryWeight()));
                    ps1.setObject(41, matZcMInfoEntity.getJudgeResultCodel());
                    ps1.setObject(42, matZcMInfoEntity.getProBc());
                    ps1.setObject(43, matZcMInfoEntity.getProBz());
                    ps1.setObject(44, matZcMInfoEntity.getRpoTime());
                    ps1.setObject(45, matZcMInfoEntity.getMatNo());
                    ps1.setObject(46, matZcMInfoEntity.getMatNo());
                    ps1.setObject(47, userName);
                    ps1.setObject(48, strNowTime);
                    ps1.setObject(49, delvNo);
                    ps1.setObject(50, matInfoEntity.getVstel());
                    ps1.setObject(51, matInfoEntity.getVstelDesc());
                    ps1.setObject(52, String.valueOf(len.get(w)));
                    ps1.setObject(53, matZcMInfoEntity.getActFixNum());
                    ps1.setObject(54, matZcMInfoEntity.getActFixLen());
                    ps1.addBatch();
                    w++;
                }

            }
            // psMatlen.executeBatch();

            // 查询库存可编制量
            double sumWgtCan = 0;
            int sumCount = 0;
            double sumLen = 0;
            for (PipeMatGpMEntity matInfoEntity : matInfoEntitys) {
                double[] amount = CoreCasCarBill.getInventoryCanWgt(con,
                        matInfoEntity);
                int amountCount = (int) amount[0];
                double amountWgt = amount[1];
                double amountLen = amount[2];
                if (amountCount != Integer
                        .parseInt(matInfoEntity.getActCount())
                        || Math.abs(amountWgt
                                - Double.parseDouble(matInfoEntity
                                        .getActWeight())) > 0.0000000001) {
                    core.setV_errMsg("库存可编制量信息发生改变,请重新获取数据!");
                    return core;
                }
                sumWgtCan += amountWgt;
                sumCount += amountCount;
                sumLen += amountLen;
            }
            // 新增提单主表信息
            if (flag) {

                YdmZcBillMEntity billMEntity = new YdmZcBillMEntity();
                BeanUtils.copyProperties(billMEntity, sendPlanMEntity);
                // 查询提单主表最大序号
                int indexMaxM = CoreCasCarBill.getBillMMaxSeq(con, billNo);
                String billNoSeq = billNo + df.format(indexMaxM);
                BillOrderInfoEntity orderLineEntity = CoreCasCarBill
                        .getBillOrderInfo(con, ordLnDlyPk);
                BeanUtils.copyProperties(billMEntity, planCEntity);
                billMEntity.setStdCode(orderLineEntity.getStdCode());
                billMEntity.setStdName(orderLineEntity.getStdName());
                billMEntity.setSteelcode(orderLineEntity.getSteelcode());
                billMEntity.setSteelname(orderLineEntity.getSteelname());
                billMEntity.setStlmtUnitNo(orderLineEntity.getStlmtUnitNo());
                billMEntity.setStlmtUnitNm(orderLineEntity.getStlmtUnitNm());
                billMEntity.setReceivAddr(orderLineEntity.getReceivAddr());
                billMEntity.setDelvryAddr(orderLineEntity.getDelvryAddr());
                billMEntity
                        .setFstDraweeUnit(orderLineEntity.getFstDraweeUnit());
                billMEntity.setAgencyFl(orderLineEntity.getAgencyFl());
                billMEntity.setPickupFlag(orderLineEntity.getPickupFlag());
                billMEntity.setPlanTransType("110502");
                billMEntity.setBillNo(billNo);
                billMEntity.setBillNoSeq(billNoSeq);
                billMEntity.setPlanNum(String.valueOf(sumCount));
                billMEntity.setPlanWt(String.valueOf(sumWgtCan));
                billMEntity.setPlanTotlLen(String.valueOf(sumLen));
                billMEntity.setCreateName(userName);
                billMEntity.setBillStatus("0");
                billMEntity.setLoadvehicleType(virtualFlag);
                billMEntity.setActStartTime(beginTime);
                billMEntity.setActEndTime(endTime);
                billMEntity.setMemo("");
                billMEntity.setCreateTime(strNowTime);
                billMEntity.setCreditNo("");
                billMEntity.setExtraFlag(orderLineEntity.getExtraFlag());
                String sqlAddBillMStr = EntityTool.fillSqlByEntity(sqlAddBillM,
                        billMEntity);
                ps = con.prepareStatement(sqlAddBillMStr);
                ps.executeUpdate();
                ps.close();

                psDeryL.setObject(1, userName);
                psDeryL.setObject(2, strNowTime);
                psDeryL.setObject(3, billNo);
                psDeryL.setObject(4, ordLnDlyPk);
                psDeryL.addBatch();
            }
            else {
                // 修改提单主表信息
                ps = con.prepareStatement(sqlupdateBillM);
                ps.setObject(1, String.valueOf(sumCount));
                ps.setObject(2, String.valueOf(sumWgtCan));
                ps.setObject(3, String.valueOf(sumLen));
                ps.setObject(4, billNo);
                ps.setObject(5, sendPlanNo);
                // ps.setObject(6, ordPk);
                // ps.setObject(7, ordLnPk);
                ps.setObject(6, ordLnDlyPk);
                ps.executeUpdate();
                ps.close();

                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("CoreCasCarBill.queryDeryL");
                PreparedStatement psd = con.prepareStatement(sqlquery);
                psd.setObject(1, ordLnDlyPk);
                psd.setObject(2, billNo);
                ResultSet rsd = psd.executeQuery();
                if (!rsd.next()) {
                    psDeryL.setObject(1, userName);
                    psDeryL.setObject(2, strNowTime);
                    psDeryL.setObject(3, billNo);
                    psDeryL.setObject(4, ordLnDlyPk);
                    psDeryL.addBatch();
                }
            }
            psMat.executeBatch();
            psMat.close();
            ps1.executeBatch();
            ps1.close();

            psSendM.setObject(1, String.valueOf(sumWgtCan));
            psSendM.setObject(2, userName);
            psSendM.setObject(3, strNowTime);
            psSendM.setObject(4, sendPlanNo);
            psSendM.addBatch();
            psSendC.setObject(1, String.valueOf(sumWgtCan));
            psSendC.setObject(2, userName);
            psSendC.setObject(3, strNowTime);
            psSendC.setObject(4, sendPlanNo);
            psSendC.setObject(5, ordLnDlyPk);
            psSendC.addBatch();

            if (flag) {
                core.setV_errMsg("新增提货单成功!");
            }
            else {
                core.setV_errMsg("修改提货单成功!");
            }
            psSendM.executeBatch();
            psSendC.executeBatch();
            psDeryL.executeBatch();
            con.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return core;
    }

    /**
     * 验证是否分炉
     * @param con
     * @param justStoveNo
     * @param batchNo
     * @param actCount
     * @return
     * @throws SQLException
     */
    public static String checkIsPrtByJustNo(Connection con, String justStoveNo,
            String batchNo, String groupNo, String actCount, String prtFlag)
            throws SQLException {
        String prt = "0";
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.checkIsPrtByJustNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, justStoveNo);
        ps.setObject(2, batchNo);
        ps.setObject(3, groupNo);
        ResultSet rs = ps.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt("COUNT");
        }
        rs.close();
        ps.close();
        if (prtFlag.equals("110501")) {
            if (Integer.parseInt(actCount) != count) {
                prt = "1";
            }
        }
        return prt;
    }

    /**
     * 获取交货行交货地点
     * @param con
     * @param ordLnDvPk
     * @return
     * @throws SQLException
     */
    public static String queryOrderLinDer(Connection con, String ordLnDvPk,
            String storage, String storageName) throws SQLException {
        String prt = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.queryOrderLinDer");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDvPk);
        ResultSet rs = ps.executeQuery();
        // String addr = "";
        // if (rs.next()) {
        // addr = rs.getString("DELVRY_ADDR");
        // }
        rs.close();
        ps.close();
        // if (addr.equals("客户库-腾飞") && !storage.equals("LTF1")) {
        // prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
        // + "，不一致,不能进行此操作！";
        // }
        // if (addr.equals("客户库-宝仓"))
        // if (!storage.equals("LBC2") && !storage.equals("LBC1")) {
        // prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
        // + "，不一致,不能进行此操作！";
        // }
        // if (addr.equals("客户库-安利"))
        // if (!storage.equals("LAL1") && !storage.equals("LAL2")
        // && !storage.equals("LAL3")) {
        // prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
        // + "，不一致,不能进行此操作！";
        // }
        // if (addr.equals("客户库-润德")) {
        // if (!storage.equals("LZD1") && !storage.equals("LZT1")) {
        // prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
        // + "，不一致,不能进行此操作！";
        // }
        // }
        return prt;
    }

    /**
     * 查询库存可编制资源信息
     * @param con 连接字符串
     * @param matInfoEntity 资源类
     * @return List<MatZcMInfoEntity>
     * @throws SQLException
     */
    public static List<MatGpMEntity1> getInventoryCanInfo(Connection con,
            PipeMatGpMEntity matInfoEntity) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.getInventoryCanInfo");
        sql = EntityTool.fillSqlByEntity(sql, matInfoEntity);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, Integer.parseInt(matInfoEntity.getActCount()) + 1);
        ResultSet rs = ps.executeQuery();
        List<MatGpMEntity1> matZcMInfoEntitys = EntityTool.getEntitysByRs(rs,
                MatGpMEntity1.class);
        ps.close();
        rs.close();
        if (matZcMInfoEntitys == null || matZcMInfoEntitys.size() == 0) {
            return null;
        }
        else {
            for (MatGpMEntity1 matZcMInfoEntity : matZcMInfoEntitys) {
                QualityDesignTool.nullConverNullString(matZcMInfoEntity);
            }
            return matZcMInfoEntitys;
        }

    }

    /**
     * 查询提单从表最大序号
     * @param con
     * @param matNo
     * @return
     * @throws SQLException
     */
    public static int getBillCMaxSeq(Connection con, String matNo)
            throws SQLException {
        int index = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.getBillCMaxSeq");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, matNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            index = rs.getInt("INDEX_SEQ");
        }
        rs.close();
        ps.close();
        return index;
    }

    /**
     * 查询提单主表最大序号
     * @param con
     * @param matNo
     * @return
     * @throws SQLException
     */
    public static int getBillMMaxSeq(Connection con, String billNo)
            throws SQLException {
        int index = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.getBillMMaxSeq");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, billNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            index = rs.getInt("INDEX_SEQ");
        }
        rs.close();
        ps.close();
        return index;
    }

    /**
     * 查询合同信息
     * @param con
     * @param matNo
     * @return
     * @throws SQLException
     */
    public static BillOrderInfoEntity getBillOrderInfo(Connection con,
            String ordLnDlyPk) throws SQLException {
        // 查询合同信息
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCasCarBill.getOrdNoInfoByOrdLnPk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        List<BillOrderInfoEntity> orderLineEntitys = EntityTool.getEntitysByRs(
                rs, BillOrderInfoEntity.class);
        rs.close();
        ps.close();
        if (orderLineEntitys == null || orderLineEntitys.size() == 0) {
            return null;
        }
        else {
            for (BillOrderInfoEntity billOrderInfoEntity : orderLineEntitys) {
                QualityDesignTool.nullConverNullString(billOrderInfoEntity);
            }
            return orderLineEntitys.get(0);
        }
    }

    /**
     * 合同撤销
     * @param param
     * @return
     */
    public CoreReturnObject cancelDownOrderNo(ArrayList<String> param) {
        CoreReturnObject core = new CoreReturnObject();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.getBillInfoByBillNo");
            String billNo = param.get(0);
            String ordLnDlyPk = param.get(3);
            String judgeStoveNo = param.get(4);
            String batchNo = param.get(5);
            String userName = param.get(6);
            String groupNo = param.get(7);
            ps = con.prepareStatement(sql);
            ps.setObject(1, billNo);
            rs = ps.executeQuery();
            String status = "";
            if (rs.next()) {
                status = rs.getString("BILL_STATUS");
            }
            rs.close();
            ps.close();
            if (!status.equals("0")) {
                core.setV_errMsg("提单号:" + billNo + "发生变化,请重新获取数据!");
                return core;
            }
            // 修改物料表里数据(状态改为 80150301)
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.deleteMatByOrdPkAndJust");
            ps = con.prepareStatement(sql);
            ps.setObject(1, userName);
            ps.setObject(2, billNo);
            ps.setObject(3, ordLnDlyPk);
            ps.setObject(4, judgeStoveNo);
            ps.setObject(5, batchNo);
            ps.setObject(6, groupNo);
            ps.executeUpdate();
            ps.close();
            // 判断量(主表量)
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.queryBillMByOrdPkAndJust");
            ps = con.prepareStatement(sql);
            ps.setObject(1, billNo);
            ps.setObject(2, ordLnDlyPk);
            rs = ps.executeQuery();
            double planWgt = 0;
            int planNum = 0;
            double planLen = 0;
            String sendPlanNo = "";
            if (rs.next()) {
                planWgt = rs.getDouble("PLAN_WT");
                planNum = rs.getInt("PLAN_NUM");
                sendPlanNo = rs.getString("SEND_PLAN_NO");
                planLen = rs.getDouble("PLAN_TOTL_LEN");
            }
            rs.close();
            ps.close();
            // 从表量
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.queryBillCByOrdPkAndJust");
            ps = con.prepareStatement(sql);
            ps.setObject(1, billNo);
            ps.setObject(2, ordLnDlyPk);
            ps.setObject(3, judgeStoveNo);
            ps.setObject(4, batchNo);
            ps.setObject(5, groupNo);
            rs = ps.executeQuery();
            double actWgt = 0;
            int actNum = 0;
            double actLen = 0;
            if (rs.next()) {
                actWgt = rs.getDouble("ACT_WEIGHT");
                actNum = rs.getInt("ACT_COUNT");
                actLen = rs.getDouble("ACT_LEN");
            }
            rs.close();
            ps.close();
            // 提单主表处理
            if (planNum > actNum) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CoreCasCarBill.updateBillMByOrdPkAndJust");
                ps = con.prepareStatement(sql);
                ps.setObject(1, planWgt - actWgt);
                ps.setObject(2, planNum - actNum);
                ps.setObject(3, planLen - actLen);
                ps.setObject(4, billNo);
                ps.setObject(5, ordLnDlyPk);
                ps.executeUpdate();
                ps.close();
            }
            else {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CoreCasCarBill.deleteBillMByOrdPkAndJust");
                ps = con.prepareStatement(sql);
                ps.setObject(1, billNo);
                ps.setObject(2, ordLnDlyPk);
                ps.executeUpdate();
                ps.close();

                sql = CoreXmlSqlParsersFactory
                        .getSql("CoreCasCarBill.DeleteTRANSIT");
                ps = con.prepareStatement(sql);
                ps.setObject(1, billNo);
                ps.setObject(2, ordLnDlyPk);
                ps.executeUpdate();
                ps.close();
            }

            // 删除提单从表对应的数据
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.deleteBillCByOrdPkAndJust");
            ps = con.prepareStatement(sql);
            ps.setObject(1, billNo);
            ps.setObject(2, ordLnDlyPk);
            ps.setObject(3, judgeStoveNo);
            ps.setObject(4, batchNo);
            ps.setObject(5, groupNo);
            ps.executeUpdate();
            ps.close();

            String sqlSendM = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.DeleteSendM");
            String sqlSendC = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.DeleteSendC");

            ps = con.prepareStatement(sqlSendM);
            ps.setObject(1, actWgt);
            ps.setObject(2, sendPlanNo);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement(sqlSendC);
            ps.setObject(1, actWgt);
            ps.setObject(2, sendPlanNo);
            ps.setObject(3, ordLnDlyPk);
            ps.executeUpdate();
            ps.close();

            core.setV_errMsg("撤销成功!");
            con.commit();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return core;
    }

    /**
     * 修改出库地点
     * @param billNos
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject updateStoager(ArrayList<Object> pram,
            String tranType) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "";
        try {
            con.setAutoCommit(false);
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.QUERYBILLMATNO");
            String sqlU = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.UpdateBillCStorage");// 修改提单从表出库地点
            PreparedStatement psupdateBill = con.prepareStatement(sqlU);
            String sqlUty = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.UpdateBillCStoragety");// 修改提单从表出库地点(铁运)
            PreparedStatement psupdateBillty = con.prepareStatement(sqlUty);
            String sqlSend = CoreXmlSqlParsersFactory
                    .getSql("CoreCasCarBill.Updatebuttress");// 修改位置表出库地点
            PreparedStatement psupdateButt = con.prepareStatement(sqlSend);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String billNo = list.get(0);
                String justNo = list.get(1);
                String batchNo = list.get(2);
                String groupNo = list.get(3);
                String storag = list.get(4);
                String storaname = list.get(5);
                String matNo = "";
                ps = con.prepareStatement(sql);
                ps.setObject(1, billNo);
                ps.setObject(2, justNo);
                ps.setObject(3, batchNo);
                ps.setObject(4, groupNo);
                rs = ps.executeQuery();
                while (rs.next()) {
                    matNo = rs.getString("MAT_NO");
                    psupdateButt.setObject(1, storag);
                    psupdateButt.setObject(2, storag + 10101);
                    psupdateButt.setObject(3, matNo);
                    psupdateButt.addBatch();

                    if (tranType.equals("铁运")) {
                        psupdateBillty.setObject(1, storag);
                        psupdateBillty.setObject(2, storag + 10101);
                        psupdateBillty.setObject(3, billNo);
                        psupdateBillty.setObject(4, matNo);
                        psupdateBillty.addBatch();
                    }
                    else {
                        psupdateBill.setObject(1, storag);
                        psupdateBill.setObject(2, storag + 10101);
                        psupdateBill.setObject(3, storag);
                        psupdateBill.setObject(4, storaname);
                        psupdateBill.setObject(5, billNo);
                        psupdateBill.setObject(6, matNo);
                        psupdateBill.addBatch();
                    }

                }
                rs.close();
                ps.close();

            }
            psupdateButt.executeBatch();
            psupdateBill.executeBatch();
            psupdateBillty.executeBatch();
            core.setResult(1);
            con.commit();
        }
        catch (SQLException e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return core;
    }

}
