package com.genlot.nativesdk.sender;

import android.text.TextUtils;

import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.model.CopyLotteryBean;
import com.genlot.nativesdk.model.GameCashTicket;
import com.genlot.nativesdk.model.GameHistoryNumber;
import com.genlot.nativesdk.model.GameRuntimeAttr;
import com.genlot.nativesdk.model.GameTicket;
import com.genlot.nativesdk.model.GameWinBulletin;
import com.genlot.nativesdk.model.GameWinLevel;
import com.genlot.nativesdk.model.GameWinPool;
import com.genlot.nativesdk.model.OpBean;
import com.genlot.nativesdk.model.ReportBankPayment;
import com.genlot.nativesdk.model.ReportData;
import com.genlot.nativesdk.model.ReportItem;
import com.genlot.nativesdk.model.WinInfo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * @author ZhuYongKang
 * @description 交易返回结果解析类
 * @date 2019/8/15
 */
public class TCPParser {

    private static final String DELIMITER1 = ";";
    private static final String DELIMITER2 = "|";

    public static String parseNString(String data, String str, int nIndex) {
        int count = 0;
        int index = 0;

        if (nIndex <= 0 || data == null || str == null) {
            return null;
        }
        StringBuilder des = new StringBuilder(data);
        String result = null;
        while (true) {
            index = des.indexOf(str);
            if (index < 0) {
                result = null;
                break;
            } else {
                count++;
                if (count == nIndex) {
                    result = des.substring(0, index);
                    break;
                } else {
                    des.delete(0, index + str.length());
                }
            }
        }
        return result;
    }


    static boolean t101027Unpack(String data, String money) {
        try {
            int index = 1;
            //系统备份标记
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            //强制通信线路
            index++;
            //数据链路模式0－短联接，1－长联接，2－自动
            index++;
            //心跳时间间隔
            index++;
            //数据链路保持联接时间
            index++;
            //服务器时间
            index++;
            //文件下载标记
            index++;
            //打印机切纸模式0－手工设置，1－定长，2－不定长
            index++;
            //是否打印兑奖单
            index++;
            //是否打印注销单
            index++;
            //是否打印冲正单
            index++;
            //半票确认
            index++;
            //交易确认模式
            index++;
            //游戏个数
            index++;
            //游戏ID串
            index++;
            //通信参数1
            index++;
            //通信参数2
            index++;
            //通信参数3
            index++;
            //通信参数4
            index++;
            //通信参数5
            index++;
            //通信参数6
            index++;
            //操作员认证方式
            index++;
            //投注机认证方式
            index++;
            //即时消息标记，0－没有，>0表示有消息的数量
            index++;
            //单票保护金额
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            money = result;
            //工号9密码
            index++;
            //通信参数7
            index++;
            //通信参数8
            index++;
            //通信参数9
            index++;
            //通信参数10
            index++;
            //通信参数11
            index++;
            //通信参数12
            index++;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 查询游戏信息数据解析
     *
     * @param data   交易报文
     * @param gameID 游戏ID
     * @param attr   游戏数据
     * @return true or false
     */
    static boolean t101005Unpack(String data, int gameID, GameRuntimeAttr attr) {
        try {
            int index = 1;
            //游戏ID
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (gameID != Integer.parseInt(result)) {
                return false;
            }
            attr.mGameID = gameID;
            //游戏类型
            index++;
            //游戏有效性
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mIsValid = Byte.parseByte(result);
            //游戏参数版本号
            index++;
            //开始销售时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mSaleSTime = Long.parseLong(result);
            //结束销售时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mSaleETime = Long.parseLong(result);
            //开奖时间（多字段，用"|"分隔）
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            result = parseNString(result, DELIMITER2, 1);
            if (result == null) {
                return false;
            }
            attr.mDrawTime = Long.parseLong(result);
            //开奖期号（多字段，用"|"分隔）
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            result = parseNString(result, DELIMITER2, 1);
            if (result == null) {
                return false;
            }
            attr.mGameIssue = Long.parseLong(result);
            //销售权限
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mIsAllowSale = Byte.parseByte(result);
            //兑奖权限
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mIsAllowCash = Byte.parseByte(result);
            //注销权限
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mIsAllowCancel = Byte.parseByte(result);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * 查询游戏流水号报文解析
     *
     * @param data  交易报文数据
     * @param snMap 游戏数据
     * @return true or false
     */
    static boolean t101022Unpack(String data, HashMap<Integer, String> snMap) {
        try {
            int index = 1;
            //报文段数
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            int size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                //游戏ID
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                int gameID = Integer.parseInt(result);
                //游戏流水号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                snMap.put(gameID, result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * 查询账户余额报文解析
     *
     * @param data  交易报文
     * @param value 账户余额及信用额度
     * @return true or false
     */
    static boolean t101026Unpack(String data, double[] value) {
        try {
            int index = 1;
            //账户余额
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            value[0] = Double.parseDouble(result);
            //账户信用额度
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            value[1] = Math.abs(Double.parseDouble(result));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 投注报文解析
     *
     * @param data  交易报文
     * @param ticket GameTicket
     * @return true or false
     */
    static boolean t101200Unpack(String data, GameTicket ticket) {
        try {
            int index = 1;
            //投注时间
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mBettingTime = Long.parseLong(result);
            //账户余额
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            double total = Double.parseDouble(result);

            //报文段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            int size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                //票流水号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                if (ticket.mTicketSN != Integer.parseInt(result)) {
                    return false;
                }

                //公益金提示语
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                if (!TextUtils.isEmpty(result)) {
                    ticket.mWelfareFundTipsString = result;
                } else {
                    ticket.mWelfareFundTipsString = "";
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 兑奖报文解析
     * @param data  交易报文
     * @return  兑奖时间
     */
    static long t101009Unpack(String data) {
        try {
            int index = 1;
            //兑奖时间
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return 0;
            }
            return Long.parseLong(result);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 注销报文解析
     * @param data  注销报文
     * @return  注销时间
     */
    static long t101300Unpack(String data) {
        try {
            int index = 1;
            //注销时间
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return 0;
            }
            return Long.parseLong(result);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 冲正报文解析
     * @param data  冲正报文
     * @return  冲正时间
     */
    static long t101999Unpack(String data) {
        try {
            int index = 1;
            //冲正时间
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return 0;
            }
            return Long.parseLong(result);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 查询报文解析
     * @param data  报文
     * @param ticket    GameTicket
     * @return  true or false
     */
    static boolean t101007Unpack(String data, GameTicket ticket) {
        try {
            int index = 1;
            //终端编号
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mTerminalID = Long.parseLong(result);
            //游戏期号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mGameIssue = Long.parseLong(result);
            //游戏ID
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mGameID = Integer.parseInt(result);
            //票流水号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mTicketSN = Long.parseLong(result);
            //销售期号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mSaleIssue = Long.parseLong(result);
            //工号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mOperatorID = result;
            //投注金额
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mBettingMoney = Double.parseDouble(result);
            //投注时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mBettingTime = Long.parseLong(result);
            //票状态
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mValid = Integer.parseInt(result);
            //注销时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (result.length() > 0) {
                ticket.mCancelTime = Long.parseLong(result);
            }
            //注销的终端编号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (result.length() > 0) {
                ticket.mCancelTermID = Long.parseLong(result);
            }
            //注数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mBets = Integer.parseInt(result);
            //期数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mMultiIssue = Integer.parseInt(result);
            //积分卡号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            ticket.mMemberID = result;
            //报文段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            int size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                //注序号
                index++;
                //投注方式
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                ticket.mBettingMode[i] = Integer.parseInt(result);
                //游戏玩法
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                ticket.mPlayType[i] = Integer.parseInt(result);
                //倍数
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                ticket.mMultiTimes[i] = Integer.parseInt(result);
                //投注号码
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                ticket.gameBetDataString[i] = result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 中奖查询报文解析
     * @param data  报文
     * @param cashTicket  GameCashTicket
     * @return  true or false
     */
    static boolean t101008Unpack(String data, GameCashTicket cashTicket) {
        try {
            int index = 1;
            //终端编号
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mTerminalID = Long.parseLong(result);
            //游戏期号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mCashIssue = Long.parseLong(result);
            //游戏ID
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mGameID = Integer.parseInt(result);
            //票流水号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mTicketSN = Long.parseLong(result);
            //票中奖金额
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mPrizeMoney = Double.parseDouble(result);
            //票中奖税额
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mTaxMoney = Double.parseDouble(result);
            //投注时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (result.length() > 0) {
                cashTicket.mBettingTime = Long.parseLong(result);
            }

            //票状态
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mStatus = Integer.parseInt(result);
            //兑奖时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (result.length() > 0) {
                cashTicket.mCashTime = Long.parseLong(result);
            }
            //兑奖的终端编号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (result.length() > 0) {
                cashTicket.mCashTermID = Long.parseLong(result);
            }
            //兑奖截止时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            cashTicket.mCashEndTime = Long.parseLong(result);
            //票面金额
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (result.length() > 0) {
                cashTicket.mTicketMoney = Double.parseDouble(result);
            }

            //报文段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            int size = Integer.parseInt(result);
            cashTicket.mTotalCount = size;
            for (int i = 0; i < size; i++) {
                //注序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                cashTicket.mBetSN[i] = Integer.parseInt(result);
                //开奖序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                cashTicket.mDrawSN[i] = Integer.parseInt(result);
                //中奖奖等
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                cashTicket.mWinLevelName[i] = result;
                //中奖注数
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                cashTicket.mWinLevelCount[i] = Integer.parseInt(result);
                //单注中奖金额
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                cashTicket.mPrizeBet[i] = Double.parseDouble(result);
                //倍数
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                cashTicket.mMultiTimes[i] = Integer.parseInt(result);
                //单注中奖税额
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                cashTicket.mTaxBet[i] = Double.parseDouble(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 时段报表查询报文解析
     * @param data  报文
     * @param report  ReportData
     * @return  true or false
     */
    static boolean t101412Unpack(String data, ReportData report) {
        try {
            int index = 1;
            String str;
            //应缴款
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mPayment = str;
            //已缴款
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mHadPaid = str;
            //欠缴款
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mArrearage = str;
            //实时余额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mBalance = str;

            //信用额度
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mCredit = str;
            //基本通信费
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mBaseCommFee = str;
            //销量通信费
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mSaleCommFee = str;
            //注销补偿款
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mRetrieveAmount = str;
            //汇总标记
            index++;
            //报文段数
            int count = 0;
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            count = Integer.parseInt(str);
            for (int i = 0; i < count; i++) {
                ReportItem reportItem = new ReportItem();
                //游戏ID
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mGameID = Integer.parseInt(str);
                //总销售票数
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mSaleSize = Integer.parseInt(str);
                //总销售金额
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mSaleMoney = Double.parseDouble(str);
                //注销票票数
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mCancelSize = Integer.parseInt(str);
                //注销票金额
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mCancelMoney = Double.parseDouble(str);
                //兑奖票票数
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mCashSize = Integer.parseInt(str);
                //兑奖票金额
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mCashMoney = Double.parseDouble(str);
                //中心注销票票数
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mCenterCancelSize = Integer.parseInt(str);
                //中心注销票金额
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mCenterCancelMoney = Double.parseDouble(str);
                //冲正票票数
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mReverseSize = Integer.parseInt(str);
                //冲正票金额
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mReverseMoney = Double.parseDouble(str);
                //代销费
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mSalesCommission = Double.parseDouble(str);
                //代兑费
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                reportItem.mCashingBonus = Double.parseDouble(str);
                //实销 = 销量-中心销量-冲正-注销；
                reportItem.mActualSaleSize = reportItem.mSaleSize - reportItem.mCenterCancelSize - reportItem.mReverseSize - reportItem.mCancelSize;
                reportItem.mActualSaleMoney = reportItem.mSaleMoney - reportItem.mCenterCancelMoney - reportItem.mReverseMoney - reportItem.mCancelMoney;
                report.mReportItem.add(reportItem);
            }
            //其它发放款
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mOtherIncomeMoney = str;
            //其它缴费
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            report.mOtherOutgoMoney = str;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    /**
     * 期报表查询报文解析
     * @param data  报文
     * @param reportItem  ReportItem
     * @return  true or false
     */
    static boolean t101401Unpack(String data, ReportItem reportItem) {
        try {
            int index = 1;
            //总销售票数
            String str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mSaleSize = Integer.parseInt(str);
            //总销售金额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mSaleMoney = Double.parseDouble(str);
            //注销票票数
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mCancelSize = Integer.parseInt(str);
            //注销票金额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mCancelMoney = Double.parseDouble(str);
            //兑奖票票数
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mCashSize = Integer.parseInt(str);
            //兑奖票金额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mCashMoney = Double.parseDouble(str);
            //中心注销票票数
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mCenterCancelSize = Integer.parseInt(str);
            //中心注销票金额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mCenterCancelMoney = Double.parseDouble(str);
            //冲正票票数
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mReverseSize = Integer.parseInt(str);
            //冲正票金额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            reportItem.mReverseMoney = Double.parseDouble(str);

            reportItem.mActualSaleSize = reportItem.mSaleSize - reportItem.mCenterCancelSize - reportItem.mReverseSize - reportItem.mCancelSize;
            reportItem.mActualSaleMoney = reportItem.mSaleMoney - reportItem.mCenterCancelMoney - reportItem.mReverseMoney - reportItem.mCancelMoney;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    static boolean t101404Unpack(String data, ReportBankPayment bankPayment) {
        try {
            int index = 1;
            String str;
            //缴款总额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            bankPayment.PayedAmount = str;
            //账户余额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            bankPayment.StationAmount = str;
            //报文段数
            int count = 0;
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            count = Integer.parseInt(str);
            for (int i = 0; i < count; i++) {
                ReportBankPayment.BankPayment payment = new ReportBankPayment.BankPayment();
                //缴款时间
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                payment.PayedTime = str;
                //缴款金额
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                payment.PayedMoney = str;
                bankPayment.bankPaymentList.add(payment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 本机中奖信息报文解析
     *
     * @param data 报文
     * @param infos WinInfo集合
     * @return 中奖信息
     */
    static boolean t101451Unpack(String data, ArrayList<WinInfo> infos) {
        int index = 1;
        String str;
        int count;
        str = parseNString(data, DELIMITER1, index++);
        if (str == null) {
            return false;
        }
        try {
            count = Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        for (int i = 0; i < count; i++) {
            WinInfo info = new WinInfo();
            //开奖序号不需要
            index++;
            //奖等序号
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            info.winLevelName = str;
            //中奖注数
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            info.winCount = str;
            //中奖金额
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            info.winAmount = str;
            infos.add(info);
        }
        return true;
    }

    /**
     * 查询工号列表报文解析
     * @param data  报文
     * @return  boolean
     */
    static boolean t101043Unpack(String data, List<OpBean> list) {
        try {
            int index = 1;
            String str;
            //报文段数
            int count = 0;
            str = parseNString(data, DELIMITER1, index++);
            if (str == null) {
                return false;
            }
            count = Integer.parseInt(str);
            for (int i = 0; i < count; i++) {
                OpBean bean = new OpBean();
                //操作员编号
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                bean.OpID = str;
                //操作员密码
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                bean.OpPassword = str;
                //操作员级别
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                bean.OpLevel = str;
                //操作员姓名
                str = parseNString(data, DELIMITER1, index++);
                if (str == null) {
                    return false;
                }
                bean.OpName = str;
                list.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 获取新期(短周期)报文解析
     * @param data  报文
     * @param attr  GameRuntimeAttr
     * @return  是否成功
     */
    static boolean t101453Unpack(String data, GameRuntimeAttr attr) {
        try {
            int index = 1;
            //开始销售时间
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mSaleSTime = Long.parseLong(result);
            //结束销售时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mSaleETime = Long.parseLong(result);
            //开奖时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mDrawTime = Long.parseLong(result);
            //游戏期号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mGameIssue = Long.parseLong(result);
            //游戏流水号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            attr.mGameSN = Long.parseLong(result);
//            //游戏结束标记
//            index++;
//            index = 9;
//            result = parseNString(data, DELIMITER1, index);
//            if (result == null) {
//                LogTool.logE(LogTool.tag(), "jackpot 错误");
//                return false;
//            }
//            attr.mJckport = String.valueOf(result);
//            Log.i("www", "mjackport:" + attr.mJckport);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        attr.mIssueDate = data;
        return true;
    }

    /**
     * 获取开奖号码报文解析
     * @param data  报文
     * @param gameID  游戏ID
     * @param issue  游戏期号
     * @param list   String
     * @return      是否成功
     */
    static boolean t101454Unpack(String data, int gameID, long issue, ArrayList<String> list) {
        try {
            int index = 1;
            //游戏ID
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (gameID != Integer.parseInt(result)) {
                return false;
            }
            //期号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (issue != Long.parseLong(result)) {
                return false;
            }
            //报文段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            int size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                //开奖序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                //开奖号码序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                //开奖号码
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                list.add(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 获取开奖公告报文解析
     * @param data  报文
     * @param gameID    游戏ID
     * @param issue     游戏期号
     * @param winBulletin   GameWinBulletin
     * @return      是否成功
     */
    static boolean t101455Unpack(String data, int gameID, long issue, GameWinBulletin winBulletin) {
        try {
            int index = 1;
            //游戏ID
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (gameID != Integer.parseInt(result)) {
                return false;
            }
            winBulletin.mGameID = gameID;
            //期号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            if (issue != Long.parseLong(result)) {
                return false;
            }
            winBulletin.mGameIssue = issue;
            //总销量
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            winBulletin.mAllSales = Double.parseDouble(result);
            //游戏名称
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            winBulletin.mGameName = result;
            //报文段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            int size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                //开奖序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                //开奖号码序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                //开奖号码
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                if (result.equals("*")) {
                    winBulletin.mWinNumber[i] = 255;
                } else {
                    winBulletin.mWinNumber[i] = Integer.parseInt(result);
                }
            }
            //报文段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                GameWinPool winPool = new GameWinPool();
                //奖池编号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winPool.mWinPoolID = Integer.parseInt(result);
                //奖池名称
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winPool.mWinPoolName = result;
                //奖池销量
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winPool.mWinPoolSales = Double.parseDouble(result);
                //奖池余额
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winPool.mLeft = Double.parseDouble(result);
                winBulletin.mWinPool.add(winPool);
            }
            //报文段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                GameWinLevel winLevel = new GameWinLevel();
                //开奖序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                //奖等
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winLevel.mWinLevel = Integer.parseInt(result);
                //奖等名称
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winLevel.mWinLevelName = result;
                //中奖注数
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winLevel.mWinSize = Integer.parseInt(result);
                //奖等奖金
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                winLevel.mWinBonus = Double.parseDouble(result);
                winBulletin.mWinLevel.add(winLevel);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 查询历史开奖号码报文解析
     * @param data  报文
     * @param numberList    历史开奖号码列表
     * @return   是否成功
     */
    static boolean t101459Unpack(String data, ArrayList<GameHistoryNumber> numberList) {
        try {
            String firstIssue = "";
            int index = 1;
            //游戏ID
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            //段数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            int size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                GameHistoryNumber number = new GameHistoryNumber();
                //期号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                if (i == 0) {
                    firstIssue = result;
                    number.mGameIssue = Long.parseLong(firstIssue);
                } else {
                    int length = firstIssue.length() - result.length();
                    String issue = firstIssue.substring(0, length) + result;
                    number.mGameIssue = Long.parseLong(issue);
                }

                //开奖号码
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                number.mWinNumber = result;
                numberList.add(number);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 取JACKPOT实时累计奖励报文解析
     * @param data 报文
     * @return  string
     */
    static String t101462Unpack(String data) {
        String result;
        try {
            int index = 1;
            //开始销售时间
            result = parseNString(data, DELIMITER1, index++);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    /**
     * 复制票报文解析
     * @param data  报文
     * @param bean  CopyLotteryBean
     * @return  是否成功
     */
    static boolean t103007Unpack(String data, CopyLotteryBean bean) {
        try {
            int index = 1;
            //票面投注机号
            String result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strTicketStationID = result;
            //开奖期号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strIssue = result;
            //游戏ID
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strGameID = result;
            //票流水号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strTSN = result;
            //投注期号（多期的时候才会有不同）
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strWagerIssue = result;
            //销售此票的操作员工号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strSaleOperatorID = result;
            //投注金额
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strParValue = result;
            //投注时间，格式为（YYYYMMDDHHMMSS）
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strWagerTime = result;
            //票状态（0－投注机注销，1－冲正，2－中心注销，3－投注，4－投注机兑奖，5－中心兑奖，6－已弃奖）
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strTicketStat = result;
            //注销时间
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strCancelTime = result;
            //注销此票的投注机号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strCancelStationID = result;
            //注数（指打印的行数）
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strBetPerTicket = result;
            //多期期数
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strMultiIssue = result;
            //积分卡号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strVIPID = result;
            //积分卡号
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.strVIPID = result;
            //多笔标志
            result = parseNString(data, DELIMITER1, index++);
            if (result == null) {
                return false;
            }
            bean.gameCopyInfoBeans = new ArrayList<>();
            int size = Integer.parseInt(result);
            for (int i = 0; i < size; i++) {
                CopyLotteryBean.GameCopyInfoBean infoBean = new CopyLotteryBean.GameCopyInfoBean();
                //注序号
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                infoBean.strBetSN = result;

                //投注方式
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                infoBean.strWagerMode = result;

                //游戏玩法
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                infoBean.strPlayType = result;

                //倍数
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                infoBean.strMultiTimes = result;

                //投注号码
                result = parseNString(data, DELIMITER1, index++);
                if (result == null) {
                    return false;
                }
                infoBean.strBetNumber = result;

                bean.gameCopyInfoBeans.add(infoBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
