数据统计,日期补全

 /**
    * **自定义定义泛型需要的数据**
    */
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);
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值