柯桥增值式服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.8KB

  1. package com.ningdatech.kqapi.common.util;
  2. import cn.hutool.core.text.StrPool;
  3. import cn.hutool.core.util.StrUtil;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.util.NumberUtils;
  6. import java.math.BigDecimal;
  7. import java.math.RoundingMode;
  8. import java.time.LocalDateTime;
  9. import java.time.ZoneId;
  10. import java.util.*;
  11. import java.util.function.Consumer;
  12. import java.util.function.Function;
  13. import java.util.stream.Collectors;
  14. import static java.util.stream.Collectors.toList;
  15. /**
  16. * <p>
  17. * BizUtils
  18. * </p>
  19. *
  20. * @author WendyYang
  21. * @since 17:32 2023/1/29
  22. */
  23. public class BizUtils {
  24. private BizUtils() {
  25. }
  26. public static <T extends Collection<?>> void notEmpty(T objs, Consumer<T> consumer) {
  27. if (objs != null && !objs.isEmpty()) {
  28. consumer.accept(objs);
  29. }
  30. }
  31. public static <T extends Number> List<T> splitToNum(String str, Class<T> aClass) {
  32. if (StrUtil.isEmpty(str)) {
  33. return Collections.emptyList();
  34. }
  35. return Arrays.stream(str.split(StrPool.COMMA))
  36. .map(w -> NumberUtils.parseNumber(w, aClass))
  37. .collect(Collectors.toList());
  38. }
  39. public static List<Long> splitToLong(String str) {
  40. return splitToNum(str, Long.class);
  41. }
  42. public static <T> void notNull(T obj, Consumer<T> consumer) {
  43. if (obj != null) {
  44. consumer.accept(obj);
  45. }
  46. }
  47. public static void notBlank(String str, Consumer<String> consumer) {
  48. if (StrUtil.isNotBlank(str)) {
  49. consumer.accept(str);
  50. }
  51. }
  52. public static boolean getJSONType(String str) {
  53. boolean result = false;
  54. if (StringUtils.isNotBlank(str)) {
  55. str = str.trim();
  56. if (str.startsWith("{") && str.endsWith("}")) {
  57. result = true;
  58. } else if (str.startsWith("[") && str.endsWith("]")) {
  59. result = true;
  60. }
  61. }
  62. return result;
  63. }
  64. public static String uuid32() {
  65. return UUID.randomUUID().toString().replace("-", "");
  66. }
  67. /**
  68. * 对象分组取第一条
  69. *
  70. * @param objs 对象集合
  71. * @param group 分组函数
  72. * @param comparator 比较器
  73. * @return java.util.Collection<T>
  74. * @author WendyYang
  75. **/
  76. public static <T> Collection<T> groupFirst(Collection<T> objs, Function<T, Object> group, Comparator<T> comparator) {
  77. return groupFirstMap(objs, group, comparator).values();
  78. }
  79. /**
  80. * 对象分组取第一条
  81. *
  82. * @param objs 对象集合
  83. * @param group 分组函数
  84. * @param comparator 比较器
  85. * @return java.util.Collection<T>
  86. * @author WendyYang
  87. **/
  88. public static <V, K> Map<K, V> groupFirstMap(Collection<V> objs, Function<V, K> group, Comparator<V> comparator) {
  89. return objs.stream().collect(Collectors.groupingBy(group,
  90. Collectors.collectingAndThen(toList(), w -> {
  91. w.sort(comparator);
  92. return w.get(0);
  93. })));
  94. }
  95. public static LocalDateTime convertDate(Date date) {
  96. if (Objects.nonNull(date)) {
  97. return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  98. }
  99. return null;
  100. }
  101. public static String computeRise(Integer numa, Integer numb) {
  102. if((Objects.isNull(numb)|| numb.equals(0)) &&
  103. (Objects.isNull(numa)|| numa.equals(0))){
  104. return "0";
  105. }
  106. if(Objects.isNull(numa) || numa.equals(0)){
  107. return "-100";
  108. }
  109. if(Objects.isNull(numb)|| numb.equals(0)){
  110. return "100";
  111. }
  112. return BigDecimal.valueOf((numa - numb) * 100.0).divide(BigDecimal.valueOf(numb),2, RoundingMode.CEILING)
  113. .stripTrailingZeros().toPlainString();
  114. }
  115. }