package com.energy.manage.common.constant.redis;

import com.energy.manage.common.enums.IdPrefixEnum;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * redis常量
 */
public class ManagerRedisKeyConstant {

    public static String SYSTEM_USER_LOGIN_INFO = "zhzn_energy_system_user_login_info:%s";

    public static String DEFAULT_PREFIX_KEY = "zhzn";

    public static String PLACEHOLDER = ":";

    public static String DICT_CONSTANTS_KEY = "energy:dict:constants";

    /**
     * skf震动key
     */
    public static String SKF_SHAKE_KEY = "energy:skf:shake";

    /**
     * id 工具缓存
     */
    public static final String IDGENERATOR_CONSTANTS_KEY = "energy:Idgenerator:{id}";

    // 字典
    public static final String IDGENERATOR_UDTC_KEY = "energy:Idgenerator:unit:udtc";
    // 属性
    public static final String IDGENERATOR_UDPC_KEY = "energy:Idgenerator:unit:{id}";

    //品牌对应型号，数据集合
    public static final String IDGENERATOR_UDPB_KEY = "energy:Idgenerator:unit:brandlist:{id}";

    private static final String PLACE_HOLDER = "{id}";


    /**
     * 字符串分隔符
     */
    private static final String SEPARATOR = "-";

    /**
     * redis key的构建方法
     * @param key
     * @param ids
     * @return
     */
    public  static String build(String key, Serializable... ids) {
        //换成buffer, 为了 1.lambda中能用 2.方便字符串拼接
        StringBuffer keyBuffer = new StringBuffer(key);
        //数组转换为list,为了使用stream和获取下标
        List<Serializable> idList = Arrays.asList(ids);

        idList.stream().filter(id -> id != null).forEachOrdered(id -> {
            //首个id进行替换 后面的累加
            if(idList.indexOf(id) == 0){
                int start = keyBuffer.indexOf(PLACE_HOLDER);
                int end = start + PLACE_HOLDER.length();
                keyBuffer.replace(start, end, id.toString());
                return;
            }
            keyBuffer.append(SEPARATOR + id);
        });
        return keyBuffer.toString();
    }

    public static void main(String[] args) {
        String a = ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.WIND_FIELD_NUMBER.getCode(),IdPrefixEnum.WIND_ENGINE_BATCH_NUMBER.getCode());
        System.out.println(a);
    }
}
