package quant.rich.emoney.enums;

import lombok.Getter;

/**
 * 股价数据粒度
 * @author Administrator
 *
 */
public enum StockSpan {
    
    Tick("7", null),
    Minute("8", 1),
    Minute5("0", 5),
    Minute15("1", 15),
    Minute30("2", 30),
    Minute60("3", 60),
    Minute120(null, 120),
    Daily("4", null),
    Weekly("5", null),
    Monthly("6", null),
    Seasonly("10", null),
    HalfYearly(null, null),
    Yearly("11", null);
    
    private String tdxCode;
    @Getter
    private Integer min;
    
    private StockSpan(String tdxCode, Integer min) {
        this.tdxCode = tdxCode;
        this.min = min;
    }
    
    /**
     * 获取东财对应的时间粒度表示
     * @return
     */
    public String getEastMoneyCode() {
        
        if (this.min != null) {
            return String.format("%d", 0);
        }
        
        switch(this) {
        case Daily:
            return "101";
        case Weekly:
            return "102";
        case Monthly:
            return "103";
        case Seasonly:
            return "104";
        case HalfYearly:
            return "105";
        case Yearly:
            return "106";
        default:
            throw new RuntimeException("东财时间粒度不支持 " + this);
        }
    }
    
    /**
     * 从东财时间粒度转换为 StockSpan
     * @param eastMoneyCode
     * @return
     */
    public static StockSpan fromEastMoneyCode(String eastMoneyCode) {
        
        switch (eastMoneyCode) {
        case "1":
            return Minute;
        case "5":
            return Minute5;
        case "15":
            return Minute15;
        case "30":
            return Minute30;
        case "60":
            return Minute60;
        case "120":
            return Minute120;
        case "101":
            return Daily;
        case "102":
            return Weekly;
        case "103":
            return Monthly;
        case "104":
            return Seasonly;
        case "105":
            return HalfYearly;
        case "106":
            return Yearly;
        default:
            throw new RuntimeException("未知的东财时间粒度 " + eastMoneyCode);  
        }
    }
    
    /**
     * 转换为益盟时间粒度
     * @return
     */
    public Integer getEmoneyCode() {
        
        if (this.min != null) {
            return this.min;
        }
        
        switch(this) {
        case Daily:
            return 10000;
        case Weekly:
            return 20000;
        case Monthly:
            return 30000;
        case Seasonly:
            return 40000;
        case HalfYearly:
            return 50000;
        case Yearly:
            return 60000;
        default:
            throw new RuntimeException("益盟时间粒度不支持：" + this);
        }
        
    }
    
    /**
     * 从益盟时间粒度转换
     * @param emoneyDataPeriod
     * @return
     */
    public static StockSpan fromEmoneyCode(Integer emoneyDataPeriod) {
        switch (emoneyDataPeriod) {
        case 1:
            return Minute;
        case 5:
            return Minute5;
        case 15:
            return Minute15;
        case 30:
            return Minute30;
        case 60:
            return Minute60;
        case 120:
            return Minute120;
        case 10000:
            return Daily;
        case 20000:
            return Weekly;
        case 30000:
            return Monthly;
        case 40000:
            return Seasonly;
        case 50000:
            return HalfYearly;
        case 60000:
            return Yearly;
        default:
            throw new RuntimeException("未知的益盟时间粒度 " + emoneyDataPeriod);  
        }
    }
    
    /**
     * 转换成开盘啦数据粒度
     * @return
     */
    public String getKaipanlaType() {
        String type;
        if (this == StockSpan.Yearly) {
            type = "y";
        }
        else if (this == StockSpan.Monthly) {
            type = "m";
        }
        else if (this == StockSpan.Weekly) {
            type = "w";
        }
        else if (this == StockSpan.Minute60) {
            type = "60";
        }
        else if (this == StockSpan.Minute30) {
            type = "30";
        }
        else if (this == StockSpan.Minute15) {
            type = "15";
        }
        else if (this == StockSpan.Minute5) {
            type = "5";
        }
        else if (this == StockSpan.Daily){
            type = "d";
        }
        else {
            throw new RuntimeException("开盘啦指标不支持该数据粒度 " + this);
        }
        return type;
    }
    
    /**
     * 从开盘啦时间粒度转换
     * @param kaipanlaType
     * @return
     */
    public static StockSpan fromKaipanlaType(String kaipanlaType) {

        switch (kaipanlaType) {
        case "5":
            return Minute5;
        case "15":
            return Minute15;
        case "30":
            return Minute30;
        case "60":
            return Minute60;
        case "d":
            return Daily;
        case "w":
            return Weekly;
        case "n":
            return Monthly;
        case "y":
            return Yearly;
        default:
            throw new RuntimeException("未知的开盘啦时间粒度 " + kaipanlaType);  
        }
    }
}
