This commit is contained in:
墨大叔 2024-07-22 10:54:00 +08:00
parent 99b0d2767b
commit 8d753fbdbb
2 changed files with 63 additions and 48 deletions

View File

@ -1,9 +1,8 @@
package com.ruoyi.common.utils.collection;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -32,22 +31,72 @@ public class CollectionUtils extends org.springframework.util.CollectionUtils {
return !isEmptyElement(list);
}
public static boolean isNotEmpty(List<?> list) {
return !isEmpty(list);
/**
* 填充列表中的元素
* @param oldList 旧列表
* @param startDate 起始日期
* @param endDate 结束日期
* @param clazz 填充元素的类
*/
public static List<? super FillAble> fillElement(List<? extends FillAble> oldList, Date startDate, Date endDate,Class<? extends FillAble> clazz ) throws InstantiationException, IllegalAccessException {
List<FillAble> newList = new ArrayList<>();
while (startDate.compareTo(endDate) <= 0) {
Date finalI = startDate;
FillAble fillable = oldList.stream().filter(item -> DateUtils.getYYYY_MM_DD(item.fillDate()).equals(DateUtils.getYYYY_MM_DD(finalI))).findFirst().orElse(null);
if (fillable == null) {
fillable = clazz.newInstance().fillElement(finalI);
}
newList.add(fillable);
startDate = DateUtils.addDays(startDate,1);
}
return newList;
}
// TODO 未经验证
/**
* 判断分隔字符串中是否存在目标元素
* @param str 分隔字符串
* @param split 分隔符
* @param target 目标元素
* @param parseMapper 类型转换方法
* 补全列表中的日期数据对于缺失的日期使用提供的工厂方法生成对象
*
* @param dataList 原始数据列表元素需有getDateMethod返回Date类型属性的方法
* @param dateGetter 获取元素日期属性的方法引用
* @param customFactory 当日期缺失时用于生成对应元素的对象工厂方法
* @param <T> 数据列表中元素的类型
* @return 完整日期序列下的数据列表缺失的日期已由customFactory填充
*/
public static <T> boolean splitContains(String str, String split, T target, Function<String, ? extends T> parseMapper) {
if (!StringUtils.hasText(str)) {
return false;
public static <T> List<T> completeMissingDates(List<T> dataList,
Function<T, Date> dateGetter,
Function<Date, T> customFactory) {
if (dataList == null || dataList.isEmpty()) {
return Collections.emptyList();
}
return Arrays.stream(str.split(split)).map(parseMapper).collect(Collectors.toList()).contains(target);
List<Date> dates = dataList.stream()
.map(dateGetter)
.sorted()
.collect(Collectors.toList());
Date startDate = dates.get(0);
Date endDate = dates.get(dates.size() - 1);
List<Date> fullDates = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
while (!calendar.getTime().after(endDate)) {
fullDates.add(calendar.getTime());
calendar.add(Calendar.DATE, 1);
}
Map<Date, T> dateToObjectMap = dataList.stream()
.collect(Collectors.toMap(dateGetter, Function.identity()));
return fullDates.stream()
.map(date -> dateToObjectMap.getOrDefault(date, customFactory.apply(date)))
.collect(Collectors.toList());
}
public static boolean isNotEmpty(List<?> deviceList) {
return !isEmpty(deviceList);
}
/**
@ -56,38 +105,4 @@ public class CollectionUtils extends org.springframework.util.CollectionUtils {
public static <T, R> List<R> map(List<T> list, Function<? super T, ? extends R> keyMapper) {
return list.stream().map(keyMapper).filter(Objects::nonNull).distinct().collect(Collectors.toList());
}
/**
* 累加 BigDecimal
*/
public static <T> BigDecimal sumBigDecimal(List<? extends T> list, Function<T, BigDecimal> valueMapper) {
if (CollectionUtils.isEmptyElement(list)) {
return BigDecimal.ZERO;
}
return list.stream().filter(Objects::nonNull)
.map(valueMapper)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
/**
* 累加 Integer
*/
public static <T> Integer sumInteger(List<? extends T> list, Function<T, Integer> valueMapper) {
if (CollectionUtils.isEmptyElement(list)) {
return 0;
}
return list.stream().filter(Objects::nonNull)
.map(valueMapper)
.filter(Objects::nonNull)
.reduce(Integer::sum)
.orElse(0);
}
public static <T> void addNotNull(List<? super T> list, T obj) {
if (obj == null) {
return;
}
list.add(obj);
}
}

View File

@ -66,7 +66,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<where>
<include refid="searchCondition"/>
</where>
order by ss.create_time desc
order by ss.sort asc, ss.create_time desc
</select>
<select id="selectSuitBySuitId" parameterType="Long" resultMap="SuitResult">