package com.ruoyi.sales.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.sales.dto.SalesTrendDto; 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; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * @author :yys * @date : 2026/1/15 9:41 */ @Service @Slf4j public class MetricStatisticsServiceImpl { @Autowired private SalesLedgerMapper salesLedgerMapper; @Autowired private SalesLedgerProductMapper salesLedgerProductMapper; @Autowired private ShippingInfoMapper shippingInfoMapper; public AjaxResult total() { List salesLedgers = salesLedgerMapper.selectList(null); if(CollectionUtils.isEmpty(salesLedgers)) return AjaxResult.success(salesLedgers); Map map = new HashMap<>(); // 销售额 map.put("contractAmountTotal", salesLedgers.stream().map(SalesLedger::getContractAmount).reduce(BigDecimal.ZERO, BigDecimal::add)); // 订单数量 map.put("total", new BigDecimal(salesLedgers.size())); // 发货率 List salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper() .eq(SalesLedgerProduct::getType, 1)); map.put("shipRate", "0%"); if(CollectionUtils.isEmpty(salesLedgerProducts)) return AjaxResult.success(map); // 发货数量 long count = shippingInfoMapper.selectCount(new LambdaQueryWrapper() .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 map = new HashMap<>(); 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 salesTrendDtos = salesLedgerMapper.statisticsTable(statisticsTableDto); // 创建月份到数据的映射 Map trendMap = new HashMap<>(); if (!CollectionUtils.isEmpty(salesTrendDtos)) { for (SalesTrendDto dto : salesTrendDtos) { trendMap.put(dto.getMonth(), dto); } } // 生成月份列表 List dateList = new ArrayList<>(); List orderCountList = new ArrayList<>(); List salesAmountList = new ArrayList<>(); List 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); } }