feat: 统一工作台与概览页筛选器,商品概览支持按店铺过滤

This commit is contained in:
小蟑螂
2026-05-23 15:18:17 +08:00
parent 6531523c57
commit 34f2f83a5e
11 changed files with 1038 additions and 124 deletions
@@ -35,6 +35,12 @@ public class ProductOverviewQueryDTO {
@NotNull(message = "结束日期不能为空")
private LocalDate endDate;
/**
* 店铺ID(可选;传入时仅统计该店铺下该商品的订单与推广费分摊)
*/
@Schema(description = "店铺ID(可选)", example = "1")
private Long shopId;
/**
* 税率(默认0.04)
*/
@@ -16,8 +16,10 @@ import top.xyquan.ecomdmbbackend.modules.ecom.product.overview.service.ProductOv
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductService;
import top.xyquan.ecomdmbbackend.modules.ecom.promotion.mapper.PromotionFeeMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.promotion.model.po.PromotionFeePO;
import top.xyquan.ecomdmbbackend.modules.ecom.shop.model.po.ShopPO;
import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.mapper.ShopMonthlyTargetMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.model.po.ShopMonthlyTargetPO;
import top.xyquan.ecomdmbbackend.modules.ecom.shop.service.ShopService;
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import java.math.BigDecimal;
@@ -41,6 +43,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
private final ProductService productService;
private final ProductSpecMapper productSpecMapper;
private final ShopMonthlyTargetMapper shopMonthlyTargetMapper;
private final ShopService shopService;
public ProductOverviewServiceImpl(CurrentUserContext currentUserContext,
OrderMapper orderMapper,
@@ -48,7 +51,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
OrderStatusStatResolver orderStatusStatResolver,
ProductService productService,
ProductSpecMapper productSpecMapper,
ShopMonthlyTargetMapper shopMonthlyTargetMapper) {
ShopMonthlyTargetMapper shopMonthlyTargetMapper,
ShopService shopService) {
this.currentUserContext = currentUserContext;
this.orderMapper = orderMapper;
this.promotionFeeMapper = promotionFeeMapper;
@@ -56,6 +60,29 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
this.productService = productService;
this.productSpecMapper = productSpecMapper;
this.shopMonthlyTargetMapper = shopMonthlyTargetMapper;
this.shopService = shopService;
}
private void applyProductOverviewOrderScope(LambdaQueryWrapper<OrderPO> wrapper,
Long companyId,
Long productId,
Long shopId,
LocalDateTime startDateTime,
LocalDateTime endDateTime,
List<Long> unionStatusIds) {
wrapper.eq(OrderPO::getCompanyId, companyId)
.apply("EXISTS (SELECT 1 FROM ec_product_spec s WHERE s.id = ec_order.product_spec_id AND s.product_id = {0})",
productId)
.ge(OrderPO::getOrderTime, startDateTime)
.le(OrderPO::getOrderTime, endDateTime);
if (shopId != null) {
wrapper.eq(OrderPO::getShopId, shopId);
}
if (!unionStatusIds.isEmpty()) {
wrapper.in(OrderPO::getStatusId, unionStatusIds);
} else {
wrapper.isNull(OrderPO::getId);
}
}
/** 税额 = max(0, 销售额 − 退款) × 税率,保留两位小数。 */
@@ -85,6 +112,14 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
throw new BusinessException("开始日期不能晚于结束日期");
}
Long filterShopId = queryDTO.getShopId();
if (filterShopId != null) {
ShopPO shop = shopService.getById(filterShopId);
if (shop == null || !shop.getCompanyId().equals(companyId)) {
throw new BusinessException("店铺不存在");
}
}
LocalDate startDate = queryDTO.getStartDate();
LocalDate endDate = queryDTO.getEndDate();
LocalDateTime startDateTime = startDate.atStartOfDay();
@@ -100,24 +135,21 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
// 查询订单数据
LambdaQueryWrapper<OrderPO> orderWrapper = new LambdaQueryWrapper<>();
orderWrapper.eq(OrderPO::getCompanyId, companyId)
.apply("EXISTS (SELECT 1 FROM ec_product_spec s WHERE s.id = ec_order.product_spec_id AND s.product_id = {0})",
queryDTO.getProductId())
.ge(OrderPO::getOrderTime, startDateTime)
.le(OrderPO::getOrderTime, endDateTime);
if (!unionStatusIds.isEmpty()) {
orderWrapper.in(OrderPO::getStatusId, unionStatusIds);
} else {
orderWrapper.isNull(OrderPO::getId);
}
applyProductOverviewOrderScope(orderWrapper, companyId, queryDTO.getProductId(), filterShopId,
startDateTime, endDateTime, unionStatusIds);
List<OrderPO> orders = orderMapper.selectList(orderWrapper);
// 收集店铺ID列表
List<Long> shopIds = orders.stream()
.map(OrderPO::getShopId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
// 推广费/达成率所用店铺:指定店铺时固定该店,否则取订单涉及店铺
List<Long> shopIds;
if (filterShopId != null) {
shopIds = List.of(filterShopId);
} else {
shopIds = orders.stream()
.map(OrderPO::getShopId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
}
// 查询推广费(相关店铺 + 日期)
List<PromotionFeePO> promotionFees = new ArrayList<>();
@@ -154,16 +186,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
LocalDateTime lastEndTime = lastEndDate.atTime(23, 59, 59);
LambdaQueryWrapper<OrderPO> lastPeriodWrapper = new LambdaQueryWrapper<>();
lastPeriodWrapper.eq(OrderPO::getCompanyId, companyId)
.apply("EXISTS (SELECT 1 FROM ec_product_spec s WHERE s.id = ec_order.product_spec_id AND s.product_id = {0})",
queryDTO.getProductId())
.ge(OrderPO::getOrderTime, lastStartTime)
.le(OrderPO::getOrderTime, lastEndTime);
if (!unionStatusIds.isEmpty()) {
lastPeriodWrapper.in(OrderPO::getStatusId, unionStatusIds);
} else {
lastPeriodWrapper.isNull(OrderPO::getId);
}
applyProductOverviewOrderScope(lastPeriodWrapper, companyId, queryDTO.getProductId(), filterShopId,
lastStartTime, lastEndTime, unionStatusIds);
List<OrderPO> lastPeriodOrders = orderMapper.selectList(lastPeriodWrapper);
// ========== 构建返回对象 ==========