package com.energy.manage.service.util;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

public class RandomSeriUtil {

  /**
   * 生成随机六位数，用作验证码
   *
   * @return
   */
  public static String genRandoms() {
    return String.valueOf(ThreadLocalRandom.current().nextInt(100000, 999999));
  }

  /**
   * 生成随机六位数，用作验证码
   *
   * @return
   */
  public static String genRandoms1_10() {
    return String.valueOf(ThreadLocalRandom.current().nextInt(1, 11));
  }


  /**
   * 字符串占位符
   *
   * @param str
   * @param arguments
   * @return
   */
  public static String formatStr(String str, Object... arguments) {
    return MessageFormat.format(str, arguments);
  }


  /**
   * 内部方法，不丢精度还原物品价格
   *
   * @param price
   * @return
   */
  public static int getItemPrice(String price) {
    if (StringUtils.isEmpty(price)) {
      return 0;
    }
    BigDecimal temp = BigDecimal.valueOf(Double.valueOf(price));
    temp = temp.multiply(BigDecimal.valueOf(100));
    return temp.intValue();
  }


  /**
   * 随机数
   *
   * @param count
   * @return
   */
  public static List<Long> createRandomByCount(int count, long max) {
    count = count * 10;
    Set<Long> set = new HashSet<>();
    for (int i = 0; i < count; i++) {
      long item = genRandoms(max);
      set.add(item);
    }
    List<Long> idList = new ArrayList<>(count * 2);
    set.stream().forEach(item -> {
      idList.add(item);
    });
    return idList;
  }

  public static long genRandoms(long max) {
    return ThreadLocalRandom.current().nextLong(1, max);
  }
}
