/**
* **自定义定义泛型需要的数据**
*/
public interface DateCompletion {
/**
* 定义方法 获取统计时间
*/
LocalDate getDate();
DateCompletion emptyData(LocalDate localDate);
}
// 自定义排序类
public interface DateComparable extends Comparable<DateComparable>{
default boolean reversed() {
return false;
}
ChronoLocalDate getComparableDate();
@Override
default int compareTo(DateComparable o) {
if (!reversed()) {
return this.getComparableDate().compareTo(o.getComparableDate());
} else {
return o.getComparableDate().compareTo(getComparableDate());
}
}
}
/**
* 补全天数 方法
*/
@SneakyThrows
public <T extends DateCompletion> List<T> dateCompletion(List<T> list, LocalDate startDate, LocalDate endDate, Class<T> tClass) {
// 相差的天数
long days = ChronoUnit.DAYS.between(startDate, endDate);
// 补相差的天数
List<LocalDate> dateList = LongStream.range(0, days).boxed().map(startDate::plusDays).collect(Collectors.toList());
for (LocalDate localDate : dateList) {
// 构造当前数据 , 值为 0
T dateCompletion = (T) tClass.newInstance().emptyData(localDate);
// 判断是否有当天数据
if (!list.contains(dateCompletion)) {
// 不存在的添加
list.add(dateCompletion);
}
}
return list;
}
/**
* 采购量/复用量 柱状图
*
* @param startDate
* @param endDate
* @return
*/
public List<EveryDayProcurementDataDto> findEveryDayProcurementData(LocalDate startDate, LocalDate endDate, Integer type) {
if (endDate != null) {
endDate = endDate.plusDays(1);
}
if (type == 0) {
// 复用量
List<EveryDayProcurementDataDto> dataList = statisticsMapper.findEveryDayProcurementData(startDate, endDate, type);
// 调用方法补全日期
List<EveryDayProcurementDataDto> dataDtoList = dateCompletion(dataList, startDate, endDate, EveryDayProcurementDataDto.class);
// 日期排序
Collections.sort(dataDtoList);
return dataDtoList;
} else {
// 采购量
List<EveryDayProcurementDataDto> suppWorkLog = statisticsMapper.findSuppWorkLog(startDate, endDate, type);
// 调用方法补全日期
List<EveryDayProcurementDataDto> dataDtoList = dateCompletion(suppWorkLog, startDate, endDate, EveryDayProcurementDataDto.class);
// 日期排序
Collections.sort(dataDtoList);
return dataDtoList;
}
}
/**
* 返回数据的dto
*/
@Data
@NoArgsConstructor
@EqualsAndHashCode(of = "statDate")
public class EveryDayProcurementDataDto implements DateComparable, DateCompletion {
/**
* 数据量
*/
private Integer dataSize;
/**
* 统计日期
*/
@JsonFormat(pattern = "MM-dd")
private LocalDate statDate;
public EveryDayProcurementDataDto(LocalDate statDate) {
this.statDate = statDate;
this.dataSize = 0;
}
@JsonIgnore
@Override
public ChronoLocalDate getComparableDate() {
return this.statDate;
}
@JsonIgnore
@Override
public LocalDate getDate() {
return this.statDate;
}
@Override
public EveryDayProcurementDataDto emptyData(LocalDate localDate) {
return new EveryDayProcurementDataDto(localDate);
}
}
数据统计,日期补全
最新推荐文章于 2023-07-04 02:37:51 发布