# Conflicts: # pom.xml # src/main/java/com/zhgd/xmgl/modules/basicdata/service/impl/CompanyServiceImpl.java # src/main/java/com/zhgd/xmgl/modules/quality/controller/QualityRegionController.java # src/main/java/com/zhgd/xmgl/modules/quality/mapper/QualityRegionMapper.java # src/main/java/com/zhgd/xmgl/modules/quality/mapper/xml/QualityRegionMapper.xml # src/main/java/com/zhgd/xmgl/modules/quality/service/IQualityRegionService.java # src/main/java/com/zhgd/xmgl/modules/quality/service/impl/QualityRegionServiceImpl.java # src/main/java/com/zhgd/xmgl/util/Fileutils.java
213 lines
7.6 KiB
Java
213 lines
7.6 KiB
Java
package com.zhgd.xmgl.util;
|
||
|
||
import cn.hutool.core.convert.Convert;
|
||
import cn.hutool.core.date.DateUtil;
|
||
import cn.hutool.core.util.NumberUtil;
|
||
import cn.hutool.core.util.StrUtil;
|
||
|
||
import java.math.BigDecimal;
|
||
import java.math.RoundingMode;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Objects;
|
||
import java.util.concurrent.ThreadLocalRandom;
|
||
import java.util.function.Consumer;
|
||
import java.util.function.Function;
|
||
import java.util.stream.Collectors;
|
||
|
||
public class EntityUtils {
|
||
/**
|
||
* 对象保存随机的String
|
||
* eg:EntityUtils.setRandomStrByPoint(config.getX(), data::setX, random);
|
||
*
|
||
* @param config
|
||
* @param strSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomStrByPoint(String config, Consumer<String> strSet, ThreadLocalRandom random) {
|
||
if (StrUtil.isNotBlank(config)) {
|
||
List<String> split = StrUtil.split(config, ",");
|
||
strSet.accept(split.get(random.nextInt(split.size())));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Int
|
||
* eg:EntityUtils.setRandomIntByPoint(config.getX(), data::setX, random);
|
||
*
|
||
* @param config
|
||
* @param intSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomIntByPoint(String config, Consumer<Integer> intSet, ThreadLocalRandom random) {
|
||
if (StrUtil.isNotBlank(config)) {
|
||
List<String> split = StrUtil.split(config, ",");
|
||
Integer value = Integer.valueOf(split.get(random.nextInt(split.size())));
|
||
intSet.accept(value);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Int
|
||
* eg:EntityUtils.setRandomInt(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param intSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomInt(Integer begin, Integer end, Consumer<Integer> intSet, ThreadLocalRandom random) {
|
||
if (begin != null && end != null) {
|
||
intSet.accept(random.nextInt(begin, end + 1));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Int
|
||
* eg:EntityUtils.setRandomInt(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param intSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomInt(String begin, String end, Consumer<Integer> intSet, ThreadLocalRandom random) {
|
||
if (StrUtil.isNotBlank(begin) && StrUtil.isNotBlank(end)) {
|
||
intSet.accept(random.nextInt(Convert.toInt(begin), Convert.toInt(end) + 1));
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 对象保存随机的Double
|
||
* eg:EntityUtils.setRandomDouble(config.getXBegin(), config.getXEnd(), data::setX, 2, random);
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param doubleSet
|
||
* @param scale 保留小数
|
||
* @param random
|
||
*/
|
||
public static void setRandomDouble(Double begin, Double end, Consumer<Double> doubleSet, int scale, ThreadLocalRandom random) {
|
||
if (begin != null && end != null) {
|
||
doubleSet.accept(NumberUtil.round(random.nextDouble(begin, Math.nextUp(end)), scale, RoundingMode.DOWN).doubleValue());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Double,不保留小数
|
||
* eg:EntityUtils.setRandomDoubleInt(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param doubleSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomDoubleInt(String begin, String end, Consumer<String> doubleSet, ThreadLocalRandom random) {
|
||
if (StrUtil.isNotBlank(begin) && StrUtil.isNotBlank(end)) {
|
||
doubleSet.accept(NumberUtil.round(random.nextDouble(Convert.toDouble(begin), Math.nextUp(Convert.toDouble(end))), 0, RoundingMode.DOWN).toString());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Double,不保留小数
|
||
* eg:EntityUtils.setRandomDoubleInt(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param doubleSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomDoubleInt(Double begin, Double end, Consumer<Double> doubleSet, ThreadLocalRandom random) {
|
||
if (begin != null && end != null) {
|
||
doubleSet.accept(NumberUtil.round(random.nextDouble(Convert.toDouble(begin), Math.nextUp(Convert.toDouble(end))), 0, RoundingMode.DOWN).doubleValue());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Double,不保留小数
|
||
* eg:EntityUtils.setRandomDoubleInt(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param doubleSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomDoubleIntFromStr(String begin, String end, Consumer<BigDecimal> doubleSet, ThreadLocalRandom random) {
|
||
if (begin != null && end != null) {
|
||
doubleSet.accept(NumberUtil.round(random.nextDouble(Convert.toDouble(begin), Math.nextUp(Convert.toDouble(end))), 0, RoundingMode.DOWN));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Double
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param doubleSet
|
||
* @param scale
|
||
* @param random
|
||
*/
|
||
public static void setRandomDouble(String begin, String end, Consumer<String> doubleSet, int scale, ThreadLocalRandom random) {
|
||
if (StrUtil.isNotBlank(begin) && StrUtil.isNotBlank(end)) {
|
||
doubleSet.accept(NumberUtil.round(random.nextDouble(Convert.toDouble(begin), Math.nextUp(Convert.toDouble(end))), scale, RoundingMode.DOWN).toString());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的日期时间
|
||
* eg:EntityUtils.setRandomDateTime(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*/
|
||
public static void setRandomDateTime(Date begin, Date end, Consumer<Date> dateSet, ThreadLocalRandom random) {
|
||
if (begin != null && end != null) {
|
||
dateSet.accept(new Date(random.nextLong(begin.getTime(), end.getTime() + 1)));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的日期时间
|
||
* eg:EntityUtils.setRandomDateTimeStr(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*/
|
||
public static void setRandomDateTimeStr(Date begin, Date end, Consumer<String> dateSet, ThreadLocalRandom random) {
|
||
if (begin != null && end != null) {
|
||
dateSet.accept(DateUtil.formatDateTime(new Date(random.nextLong(begin.getTime(), end.getTime() + 1))));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 对象保存随机的Float
|
||
* eg:EntityUtils.setRandomFloatInt(config.getXBegin(), config.getXEnd(), data::setX, random);
|
||
*
|
||
* @param begin
|
||
* @param end
|
||
* @param doubleSet
|
||
* @param random
|
||
*/
|
||
public static void setRandomFloatInt(Float begin, Float end, Consumer<Float> doubleSet, ThreadLocalRandom random) {
|
||
if (begin != null && end != null) {
|
||
doubleSet.accept((float) NumberUtil.round(random.nextDouble(begin, Math.nextUp(end)), 0, RoundingMode.DOWN).doubleValue());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 设置名称字段(通用方法)
|
||
*
|
||
* @param sourceIds 源ID字符串,用逗号分隔
|
||
* @param nameMap 映射的Map
|
||
* @param nameExtractor 名称提取函数
|
||
* @return 拼接后的名称字符串
|
||
*/
|
||
public static <T> String convertIdsToNames(String sourceIds, Map<Long, T> nameMap, Function<T, String> nameExtractor) {
|
||
if (StrUtil.isBlank(sourceIds)) {
|
||
return "";
|
||
}
|
||
return StrUtil.split(sourceIds, ",")
|
||
.stream()
|
||
.map(id -> nameMap.get(Convert.toLong(id)))
|
||
.filter(Objects::nonNull) // 过滤掉null值
|
||
.map(nameExtractor)
|
||
.collect(Collectors.joining(","));
|
||
}
|
||
}
|