| | |
| | | |
| | | public AjaxResult statisticsTable(StatisticsTableDto statisticsTableDto) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | if (statisticsTableDto.getEntryDateStart() == null || statisticsTableDto.getEntryDateEnd() == null) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | // 结束时间默认是当前时间 |
| | | statisticsTableDto.setEntryDateEnd(new Date()); |
| | | // 开始时间默认是6个月前 |
| | | calendar.add(Calendar.MONTH, -6); |
| | | statisticsTableDto.setEntryDateStart(calendar.getTime()); |
| | | Date endDate = statisticsTableDto.getEntryDateEnd() != null ? statisticsTableDto.getEntryDateEnd() : new Date(); |
| | | statisticsTableDto.setEntryDateEnd(endDate); |
| | | |
| | | // 开始时间默认是12个月前 |
| | | Date startDate; |
| | | if (statisticsTableDto.getEntryDateStart() != null) { |
| | | startDate = statisticsTableDto.getEntryDateStart(); |
| | | } else { |
| | | calendar.setTime(endDate); |
| | | calendar.add(Calendar.MONTH, -11); // 减11个月,加上当前月共12个月 |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); // 设为月初 |
| | | startDate = calendar.getTime(); |
| | | } |
| | | statisticsTableDto.setEntryDateStart(startDate); |
| | | |
| | | // 查询数据库获取有数据的月份 |
| | | List<SalesTrendDto> salesTrendDtos = salesLedgerMapper.statisticsTable(statisticsTableDto); |
| | | if(CollectionUtils.isEmpty(salesTrendDtos)) return AjaxResult.success(map); |
| | | map.put("dateList", salesTrendDtos.stream().map(SalesTrendDto::getMonth).collect(Collectors.toList())); |
| | | map.put("orderCountList", salesTrendDtos.stream().map(SalesTrendDto::getOrderCount).collect(Collectors.toList())); |
| | | map.put("salesAmountList", salesTrendDtos.stream().map(SalesTrendDto::getSalesAmount).collect(Collectors.toList())); |
| | | map.put("shippingRateList", salesTrendDtos.stream().map(SalesTrendDto::getShipRate).collect(Collectors.toList())); |
| | | |
| | | // 创建月份到数据的映射 |
| | | Map<String, SalesTrendDto> trendMap = new HashMap<>(); |
| | | if (!CollectionUtils.isEmpty(salesTrendDtos)) { |
| | | for (SalesTrendDto dto : salesTrendDtos) { |
| | | trendMap.put(dto.getMonth(), dto); |
| | | } |
| | | } |
| | | |
| | | // 生成月份列表 |
| | | List<String> dateList = new ArrayList<>(); |
| | | List<BigDecimal> orderCountList = new ArrayList<>(); |
| | | List<BigDecimal> salesAmountList = new ArrayList<>(); |
| | | List<BigDecimal> shippingRateList = new ArrayList<>(); |
| | | |
| | | Calendar tempCalendar = Calendar.getInstance(); |
| | | tempCalendar.setTime(startDate); |
| | | tempCalendar.set(Calendar.DAY_OF_MONTH, 1); // 确保从月初开始 |
| | | |
| | | Calendar endCalendar = Calendar.getInstance(); |
| | | endCalendar.setTime(endDate); |
| | | endCalendar.set(Calendar.DAY_OF_MONTH, 1); // 确保到月末 |
| | | |
| | | // 循环生成月份列表,直到达到结束月份 |
| | | while (!tempCalendar.after(endCalendar)) { |
| | | String monthStr = String.format("%04d-%02d", tempCalendar.get(Calendar.YEAR), tempCalendar.get(Calendar.MONTH) + 1); |
| | | dateList.add(monthStr); |
| | | |
| | | // 获取当前月份的数据,如果没有则使用默认值 |
| | | SalesTrendDto dto = trendMap.get(monthStr); |
| | | if (dto != null) { |
| | | orderCountList.add(new BigDecimal(dto.getOrderCount())); |
| | | salesAmountList.add(dto.getSalesAmount() != null ? dto.getSalesAmount() : BigDecimal.ZERO); |
| | | shippingRateList.add(new BigDecimal(String.valueOf(dto.getShipRate()))); |
| | | } else { |
| | | orderCountList.add(BigDecimal.ZERO); |
| | | salesAmountList.add(BigDecimal.ZERO); |
| | | shippingRateList.add(BigDecimal.ZERO); |
| | | } |
| | | |
| | | // 下一个月 |
| | | tempCalendar.add(Calendar.MONTH, 1); |
| | | } |
| | | |
| | | map.put("dateList", dateList); |
| | | map.put("orderCountList", orderCountList); |
| | | map.put("salesAmountList", salesAmountList); |
| | | map.put("shippingRateList", shippingRateList); |
| | | return AjaxResult.success(map); |
| | | } |
| | | } |