package com.steering.pss.ydm.pipeleavefactory;

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

import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * queryBillInfo 理货管理
 * @author Administrator TJG 2015-03-10
 */
public class FrmTallYsheet extends DBCommon {
    /**
     * 查询客户信息.
     * @return obj
     */
    public CoreReturnObject queryCustomer() {
        // 通过sqlID查询数据
        return this.getDataBySqlID("FrmTallYsheet.QueryCustomer");
    }

    /**
     * 查询理货装车状态
     * @return obj
     */
    public CoreReturnObject queryTallType(String billNo, String tallNo) {
        // 通过sqlID查询数据
        return this.getDataBySqlID("FrmTallYsheet.queryTallType", new Object[] {
                billNo, tallNo });
    }

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

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

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

    /**
     * 查询日请车数据
     * @return obj
     */
    public CoreReturnObject doQueryAskDay(ArrayList<String> list) {
        return this.getDataBySqlID("FrmTallYsheet.doQueryAskDay", list
                .toArray());
    }

    /***
     * 回写打印次数，打印时间
     * @param areaNo
     * @return
     */
    public CoreReturnObject updatePintFlag(String time, String tallNo,
            String billNo) {
        return this.setDataBySqlID("FrmTallYsheet.updatePintFlag",
                new Object[] { time, tallNo, billNo });
    }

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

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

    // /**
    // * 自动获取理货单号
    // * @param conn c
    // * @param strlist 传递参数
    // * @return obj
    // * @throws Exception 异常处理
    // */
    // public static String querySendList(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("FrmTallYsheet.QuerySendList");
    // psta = conn.prepareStatement(sqlLovsqe);
    // psta.setString(1, strlist);
    // psta.setString(2, matter1.format(dt));
    // psta.setString(3, matter.format(dt));
    // psta.setString(4, strlist);
    // ResultSet rs = psta.executeQuery();
    // if (rs.next()) {
    // seq = rs.getString(1);
    // }
    // rs.close();
    // psta.close();
    // return seq;
    // }
    /**
     * 查询理货单号.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject querySendList1(ArrayList<String> list) {
        return this.getDataBySqlID("FrmTallYsheet.QuerySendList1", list
                .toArray());
    }

    /**
     * 查询理货车辆信息车皮数
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryAskTallyT(String batchDay, String billNO) {
        return this.getDataBySqlID("FrmTallYsheet.queryAskTallyT",
                new Object[] { batchDay, billNO });
    }

    /**
     * 查询提单信息.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryBillInfo(ArrayList<String> list,
            String strStatus, String[] storage,String custom) {
        String sql = "";
        sql = CoreXmlSqlParsersFactory.getSql("FrmTallYsheet.QueryBillInfo");
        sql += " AND DECODE('" + list.get(4)
                + "', NULL, SYSDATE,T.CREATE_TIME) BETWEEN DECODE('"
                + list.get(4) + "', NULL, SYSDATE,TO_DATE('" + list.get(4)
                + "','YYYY-MM-DD HH24:MI:SS'))" + " AND DECODE('" + list.get(5)
                + "', NULL, SYSDATE,TO_DATE('" + list.get(5)
                + "','YYYY-MM-DD HH24:MI:SS')) ";
        if (!list.get(1).equals("")) {
            sql += " AND UPPER(T.CUSTOMER_NM) LIKE '%' || UPPER('"
                    + list.get(1) + "') || '%'";
        }
        if (!list.get(0).equals("")) {
            sql += " AND T.BILL_NO LIKE '%' || '" + list.get(0) + "' || '%'";
        }
        if (!list.get(2).equals("")) {
            sql += "AND T.PLAN_TRANS_TYPE LIKE '%' || '" + list.get(2)
                    + "' || '%'";
        }
        sql += "  AND UPPER(T.ORDER_NO) LIKE '%'||UPPER('" + list.get(6)
                + "')||'%' AND UPPER(C.JUDGE_STOVE_NO) LIKE '%'||UPPER('"
                + list.get(7) + "') ||'%' AND T.ZOUT_FLAG='0'";
        if (strStatus.equals("1")) {
            sql += " AND T.BILL_STATUS IN('1','3') ";
        }
        else if (strStatus.equals("0")) {
            // sql += " AND T.BILL_STATUS IN('1','3','4')";
        }
        else {
            sql += " AND T.BILL_STATUS >= '" + strStatus + "' ";
        }
        if (list.get(3).equals("")) {
            String storNo = CoreConverter.toSqlConditionString("C.STORAGE_NO",
                    storage);
            String vstelNo = CoreConverter.toSqlConditionString("C.VSTEL",
                    storage);

            String storag = storNo.substring(1, 4) + "("
                    + storNo.substring(4, storNo.length()) + " OR"
                    + vstelNo.substring(4, vstelNo.length()) + ")";
            sql += storNo;
        }
        else {
             sql += " AND C.STORAGE_NO ='" + list.get(3) + "'";
//            sql += " AND (C.STORAGE_NO ='" + list.get(3) + "' OR C.VSTEL ='"
//                    + list.get(3) + "')";
        }
       sql += "  AND T.VALIDFLAG = '1' AND NOT (t.bill_no LIKE 'FT%' AND t.LOADVEHICLE_TYPE = '2') "
       		+ " GROUP BY T.BILL_NO,C.ORDER_NO, C.ORDER_SEQ, C.DELIVERY_NO,T.PLAN_TRANS_TYPE,T.CUSTOMER_NO, T.CUSTOMER_NM, C.JUDGE_STOVE_NO, C.BATCH_NO,C.GROUP_NO, C.STORAGE_NO,C.LOCATION_NO,"
                    + "C.VSTEL, C.VSTEL_DESC,S.STORAGE_NAME, T.CREATE_NAME, T.CREATE_TIME, C.PRODUCNAME, C.STEELNAME,C.SPEC_NAME, T.BILL_STATUS, S.STORAGE_ADDR_NO,"
                    + "T.LOADVEHICLE_TYPE, T.STATION_NM,T.ORD_PK,T.ORD_LN_PK,T.ORD_LN_DLY_PK,T.SPCL_LN_NM,C.STD_NAME,C.GRADENAME,T.STATION_NO,T.SPCL_LN_NO,T.RECEIV_NO,T.RECEIV_NM,"
                    + "C.STD_STYLE_DESC,C.MODEL_DESC,T.ISPRECLOSE,T.SALE_ORG,T.SALE_ORG_DESC,T.PICKUP_FLAG,T.DELVRY_ADDR,T.AGENCY_FL,C.MOVE_LOAD_FLAG,"
                    + " T.CREDIT_NO,T.PORT_NO,T.PORT_DESC,T.BILL_TICKET_STATUS,T.OUTSTOCK_TICKET_STATUS, T.TRANSMNY_TICKET_STATUS,T.SUPPLY_UNIT,T.SUPPLY_UNIT_DESC,C.MEMO,M.PIPMEMO  ORDER BY T.CREATE_TIME DESC)T1";
        if (strStatus.equals("1")) {
            sql += "  WHERE T1.PLAN_COUNT!=0";
        }
        System.out.println(sql);
        return this.getData(sql);
    }

    /**
     * 查询理货材料信息（GROUP）.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallyInfoMat(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTallYsheet.QueryTallyInfoMat");
        sql += " GROUP BY T.TALLYSHEET_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.GROUP_NO, T.ORDER_NO,T.ORDER_SEQ,T.DELIVERY_NO,T.STOVE_NO,"
                + "T.MATERIAL_NAME,T.PRODUCNAME,T.STD_NAME,T.GRADENAME,T.STEELNAME,T.STD_STYLE_DESC,T.SPEC_NAME,"
                + "T.MODEL_DESC,T.PROCESS_DESC,T.PLINE_NAME,T.ACT_DIMATER,T.ACT_HEIGHT,T.ACT_LEN_MIN,T.ACT_LEN_MAX,"
                + "T.JUDGE_RESULT_CODE,T.CREATE_NAME,T.STORAGE_NO,T.VSTEL_DESC,T.ORD_PK,T.ORD_LN_PK,T.ORD_LN_DLY_PK,T.BILL_NO,T.MEMO ORDER BY T.JUDGE_STOVE_NO ASC";
        return this.getData(sql, list.toArray());
    }

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

    /**
     * 查询理货主信息（GROUP）.
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallyInfo(String strTallyNO) {
        return this.getDataBySqlID("FrmTallYsheet.QueryTallyInfo",
                new Object[] { strTallyNO });
    }

    /**
     * 根据提单号查询理货单
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryPerformance(ArrayList<String> list,
            String[] storager, String storag, String loadType) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTallYsheet.queryPerformance");
        if (storag.equals("")) {
            sql += CoreConverter.toSqlConditionString("T.OUT_STOCK_CODE",
                    storager);
        }
        else {
            sql += " AND T.OUT_STOCK_CODE ='" + storag + "'";
        }
        if (loadType.equals("")) {
            sql += " AND T.LOADVEHICLE_FLAG IN ('0','1','2')";
        }
        else if (loadType.equals("2")) {
            sql += " AND T.LOADVEHICLE_FLAG='2'";
        }
        else {
            sql += " AND T.LOADVEHICLE_FLAG IN ('0','1')";
        }
        sql += " GROUP BY T.TALLYSHEET_NO,T.BILL_NO,T.TRANS_TYPE,T.LOADVEHICLE_FLAG,T.CREATE_TIME,"
                + "T.PRINT_NUM,T.PRINT_FLAG,T.END_TIME  ORDER BY T.CREATE_TIME DESC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 根据提单号，理货单号
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryPerforIn(ArrayList<String> list) {
        return this.getDataBySqlID("FrmTallYsheet.queryPerforIn", list
                .toArray());
    }

    /**
     * 查询提单类型、提单状态
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryBillType(ArrayList<String> list) {
        return this.getDataBySqlID("FrmTallYsheet.queryBillType", list
                .toArray());
    }

    /**
     * 查询是否虚单未理货
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryBillTypeXd(ArrayList<String> list) {
        return this.getDataBySqlID("FrmTallYsheet.queryBillTypeXd", list
                .toArray());
    }

    /**
     * 查询理货主表是否存在该记录
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallaySheet(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmTallYsheet.queryTallaySheet");
        sql += " AND TALLYSHEET_NO =" + "'" + list.get(4) + "'"
                + " AND BILL_NO=" + "'" + list.get(0) + "'"
                + " AND VEHICLE_NO=" + "'" + list.get(7) + "'"
                + " AND CARRY_COMPANY_CODE=" + "'" + list.get(8) + "'"
                + " ORDER BY VEHICLE_NO ASC";
        return this.getData(sql);
    }

    /**
     * 查询理货材料对应的理货单序号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSl(Connection conn, String strlist) throws Exception {
        int seq = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmTallYsheet.QuerySendSql_c");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strlist + "000001";
            }
            else {
                seq = Integer.parseInt(res.substring(16, 22));
            }
        }
        rs.close();
        psta.close();
        return strlist + StringTool.formatDecimal(++seq, "000000");
    }

    /**
     * 自动生成理货单号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendTally(Connection conn, String strSendList)
            throws Exception {
        int seq = 0;
        PreparedStatement psta = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");// 设置日期格式
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmTallYsheet.QuerySendList");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, df.format(new Date()));
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strSendList + "0001";
            }
            else {
                seq = Integer.parseInt(res.substring(10, 14));
            }
        }
        rs.close();
        psta.close();
        return strSendList + StringTool.formatDecimal(++seq, "0000");
    }

    /**
     * 新增理貨单主信息.
     * @param pram 理货车辆信息参数
     * @param pram1 新增理货材料表参数
     * @param list 新增理货主表參數
     * @return obj
     */
    public CoreReturnObject addTallySheet(ArrayList<Object> pram,
                                          ArrayList<Object> pram1, ArrayList<Object> list) {
        return addTallySheet(pram,pram1,list,"");
    }

    public CoreReturnObject addTallySheet(ArrayList<Object> pram,
            ArrayList<Object> pram1, ArrayList<Object> list,String sfjl) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = null;
        //Connection limsCon = null;
        PreparedStatement psta = null;
        PreparedStatement ps = null;
        try {
            connection = this.getDao(CoreConstantData.sysbeanstr)
                    .getConnection();
            connection.setAutoCommit(false); // 取消自动提交事务
            /*if("1".equals(sfjl)){
                limsCon = this.getDao("LimsDao").getConnection();
                limsCon.setAutoCommit(false); // 取消自动提交事务
            }*/
            String sqlquery = "";
            String sqlinsertTallC = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.AddTallyMat"); // 新增到理货单材料表
            PreparedStatement psta3 = connection
                    .prepareStatement(sqlinsertTallC);
            String sqlinsertTallM = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.AddTallyInfo"); // 新增到理货主表
            PreparedStatement psta6 = connection
                    .prepareStatement(sqlinsertTallM);
            String sqlinsertTallCar = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.AddTallyCar"); // 理货车辆信息表
            PreparedStatement psta7 = connection
                    .prepareStatement(sqlinsertTallCar);
            String updateBillC = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateBillC");// 修改提单材料表理货状态
            PreparedStatement updatebillC = connection
                    .prepareStatement(updateBillC);
            String updateBillM = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateBillM");// 修改提单主表提单状态
            PreparedStatement updatebillM = connection
                    .prepareStatement(updateBillM);
            String updateTallM = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateTallM");// 修改理货主表理货量
            PreparedStatement updatetallM = connection
                    .prepareStatement(updateTallM);
            String updateAskDay60 = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateAskDay60");// 修改日请车使用数
            PreparedStatement updateDay60 = connection
                    .prepareStatement(updateAskDay60);
            String updateAskDay70 = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateAskDay70");// 修改日请车使用数
            PreparedStatement updateDay70 = connection
                    .prepareStatement(updateAskDay70);
            List<YdmZcBillCEntity> ydmBill = new ArrayList<YdmZcBillCEntity>();
            // ArrayList<String> billMats = new ArrayList<String>(); //
            // 保存提单材料表主键
            // 数组长度等于装车支数和
            // String strSend = querySendTally(connection, sendList);
            int strcount = 0;// 支数
            double strWeight = 0;// 重量
            double strLen = 0;// 长度
            String butessMatNo = "";
            String TallMsqe = "";
            DecimalFormat df = new DecimalFormat("0.000");
            DecimalFormat dfLen = new DecimalFormat("0.00");
            for (int i = 0; i < pram1.size(); i++) {
                ydmBill.clear();
                ArrayList<String> list1 = (ArrayList<String>) pram1.get(i);
                // billMats = new ArrayList<String>();
                // String tallNo= querySendList(connection,);
                butessMatNo = querySendSl(connection, list1.get(9));

                int count = Integer.parseInt(list1.get(4));
                int countW = 0;
                // 查询提单对应合同、炉号库存剩余量
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmTallYsheet.queryAmount");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list1.get(0));
                psta.setString(2, list1.get(2));
                psta.setString(3, list1.get(3));
                psta.setString(4, list1.get(5)+list1.get(6));
                psta.setString(5, list1.get(10));
                psta.setString(6, list1.get(21));
                psta.setString(7, list1.get(27));
                psta.setString(8, list1.get(28));
                ResultSet rsnum = psta.executeQuery();
                if (rsnum.next()) {
                    countW = Integer.parseInt(rsnum.getString(1));
                }
                rsnum.close();
                psta.close();

                if (count > countW) {
                    cro.setV_errMsg("您输入的支数在[" + list1.get(2) + "]该炉、["
                            + list1.get(3) + "]该批号下已不足" + count + "支,请重新输入！");
                    cro.setV_errCode(-1);
                    return cro;
                }

                // 按炉批行和提单号查询提单材料表主键
                sqlquery = CoreXmlSqlParsersFactory.getSql(
                        "FrmTallYsheet.QueryBillSql").replace("#rumCont#",
                        String.valueOf(count + 1));
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list1.get(0));
                psta.setString(2, list1.get(2));
                psta.setString(3, list1.get(3));
                psta.setString(4, list1.get(5)+list1.get(6));
                psta.setString(5, list1.get(10));
                psta.setString(6, list1.get(21));
                psta.setString(7, list1.get(27));
                psta.setString(8, list1.get(28));
                ResultSet rs = psta.executeQuery();
                ydmBill = EntityTool.getEntitysByRs(rs, YdmZcBillCEntity.class);
                // while (rs.next()) {
                // billMats.add(rs.getString(1));
                // }
                rs.close();
                psta.close();

                String attrNo = "";
                String attrName = "";
                String typeNo = "";
                String typeName = "";
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmTallYsheet.queryTager"); // 查詢库位信息
                PreparedStatement psta2 = connection.prepareStatement(sqlquery);
                psta2.setString(1, list1.get(22));
                ResultSet rsSto = psta2.executeQuery();
                if (rsSto.next()) {
                    attrNo = rsSto.getString(1);
                    attrName = rsSto.getString(2);
                    typeNo = rsSto.getString(3);
                    typeName = rsSto.getString(4);
                }
                rsSto.close();
                psta2.close();

                int tallSeq = Integer.parseInt(butessMatNo.substring(
                        butessMatNo.length() - 6, butessMatNo.length()));
                for (YdmZcBillCEntity lvMat : ydmBill) {
                    strcount += Integer.parseInt(lvMat.getActCount());
                    strWeight += Double.parseDouble(lvMat.getActWeight());
                    strLen += Double.parseDouble(lvMat.getActLen());

                    psta3.setString(1, list1.get(9)
                            + StringTool.formatDecimal(tallSeq, "000000"));
                    psta3.setString(2, list1.get(0));
                    psta3.setString(3, list1.get(9));
                    psta3.setString(4, lvMat.getMatNo());
                    psta3.setString(5, list1.get(5));
                    psta3.setString(6, list1.get(6));
                    psta3.setString(7, lvMat.getLotNo());
                    psta3.setString(8, lvMat.getJudgeStoveNo());
                    psta3.setString(9, lvMat.getBatchNo());
                    psta3.setString(10, lvMat.getGroupNo());
                    psta3.setString(11, lvMat.getStoveNo());
                    psta3.setString(12, lvMat.getMaterialNo());
                    psta3.setString(13, lvMat.getMaterialName());
                    psta3.setString(14, lvMat.getProduccode());
                    psta3.setString(15, lvMat.getProducname());
                    psta3.setString(16, lvMat.getStdCode());
                    psta3.setString(17, lvMat.getStdName());
                    psta3.setString(18, lvMat.getGradecode());
                    psta3.setString(19, lvMat.getGradename());
                    psta3.setString(20, lvMat.getSteelcode());
                    psta3.setString(21, lvMat.getSteelname());
                    psta3.setString(22, lvMat.getStdStyle());
                    psta3.setString(23, lvMat.getStdStyleDesc());
                    psta3.setString(24, lvMat.getSpecCode());
                    psta3.setString(25, lvMat.getSpecName());
                    psta3.setString(26, lvMat.getModelCode());
                    psta3.setString(27, lvMat.getModelDesc());
                    psta3.setString(28, lvMat.getPlineCode());
                    psta3.setString(29, lvMat.getPlineName());
                    psta3.setString(30, lvMat.getActDimater());
                    psta3.setString(31, lvMat.getActHeight());
                    psta3.setString(32, String.valueOf(lvMat.getActLen()));
                    psta3.setString(33, lvMat.getActLenMin());
                    psta3.setString(34, lvMat.getActLenMax());
                    psta3.setString(35, String.valueOf(lvMat.getActCount()));
                    psta3.setString(36, String.valueOf(lvMat.getActWeight()));
                    psta3.setString(37, String.valueOf(lvMat
                            .getActTheoryWeight()));
                    psta3.setString(38, lvMat.getJudgeResultCode());
                    psta3.setString(39, lvMat.getProBc());
                    psta3.setString(40, lvMat.getProBz());

                    if (lvMat.getRpoTime().equals("")) {
                        psta3.setString(41, "");
                    }
                    else {
                        psta3
                                .setString(41, lvMat.getRpoTime().substring(0,
                                        19));
                    }
                    psta3.setString(42, list1.get(10));
                    psta3.setString(43, lvMat.getLocationNo());
                    psta3.setString(44, attrNo);
                    psta3.setString(45, attrName);
                    psta3.setString(46, typeNo);
                    psta3.setString(47, typeName);
                    psta3.setString(48, list1.get(15));
                    psta3.setString(49, list1.get(16));
                    psta3.setString(50, list1.get(7));
                    psta3.setString(51, list1.get(8));
                    psta3.setString(52, list1.get(1));
                    psta3.setString(53, list1.get(12));
                    psta3.setString(54, list1.get(13));
                    psta3.setString(55, list1.get(14));
                    psta3.setString(56, list1.get(19));
                    psta3.setString(57, list1.get(20));
                    psta3.setString(58, list1.get(22));
                    psta3.setString(59, list1.get(23));
                    psta3.setString(60, String.valueOf(lvMat.getSendNum()));
                    psta3.setString(61, String.valueOf(lvMat.getActFixNum()));
                    psta3.setString(62, String.valueOf(lvMat.getActFixLen()));
                    psta3.setString(63, lvMat.getFinishproMold());
                    psta3.setString(64, lvMat.getInMold());
                    psta3.setString(65, list1.get(28));
                    psta3.setString(66, lvMat.getInType());
                    psta3.addBatch();
                    tallSeq++;
                }
                updatebillC.setObject(1, list1.get(0));
                updatebillC.setObject(2, list1.get(2));
                updatebillC.setObject(3, list1.get(3));
                updatebillC.setObject(4, list1.get(5)+list1.get(6));
                updatebillC.setObject(5, list1.get(10));
                updatebillC.setObject(6, list1.get(21));
                updatebillC.setObject(7, list1.get(27));
                updatebillC.setObject(8, list1.get(28));
                updatebillC.setObject(9, String.valueOf(count + 1));
                updatebillC.addBatch();

                psta3.executeBatch();
                updatebillC.executeBatch();
            }

            psta3.close();
            updatebillC.close();

            FrmLoadVehicleMat lvMat = null;
            for (int z = 0; z < list.size(); z++) {
                ArrayList<String> list1 = (ArrayList<String>) list.get(z);
                TallMsqe = querySendSql(connection, list1.get(1));
                int tallSeqM = Integer.parseInt(TallMsqe.substring(TallMsqe
                        .length() - 6, TallMsqe.length()));

                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmTallYsheet.QueryAddSend"); // 查詢出要新增的数据
                PreparedStatement psta4 = connection.prepareStatement(sqlquery);
                psta4.setString(1, list1.get(0));
                psta4.setString(2, list1.get(2)+list1.get(3));
                ResultSet rs4 = psta4.executeQuery();
                lvMat = new FrmLoadVehicleMat();
                if (rs4.next()) {
                    lvMat.setStrTranstpe(rs4.getString(1));
                    lvMat.setStrReceivno(rs4.getString(2));
                    lvMat.setStrReceivnm(rs4.getString(3));
                    lvMat.setStrStationno(rs4.getString(4));
                    lvMat.setStrStationnm(rs4.getString(5));
                    lvMat.setStrSpclno(rs4.getString(6));
                    lvMat.setStrSpclname(rs4.getString(7));
                    lvMat.setStrPort(rs4.getString(8));
                    lvMat.setStrVstel(rs4.getString(9));
                    lvMat.setStrVsteldesc(rs4.getString(10));
                    lvMat.setStrDelivycode(rs4.getString(11));
                    lvMat.setStrDelivyname(rs4.getString(12));
                    lvMat.setStrUnitno(rs4.getString(13));
                    lvMat.setStrUnitname(rs4.getString(14));
                    lvMat.setLoadtype(rs4.getString(15));
                    lvMat.setPrdCls(rs4.getString(16));
                }
                rs4.close();
                psta4.close();

                String attrNo = "";
                String attrName = "";
                String typeNo = "";
                String typeName = "";
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmTallYsheet.queryTager"); // 查詢库位信息
                PreparedStatement psta5 = connection.prepareStatement(sqlquery);
                psta5.setString(1, list1.get(5));
                ResultSet rs5 = psta5.executeQuery();
                if (rs5.next()) {
                    attrNo = rs5.getString(1);
                    attrName = rs5.getString(2);
                    typeNo = rs5.getString(3);
                    typeName = rs5.getString(4);
                }
                rs5.close();
                psta5.close();

                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmTallYsheet.queryTallMAT");// 查询理货主表是否存在该数据
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, list1.get(0));
                psta.setObject(2, list1.get(1));
                psta.setObject(3, list1.get(2)+list1.get(3));
                ResultSet rsTaM = psta.executeQuery();
                if (rsTaM.next()) {
                    updatetallM.setObject(1, df.format(strWeight));
                    updatetallM.setObject(2, strcount);
                    updatetallM.setObject(3, dfLen.format(strLen));
                    // updatetallM.setObject(4, list1.get(7));
                    // updatetallM.setObject(5, list1.get(8));
                    updatetallM.setObject(4, rsTaM.getString(1));
                    updatetallM.addBatch();
                }
                else {
                    psta6.setString(1, list1.get(1)
                            + StringTool.formatDecimal(tallSeqM, "000000"));
                    psta6.setString(2, list1.get(0));
                    psta6.setString(3, list1.get(1));
                    psta6.setString(4, list1.get(2));
                    psta6.setString(5, list1.get(3));
                    psta6.setString(6, list1.get(4));
                    psta6.setString(7, list1.get(6));
                    psta6.setString(8, lvMat.getStrTranstpe());
                    psta6.setString(9, lvMat.getStrReceivno());
                    psta6.setString(10, lvMat.getStrReceivnm());
                    psta6.setString(11, lvMat.getStrStationno());
                    psta6.setString(12, lvMat.getStrStationnm());
                    psta6.setString(13, lvMat.getStrSpclno());
                    psta6.setString(14, lvMat.getStrSpclname());
                    psta6.setString(15, list1.get(16));
                    psta6.setString(16, lvMat.getStrVstel());
                    psta6.setString(17, lvMat.getStrVsteldesc());
                    psta6.setString(18, lvMat.getStrDelivycode());
                    psta6.setString(19, lvMat.getStrDelivyname());
                    psta6.setString(20, lvMat.getStrUnitno());
                    psta6.setString(21, lvMat.getStrUnitname());
                    psta6.setString(22, attrNo);
                    psta6.setString(23, attrName);
                    psta6.setString(24, typeNo);
                    psta6.setString(25, typeName);
                    psta6.setString(26, list1.get(9));
                    psta6.setString(27, list1.get(10));
                    psta6.setString(28, list1.get(7));
                    psta6.setString(29, lvMat.getLoadtype());
                    psta6.setString(30, list1.get(13));
                    psta6.setString(31, list1.get(14));
                    psta6.setString(32, list1.get(15));
                    psta6.setObject(33, df.format(strWeight));
                    psta6.setObject(34, strcount);
                    psta6.setObject(35, list1.get(8));
                    psta6.setObject(36, list1.get(8));
                    psta6.setObject(37, list1.get(11));
                    psta6.setObject(38, list1.get(12));
                    psta6.setObject(39, list1.get(5));
                    psta6.setObject(40, dfLen.format(strLen));
                    psta6.setObject(41, list1.get(17));
                    psta6.setObject(42, list1.get(18));
                    psta6.setObject(43, lvMat.getPrdCls());
                    psta6.addBatch();
                    // tallSeqM++;
                    psta6.executeBatch();
                }
                rsTaM.close();
                psta.close();

                updatebillM.setObject(1, list1.get(0));
                updatebillM.addBatch();
            }

            for (int j = 0; j < pram.size(); j++) {
                ArrayList<String> list1 = (ArrayList<String>) pram.get(j);
                String strCar = "";
                double batchDay = Double.parseDouble(list1.get(11));

                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmTallYsheet.QueryTallCar");
                PreparedStatement psta8 = connection.prepareStatement(sqlquery);
                psta8.setString(1, list1.get(0));
                psta8.setString(2, list1.get(1));
                ResultSet rs4 = psta8.executeQuery();
                if (rs4.next()) {
                    strCar = (rs4.getString(2));
                }
                rs4.close();
                psta8.close();

                if (strCar.equals(list1.get(1))) {
                    cro.setV_errMsg("同一理货单下不能有重复车号,请重新输入车号！");
                    cro.setV_errCode(-1);
                    return cro;
                }
                if (!list1.get(7).equals("")) {
                    if (batchDay >= Double.parseDouble(list1.get(10))) {
                        cro.setV_errMsg("输入的车皮数超过日请车批复数！");
                        cro.setV_errCode(-1);
                        return cro;
                    }
                }

                String preDictNo = "";
                /*if("1".equals(sfjl)) {
                    //获取预报编号
                    preDictNo = getPreDictNo(limsCon);
                }*/

                psta7.setString(1, list1.get(0));
                psta7.setString(2, list1.get(1));
                psta7.setString(3, list1.get(2));
                psta7.setString(4, list1.get(3));
                psta7.setString(5, list1.get(7));
                psta7.setString(6, list1.get(5));
                psta7.setString(7, list1.get(9));
                psta7.setString(8, preDictNo);
                psta7.addBatch();

                if (list1.get(5).equals("60吨车皮")) {
                    updateDay60.setObject(1, list1.get(7));
                    updateDay60.addBatch();
                }
                else if (list1.get(5).equals("70吨车皮")) {
                    updateDay70.setObject(1, list1.get(7));
                    updateDay70.addBatch();
                }
                // 生成计量预报
                ArrayList<String> list2 = (ArrayList<String>) pram1.get(0);
                Date now = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                String systime = dateFormat.format(now);
                Calendar calen = new GregorianCalendar();
                calen.setTime(now);
                calen.add(calen.DATE,1);
                now = calen.getTime();
                String systimeN = dateFormat.format(now);
                if("1".equals(sfjl)){
                    PreTrackScaleEntity preEntity = new PreTrackScaleEntity();
                    preEntity.setPredictionNo(preDictNo);
                    preEntity.setNoticeNo(list1.get(0));
                    preEntity.setCarNo(list1.get(1));
                    preEntity.setMatterNo(ydmBill.get(0).getProduccode());
                    preEntity.setMatterName(ydmBill.get(0).getProduccode());
                    preEntity.setContractNo(list1.get(9));
                    preEntity.setForwardingUnitNo("02108163073");
                    preEntity.setForwardingUnitName("天津钢管制造有限公司");
                    preEntity.setReceivingUintNo(lvMat.getStrReceivno());
                    preEntity.setReceivingUintName(lvMat.getStrReceivno());
                    preEntity.setMaterialNo(ydmBill.get(0).getMaterialNo());
                    preEntity.setMaterialName(ydmBill.get(0).getMaterialName());
                    preEntity.setSpecNo(ydmBill.get(0).getSpecCode());
                    preEntity.setSpecName(ydmBill.get(0).getSpecName());
                    preEntity.setPredictionType("1");
                    preEntity.setUsedType("0");
                    preEntity.setValueFlag("1");
                    preEntity.setCreateManNo("SYSTEM");
                    preEntity.setCreateManName(list2.get(7));
                    preEntity.setCreateTime(systime);
                    preEntity.setMeterTypeNo("001006002");
                    preEntity.setMeterTypeName("销售出厂");
                    preEntity.setEffectiveNum("1");
                    preEntity.setEffectiveStartTime(systime);
                    preEntity.setEffectiveEndTime(systimeN);
                    // 计量预报
                    /*String insertPreDictNoSql = CoreXmlSqlParsersFactory.getSql("PreTrackNotice.InsertPreDictNo");
                    insertPreDictNoSql = EntityTool.fillSqlByEntity(insertPreDictNoSql,preEntity);
                    ps = limsCon.prepareStatement(insertPreDictNoSql);
                    ps.executeUpdate();
                    ps.close();*/
                    // 计量环节
                    /*String insertPreProcessLink = CoreXmlSqlParsersFactory.getSql("PreTrackNotice.InsertPreProcess_LINK");
                    insertPreProcessLink = EntityTool.fillSqlByEntity(insertPreProcessLink, preEntity);
                    ps = limsCon.prepareStatement(insertPreProcessLink);
                    ps.executeUpdate();
                    ps.close();*/
                }
            }


            cro.setResult(1);
            updatetallM.executeBatch();
            updatetallM.close();
            psta7.executeBatch();
            psta7.close();
            updatebillM.executeBatch();
            updatebillM.close();
            updateDay60.executeBatch();
            updateDay60.close();
            updateDay70.executeBatch();
            updateDay70.close();
            cro.setV_errMsg("理货成功!");
            connection.commit();

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

    /***
     * 获取预报编号
     * @param mcsCon
     * @return
     */
    public String getPreDictNo(Connection mcsCon) throws SQLException {
        String preDictNo = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = CoreXmlSqlParsersFactory.getSql("PreTrackNotice.getPreDictNo");
        ps = mcsCon.prepareStatement(sql);
        rs = ps.executeQuery();
        if(rs.next())
        {
            preDictNo = rs.getString("PREDICTION_NO");
        }
        rs.close();
        ps.close();
        return preDictNo;
    }

    /**
     * 作废理货单号整单信息
     * @param list 修改参数
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject upDateAllInfo(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        //Connection limsCon = this.getDao("LimsDao").getConnection();注释天管计量 2024/4/24
        PreparedStatement psta = null;
        PreparedStatement ps = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            //limsCon.setAutoCommit(false); // 取消自动提交事务
            String sqlquery = "";
            String sqlupdate = "";
            String updateBillC = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateBillC1");// 修改提单材料理货状态
            PreparedStatement psta1 = connection.prepareStatement(updateBillC);
            String updateAskDay601 = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateAskDay601");// 修改日请车使用数
            PreparedStatement updateDay60 = connection
                    .prepareStatement(updateAskDay601);
            String updateAskDay701 = CoreXmlSqlParsersFactory
                    .getSql("FrmTallYsheet.updateAskDay701");// 修改日请车使用数
            PreparedStatement updateDay70 = connection
                    .prepareStatement(updateAskDay701);
            for (int j = 0; j < pram.size(); j++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(j);
                // ArrayList<String> loadMats = new ArrayList<String>(); //
                // 保存提货单材料表主键
                // ArrayList<String> billMats = new ArrayList<String>(); //
                // 保存理货单材料号
                //
                // sqlquery =
                // "SELECT DISTINCT T.MAT_NO FROM YDM_ZC_TALLYSHEET_C T "
                // +
                // "LEFT JOIN YDM_ZC_TALLYSHEET_M M ON T.BILL_NO=M.BILL_NO AND T.TALLYSHEET_NO=M.TALLYSHEET_NO"
                // + " WHERE M.BILL_NO="
                // + "'"
                // + list.get(0)
                // + "'"
                // + " AND M.TALLYSHEET_NO="
                // + "'"
                // + list.get(1)
                // + "'"
                // + " AND T.VALIDFLAG !='0'  ORDER BY T.MAT_NO ASC";
                // psta = connection.prepareStatement(sqlquery);
                // ResultSet rs1 = psta.executeQuery();
                // while (rs1.next()) {
                // billMats.add(rs1.getString(1));
                // }
                // rs1.close();
                // psta.close();

                // for (int i = 0; i < billMats.size(); i++) {
                psta1.setObject(1, list.get(0));
                psta1.setObject(2, list.get(1));
                psta1.addBatch();
                psta1.executeBatch();
                // }
                sqlupdate = "DELETE ydm_zc_tallysheet_m  where BILL_NO=" + "'"
                        + list.get(0) + "'" + " AND  TALLYSHEET_NO=" + "'"
                        + list.get(1) + "'" + ""; // 删除理货主表
                Statement sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();

                sqlupdate = "DELETE ydm_zc_tallysheet_c  where BILL_NO=" + "'"
                        + list.get(0) + "'" + " AND  TALLYSHEET_NO=" + "'"
                        + list.get(1) + "'" + ""; // 删除理货材料表
                sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();

                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmTallYsheet.QueryLoadSqlTally");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(0));
                // psta.setString(2, list.get(0));
                ResultSet rslist = psta.executeQuery();
                if (!rslist.next()) {
                    sqlupdate = "UPDATE YDM_ZC_BILL_M SET BILL_STATUS = '1'"
                            + " WHERE BILL_NO='" + list.get(0) + "'"; // 修改提单主表状态
                    Statement sta1 = connection.createStatement();
                    sta1.executeUpdate(sqlupdate);
                    sta1.close();
                }
                rslist.close();
                psta.close();
                if (list.get(4).equals("110502")) {
                    sqlquery = CoreXmlSqlParsersFactory
                            .getSql("FrmTallYsheet.queryAskTallyTM");
                    psta = connection.prepareStatement(sqlquery);
                    psta.setObject(1, list.get(1));
                    ResultSet rs2 = psta.executeQuery();
                    while (rs2.next()) {
                        if ("60吨车皮".equals(rs2.getString(2))) {
                            updateDay60.setObject(1, rs2.getString(1));
                            updateDay60.addBatch();
                        }
                        else {
                            updateDay70.setObject(1, rs2.getString(1));
                            updateDay70.addBatch();
                        }
                    }
                    rs2.close();
                    psta.close();
                }
                // 删除理货车辆信息表
                sqlupdate = "DELETE YDM_ZC_TALLYSHEET_T  WHERE TALLYSHEET_NO = '"
                        + list.get(1) + "' ";
                sta = connection.createStatement();
                sta.executeUpdate(sqlupdate);
                sta.close();

                /*sqlupdate = "UPDATE PRE_TRACK_SCALE SET VALUE_FLAG = '3',DELETE_MAN_NO = 'SYSTEM'," +
                        "DELETE_MAN_NAME = 'SYSTEM' ,MEMO  = '产销撤销理货，自动删除计量预报' WHERE NOTICE_NO = '"
                        + list.get(1) + "' ";
                Statement sta1 = limsCon.createStatement();
                sta1.executeUpdate(sqlupdate);
                sta1.close();*/

            }
            cro.setResult(1);

            updateDay60.executeBatch();
            updateDay70.executeBatch();
            psta1.close();
            connection.commit();
            //limsCon.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
            /*if (limsCon != null) {
                try {
                    limsCon.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }*/
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
                /*if (limsCon != null && !limsCon.isClosed()) {
                    limsCon.close();
                }*/
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }
}
