package com.engine.salary.remote.cbs8;
import com.alibaba.fastjson.JSON;
import com.engine.salary.exception.CBS8RunTimeException;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* 实体类相关
*
Copyright: Copyright (c) 2022
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
public class SalaryEntityUtil {
private static final DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
/**
* 数字正则表达式
* 包含负数、正数、小数、0、0.00000等
*/
public static final String NUMBER_REGEX = "(-?[1-9]\\d*\\.?\\d+)|(-?0\\.\\d*[0-9])|(\\d+)";
/**
* 千分位格式化
*
* @param originMap 原始map
* @param targetMap 目标map
*/
public static void thousandthConvert(Map originMap, Map targetMap) {
if (MapUtils.isNotEmpty(originMap)) {
originMap.forEach((k, v) -> {
if (StringUtils.isNotBlank(String.valueOf(v))) {
targetMap.put(k, decimalFormat.format(Double.valueOf(String.valueOf(v))));
}
});
}
}
/**
* 千分位格式化
*
* @param originString 原始字符串
* @return 格式化后的字符串
*/
public static String thousandthConvert(String originString) {
if (StringUtils.isNotBlank(originString)) {
return decimalFormat.format(Double.valueOf(originString));
}
return "0.00";
}
/**
* 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty
*
* @param obj
* @return
*/
public static boolean isNullOrEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj instanceof Collection) {
return ((Collection) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map) obj).isEmpty();
}
if (obj instanceof Object[]) {
Object[] object = (Object[]) obj;
if (object.length == 0) {
return true;
}
boolean empty = true;
for (int i = 0; i < object.length; i++) {
if (!isNullOrEmpty(object[i])) {
empty = false;
break;
}
}
return empty;
}
return false;
}
public static boolean isNotNullOrEmpty(Object obj) {
return !isNullOrEmpty(obj);
}
public static A properties(Collection objs, Function function, Collector collectors) {
return objs.stream().map(function).collect(collectors);
}
public static Set properties(Collection objs, Function function) {
if (CollectionUtils.isEmpty(objs)) {
return Sets.newHashSet();
}
return properties(objs, function, Collectors.toSet());
}
public static Map convert2Map(Collection objs, Function function) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream().collect(Collectors.toMap(function, Function.identity(), (a, b) -> a));
}
public static Map convert2Map(Collection objs, Function keyMapper, Function valueMapper) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream()
.filter(e -> valueMapper.apply(e) != null && keyMapper.apply(e) != null)
.collect(Collectors.toMap(keyMapper, valueMapper, (a, b) -> a));
}
public static Map> group2Map(Collection objs, Function function) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream().collect(Collectors.groupingBy(function));
}
public static Map> group2Map(Collection objs, Function keyMapper, Function valueMapper) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream()
.filter(e -> keyMapper.apply(e) != null && valueMapper.apply(e) != null)
.collect(Collectors.groupingBy(keyMapper,
Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().map(valueMapper).collect(Collectors.toSet()))));
}
public static Map> group2ListMap(Collection objs, Function keyMapper, Function valueMapper) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream()
.filter(e -> keyMapper.apply(e) != null && valueMapper.apply(e) != null)
.collect(Collectors.groupingBy(keyMapper,
Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().map(valueMapper).collect(Collectors.toList()))));
}
public static Map> list2Map(Collection objs, Function function1, Function function2, Function function3) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
Map> collect = objs.stream().collect(Collectors.groupingBy(function1));
Map> map = new HashMap<>();
for (Map.Entry> entry: collect.entrySet()) {
Map values = map.getOrDefault(entry.getKey(), new HashMap<>());
entry.getValue().forEach(e -> values.put(function2.apply(e), function3.apply(e)));
map.put(entry.getKey(), values);
}
return map;
}
public static Map> list2Map(Collection objs, Function function1, Function function2) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
Map> collect = objs.stream().collect(Collectors.groupingBy(function1));
Map> map = new HashMap<>();
for (Map.Entry> entry: collect.entrySet()) {
Map values = map.getOrDefault(entry.getKey(), new HashMap<>());
entry.getValue().forEach(e -> values.put(function2.apply(e), e));
map.put(entry.getKey(), values);
}
return map;
}
/**
* LinkedHashMap有序去重
*
* @param keyExtractor
* @param
* @return
*/
public static Predicate distinctByKey(Function super T, Object> keyExtractor) {
LinkedHashMap