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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.YdmZcLoadvehicleMatEntity;
import com.steering.pss.ydm.pipeleavefactory.FrmPipeEntruck;
import com.steering.pss.ydm.pipeleavefactory.FrmReturnDashed;

/**
 * 提单退货红冲
 * @author Administrator
 */
public class FrmCasReturnDashed extends DBCommon {
    /**
     * 通过提单号查询炉信息
     * @param billNo 提单号
     * @return CoreReturnObject
     */
    public CoreReturnObject queryBillC(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCasReturnDashed.QueryBillC");
        return this.getData(sql, list.toArray());
    }

    /**
     * 根据炉号、批号自动组号
     * @param conn c
     * @param strLcode 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String queryGroupNo(Connection conn, String strLcode, String strBatch)
            throws Exception {
        int count = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("CorePipeInTemp.queryGroupNo");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strLcode);
        psta.setString(2, strBatch);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = "001";
            }
            else {
                count = Integer.parseInt(res.substring(res.length() - 3, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return StringTool.formatDecimal(++count, "000");
    }

    /**
     * 查询发货实绩表理货单号对应的材料信息
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryOutListBill(ArrayList<String> list,
            String[] salgs,String cusInfo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCasReturnDashed.queryOutListBill");
        sql +=" AND (T.SUPPLY_UNIT_CODE IS NULL OR T.SUPPLY_UNIT_CODE='" + cusInfo +"')";
        sql += CoreConverter.toSqlConditionString("T.BELONG_CODE", salgs);
        sql += "  GROUP BY T.M_BILL_NO,T.HAND_FLAG   ORDER BY CREATE_TIME DESC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询发货实绩表理货单号对应的材料信息
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryLoadC(String billNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCasReturnDashed.QueryLoadC");
        sql += "   GROUP BY T.JUDGE_STOVE_NO, T.BATCH_NO, T.GROUD_NO, T.ORDER_NO, T.ORDER_SEQ, "
                + "T.ORDER_SEQ_NO, T.M_BILL_NO, T.MATERIAL_NAME, T.RECEIV_NM, T.MEMO, T.PRODUCNAME,  "
                + "T.JUDGE_GRADENAME, T.SPEC_NAME, T.STORAGE_NO, B.STORAGE_NAME, T.JUDGE_STD_NAME,  "
                + "T.BELONG_CODE, T.BELONG_NAME, T.CREATE_NAME, T.ORD_LN_DLY_PK, T.ACT_LEN_MIN,  "
                + "T.ACT_LEN_MAX  ORDER BY CREATE_TIME DESC";
        return this.getData(sql, new Object[] { billNo });
    }

    /**
     * 每支材料对应的倒库倒库单号
     * @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("FrmCasReturnDashed.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 = "HO" + matter1.format(dt) + "00001";
            }
            else {
                seq = Integer.parseInt(res.substring(res.length() - 5, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return "HO" + matter1.format(dt)
                + StringTool.formatDecimal(++seq, "00000");
    }

    /**
     * 每支材料对应的入库单号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public static String querySendIn(Connection conn, String strlist)
            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("FrmCasReturnDashed.queryInList");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, matter1.format(dt));
        psta.setString(2, strlist);
        psta.setString(3, matter.format(dt));
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString("INSTOCK_DOC");
        }
        rs.close();
        psta.close();
        return seq;
    }

    /**
     * 获取退货最大材料号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public static String qureyMatNo(Connection conn, String jusNo, String bathNo)
            throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmCasReturnDashed.queryMatNoMax");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, jusNo);
        psta.setString(2, bathNo);
        psta.setString(3, jusNo);
        psta.setString(4, bathNo);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString("MAT_NO");
        }
        rs.close();
        psta.close();
        return seq;
    }

    /**
     *新增退货
     * @param pram
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject addLoadReturn(ArrayList<Object> pram,
            String strbillNo) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sqlquery = "";
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            List<YdmZcLoadvehicleMatEntity> loadCEntitys = null;
            String sqlinsertOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.addOutReturn"); // 新增到出库表
            PreparedStatement pstaOut = connection
                    .prepareStatement(sqlinsertOut);
            String sqlinsertMatNo = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.addMatNoReturn"); // 新增材料表
            PreparedStatement pstaMatNo = connection
                    .prepareStatement(sqlinsertMatNo);
            String sqlinsertbutter = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.addButterReturn"); // 新增位置表
            PreparedStatement pstabutter = connection
                    .prepareStatement(sqlinsertbutter);
            String insetBillM = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.addBillMReturn"); // 新增到提单主表
            PreparedStatement psatBillM = connection
                    .prepareStatement(insetBillM);
            String insetBillC = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.addBillCReturn"); // 新增到提单从表
            PreparedStatement pstaBillC = connection
                    .prepareStatement(insetBillC);
            String insetLoad = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.addLoadReturn"); // 新增到装车主表
            PreparedStatement pstaLoad = connection.prepareStatement(insetLoad);
            String insetLoadMat = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.addLoadMatReturn"); // 新增到装车从表
            PreparedStatement pstaLoadMat = connection
                    .prepareStatement(insetLoadMat);
            String updateProOrd = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.updateProOrd"); // 回写进程表
            PreparedStatement pstaPro = connection
                    .prepareStatement(updateProOrd);
            String cratname = "";
            String crattime = "";
            String billNo = "";
            String bill_no = FrmReturnDashed.querySendIn(connection, strbillNo);
            String loadNo = FrmReturnDashed.queryLoadNo(connection, bill_no);
            double loadCount = 0;
            double loadWeghit = 0;
            double loadlen = 0;
            for (int i = 0; i < pram.size(); i++) {

                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                billNo = list.get(0);
                String ordLnDlyPk = list.get(4);
                String storageNo = list.get(5);
                String judgeNo = list.get(6);
                String batchNo = list.get(7);
                String groupNo = list.get(8);
                cratname = list.get(11);
                crattime = list.get(12);
                String manageNO = list.get(13);
                String manageName = list.get(14);
                String deparId = list.get(15);
                String deparNmae = list.get(16);
                String gbflag = list.get(17);
                String bc = list.get(18);
                String bz = list.get(19);
                double sendnum = Double.parseDouble(list.get(20));
                int count = 0;// 支数
                double weghit = Double.parseDouble(list.get(10));// 退货重量
                double sjweghit = 0;// 实绩重量
                double gbweghit = 0;// 过磅重量

                // 判断是否0支非0吨
                if (list.get(9).equals("0")) {
                    count = 1;
                }
                else {
                    count = Integer.parseInt(list.get(9));
                }
                // 判断是否过磅
                if (gbflag.equals("是")) {
                    gbweghit = weghit;
                }
                else {
                    sjweghit = weghit;
                }

                double loadnum = 0;// 装车支数
                // double loadwt = 0;// 装车吨数
                // double loadpowt = 0;// 装车过磅吨数
                // 查询要退货的装车材料支数
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasReturnDashed.queryYdmLoadMat");
                PreparedStatement psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                psta.setObject(2, judgeNo);
                psta.setObject(3, batchNo);
                psta.setObject(4, groupNo);
                psta.setObject(5, ordLnDlyPk);
                // psta.setObject(6, vehiNo);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    loadnum = Double.parseDouble(rs.getString("ACT_COUNT"));
                    // loadwt = Double.parseDouble(rs.getString("ACT_WEIGHT"));
                    // loadpowt = Double.parseDouble(rs
                    // .getString("PONDER_GROSS_WT"));
                }
                rs.close();
                psta.close();

                double outnum = 0;// 已退货支数
                // double outdwt = 0;// 已退货吨数
                // double outpowt = 0;// 已退货车过磅吨数
                // 查询要已退货的装车材料支数
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasReturnDashed.queryOutLoadMat");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                psta.setObject(2, judgeNo);
                psta.setObject(3, batchNo);
                psta.setObject(4, groupNo);
                psta.setObject(5, ordLnDlyPk);
                // psta.setObject(6, vehiNo);
                rs = psta.executeQuery();
                if (rs.next()) {
                    outnum = Double.parseDouble(rs.getString("ACT_COUNT"));
                    // outdwt = Double.parseDouble(rs.getString("ACT_WEIGHT"));
                    // outpowt = Double.parseDouble(rs
                    // .getString("PONDER_GROSS_WT"));
                }
                rs.close();
                psta.close();

                if (loadnum + outnum == 0) {
                    cro.setV_errMsg("炉号为：" + judgeNo + ",批号为：" + batchNo
                            + ",组号为：" + groupNo + ",已退完。");
                    return cro;
                }

                // 查询要退货的装车材料记录
                sqlquery = CoreXmlSqlParsersFactory.getSql(
                        "FrmCasReturnDashed.queryLoadReturn").replace(
                        "#RUMCONT#", String.valueOf(Math.abs(count) + 1));
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                psta.setObject(2, ordLnDlyPk);
                psta.setObject(3, storageNo);
                psta.setObject(4, judgeNo);
                psta.setObject(5, batchNo);
                psta.setObject(6, groupNo);
                rs = psta.executeQuery();
                loadCEntitys = EntityTool.getEntitysByRs(rs,
                        YdmZcLoadvehicleMatEntity.class);
                rs.close();
                psta.close();

                if (loadCEntitys == null || loadCEntitys.size() == 0) {
                    cro.setV_errMsg("装车信息发生变化,请重新获取数据!");
                    return cro;
                }
                String sendOut = querySendOut(connection, "800806");
                // 获取出库流水号
                String butessMatNo = FrmPipeEntruck.querySendOutList(
                        connection, sendOut, "800806");
                int outNo = Integer.parseInt(butessMatNo.substring(butessMatNo
                        .length() - 4, butessMatNo.length()));
                // 获取材料号
                String matNoMax = qureyMatNo(connection, judgeNo, batchNo);
                int matNo = Integer.parseInt(matNoMax.substring(matNoMax
                        .length() - 4, matNoMax.length()));
                String loadSeq = FrmReturnDashed.queryLoadMatSeqNo(connection,
                        loadNo);
                int loadSeqNo = Integer.parseInt(loadSeq.substring(loadSeq
                        .length() - 5, loadSeq.length()));
                double billCount = 0;
                double billlen = 0;
                if (list.get(9).equals("0")) {// 0支非0吨
                    for (YdmZcLoadvehicleMatEntity loadCEntity : loadCEntitys) {
                        loadCount = 0;
                        loadlen = 0;
                        loadWeghit = weghit;
                        // 新增出库表
                        pstaOut.setString(1, sendOut
                                + StringTool.formatDecimal(outNo, "0000"));
                        pstaOut.setString(2, sendOut);
                        pstaOut.setString(3, "800806");
                        pstaOut.setString(4, "出库红冲");
                        pstaOut.setString(5, loadCEntity.getVehicleNo());
                        pstaOut.setString(6, loadCEntity.getMatNo());
                        pstaOut.setString(7, loadCEntity.getLotNo());
                        pstaOut.setString(8, loadCEntity.getStoveNo());
                        pstaOut.setString(9, loadCEntity.getJudgeStoveNo());
                        pstaOut.setString(10, loadCEntity.getBatchNo());
                        pstaOut.setString(11, loadCEntity.getGroupNo());
                        pstaOut.setString(12, loadCEntity.getMaterialNo());
                        pstaOut.setString(13, loadCEntity.getMaterialName());
                        pstaOut.setString(14, loadCEntity.getProduccode());
                        pstaOut.setString(15, loadCEntity.getProducname());
                        pstaOut.setString(16, loadCEntity.getStdCode());
                        pstaOut.setString(17, loadCEntity.getStdName());
                        pstaOut.setString(18, loadCEntity.getGradecode());
                        pstaOut.setString(19, loadCEntity.getGradename());
                        pstaOut.setString(20, loadCEntity.getSpecCode());
                        pstaOut.setString(21, loadCEntity.getSpecName()); 
                        pstaOut.setString(22, loadCEntity.getPlineCode());
                        pstaOut.setString(23, loadCEntity.getPlineName());
                        pstaOut.setString(24, loadCEntity.getActDimater());
                        pstaOut.setString(25, loadCEntity.getActHeight());
                        pstaOut.setString(26, "0");// 长度
                        pstaOut.setString(27, loadCEntity.getActLenMin());
                        pstaOut.setString(28, loadCEntity.getActLenMax());
                        pstaOut.setString(29, "0");// 支数
                        pstaOut.setObject(30, String.valueOf(weghit));// 重量
                        pstaOut.setString(31, "0");// 理重
                        pstaOut.setString(32, loadCEntity.getStorageNo());
                        pstaOut.setString(33, loadCEntity.getLocationNo());
                        pstaOut.setString(34, loadCEntity.getLaryNo());
                        pstaOut.setString(35, loadCEntity.getLocNo());
                        pstaOut.setString(36, "");
                        pstaOut.setString(37, "");
                        pstaOut.setString(38, loadCEntity.getStorageTypeNo());
                        pstaOut.setString(39, loadCEntity.getStorageTypeName());
                        pstaOut.setString(40, manageNO);
                        pstaOut.setString(41, manageName);
                        if (loadCEntity.getRpoTime() == "") {
                            pstaOut.setString(42, "");
                        }
                        else {
                            pstaOut.setString(42, loadCEntity.getRpoTime()
                                    .substring(0, 19));
                        }
                        pstaOut.setString(43, loadCEntity.getProBc());
                        pstaOut.setString(44, loadCEntity.getProBz());
                        pstaOut.setString(45, crattime);
                        pstaOut.setString(46, bc);
                        pstaOut.setString(47, bz);
                        pstaOut.setString(48, cratname);
                        pstaOut.setString(49, crattime);
                        pstaOut.setString(50, loadCEntity.getOrdPk());
                        pstaOut.setString(51, loadCEntity.getOrdLnPk());
                        pstaOut.setString(52, loadCEntity.getOrdLnDlyPk());
                        pstaOut.setString(53, loadCEntity.getOrderNo());
                        pstaOut.setString(54, loadCEntity.getOrderSeq());
                        pstaOut.setString(55, loadCEntity.getOrderSeqNo());
                        pstaOut.setString(56, deparId);
                        pstaOut.setString(57, deparNmae);
                        pstaOut.setString(58, loadCEntity.getSaleOrg());
                        pstaOut.setString(59, loadCEntity.getSaleOrgDesc());
                        pstaOut.setString(60, loadCEntity.getPonderNo());
                        pstaOut.setObject(61, String.valueOf(weghit));
                        pstaOut.setObject(62, bill_no);
                        pstaOut.setObject(63, String.valueOf(sendnum));
                        pstaOut.setObject(64, loadCEntity.getVstel());
                        pstaOut.setObject(65, loadCEntity.getVstelDesc());
                        pstaOut.setObject(66, "0");
                        pstaOut.setObject(67, "801403");
                        pstaOut.setObject(68, "80150301");
                        pstaOut.setObject(69, loadCEntity.getReceivNo());
                        pstaOut.setObject(70, loadCEntity.getReceivNm());
                        pstaOut.setObject(71, "");
                        pstaOut.setObject(72, loadCEntity.getActFixNum());
                        pstaOut.setObject(73, loadCEntity.getActFixLen());
                        pstaOut.addBatch();

                        // 提单从表
                        String billCseq = FrmReturnDashed.queryBillCSeq(
                                connection, loadCEntity.getMatNo());
                        pstaBillC.setObject(1, billCseq);
                        pstaBillC.setObject(2, bill_no);
                        pstaBillC.setObject(3, loadCEntity.getMatNo());
                        pstaBillC.setObject(4, "0");
                        pstaBillC.setObject(5, "0");
                        pstaBillC.setObject(6, String.valueOf(weghit));
                        pstaBillC.setObject(7, "0");
                        pstaBillC.setObject(8, cratname);
                        pstaBillC.setObject(9, crattime);
                        pstaBillC.setObject(10, String.valueOf(sendnum));
                        pstaBillC.setObject(11, loadCEntity.getMatNo());
                        pstaBillC.addBatch();
                        // 装车从表
                        pstaLoadMat.setObject(1, loadNo
                                + StringTool.formatDecimal(loadSeqNo, "00000"));
                        pstaLoadMat.setObject(2, loadNo);
                        pstaLoadMat.setObject(3, bill_no);
                        pstaLoadMat.setObject(4, loadCEntity.getMatNo());
                        pstaLoadMat.setObject(5, "0");
                        pstaLoadMat.setObject(6, "0");
                        pstaLoadMat.setObject(7, String.valueOf(weghit));
                        pstaLoadMat.setObject(8, "0");
                        pstaLoadMat.setObject(9, crattime);
                        pstaLoadMat.setObject(10, cratname);
                        pstaLoadMat.setObject(11, crattime);
                        pstaLoadMat.setObject(12, String.valueOf(weghit));
                        pstaLoadMat.setObject(13, String.valueOf(sendnum));
                        pstaLoadMat.setObject(14, loadCEntity.getMatNo());
                        pstaLoadMat.addBatch();
                        loadSeqNo++;
                        matNo++;
                    }
                    pstaPro.setObject(1, Math.abs(count));
                    pstaPro.setObject(2, Math.abs(weghit));
                    pstaPro.setObject(3, String.valueOf(Math.abs(sendnum)));
                    pstaPro.setObject(4, ordLnDlyPk);
                    pstaPro.addBatch();

                    // pstaOut.executeBatch();
                    // pstaPro.executeBatch();
                }
                else {// 非0支非0吨
                    List<double[]> lenList = MeanValueCalculation
                            .MeanCalculation(Math.abs(weghit), Math.abs(count));
                    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]));
                            }
                        }
                    }
                    List<double[]> lensendnum = MeanValueCalculation
                            .MeanCalculation(Math.abs(sendnum), Math.abs(count));
                    List<String> len1 = new ArrayList<String>();// 计算每只退货实发量
                    for (int k = 0; k < lensendnum.size(); k++) {
                        double[] conLen1 = lensendnum.get(k);
                        if ((int) conLen1[0] >= 0) {
                            for (int h = 0; h < (int) conLen1[0]; h++) {
                                len1.add(String.valueOf(conLen1[1]));
                            }
                        }
                    }
                    int w = 0;
                    int s = 0;
                    for (YdmZcLoadvehicleMatEntity loadCEntity : loadCEntitys) {
                        billCount += Double.parseDouble(loadCEntity
                                .getActCount());
                        billlen += Double.parseDouble(loadCEntity.getActLen());
                        double weghitgb = Double.parseDouble(loadCEntity
                                .getPonderGrossWt());
                        double weghitsj = Double.parseDouble(loadCEntity
                                .getActWeight());
                        if (gbflag.equals("是")) {
                            weghitgb = Double.parseDouble(len.get(w));
                        }
                        else {
                            weghitsj = Double.parseDouble(len.get(w));
                        }
                        loadCount += Double.parseDouble(loadCEntity
                                .getActCount());
                        loadlen += Double.parseDouble(loadCEntity.getActLen());
                        loadWeghit += -weghitsj;

                        // 新增材料表
                        pstaMatNo.setObject(1, judgeNo + batchNo
                                + StringTool.formatDecimal(matNo, "0000"));
                        pstaMatNo.setObject(2, "801403");
                        pstaMatNo.setObject(3, "80150301");
                        pstaMatNo.setObject(4, loadCEntity.getLotNo());
                        pstaMatNo.setObject(5, loadCEntity.getStoveNo());
                        pstaMatNo.setObject(6, loadCEntity.getJudgeStoveNo());
                        pstaMatNo.setObject(7, loadCEntity.getBatchNo());
                        pstaMatNo.setObject(8, loadCEntity.getGroupNo());
                        pstaMatNo.setObject(9, loadCEntity.getMaterialNo());
                        pstaMatNo.setObject(10, loadCEntity.getMaterialName());
                        pstaMatNo.setObject(11, loadCEntity.getProduccode());
                        pstaMatNo.setObject(12, loadCEntity.getProducname());
                        pstaMatNo.setObject(13, loadCEntity.getGradecode());
                        pstaMatNo.setObject(14, loadCEntity.getGradename());
                        pstaMatNo.setObject(15, loadCEntity.getSpecCode());
                        pstaMatNo.setObject(16, loadCEntity.getSpecName());
                        pstaMatNo.setObject(17, loadCEntity.getStdCode());
                        pstaMatNo.setObject(18, loadCEntity.getStdName());
                        pstaMatNo.setObject(19, loadCEntity.getProOrderNo());
                        pstaMatNo.setObject(20, loadCEntity.getOrderNo());
                        pstaMatNo.setObject(21, loadCEntity.getOrderSeq());
                        pstaMatNo.setObject(22, loadCEntity.getOrderSeqNo());
                        pstaMatNo.setObject(23, loadCEntity.getProcessCdoe());
                        pstaMatNo.setObject(24, loadCEntity.getProcessDesc());
                        pstaMatNo.setObject(25, loadCEntity.getPlineCode());
                        pstaMatNo.setObject(26, loadCEntity.getPlineName());
                        pstaMatNo.setObject(27, loadCEntity.getActDimater());
                        pstaMatNo.setObject(28, loadCEntity.getActHeight());
                        pstaMatNo.setObject(29, String.valueOf(loadCEntity
                                .getActLen()));
                        pstaMatNo.setObject(30, loadCEntity.getActLenMin());
                        pstaMatNo.setObject(31, loadCEntity.getActLenMax());
                        pstaMatNo.setObject(32, loadCEntity.getActCount());
                        pstaMatNo.setObject(33, String.valueOf(weghitsj));
                        pstaMatNo.setObject(34, String.valueOf(loadCEntity
                                .getActTheoryWeight()));
                        pstaMatNo.setObject(35, loadCEntity.getSaleOrg());
                        pstaMatNo.setObject(36, loadCEntity.getSaleOrgDesc());
                        pstaMatNo.setObject(37, crattime);
                        pstaMatNo.setObject(38, "1");
                        pstaMatNo.setObject(39, loadCEntity.getProBc());
                        pstaMatNo.setObject(40, loadCEntity.getProBz());
                        if (loadCEntity.getRpoTime() == "") {
                            pstaMatNo.setObject(41, "");
                        }
                        else {
                            pstaMatNo.setObject(41, loadCEntity.getRpoTime()
                                    .substring(0, 19));
                        }
                        pstaMatNo.setObject(42, cratname);
                        pstaMatNo.setObject(43, crattime);
                        pstaMatNo.setObject(44, loadCEntity.getOrdPk());
                        pstaMatNo.setObject(45, loadCEntity.getOrdLnPk());
                        pstaMatNo.setObject(46, loadCEntity.getOrdLnDlyPk());
                        pstaMatNo.setObject(47, loadCEntity.getPonderNo());
                        pstaMatNo.setObject(48, String.valueOf(weghitgb));
                        pstaMatNo.setObject(49, loadCEntity.getActFixNum());
                        pstaMatNo.setObject(50, loadCEntity.getActFixLen());
                        pstaMatNo.addBatch();

                        // 新增位置表
                        pstabutter.setObject(1, judgeNo + batchNo
                                + StringTool.formatDecimal(matNo, "0000"));
                        pstabutter.setObject(2, loadCEntity.getJudgeStoveNo());
                        pstabutter.setObject(3, loadCEntity.getBatchNo());
                        pstabutter.setObject(4, loadCEntity.getGroupNo());
                        pstabutter.setObject(5, loadCEntity.getStorageNo());
                        pstabutter.setObject(6, loadCEntity.getLocationNo());
                        pstabutter.setObject(7, loadCEntity.getLaryNo());
                        pstabutter.setObject(8, loadCEntity.getLocNo());
                        pstabutter.setObject(9, "");
                        pstabutter.setObject(10, "");
                        pstabutter
                                .setObject(11, loadCEntity.getStorageTypeNo());
                        pstabutter.setObject(12, loadCEntity
                                .getStorageTypeName());
                        pstabutter.setObject(13, manageNO);
                        pstabutter.setObject(14, manageName);
                        pstabutter.setObject(15, cratname);
                        pstabutter.setObject(16, crattime);
                        pstabutter.setObject(17, deparId);
                        pstabutter.setObject(18, deparNmae);
                        pstabutter.addBatch();

                        // 新增出库表
                        pstaOut.setString(1, sendOut
                                + StringTool.formatDecimal(outNo, "0000"));
                        pstaOut.setString(2, sendOut);
                        pstaOut.setString(3, "800806");
                        pstaOut.setString(4, "出库红冲");
                        pstaOut.setString(5, loadCEntity.getVehicleNo());
                        pstaOut.setString(6, judgeNo + batchNo
                                + StringTool.formatDecimal(matNo, "0000"));
                        pstaOut.setString(7, loadCEntity.getLotNo());
                        pstaOut.setString(8, loadCEntity.getStoveNo());
                        pstaOut.setString(9, loadCEntity.getJudgeStoveNo());
                        pstaOut.setString(10, loadCEntity.getBatchNo());
                        pstaOut.setString(11, loadCEntity.getGroupNo());
                        pstaOut.setString(12, loadCEntity.getMaterialNo());
                        pstaOut.setString(13, loadCEntity.getMaterialName());
                        pstaOut.setString(14, loadCEntity.getProduccode());
                        pstaOut.setString(15, loadCEntity.getProducname());
                        pstaOut.setString(16, loadCEntity.getStdCode());
                        pstaOut.setString(17, loadCEntity.getStdName());
                        pstaOut.setString(18, loadCEntity.getGradecode());
                        pstaOut.setString(19, loadCEntity.getGradename());
                        pstaOut.setString(20, loadCEntity.getSpecCode());
                        pstaOut.setString(21, loadCEntity.getSpecName());
                        pstaOut.setString(22, loadCEntity.getPlineCode());
                        pstaOut.setString(23, loadCEntity.getPlineName());
                        pstaOut.setString(24, loadCEntity.getActDimater());
                        pstaOut.setString(25, loadCEntity.getActHeight());
                        pstaOut.setString(26, String.valueOf(-Double
                                .parseDouble(loadCEntity.getActLen())));// 长度
                        pstaOut.setString(27, loadCEntity.getActLenMin());
                        pstaOut.setString(28, loadCEntity.getActLenMax());
                        pstaOut.setString(29, "-1");// 支数
                        pstaOut.setObject(30, String.valueOf(-weghitsj));// 重量
                        pstaOut
                                .setString(31, String.valueOf(-Double
                                        .parseDouble(loadCEntity
                                                .getActTheoryWeight())));// 理重
                        pstaOut.setString(32, loadCEntity.getStorageNo());
                        pstaOut.setString(33, loadCEntity.getLocationNo());
                        pstaOut.setString(34, loadCEntity.getLaryNo());
                        pstaOut.setString(35, loadCEntity.getLocNo());
                        pstaOut.setString(36, "");
                        pstaOut.setString(37, "");
                        pstaOut.setString(38, loadCEntity.getStorageTypeNo());
                        pstaOut.setString(39, loadCEntity.getStorageTypeName());
                        pstaOut.setString(40, manageNO);
                        pstaOut.setString(41, manageName);
                        if (loadCEntity.getRpoTime() == "") {
                            pstaOut.setString(42, "");
                        }
                        else {
                            pstaOut.setString(42, loadCEntity.getRpoTime()
                                    .substring(0, 19));
                        }
                        pstaOut.setString(43, loadCEntity.getProBc());
                        pstaOut.setString(44, loadCEntity.getProBz());
                        pstaOut.setString(45, crattime);
                        pstaOut.setString(46, bc);
                        pstaOut.setString(47, bz);
                        pstaOut.setString(48, cratname);
                        pstaOut.setString(49, crattime);
                        pstaOut.setString(50, loadCEntity.getOrdPk());
                        pstaOut.setString(51, loadCEntity.getOrdLnPk());
                        pstaOut.setString(52, loadCEntity.getOrdLnDlyPk());
                        pstaOut.setString(53, loadCEntity.getOrderNo());
                        pstaOut.setString(54, loadCEntity.getOrderSeq());
                        pstaOut.setString(55, loadCEntity.getOrderSeqNo());
                        pstaOut.setString(56, deparId);
                        pstaOut.setString(57, deparNmae);
                        pstaOut.setString(58, loadCEntity.getSaleOrg());
                        pstaOut.setString(59, loadCEntity.getSaleOrgDesc());
                        pstaOut.setString(60, loadCEntity.getPonderNo());
                        pstaOut.setObject(61, String.valueOf(-weghitgb));
                        pstaOut.setObject(62, bill_no);
                        pstaOut.setObject(63, String.valueOf(-Double
                                .parseDouble(len1.get(s))));
                        pstaOut.setObject(64, loadCEntity.getVstel());
                        pstaOut.setObject(65, loadCEntity.getVstelDesc());
                        pstaOut.setObject(66, "0");
                        pstaOut.setObject(67, "801403");
                        pstaOut.setObject(68, "80150301");
                        pstaOut.setObject(69, loadCEntity.getReceivNo());
                        pstaOut.setObject(70, loadCEntity.getReceivNm());
                        pstaOut.setObject(71, "");
                        pstaOut.setObject(72, loadCEntity.getActFixNum());
                        pstaOut.setObject(73, loadCEntity.getActFixLen());
                        pstaOut.addBatch();

                        // 提单从表
                        String billCseq = FrmReturnDashed.queryBillCSeq(
                                connection, judgeNo
                                        + batchNo
                                        + StringTool.formatDecimal(matNo,
                                                "0000"));
                        pstaBillC.setObject(1, billCseq);
                        pstaBillC.setObject(2, bill_no);
                        pstaBillC.setObject(3, judgeNo + batchNo
                                + StringTool.formatDecimal(matNo, "0000"));
                        pstaBillC.setObject(4, String.valueOf(-Double
                                .parseDouble(loadCEntity.getActLen())));
                        pstaBillC.setObject(5, "-1");
                        pstaBillC.setObject(6, String.valueOf(-weghitsj));
                        pstaBillC
                                .setObject(7, String.valueOf(-Double
                                        .parseDouble(loadCEntity
                                                .getActTheoryWeight())));
                        pstaBillC.setObject(8, cratname);
                        pstaBillC.setObject(9, crattime);
                        pstaBillC.setObject(10, String.valueOf(-Double
                                .parseDouble(len1.get(s))));
                        pstaBillC.setObject(11, loadCEntity.getMatNo());
                        pstaBillC.addBatch();

                        // 装车从表
                        pstaLoadMat.setObject(1, loadNo
                                + StringTool.formatDecimal(loadSeqNo, "00000"));
                        pstaLoadMat.setObject(2, loadNo);
                        pstaLoadMat.setObject(3, bill_no);
                        pstaLoadMat.setObject(4, judgeNo + batchNo
                                + StringTool.formatDecimal(matNo, "0000"));
                        pstaLoadMat.setObject(5, String.valueOf(-Double
                                .parseDouble(loadCEntity.getActLen())));
                        pstaLoadMat.setObject(6, "-1");
                        pstaLoadMat.setObject(7, String.valueOf(-weghitsj));
                        pstaLoadMat
                                .setObject(8, String.valueOf(-Double
                                        .parseDouble(loadCEntity
                                                .getActTheoryWeight())));
                        pstaLoadMat.setObject(9, crattime);
                        pstaLoadMat.setObject(10, cratname);
                        pstaLoadMat.setObject(11, crattime);
                        pstaLoadMat.setObject(12, String.valueOf(-weghitgb));
                        pstaLoadMat.setObject(13, String.valueOf(-Double
                                .parseDouble(len1.get(s))));
                        pstaLoadMat.setObject(14, loadCEntity.getMatNo());
                        pstaLoadMat.addBatch();

                        loadSeqNo++;
                        outNo++;
                        w++;
                        s++;
                        matNo++;
                    }

                    pstaPro.setObject(1, String.valueOf(Math.abs(count)));
                    pstaPro.setObject(2, String.valueOf(Math.abs(weghit)));
                    pstaPro.setObject(3, String.valueOf(Math.abs(sendnum)));
                    pstaPro.setObject(4, ordLnDlyPk);
                    pstaPro.addBatch();

                }

                // 查询提单主表是否有记录
                String BillCOUNT = "";
                String billMatNo = "";// 对齐
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmReturnDashed.queryBillMReturn");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, bill_no);
                psta.setObject(2, ordLnDlyPk);
                rs = psta.executeQuery();
                if (rs.next()) {
                    BillCOUNT = rs.getString("COUNT");
                    billMatNo = rs.getString("BILL_NO_SEQ");
                }
                rs.close();
                psta.close();

                if (BillCOUNT.equals("0")) {
                    String bill_seq = FrmReturnDashed.querySendInList(
                            connection, bill_no);
                    psatBillM.setObject(1, bill_seq);
                    psatBillM.setObject(2, bill_no);
                    psatBillM.setObject(3, String.valueOf(weghit));
                    psatBillM.setObject(4, String.valueOf(-billCount));
                    psatBillM.setObject(5, String.valueOf(-billlen));
                    psatBillM.setObject(6, String.valueOf(weghit));
                    psatBillM.setObject(7, String.valueOf(-billCount));
                    psatBillM.setObject(8, String.valueOf(-billlen));
                    psatBillM.setObject(9, cratname);
                    psatBillM.setObject(10, crattime);
                    psatBillM.setObject(11, billNo);
                    psatBillM.setObject(12, ordLnDlyPk);
                    psatBillM.addBatch();
                    psatBillM.executeBatch();
                }
                else {
                    String sqlupdate = "UPDATE YDM_ZC_BILL_M T"
                            + " SET T.PLAN_WT   = TO_NUMBER(T.PLAN_WT) + TO_NUMBER('"
                            + String.valueOf(weghit)
                            + "'),"
                            + "T.PLAN_NUM      = TO_NUMBER(T.PLAN_NUM) + TO_NUMBER('"
                            + String.valueOf(-billCount)
                            + "'),"
                            + "T.PLAN_TOTL_LEN = TO_NUMBER(T.PLAN_TOTL_LEN) + TO_NUMBER('"
                            + String.valueOf(-billlen)
                            + "'),"
                            + "T.ACT_WT        = TO_NUMBER(T.ACT_WT) + TO_NUMBER('"
                            + String.valueOf(weghit)
                            + "'),"
                            + "T.ACT_NUM       = TO_NUMBER(T.ACT_NUM) + TO_NUMBER('"
                            + String.valueOf(-billCount)
                            + "'),"
                            + "T.ACT_TOTL_LEN  = TO_NUMBER(T.ACT_TOTL_LEN) + TO_NUMBER('"
                            + String.valueOf(-billlen) + "')"
                            + " WHERE T.BILL_NO_SEQ = '" + billMatNo
                            + "'  AND T.RETURN_FLAG = '1'"; // 修改提单主表量
                    Statement sta1 = connection.createStatement();
                    sta1.executeUpdate(sqlupdate);
                    sta1.close();
                }
                pstaMatNo.executeBatch();
                pstabutter.executeBatch();
                pstaBillC.executeBatch();
                pstaLoadMat.executeBatch();
                pstaOut.executeBatch();
                pstaPro.executeBatch();
            }
            // 装车主表
            pstaLoad.setObject(1, loadNo);
            pstaLoad.setObject(2, bill_no);
            pstaLoad.setObject(3, String.valueOf(loadWeghit));
            pstaLoad.setObject(4, String.valueOf(-loadCount));
            pstaLoad.setObject(5, String.valueOf(-loadlen));
            pstaLoad.setObject(6, crattime);
            pstaLoad.setObject(7, cratname);
            pstaLoad.setObject(8, crattime);
            pstaLoad.setObject(9, "1");
            pstaLoad.setObject(10, billNo);
            pstaLoad.addBatch();

            pstaLoad.executeBatch();
            cro.setV_errMsg("新增退货成功！");

            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /**
     * 撤销退货
     * @param pram
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject deleteLoadReturn(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqldeleteOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteOutReturn"); // 删除出库表
            PreparedStatement pstaOut = connection
                    .prepareStatement(sqldeleteOut);
            String sqldeleteMatNo = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteMatNoReturn"); // 删除材料表
            PreparedStatement pstaMatNo = connection
                    .prepareStatement(sqldeleteMatNo);
            String sqldeletebutter = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteButterReturn"); // 删除位置表
            PreparedStatement pstabutter = connection
                    .prepareStatement(sqldeletebutter);
            String deleteBillM = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.deleteBillMReturn"); // 删除提单主表
            PreparedStatement pstabillM = connection
                    .prepareStatement(deleteBillM);
            String deleteBillC = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.deleteBillCReturn"); // 删除提单从表
            PreparedStatement pstaBillC = connection
                    .prepareStatement(deleteBillC);
            String deleteLoad = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.deleteLoadReturn"); // 删除装车主表
            PreparedStatement pstaLoad = connection
                    .prepareStatement(deleteLoad);
            String deleteLoadMat = CoreXmlSqlParsersFactory
                    .getSql("FrmReturnDashed.deleteLoadMatReturn"); // 删除装车从表
            PreparedStatement pstaLoadMat = connection
                    .prepareStatement(deleteLoadMat);

            String updateProOrd = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteProOrd"); // 回写进程表
            PreparedStatement pstaPro = connection
                    .prepareStatement(updateProOrd);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String billNo = list.get(0);
                String judgeNo = list.get(1);
                String batchNo = list.get(2);
                String groupNo = list.get(3);
                String ordLnDlyPk = list.get(5);
                double NuM = Math.abs(Double.parseDouble(list.get(4)));
                String profalg = list.get(6);
                double countc = 0;
                double weghit = 0;
                double prowt = 0;
                double sendnum = 0;
                double len = 0;
                String strcou = "";
                // 查询要已退货量
                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasReturnDashed.queryOutLoadMat");
                PreparedStatement psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                psta.setObject(2, judgeNo);
                psta.setObject(3, batchNo);
                psta.setObject(4, groupNo);
                psta.setObject(5, ordLnDlyPk);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    countc = Double.parseDouble(rs.getString("ACT_COUNT"));
                    weghit = Double.parseDouble(rs.getString("ACT_WEIGHT"));
                    prowt = Double.parseDouble(rs.getString("PONDER_GROSS_WT"));
                    sendnum = Double.parseDouble(rs.getString("SEND_NUM"));
                    len = Double.parseDouble(rs.getString("ACT_LEN"));
                    strcou = rs.getString("COUNNUM");
                }
                rs.close();
                psta.close();
                if (strcou.equals("0")) {
                    cro.setV_errMsg("该记录已撤销退货！");
                    return cro;
                }
                // 查询提单主表是否可退货
                String BILL_TICKET_STATUS = "";
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmReturnDashed.queryBillMRtrun");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                rs = psta.executeQuery();
                if (rs.next()) {
                    BILL_TICKET_STATUS = rs.getString("BILL_TICKET_STATUS");
                }
                rs.close();
                psta.close();
                if (!BILL_TICKET_STATUS.equals("0")) {
                    cro.setV_errMsg("该退货记录提单已票据对齐！");
                    return cro;
                }

                // 查询材料是否可撤销退货
                String matNO = "";
                String LOCK_FLAG = "";
                double count = 0;
                String sqlquery1 = CoreXmlSqlParsersFactory
                        .getSql("FrmCasReturnDashed.queryMatZcNo");
                PreparedStatement psta1 = connection
                        .prepareStatement(sqlquery1);
                psta1.setObject(1, billNo);
                psta1.setObject(2, judgeNo);
                psta1.setObject(3, batchNo);
                psta1.setObject(4, groupNo);
                psta1.setObject(5, ordLnDlyPk);
                ResultSet rs1 = psta1.executeQuery();
                if (rs1.next()) {
                    matNO = rs1.getString("MAT_STATUS");
                    LOCK_FLAG = rs1.getString("LOCK_FLAG");
                    count = Double.parseDouble(rs1.getString("COUNT"));
                }
                rs1.close();
                psta1.close();

                if (count < NuM) {
                    cro.setV_errMsg("炉号:" + judgeNo + ",批号:" + batchNo + ",组号:"
                            + groupNo + "该退货记录已库存红冲,不能撤销！");
                    return cro;
                }
                if (!matNO.equals("80150301")) {
                    cro.setV_errMsg("炉号:" + judgeNo + ",批号:" + batchNo + ",组号:"
                            + groupNo + "该退货记录已发运或已脱单,不能撤销！");
                    return cro;
                }
                if (!LOCK_FLAG.equals("0")) {
                    cro.setV_errMsg("炉号:" + judgeNo + ",批号:" + batchNo + ",组号:"
                            + groupNo + "该退货记录已质量封锁,不能撤销！");
                    return cro;
                }
                double wt = 0;
                if (profalg.equals("是")) {
                    wt = prowt;
                }
                else {
                    wt = weghit;
                }

                if (Math.abs(countc) == 0) {
                    pstaPro.setObject(1, Math.abs(countc));
                    pstaPro.setObject(2, Math.abs(wt));
                    pstaPro.setObject(3, Math.abs(sendnum));
                    pstaPro.setObject(4, ordLnDlyPk);
                    pstaPro.addBatch();

                    // 删除出库表
                    pstaOut.setObject(1, billNo);
                    pstaOut.setObject(2, judgeNo);
                    pstaOut.setObject(3, batchNo);
                    pstaOut.setObject(4, groupNo);
                    pstaOut.setObject(5, ordLnDlyPk);
                    pstaOut.addBatch();

                    // 删除提单从表‘
                    pstaBillC.setObject(1, billNo);
                    pstaBillC.setObject(2, ordLnDlyPk);
                    pstaBillC.setObject(3, judgeNo);
                    pstaBillC.setObject(4, batchNo);
                    pstaBillC.setObject(5, groupNo);
                    pstaBillC.addBatch();

                    // 删除装车从表
                    pstaLoadMat.setObject(1, billNo);
                    pstaLoadMat.setObject(2, ordLnDlyPk);
                    pstaLoadMat.setObject(3, judgeNo);
                    pstaLoadMat.setObject(4, batchNo);
                    pstaLoadMat.setObject(5, groupNo);
                    pstaLoadMat.addBatch();
                }
                else {
                    // 删除材料表
                    pstaMatNo.setObject(1, billNo);
                    pstaMatNo.setObject(2, judgeNo);
                    pstaMatNo.setObject(3, batchNo);
                    pstaMatNo.setObject(4, groupNo);
                    pstaMatNo.setObject(5, ordLnDlyPk);
                    pstaMatNo.addBatch();

                    // 删除位置表
                    pstabutter.setObject(1, billNo);
                    pstabutter.setObject(2, judgeNo);
                    pstabutter.setObject(3, batchNo);
                    pstabutter.setObject(4, groupNo);
                    pstabutter.setObject(5, ordLnDlyPk);
                    pstabutter.addBatch();
                    // 回写进程表
                    pstaPro.setObject(1, Math.abs(countc));
                    pstaPro.setObject(2, Math.abs(wt));
                    pstaPro.setObject(3, Math.abs(sendnum));
                    pstaPro.setObject(4, ordLnDlyPk);
                    pstaPro.addBatch();

                    // 删除提单从表‘
                    pstaBillC.setObject(1, billNo);
                    pstaBillC.setObject(2, ordLnDlyPk);
                    pstaBillC.setObject(3, judgeNo);
                    pstaBillC.setObject(4, batchNo);
                    pstaBillC.setObject(5, groupNo);
                    pstaBillC.addBatch();

                    // 删除装车从表
                    pstaLoadMat.setObject(1, billNo);
                    pstaLoadMat.setObject(2, ordLnDlyPk);
                    pstaLoadMat.setObject(3, judgeNo);
                    pstaLoadMat.setObject(4, batchNo);
                    pstaLoadMat.setObject(5, groupNo);
                    pstaLoadMat.addBatch();

                    // 删除出库表
                    pstaOut.setObject(1, billNo);
                    pstaOut.setObject(2, judgeNo);
                    pstaOut.setObject(3, batchNo);
                    pstaOut.setObject(4, groupNo);
                    pstaOut.setObject(5, ordLnDlyPk);
                    pstaOut.addBatch();
                }
                pstaBillC.executeBatch();
                pstaLoadMat.executeBatch();
                // 查询提单从表是否还存在提单
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmReturnDashed.queryBillCRtrun");
                PreparedStatement psta2 = connection.prepareStatement(sqlquery);
                psta2.setObject(1, billNo);
                psta2.setObject(2, judgeNo);
                psta2.setObject(3, batchNo);
                psta2.setObject(4, groupNo);
                psta2.setObject(5, ordLnDlyPk);
                ResultSet rs2 = psta2.executeQuery();
                if (rs2.next()) {
                    String sqlupdate = "UPDATE YDM_ZC_BILL_M T"
                            + " SET T.PLAN_WT   = TO_NUMBER(T.PLAN_WT) + TO_NUMBER('"
                            + Math.abs(weghit)
                            + "'),"
                            + "T.PLAN_NUM      = TO_NUMBER(T.PLAN_NUM) + TO_NUMBER('"
                            + Math.abs(countc)
                            + "'),"
                            + "T.PLAN_TOTL_LEN = TO_NUMBER(T.PLAN_TOTL_LEN) + TO_NUMBER('"
                            + Math.abs(len)
                            + "'),"
                            + "T.ACT_WT        = TO_NUMBER(T.ACT_WT) + TO_NUMBER('"
                            + Math.abs(weghit)
                            + "'),"
                            + "T.ACT_NUM       = TO_NUMBER(T.ACT_NUM) + TO_NUMBER('"
                            + Math.abs(countc)
                            + "'),"
                            + "T.ACT_TOTL_LEN  = TO_NUMBER(T.ACT_TOTL_LEN) + TO_NUMBER('"
                            + Math.abs(len)
                            + "')"
                            + " WHERE T.BILL_NO = '"
                            + billNo
                            + "' AND T.ORD_LN_DLY_PK='"
                            + ordLnDlyPk
                            + "'  AND T.RETURN_FLAG = '1'"; // 修改提单主表量
                    Statement sta1 = connection.createStatement();
                    sta1.executeUpdate(sqlupdate);
                    sta1.close();
                }
                else {
                    // 删除提单主表
                    pstabillM.setObject(1, billNo);
                    pstabillM.setObject(2, ordLnDlyPk);
                    pstabillM.addBatch();
                }
                rs2.close();
                psta2.close();

                // 查询装车从表是否还存在提单
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmReturnDashed.queryLoadMatRtrun");
                PreparedStatement psta3 = connection.prepareStatement(sqlquery);
                psta3.setObject(1, billNo);
                psta3.setObject(2, judgeNo);
                psta3.setObject(3, batchNo);
                psta3.setObject(4, groupNo);
                psta3.setObject(5, ordLnDlyPk);
                ResultSet rs3 = psta3.executeQuery();
                if (rs3.next()) {
                    String sqlupdate = "UPDATE YDM_ZC_LOADVEHICLE T "
                            + " SET T.STACKING_WT       = TO_NUMBER(T.STACKING_WT) + TO_NUMBER('"
                            + Math.abs(weghit)
                            + "'),"
                            + "  T.STACKING_PIECE    = TO_NUMBER(T.STACKING_PIECE) + TO_NUMBER('"
                            + Math.abs(countc)
                            + "'),"
                            + " T.STACKING_TOTL_LEN = TO_NUMBER(T.STACKING_TOTL_LEN) + TO_NUMBER('"
                            + Math.abs(len) + "')" + "  WHERE T.BILL_NO = '"
                            + billNo + "'   AND T.RETURN_FLAG = '1'"; // 修改提单主表量
                    Statement sta1 = connection.createStatement();
                    sta1.executeUpdate(sqlupdate);
                    sta1.close();
                }
                else {
                    // 删除装车主表
                    pstaLoad.setObject(1, billNo);
                    pstaLoad.addBatch();
                }
                rs3.close();
                psta3.close();
            }
            pstabillM.executeBatch();
            pstaLoad.executeBatch();
            pstaPro.executeBatch();
            pstaMatNo.executeBatch();
            pstabutter.executeBatch();
            // pstaInto.executeBatch();
            pstaOut.executeBatch();
            cro.setV_errMsg("撤销退货成功！");
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /**
     * 插入提单号并返回提单号
     * @param 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(
                "FrmCasReturnDashed.createBillNo").replace("#BILLNO#",
                "BTH" + 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 pram
     * @return
     */
    public CoreReturnObject addPipDataReturn(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlinsertOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.addOutReturn"); // 新增到出库表
            PreparedStatement pstaOut = connection
                    .prepareStatement(sqlinsertOut);
            String sqlinsertMatNo1 = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.addMatNoReturn1"); // 新增材料表
            PreparedStatement pstaMatNo1 = connection
                    .prepareStatement(sqlinsertMatNo1);
            String sqlinsertMatNo = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.addMatNoReturn"); // 新增材料表
            PreparedStatement pstaMatNo = connection
                    .prepareStatement(sqlinsertMatNo);
            String sqlinsertbutter = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.addButterReturn"); // 新增位置表
            PreparedStatement pstabutter = connection
                    .prepareStatement(sqlinsertbutter);
            String updateProOrd = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.updateProOrd"); // 回写进程表
            PreparedStatement pstaPro = connection
                    .prepareStatement(updateProOrd);
            String cratname = "";
            String crattime = "";

            String ordLnDlyPk = list.get(19);
            String judgeNo = list.get(24);
            String batchNo = list.get(25);
            String groupNo = queryGroupNo(connection, judgeNo, batchNo);
            cratname = list.get(42);
            crattime = list.get(43);
            String manageNO = list.get(40);
            String manageName = list.get(41);
            String deparId = list.get(38);
            String deparNmae = list.get(39);
            int count = Integer.parseInt(list.get(26));// 支数
            double weghit = Double.parseDouble(list.get(27));// 退货重量
            double actlen = Double.parseDouble(list.get(28));
            String sendOut = querySendOut(connection, "800806");
            // 获取出库流水号
            String butessMatNo = FrmCasOutAutomatic.querySendOutList(
                    connection, sendOut, "800806");
            int outNo = Integer.parseInt(butessMatNo.substring(butessMatNo
                    .length() - 4, butessMatNo.length()));
            // 获取材料号
            String matNoMax = qureyMatNo(connection, judgeNo, batchNo);
            int matNo = Integer.parseInt(matNoMax.substring(
                    matNoMax.length() - 4, matNoMax.length()));
            String billNo = insertBillNo(connection, list.get(36));
            String sqlInsertBill = "INSERT INTO YDM_ZC_BILL_NO (BILL_NO) values ('"
                    + billNo + "')";
            Statement sta1 = connection.createStatement();
            sta1.executeUpdate(sqlInsertBill);

            if (count == 0) {// 0支非0吨
                // 新增出库表
                pstaOut.setString(1, sendOut
                        + StringTool.formatDecimal(outNo, "0000"));
                pstaOut.setString(2, sendOut);
                pstaOut.setString(3, "800806");
                pstaOut.setString(4, "出库红冲");
                pstaOut.setString(5, "");
                pstaOut.setString(6, judgeNo + batchNo
                        + StringTool.formatDecimal(matNo, "0000"));
                pstaOut.setString(7, "");
                pstaOut.setString(8, "");
                pstaOut.setString(9, judgeNo);
                pstaOut.setString(10, batchNo);
                pstaOut.setString(11, groupNo);
                pstaOut.setString(12, "");
                pstaOut.setString(13, "");
                pstaOut.setString(14, list.get(0));
                pstaOut.setString(15, list.get(1));
                pstaOut.setString(16, list.get(10));
                pstaOut.setString(17, list.get(11));
                pstaOut.setString(18, list.get(4));
                pstaOut.setString(19, list.get(5));
                pstaOut.setString(20, list.get(6));
                pstaOut.setString(21, list.get(7));
                pstaOut.setString(22, list.get(31));
                pstaOut.setString(23, list.get(32));
                pstaOut.setString(24, list.get(20));
                pstaOut.setString(25, list.get(21));
                pstaOut.setString(26, String.valueOf(-actlen));// 长度
                pstaOut.setString(27, list.get(22));
                pstaOut.setString(28, list.get(23));
                pstaOut.setString(29, String.valueOf(-count));// 支数
                pstaOut.setObject(30, String.valueOf(-weghit));// 重量
                pstaOut.setString(31, "");// 理重
                pstaOut.setString(32, list.get(34));
                pstaOut.setString(33, list.get(35));
                pstaOut.setString(34, "");
                pstaOut.setString(35, "");
                pstaOut.setString(36, "");
                pstaOut.setString(37, "");
                pstaOut.setString(38, list.get(48));
                pstaOut.setString(39, list.get(49));
                pstaOut.setString(40, manageNO);
                pstaOut.setString(41, manageName);
                pstaOut.setString(42, "");
                pstaOut.setString(43, "");
                pstaOut.setString(44, "");
                pstaOut.setString(45, crattime);
                pstaOut.setString(46, list.get(44));
                pstaOut.setString(47, list.get(45));
                pstaOut.setString(48, cratname);
                pstaOut.setString(49, crattime);
                pstaOut.setString(50, list.get(17));
                pstaOut.setString(51, list.get(18));
                pstaOut.setString(52, ordLnDlyPk);// 交货行PK
                pstaOut.setString(53, list.get(14));
                pstaOut.setString(54, list.get(15));
                pstaOut.setString(55, list.get(16));
                pstaOut.setString(56, deparId);
                pstaOut.setString(57, deparNmae);
                pstaOut.setString(58, list.get(36));
                pstaOut.setString(59, list.get(37));
                pstaOut.setString(60, "");
                pstaOut.setObject(61, "");
                pstaOut.setObject(62, billNo);
                pstaOut.setObject(63, "");
                pstaOut.setObject(64, list.get(34));
                pstaOut.setObject(65, list.get(50));
                pstaOut.setObject(66, "1");
                pstaOut.setObject(67, list.get(46));
                pstaOut.setObject(68, list.get(47));
                pstaOut.setObject(69, list.get(51));
                pstaOut.setObject(70, list.get(52));
                pstaOut.setObject(71, list.get(33));
                pstaOut.setObject(72, "");
                pstaOut.setObject(73, "");
                pstaOut.addBatch();

                pstaPro.setObject(1, String.valueOf(count));
                pstaPro.setObject(2, String.valueOf(weghit));
                pstaPro.setObject(3, String.valueOf(0));
                pstaPro.setObject(4, ordLnDlyPk);
                pstaPro.addBatch();

                pstaOut.executeBatch();
                pstaPro.executeBatch();
            }
            else {// 非0支非0吨
                List<double[]> lenList = MeanValueCalculation.MeanCalculation(
                        Math.abs(weghit), Math.abs(count));
                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]));
                        }
                    }
                }
                List<double[]> lensendnum = MeanValueCalculation
                        .MeanCalculation(Math.abs(actlen), Math.abs(count));
                List<String> len1 = new ArrayList<String>();// 计算每只退货实发量
                for (int k = 0; k < lensendnum.size(); k++) {
                    double[] conLen1 = lensendnum.get(k);
                    if ((int) conLen1[0] >= 0) {
                        for (int h = 0; h < (int) conLen1[0]; h++) {
                            len1.add(String.valueOf(conLen1[1]));
                        }
                    }
                }
                int w = 0;
                int s = 0;
                for (int c = 0; c < count; c++) {

                    if (!list.get(19).equals("")) {
                        // 新增材料表
                        pstaMatNo.setObject(1, judgeNo + batchNo
                                + StringTool.formatDecimal(matNo, "0000"));
                        pstaMatNo.setObject(2, list.get(46));
                        pstaMatNo.setObject(3, list.get(47));
                        pstaMatNo.setObject(4, "");
                        pstaMatNo.setObject(5, "");
                        pstaMatNo.setObject(6, judgeNo);
                        pstaMatNo.setObject(7, batchNo);
                        pstaMatNo.setObject(8, groupNo);
                        pstaMatNo.setObject(9, "");
                        pstaMatNo.setObject(10, "");
                        pstaMatNo.setObject(11, list.get(0));
                        pstaMatNo.setObject(12, list.get(1));
                        pstaMatNo.setObject(13, list.get(4));
                        pstaMatNo.setObject(14, list.get(5));
                        pstaMatNo.setObject(15, list.get(6));
                        pstaMatNo.setObject(16, list.get(7));
                        pstaMatNo.setObject(17, list.get(10));
                        pstaMatNo.setObject(18, list.get(11));
                        pstaMatNo.setObject(19, "");
                        pstaMatNo.setObject(20, list.get(14));
                        pstaMatNo.setObject(21, list.get(15));
                        pstaMatNo.setObject(22, list.get(16));
                        pstaMatNo.setObject(23, "");
                        pstaMatNo.setObject(24, "");
                        pstaMatNo.setObject(25, list.get(31));
                        pstaMatNo.setObject(26, list.get(32));
                        pstaMatNo.setObject(27, list.get(20));
                        pstaMatNo.setObject(28, list.get(21));
                        pstaMatNo.setObject(29, String.valueOf(len1.get(s)));
                        pstaMatNo.setObject(30, list.get(22));
                        pstaMatNo.setObject(31, list.get(23));
                        pstaMatNo.setObject(32, "1");
                        pstaMatNo.setObject(33, String.valueOf(len.get(w)));
                        pstaMatNo.setObject(34, "");
                        pstaMatNo.setObject(35, list.get(36));
                        pstaMatNo.setObject(36, list.get(37));
                        pstaMatNo.setObject(37, crattime);
                        pstaMatNo.setObject(38, "1");
                        pstaMatNo.setObject(39, "");
                        pstaMatNo.setObject(40, "");
                        pstaMatNo.setObject(41, "");

                        pstaMatNo.setObject(42, cratname);
                        pstaMatNo.setObject(43, crattime);
                        pstaMatNo.setObject(44, list.get(17));
                        pstaMatNo.setObject(45, list.get(18));
                        pstaMatNo.setObject(46, list.get(19));
                        pstaMatNo.setObject(47, "");
                        pstaMatNo.setObject(48, "");
                        pstaMatNo.setObject(49, "");
                        pstaMatNo.setObject(50, "");
                        pstaMatNo.addBatch();
                    }
                    else {// 新增材料表
                        pstaMatNo1.setObject(1, judgeNo + batchNo
                                + StringTool.formatDecimal(matNo, "0000"));
                        pstaMatNo1.setObject(2, list.get(46));
                        pstaMatNo1.setObject(3, list.get(47));
                        pstaMatNo1.setObject(4, "");
                        pstaMatNo1.setObject(5, "");
                        pstaMatNo1.setObject(6, judgeNo);
                        pstaMatNo1.setObject(7, batchNo);
                        pstaMatNo1.setObject(8, groupNo);
                        pstaMatNo1.setObject(9, "");
                        pstaMatNo1.setObject(10, "");
                        pstaMatNo1.setObject(11, list.get(0));
                        pstaMatNo1.setObject(12, list.get(1));
                        pstaMatNo1.setObject(13, list.get(4));
                        pstaMatNo1.setObject(14, list.get(5));
                        pstaMatNo1.setObject(15, list.get(6));
                        pstaMatNo1.setObject(16, list.get(7));
                        pstaMatNo1.setObject(17, list.get(10));
                        pstaMatNo1.setObject(18, list.get(11));
                        pstaMatNo1.setObject(19, "");
                        pstaMatNo1.setObject(20, list.get(14));
                        pstaMatNo1.setObject(21, list.get(15));
                        pstaMatNo1.setObject(22, list.get(16));
                        pstaMatNo1.setObject(23, "");
                        pstaMatNo1.setObject(24, "");
                        pstaMatNo1.setObject(25, list.get(31));
                        pstaMatNo1.setObject(26, list.get(32));
                        pstaMatNo1.setObject(27, list.get(20));
                        pstaMatNo1.setObject(28, list.get(21));
                        pstaMatNo1.setObject(29, String.valueOf(len1.get(s)));
                        pstaMatNo1.setObject(30, list.get(22));
                        pstaMatNo1.setObject(31, list.get(23));
                        pstaMatNo1.setObject(32, "1");
                        pstaMatNo1.setObject(33, String.valueOf(len.get(w)));
                        pstaMatNo1.setObject(34, "");
                        pstaMatNo1.setObject(35, list.get(36));
                        pstaMatNo1.setObject(36, list.get(37));
                        pstaMatNo1.setObject(37, crattime);
                        pstaMatNo1.setObject(38, "1");
                        pstaMatNo1.setObject(39, "");
                        pstaMatNo1.setObject(40, "");
                        pstaMatNo1.setObject(41, "");

                        pstaMatNo1.setObject(42, cratname);
                        pstaMatNo1.setObject(43, crattime);
                        pstaMatNo1.setObject(44, list.get(17));
                        pstaMatNo1.setObject(45, list.get(18));
                        pstaMatNo1.setObject(46, list.get(19));
                        pstaMatNo1.setObject(47, "");
                        pstaMatNo1.setObject(48, "");
                        pstaMatNo1.addBatch();
                    }
                    // 新增位置表
                    pstabutter.setObject(1, judgeNo + batchNo
                            + StringTool.formatDecimal(matNo, "0000"));
                    pstabutter.setObject(2, judgeNo);
                    pstabutter.setObject(3, batchNo);
                    pstabutter.setObject(4, groupNo);
                    pstabutter.setObject(5, list.get(34));
                    pstabutter.setObject(6, list.get(35));
                    pstabutter.setObject(7, "");
                    pstabutter.setObject(8, "");
                    pstabutter.setObject(9, "");
                    pstabutter.setObject(10, "");
                    pstabutter.setObject(11, list.get(48));
                    pstabutter.setObject(12, list.get(49));
                    pstabutter.setObject(13, manageNO);
                    pstabutter.setObject(14, manageName);
                    pstabutter.setObject(15, cratname);
                    pstabutter.setObject(16, crattime);
                    pstabutter.setObject(17, deparId);
                    pstabutter.setObject(18, deparNmae);
                    pstabutter.addBatch();

                    // 新增出库表
                    pstaOut.setString(1, sendOut
                            + StringTool.formatDecimal(outNo, "0000"));
                    pstaOut.setString(2, sendOut);
                    pstaOut.setString(3, "800806");
                    pstaOut.setString(4, "出库红冲");
                    pstaOut.setString(5, "");
                    pstaOut.setString(6, judgeNo + batchNo
                            + StringTool.formatDecimal(matNo, "0000"));
                    pstaOut.setString(7, "");
                    pstaOut.setString(8, "");
                    pstaOut.setString(9, judgeNo);
                    pstaOut.setString(10, batchNo);
                    pstaOut.setString(11, groupNo);
                    pstaOut.setString(12, "");
                    pstaOut.setString(13, "");
                    pstaOut.setString(14, list.get(0));
                    pstaOut.setString(15, list.get(1));
                    pstaOut.setString(16, list.get(10));
                    pstaOut.setString(17, list.get(11));
                    pstaOut.setString(18, list.get(4));
                    pstaOut.setString(19, list.get(5));
                    pstaOut.setString(20, list.get(6));
                    pstaOut.setString(21, list.get(7));
                    pstaOut.setString(22, list.get(31));
                    pstaOut.setString(23, list.get(32));
                    pstaOut.setString(24, list.get(20));
                    pstaOut.setString(25, list.get(21));
                    pstaOut.setString(26, String.valueOf("-" + len1.get(s)));// 长度
                    pstaOut.setString(27, list.get(22));
                    pstaOut.setString(28, list.get(23));
                    pstaOut.setString(29, "-1");// 支数
                    pstaOut.setObject(30, String.valueOf("-" + len.get(w)));// 重量
                    pstaOut.setString(31, "");// 理重
                    pstaOut.setString(32, list.get(34));
                    pstaOut.setString(33, list.get(35));
                    pstaOut.setString(34, "");
                    pstaOut.setString(35, "");
                    pstaOut.setString(36, "");
                    pstaOut.setString(37, "");
                    pstaOut.setString(38, list.get(48));
                    pstaOut.setString(39, list.get(49));
                    pstaOut.setString(40, manageNO);
                    pstaOut.setString(41, manageName);
                    pstaOut.setString(42, "");
                    pstaOut.setString(43, "");
                    pstaOut.setString(44, "");
                    pstaOut.setString(45, crattime);
                    pstaOut.setString(46, list.get(44));
                    pstaOut.setString(47, list.get(45));
                    pstaOut.setString(48, cratname);
                    pstaOut.setString(49, crattime);
                    pstaOut.setString(50, list.get(17));
                    pstaOut.setString(51, list.get(18));
                    pstaOut.setString(52, ordLnDlyPk);// 交货行PK
                    pstaOut.setString(53, list.get(14));
                    pstaOut.setString(54, list.get(15));
                    pstaOut.setString(55, list.get(16));
                    pstaOut.setString(56, deparId);
                    pstaOut.setString(57, deparNmae);
                    pstaOut.setString(58, list.get(36));
                    pstaOut.setString(59, list.get(37));
                    pstaOut.setString(60, "");
                    pstaOut.setObject(61, "");
                    pstaOut.setObject(62, billNo);
                    pstaOut.setObject(63, "");
                    pstaOut.setObject(64, list.get(34));
                    pstaOut.setObject(65, list.get(50));
                    pstaOut.setObject(66, "1");
                    pstaOut.setObject(67, list.get(46));
                    pstaOut.setObject(68, list.get(47));
                    pstaOut.setObject(69, list.get(51));
                    pstaOut.setObject(70, list.get(52));
                    pstaOut.setObject(71, list.get(33));
                    pstaOut.setObject(72, "");
                    pstaOut.setObject(73, "");
                    pstaOut.addBatch();

                    outNo++;
                    w++;
                    s++;
                    matNo++;
                }

                pstaPro.setObject(1, String.valueOf(count));
                pstaPro.setObject(2, String.valueOf(weghit));
                pstaPro.setObject(3, String.valueOf(0));
                pstaPro.setObject(4, ordLnDlyPk);
                pstaPro.addBatch();

                pstaMatNo.executeBatch();
                pstaMatNo1.executeBatch();
                pstabutter.executeBatch();
                pstaOut.executeBatch();
                pstaPro.executeBatch();
            }
            cro.setV_errMsg("手工退货成功！");

            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /**
     * 手工退货撤销
     * @param pram
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject deleteLoadReturn1(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqldeleteOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteOutReturn"); // 删除出库表
            PreparedStatement pstaOut = connection
                    .prepareStatement(sqldeleteOut);
            String sqldeleteMatNo = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteMatNoReturn"); // 删除材料表
            PreparedStatement pstaMatNo = connection
                    .prepareStatement(sqldeleteMatNo);
            String sqldeletebutter = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteButterReturn"); // 删除位置表
            PreparedStatement pstabutter = connection
                    .prepareStatement(sqldeletebutter);
            String updateProOrd = CoreXmlSqlParsersFactory
                    .getSql("FrmCasReturnDashed.deleteProOrd"); // 回写进程表
            PreparedStatement pstaPro = connection
                    .prepareStatement(updateProOrd);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String billNo = list.get(0);
                String judgeNo = list.get(1);
                String batchNo = list.get(2);
                String groupNo = list.get(3);
                String ordLnDlyPk = list.get(5);
                double countc = 0;
                double weghit = 0;
                double sendnum = 0;
                String strcou = "";
                // 查询要已退货量
                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasReturnDashed.queryOutLoadMat");
                PreparedStatement psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, billNo);
                psta.setObject(2, judgeNo);
                psta.setObject(3, batchNo);
                psta.setObject(4, groupNo);
                psta.setObject(5, ordLnDlyPk);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    countc = Double.parseDouble(rs.getString("ACT_COUNT"));
                    weghit = Double.parseDouble(rs.getString("ACT_WEIGHT"));
                    sendnum = Double.parseDouble(rs.getString("SEND_NUM"));
                    strcou = rs.getString("COUNNUM");
                }
                rs.close();
                psta.close();
                if (strcou.equals("0")) {
                    cro.setV_errMsg("该记录已撤销退货！");
                    return cro;
                }
                if (Math.abs(countc) == 0) {
                    pstaPro.setObject(1, countc);
                    pstaPro.setObject(2, weghit);
                    pstaPro.setObject(3, sendnum);
                    pstaPro.setObject(4, ordLnDlyPk);
                    pstaPro.addBatch();

                    // 删除出库表
                    pstaOut.setObject(1, billNo);
                    pstaOut.setObject(2, judgeNo);
                    pstaOut.setObject(3, batchNo);
                    pstaOut.setObject(4, groupNo);
                    pstaOut.setObject(5, ordLnDlyPk);
                    pstaOut.addBatch();

                }
                else {
                    // 删除材料表
                    pstaMatNo.setObject(1, billNo);
                    pstaMatNo.setObject(2, judgeNo);
                    pstaMatNo.setObject(3, batchNo);
                    pstaMatNo.setObject(4, groupNo);
                    pstaMatNo.setObject(5, ordLnDlyPk);
                    pstaMatNo.addBatch();

                    // 删除位置表
                    pstabutter.setObject(1, billNo);
                    pstabutter.setObject(2, judgeNo);
                    pstabutter.setObject(3, batchNo);
                    pstabutter.setObject(4, groupNo);
                    pstabutter.setObject(5, ordLnDlyPk);
                    pstabutter.addBatch();

                    pstaPro.setObject(1, countc);
                    pstaPro.setObject(2, weghit);
                    pstaPro.setObject(3, sendnum);
                    pstaPro.setObject(4, ordLnDlyPk);
                    pstaPro.addBatch();

                    // 删除出库表
                    pstaOut.setObject(1, billNo);
                    pstaOut.setObject(2, judgeNo);
                    pstaOut.setObject(3, batchNo);
                    pstaOut.setObject(4, groupNo);
                    pstaOut.setObject(5, ordLnDlyPk);
                    pstaOut.addBatch();
                }

                String sqldeleteBill = "DELETE YDM_ZC_BILL_NO WHERE BILL_NO = '"
                        + billNo + "'";
                Statement sta1 = connection.createStatement();
                sta1.executeUpdate(sqldeleteBill);
            }
            pstaPro.executeBatch();
            pstaMatNo.executeBatch();
            pstabutter.executeBatch();
            pstaOut.executeBatch();
            cro.setV_errMsg("撤销退货成功！");
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

}
