package com.energy.manage.common.util;

import java.security.SecureRandom;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 */
public class IdentitieTools {

  private static SecureRandom random = new SecureRandom();

  /**
   * 封装JDK自带的UUID, 通过Random数字生成, 中间有-分割.
   */
  public static String uuid() {
    return UUID.randomUUID().toString();
  }

  /**
   * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
   */
  public static String uuid2() {
    return UUID.randomUUID().toString().replaceAll("-", "");
  }

  /**
   * 使用SecureRandom随机生成Long.
   */
  public static long randomLong() {
    return Math.abs(random.nextLong());
  }

  /**
   * 根据类型随机获取该类型url
   *
   * @param map
   * @param type
   * @return
   */
  public static String getUrlByType(Map<String, List<String>> map, String type) {
    if (map == null) {
      return getConfigStr(type);
    }
    List<String> stringList = map.get(type);
    if (CollectionTools.isEmpty(stringList)) {
      return getConfigStr(type);
    }
    return stringList.get(random.nextInt(stringList.size()));
  }


  public static String getConfigStr(String type) {
    if (StringTools.isNotBlank(type)) {
      if (StringTools.equalsString("cxcy", type)) {
        return StringTools.isNotBlank(type) ? "https://" + type + ".oss-cn-beijing.aliyuncs.com" : null;
      } else {
        return StringTools.isNotBlank(type) ? "https://gxb-" + type + ".oss-cn-beijing.aliyuncs.com" : null;
      }
    }
    return null;

  }

}
