package com.smppw.utils;

import com.smppw.constants.SecType;
import com.smppw.common.pojo.InvestmentType;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.common.pojo.enums.NavType;
import com.smppw.common.pojo.enums.Visibility;

/**
 * 关于Redis操作的工具类，底层封装jedis；
 * 建议调用这里的方法而不是直接调用jedis方法；
 */
public class RedisUtils {

    public static String getNavKey(String secId, Integer curveTypeId, Integer strategyId, Integer visibilityId) {
        String key = null;
        if(secId.startsWith("CO")){
            key = "nav:company:" + secId + ":" + curveTypeId + ":" + strategyId;
        }else if(secId.startsWith("PL")){
            key = "nav:manager:" + secId + ":" + curveTypeId + ":" + strategyId;
        }else{
            InvestmentType investmentType = InvestmentType.getInvestmentType(secId);
            if (investmentType == InvestmentType.Fund || investmentType == InvestmentType.ExternalFund) {
                key = "nav:fund:" + secId + ":" + visibilityId;
            } else if (investmentType == InvestmentType.Index) {
                key = "nav:index:" + secId;
            } else if (investmentType == InvestmentType.BFI) {
                key = "nav:bfi:" + secId;
            }
        }
        return key;
    }

    public static String getHashLockKey(String secId, String key) {
        return secId + key + "lock";
    }


    public static String getLockKey(String key) {
        return key+"lock";
    }

    public static String getNewNavKey(String secId, NavType navType, Integer curveTypeId, Integer strategyId, Visibility visibility, String fundType) {

        Integer visibilityId = visibility.getId();

        String key = null;
        if (SecType.PRIVATELY_OFFERED_FUND.equals(fundType)) {
            key = "nav:" + navType.name() + ":" + visibilityId;
        } else if (SecType.PUBLICLY_OFFERED_FUNDS.equals(fundType)) {
            key = "nav:" + navType.name() + ":" + visibilityId;
        } else if (SecType.PRIVATE_FUND.equals(fundType)) {
            key = "nav:" + navType.name();
        } else if (SecType.INDEX_FUND.equals(fundType)) {
            key = "nav:" + navType.name();
        }

        return key;
    }
    public static String getDateRangeKey(String secId, Integer curveTypeId, Integer strategyId, Integer visibilityId) {
        String key = null;
        if(secId.startsWith("CO")){
            key = "dateRange:company:" + secId + ":" + curveTypeId + ":" + strategyId;
        }else if(secId.startsWith("PL")){
            key = "dateRange:manager:" + secId + ":" + curveTypeId + ":" + strategyId;
        }else{
            InvestmentType investmentType = InvestmentType.getInvestmentType(secId);
            if (investmentType == InvestmentType.Fund || investmentType == InvestmentType.ExternalFund) {
                key = "dateRange:fund:" + secId + ":" + visibilityId;
            } else if (investmentType == InvestmentType.Index) {
                key = "dateRange:index:" + secId;
            } else if (investmentType == InvestmentType.BFI) {
                key = "dateRange:bfi:" + secId;
            }
        }
        return key;
    }

    public static String getRetKey(String secId, Integer curveTypeId, Integer strategyId, Integer visibilityId, Frequency frequency) {
        String key = null;
        if(secId.startsWith("CO")){
            key = "ret:company:" + frequency.getShortName() + ":" + secId + ":" + curveTypeId + ":" + strategyId;
        }else if(secId.startsWith("PL")){
            key = "ret:manager:" + frequency.getShortName() + ":" + secId + ":" + curveTypeId + ":" + strategyId;
        }else{
            InvestmentType investmentType = InvestmentType.getInvestmentType(secId);
            if (investmentType == InvestmentType.Fund || investmentType == InvestmentType.ExternalFund) {
                key = "ret:fund:" + frequency.getShortName() + ":" + secId + ":" + visibilityId;
            } else if (investmentType == InvestmentType.Index) {
                key = "ret:index:" + frequency.getShortName() + ":" + secId;
            } else if (investmentType == InvestmentType.BFI) {
                key = "ret:bfi:" + frequency.getShortName() + ":" + secId;
            }

        }
        return key;
    }

}
