mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 21:49:19 +08:00
refactor(ecom): CompanyOverviewServiceImpl 全面接入 OrderStatusStatResolver 多维度统计
- 用 OrderStatusStatSets 替换原有的 getAutoStatisticsStatusIds / getRefundStatusIds 逻辑 - 所有销售额/成本/退款计算改为 stat.receivedForSales / stat.totalCostForStats / stat.receivedForRefund 等方法 - 订单计数改用 stat.includesSales(statusId) 过滤,与单店概览口径一致 - 税额计算统一改用 this.taxRate 变量,移除硬编码的 0.04 - 删除 calculateShopTotalCost / calculateShopROI / calculateShopNetProfit 重载等冗余方法
This commit is contained in:
+98
-211
@@ -12,6 +12,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.CompanyOverview
|
|||||||
import top.xyquan.ecomdmbbackend.modules.ecom.brushorder.model.po.BrushOrderPO;
|
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.model.po.OrderPO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatResolver;
|
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.promotion.model.po.PromotionFeePO;
|
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.model.po.ShopPO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.mapper.ShopMonthlyTargetMapper;
|
import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.mapper.ShopMonthlyTargetMapper;
|
||||||
@@ -108,7 +109,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
|
|
||||||
LocalDateTime startDateTime = startDate.atStartOfDay();
|
LocalDateTime startDateTime = startDate.atStartOfDay();
|
||||||
LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
|
LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
|
||||||
List<Long> autoStatisticsStatusIds = getAutoStatisticsStatusIds(companyId);
|
OrderStatusStatSets stat = orderStatusStatResolver.resolve(companyId);
|
||||||
|
List<Long> unionStatusIds = stat.getUnionStatusIds();
|
||||||
|
|
||||||
// 查询所有店铺
|
// 查询所有店铺
|
||||||
LambdaQueryWrapper<ShopPO> shopWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<ShopPO> shopWrapper = new LambdaQueryWrapper<>();
|
||||||
@@ -123,8 +125,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
orderWrapper.eq(OrderPO::getCompanyId, companyId)
|
orderWrapper.eq(OrderPO::getCompanyId, companyId)
|
||||||
.ge(OrderPO::getOrderTime, startDateTime)
|
.ge(OrderPO::getOrderTime, startDateTime)
|
||||||
.le(OrderPO::getOrderTime, endDateTime);
|
.le(OrderPO::getOrderTime, endDateTime);
|
||||||
if (!autoStatisticsStatusIds.isEmpty()) {
|
if (!unionStatusIds.isEmpty()) {
|
||||||
orderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
|
orderWrapper.in(OrderPO::getStatusId, unionStatusIds);
|
||||||
} else {
|
} else {
|
||||||
orderWrapper.isNull(OrderPO::getId);
|
orderWrapper.isNull(OrderPO::getId);
|
||||||
}
|
}
|
||||||
@@ -144,9 +146,6 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
.le(BrushOrderPO::getBrushTime, endDateTime);
|
.le(BrushOrderPO::getBrushTime, endDateTime);
|
||||||
List<BrushOrderPO> brushOrders = brushOrderMapper.selectList(brushWrapper);
|
List<BrushOrderPO> brushOrders = brushOrderMapper.selectList(brushWrapper);
|
||||||
|
|
||||||
// 获取退款状态ID
|
|
||||||
List<Long> refundStatusIds = getRefundStatusIds();
|
|
||||||
|
|
||||||
// 计算上月同期数据(用于增长百分比)
|
// 计算上月同期数据(用于增长百分比)
|
||||||
LocalDate lastMonthStartDate = startDate.minusMonths(1);
|
LocalDate lastMonthStartDate = startDate.minusMonths(1);
|
||||||
LocalDate lastMonthEndDate = endDate.minusMonths(1);
|
LocalDate lastMonthEndDate = endDate.minusMonths(1);
|
||||||
@@ -157,8 +156,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
lastMonthOrderWrapper.eq(OrderPO::getCompanyId, companyId)
|
lastMonthOrderWrapper.eq(OrderPO::getCompanyId, companyId)
|
||||||
.ge(OrderPO::getOrderTime, lastMonthStartDateTime)
|
.ge(OrderPO::getOrderTime, lastMonthStartDateTime)
|
||||||
.le(OrderPO::getOrderTime, lastMonthEndDateTime);
|
.le(OrderPO::getOrderTime, lastMonthEndDateTime);
|
||||||
if (!autoStatisticsStatusIds.isEmpty()) {
|
if (!unionStatusIds.isEmpty()) {
|
||||||
lastMonthOrderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
|
lastMonthOrderWrapper.in(OrderPO::getStatusId, unionStatusIds);
|
||||||
} else {
|
} else {
|
||||||
lastMonthOrderWrapper.isNull(OrderPO::getId);
|
lastMonthOrderWrapper.isNull(OrderPO::getId);
|
||||||
}
|
}
|
||||||
@@ -167,15 +166,15 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
// 构建返回对象
|
// 构建返回对象
|
||||||
CompanyOverviewVO overviewVO = new CompanyOverviewVO();
|
CompanyOverviewVO overviewVO = new CompanyOverviewVO();
|
||||||
|
|
||||||
// 1. 计算全店总销售额
|
// 1. 计算全店总销售额(按状态「计入销售额」维度,与单店概览一致)
|
||||||
BigDecimal totalSalesAmount = orders.stream()
|
BigDecimal totalSalesAmount = orders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
overviewVO.setTotalSalesAmount(totalSalesAmount);
|
overviewVO.setTotalSalesAmount(totalSalesAmount);
|
||||||
|
|
||||||
// 2. 计算较上月同期增长百分比
|
// 2. 计算较上月同期增长百分比
|
||||||
BigDecimal lastMonthSalesAmount = lastMonthOrders.stream()
|
BigDecimal lastMonthSalesAmount = lastMonthOrders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal monthOverMonthGrowth = BigDecimal.ZERO;
|
BigDecimal monthOverMonthGrowth = BigDecimal.ZERO;
|
||||||
if (lastMonthSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
|
if (lastMonthSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||||
@@ -187,8 +186,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
}
|
}
|
||||||
overviewVO.setMonthOverMonthGrowth(monthOverMonthGrowth);
|
overviewVO.setMonthOverMonthGrowth(monthOverMonthGrowth);
|
||||||
|
|
||||||
// 3. 计算全店总单量
|
// 3. 计算全店总单量(按状态「计入销售额」维度计单)
|
||||||
int totalOrderCount = orders.size();
|
int totalOrderCount = (int) orders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||||
overviewVO.setTotalOrderCount(totalOrderCount);
|
overviewVO.setTotalOrderCount(totalOrderCount);
|
||||||
|
|
||||||
// 4. 计算总推广费支出
|
// 4. 计算总推广费支出
|
||||||
@@ -204,45 +203,45 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
}
|
}
|
||||||
overviewVO.setAverageFeeRatio(averageFeeRatio);
|
overviewVO.setAverageFeeRatio(averageFeeRatio);
|
||||||
|
|
||||||
// 6. 计算全店总净利(税后)和总税额
|
// 6. 计算全店总净利(税后)和总税额(利润口径与单店一致;税额按销售额比例)
|
||||||
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders);
|
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat);
|
||||||
BigDecimal totalTaxAmount = totalProfitBeforeTax.multiply(taxRate);
|
BigDecimal totalTaxAmount = totalSalesAmount.multiply(taxRate);
|
||||||
BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalTaxAmount);
|
BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalTaxAmount);
|
||||||
overviewVO.setTotalNetProfitAfterTax(totalNetProfitAfterTax);
|
overviewVO.setTotalNetProfitAfterTax(totalNetProfitAfterTax);
|
||||||
overviewVO.setTotalTaxAmount(totalTaxAmount);
|
overviewVO.setTotalTaxAmount(totalTaxAmount);
|
||||||
|
|
||||||
// 7. 计算各店铺销售占比排行
|
// 7. 计算各店铺销售占比排行
|
||||||
List<CompanyOverviewVO.ShopSalesRatio> shopSalesRatios = calculateShopSalesRatios(shops, orders, totalSalesAmount);
|
List<CompanyOverviewVO.ShopSalesRatio> shopSalesRatios = calculateShopSalesRatios(shops, orders, totalSalesAmount, stat);
|
||||||
overviewVO.setShopSalesRatios(shopSalesRatios);
|
overviewVO.setShopSalesRatios(shopSalesRatios);
|
||||||
|
|
||||||
// 8. 计算销售额与单量趋势
|
// 8. 计算销售额与单量趋势
|
||||||
CompanyOverviewVO.TrendData salesAmountTrend = calculateSalesAmountTrend(orders, startDate, endDate);
|
CompanyOverviewVO.TrendData salesAmountTrend = calculateSalesAmountTrend(orders, startDate, endDate, stat);
|
||||||
CompanyOverviewVO.TrendData orderCountTrend = calculateOrderCountTrend(orders, startDate, endDate);
|
CompanyOverviewVO.TrendData orderCountTrend = calculateOrderCountTrend(orders, startDate, endDate, stat);
|
||||||
overviewVO.setSalesAmountTrend(salesAmountTrend);
|
overviewVO.setSalesAmountTrend(salesAmountTrend);
|
||||||
overviewVO.setOrderCountTrend(orderCountTrend);
|
overviewVO.setOrderCountTrend(orderCountTrend);
|
||||||
|
|
||||||
// 9. 计算店铺效率象限(ROI vs 净利)
|
// 9. 计算店铺效率象限(ROI vs 净利)
|
||||||
List<CompanyOverviewVO.ShopEfficiencyQuadrant> shopEfficiencyQuadrants = calculateShopEfficiencyQuadrants(
|
List<CompanyOverviewVO.ShopEfficiencyQuadrant> shopEfficiencyQuadrants = calculateShopEfficiencyQuadrants(
|
||||||
shops, orders, promotionFees, brushOrders, refundStatusIds);
|
shops, orders, promotionFees, brushOrders, stat);
|
||||||
overviewVO.setShopEfficiencyQuadrants(shopEfficiencyQuadrants);
|
overviewVO.setShopEfficiencyQuadrants(shopEfficiencyQuadrants);
|
||||||
|
|
||||||
// 9. 多店铺数据横向汇总
|
// 9. 多店铺数据横向汇总
|
||||||
CompanyOverviewVO.ShopOverview shopOverview = calculateShopComparisons(
|
CompanyOverviewVO.ShopOverview shopOverview = calculateShopComparisons(
|
||||||
shops, orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate);
|
shops, orders, promotionFees, brushOrders, stat, companyId, startDate, endDate);
|
||||||
overviewVO.setShopComparisons(shopOverview);
|
overviewVO.setShopComparisons(shopOverview);
|
||||||
|
|
||||||
// 10. 多品类数据横向汇总
|
// 10. 多品类数据横向汇总
|
||||||
CompanyOverviewVO.CategoryOverview categoryOverview = calculateCategoryComparisons(
|
CompanyOverviewVO.CategoryOverview categoryOverview = calculateCategoryComparisons(
|
||||||
orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate);
|
orders, promotionFees, brushOrders, stat, companyId, startDate, endDate);
|
||||||
overviewVO.setCategoryComparisons(categoryOverview);
|
overviewVO.setCategoryComparisons(categoryOverview);
|
||||||
|
|
||||||
// 11. 平台维度汇总
|
// 11. 平台维度汇总
|
||||||
CompanyOverviewVO.PlatformOverview platformOverview = calculatePlatformSummary(
|
CompanyOverviewVO.PlatformOverview platformOverview = calculatePlatformSummary(
|
||||||
shops, orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate);
|
shops, orders, promotionFees, brushOrders, stat, companyId, startDate, endDate);
|
||||||
overviewVO.setPlatformSummary(platformOverview);
|
overviewVO.setPlatformSummary(platformOverview);
|
||||||
|
|
||||||
// 12. 汇总ROI和总税后盈亏
|
// 12. 汇总ROI和总税后盈亏
|
||||||
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusIds);
|
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, stat);
|
||||||
overviewVO.setTotalROI(totalROI);
|
overviewVO.setTotalROI(totalROI);
|
||||||
overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax);
|
overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax);
|
||||||
|
|
||||||
@@ -322,19 +321,11 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算总利润(税前)
|
* 计算总利润(税前):与单店概览一致,按订单状态各维度计入销售额与各项成本
|
||||||
*/
|
*/
|
||||||
private BigDecimal calculateTotalProfit(List<OrderPO> orders) {
|
private BigDecimal calculateTotalProfit(List<OrderPO> orders, OrderStatusStatSets stat) {
|
||||||
return orders.stream()
|
return orders.stream()
|
||||||
.map(order -> {
|
.map(order -> stat.receivedForSales(order).subtract(stat.totalCostForStats(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);
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,13 +333,13 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
* 计算各店铺销售占比排行
|
* 计算各店铺销售占比排行
|
||||||
*/
|
*/
|
||||||
private List<CompanyOverviewVO.ShopSalesRatio> calculateShopSalesRatios(
|
private List<CompanyOverviewVO.ShopSalesRatio> calculateShopSalesRatios(
|
||||||
List<ShopPO> shops, List<OrderPO> orders, BigDecimal totalSalesAmount) {
|
List<ShopPO> shops, List<OrderPO> orders, BigDecimal totalSalesAmount, OrderStatusStatSets stat) {
|
||||||
Map<Long, BigDecimal> shopSalesMap = orders.stream()
|
Map<Long, BigDecimal> shopSalesMap = orders.stream()
|
||||||
.collect(Collectors.groupingBy(
|
.collect(Collectors.groupingBy(
|
||||||
OrderPO::getShopId,
|
OrderPO::getShopId,
|
||||||
Collectors.reducing(
|
Collectors.reducing(
|
||||||
BigDecimal.ZERO,
|
BigDecimal.ZERO,
|
||||||
order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO,
|
stat::receivedForSales,
|
||||||
BigDecimal::add)));
|
BigDecimal::add)));
|
||||||
|
|
||||||
List<CompanyOverviewVO.ShopSalesRatio> ratios = new ArrayList<>();
|
List<CompanyOverviewVO.ShopSalesRatio> ratios = new ArrayList<>();
|
||||||
@@ -378,7 +369,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
*/
|
*/
|
||||||
private List<CompanyOverviewVO.ShopEfficiencyQuadrant> calculateShopEfficiencyQuadrants(
|
private List<CompanyOverviewVO.ShopEfficiencyQuadrant> calculateShopEfficiencyQuadrants(
|
||||||
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat) {
|
||||||
List<CompanyOverviewVO.ShopEfficiencyQuadrant> quadrants = new ArrayList<>();
|
List<CompanyOverviewVO.ShopEfficiencyQuadrant> quadrants = new ArrayList<>();
|
||||||
|
|
||||||
// 按店铺分组数据
|
// 按店铺分组数据
|
||||||
@@ -399,18 +390,17 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
||||||
|
|
||||||
BigDecimal shopSales = shopOrders.stream()
|
BigDecimal shopSales = shopOrders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal shopRefund = shopOrders.stream()
|
BigDecimal shopRefund = shopOrders.stream()
|
||||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
.map(stat::receivedForRefund)
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal shopPromotionFee = shopPromotionFees.stream()
|
BigDecimal shopPromotionFee = shopPromotionFees.stream()
|
||||||
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee);
|
BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee);
|
||||||
BigDecimal profit = calculateShopProfit(shopOrders, refundStatusIds);
|
BigDecimal profit = calculateShopProfit(shopOrders, stat);
|
||||||
BigDecimal netProfit = calculateShopNetProfit(profit);
|
BigDecimal netProfit = calculateShopNetProfit(profit, shopSales);
|
||||||
|
|
||||||
allROIs.add(roi);
|
allROIs.add(roi);
|
||||||
allNetProfits.add(netProfit);
|
allNetProfits.add(netProfit);
|
||||||
@@ -430,18 +420,17 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
||||||
|
|
||||||
BigDecimal shopSales = shopOrders.stream()
|
BigDecimal shopSales = shopOrders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal shopRefund = shopOrders.stream()
|
BigDecimal shopRefund = shopOrders.stream()
|
||||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
.map(stat::receivedForRefund)
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal shopPromotionFee = shopPromotionFees.stream()
|
BigDecimal shopPromotionFee = shopPromotionFees.stream()
|
||||||
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee);
|
BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee);
|
||||||
BigDecimal profit = calculateShopProfit(shopOrders, refundStatusIds);
|
BigDecimal profit = calculateShopProfit(shopOrders, stat);
|
||||||
BigDecimal netProfit = calculateShopNetProfit(profit);
|
BigDecimal netProfit = calculateShopNetProfit(profit, shopSales);
|
||||||
|
|
||||||
int quadrant;
|
int quadrant;
|
||||||
if (roi.compareTo(avgROI) >= 0 && netProfit.compareTo(avgNetProfit) >= 0) {
|
if (roi.compareTo(avgROI) >= 0 && netProfit.compareTo(avgNetProfit) >= 0) {
|
||||||
@@ -471,7 +460,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
*/
|
*/
|
||||||
private CompanyOverviewVO.ShopOverview calculateShopComparisons(
|
private CompanyOverviewVO.ShopOverview calculateShopComparisons(
|
||||||
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds, Long companyId, LocalDate startDate, LocalDate endDate) {
|
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat, Long companyId, LocalDate startDate, LocalDate endDate) {
|
||||||
List<CompanyOverviewVO.ShopComparison> comparisons = new ArrayList<>();
|
List<CompanyOverviewVO.ShopComparison> comparisons = new ArrayList<>();
|
||||||
|
|
||||||
// 按店铺分组数据
|
// 按店铺分组数据
|
||||||
@@ -506,16 +495,15 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
||||||
List<ShopMonthlyTargetPO> shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
List<ShopMonthlyTargetPO> shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
||||||
|
|
||||||
// 计算各项指标
|
// 计算各项指标(与单店概览:按订单状态统计维度)
|
||||||
BigDecimal salesAmount = shopOrders.stream()
|
BigDecimal salesAmount = shopOrders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
int orderCount = shopOrders.size();
|
int orderCount = (int) shopOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||||
|
|
||||||
BigDecimal refundAmount = shopOrders.stream()
|
BigDecimal refundAmount = shopOrders.stream()
|
||||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
.map(stat::receivedForRefund)
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
BigDecimal brushFee = shopBrushOrders.stream()
|
BigDecimal brushFee = shopBrushOrders.stream()
|
||||||
@@ -528,33 +516,21 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
|
|
||||||
// 计算商品成本
|
// 计算商品成本
|
||||||
BigDecimal productCost = shopOrders.stream()
|
BigDecimal productCost = shopOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::productCostForStats)
|
||||||
BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO;
|
|
||||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
||||||
return cost.multiply(new BigDecimal(quantity));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
// 计算运费成本
|
// 计算运费成本
|
||||||
BigDecimal shippingCost = shopOrders.stream()
|
BigDecimal shippingCost = shopOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::freightCostForStats)
|
||||||
BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO;
|
|
||||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
||||||
return cost.multiply(new BigDecimal(quantity));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
// 计算其他成本
|
// 计算其他成本
|
||||||
BigDecimal otherCost = shopOrders.stream()
|
BigDecimal otherCost = shopOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::otherCostForStats)
|
||||||
BigDecimal cost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO;
|
|
||||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
||||||
return cost.multiply(new BigDecimal(quantity));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
BigDecimal totalCost = calculateShopTotalCost(shopOrders);
|
BigDecimal orderLineCost = productCost.add(shippingCost).add(otherCost);
|
||||||
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
|
BigDecimal profitBeforeTax = salesAmount.subtract(orderLineCost).subtract(refundAmount);
|
||||||
|
|
||||||
// 销售额4%税额
|
// 销售额4%税额
|
||||||
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate);
|
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate);
|
||||||
@@ -673,7 +649,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
: BigDecimal.ZERO;
|
: BigDecimal.ZERO;
|
||||||
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
|
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
|
||||||
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
|
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
|
||||||
BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04"));
|
BigDecimal salesTax = totalSales.multiply(taxRate);
|
||||||
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
|
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
|
||||||
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
|
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
|
||||||
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
|
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
|
||||||
@@ -715,60 +691,13 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算店铺利润(扣除退款)
|
* 店铺维度税前利润(与品类/单店口径一致:计入销售额的实收 − 各成本维度 − 计入退款的实收)
|
||||||
*/
|
*/
|
||||||
private BigDecimal calculateShopProfit(List<OrderPO> orders, List<Long> refundStatusIds) {
|
private BigDecimal calculateShopProfit(List<OrderPO> orders, OrderStatusStatSets stat) {
|
||||||
BigDecimal totalSales = orders.stream()
|
BigDecimal salesAmount = orders.stream().map(stat::receivedForSales).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
BigDecimal orderLineCost = orders.stream().map(stat::totalCostForStats).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal refundAmount = orders.stream().map(stat::receivedForRefund).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
return salesAmount.subtract(orderLineCost).subtract(refundAmount);
|
||||||
BigDecimal refundAmount = orders.stream()
|
|
||||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
||||||
|
|
||||||
BigDecimal totalCost = calculateShopTotalCost(orders);
|
|
||||||
return totalSales.subtract(totalCost).subtract(refundAmount);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 计算店铺总成本(订单成本)
|
|
||||||
*/
|
|
||||||
private BigDecimal calculateShopTotalCost(List<OrderPO> orders) {
|
|
||||||
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));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 计算店铺总成本(推广费+刷单费)
|
|
||||||
*/
|
|
||||||
private BigDecimal calculateShopTotalCost(List<PromotionFeePO> promotionFees, List<BrushOrderPO> brushOrders) {
|
|
||||||
BigDecimal promotionFee = promotionFees.stream()
|
|
||||||
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
||||||
|
|
||||||
BigDecimal brushFee = brushOrders.stream()
|
|
||||||
.map(brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
||||||
|
|
||||||
return promotionFee.add(brushFee);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 计算店铺ROI
|
|
||||||
*/
|
|
||||||
private BigDecimal calculateShopROI(BigDecimal profit, BigDecimal totalCost) {
|
|
||||||
if (totalCost.compareTo(BigDecimal.ZERO) <= 0) {
|
|
||||||
return BigDecimal.ZERO;
|
|
||||||
}
|
|
||||||
return profit.divide(totalCost, 4, RoundingMode.HALF_UP);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -782,24 +711,23 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算店铺净利(税后)
|
* 店铺净利(税后):税额按「计入销售额」的销售额比例,与单店概览一致
|
||||||
*/
|
*/
|
||||||
private BigDecimal calculateShopNetProfit(BigDecimal profitBeforeTax) {
|
private BigDecimal calculateShopNetProfit(BigDecimal profitBeforeTax, BigDecimal salesAmountForTax) {
|
||||||
BigDecimal taxAmount = profitBeforeTax.multiply(taxRate);
|
BigDecimal salesAmount = salesAmountForTax != null ? salesAmountForTax : BigDecimal.ZERO;
|
||||||
return profitBeforeTax.subtract(taxAmount);
|
return profitBeforeTax.subtract(salesAmount.multiply(taxRate));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算汇总ROI(新公式):(销售额 - 退款金额)/ 推广费用
|
* 计算汇总ROI(新公式):(销售额 - 退款金额)/ 推广费用
|
||||||
*/
|
*/
|
||||||
private BigDecimal calculateTotalROI(List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
private BigDecimal calculateTotalROI(List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat) {
|
||||||
BigDecimal totalSales = orders.stream()
|
BigDecimal totalSales = orders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal totalRefund = orders.stream()
|
BigDecimal totalRefund = orders.stream()
|
||||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
.map(stat::receivedForRefund)
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal netSales = totalSales.subtract(totalRefund);
|
BigDecimal netSales = totalSales.subtract(totalRefund);
|
||||||
BigDecimal totalPromotionFee = promotionFees.stream()
|
BigDecimal totalPromotionFee = promotionFees.stream()
|
||||||
@@ -808,13 +736,6 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
return calculateSalesROI(netSales, totalPromotionFee);
|
return calculateSalesROI(netSales, totalPromotionFee);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取退款状态ID(通过 is_refund 字段查询)
|
|
||||||
*/
|
|
||||||
private List<Long> getRefundStatusIds() {
|
|
||||||
return orderStatusStatResolver.resolve(currentUserContext.getCurrentCompanyId()).getRefundStatusIds();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 订单行上的 product_spec_id → 商品 id */
|
/** 订单行上的 product_spec_id → 商品 id */
|
||||||
private Map<Long, Long> orderSpecIdToProductIdMap(List<OrderPO> orders) {
|
private Map<Long, Long> orderSpecIdToProductIdMap(List<OrderPO> orders) {
|
||||||
List<Long> specIds = orders.stream()
|
List<Long> specIds = orders.stream()
|
||||||
@@ -840,7 +761,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
*/
|
*/
|
||||||
private CompanyOverviewVO.CategoryOverview calculateCategoryComparisons(
|
private CompanyOverviewVO.CategoryOverview calculateCategoryComparisons(
|
||||||
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds, Long companyId,
|
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat, Long companyId,
|
||||||
LocalDate startDate, LocalDate endDate) {
|
LocalDate startDate, LocalDate endDate) {
|
||||||
|
|
||||||
// Query all categories to build categoryId -> categoryName map
|
// Query all categories to build categoryId -> categoryName map
|
||||||
@@ -896,45 +817,31 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
List<OrderPO> categoryOrders = entry.getValue();
|
List<OrderPO> categoryOrders = entry.getValue();
|
||||||
|
|
||||||
BigDecimal salesAmount = categoryOrders.stream()
|
BigDecimal salesAmount = categoryOrders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
int orderCount = categoryOrders.size();
|
int orderCount = (int) categoryOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||||
|
|
||||||
BigDecimal refundAmount = categoryOrders.stream()
|
BigDecimal refundAmount = categoryOrders.stream()
|
||||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
.map(stat::receivedForRefund)
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
BigDecimal productCost = categoryOrders.stream()
|
BigDecimal productCost = categoryOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::productCostForStats)
|
||||||
BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO;
|
|
||||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
||||||
return cost.multiply(new BigDecimal(quantity));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
BigDecimal shippingCost = categoryOrders.stream()
|
BigDecimal shippingCost = categoryOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::freightCostForStats)
|
||||||
BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO;
|
|
||||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
||||||
return cost.multiply(new BigDecimal(quantity));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
BigDecimal otherCost = categoryOrders.stream()
|
BigDecimal otherCost = categoryOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::otherCostForStats)
|
||||||
BigDecimal cost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO;
|
|
||||||
return cost;
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
Map<Long, BigDecimal> categorySalesByShop = categoryOrders.stream()
|
Map<Long, BigDecimal> categorySalesByShop = categoryOrders.stream()
|
||||||
.collect(Collectors.groupingBy(
|
.collect(Collectors.groupingBy(
|
||||||
OrderPO::getShopId,
|
OrderPO::getShopId,
|
||||||
Collectors.reducing(BigDecimal.ZERO,
|
Collectors.reducing(BigDecimal.ZERO, stat::receivedForSales, BigDecimal::add)));
|
||||||
order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO,
|
|
||||||
BigDecimal::add)));
|
|
||||||
|
|
||||||
BigDecimal categoryTotalSales = categorySalesByShop.values().stream()
|
BigDecimal categoryTotalSales = categorySalesByShop.values().stream()
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
@@ -1039,7 +946,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
: BigDecimal.ZERO;
|
: BigDecimal.ZERO;
|
||||||
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
|
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
|
||||||
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
|
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
|
||||||
BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04"));
|
BigDecimal salesTax = totalSales.multiply(taxRate);
|
||||||
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
|
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
|
||||||
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
|
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
|
||||||
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
|
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
|
||||||
@@ -1069,16 +976,12 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Long> getAutoStatisticsStatusIds(Long companyId) {
|
|
||||||
return orderStatusStatResolver.resolve(companyId).getUnionStatusIds();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算平台维度汇总
|
* 计算平台维度汇总
|
||||||
*/
|
*/
|
||||||
private CompanyOverviewVO.PlatformOverview calculatePlatformSummary(
|
private CompanyOverviewVO.PlatformOverview calculatePlatformSummary(
|
||||||
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds, Long companyId,
|
List<BrushOrderPO> brushOrders, OrderStatusStatSets stat, Long companyId,
|
||||||
LocalDate startDate, LocalDate endDate) {
|
LocalDate startDate, LocalDate endDate) {
|
||||||
|
|
||||||
List<CompanyOverviewVO.PlatformSummary> platformSummaries = new ArrayList<>();
|
List<CompanyOverviewVO.PlatformSummary> platformSummaries = new ArrayList<>();
|
||||||
@@ -1151,45 +1054,36 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
||||||
List<ShopMonthlyTargetPO> shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
List<ShopMonthlyTargetPO> shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
||||||
|
|
||||||
// 销售额
|
// 销售额(按状态「计入销售额」)
|
||||||
BigDecimal shopSales = shopOrders.stream()
|
BigDecimal shopSales = shopOrders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalSalesAmount = totalSalesAmount.add(shopSales);
|
totalSalesAmount = totalSalesAmount.add(shopSales);
|
||||||
|
|
||||||
// 订单数
|
// 订单数(按状态「计入销售额」计单)
|
||||||
totalOrderCount += shopOrders.size();
|
totalOrderCount += (int) shopOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||||
|
|
||||||
// 退款金额
|
// 退款金额(按状态「计入退款」)
|
||||||
BigDecimal shopRefund = shopOrders.stream()
|
BigDecimal shopRefund = shopOrders.stream()
|
||||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
.map(stat::receivedForRefund)
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalRefundAmount = totalRefundAmount.add(shopRefund);
|
totalRefundAmount = totalRefundAmount.add(shopRefund);
|
||||||
|
|
||||||
// 商品成本
|
// 商品成本
|
||||||
BigDecimal shopProductCost = shopOrders.stream()
|
BigDecimal shopProductCost = shopOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::productCostForStats)
|
||||||
BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO;
|
|
||||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
||||||
return cost.multiply(new BigDecimal(quantity));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalProductCost = totalProductCost.add(shopProductCost);
|
totalProductCost = totalProductCost.add(shopProductCost);
|
||||||
|
|
||||||
// 运费成本
|
// 运费成本
|
||||||
BigDecimal shopShippingCost = shopOrders.stream()
|
BigDecimal shopShippingCost = shopOrders.stream()
|
||||||
.map(order -> {
|
.map(stat::freightCostForStats)
|
||||||
BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO;
|
|
||||||
Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
||||||
return cost.multiply(new BigDecimal(quantity));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalShippingCost = totalShippingCost.add(shopShippingCost);
|
totalShippingCost = totalShippingCost.add(shopShippingCost);
|
||||||
|
|
||||||
// 其他成本
|
// 其他成本
|
||||||
BigDecimal shopOtherCost = shopOrders.stream()
|
BigDecimal shopOtherCost = shopOrders.stream()
|
||||||
.map(order -> order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO)
|
.map(stat::otherCostForStats)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalOtherCost = totalOtherCost.add(shopOtherCost);
|
totalOtherCost = totalOtherCost.add(shopOtherCost);
|
||||||
|
|
||||||
@@ -1286,7 +1180,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
if (!shopsWithoutPlatform.isEmpty()) {
|
if (!shopsWithoutPlatform.isEmpty()) {
|
||||||
CompanyOverviewVO.PlatformSummary otherSummary = calculateOtherPlatformSummary(
|
CompanyOverviewVO.PlatformSummary otherSummary = calculateOtherPlatformSummary(
|
||||||
shopsWithoutPlatform, ordersByShop, promotionFeesByShop, brushOrdersByShop,
|
shopsWithoutPlatform, ordersByShop, promotionFeesByShop, brushOrdersByShop,
|
||||||
targetsByShop, refundStatusIds, taxRate);
|
targetsByShop, stat, taxRate);
|
||||||
if (otherSummary.getOrderCount() > 0) {
|
if (otherSummary.getOrderCount() > 0) {
|
||||||
platformSummaries.add(otherSummary);
|
platformSummaries.add(otherSummary);
|
||||||
}
|
}
|
||||||
@@ -1338,8 +1232,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
: BigDecimal.ZERO;
|
: BigDecimal.ZERO;
|
||||||
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
|
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
|
||||||
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount);
|
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount);
|
||||||
BigDecimal taxRate = new BigDecimal("0.04");
|
BigDecimal salesTax = totalSalesAmount.multiply(this.taxRate);
|
||||||
BigDecimal salesTax = totalSalesAmount.multiply(taxRate);
|
|
||||||
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
|
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
|
||||||
BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0
|
BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0
|
||||||
? totalSalesAmount.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP)
|
? totalSalesAmount.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP)
|
||||||
@@ -1389,7 +1282,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
Map<Long, List<PromotionFeePO>> promotionFeesByShop,
|
Map<Long, List<PromotionFeePO>> promotionFeesByShop,
|
||||||
Map<Long, List<BrushOrderPO>> brushOrdersByShop,
|
Map<Long, List<BrushOrderPO>> brushOrdersByShop,
|
||||||
Map<Long, List<ShopMonthlyTargetPO>> targetsByShop,
|
Map<Long, List<ShopMonthlyTargetPO>> targetsByShop,
|
||||||
List<Long> refundStatusIds,
|
OrderStatusStatSets stat,
|
||||||
BigDecimal taxRate) {
|
BigDecimal taxRate) {
|
||||||
|
|
||||||
BigDecimal totalSalesAmount = BigDecimal.ZERO;
|
BigDecimal totalSalesAmount = BigDecimal.ZERO;
|
||||||
@@ -1410,37 +1303,28 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
List<ShopMonthlyTargetPO> shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
List<ShopMonthlyTargetPO> shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>());
|
||||||
|
|
||||||
BigDecimal shopSales = shopOrders.stream()
|
BigDecimal shopSales = shopOrders.stream()
|
||||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalSalesAmount = totalSalesAmount.add(shopSales);
|
totalSalesAmount = totalSalesAmount.add(shopSales);
|
||||||
totalOrderCount += shopOrders.size();
|
totalOrderCount += (int) shopOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||||
|
|
||||||
BigDecimal shopRefund = shopOrders.stream()
|
BigDecimal shopRefund = shopOrders.stream()
|
||||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
.map(stat::receivedForRefund)
|
||||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalRefundAmount = totalRefundAmount.add(shopRefund);
|
totalRefundAmount = totalRefundAmount.add(shopRefund);
|
||||||
|
|
||||||
BigDecimal shopProductCost = shopOrders.stream()
|
BigDecimal shopProductCost = shopOrders.stream()
|
||||||
.map(o -> {
|
.map(stat::productCostForStats)
|
||||||
BigDecimal c = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO;
|
|
||||||
int qty = o.getQuantity() != null ? o.getQuantity() : 1;
|
|
||||||
return c.multiply(new BigDecimal(qty));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalProductCost = totalProductCost.add(shopProductCost);
|
totalProductCost = totalProductCost.add(shopProductCost);
|
||||||
|
|
||||||
BigDecimal shopShippingCost = shopOrders.stream()
|
BigDecimal shopShippingCost = shopOrders.stream()
|
||||||
.map(o -> {
|
.map(stat::freightCostForStats)
|
||||||
BigDecimal c = o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO;
|
|
||||||
int qty = o.getQuantity() != null ? o.getQuantity() : 1;
|
|
||||||
return c.multiply(new BigDecimal(qty));
|
|
||||||
})
|
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalShippingCost = totalShippingCost.add(shopShippingCost);
|
totalShippingCost = totalShippingCost.add(shopShippingCost);
|
||||||
|
|
||||||
BigDecimal shopOtherCost = shopOrders.stream()
|
BigDecimal shopOtherCost = shopOrders.stream()
|
||||||
.map(o -> o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO)
|
.map(stat::otherCostForStats)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
totalOtherCost = totalOtherCost.add(shopOtherCost);
|
totalOtherCost = totalOtherCost.add(shopOtherCost);
|
||||||
|
|
||||||
@@ -1522,12 +1406,13 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
return platform != null ? platform.getPlatformName() : null;
|
return platform != null ? platform.getPlatformName() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompanyOverviewVO.TrendData calculateSalesAmountTrend(List<OrderPO> orders, LocalDate startDate, LocalDate endDate) {
|
private CompanyOverviewVO.TrendData calculateSalesAmountTrend(List<OrderPO> orders, LocalDate startDate,
|
||||||
|
LocalDate endDate, OrderStatusStatSets stat) {
|
||||||
CompanyOverviewVO.TrendData trendData = new CompanyOverviewVO.TrendData();
|
CompanyOverviewVO.TrendData trendData = new CompanyOverviewVO.TrendData();
|
||||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||||
.collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate()));
|
.collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate()));
|
||||||
BigDecimal total = orders.stream()
|
BigDecimal total = orders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
trendData.setTotal(total);
|
trendData.setTotal(total);
|
||||||
List<LocalDate> dateList = new ArrayList<>();
|
List<LocalDate> dateList = new ArrayList<>();
|
||||||
@@ -1542,7 +1427,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
item.setDate(date);
|
item.setDate(date);
|
||||||
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
|
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
|
||||||
BigDecimal dayTotal = dayOrders.stream()
|
BigDecimal dayTotal = dayOrders.stream()
|
||||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
.map(stat::receivedForSales)
|
||||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
item.setValue(dayTotal);
|
item.setValue(dayTotal);
|
||||||
return item;
|
return item;
|
||||||
@@ -1553,11 +1438,12 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
return trendData;
|
return trendData;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompanyOverviewVO.TrendData calculateOrderCountTrend(List<OrderPO> orders, LocalDate startDate, LocalDate endDate) {
|
private CompanyOverviewVO.TrendData calculateOrderCountTrend(List<OrderPO> orders, LocalDate startDate,
|
||||||
|
LocalDate endDate, OrderStatusStatSets stat) {
|
||||||
CompanyOverviewVO.TrendData trendData = new CompanyOverviewVO.TrendData();
|
CompanyOverviewVO.TrendData trendData = new CompanyOverviewVO.TrendData();
|
||||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||||
.collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate()));
|
.collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate()));
|
||||||
int total = orders.size();
|
int total = (int) orders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||||
trendData.setTotal(new BigDecimal(total));
|
trendData.setTotal(new BigDecimal(total));
|
||||||
List<LocalDate> dateList = new ArrayList<>();
|
List<LocalDate> dateList = new ArrayList<>();
|
||||||
LocalDate currentDate = startDate;
|
LocalDate currentDate = startDate;
|
||||||
@@ -1570,7 +1456,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
|||||||
CompanyOverviewVO.TrendDataItem item = new CompanyOverviewVO.TrendDataItem();
|
CompanyOverviewVO.TrendDataItem item = new CompanyOverviewVO.TrendDataItem();
|
||||||
item.setDate(date);
|
item.setDate(date);
|
||||||
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
|
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
|
||||||
item.setValue(new BigDecimal(dayOrders.size()));
|
int dayCount = (int) dayOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count();
|
||||||
|
item.setValue(new BigDecimal(dayCount));
|
||||||
return item;
|
return item;
|
||||||
})
|
})
|
||||||
.sorted(Comparator.comparing(CompanyOverviewVO.TrendDataItem::getDate))
|
.sorted(Comparator.comparing(CompanyOverviewVO.TrendDataItem::getDate))
|
||||||
|
|||||||
Reference in New Issue
Block a user