mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
refactor(ecom): 订单状态统计规则从单一开关升级为多维度独立控制
- ec_order_status 表 auto_statistics 拆分为四个独立字段:include_sales_amount / include_product_cost / include_freight_cost / include_other_cost / include_refund - CompanyOverviewServiceImpl / ShopOverviewStatisticsServiceImpl / ProductOverviewServiceImpl / ExportStatusRangeHelper 全部改为按各维度独立统计 - OrderStatus PO/DTO/VO 层字段更新,Service 层校验逻辑同步调整 - ProductSpecImportHelper 导入逻辑适配新字段 - 前端订单状态配置页适配新字段及展示 - 新增 OrderStatusCard.vue 卡片组件 - 更新 ec_design.sql 设计文档
This commit is contained in:
+5
-1
@@ -464,7 +464,11 @@ public class ProductSpecImportHelper {
|
||||
orderStatus.setStatusName(trimmed);
|
||||
orderStatus.setDescription("自动创建的状态");
|
||||
orderStatus.setSortOrder(0);
|
||||
orderStatus.setAutoStatistics(1);
|
||||
orderStatus.setIncludeSalesAmount(1);
|
||||
orderStatus.setIncludeProductCost(1);
|
||||
orderStatus.setIncludeFreightCost(1);
|
||||
orderStatus.setIncludeOtherCost(1);
|
||||
orderStatus.setIncludeRefund(0);
|
||||
orderStatus.setCreateBy(createBy);
|
||||
orderStatusService.save(orderStatus);
|
||||
|
||||
|
||||
+13
-12
@@ -2,7 +2,6 @@ package top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@@ -37,17 +36,19 @@ public class CreateOrderStatusDTO {
|
||||
@Schema(description = "排序", example = "1")
|
||||
private Integer sortOrder;
|
||||
|
||||
/**
|
||||
* 是否自动参与统计 0不参与 1参与
|
||||
*/
|
||||
@Schema(description = "是否自动参与统计", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "是否自动参与统计不能为空")
|
||||
private Integer autoStatistics;
|
||||
@Schema(description = "是否计入销售额/单量等统计 0否 1是", example = "1")
|
||||
private Integer includeSalesAmount;
|
||||
|
||||
/**
|
||||
* 是否为退款状态 0否 1是
|
||||
*/
|
||||
@Schema(description = "是否为退款状态", example = "0")
|
||||
private Integer isRefund;
|
||||
@Schema(description = "是否计入商品成本统计 0否 1是", example = "1")
|
||||
private Integer includeProductCost;
|
||||
|
||||
@Schema(description = "是否计入运费成本统计 0否 1是", example = "1")
|
||||
private Integer includeFreightCost;
|
||||
|
||||
@Schema(description = "是否计入其他成本统计 0否 1是", example = "1")
|
||||
private Integer includeOtherCost;
|
||||
|
||||
@Schema(description = "是否计入退款金额统计 0否 1是", example = "0")
|
||||
private Integer includeRefund;
|
||||
}
|
||||
|
||||
|
||||
+13
-10
@@ -10,15 +10,18 @@ import lombok.Data;
|
||||
@Schema(description = "部分更新订单状态DTO")
|
||||
public class PatchOrderStatusDTO {
|
||||
|
||||
/**
|
||||
* 是否自动参与统计 0不参与 1参与
|
||||
*/
|
||||
@Schema(description = "是否自动参与统计", example = "1")
|
||||
private Integer autoStatistics;
|
||||
@Schema(description = "是否计入销售额/单量等统计 0否 1是", example = "1")
|
||||
private Integer includeSalesAmount;
|
||||
|
||||
/**
|
||||
* 是否为退款状态 0否 1是
|
||||
*/
|
||||
@Schema(description = "是否为退款状态", example = "0")
|
||||
private Integer isRefund;
|
||||
@Schema(description = "是否计入商品成本统计 0否 1是", example = "1")
|
||||
private Integer includeProductCost;
|
||||
|
||||
@Schema(description = "是否计入运费成本统计 0否 1是", example = "1")
|
||||
private Integer includeFreightCost;
|
||||
|
||||
@Schema(description = "是否计入其他成本统计 0否 1是", example = "1")
|
||||
private Integer includeOtherCost;
|
||||
|
||||
@Schema(description = "是否计入退款金额统计 0否 1是", example = "0")
|
||||
private Integer includeRefund;
|
||||
}
|
||||
|
||||
+18
-11
@@ -37,17 +37,24 @@ public class UpdateOrderStatusDTO {
|
||||
@Schema(description = "排序", example = "1")
|
||||
private Integer sortOrder;
|
||||
|
||||
/**
|
||||
* 是否自动参与统计 0不参与 1参与
|
||||
*/
|
||||
@Schema(description = "是否自动参与统计", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "是否自动参与统计不能为空")
|
||||
private Integer autoStatistics;
|
||||
@Schema(description = "是否计入销售额/单量等统计 0否 1是", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "includeSalesAmount 不能为空")
|
||||
private Integer includeSalesAmount;
|
||||
|
||||
/**
|
||||
* 是否为退款状态 0否 1是
|
||||
*/
|
||||
@Schema(description = "是否为退款状态", example = "0")
|
||||
private Integer isRefund;
|
||||
@Schema(description = "是否计入商品成本统计 0否 1是", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "includeProductCost 不能为空")
|
||||
private Integer includeProductCost;
|
||||
|
||||
@Schema(description = "是否计入运费成本统计 0否 1是", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "includeFreightCost 不能为空")
|
||||
private Integer includeFreightCost;
|
||||
|
||||
@Schema(description = "是否计入其他成本统计 0否 1是", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "includeOtherCost 不能为空")
|
||||
private Integer includeOtherCost;
|
||||
|
||||
@Schema(description = "是否计入退款金额统计 0否 1是", example = "0", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "includeRefund 不能为空")
|
||||
private Integer includeRefund;
|
||||
}
|
||||
|
||||
|
||||
+24
-6
@@ -51,16 +51,34 @@ public class OrderStatusPO {
|
||||
private Integer sortOrder;
|
||||
|
||||
/**
|
||||
* 是否自动参与统计 0不参与 1参与
|
||||
* 是否计入销售额/单量等统计 0否 1是
|
||||
*/
|
||||
@TableField("auto_statistics")
|
||||
private Integer autoStatistics;
|
||||
@TableField("include_sales_amount")
|
||||
private Integer includeSalesAmount;
|
||||
|
||||
/**
|
||||
* 是否为退款状态 0否 1是
|
||||
* 是否计入商品成本统计 0否 1是
|
||||
*/
|
||||
@TableField("is_refund")
|
||||
private Integer isRefund;
|
||||
@TableField("include_product_cost")
|
||||
private Integer includeProductCost;
|
||||
|
||||
/**
|
||||
* 是否计入运费成本统计 0否 1是
|
||||
*/
|
||||
@TableField("include_freight_cost")
|
||||
private Integer includeFreightCost;
|
||||
|
||||
/**
|
||||
* 是否计入其他成本统计 0否 1是
|
||||
*/
|
||||
@TableField("include_other_cost")
|
||||
private Integer includeOtherCost;
|
||||
|
||||
/**
|
||||
* 是否计入退款金额统计 0否 1是
|
||||
*/
|
||||
@TableField("include_refund")
|
||||
private Integer includeRefund;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
|
||||
+13
-10
@@ -48,17 +48,20 @@ public class OrderStatusVO {
|
||||
@Schema(description = "排序", example = "1")
|
||||
private Integer sortOrder;
|
||||
|
||||
/**
|
||||
* 是否自动参与统计 0不参与 1参与
|
||||
*/
|
||||
@Schema(description = "是否自动参与统计", example = "1")
|
||||
private Integer autoStatistics;
|
||||
@Schema(description = "是否计入销售额/单量等统计 0否 1是", example = "1")
|
||||
private Integer includeSalesAmount;
|
||||
|
||||
/**
|
||||
* 是否为退款状态 0否 1是
|
||||
*/
|
||||
@Schema(description = "是否为退款状态", example = "0")
|
||||
private Integer isRefund;
|
||||
@Schema(description = "是否计入商品成本统计 0否 1是", example = "1")
|
||||
private Integer includeProductCost;
|
||||
|
||||
@Schema(description = "是否计入运费成本统计 0否 1是", example = "1")
|
||||
private Integer includeFreightCost;
|
||||
|
||||
@Schema(description = "是否计入其他成本统计 0否 1是", example = "1")
|
||||
private Integer includeOtherCost;
|
||||
|
||||
@Schema(description = "是否计入退款金额统计 0否 1是", example = "0")
|
||||
private Integer includeRefund;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ public interface OrderStatusService extends IService<OrderStatusPO> {
|
||||
OrderStatusVO updateOrderStatus(UpdateOrderStatusDTO updateOrderStatusDTO, Long id);
|
||||
|
||||
/**
|
||||
* 部分更新订单状态(仅更新 autoStatistics 和 isRefund)
|
||||
* 部分更新订单状态(仅更新各统计维度开关)
|
||||
*
|
||||
* @param patchDTO 部分更新DTO
|
||||
* @param id 订单状态ID
|
||||
|
||||
+34
-12
@@ -59,12 +59,7 @@ public class OrderStatusServiceImpl extends ServiceImpl<OrderStatusMapper, Order
|
||||
if (orderStatus.getSortOrder() == null) {
|
||||
orderStatus.setSortOrder(0);
|
||||
}
|
||||
if (orderStatus.getAutoStatistics() == null) {
|
||||
orderStatus.setAutoStatistics(1);
|
||||
}
|
||||
if (orderStatus.getIsRefund() == null) {
|
||||
orderStatus.setIsRefund(0);
|
||||
}
|
||||
applyStatDefaultsForCreate(orderStatus);
|
||||
|
||||
// 设置创建人ID
|
||||
orderStatus.setCreateBy(currentUserContext.getCurrentUserId());
|
||||
@@ -121,7 +116,7 @@ public class OrderStatusServiceImpl extends ServiceImpl<OrderStatusMapper, Order
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分更新订单状态(仅更新 autoStatistics 和 isRefund)
|
||||
* 部分更新订单状态(仅更新各统计维度开关)
|
||||
*
|
||||
* @param patchDTO 部分更新DTO
|
||||
* @param id 订单状态ID
|
||||
@@ -138,12 +133,20 @@ public class OrderStatusServiceImpl extends ServiceImpl<OrderStatusMapper, Order
|
||||
throw new BusinessException("订单状态不存在");
|
||||
}
|
||||
|
||||
// 只更新传入的字段
|
||||
if (patchDTO.getAutoStatistics() != null) {
|
||||
existingStatus.setAutoStatistics(patchDTO.getAutoStatistics());
|
||||
if (patchDTO.getIncludeSalesAmount() != null) {
|
||||
existingStatus.setIncludeSalesAmount(patchDTO.getIncludeSalesAmount());
|
||||
}
|
||||
if (patchDTO.getIsRefund() != null) {
|
||||
existingStatus.setIsRefund(patchDTO.getIsRefund());
|
||||
if (patchDTO.getIncludeProductCost() != null) {
|
||||
existingStatus.setIncludeProductCost(patchDTO.getIncludeProductCost());
|
||||
}
|
||||
if (patchDTO.getIncludeFreightCost() != null) {
|
||||
existingStatus.setIncludeFreightCost(patchDTO.getIncludeFreightCost());
|
||||
}
|
||||
if (patchDTO.getIncludeOtherCost() != null) {
|
||||
existingStatus.setIncludeOtherCost(patchDTO.getIncludeOtherCost());
|
||||
}
|
||||
if (patchDTO.getIncludeRefund() != null) {
|
||||
existingStatus.setIncludeRefund(patchDTO.getIncludeRefund());
|
||||
}
|
||||
existingStatus.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
@@ -228,5 +231,24 @@ public class OrderStatusServiceImpl extends ServiceImpl<OrderStatusMapper, Order
|
||||
BeanUtils.copyProperties(orderStatus, vo);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** 创建时未传的统计开关默认:前四项参与(等同原 auto_statistics=1),退款不参与 */
|
||||
private void applyStatDefaultsForCreate(OrderStatusPO p) {
|
||||
if (p.getIncludeSalesAmount() == null) {
|
||||
p.setIncludeSalesAmount(1);
|
||||
}
|
||||
if (p.getIncludeProductCost() == null) {
|
||||
p.setIncludeProductCost(1);
|
||||
}
|
||||
if (p.getIncludeFreightCost() == null) {
|
||||
p.setIncludeFreightCost(1);
|
||||
}
|
||||
if (p.getIncludeOtherCost() == null) {
|
||||
p.setIncludeOtherCost(1);
|
||||
}
|
||||
if (p.getIncludeRefund() == null) {
|
||||
p.setIncludeRefund(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ecom.order.status.support;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.service.OrderStatusService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 加载当前公司的订单状态并解析为 {@link OrderStatusStatSets}。
|
||||
*/
|
||||
@Component
|
||||
public class OrderStatusStatResolver {
|
||||
|
||||
private final OrderStatusService orderStatusService;
|
||||
|
||||
public OrderStatusStatResolver(OrderStatusService orderStatusService) {
|
||||
this.orderStatusService = orderStatusService;
|
||||
}
|
||||
|
||||
public OrderStatusStatSets resolve(Long companyId) {
|
||||
if (companyId == null) {
|
||||
return OrderStatusStatSets.from(List.of());
|
||||
}
|
||||
List<OrderStatusPO> list = orderStatusService.list(
|
||||
new LambdaQueryWrapper<OrderStatusPO>()
|
||||
.eq(OrderStatusPO::getCompanyId, companyId));
|
||||
return OrderStatusStatSets.from(list);
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ecom.order.status.support;
|
||||
|
||||
import lombok.Getter;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 订单状态在各统计维度上的生效集合(由 {@link OrderStatusPO} 列表解析)。
|
||||
*/
|
||||
@Getter
|
||||
public final class OrderStatusStatSets {
|
||||
|
||||
private final List<Long> unionStatusIds;
|
||||
private final Set<Long> salesIds;
|
||||
private final Set<Long> productCostIds;
|
||||
private final Set<Long> freightCostIds;
|
||||
private final Set<Long> otherCostIds;
|
||||
private final Set<Long> refundIds;
|
||||
|
||||
private OrderStatusStatSets(List<Long> unionStatusIds,
|
||||
Set<Long> salesIds,
|
||||
Set<Long> productCostIds,
|
||||
Set<Long> freightCostIds,
|
||||
Set<Long> otherCostIds,
|
||||
Set<Long> refundIds) {
|
||||
this.unionStatusIds = unionStatusIds;
|
||||
this.salesIds = salesIds;
|
||||
this.productCostIds = productCostIds;
|
||||
this.freightCostIds = freightCostIds;
|
||||
this.otherCostIds = otherCostIds;
|
||||
this.refundIds = refundIds;
|
||||
}
|
||||
|
||||
public static OrderStatusStatSets from(List<OrderStatusPO> statuses) {
|
||||
Set<Long> union = new HashSet<>();
|
||||
Set<Long> sales = new HashSet<>();
|
||||
Set<Long> productCost = new HashSet<>();
|
||||
Set<Long> freightCost = new HashSet<>();
|
||||
Set<Long> otherCost = new HashSet<>();
|
||||
Set<Long> refund = new HashSet<>();
|
||||
for (OrderStatusPO s : statuses) {
|
||||
Long id = s.getId();
|
||||
if (id == null) {
|
||||
continue;
|
||||
}
|
||||
boolean any = false;
|
||||
if (isOn(s.getIncludeSalesAmount())) {
|
||||
sales.add(id);
|
||||
any = true;
|
||||
}
|
||||
if (isOn(s.getIncludeProductCost())) {
|
||||
productCost.add(id);
|
||||
any = true;
|
||||
}
|
||||
if (isOn(s.getIncludeFreightCost())) {
|
||||
freightCost.add(id);
|
||||
any = true;
|
||||
}
|
||||
if (isOn(s.getIncludeOtherCost())) {
|
||||
otherCost.add(id);
|
||||
any = true;
|
||||
}
|
||||
if (isOn(s.getIncludeRefund())) {
|
||||
refund.add(id);
|
||||
any = true;
|
||||
}
|
||||
if (any) {
|
||||
union.add(id);
|
||||
}
|
||||
}
|
||||
List<Long> unionList = new ArrayList<>(union);
|
||||
Collections.sort(unionList);
|
||||
return new OrderStatusStatSets(
|
||||
Collections.unmodifiableList(unionList),
|
||||
Collections.unmodifiableSet(sales),
|
||||
Collections.unmodifiableSet(productCost),
|
||||
Collections.unmodifiableSet(freightCost),
|
||||
Collections.unmodifiableSet(otherCost),
|
||||
Collections.unmodifiableSet(refund));
|
||||
}
|
||||
|
||||
private static boolean isOn(Integer v) {
|
||||
return Objects.equals(v, 1);
|
||||
}
|
||||
|
||||
public boolean isUnionEmpty() {
|
||||
return unionStatusIds.isEmpty();
|
||||
}
|
||||
|
||||
public boolean includesSales(Long statusId) {
|
||||
return statusId != null && salesIds.contains(statusId);
|
||||
}
|
||||
|
||||
public boolean includesProductCost(Long statusId) {
|
||||
return statusId != null && productCostIds.contains(statusId);
|
||||
}
|
||||
|
||||
public boolean includesFreightCost(Long statusId) {
|
||||
return statusId != null && freightCostIds.contains(statusId);
|
||||
}
|
||||
|
||||
public boolean includesOtherCost(Long statusId) {
|
||||
return statusId != null && otherCostIds.contains(statusId);
|
||||
}
|
||||
|
||||
public boolean includesRefund(Long statusId) {
|
||||
return statusId != null && refundIds.contains(statusId);
|
||||
}
|
||||
|
||||
public List<Long> getRefundStatusIds() {
|
||||
return refundIds.stream().sorted().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static int quantity(OrderPO order) {
|
||||
Integer q = order.getQuantity();
|
||||
return q != null && q > 0 ? q : 1;
|
||||
}
|
||||
|
||||
public BigDecimal receivedForSales(OrderPO order) {
|
||||
return includesSales(order.getStatusId())
|
||||
? nz(order.getReceivedAmount())
|
||||
: BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
public BigDecimal productCostForStats(OrderPO order) {
|
||||
if (!includesProductCost(order.getStatusId())) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
BigDecimal c = nz(order.getProductCost());
|
||||
return c.multiply(BigDecimal.valueOf(quantity(order)));
|
||||
}
|
||||
|
||||
public BigDecimal freightCostForStats(OrderPO order) {
|
||||
if (!includesFreightCost(order.getStatusId())) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
BigDecimal c = nz(order.getShippingCost());
|
||||
return c.multiply(BigDecimal.valueOf(quantity(order)));
|
||||
}
|
||||
|
||||
public BigDecimal otherCostForStats(OrderPO order) {
|
||||
if (!includesOtherCost(order.getStatusId())) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
BigDecimal c = nz(order.getOtherCost());
|
||||
return c.multiply(BigDecimal.valueOf(quantity(order)));
|
||||
}
|
||||
|
||||
public BigDecimal totalCostForStats(OrderPO order) {
|
||||
return productCostForStats(order)
|
||||
.add(freightCostForStats(order))
|
||||
.add(otherCostForStats(order));
|
||||
}
|
||||
|
||||
public BigDecimal receivedForRefund(OrderPO order) {
|
||||
return includesRefund(order.getStatusId())
|
||||
? nz(order.getReceivedAmount())
|
||||
: BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
private static BigDecimal nz(BigDecimal v) {
|
||||
return v != null ? v : BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
+6
-20
@@ -11,8 +11,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.promotion.mapper.PromotionFeeMappe
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.CompanyOverviewQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.brushorder.model.po.BrushOrderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.service.OrderStatusService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatResolver;
|
||||
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;
|
||||
@@ -55,7 +54,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
private final BrushOrderMapper brushOrderMapper;
|
||||
private final ShopMonthlyTargetMapper shopMonthlyTargetMapper;
|
||||
private final ShopService shopService;
|
||||
private final OrderStatusService orderStatusService;
|
||||
private final OrderStatusStatResolver orderStatusStatResolver;
|
||||
private final ProductMapper productMapper;
|
||||
private final ProductSpecMapper productSpecMapper;
|
||||
private final CategoryService categoryService;
|
||||
@@ -67,7 +66,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
BrushOrderMapper brushOrderMapper,
|
||||
ShopMonthlyTargetMapper shopMonthlyTargetMapper,
|
||||
ShopService shopService,
|
||||
OrderStatusService orderStatusService,
|
||||
OrderStatusStatResolver orderStatusStatResolver,
|
||||
ProductMapper productMapper,
|
||||
ProductSpecMapper productSpecMapper,
|
||||
CategoryService categoryService,
|
||||
@@ -78,7 +77,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
this.brushOrderMapper = brushOrderMapper;
|
||||
this.shopMonthlyTargetMapper = shopMonthlyTargetMapper;
|
||||
this.shopService = shopService;
|
||||
this.orderStatusService = orderStatusService;
|
||||
this.orderStatusStatResolver = orderStatusStatResolver;
|
||||
this.productMapper = productMapper;
|
||||
this.productSpecMapper = productSpecMapper;
|
||||
this.categoryService = categoryService;
|
||||
@@ -813,13 +812,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
* 获取退款状态ID(通过 is_refund 字段查询)
|
||||
*/
|
||||
private List<Long> getRefundStatusIds() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
return orderStatusStatResolver.resolve(currentUserContext.getCurrentCompanyId()).getRefundStatusIds();
|
||||
}
|
||||
|
||||
/** 订单行上的 product_spec_id → 商品 id */
|
||||
@@ -1077,14 +1070,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
}
|
||||
|
||||
private List<Long> getAutoStatisticsStatusIds(Long companyId) {
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getAutoStatistics, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream()
|
||||
.map(OrderStatusPO::getId)
|
||||
.collect(Collectors.toList());
|
||||
return orderStatusStatResolver.resolve(companyId).getUnionStatusIds();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+14
-19
@@ -1,14 +1,12 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ecom.overview.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.service.OrderStatusService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatResolver;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatSets;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 导出共用的日期区间与订单状态 ID 解析
|
||||
@@ -16,10 +14,10 @@ import java.util.stream.Collectors;
|
||||
@Component
|
||||
public class ExportStatusRangeHelper {
|
||||
|
||||
private final OrderStatusService orderStatusService;
|
||||
private final OrderStatusStatResolver orderStatusStatResolver;
|
||||
|
||||
public ExportStatusRangeHelper(OrderStatusService orderStatusService) {
|
||||
this.orderStatusService = orderStatusService;
|
||||
public ExportStatusRangeHelper(OrderStatusStatResolver orderStatusStatResolver) {
|
||||
this.orderStatusStatResolver = orderStatusStatResolver;
|
||||
}
|
||||
|
||||
public LocalDate[] parseDateRange(List<LocalDate> dateRange) {
|
||||
@@ -34,21 +32,18 @@ public class ExportStatusRangeHelper {
|
||||
return new LocalDate[]{start, end};
|
||||
}
|
||||
|
||||
/**
|
||||
* 任一会参与统计的维度为 1 的订单状态 ID(用于拉取订单并集)
|
||||
*/
|
||||
public List<Long> getAutoStatusIds(Long companyId) {
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(
|
||||
new LambdaQueryWrapper<OrderStatusPO>()
|
||||
.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getAutoStatistics, 1)
|
||||
.select(OrderStatusPO::getId));
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
return orderStatusStatResolver.resolve(companyId).getUnionStatusIds();
|
||||
}
|
||||
|
||||
public List<Long> getRefundStatusIds(Long companyId) {
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
return orderStatusStatResolver.resolve(companyId).getRefundStatusIds();
|
||||
}
|
||||
|
||||
public OrderStatusStatSets resolveStat(Long companyId) {
|
||||
return orderStatusStatResolver.resolve(companyId);
|
||||
}
|
||||
}
|
||||
|
||||
+63
-132
@@ -11,8 +11,8 @@ import top.xyquan.ecomdmbbackend.modules.ecom.promotion.mapper.PromotionFeeMappe
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.ShopOverviewQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.brushorder.model.po.BrushOrderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.service.OrderStatusService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatResolver;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatSets;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductSpecMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
|
||||
@@ -56,7 +56,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
private final ShopService shopService;
|
||||
private final ProductService productService;
|
||||
private final ProductSpecMapper productSpecMapper;
|
||||
private final OrderStatusService orderStatusService;
|
||||
private final OrderStatusStatResolver orderStatusStatResolver;
|
||||
private final CategoryService categoryService;
|
||||
|
||||
public ShopOverviewStatisticsServiceImpl(CurrentUserContext currentUserContext,
|
||||
@@ -67,7 +67,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
ShopService shopService,
|
||||
ProductService productService,
|
||||
ProductSpecMapper productSpecMapper,
|
||||
OrderStatusService orderStatusService,
|
||||
OrderStatusStatResolver orderStatusStatResolver,
|
||||
CategoryService categoryService) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.orderMapper = orderMapper;
|
||||
@@ -77,7 +77,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
this.shopService = shopService;
|
||||
this.productService = productService;
|
||||
this.productSpecMapper = productSpecMapper;
|
||||
this.orderStatusService = orderStatusService;
|
||||
this.orderStatusStatResolver = orderStatusStatResolver;
|
||||
this.categoryService = categoryService;
|
||||
}
|
||||
|
||||
@@ -104,7 +104,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
|
||||
LocalDateTime startDateTime = startDate.atStartOfDay();
|
||||
LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
|
||||
List<Long> autoStatisticsStatusIds = getAutoStatisticsStatusIds(companyId);
|
||||
OrderStatusStatSets stat = orderStatusStatResolver.resolve(companyId);
|
||||
List<Long> unionStatusIds = stat.getUnionStatusIds();
|
||||
|
||||
// 验证店铺是否存在
|
||||
ShopPO shop = shopService.getById(queryDTO.getShopId());
|
||||
@@ -118,8 +119,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
.eq(OrderPO::getShopId, queryDTO.getShopId())
|
||||
.ge(OrderPO::getOrderTime, startDateTime)
|
||||
.le(OrderPO::getOrderTime, endDateTime);
|
||||
if (!autoStatisticsStatusIds.isEmpty()) {
|
||||
orderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
|
||||
if (!unionStatusIds.isEmpty()) {
|
||||
orderWrapper.in(OrderPO::getStatusId, unionStatusIds);
|
||||
} else {
|
||||
orderWrapper.isNull(OrderPO::getId);
|
||||
}
|
||||
@@ -141,9 +142,6 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
.le(BrushOrderPO::getBrushTime, endDateTime);
|
||||
List<BrushOrderPO> brushOrders = brushOrderMapper.selectList(brushWrapper);
|
||||
|
||||
// 获取所有退款状态ID列表
|
||||
List<Long> refundStatusIds = getRefundStatusIds();
|
||||
|
||||
// 计算上月同期数据(用于增长百分比)
|
||||
LocalDate lastMonthStartDate = startDate.minusMonths(1);
|
||||
LocalDate lastMonthEndDate = endDate.minusMonths(1);
|
||||
@@ -155,8 +153,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
.eq(OrderPO::getShopId, queryDTO.getShopId())
|
||||
.ge(OrderPO::getOrderTime, lastMonthStartDateTime)
|
||||
.le(OrderPO::getOrderTime, lastMonthEndDateTime);
|
||||
if (!autoStatisticsStatusIds.isEmpty()) {
|
||||
lastMonthOrderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
|
||||
if (!unionStatusIds.isEmpty()) {
|
||||
lastMonthOrderWrapper.in(OrderPO::getStatusId, unionStatusIds);
|
||||
} else {
|
||||
lastMonthOrderWrapper.isNull(OrderPO::getId);
|
||||
}
|
||||
@@ -171,15 +169,15 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
overviewVO.setShopName(shopEntity.getName());
|
||||
}
|
||||
|
||||
// 1. 计算店铺总销售额
|
||||
// 1. 计算店铺总销售额(按状态「计入销售额」维度)
|
||||
BigDecimal totalSalesAmount = orders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
overviewVO.setTotalSalesAmount(totalSalesAmount);
|
||||
|
||||
// 2. 计算较上月同期增长百分比
|
||||
BigDecimal lastMonthSalesAmount = lastMonthOrders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal monthOverMonthGrowth = BigDecimal.ZERO;
|
||||
if (lastMonthSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||
@@ -191,18 +189,14 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
}
|
||||
overviewVO.setMonthOverMonthGrowth(monthOverMonthGrowth);
|
||||
|
||||
// 3. 计算店铺总单量
|
||||
int totalOrderCount = orders.size();
|
||||
// 3. 计算店铺总单量(按状态「计入销售额」维度计单)
|
||||
int totalOrderCount = (int) orders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||
overviewVO.setTotalOrderCount(totalOrderCount);
|
||||
|
||||
// 3.1 退款金额、客单价(概览页与导出汇总表共用)
|
||||
BigDecimal refundAmountAgg = BigDecimal.ZERO;
|
||||
if (!refundStatusIds.isEmpty()) {
|
||||
refundAmountAgg = orders.stream()
|
||||
.filter(o -> refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
}
|
||||
// 3.1 退款金额、客单价(按状态「计入退款」维度)
|
||||
BigDecimal refundAmountAgg = orders.stream()
|
||||
.map(stat::receivedForRefund)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
overviewVO.setRefundAmount(refundAmountAgg);
|
||||
BigDecimal averageOrderValue = BigDecimal.ZERO;
|
||||
if (totalOrderCount > 0) {
|
||||
@@ -225,7 +219,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
overviewVO.setAverageFeeRatio(averageFeeRatio);
|
||||
|
||||
// 6. 计算店铺总净利(税后)和总税额
|
||||
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders);
|
||||
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat);
|
||||
overviewVO.setTotalProfitBeforeTax(totalProfitBeforeTax);
|
||||
BigDecimal totalSalesTaxAmount = totalSalesAmount.multiply(taxRate);
|
||||
BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalSalesTaxAmount);
|
||||
@@ -236,24 +230,24 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
calculateTargetData(overviewVO, companyId, queryDTO.getShopId(), startDate, endDate, totalSalesAmount, totalOrderCount);
|
||||
|
||||
// 8. 汇总ROI和总税后盈亏
|
||||
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusIds);
|
||||
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, stat);
|
||||
overviewVO.setTotalROI(totalROI);
|
||||
overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax);
|
||||
|
||||
// 9. 计算趋势分析数据
|
||||
ShopOverviewStatisticsVO.TrendData salesAmountTrend = calculateSalesAmountTrend(orders, startDate, endDate);
|
||||
ShopOverviewStatisticsVO.TrendData orderCountTrend = calculateOrderCountTrend(orders, startDate, endDate);
|
||||
ShopOverviewStatisticsVO.TrendData salesAmountTrend = calculateSalesAmountTrend(orders, startDate, endDate, stat);
|
||||
ShopOverviewStatisticsVO.TrendData orderCountTrend = calculateOrderCountTrend(orders, startDate, endDate, stat);
|
||||
overviewVO.setSalesAmountTrend(salesAmountTrend);
|
||||
overviewVO.setOrderCountTrend(orderCountTrend);
|
||||
|
||||
// 10. 多商品数据横向汇总
|
||||
ShopOverviewStatisticsVO.ProductOverview productOverview = calculateProductComparisons(
|
||||
orders, promotionFees, brushOrders, refundStatusIds);
|
||||
orders, promotionFees, brushOrders, stat);
|
||||
overviewVO.setProductOverview(productOverview);
|
||||
|
||||
// 11. 品类多维度数据汇总
|
||||
ShopOverviewStatisticsVO.CategoryOverview categoryOverview = calculateCategoryComparisons(
|
||||
orders, promotionFees, brushOrders, refundStatusIds);
|
||||
orders, promotionFees, brushOrders, stat);
|
||||
overviewVO.setCategoryOverview(categoryOverview);
|
||||
|
||||
return overviewVO;
|
||||
@@ -262,17 +256,9 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
/**
|
||||
* 计算总利润(税前)
|
||||
*/
|
||||
private BigDecimal calculateTotalProfit(List<OrderPO> orders) {
|
||||
private BigDecimal calculateTotalProfit(List<OrderPO> orders, OrderStatusStatSets stat) {
|
||||
return orders.stream()
|
||||
.map(order -> {
|
||||
BigDecimal receivedAmount = order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO;
|
||||
BigDecimal productCost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO;
|
||||
BigDecimal shippingCost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO;
|
||||
BigDecimal otherCost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO;
|
||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
||||
BigDecimal totalCost = productCost.add(shippingCost).add(otherCost).multiply(new BigDecimal(quantity));
|
||||
return receivedAmount.subtract(totalCost);
|
||||
})
|
||||
.map(order -> stat.receivedForSales(order).subtract(stat.totalCostForStats(order)))
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
}
|
||||
|
||||
@@ -335,13 +321,12 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
* 计算汇总ROI
|
||||
*/
|
||||
private BigDecimal calculateTotalROI(List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat) {
|
||||
BigDecimal totalSales = orders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal totalRefund = orders.stream()
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForRefund)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal totalPromotionFee = promotionFees.stream()
|
||||
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
||||
@@ -393,7 +378,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
*/
|
||||
private ShopOverviewStatisticsVO.ProductOverview calculateProductComparisons(
|
||||
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat) {
|
||||
List<ShopOverviewStatisticsVO.ProductComparison> comparisons = new ArrayList<>();
|
||||
|
||||
Map<Long, Long> specToProduct = orderSpecIdToProductIdMap(orders);
|
||||
@@ -413,7 +398,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal totalSalesAmount = orders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
for (Map.Entry<Long, List<OrderPO>> entry : ordersByProduct.entrySet()) {
|
||||
@@ -431,14 +416,15 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
|
||||
// 计算各项指标
|
||||
BigDecimal salesAmount = productOrders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
int orderCount = productOrders.size();
|
||||
int orderCount = (int) productOrders.stream()
|
||||
.filter(o -> stat.includesSales(o.getStatusId()))
|
||||
.count();
|
||||
|
||||
BigDecimal refundAmount = productOrders.stream()
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForRefund)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
BigDecimal brushFee = brushOrdersByProduct.getOrDefault(productId, new ArrayList<>()).stream()
|
||||
@@ -454,32 +440,20 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
|
||||
// 计算商品成本
|
||||
BigDecimal productCost = productOrders.stream()
|
||||
.map(order -> {
|
||||
BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO;
|
||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
||||
return cost.multiply(new BigDecimal(quantity));
|
||||
})
|
||||
.map(stat::productCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 计算运费成本
|
||||
BigDecimal shippingCost = productOrders.stream()
|
||||
.map(order -> {
|
||||
BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO;
|
||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
||||
return cost.multiply(new BigDecimal(quantity));
|
||||
})
|
||||
.map(stat::freightCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 计算其他成本
|
||||
BigDecimal otherCost = productOrders.stream()
|
||||
.map(order -> {
|
||||
BigDecimal cost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO;
|
||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
||||
return cost.multiply(new BigDecimal(quantity));
|
||||
})
|
||||
.map(stat::otherCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
BigDecimal totalCost = calculateProductTotalCost(productOrders);
|
||||
BigDecimal totalCost = calculateProductTotalCost(productOrders, stat);
|
||||
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
|
||||
|
||||
// 销售额4%税额
|
||||
@@ -597,31 +571,25 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
/**
|
||||
* 计算商品总成本(订单成本)
|
||||
*/
|
||||
private BigDecimal calculateProductTotalCost(List<OrderPO> orders) {
|
||||
private BigDecimal calculateProductTotalCost(List<OrderPO> orders, OrderStatusStatSets stat) {
|
||||
return orders.stream()
|
||||
.map(order -> {
|
||||
BigDecimal productCost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO;
|
||||
BigDecimal shippingCost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO;
|
||||
BigDecimal otherCost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO;
|
||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
||||
return productCost.add(shippingCost).add(otherCost).multiply(new BigDecimal(quantity));
|
||||
})
|
||||
.map(stat::totalCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算销售额趋势分析
|
||||
*/
|
||||
private ShopOverviewStatisticsVO.TrendData calculateSalesAmountTrend(List<OrderPO> orders, LocalDate startDate, LocalDate endDate) {
|
||||
private ShopOverviewStatisticsVO.TrendData calculateSalesAmountTrend(List<OrderPO> orders, LocalDate startDate,
|
||||
LocalDate endDate, OrderStatusStatSets stat) {
|
||||
ShopOverviewStatisticsVO.TrendData trendData = new ShopOverviewStatisticsVO.TrendData();
|
||||
|
||||
// 按日期分组订单数据
|
||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||
.collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate()));
|
||||
|
||||
// 计算总销售额
|
||||
BigDecimal total = orders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
trendData.setTotal(total);
|
||||
|
||||
@@ -640,7 +608,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
item.setDate(date);
|
||||
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, new ArrayList<>());
|
||||
BigDecimal dayAmount = dayOrders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
item.setValue(dayAmount);
|
||||
return item;
|
||||
@@ -655,15 +623,15 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
/**
|
||||
* 计算单量趋势分析
|
||||
*/
|
||||
private ShopOverviewStatisticsVO.TrendData calculateOrderCountTrend(List<OrderPO> orders, LocalDate startDate, LocalDate endDate) {
|
||||
private ShopOverviewStatisticsVO.TrendData calculateOrderCountTrend(List<OrderPO> orders, LocalDate startDate,
|
||||
LocalDate endDate, OrderStatusStatSets stat) {
|
||||
ShopOverviewStatisticsVO.TrendData trendData = new ShopOverviewStatisticsVO.TrendData();
|
||||
|
||||
// 按日期分组订单数据
|
||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||
.collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate()));
|
||||
|
||||
// 计算总单量
|
||||
int totalCount = orders.size();
|
||||
int totalCount = (int) orders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||
trendData.setTotal(new BigDecimal(totalCount));
|
||||
|
||||
// 生成日期范围内的所有日期列表
|
||||
@@ -680,7 +648,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
ShopOverviewStatisticsVO.TrendDataItem item = new ShopOverviewStatisticsVO.TrendDataItem();
|
||||
item.setDate(date);
|
||||
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, new ArrayList<>());
|
||||
int dayCount = dayOrders.size();
|
||||
int dayCount = (int) dayOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||
item.setValue(new BigDecimal(dayCount));
|
||||
return item;
|
||||
})
|
||||
@@ -691,38 +659,12 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
return trendData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取退款状态ID(通过 is_refund 字段查询)
|
||||
*/
|
||||
private List<Long> getRefundStatusIds() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream()
|
||||
.map(OrderStatusPO::getId)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Long> getAutoStatisticsStatusIds(Long companyId) {
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getAutoStatistics, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream()
|
||||
.map(OrderStatusPO::getId)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算品类多维度数据汇总
|
||||
*/
|
||||
private ShopOverviewStatisticsVO.CategoryOverview calculateCategoryComparisons(
|
||||
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat) {
|
||||
|
||||
// 构建商品ID->品类ID的映射
|
||||
Map<Long, Long> productCategoryMap = new HashMap<>();
|
||||
@@ -760,7 +702,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal totalSalesAmount = orders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 按品类聚合
|
||||
@@ -770,14 +712,13 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
List<OrderPO> catOrders = entry.getValue();
|
||||
|
||||
BigDecimal salesAmount = catOrders.stream()
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
int orderCount = catOrders.size();
|
||||
int orderCount = (int) catOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||
|
||||
BigDecimal refundAmount = catOrders.stream()
|
||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForRefund)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 推广费按销售额比例
|
||||
@@ -787,27 +728,17 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
// 成本聚合
|
||||
BigDecimal productCost = catOrders.stream()
|
||||
.map(o -> {
|
||||
BigDecimal cost = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO;
|
||||
Integer qty = o.getQuantity() != null ? o.getQuantity() : 1;
|
||||
return cost.multiply(new BigDecimal(qty));
|
||||
}).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
.map(stat::productCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
BigDecimal shippingCost = catOrders.stream()
|
||||
.map(o -> {
|
||||
BigDecimal cost = o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO;
|
||||
Integer qty = o.getQuantity() != null ? o.getQuantity() : 1;
|
||||
return cost.multiply(new BigDecimal(qty));
|
||||
}).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
.map(stat::freightCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
BigDecimal otherCost = catOrders.stream()
|
||||
.map(o -> {
|
||||
BigDecimal cost = o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO;
|
||||
Integer qty = o.getQuantity() != null ? o.getQuantity() : 1;
|
||||
return cost.multiply(new BigDecimal(qty));
|
||||
}).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
.map(stat::otherCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
BigDecimal totalCost = productCost.add(shippingCost).add(otherCost);
|
||||
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
|
||||
|
||||
+68
-127
@@ -5,8 +5,8 @@ import org.springframework.stereotype.Service;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.mapper.OrderStatusMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatResolver;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatSets;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductSpecMapper;
|
||||
@@ -37,7 +37,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
private final CurrentUserContext currentUserContext;
|
||||
private final OrderMapper orderMapper;
|
||||
private final PromotionFeeMapper promotionFeeMapper;
|
||||
private final OrderStatusMapper orderStatusMapper;
|
||||
private final OrderStatusStatResolver orderStatusStatResolver;
|
||||
private final ProductService productService;
|
||||
private final ProductSpecMapper productSpecMapper;
|
||||
private final ShopMonthlyTargetMapper shopMonthlyTargetMapper;
|
||||
@@ -45,14 +45,14 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
public ProductOverviewServiceImpl(CurrentUserContext currentUserContext,
|
||||
OrderMapper orderMapper,
|
||||
PromotionFeeMapper promotionFeeMapper,
|
||||
OrderStatusMapper orderStatusMapper,
|
||||
OrderStatusStatResolver orderStatusStatResolver,
|
||||
ProductService productService,
|
||||
ProductSpecMapper productSpecMapper,
|
||||
ShopMonthlyTargetMapper shopMonthlyTargetMapper) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.orderMapper = orderMapper;
|
||||
this.promotionFeeMapper = promotionFeeMapper;
|
||||
this.orderStatusMapper = orderStatusMapper;
|
||||
this.orderStatusStatResolver = orderStatusStatResolver;
|
||||
this.productService = productService;
|
||||
this.productSpecMapper = productSpecMapper;
|
||||
this.shopMonthlyTargetMapper = shopMonthlyTargetMapper;
|
||||
@@ -83,7 +83,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
? queryDTO.getTaxRate()
|
||||
: new BigDecimal("0.04");
|
||||
|
||||
List<Long> autoStatisticsStatusIds = getAutoStatisticsStatusIds(companyId);
|
||||
OrderStatusStatSets stat = orderStatusStatResolver.resolve(companyId);
|
||||
List<Long> unionStatusIds = stat.getUnionStatusIds();
|
||||
|
||||
// 查询订单数据
|
||||
LambdaQueryWrapper<OrderPO> orderWrapper = new LambdaQueryWrapper<>();
|
||||
@@ -92,8 +93,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
queryDTO.getProductId())
|
||||
.ge(OrderPO::getOrderTime, startDateTime)
|
||||
.le(OrderPO::getOrderTime, endDateTime);
|
||||
if (!autoStatisticsStatusIds.isEmpty()) {
|
||||
orderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
|
||||
if (!unionStatusIds.isEmpty()) {
|
||||
orderWrapper.in(OrderPO::getStatusId, unionStatusIds);
|
||||
} else {
|
||||
orderWrapper.isNull(OrderPO::getId);
|
||||
}
|
||||
@@ -117,9 +118,6 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
promotionFees = promotionFeeMapper.selectList(promoWrapper);
|
||||
}
|
||||
|
||||
// 获取退款状态ID
|
||||
Set<Long> refundStatusIds = getRefundStatusIds(companyId);
|
||||
|
||||
// 查询上月同期数据(用于环比计算)
|
||||
long dayCount = ChronoUnit.DAYS.between(startDate, endDate) + 1;
|
||||
LocalDate lastStartDate = startDate.minusDays(dayCount);
|
||||
@@ -133,8 +131,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
queryDTO.getProductId())
|
||||
.ge(OrderPO::getOrderTime, lastStartTime)
|
||||
.le(OrderPO::getOrderTime, lastEndTime);
|
||||
if (!autoStatisticsStatusIds.isEmpty()) {
|
||||
lastPeriodWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
|
||||
if (!unionStatusIds.isEmpty()) {
|
||||
lastPeriodWrapper.in(OrderPO::getStatusId, unionStatusIds);
|
||||
} else {
|
||||
lastPeriodWrapper.isNull(OrderPO::getId);
|
||||
}
|
||||
@@ -146,22 +144,17 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
vo.setProductName(product.getName());
|
||||
|
||||
// ---------- KPI 汇总指标 ----------
|
||||
// 总销售额
|
||||
// 总销售额(按状态「计入销售额」维度)
|
||||
BigDecimal totalSalesAmount = orders.stream()
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
vo.setTotalSalesAmount(totalSalesAmount);
|
||||
|
||||
// 总订单量(按平台订单号去重)
|
||||
long totalOrderCount = orders.stream()
|
||||
.map(OrderPO::getPlatformOrderNo)
|
||||
.filter(no -> no != null && !no.isEmpty())
|
||||
.distinct()
|
||||
// 总订单量(按状态「计入销售额」维度计单,与店铺概览一致)
|
||||
int totalOrderCount = (int) orders.stream()
|
||||
.filter(o -> stat.includesSales(o.getStatusId()))
|
||||
.count();
|
||||
if (totalOrderCount == 0) {
|
||||
totalOrderCount = orders.size();
|
||||
}
|
||||
vo.setTotalOrderCount((int) totalOrderCount);
|
||||
vo.setTotalOrderCount(totalOrderCount);
|
||||
|
||||
// 汇总客单价
|
||||
BigDecimal avgOrderValue = totalOrderCount > 0
|
||||
@@ -169,16 +162,15 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
: BigDecimal.ZERO;
|
||||
vo.setAverageOrderValue(avgOrderValue);
|
||||
|
||||
// 总退款金额(退款状态订单的实收金额)
|
||||
// 总退款金额(按状态「计入退款」维度的实收金额)
|
||||
BigDecimal totalRefundAmount = orders.stream()
|
||||
.filter(o -> refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForRefund)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
vo.setRefundAmount(totalRefundAmount);
|
||||
|
||||
// 总成本(含数量)
|
||||
// 总成本(商品/运费/其他成本按各维度开关,含数量)
|
||||
BigDecimal totalCostAmount = orders.stream()
|
||||
.map(this::calcOrderCost)
|
||||
.map(stat::totalCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
vo.setAverageFeeRatio(null); // 由 spec 维度均化,待后面补充
|
||||
|
||||
@@ -199,9 +191,10 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
vo.setAverageFeeRatio(avgFeeRatio.multiply(new BigDecimal("100"))
|
||||
.setScale(2, RoundingMode.HALF_UP)); // 转为百分比
|
||||
|
||||
// 税前盈亏 = 销售额 - 成本 - 推广费
|
||||
BigDecimal totalProfitBeforeTax = totalSalesAmount
|
||||
.subtract(totalCostAmount)
|
||||
// 税前盈亏 = Σ(计入销售额的实收 - 按维度统计的成本) - 推广费
|
||||
BigDecimal totalProfitBeforeTax = orders.stream()
|
||||
.map(o -> stat.receivedForSales(o).subtract(stat.totalCostForStats(o)))
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add)
|
||||
.subtract(totalPromotionFee);
|
||||
vo.setTotalProfitBeforeTax(totalProfitBeforeTax);
|
||||
|
||||
@@ -215,17 +208,16 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
vo.setTotalProfitAfterTax(totalProfitAfterTax);
|
||||
vo.setTotalNetProfitAfterTax(totalProfitAfterTax);
|
||||
|
||||
// ROI = (销售额 - 成本) / 推广费
|
||||
// ROI = (销售额 - 退款金额) / 推广费(与店铺概览 calculateSalesROI 口径一致)
|
||||
BigDecimal totalROI = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0
|
||||
? totalSalesAmount.subtract(totalCostAmount)
|
||||
.divide(totalPromotionFee, 2, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal("100"))
|
||||
? totalSalesAmount.subtract(totalRefundAmount)
|
||||
.divide(totalPromotionFee, 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
vo.setTotalROI(totalROI);
|
||||
|
||||
// ---------- 环比增长(上月同期) ----------
|
||||
BigDecimal lastPeriodSales = lastPeriodOrders.stream()
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal monthOverMonthGrowth = BigDecimal.ZERO;
|
||||
if (lastPeriodSales.compareTo(BigDecimal.ZERO) > 0) {
|
||||
@@ -268,7 +260,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
vo.setSalesAmountAchievementRate(salesRate);
|
||||
}
|
||||
if (totalTargetOrderCount != null && totalTargetOrderCount > 0) {
|
||||
BigDecimal orderRate = BigDecimal.valueOf(totalOrderCount)
|
||||
BigDecimal orderRate = BigDecimal.valueOf((long) totalOrderCount)
|
||||
.divide(BigDecimal.valueOf(totalTargetOrderCount), 4, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal("100"))
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
@@ -278,29 +270,17 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
|
||||
// ---------- 趋势数据 ----------
|
||||
vo.setSalesAmountTrend(buildTrend(orders, dateList -> dateList.stream()
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add), startDate, endDate));
|
||||
|
||||
vo.setOrderCountTrend(buildOrderCountTrend(orders, startDate, endDate));
|
||||
vo.setOrderCountTrend(buildOrderCountTrend(orders, stat, startDate, endDate));
|
||||
|
||||
// ---------- 规格销售汇总 ----------
|
||||
vo.setSpecSummaries(buildSpecSummary(orders, promotionFees, refundStatusIds, taxRate, shopIds));
|
||||
vo.setSpecSummaries(buildSpecSummary(orders, promotionFees, stat, taxRate, shopIds));
|
||||
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算单笔订单总成本(含数量)
|
||||
*/
|
||||
private BigDecimal calcOrderCost(OrderPO order) {
|
||||
BigDecimal productCost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO;
|
||||
BigDecimal shippingCost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO;
|
||||
BigDecimal otherCost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO;
|
||||
int quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
||||
return productCost.add(shippingCost).add(otherCost)
|
||||
.multiply(BigDecimal.valueOf(quantity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建销售趋势
|
||||
*/
|
||||
@@ -337,6 +317,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
* 构建订单数趋势
|
||||
*/
|
||||
private ProductOverviewVO.TrendData buildOrderCountTrend(List<OrderPO> orders,
|
||||
OrderStatusStatSets stat,
|
||||
LocalDate startDate, LocalDate endDate) {
|
||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||
.collect(Collectors.groupingBy(o -> o.getOrderTime().toLocalDate()));
|
||||
@@ -345,13 +326,9 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
LocalDate current = startDate;
|
||||
while (!current.isAfter(endDate)) {
|
||||
List<OrderPO> dayOrders = ordersByDate.getOrDefault(current, List.of());
|
||||
// 按平台订单号去重
|
||||
long count = dayOrders.stream()
|
||||
.map(OrderPO::getPlatformOrderNo)
|
||||
.filter(no -> no != null && !no.isEmpty())
|
||||
.distinct()
|
||||
.filter(o -> stat.includesSales(o.getStatusId()))
|
||||
.count();
|
||||
if (count == 0) count = dayOrders.size();
|
||||
|
||||
ProductOverviewVO.TrendDataItem item = new ProductOverviewVO.TrendDataItem();
|
||||
item.setDate(current);
|
||||
@@ -360,9 +337,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
current = current.plusDays(1);
|
||||
}
|
||||
|
||||
BigDecimal total = BigDecimal.valueOf(dailyList.stream()
|
||||
.mapToLong(i -> i.getValue().longValue())
|
||||
.sum());
|
||||
long sumDaily = dailyList.stream().mapToLong(i -> i.getValue().longValue()).sum();
|
||||
BigDecimal total = BigDecimal.valueOf(sumDaily);
|
||||
|
||||
ProductOverviewVO.TrendData trend = new ProductOverviewVO.TrendData();
|
||||
trend.setTotal(total);
|
||||
@@ -375,7 +351,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
*/
|
||||
private ProductOverviewVO.SpecSummary buildSpecSummary(List<OrderPO> orders,
|
||||
List<PromotionFeePO> promotionFees,
|
||||
Set<Long> refundStatusIds,
|
||||
OrderStatusStatSets stat,
|
||||
BigDecimal taxRate,
|
||||
List<Long> shopIds) {
|
||||
// 查询商品所有规格
|
||||
@@ -426,18 +402,15 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
List<OrderPO> specOrders = entry.getValue();
|
||||
ProductSpecPO spec = specMap.get(specId);
|
||||
|
||||
// 该规格的销售额
|
||||
// 该规格的销售额(计入销售额维度)
|
||||
BigDecimal salesAmount = specOrders.stream()
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForSales)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 该规格的订单量(去重)
|
||||
long orderCount = specOrders.stream()
|
||||
.map(OrderPO::getPlatformOrderNo)
|
||||
.filter(no -> no != null && !no.isEmpty())
|
||||
.distinct()
|
||||
// 该规格的订单量(计入销售额维度计单)
|
||||
int orderCount = (int) specOrders.stream()
|
||||
.filter(o -> stat.includesSales(o.getStatusId()))
|
||||
.count();
|
||||
if (orderCount == 0) orderCount = specOrders.size();
|
||||
|
||||
// 总件数
|
||||
int quantity = specOrders.stream()
|
||||
@@ -445,38 +418,35 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
.reduce(0, Integer::sum);
|
||||
|
||||
// 客单价
|
||||
BigDecimal avgOV = BigDecimal.valueOf(orderCount).compareTo(BigDecimal.ZERO) > 0
|
||||
BigDecimal avgOV = orderCount > 0
|
||||
? salesAmount.divide(BigDecimal.valueOf(orderCount), 2, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
|
||||
// 商品成本
|
||||
// 商品 / 运费 / 其他成本(各维度开关 + 数量,与店铺商品对比一致)
|
||||
BigDecimal productCostTotal = specOrders.stream()
|
||||
.map(o -> {
|
||||
BigDecimal pc = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO;
|
||||
int qty = o.getQuantity() != null ? o.getQuantity() : 1;
|
||||
return pc.multiply(BigDecimal.valueOf(qty));
|
||||
})
|
||||
.map(stat::productCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 运费 + 其他成本
|
||||
BigDecimal shippingCostTotal = specOrders.stream()
|
||||
.map(o -> o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO)
|
||||
.map(stat::freightCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal otherCostTotal = specOrders.stream()
|
||||
.map(o -> o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO)
|
||||
.map(stat::otherCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 退款金额
|
||||
BigDecimal totalCostStat = specOrders.stream()
|
||||
.map(stat::totalCostForStats)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 退款金额(计入退款维度)
|
||||
BigDecimal refundAmount = specOrders.stream()
|
||||
.filter(o -> refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.map(stat::receivedForRefund)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
// 推广费分摊(按该规格订单数占所有订单数比例)
|
||||
int totalOrderCountAll = orders.size();
|
||||
BigDecimal specPromotionFee = BigDecimal.ZERO;
|
||||
if (totalOrderCountAll > 0) {
|
||||
BigDecimal proportion = BigDecimal.valueOf(orderCount)
|
||||
BigDecimal proportion = BigDecimal.valueOf((long) orderCount)
|
||||
.divide(BigDecimal.valueOf(totalOrderCountAll), 4, RoundingMode.HALF_UP);
|
||||
BigDecimal totalPromo = shopPromotionFeeMap.values().stream()
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
@@ -489,22 +459,20 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
? specPromotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
|
||||
// 税前盈亏
|
||||
// 税前盈亏(与店铺商品对比:销售额 - 总成本 - 退款金额 - 推广费)
|
||||
BigDecimal profitBeforeTax = salesAmount
|
||||
.subtract(productCostTotal)
|
||||
.subtract(shippingCostTotal)
|
||||
.subtract(otherCostTotal)
|
||||
.subtract(totalCostStat)
|
||||
.subtract(refundAmount)
|
||||
.subtract(specPromotionFee);
|
||||
|
||||
// 税额
|
||||
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate)
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
|
||||
// ROI
|
||||
// ROI = (销售额 - 退款) / 推广费
|
||||
BigDecimal roi = specPromotionFee.compareTo(BigDecimal.ZERO) > 0
|
||||
? salesAmount.subtract(productCostTotal)
|
||||
.divide(specPromotionFee, 2, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal("100"))
|
||||
? salesAmount.subtract(refundAmount)
|
||||
.divide(specPromotionFee, 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
|
||||
// 税后盈亏
|
||||
@@ -515,13 +483,13 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
item.setSpecName(spec != null ? spec.getName() : "未知规格");
|
||||
item.setSpecCode(spec != null ? spec.getSpecCode() : null);
|
||||
item.setSalesAmount(salesAmount);
|
||||
item.setOrderCount((int) orderCount);
|
||||
item.setOrderCount(orderCount);
|
||||
item.setQuantity(quantity);
|
||||
item.setAverageOrderValue(avgOV);
|
||||
item.setProductCost(productCostTotal);
|
||||
item.setShippingCost(shippingCostTotal);
|
||||
item.setOtherCost(otherCostTotal);
|
||||
item.setTotalCost(productCostTotal.add(shippingCostTotal).add(otherCostTotal));
|
||||
item.setTotalCost(totalCostStat);
|
||||
item.setRefundAmount(refundAmount);
|
||||
item.setPromotionFee(specPromotionFee);
|
||||
item.setFeeRatio(feeRatio.multiply(new BigDecimal("100"))
|
||||
@@ -571,19 +539,18 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
.setScale(2, RoundingMode.HALF_UP));
|
||||
|
||||
BigDecimal totalProfitBT = total.getSalesAmount()
|
||||
.subtract(total.getProductCost())
|
||||
.subtract(total.getShippingCost())
|
||||
.subtract(total.getOtherCost())
|
||||
.subtract(total.getTotalCost())
|
||||
.subtract(total.getRefundAmount())
|
||||
.subtract(total.getPromotionFee());
|
||||
total.setProfitBeforeTax(totalProfitBT);
|
||||
|
||||
BigDecimal totalSalesTax = totalSalesAmt.multiply(taxRate).setScale(2, RoundingMode.HALF_UP);
|
||||
total.setSalesTaxAmount(totalSalesTax);
|
||||
|
||||
BigDecimal totalRefundForRoi = total.getRefundAmount();
|
||||
BigDecimal totalRoi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
|
||||
? totalSalesAmt.subtract(total.getProductCost())
|
||||
.divide(totalPromoFee, 2, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal("100"))
|
||||
? totalSalesAmt.subtract(totalRefundForRoi)
|
||||
.divide(totalPromoFee, 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
total.setRoi(totalRoi);
|
||||
|
||||
@@ -618,30 +585,4 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
summary.setTotal(total);
|
||||
return summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参与统计的订单状态ID列表
|
||||
*/
|
||||
private List<Long> getAutoStatisticsStatusIds(Long companyId) {
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getAutoStatistics, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
return orderStatusMapper.selectList(wrapper).stream()
|
||||
.map(OrderStatusPO::getId)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取退款状态ID集合
|
||||
*/
|
||||
private Set<Long> getRefundStatusIds(Long companyId) {
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
return orderStatusMapper.selectList(wrapper).stream()
|
||||
.map(OrderStatusPO::getId)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user