feat: move

This commit is contained in:
ah--desktop
2025-12-17 01:12:10 +08:00
parent ca15b80d86
commit cb35521713
14 changed files with 1347 additions and 551 deletions
@@ -12,14 +12,12 @@ import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.CreateShopDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopDetailQueryDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopProductFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopProductPageFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopStatisticsQueryDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.UpdateShopDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopDetailVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopProductDetailVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopStatisticsVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ProductVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopOverviewVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopVO;
import top.xyquan.ecomdmbbackend.modules.ecom.service.ShopService;
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
@@ -87,39 +85,21 @@ public class ShopController {
return ApiResponse.success(mediaImageVO);
}
@Operation(summary = "获取店铺当月统计信息", description = "获取当前店铺当月的销售额、订单数、客单价、成本、退款金额、推广费、费比等统计信息")
@GetMapping("/statistics/current-month/{id}")
@RequireRole(description = "查询店铺当月统计", allowUser = true)
public ApiResponse<ShopStatisticsVO> getShopCurrentMonthStatistics(
@Parameter(description = "店铺ID") @PathVariable Long id) {
ShopStatisticsVO statistics = shopService.getShopCurrentMonthStatistics(id);
return ApiResponse.success(statistics);
}
@Operation(summary = "获取店铺统计信息", description = "通过维度参数和时间范围返回不同维度指定时间的统计信息(销售额、订单数、客单价、成本、退款金额、推广费、费比)")
@PostMapping("/statistics")
@RequireRole(description = "查询店铺统计", allowUser = true)
public ApiResponse<ShopStatisticsVO> getShopStatistics(
@Operation(summary = "获取店铺总览", description = "获取店铺总览,返回日期范围内每日的销售额、订单数、客单价、成本、退款金额、推广费、费比统计数据,包含时间范围的总值和每日数据数组")
@PostMapping("/overview")
@RequireRole(description = "查询店铺总览", allowUser = true)
public ApiResponse<ShopOverviewVO> getShopOverview(
@Valid @RequestBody ShopStatisticsQueryDTO queryDTO) {
ShopStatisticsVO statistics = shopService.getShopStatistics(queryDTO);
return ApiResponse.success(statistics);
ShopOverviewVO overview = shopService.getShopOverview(queryDTO);
return ApiResponse.success(overview);
}
@Operation(summary = "分页获取店铺商品详细信息", description = "分页获取店铺商品的详细信息,包含商品基础信息和统计数据(销售额、订单数、客单价),支持按商品名称、商品编码筛选,日期范围参数不传默认为当月1号到今天")
@Operation(summary = "分页获取店铺商品信息", description = "通过中间表分页获取店铺商品信息,包含商品基本信息和规格列表")
@PostMapping("/products/page")
@RequireRole(description = "查询店铺商品列表", allowUser = true)
public ApiResponse<PageResultVO<ShopProductDetailVO>> getShopProductPage(
@Valid @RequestBody PageQueryDTO<ShopProductFilterDTO> pageQueryDTO) {
PageResultVO<ShopProductDetailVO> result = shopService.getShopProductPage(pageQueryDTO);
public ApiResponse<PageResultVO<ProductVO>> getShopProductPage(
@Valid @RequestBody PageQueryDTO<ShopProductPageFilterDTO> pageQueryDTO) {
PageResultVO<ProductVO> result = shopService.getShopProductPage(pageQueryDTO);
return ApiResponse.success(result);
}
@Operation(summary = "获取店铺详情", description = "获取店铺详情,包含统计信息(销售额、订单数、客单价、成本、退款金额、推广费、费比)和所有商品列表,支持按时间范围查询,日期范围参数不传默认为当月1号到今天")
@PostMapping("/detail")
@RequireRole(description = "查询店铺详情", allowUser = true)
public ApiResponse<ShopDetailVO> getShopDetail(
@Valid @RequestBody ShopDetailQueryDTO queryDTO) {
ShopDetailVO shopDetail = shopService.getShopDetail(queryDTO);
return ApiResponse.success(shopDetail);
}
}
@@ -0,0 +1,21 @@
package top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
/**
* 店铺商品分页查询过滤DTO
*/
@Data
@Schema(description = "店铺商品分页查询过滤DTO")
public class ShopProductPageFilterDTO {
/**
* 店铺ID
*/
@Schema(description = "店铺ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "店铺ID不能为空")
private Long shopId;
}
@@ -0,0 +1,99 @@
package top.xyquan.ecomdmbbackend.modules.ecom.model.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
/**
* 店铺总览VO
*/
@Data
@Schema(description = "店铺总览VO")
public class ShopOverviewVO {
/**
* 销售额统计
*/
@Schema(description = "销售额统计")
private DailyStatistics salesAmount;
/**
* 订单数统计
*/
@Schema(description = "订单数统计")
private DailyStatistics orderCount;
/**
* 客单价统计
*/
@Schema(description = "客单价统计")
private DailyStatistics averageOrderValue;
/**
* 成本统计
*/
@Schema(description = "成本统计")
private DailyStatistics costAmount;
/**
* 退款金额统计
*/
@Schema(description = "退款金额统计")
private DailyStatistics refundAmount;
/**
* 推广费统计
*/
@Schema(description = "推广费统计")
private DailyStatistics promotionFee;
/**
* 费比统计
*/
@Schema(description = "费比统计")
private DailyStatistics feeRatio;
/**
* 每日统计数据
*/
@Data
@Schema(description = "每日统计数据")
public static class DailyStatistics {
/**
* 时间范围的总值
*/
@Schema(description = "时间范围的总值")
private BigDecimal total;
/**
* 每日数据列表
*/
@Schema(description = "每日数据列表")
private List<DailyData> dailyDataList;
}
/**
* 每日数据项
*/
@Data
@Schema(description = "每日数据项")
public static class DailyData {
/**
* 日期
*/
@JsonFormat(pattern = "yyyy-MM-dd")
@Schema(description = "日期", example = "2024-01-01")
private LocalDate date;
/**
* 对应的值
*/
@Schema(description = "对应的值")
private BigDecimal value;
}
}
@@ -5,15 +5,13 @@ import org.springframework.web.multipart.MultipartFile;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.CreateShopDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopDetailQueryDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopProductFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopProductPageFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopStatisticsQueryDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.UpdateShopDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.ShopPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopDetailVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopProductDetailVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopStatisticsVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ProductVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopOverviewVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopVO;
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
@@ -63,34 +61,18 @@ public interface ShopService extends IService<ShopPO> {
MediaImageVO uploadLogo(MultipartFile file);
/**
* 获取店铺当月统计信息
*
* @param shopId 店铺ID
* @return 店铺统计信息
*/
ShopStatisticsVO getShopCurrentMonthStatistics(Long shopId);
/**
* 获取店铺指定时间范围的统计信息
* 获取店铺总览(每日统计数据)
*
* @param queryDTO 统计查询DTO
* @return 店铺统计信息
* @return 店铺总览VO
*/
ShopStatisticsVO getShopStatistics(ShopStatisticsQueryDTO queryDTO);
ShopOverviewVO getShopOverview(ShopStatisticsQueryDTO queryDTO);
/**
* 分页获取店铺商品详细信息
* 分页获取店铺商品信息
*
* @param pageQueryDTO 分页查询DTO
* @return 分页结果
*/
PageResultVO<ShopProductDetailVO> getShopProductPage(PageQueryDTO<ShopProductFilterDTO> pageQueryDTO);
/**
* 获取店铺详情(包含统计信息和商品分页列表)
*
* @param queryDTO 店铺详情查询DTO
* @return 店铺详情VO
*/
ShopDetailVO getShopDetail(ShopDetailQueryDTO queryDTO);
PageResultVO<ProductVO> getShopProductPage(PageQueryDTO<ShopProductPageFilterDTO> pageQueryDTO);
}
@@ -17,21 +17,22 @@ import top.xyquan.ecomdmbbackend.modules.ecom.mapper.PromotionFeeMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.mapper.ShopMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.mapper.ShopProductRelationMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.CreateShopDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopDetailQueryDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopProductFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopProductPageFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.ShopStatisticsQueryDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.shop.UpdateShopDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.OrderPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.OrderStatusPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.ProductSpecPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.PromotionFeePO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.ShopPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.ShopProductRelationPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopDetailVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopProductDetailVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopStatisticsVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ProductSpecVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ProductVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopOverviewVO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ShopVO;
import top.xyquan.ecomdmbbackend.modules.ecom.service.ProductSpecService;
import top.xyquan.ecomdmbbackend.modules.ecom.service.ShopService;
import top.xyquan.ecomdmbbackend.modules.media.service.MediaImageService;
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
@@ -42,7 +43,10 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -58,11 +62,12 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
private final OrderStatusMapper orderStatusMapper;
private final ProductMapper productMapper;
private final ShopProductRelationMapper shopProductRelationMapper;
private final ProductSpecService productSpecService;
public ShopServiceImpl(CurrentUserContext currentUserContext, MediaImageService mediaImageService,
OrderMapper orderMapper, PromotionFeeMapper promotionFeeMapper,
OrderStatusMapper orderStatusMapper, ProductMapper productMapper,
ShopProductRelationMapper shopProductRelationMapper) {
ShopProductRelationMapper shopProductRelationMapper, ProductSpecService productSpecService) {
this.currentUserContext = currentUserContext;
this.mediaImageService = mediaImageService;
this.orderMapper = orderMapper;
@@ -70,6 +75,7 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
this.orderStatusMapper = orderStatusMapper;
this.productMapper = productMapper;
this.shopProductRelationMapper = shopProductRelationMapper;
this.productSpecService = productSpecService;
}
/**
@@ -244,38 +250,31 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
}
/**
* 获取店铺当月统计信息
* 获取退款状态ID
*/
@Override
public ShopStatisticsVO getShopCurrentMonthStatistics(Long shopId) {
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
// 验证店铺是否存在
ShopPO shop = this.getById(shopId);
if (shop == null || !shop.getCompanyId().equals(companyId)) {
throw new BusinessException("店铺不存在");
}
// 获取当月开始和结束时间
LocalDate now = LocalDate.now();
LocalDate startDate = now.withDayOfMonth(1);
LocalDate endDate = now.withDayOfMonth(now.lengthOfMonth());
// 构建查询DTO
ShopStatisticsQueryDTO queryDTO = new ShopStatisticsQueryDTO();
queryDTO.setShopId(shopId);
queryDTO.setStartDate(startDate);
queryDTO.setEndDate(endDate);
return calculateStatistics(queryDTO, companyId);
private Long getRefundStatusId(Long companyId) {
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
.eq(OrderStatusPO::getStatusName, "已退款")
.eq(OrderStatusPO::getIsActive, 1);
OrderStatusPO status = orderStatusMapper.selectOne(wrapper);
return status != null ? status.getId() : null;
}
/**
* 获取店铺指定时间范围的统计信息
* 转换为VO
*/
private ShopVO convertToVO(ShopPO shop) {
ShopVO shopVO = new ShopVO();
BeanUtils.copyProperties(shop, shopVO);
return shopVO;
}
/**
* 获取店铺总览(每日统计数据)
*/
@Override
public ShopStatisticsVO getShopStatistics(ShopStatisticsQueryDTO queryDTO) {
public ShopOverviewVO getShopOverview(ShopStatisticsQueryDTO queryDTO) {
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
@@ -290,15 +289,6 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
throw new BusinessException("开始日期不能晚于结束日期");
}
return calculateStatistics(queryDTO, companyId);
}
/**
* 计算统计信息
*/
private ShopStatisticsVO calculateStatistics(ShopStatisticsQueryDTO queryDTO, Long companyId) {
ShopStatisticsVO statistics = new ShopStatisticsVO();
// 查询时间范围(订单使用orderDate,推广费使用feeDate)
LocalDateTime startDateTime = queryDTO.getStartDate().atStartOfDay();
LocalDateTime endDateTime = queryDTO.getEndDate().atTime(23, 59, 59);
@@ -322,87 +312,329 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
// 获取退款状态ID(状态名称为"已退款")
Long refundStatusId = getRefundStatusId(companyId);
// 计算销售额(商家实收金额总和)
BigDecimal salesAmount = orders.stream()
// 按日期分组订单数据
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
.collect(Collectors.groupingBy(order -> order.getOrderDate().toLocalDate()));
// 按日期分组推广费数据
Map<LocalDate, List<PromotionFeePO>> promotionFeesByDate = promotionFees.stream()
.collect(Collectors.groupingBy(PromotionFeePO::getFeeDate));
// 生成日期范围内的所有日期列表
List<LocalDate> dateList = new ArrayList<>();
LocalDate currentDate = queryDTO.getStartDate();
while (!currentDate.isAfter(queryDTO.getEndDate())) {
dateList.add(currentDate);
currentDate = currentDate.plusDays(1);
}
// 构建返回对象
ShopOverviewVO overviewVO = new ShopOverviewVO();
// 计算销售额统计
ShopOverviewVO.DailyStatistics salesAmountStats = calculateSalesAmountStatistics(
ordersByDate, dateList, orders);
overviewVO.setSalesAmount(salesAmountStats);
// 计算订单数统计
ShopOverviewVO.DailyStatistics orderCountStats = calculateOrderCountStatistics(
ordersByDate, dateList, orders);
overviewVO.setOrderCount(orderCountStats);
// 计算客单价统计
ShopOverviewVO.DailyStatistics averageOrderValueStats = calculateAverageOrderValueStatistics(
salesAmountStats, orderCountStats);
overviewVO.setAverageOrderValue(averageOrderValueStats);
// 计算成本统计
ShopOverviewVO.DailyStatistics costAmountStats = calculateCostAmountStatistics(
ordersByDate, dateList, orders);
overviewVO.setCostAmount(costAmountStats);
// 计算退款金额统计
ShopOverviewVO.DailyStatistics refundAmountStats = calculateRefundAmountStatistics(
ordersByDate, dateList, orders, refundStatusId);
overviewVO.setRefundAmount(refundAmountStats);
// 计算推广费统计
ShopOverviewVO.DailyStatistics promotionFeeStats = calculatePromotionFeeStatistics(
promotionFeesByDate, dateList, promotionFees);
overviewVO.setPromotionFee(promotionFeeStats);
// 计算费比统计
ShopOverviewVO.DailyStatistics feeRatioStats = calculateFeeRatioStatistics(
salesAmountStats, promotionFeeStats);
overviewVO.setFeeRatio(feeRatioStats);
return overviewVO;
}
/**
* 计算销售额统计
*/
private ShopOverviewVO.DailyStatistics calculateSalesAmountStatistics(
Map<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList, List<OrderPO> allOrders) {
ShopOverviewVO.DailyStatistics stats = new ShopOverviewVO.DailyStatistics();
// 计算总销售额
BigDecimal total = allOrders.stream()
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
statistics.setSalesAmount(salesAmount);
stats.setTotal(total);
// 计算订单数(按平台订单号去重)
long orderCount = orders.stream()
// 计算每日销售额
List<ShopOverviewVO.DailyData> dailyDataList = dateList.stream()
.map(date -> {
ShopOverviewVO.DailyData dailyData = new ShopOverviewVO.DailyData();
dailyData.setDate(date);
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
BigDecimal dayAmount = dayOrders.stream()
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
dailyData.setValue(dayAmount);
return dailyData;
})
.sorted(Comparator.comparing(ShopOverviewVO.DailyData::getDate))
.collect(Collectors.toList());
stats.setDailyDataList(dailyDataList);
return stats;
}
/**
* 计算订单数统计
*/
private ShopOverviewVO.DailyStatistics calculateOrderCountStatistics(
Map<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList, List<OrderPO> allOrders) {
ShopOverviewVO.DailyStatistics stats = new ShopOverviewVO.DailyStatistics();
// 计算总订单数(按平台订单号去重)
long totalCount = allOrders.stream()
.map(OrderPO::getPlatformOrderNo)
.filter(no -> no != null && !no.isEmpty())
.distinct()
.count();
// 如果没有平台订单号,则使用订单记录数
if (orderCount == 0) {
orderCount = orders.size();
if (totalCount == 0) {
totalCount = allOrders.size();
}
statistics.setOrderCount(orderCount);
stats.setTotal(BigDecimal.valueOf(totalCount));
// 计算客单价(销售额/订单数)
BigDecimal averageOrderValue = orderCount > 0
? salesAmount.divide(BigDecimal.valueOf(orderCount), 2, RoundingMode.HALF_UP)
// 计算每日订单数
List<ShopOverviewVO.DailyData> dailyDataList = dateList.stream()
.map(date -> {
ShopOverviewVO.DailyData dailyData = new ShopOverviewVO.DailyData();
dailyData.setDate(date);
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
long dayCount = dayOrders.stream()
.map(OrderPO::getPlatformOrderNo)
.filter(no -> no != null && !no.isEmpty())
.distinct()
.count();
if (dayCount == 0) {
dayCount = dayOrders.size();
}
dailyData.setValue(BigDecimal.valueOf(dayCount));
return dailyData;
})
.sorted(Comparator.comparing(ShopOverviewVO.DailyData::getDate))
.collect(Collectors.toList());
stats.setDailyDataList(dailyDataList);
return stats;
}
/**
* 计算客单价统计
*/
private ShopOverviewVO.DailyStatistics calculateAverageOrderValueStatistics(
ShopOverviewVO.DailyStatistics salesAmountStats, ShopOverviewVO.DailyStatistics orderCountStats) {
ShopOverviewVO.DailyStatistics stats = new ShopOverviewVO.DailyStatistics();
// 计算总客单价
BigDecimal totalSales = salesAmountStats.getTotal();
BigDecimal totalOrders = orderCountStats.getTotal();
BigDecimal total = totalOrders.compareTo(BigDecimal.ZERO) > 0
? totalSales.divide(totalOrders, 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
statistics.setAverageOrderValue(averageOrderValue);
stats.setTotal(total);
// 计算成本(成本价*数量总和)
BigDecimal costAmount = orders.stream()
// 计算每日客单价
List<ShopOverviewVO.DailyData> dailyDataList = new ArrayList<>();
for (int i = 0; i < salesAmountStats.getDailyDataList().size(); i++) {
ShopOverviewVO.DailyData salesData = salesAmountStats.getDailyDataList().get(i);
ShopOverviewVO.DailyData orderData = orderCountStats.getDailyDataList().get(i);
ShopOverviewVO.DailyData dailyData = new ShopOverviewVO.DailyData();
dailyData.setDate(salesData.getDate());
BigDecimal dayValue = orderData.getValue().compareTo(BigDecimal.ZERO) > 0
? salesData.getValue().divide(orderData.getValue(), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
dailyData.setValue(dayValue);
dailyDataList.add(dailyData);
}
stats.setDailyDataList(dailyDataList);
return stats;
}
/**
* 计算成本统计
*/
private ShopOverviewVO.DailyStatistics calculateCostAmountStatistics(
Map<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList, List<OrderPO> allOrders) {
ShopOverviewVO.DailyStatistics stats = new ShopOverviewVO.DailyStatistics();
// 计算总成本
BigDecimal total = allOrders.stream()
.map(order -> {
BigDecimal cost = order.getCostPrice() != null ? order.getCostPrice() : BigDecimal.ZERO;
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 0;
return cost.multiply(BigDecimal.valueOf(quantity));
})
.reduce(BigDecimal.ZERO, BigDecimal::add);
statistics.setCostAmount(costAmount);
stats.setTotal(total);
// 计算退款金额(已退款订单的实收金额总和)
BigDecimal refundAmount = BigDecimal.ZERO;
// 计算每日成本
List<ShopOverviewVO.DailyData> dailyDataList = dateList.stream()
.map(date -> {
ShopOverviewVO.DailyData dailyData = new ShopOverviewVO.DailyData();
dailyData.setDate(date);
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
BigDecimal dayCost = dayOrders.stream()
.map(order -> {
BigDecimal cost = order.getCostPrice() != null ? order.getCostPrice() : BigDecimal.ZERO;
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 0;
return cost.multiply(BigDecimal.valueOf(quantity));
})
.reduce(BigDecimal.ZERO, BigDecimal::add);
dailyData.setValue(dayCost);
return dailyData;
})
.sorted(Comparator.comparing(ShopOverviewVO.DailyData::getDate))
.collect(Collectors.toList());
stats.setDailyDataList(dailyDataList);
return stats;
}
/**
* 计算退款金额统计
*/
private ShopOverviewVO.DailyStatistics calculateRefundAmountStatistics(
Map<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList,
List<OrderPO> allOrders, Long refundStatusId) {
ShopOverviewVO.DailyStatistics stats = new ShopOverviewVO.DailyStatistics();
// 计算总退款金额
BigDecimal total = BigDecimal.ZERO;
if (refundStatusId != null) {
refundAmount = orders.stream()
total = allOrders.stream()
.filter(order -> refundStatusId.equals(order.getStatusId()))
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
statistics.setRefundAmount(refundAmount);
stats.setTotal(total);
// 计算推广费
BigDecimal promotionFee = promotionFees.stream()
// 计算每日退款金额
List<ShopOverviewVO.DailyData> dailyDataList = dateList.stream()
.map(date -> {
ShopOverviewVO.DailyData dailyData = new ShopOverviewVO.DailyData();
dailyData.setDate(date);
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
BigDecimal dayRefund = BigDecimal.ZERO;
if (refundStatusId != null) {
dayRefund = dayOrders.stream()
.filter(order -> refundStatusId.equals(order.getStatusId()))
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
dailyData.setValue(dayRefund);
return dailyData;
})
.sorted(Comparator.comparing(ShopOverviewVO.DailyData::getDate))
.collect(Collectors.toList());
stats.setDailyDataList(dailyDataList);
return stats;
}
/**
* 计算推广费统计
*/
private ShopOverviewVO.DailyStatistics calculatePromotionFeeStatistics(
Map<LocalDate, List<PromotionFeePO>> promotionFeesByDate, List<LocalDate> dateList,
List<PromotionFeePO> allPromotionFees) {
ShopOverviewVO.DailyStatistics stats = new ShopOverviewVO.DailyStatistics();
// 计算总推广费
BigDecimal total = allPromotionFees.stream()
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
statistics.setPromotionFee(promotionFee);
stats.setTotal(total);
// 计算费比(推广费/销售额)
BigDecimal feeRatio = salesAmount.compareTo(BigDecimal.ZERO) > 0
? promotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
statistics.setFeeRatio(feeRatio);
// 计算每日推广费
List<ShopOverviewVO.DailyData> dailyDataList = dateList.stream()
.map(date -> {
ShopOverviewVO.DailyData dailyData = new ShopOverviewVO.DailyData();
dailyData.setDate(date);
List<PromotionFeePO> dayFees = promotionFeesByDate.getOrDefault(date, List.of());
BigDecimal dayFee = dayFees.stream()
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
dailyData.setValue(dayFee);
return dailyData;
})
.sorted(Comparator.comparing(ShopOverviewVO.DailyData::getDate))
.collect(Collectors.toList());
stats.setDailyDataList(dailyDataList);
return statistics;
return stats;
}
/**
* 获取退款状态ID
* 计算费比统计
*/
private Long getRefundStatusId(Long companyId) {
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
.eq(OrderStatusPO::getStatusName, "已退款")
.eq(OrderStatusPO::getIsActive, 1);
OrderStatusPO status = orderStatusMapper.selectOne(wrapper);
return status != null ? status.getId() : null;
private ShopOverviewVO.DailyStatistics calculateFeeRatioStatistics(
ShopOverviewVO.DailyStatistics salesAmountStats, ShopOverviewVO.DailyStatistics promotionFeeStats) {
ShopOverviewVO.DailyStatistics stats = new ShopOverviewVO.DailyStatistics();
// 计算总费比
BigDecimal totalSales = salesAmountStats.getTotal();
BigDecimal totalPromotionFee = promotionFeeStats.getTotal();
BigDecimal total = totalSales.compareTo(BigDecimal.ZERO) > 0
? totalPromotionFee.divide(totalSales, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
stats.setTotal(total);
// 计算每日费比
List<ShopOverviewVO.DailyData> dailyDataList = new ArrayList<>();
for (int i = 0; i < salesAmountStats.getDailyDataList().size(); i++) {
ShopOverviewVO.DailyData salesData = salesAmountStats.getDailyDataList().get(i);
ShopOverviewVO.DailyData promotionData = promotionFeeStats.getDailyDataList().get(i);
ShopOverviewVO.DailyData dailyData = new ShopOverviewVO.DailyData();
dailyData.setDate(salesData.getDate());
BigDecimal dayValue = salesData.getValue().compareTo(BigDecimal.ZERO) > 0
? promotionData.getValue().divide(salesData.getValue(), 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
dailyData.setValue(dayValue);
dailyDataList.add(dailyData);
}
stats.setDailyDataList(dailyDataList);
return stats;
}
/**
* 分页获取店铺商品详细信息
* 分页获取店铺商品信息
*/
@Override
public PageResultVO<ShopProductDetailVO> getShopProductPage(PageQueryDTO<ShopProductFilterDTO> pageQueryDTO) {
public PageResultVO<ProductVO> getShopProductPage(PageQueryDTO<ShopProductPageFilterDTO> pageQueryDTO) {
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
// 获取过滤条件
ShopProductFilterDTO filter = pageQueryDTO.getFilters();
ShopProductPageFilterDTO filter = pageQueryDTO.getFilters();
if (filter == null || filter.getShopId() == null) {
throw new BusinessException("店铺ID不能为空");
}
@@ -415,18 +647,6 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
throw new BusinessException("店铺不存在");
}
// 确定统计日期范围(不传默认为当月1号到今天)
LocalDate startDate = filter.getStartDate() != null ? filter.getStartDate() : LocalDate.now().withDayOfMonth(1);
LocalDate endDate = filter.getEndDate() != null ? filter.getEndDate() : LocalDate.now();
// 验证日期范围
if (startDate.isAfter(endDate)) {
throw new BusinessException("开始日期不能晚于结束日期");
}
LocalDateTime startDateTime = startDate.atStartOfDay();
LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
// 创建分页对象
Page<ShopProductRelationPO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
@@ -435,33 +655,6 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
wrapper.eq(ShopProductRelationPO::getCompanyId, companyId)
.eq(ShopProductRelationPO::getShopId, shopId);
// 添加商品名称和编码过滤(需要关联商品表)
// 如果同时有商品名称和编码过滤,需要取交集
boolean hasProductFilter = (filter.getProductName() != null && !filter.getProductName().trim().isEmpty()) ||
(filter.getProductCode() != null && !filter.getProductCode().trim().isEmpty());
if (hasProductFilter) {
LambdaQueryWrapper<ProductPO> productWrapper = new LambdaQueryWrapper<>();
productWrapper.eq(ProductPO::getCompanyId, companyId);
if (filter.getProductName() != null && !filter.getProductName().trim().isEmpty()) {
productWrapper.like(ProductPO::getName, filter.getProductName().trim());
}
if (filter.getProductCode() != null && !filter.getProductCode().trim().isEmpty()) {
productWrapper.like(ProductPO::getProductCode, filter.getProductCode().trim());
}
List<ProductPO> products = productMapper.selectList(productWrapper);
if (products.isEmpty()) {
// 如果没有匹配的商品,返回空结果
return PageResultVO.create(List.of(), 0L, pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
}
List<Long> productIds = products.stream().map(ProductPO::getId).collect(Collectors.toList());
wrapper.in(ShopProductRelationPO::getProductId, productIds);
}
// 排序
wrapper.orderByAsc(ShopProductRelationPO::getSortOrder)
.orderByDesc(ShopProductRelationPO::getCreateTime);
@@ -469,29 +662,20 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
// 执行分页查询
IPage<ShopProductRelationPO> pageResult = shopProductRelationMapper.selectPage(page, wrapper);
// 转换为VO并填充统计数据
List<ShopProductDetailVO> voList = pageResult.getRecords().stream()
// 转换为VO并填充商品信息和规格信息
List<ProductVO> voList = pageResult.getRecords().stream()
.map(relation -> {
ShopProductDetailVO vo = new ShopProductDetailVO();
vo.setRelationId(relation.getId());
vo.setSortOrder(relation.getSortOrder());
vo.setCreateTime(relation.getCreateTime());
vo.setUpdateTime(relation.getUpdateTime());
// 查询商品信息
ProductPO product = productMapper.selectById(relation.getProductId());
if (product != null) {
vo.setProductId(product.getId());
vo.setProductName(product.getName());
vo.setProductCode(product.getProductCode());
vo.setMainImage(product.getMainImage());
if (product == null) {
return null;
}
// 统计订单数据(按日期)
calculateProductStatistics(vo, shopId, relation.getProductId(), companyId, startDateTime, endDateTime);
return vo;
// 转换为VO
ProductVO productVO = convertProductToVO(product);
return productVO;
})
.filter(productVO -> productVO != null)
.collect(Collectors.toList());
// 构建分页结果
@@ -499,138 +683,33 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopPO> implements
}
/**
* 计算商品统计数据
* 转换商品PO为VO(包含规格信息)
*/
private void calculateProductStatistics(ShopProductDetailVO vo, Long shopId, Long productId,
Long companyId, LocalDateTime startDateTime, LocalDateTime endDateTime) {
// 查询该店铺该商品在指定日期的订单
LambdaQueryWrapper<OrderPO> orderWrapper = new LambdaQueryWrapper<>();
orderWrapper.eq(OrderPO::getCompanyId, companyId)
.eq(OrderPO::getShopId, shopId)
.eq(OrderPO::getProductId, productId)
.ge(OrderPO::getOrderDate, startDateTime)
.le(OrderPO::getOrderDate, endDateTime);
private ProductVO convertProductToVO(ProductPO product) {
ProductVO productVO = new ProductVO();
BeanUtils.copyProperties(product, productVO);
List<OrderPO> orders = orderMapper.selectList(orderWrapper);
// 查询商品规格列表
LambdaQueryWrapper<ProductSpecPO> specWrapper = new LambdaQueryWrapper<>();
specWrapper.eq(ProductSpecPO::getProductId, product.getId())
.eq(ProductSpecPO::getCompanyId, product.getCompanyId())
.orderByAsc(ProductSpecPO::getCreateTime);
// 计算销售额(商家实收金额总和)
BigDecimal salesAmount = orders.stream()
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
vo.setSalesAmount(salesAmount);
// 计算订单数(按平台订单号去重)
long orderCount = orders.stream()
.map(OrderPO::getPlatformOrderNo)
.filter(no -> no != null && !no.isEmpty())
.distinct()
.count();
// 如果没有平台订单号,则使用订单记录数
if (orderCount == 0) {
orderCount = orders.size();
}
vo.setOrderCount(orderCount);
// 计算客单价(销售额/订单数)
BigDecimal averageOrderValue = orderCount > 0
? salesAmount.divide(BigDecimal.valueOf(orderCount), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
vo.setAverageOrderValue(averageOrderValue);
}
/**
* 转换为VO
*/
private ShopVO convertToVO(ShopPO shop) {
ShopVO shopVO = new ShopVO();
BeanUtils.copyProperties(shop, shopVO);
return shopVO;
}
/**
* 获取店铺详情(包含统计信息和商品列表)
*/
@Override
public ShopDetailVO getShopDetail(ShopDetailQueryDTO queryDTO) {
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
// 验证店铺是否存在
ShopPO shop = this.getById(queryDTO.getShopId());
if (shop == null || !shop.getCompanyId().equals(companyId)) {
throw new BusinessException("店铺不存在");
}
// 确定统计日期范围(不传默认为当月1号到今天)
LocalDate startDate = queryDTO.getStartDate() != null ? queryDTO.getStartDate() : LocalDate.now().withDayOfMonth(1);
LocalDate endDate = queryDTO.getEndDate() != null ? queryDTO.getEndDate() : LocalDate.now();
// 验证日期范围
if (startDate.isAfter(endDate)) {
throw new BusinessException("开始日期不能晚于结束日期");
}
LocalDateTime startDateTime = startDate.atStartOfDay();
LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
// 获取店铺统计信息
ShopStatisticsQueryDTO statisticsQueryDTO = new ShopStatisticsQueryDTO();
statisticsQueryDTO.setShopId(queryDTO.getShopId());
statisticsQueryDTO.setStartDate(startDate);
statisticsQueryDTO.setEndDate(endDate);
ShopStatisticsVO statistics = calculateStatistics(statisticsQueryDTO, companyId);
// 获取所有商品列表
List<ShopProductDetailVO> products = getAllShopProducts(queryDTO.getShopId(), companyId, startDateTime, endDateTime);
// 组装返回结果
ShopDetailVO shopDetailVO = new ShopDetailVO();
shopDetailVO.setStatistics(statistics);
shopDetailVO.setProducts(products);
return shopDetailVO;
}
/**
* 获取店铺所有商品详细信息
*/
private List<ShopProductDetailVO> getAllShopProducts(Long shopId, Long companyId,
LocalDateTime startDateTime, LocalDateTime endDateTime) {
// 构建查询条件(店铺商品关联表)
LambdaQueryWrapper<ShopProductRelationPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ShopProductRelationPO::getCompanyId, companyId)
.eq(ShopProductRelationPO::getShopId, shopId);
// 排序
wrapper.orderByAsc(ShopProductRelationPO::getSortOrder)
.orderByDesc(ShopProductRelationPO::getCreateTime);
// 查询所有商品关联记录
List<ShopProductRelationPO> relations = shopProductRelationMapper.selectList(wrapper);
// 转换为VO并填充统计数据
return relations.stream()
.map(relation -> {
ShopProductDetailVO vo = new ShopProductDetailVO();
vo.setRelationId(relation.getId());
vo.setSortOrder(relation.getSortOrder());
vo.setCreateTime(relation.getCreateTime());
vo.setUpdateTime(relation.getUpdateTime());
// 查询商品信息
ProductPO product = productMapper.selectById(relation.getProductId());
if (product != null) {
vo.setProductId(product.getId());
vo.setProductName(product.getName());
vo.setProductCode(product.getProductCode());
vo.setMainImage(product.getMainImage());
}
// 统计订单数据(按日期范围)
calculateProductStatistics(vo, shopId, relation.getProductId(), companyId, startDateTime, endDateTime);
return vo;
})
List<ProductSpecPO> specPOs = productSpecService.list(specWrapper);
List<ProductSpecVO> specVOs = specPOs.stream()
.map(this::convertSpecToVO)
.collect(Collectors.toList());
productVO.setSpecs(specVOs);
return productVO;
}
/**
* 转换规格PO为VO
*/
private ProductSpecVO convertSpecToVO(ProductSpecPO productSpec) {
ProductSpecVO productSpecVO = new ProductSpecVO();
BeanUtils.copyProperties(productSpec, productSpecVO);
return productSpecVO;
}
}