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<>(); 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()); } List 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())); return AjaxResult.success(map); } }