From 8b5ae4f644b951f7ab62c9037c8ac445b0b680a0 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期五, 13 二月 2026 11:12:57 +0800
Subject: [PATCH] 指标统计近12个月的销售趋势分析
---
src/main/java/com/ruoyi/sales/service/impl/MetricStatisticsServiceImpl.java | 89 +++++++++++++++++++++++++++++++++++++-------
1 files changed, 74 insertions(+), 15 deletions(-)
diff --git a/src/main/java/com/ruoyi/sales/service/impl/MetricStatisticsServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/MetricStatisticsServiceImpl.java
index efa3909..010dec0 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/MetricStatisticsServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/MetricStatisticsServiceImpl.java
@@ -7,8 +7,10 @@
import com.ruoyi.sales.dto.StatisticsTableDto;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
+import com.ruoyi.sales.mapper.ShippingInfoMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
+import com.ruoyi.sales.pojo.ShippingInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -31,6 +33,9 @@
@Autowired
private SalesLedgerProductMapper salesLedgerProductMapper;
+ @Autowired
+ private ShippingInfoMapper shippingInfoMapper;
+
public AjaxResult total() {
List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(null);
if(CollectionUtils.isEmpty(salesLedgers)) return AjaxResult.success(salesLedgers);
@@ -45,29 +50,83 @@
map.put("shipRate", "0%");
if(CollectionUtils.isEmpty(salesLedgerProducts)) return AjaxResult.success(map);
// 鍙戣揣鏁伴噺
- long count = salesLedgerProducts.stream()
- .filter(salesLedgerProduct -> salesLedgerProduct.getApproveStatus().equals(2))
- .count();
+ long count = shippingInfoMapper.selectCount(new LambdaQueryWrapper<ShippingInfo>()
+ .in(ShippingInfo::getSalesLedgerProductId, salesLedgerProducts.stream().map(SalesLedgerProduct::getId).collect(Collectors.toList()))
+ .eq(ShippingInfo::getStatus,"宸插彂璐�"));
map.put("shipRate", String.format("%.2f", count * 100.0 / salesLedgerProducts.size()) + "%");
return AjaxResult.success(map);
}
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());
+ Calendar calendar = Calendar.getInstance();
+
+ // 缁撴潫鏃堕棿榛樿鏄綋鍓嶆椂闂�
+ 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);
}
}
--
Gitblit v1.9.3