package com.desfate.chart.ui.old.OTC.datas;


import com.desfate.chart.ui.old.adapter.HistoryTik;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;


/**
 * 实时行情对象
 *
 * @author yongzq
 */
public class QuoteTik {
    // 交易所代码
    private int exchangeCode;

    // 商品代码
    private String goodsCode;

    // 行情时间
    private Date atTime;

    // 商品组
    private int sort;

    // 字段字典
    private HashMap<Byte, Object> fields = new HashMap<Byte, Object>();

    // 标识符
    private String symbol;

    // 小数位
    private byte decimalPlace;

    // 日期
    private String date;

    // 时间
    private String time;

    //权力金
    private String stickPrice;

    //涨跌值，与昨结算、昨收比较
    private String strChange;

    // 涨跌百分比，和昨收或昨结算比较
    private String strPctChange;

    //涨跌值 ，与开盘时的价格比较
    private String strChangeSinceOpen;


    public String getStrChange() {
        return strChange;
    }

    public void setStrChange(String strChange) {
        this.strChange = strChange;
    }

    public String getStrPctChange() {
        return strPctChange;
    }

    public void setStrPctChange(String strPctChange) {
        this.strPctChange = strPctChange;
    }

    public String getStrChangeSinceOpen() {
        return strChangeSinceOpen;
    }

    public void setStrChangeSinceOpen(String strChangeSinceOpen) {
        this.strChangeSinceOpen = strChangeSinceOpen;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
        setAtTime();
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
        setAtTime();
    }

    public int getExchangeCode() {
        return this.exchangeCode;
    }

    public void setExchangeCode(int param) {
        this.exchangeCode = param;
    }

    public String getGoodsCode() {
        return this.goodsCode;
    }

    public void setGoodsCode(String param) {
        this.goodsCode = param;
    }

    public Date getAtTime() {
        return this.atTime;
    }

    public void setAtTime(Date param) {
        this.atTime = param;
    }

    public String getSymbol() {
        if (symbol == null) {
            symbol = exchangeCode + "_" + goodsCode;
        }
        return this.symbol;
    }

    private void setSymbol(String param) {
        this.symbol = param;
    }

    public int getSort() {
        return this.sort;
    }

    public void setSort(int param) {
        this.sort = param;
    }

    public HashMap<Byte, Object> getFields() {
        return this.fields;
    }

    public void addField(Byte key, Object value) {
        fields.put(key, value);
    }

    public byte getDecimalPlace() {
        return this.decimalPlace;
    }

    public void setDecimalPlace(byte param) {
        this.decimalPlace = param;
    }

    public String getStickPrice() {
        return stickPrice;
    }

    public void setStickPrice(String stickPrice) {
        this.stickPrice = stickPrice;
    }

    public String getAsk() {
        if (fields.containsKey(QuoteTikPart.Ask)) {
            return fields.get(new Byte(QuoteTikPart.Ask)).toString();
        }

        return null;
    }

    public String getBid() {
        if (fields.containsKey(QuoteTikPart.Bid)) {
            return fields.get(new Byte(QuoteTikPart.Bid)).toString();
        }

        return null;
    }

    public String getValue(byte key) {
        if (fields.containsKey(key)) {
            return fields.get(new Byte(key)).toString();
        }

        return null;
    }

    private void setAtTime() {
        if (date != null && !date.isEmpty() && date.length() == 8 && time != null && !time.isEmpty()
                && time.length() == 6) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
            try {
                setAtTime(dateFormat.parse(date + " " + time));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return "QuoteTik [exchangeCode=" + exchangeCode + ", goodsCode=" + goodsCode + ", atTime=" + atTime + ", sort="
                + sort + ", fields=" + fields + ", symbol=" + symbol + ", decimalPlace=" + decimalPlace + ", date="
                + date + ", time=" + time + "]";
    }


    private static int subsecond = 60 * 60 * 8;

    private static String getGoodsCode(String[] fields) {
        String goodsCode = "";
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].contains(String.copyValueOf(new char[]{(char) QuoteTikPart.GoodsCode}))) {
                int index = fields[i].indexOf(String.copyValueOf(new char[]{(char) QuoteTikPart.ValueStart}));
                if (index > 0) {
                    String value = fields[i].substring(index + 1).trim();
                    return value;
                }
            }
        }

        return goodsCode;
    }

    public static ArrayList<byte[]> breakupArray(byte[] message) {
        // 单行 0x10 ...0x01 key 0x02 value ... 0x11
        ArrayList<byte[]> content = new ArrayList<>();
        // 按每行拆分
        for (int i = 0; i < message.length; i++) {

            if (message[i] == 0x11) {
                int length = 0;
                if (content.size() > 0) {
                    for (int j = 0; j < content.size(); j++) {
                        length += content.get(j).length;
                    }
                }
                byte[] line = new byte[i + 1 - length];

                // 目标数组 从0到数组长度
                System.arraycopy(message, length, line, 0, line.length);
                content.add(line);
            }
        }
        return content;
    }

    /**
     * 解析盘面报文
     */
    public static QuoteTik[] analysis(byte[] message) throws Exception {
        if (message == null || message.length == 0) {
            return null;
        }

        ArrayList<QuoteTik> quoteList = new ArrayList<QuoteTik>();

        // 遍历key ，value
        for (byte[] bytes : breakupArray(message)) {
            try {
                QuoteTik tik = new QuoteTik();
                for (int j = 0; j < bytes.length; j++) { // 每一行数据解析
                    if (bytes[j] == 0x01) {
                        // 找到0x01后面的第一个2就是key值
                        for (int k = j + 1; k < bytes.length; k++) {
                            if (bytes[k] == 0x02) {
                                byte[] key = new byte[k - j - 1];
                                System.arraycopy(bytes, j + 1, key, 0, key.length);
                                // 找到key之后继续查找value
                                byte[] value = null;
                                for (int l = k + 1; l < bytes.length; l++) {
                                    if (bytes[l] == 0x01 || bytes[l] == 0x11) {
                                        value = new byte[l - k - 1];
                                        System.arraycopy(bytes, k + 1, value, 0, value.length);
                                        j = l - 1;
                                        break;
                                    }
                                }
                                // 根据key赋值
                                if (value != null) {
                                    String _value = new String(value);
                                    switch (key[0]) {
                                        case QuoteTikPart.GoodsCode:// 商品代码
                                            tik.setGoodsCode(_value);
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.ExchangeCode:
                                            tik.setExchangeCode(Integer.parseInt(_value.trim()));
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.Opened:
                                        case QuoteTikPart.PreSettle:
                                        case QuoteTikPart.Settle:
                                        case QuoteTikPart.Last:
                                        case QuoteTikPart.PreClose:
                                        case QuoteTikPart.Lowest:
                                        case QuoteTikPart.Highest:
                                        case QuoteTikPart.Bid:
                                        case QuoteTikPart.Bid2:
                                        case QuoteTikPart.Bid3:
                                        case QuoteTikPart.Bid4:
                                        case QuoteTikPart.Bid5:
                                        case QuoteTikPart.Ask:
                                        case QuoteTikPart.Ask2:
                                        case QuoteTikPart.Ask3:
                                        case QuoteTikPart.Ask4:
                                        case QuoteTikPart.Ask5:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID6:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID7:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID8:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID9:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID10:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK6:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK7:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK8:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK9:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK10:
                                        case QuoteTikPart.LimitHigh:
                                        case QuoteTikPart.LimitLow:
                                            tik.addField(key[0], Math.abs(Double.parseDouble(toTruePrice(_value, tik.getGoodsCode()))));
                                            break;

                                        case QuoteTikPart.TotalTurnover:
                                            tik.addField(key[0], _value);

                                            break;

                                        case QuoteTikPart.BidVolume:
                                        case QuoteTikPart.BidVolume2:
                                        case QuoteTikPart.BidVolume3:
                                        case QuoteTikPart.BidVolume4:
                                        case QuoteTikPart.BidVolume5:
                                        case QuoteTikPart.AskVolume:
                                        case QuoteTikPart.AskVolume2:
                                        case QuoteTikPart.AskVolume3:
                                        case QuoteTikPart.AskVolume4:
                                        case QuoteTikPart.AskVolume5:
                                        case QuoteTikPart.LastVolume:
                                            tik.addField(key[0], Math.abs((int) Double.parseDouble(_value)));
                                            break;
                                        case QuoteTikPart.BidQueueInfo: // 买委托
                                        case QuoteTikPart.AskQueueInfo: // 卖委托
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.TotalVolume:
                                            tik.addField(key[0], Math.abs((int) Double.parseDouble(_value)));
                                            break;
                                        case QuoteTikPart.HoldIncrement:
                                            tik.addField(key[0], Integer.parseInt(_value));
                                            break;
                                        case QuoteTikPart.OrderId:
                                            try {
                                                tik.addField(QuoteTikPart.OrderId, Integer.parseInt(_value));
                                            } catch (Exception e) {
                                                tik.addField(QuoteTikPart.OrderId, Double.parseDouble(_value));
                                            }
                                            break;
                                        case QuoteTikPart.PreHoldVolume:
                                        case QuoteTikPart.HoldVolume:// 持仓量
                                            tik.addField(key[0], Double.parseDouble(_value));
                                            break;
                                        case QuoteTikPart.Strike:
                                            tik.setStickPrice(toTruePrice(_value, tik.getGoodsCode()));
                                            tik.addField(key[0], Double.parseDouble(_value));
                                            System.out.println("addField = " + _value);
                                            break;
                                        case QuoteTikPart.Time:
                                            tik.setTime(_value.substring(0, 6));
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.Date:
                                            tik.setDate(_value.substring(0, 8));
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.DecimalPlace:
                                            int decimalplace = Math.abs(Integer.parseInt(_value.toString()));
                                            if (decimalplace > 4) {
                                                decimalplace = 4;
                                            }

                                            tik.setDecimalPlace((byte) decimalplace);
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.Inventory:
                                            tik.addField(key[0], Double.parseDouble(_value));
                                            break;
                                        // 挂牌期权增加
                                        case QuoteTikPart.PutOptionPremiums:
                                        case QuoteTikPart.PutOptionPremiums2:
                                        case QuoteTikPart.PutOptionPremiums3:
                                        case QuoteTikPart.PutOptionPremiums4:
                                        case QuoteTikPart.PutOptionPremiums5:
                                        case QuoteTikPart.CallOptionPremiums:
                                        case QuoteTikPart.CallOptionPremiums2:
                                        case QuoteTikPart.CallOptionPremiums3:
                                        case QuoteTikPart.CallOptionPremiums4:
                                        case QuoteTikPart.CallOptionPremiums5:
                                            // 强制两位小数
                                            tik.addField(key[0], Math.abs(Double.parseDouble(String.valueOf(Double.parseDouble(_value) / Math.pow(10, 2)))));
                                            //tik.addField(key[0],Double.parseDouble(_value));
                                            break;
                                        case QuoteTikPart.strAskOrder:
                                        case QuoteTikPart.strAskOrder2:
                                        case QuoteTikPart.strAskOrder3:
                                        case QuoteTikPart.strAskOrder4:
                                        case QuoteTikPart.strAskOrder5:
                                        case QuoteTikPart.strBidOrder:
                                        case QuoteTikPart.strBidOrder2:
                                        case QuoteTikPart.strBidOrder3:
                                        case QuoteTikPart.strBidOrder4:
                                        case QuoteTikPart.strBidOrder5:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID_ORDER6:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID_ORDER7:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID_ORDER8:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID_ORDER9:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID_ORDER10:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK_ORDER6:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK_ORDER7:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK_ORDER8:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK_ORDER9:
                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK_ORDER10:
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.ExchangeTime: //当前交易日
                                            tik.addField(key[0], _value);
                                            break;
                                        case QuoteTikPart.ChangeFromOpen:
                                            tik.addField(key[0],_value);
                                            tik.setStrChangeSinceOpen(_value);
                                            break;
                                        case QuoteTikPart.ChangePercent:
                                            tik.addField(key[0],_value);
                                            tik.setStrPctChange(_value);
                                            break;
                                        case QuoteTikPart.Change:
                                            tik.addField(key[0],_value);
                                            tik.setStrChange(_value);
                                            break;
                                        default: // 新加的字段都不需要在添加解析了，除非比较特殊的，需要转化或者计算的。直接在BidQuoteDay或者MMQuoteDay类中的toDayQuote解析出来就行了
                                            tik.addField(key[0], _value);
                                            break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
                quoteList.add(tik);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return quoteList.toArray(new QuoteTik[quoteList.size()]);
    }

    // 保存原有解析方法
    public static QuoteTik[] analysis(byte[] message, int _value) throws Exception {
        if (message == null || message.length == 0) {
            return null;
        }

        ArrayList<QuoteTik> quoteList = new ArrayList<QuoteTik>();
        String quoteStrings = new String(message, "utf-8");
        quoteStrings = quoteStrings.replace("\0", "");
        String[] quoteStringArray = quoteStrings
                .split(String.copyValueOf(new char[]{(char) QuoteTikPart.PriceSplit}));
        for (String item : quoteStringArray) {
            if (item.length() < 2) {
                continue;
            }
            try {
                QuoteTik tik = new QuoteTik();
                String[] fields = item.split(String.copyValueOf(new char[]{(char) QuoteTikPart.FieldStart}));
                String goodsCode = getGoodsCode(fields);
                for (String tagValue : fields) {
                    try {
                        int index = tagValue.indexOf(String.copyValueOf(new char[]{(char) QuoteTikPart.ValueStart}));
                        if (index > 0) {
                            String fieldName = tagValue.substring(0, index);
                            String value = tagValue.substring(index + 1).trim();
                            byte partic = (byte) (fieldName.toCharArray()[0]);
                            Byte particB = new Byte(partic);
                            switch (partic) {
                                case QuoteTikPart.GoodsCode:
                                    tik.setGoodsCode(value.trim());
                                    tik.addField(new Byte(partic), value);
                                    break;
                                case QuoteTikPart.ExchangeCode:
                                    tik.setExchangeCode(Integer.parseInt(value.trim()));
                                    tik.addField(new Byte(partic), value);
                                    break;
                                case QuoteTikPart.Opened:
                                case QuoteTikPart.PreSettle:
                                case QuoteTikPart.Settle:
                                case QuoteTikPart.Last:
                                case QuoteTikPart.PreClose:
                                case QuoteTikPart.Lowest:
                                case QuoteTikPart.Highest:
                                case QuoteTikPart.Bid:
                                case QuoteTikPart.Bid2:
                                case QuoteTikPart.Bid3:
                                case QuoteTikPart.Bid4:
                                case QuoteTikPart.Bid5:
                                case QuoteTikPart.Ask:
                                case QuoteTikPart.Ask2:
                                case QuoteTikPart.Ask3:
                                case QuoteTikPart.Ask4:
                                case QuoteTikPart.Ask5:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID6:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID7:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID8:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID9:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID10:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK6:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK7:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK8:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK9:
                                case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK10:
                                case QuoteTikPart.LimitHigh:
                                case QuoteTikPart.LimitLow:
                                    tik.addField(particB, Math.abs(Double.parseDouble(toTruePrice(value, goodsCode))));
                                    break;

                                case QuoteTikPart.TotalTurnover:
                                    tik.addField(particB, value);
                                    break;
                                case QuoteTikPart.BidVolume:
                                case QuoteTikPart.BidVolume2:
                                case QuoteTikPart.BidVolume3:
                                case QuoteTikPart.BidVolume4:
                                case QuoteTikPart.BidVolume5:
                                case QuoteTikPart.AskVolume:
                                case QuoteTikPart.AskVolume2:
                                case QuoteTikPart.AskVolume3:
                                case QuoteTikPart.AskVolume4:
                                case QuoteTikPart.AskVolume5:
                                case QuoteTikPart.LastVolume:
                                    tik.addField(particB, Math.abs((int) Double.parseDouble(value)));
                                    break;
                                case QuoteTikPart.BidQueueInfo:// 买委托
                                case QuoteTikPart.AskQueueInfo:// 卖委托
                                    tik.addField(particB, value);
                                    break;
                                case QuoteTikPart.TotalVolume:
                                    tik.addField(particB, Math.abs((int) Double.parseDouble(value)));
                                    break;
                                case QuoteTikPart.HoldIncrement:
                                    tik.addField(particB, Integer.parseInt(value));
                                    break;
                                case QuoteTikPart.OrderId:
                                    try {
                                        tik.addField(QuoteTikPart.OrderId, Integer.parseInt(value));
                                    } catch (Exception e) {
                                        tik.addField(QuoteTikPart.OrderId, Double.parseDouble(value));
                                    }
                                    break;
                                case QuoteTikPart.PreHoldVolume:
                                case QuoteTikPart.HoldVolume:// 持仓量
                                    tik.addField(particB, Double.parseDouble(value));
                                    break;
                                case QuoteTikPart.Strike:
                                    value = toTruePrice(value, goodsCode);
                                    tik.setStickPrice(value);
                                    tik.addField(particB, value);
                                    break;
                                case QuoteTikPart.Time:
                                    tik.setTime(value.substring(0, 6));
                                    tik.addField(particB, value);
                                    break;
                                case QuoteTikPart.Date:
                                    tik.setDate(value.substring(0, 8));
                                    tik.addField(particB, value);
                                    break;
                                case QuoteTikPart.DecimalPlace:
                                    int decimalplace = Math.abs(Integer.parseInt(value.toString()));
                                    if (decimalplace > 4) {
                                        decimalplace = 4;
                                    }

                                    tik.setDecimalPlace((byte) decimalplace);
                                    tik.addField(partic, value);
                                    break;
                                case QuoteTikPart.Inventory://库存量
                                    tik.addField(particB, Double.parseDouble(value));
                                    break;
                                case QuoteTikPart.ExchangeTime: //当前交易日
                                    tik.addField(particB, value);
                                    break;
                                /// 定货期权添加字段
                                case QuoteTikPart.PutOptionPremiums:
                                case QuoteTikPart.PutOptionPremiums2:
                                    tik.addField(particB, Double.parseDouble(value));
                                    break;
                                default:
                                    tik.addField(partic, value);
                                    break;
                            }


                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                quoteList.add(tik);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return quoteList.toArray(new QuoteTik[quoteList.size()]);
    }

    /**
     * 解析实时行情报文
     */
//    public synchronized static QuoteTik[] analysis(byte[] message, QuotesCommandData commandData) throws Exception {
//
//        if (commandData.getSmallPackage() == null || commandData.getSmallPackage().length == 0) {
//            LogUtils.e("QuoteResolver", commandData.toString());
//            throw new Exception("smallPackeg is null!");
//        }
//        int exchangeCode = commandData.getSmallPackage()[0];
//        int sort = commandData.getSmallPackage()[1];
//        int second = ByteUtils.bytesToInt(message, 8, ByteOrder.BIG_ENDIAN);
//        Date date = new Date();
//
//        ArrayList<QuoteTik> quoteList = new ArrayList<QuoteTik>();
//        // date的settime是将时间设置为1970-1-1 0:00:00加上秒数
//        date.setTime((subsecond + second) * 1000);
//        byte[] quoteBytes = new byte[message.length - 15];
//        System.arraycopy(message, 15, quoteBytes, 0, quoteBytes.length);
//
//
////		//------------------------------
////		String quoteStrings = new String(quoteBytes, "utf-8");
////		quoteStrings = quoteStrings.replace("\0", "");
////		String[] quoteStringArray = quoteStrings
////				.split(String.copyValueOf(new char[] { (char) QuoteTikPart.PriceSplit }));
//
//        // 遍历key ，value
//        for (byte[] bytes : breakupArray(quoteBytes)) {
//            try {
//                QuoteTik tik = new QuoteTik();
//                tik.setAtTime(date);
//                tik.setSort(sort);
//                tik.setExchangeCode(exchangeCode);
//                for (int j = 0; j < bytes.length; j++) { // 每一行数据解析
//                    if (bytes[j] == 0x01) {
//                        // 找到0x01后面的第一个2就是key值
//                        for (int k = j + 1; k < bytes.length; k++) {
//                            if (bytes[k] == 0x02) {
//                                byte[] key = new byte[k - j - 1];
//                                System.arraycopy(bytes, j + 1, key, 0, key.length);
//                                // 找到key之后继续查找value
//                                byte[] value = null;
//                                for (int l = k + 1; l < bytes.length; l++) {
//                                    if (bytes[l] == 0x01 || bytes[l] == 0x11) {
//                                        value = new byte[l - k - 1];
//                                        System.arraycopy(bytes, k + 1, value, 0, value.length);
//                                        j = l - 1;
//                                        break;
//                                    }
//                                }
//                                // 根据key赋值
//                                if (value != null) {
//                                    String _value = new String(value);
//                                    switch (key[0]) {
//                                        case QuoteTikPart.GoodsCode:// 商品代码
//                                            tik.setGoodsCode(_value);
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.ExchangeCode:
//                                            tik.setExchangeCode(Integer.parseInt(_value.trim()));
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.Opened:
//                                        case QuoteTikPart.PreSettle:
//                                        case QuoteTikPart.Settle:
//                                        case QuoteTikPart.Last:
//                                        case QuoteTikPart.PreClose:
//                                        case QuoteTikPart.Lowest:
//                                        case QuoteTikPart.Highest:
//                                        case QuoteTikPart.Bid:
//                                        case QuoteTikPart.Bid2:
//                                        case QuoteTikPart.Bid3:
//                                        case QuoteTikPart.Bid4:
//                                        case QuoteTikPart.Bid5:
//                                        case QuoteTikPart.Ask:
//                                        case QuoteTikPart.Ask2:
//                                        case QuoteTikPart.Ask3:
//                                        case QuoteTikPart.Ask4:
//                                        case QuoteTikPart.Ask5:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID6:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID7:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID8:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID9:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_BID10:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK6:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK7:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK8:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK9:
//                                        case QuoteTikPart.MI_QUOTE_FIELD_CODE_ASK10:
//                                        case QuoteTikPart.LimitHigh:
//                                        case QuoteTikPart.LimitLow:
//                                            tik.addField(key[0], Math.abs(Double.parseDouble(toTruePrice(_value, tik.getGoodsCode()))));
//                                            break;
//                                        case QuoteTikPart.TotalTurnover:
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.BidVolume:
//                                        case QuoteTikPart.BidVolume2:
//                                        case QuoteTikPart.BidVolume3:
//                                        case QuoteTikPart.BidVolume4:
//                                        case QuoteTikPart.BidVolume5:
//                                        case QuoteTikPart.AskVolume:
//                                        case QuoteTikPart.AskVolume2:
//                                        case QuoteTikPart.AskVolume3:
//                                        case QuoteTikPart.AskVolume4:
//                                        case QuoteTikPart.AskVolume5:
//                                            tik.addField(key[0], Math.abs((int) Double.parseDouble(_value)));
//                                            break;
//                                        case QuoteTikPart.LastVolume:
//                                            tik.addField(QuoteTikPart.LastVolume, Math.abs((int) Double.parseDouble(_value)));
//                                            break;
//                                        case QuoteTikPart.BidQueueInfo: // 买委托
//                                        case QuoteTikPart.AskQueueInfo: // 卖委托
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.TotalVolume:
//                                            tik.addField(key[0], Math.abs((int) Double.parseDouble(_value)));
//                                            break;
//                                        case QuoteTikPart.HoldIncrement:
//                                            tik.addField(key[0], Integer.parseInt(_value));
//                                            break;
//                                        case QuoteTikPart.OrderId:
//                                            try {
//                                                tik.addField(QuoteTikPart.OrderId, Integer.parseInt(_value));
//                                            } catch (Exception e) {
//                                                tik.addField(QuoteTikPart.OrderId, Double.parseDouble(_value));
//                                            }
//                                            break;
//                                        case QuoteTikPart.PreHoldVolume:
//                                        case QuoteTikPart.HoldVolume:// 持仓量
//                                            tik.addField(key[0], Double.parseDouble(_value));
//                                            break;
//                                        case QuoteTikPart.Strike:
//                                            tik.setStickPrice(toTruePrice(_value, tik.getGoodsCode()));
//                                            tik.addField(key[0], toTruePrice(_value, tik.getGoodsCode()));
//                                            System.out.println("particB = " + toTruePrice(_value, tik.getGoodsCode()));
//                                            break;
//                                        case QuoteTikPart.Time:
//                                            tik.setTime(_value.substring(0, 6));
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.Date:
//                                            tik.setDate(_value.substring(0, 8));
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.DecimalPlace:
//                                            int decimalplace = Math.abs(Integer.parseInt(_value.toString()));
//                                            if (decimalplace > 4) {
//                                                decimalplace = 4;
//                                            }
//
//                                            tik.setDecimalPlace((byte) decimalplace);
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        // 挂牌期权增加
//                                        case QuoteTikPart.PutOptionPremiums:
//                                        case QuoteTikPart.PutOptionPremiums2:
//                                        case QuoteTikPart.PutOptionPremiums3:
//                                        case QuoteTikPart.PutOptionPremiums4:
//                                        case QuoteTikPart.PutOptionPremiums5:
//                                        case QuoteTikPart.CallOptionPremiums:
//                                        case QuoteTikPart.CallOptionPremiums2:
//                                        case QuoteTikPart.CallOptionPremiums3:
//                                        case QuoteTikPart.CallOptionPremiums4:
//                                        case QuoteTikPart.CallOptionPremiums5:
//                                            // 定金值强制两位小数
//                                            tik.addField(key[0], Math.abs(Double.parseDouble(String.valueOf(Double.parseDouble(_value) / Math.pow(10, 2)))));
//                                            //tik.addField(key[0],Double.parseDouble(_value));
//                                            break;
//                                        case QuoteTikPart.strAskOrder:
//                                        case QuoteTikPart.strAskOrder2:
//                                        case QuoteTikPart.strAskOrder3:
//                                        case QuoteTikPart.strAskOrder4:
//                                        case QuoteTikPart.strAskOrder5:
//                                        case QuoteTikPart.strBidOrder:
//                                        case QuoteTikPart.strBidOrder2:
//                                        case QuoteTikPart.strBidOrder3:
//                                        case QuoteTikPart.strBidOrder4:
//                                        case QuoteTikPart.strBidOrder5:
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.ExchangeTime: //当前交易日
//                                            tik.addField(key[0], _value);
//                                            break;
//                                        case QuoteTikPart.ChangeFromOpen:
//                                            tik.addField(key[0],_value);
//                                            tik.setStrChangeSinceOpen(_value);
//                                            break;
//                                        case QuoteTikPart.ChangePercent:
//                                            tik.addField(key[0],_value);
//                                            tik.setStrPctChange(_value);
//                                            break;
//                                        case QuoteTikPart.Change:
//                                            tik.addField(key[0],_value);
//                                            tik.setStrChange(_value);
//                                            break;
//                                        default:
//                                            tik.addField(key[0], _value);
//                                            break;
//                                    }
//                                }
//                                break;
//                            }
//                        }
//                    }
//                }
//                quoteList.add(tik);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return quoteList.toArray(new QuoteTik[quoteList.size()]);
//    }

    /**
     * 将实时行情转换为一笔tik数据
     *
     * @return
     */
    public HistoryTik toHistoryTik() {
        HistoryTik tik = new HistoryTik();
        if (getValue(QuoteTikPart.Last) == null || Double.parseDouble(getValue(QuoteTikPart.Last)) <= 0) {
            return null;
        } else {
            tik.setLast(Double.parseDouble(getValue(QuoteTikPart.Last)));
        }

        tik.setAtTime(getAtTime());

        if (getAsk() != null) {
            tik.setAsk(Double.parseDouble(getAsk()));
        }

        if (getBid() != null) {
            tik.setBid(Double.parseDouble(getBid()));
        }

        if (getValue(QuoteTikPart.AskVolume) != null) {
            tik.setAskVolume(Double.parseDouble(getValue(QuoteTikPart.AskVolume)));
        }

        if (getValue(QuoteTikPart.LastVolume) != null) {
            tik.setVolume(Double.parseDouble(getValue(QuoteTikPart.LastVolume)));
        }

        if (getValue(QuoteTikPart.BidVolume) != null) {
            tik.setBidVolueme(Double.parseDouble(getValue(QuoteTikPart.BidVolume)));
        }
        return tik;
    }

    /**
     * @param price
     * @return
     */
    public static String toTruePrice(String price, String goodsCode) {
        if (price == null || "".equals(price) || Double.parseDouble(price) == 0) {
            return price;
        }
//		String goodsCode = getGoodsCode();
        if (goodsCode.contains("@")) {// 期权就是坑
            goodsCode = goodsCode.split("@")[0];
        }
        int decimalPlace = /*GlobalDataCollention.getInstance().getDecimalPlace(goodsCode)*/2;
//		decimalPlace = decimalPlace == -1 ? getDecimalPlace() : decimalPlace;
        return String.valueOf(Double.parseDouble(price) / Math.pow(10, decimalPlace));
    }
}
