效果图如下:
代码如下:
//统计时间范围的数组
List<String> datList = RangeDateUtil.allRangeDate(startDate, endDate);
if(CollectionUtil.isEmpty(datList)){
return new ArrayList<>();
}
//查询时间范围内复合条件的所有数据
List<TrusteeshipInfoVO> trusteeshipInfoVOS = wxInsureTrusteeshipInfoMapper.getTreatmentNumber(startDate,endDate);
//new一个返回的结果数组
List<TrusteeshipInfoVO> result = new ArrayList<>();
//当日期数组不为空时,则开始遍历所有日期的每一天
for(String date : datList){
//判断一下查询的结果数组是否为空
if(CollectionUtil.isEmpty(trusteeshipInfoVOS)){
//当开始的第一天数据为空时,则将数据对象里面的值除了日期,其他全部初始化为0,然后添加到结果返回数组中
result.add(initTrusteeshipInfoVO(date));
continue;
}
//遍历查出来的数据,填充进日期
boolean hasValue = false;
for(TrusteeshipInfoVO trusteeshipInfoVO : trusteeshipInfoVOS){
if(date.equals(trusteeshipInfoVO.getDateTime().substring(0,10))){
trusteeshipInfoVO.setDateTime(trusteeshipInfoVO.getDateTime().substring(0,10));
result.add(trusteeshipInfoVO);
hasValue = true;
break;
}
}
if(!hasValue) {
result.add(initTrusteeshipInfoVO(date));
}
}
return result;
}
/**
* 将为空的日期的对象数据全部初始化为0
*/
private TrusteeshipInfoVO initTrusteeshipInfoVO(String date){
TrusteeshipInfoVO trusteeshipInfoVO = new TrusteeshipInfoVO();
trusteeshipInfoVO.setCustomerCount(0);
trusteeshipInfoVO.setEmployeeCount(0);
trusteeshipInfoVO.setDateTime(date);
return trusteeshipInfoVO;
}
用到的工具类如下:
/**
* @param startDateStr 开始日期
* @param endDateStr 结束日期
* @return 从开始到结束的所有日期全部统计出来
*/
public static List<String> allRangeDate(String startDateStr, String endDateStr) {
List<String> listDate = new ArrayList<>();
DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDateTime startDate = LocalDateTime.parse(startDateStr, df1);
LocalDateTime endDate = LocalDateTime.parse(endDateStr, df1);
LocalDateTime tempDate = null;
while (!(LocalDateTime.of(startDate.plusDays(-1).toLocalDate(), LocalTime.MIN)
.equals(LocalDateTime.of(endDate.toLocalDate(), LocalTime.MIN)))) {
tempDate = startDate;
String format = tempDate.format(df2);
listDate.add(format);
startDate = startDate.plusDays(1);
}
System.out.println(listDate.toString());
return listDate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}