mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 09:09:19 +08:00
feat: 工作台增加SKU汇总表并支持跳转SKU概览
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
-- SKU 分析模块权限:bi:sku / bi:sku:overview
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 3732, (SELECT id FROM `sys_permission` WHERE `code` = 'bi' LIMIT 1), 'bi:sku', 'SKU分析', 'module', 1, '', 32
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 3732 OR `code` = 'bi:sku');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 3733, (SELECT id FROM `sys_permission` WHERE `code` = 'bi:sku' LIMIT 1), 'bi:sku:overview', 'SKU概览', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 3733 OR `code` = 'bi:sku:overview');
|
||||
@@ -0,0 +1,4 @@
|
||||
-- SKU 概览嵌入页菜单(hideInMenu,由商品概览/工作台跳转打开)
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `name`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 55, (SELECT id FROM `sys_menu` WHERE `id` = 6 OR `name` = '电商页面' LIMIT 1), 0, 'SKU概览', 'SkuOverview', 'SKU概览', '/ec/sku/overview/:id', '/ec/sku/overview/index', '', 'lucide:boxes', 'embedded', 0, 1, 1, 6
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 55 OR `name` = 'SKU概览' OR `route_name` = 'SkuOverview');
|
||||
+83
@@ -55,6 +55,13 @@ public class BiOrderStatQueryService {
|
||||
return repository.listSkuDocs(companyId, productId, shopIds, start, end);
|
||||
}
|
||||
|
||||
/** 按 SKU 主键拉区间日文档(SKU 概览)。 */
|
||||
public List<BiOrderStatDailySkuDoc> listSkuDocsBySkuId(Long companyId, Long productSkuId,
|
||||
List<Long> shopIds,
|
||||
LocalDate start, LocalDate end) {
|
||||
return repository.listSkuDocsBySkuId(companyId, productSkuId, shopIds, start, end);
|
||||
}
|
||||
|
||||
/** 商品日文档按日历日合并(单商品趋势)。 */
|
||||
public List<OverviewAggRows.DailyRow> toDailyRowsFromProductDocs(
|
||||
List<BiOrderStatDailyProductDoc> productDocs) {
|
||||
@@ -72,6 +79,22 @@ public class BiOrderStatQueryService {
|
||||
return new ArrayList<>(byDay.values());
|
||||
}
|
||||
|
||||
/** SKU 日文档按日历日合并(单 SKU 趋势)。 */
|
||||
public List<OverviewAggRows.DailyRow> toDailyRowsFromSkuDocs(List<BiOrderStatDailySkuDoc> skuDocs) {
|
||||
Map<LocalDate, OverviewAggRows.DailyRow> byDay = new HashMap<>();
|
||||
for (BiOrderStatDailySkuDoc doc : skuDocs) {
|
||||
if (doc == null || doc.getStatDate() == null) {
|
||||
continue;
|
||||
}
|
||||
OverviewAggRows.DailyRow row = byDay.computeIfAbsent(doc.getStatDate(), d -> emptyDaily(d));
|
||||
addDealRefundCost(row,
|
||||
doc.getDealAmount(), doc.getDealOrderCount(),
|
||||
doc.getRefundAmount(), doc.getRefundOrderCount(),
|
||||
doc.getProductCost(), doc.getFreightCost(), doc.getOtherCost());
|
||||
}
|
||||
return new ArrayList<>(byDay.values());
|
||||
}
|
||||
|
||||
/** SKU 日文档按 productSkuId 合并区间。 */
|
||||
public List<SkuPeriodMetrics> sumSkuDocsBySku(List<BiOrderStatDailySkuDoc> skuDocs) {
|
||||
Map<Long, SkuPeriodMetrics> merged = new HashMap<>();
|
||||
@@ -122,6 +145,56 @@ public class BiOrderStatQueryService {
|
||||
return new ArrayList<>(merged.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* SKU 日文档按 (shopId, productSkuId) 合并区间,供工作台 SKU 汇总表。
|
||||
* 行上净销售三件套 = 成交 − 退款。
|
||||
*/
|
||||
public List<OverviewAggRows.ShopSkuRow> toShopSkuRows(List<BiOrderStatDailySkuDoc> docs) {
|
||||
Map<String, OverviewAggRows.ShopSkuRow> merged = new HashMap<>();
|
||||
for (BiOrderStatDailySkuDoc doc : docs) {
|
||||
if (doc == null || doc.getShopId() == null) {
|
||||
continue;
|
||||
}
|
||||
long skuId = doc.getProductSkuId() != null ? doc.getProductSkuId() : 0L;
|
||||
long productId = doc.getProductId() != null ? doc.getProductId() : 0L;
|
||||
String key = doc.getShopId() + ":" + skuId;
|
||||
OverviewAggRows.ShopSkuRow row = merged.computeIfAbsent(key, k -> {
|
||||
OverviewAggRows.ShopSkuRow r = new OverviewAggRows.ShopSkuRow();
|
||||
r.setShopId(doc.getShopId());
|
||||
r.setProductId(productId);
|
||||
r.setProductSkuId(skuId);
|
||||
r.setSalesAmount(BigDecimal.ZERO);
|
||||
r.setOrderCount(0L);
|
||||
r.setSalesQuantitySum(0L);
|
||||
r.setRefundAmount(BigDecimal.ZERO);
|
||||
r.setRefundOrderCount(0L);
|
||||
r.setRefundQuantitySum(0L);
|
||||
r.setProductCost(BigDecimal.ZERO);
|
||||
r.setFreightCost(BigDecimal.ZERO);
|
||||
r.setOtherCost(BigDecimal.ZERO);
|
||||
r.setDealAmount(BigDecimal.ZERO);
|
||||
r.setDealOrderCount(0L);
|
||||
r.setDealQuantitySum(0L);
|
||||
return r;
|
||||
});
|
||||
// 同一 SKU 跨店合并时保留非 0 的 productId
|
||||
if ((row.getProductId() == null || row.getProductId() <= 0) && productId > 0) {
|
||||
row.setProductId(productId);
|
||||
}
|
||||
row.setDealAmount(nz(row.getDealAmount()).add(nz(doc.getDealAmount())));
|
||||
row.setDealOrderCount(nzLong(row.getDealOrderCount()) + nzInt(doc.getDealOrderCount()));
|
||||
row.setDealQuantitySum(nzLong(row.getDealQuantitySum()) + nzInt(doc.getDealQuantitySum()));
|
||||
row.setRefundAmount(nz(row.getRefundAmount()).add(nz(doc.getRefundAmount())));
|
||||
row.setRefundOrderCount(nzLong(row.getRefundOrderCount()) + nzInt(doc.getRefundOrderCount()));
|
||||
row.setRefundQuantitySum(nzLong(row.getRefundQuantitySum()) + nzInt(doc.getRefundQuantitySum()));
|
||||
row.setProductCost(nz(row.getProductCost()).add(nz(doc.getProductCost())));
|
||||
row.setFreightCost(nz(row.getFreightCost()).add(nz(doc.getFreightCost())));
|
||||
row.setOtherCost(nz(row.getOtherCost()).add(nz(doc.getOtherCost())));
|
||||
fillNetSalesSku(row);
|
||||
}
|
||||
return new ArrayList<>(merged.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* SKU 区间指标。{@code salesAmount}/{@code salesOrderCount}/{@code salesQuantitySum} 为净销售三件套(成交−退款)。
|
||||
*/
|
||||
@@ -335,6 +408,16 @@ public class BiOrderStatQueryService {
|
||||
nzLong(row.getRefundQuantitySum())));
|
||||
}
|
||||
|
||||
private static void fillNetSalesSku(OverviewAggRows.ShopSkuRow row) {
|
||||
row.setSalesAmount(netAmount(row.getDealAmount(), row.getRefundAmount()));
|
||||
row.setOrderCount(netCountLong(
|
||||
nzLong(row.getDealOrderCount()),
|
||||
nzLong(row.getRefundOrderCount())));
|
||||
row.setSalesQuantitySum(netCountLong(
|
||||
nzLong(row.getDealQuantitySum()),
|
||||
nzLong(row.getRefundQuantitySum())));
|
||||
}
|
||||
|
||||
private static BigDecimal netAmount(BigDecimal deal, BigDecimal refund) {
|
||||
return nz(deal).subtract(nz(refund));
|
||||
}
|
||||
|
||||
+26
-3
@@ -73,17 +73,40 @@ public class BiOrderStatDailyRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* 区间内 SKU 日文档。{@code productId} 必填;
|
||||
* 区间内 SKU 日文档。
|
||||
* {@code productId} 为空时不按商品过滤(公司/店铺级 SKU 汇总);
|
||||
* {@code shopIds} 为空时不按店过滤。
|
||||
*/
|
||||
public List<BiOrderStatDailySkuDoc> listSkuDocs(Long companyId, Long productId,
|
||||
List<Long> shopIds,
|
||||
LocalDate start, LocalDate end) {
|
||||
if (companyId == null || productId == null || start == null || end == null) {
|
||||
if (companyId == null || start == null || end == null) {
|
||||
return List.of();
|
||||
}
|
||||
Criteria c = Criteria.where("companyId").is(companyId)
|
||||
.and("productId").is(productId)
|
||||
.and("statDate").gte(start).lte(end);
|
||||
if (productId != null) {
|
||||
c = c.and("productId").is(productId);
|
||||
}
|
||||
if (shopIds != null && !shopIds.isEmpty()) {
|
||||
c = c.and("shopId").in(shopIds);
|
||||
}
|
||||
return mongoTemplate.find(Query.query(c), BiOrderStatDailySkuDoc.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按内部 SKU 主键拉区间日文档(SKU 概览)。
|
||||
* {@code shopIds} 为空时不按店过滤。
|
||||
*/
|
||||
public List<BiOrderStatDailySkuDoc> listSkuDocsBySkuId(Long companyId, Long productSkuId,
|
||||
List<Long> shopIds,
|
||||
LocalDate start, LocalDate end) {
|
||||
if (companyId == null || productSkuId == null || productSkuId <= 0
|
||||
|| start == null || end == null) {
|
||||
return List.of();
|
||||
}
|
||||
Criteria c = Criteria.where("companyId").is(companyId)
|
||||
.and("productSkuId").is(productSkuId)
|
||||
.and("statDate").gte(start).lte(end);
|
||||
if (shopIds != null && !shopIds.isEmpty()) {
|
||||
c = c.and("shopId").in(shopIds);
|
||||
|
||||
+22
@@ -36,6 +36,28 @@ public final class OverviewAggRows {
|
||||
private Long dealQuantitySum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 店铺×SKU 区间行(工作台 SKU 汇总表);净销售三件套同 {@link ShopProductRow}。
|
||||
*/
|
||||
@Data
|
||||
public static class ShopSkuRow {
|
||||
private Long shopId;
|
||||
private Long productId;
|
||||
private Long productSkuId;
|
||||
private BigDecimal salesAmount;
|
||||
private Long orderCount;
|
||||
private Long salesQuantitySum;
|
||||
private BigDecimal refundAmount;
|
||||
private Long refundOrderCount;
|
||||
private Long refundQuantitySum;
|
||||
private BigDecimal productCost;
|
||||
private BigDecimal freightCost;
|
||||
private BigDecimal otherCost;
|
||||
private BigDecimal dealAmount;
|
||||
private Long dealOrderCount;
|
||||
private Long dealQuantitySum;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class DailyRow {
|
||||
private LocalDate statDate;
|
||||
|
||||
+2
@@ -117,6 +117,8 @@ public class CompanyOverviewVO {
|
||||
private PlatformOverview platformTable;
|
||||
private CategoryOverview categoryTable;
|
||||
private CategoryOverview productTable;
|
||||
/** SKU 汇总(复用 CategoryOverview;categoryId=skuId,categoryName=sku 名) */
|
||||
private CategoryOverview skuTable;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+20
-2
@@ -14,7 +14,24 @@ import java.util.List;
|
||||
public interface CompanyOverviewService {
|
||||
|
||||
/**
|
||||
* 基于 Mongo/SQL 聚合行构建工作台底部四表。
|
||||
* 基于 Mongo/SQL 聚合行构建工作台底部汇总表(不含 SKU 表)。
|
||||
*/
|
||||
default CompanyOverviewVO.WorkbenchTableBundle buildWorkbenchSummaryTablesFromAgg(
|
||||
BigDecimal taxRate,
|
||||
Long companyId,
|
||||
List<ShopPO> shops,
|
||||
List<OverviewAggRows.ShopProductRow> orderRows,
|
||||
List<OverviewAggRows.PromotionRow> promotionRows,
|
||||
List<OverviewAggRows.BrushRow> brushRows,
|
||||
LocalDate startDate,
|
||||
LocalDate endDate) {
|
||||
return buildWorkbenchSummaryTablesFromAgg(
|
||||
taxRate, companyId, shops, orderRows, promotionRows, brushRows,
|
||||
startDate, endDate, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于商品行 + 可选 SKU 行构建工作台底部汇总表(含 skuTable)。
|
||||
*/
|
||||
CompanyOverviewVO.WorkbenchTableBundle buildWorkbenchSummaryTablesFromAgg(
|
||||
BigDecimal taxRate,
|
||||
@@ -24,5 +41,6 @@ public interface CompanyOverviewService {
|
||||
List<OverviewAggRows.PromotionRow> promotionRows,
|
||||
List<OverviewAggRows.BrushRow> brushRows,
|
||||
LocalDate startDate,
|
||||
LocalDate endDate);
|
||||
LocalDate endDate,
|
||||
List<OverviewAggRows.ShopSkuRow> skuRows);
|
||||
}
|
||||
|
||||
+114
-4
@@ -13,6 +13,8 @@ import top.xyquan.ecomdmbbackend.modules.ec.product.category.model.po.CategoryPO
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.category.service.CategoryService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.mapper.ProductMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.sku.mapper.ProductSkuMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.sku.model.po.ProductSkuPO;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@@ -36,14 +38,17 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
|
||||
private final ShopMonthlyTargetMapper shopMonthlyTargetMapper;
|
||||
private final ProductMapper productMapper;
|
||||
private final ProductSkuMapper productSkuMapper;
|
||||
private final CategoryService categoryService;
|
||||
|
||||
public CompanyOverviewServiceImpl(
|
||||
ShopMonthlyTargetMapper shopMonthlyTargetMapper,
|
||||
ProductMapper productMapper,
|
||||
ProductSkuMapper productSkuMapper,
|
||||
CategoryService categoryService) {
|
||||
this.shopMonthlyTargetMapper = shopMonthlyTargetMapper;
|
||||
this.productMapper = productMapper;
|
||||
this.productSkuMapper = productSkuMapper;
|
||||
this.categoryService = categoryService;
|
||||
}
|
||||
|
||||
@@ -325,6 +330,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompanyOverviewVO.WorkbenchTableBundle buildWorkbenchSummaryTablesFromAgg(
|
||||
BigDecimal taxRate,
|
||||
Long companyId,
|
||||
@@ -333,19 +339,22 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
List<OverviewAggRows.PromotionRow> promotionRows,
|
||||
List<OverviewAggRows.BrushRow> brushRows,
|
||||
LocalDate startDate,
|
||||
LocalDate endDate) {
|
||||
LocalDate endDate,
|
||||
List<OverviewAggRows.ShopSkuRow> skuRows) {
|
||||
synchronized (this) {
|
||||
BigDecimal saved = this.taxRate;
|
||||
try {
|
||||
this.taxRate = taxRate != null ? taxRate : DEFAULT_TAX_RATE;
|
||||
return buildWorkbenchTablesFromAggInternal(companyId, shops, orderRows, promotionRows, brushRows, startDate, endDate);
|
||||
return buildWorkbenchTablesFromAggInternal(
|
||||
companyId, shops, orderRows, promotionRows, brushRows,
|
||||
startDate, endDate, skuRows != null ? skuRows : List.of());
|
||||
} finally {
|
||||
this.taxRate = saved;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** SQL 聚合行 → 四表(推广/刷单分摊规则与明细版一致) */
|
||||
/** 聚合行 → 五表(推广/刷单分摊规则与明细版一致) */
|
||||
private CompanyOverviewVO.WorkbenchTableBundle buildWorkbenchTablesFromAggInternal(
|
||||
Long companyId,
|
||||
List<ShopPO> shops,
|
||||
@@ -353,7 +362,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
List<OverviewAggRows.PromotionRow> promotionRows,
|
||||
List<OverviewAggRows.BrushRow> brushRows,
|
||||
LocalDate startDate,
|
||||
LocalDate endDate) {
|
||||
LocalDate endDate,
|
||||
List<OverviewAggRows.ShopSkuRow> skuRows) {
|
||||
|
||||
Map<Long, AggMetrics> byShop = new HashMap<>();
|
||||
Map<Long, AggMetrics> byCategory = new HashMap<>();
|
||||
@@ -408,9 +418,83 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
directPromoByProduct, directPromoByCategory, ratioPool, totalSalesPool, productCategoryMap, companyId));
|
||||
bundle.setProductTable(buildProductOverviewFromAgg(byProduct, productSalesByShop, brushByShop,
|
||||
directPromoByProduct, directPromoByCategory, ratioPool, totalSalesPool, productCategoryMap, companyId));
|
||||
bundle.setSkuTable(buildSkuOverviewFromAgg(skuRows, bundle.getProductTable(), brushByShop));
|
||||
return bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* SKU 汇总:订单指标来自 SKU 日汇聚;推广按父商品推广×SKU 净销占比分摊;刷单按店净销占比。
|
||||
*/
|
||||
private CompanyOverviewVO.CategoryOverview buildSkuOverviewFromAgg(
|
||||
List<OverviewAggRows.ShopSkuRow> skuRows,
|
||||
CompanyOverviewVO.CategoryOverview productTable,
|
||||
Map<Long, BigDecimal> brushByShop) {
|
||||
if (skuRows == null || skuRows.isEmpty()) {
|
||||
CompanyOverviewVO.CategoryOverview empty = new CompanyOverviewVO.CategoryOverview();
|
||||
empty.setCategories(List.of());
|
||||
empty.setTotal(computeCategoryTotal(List.of()));
|
||||
return empty;
|
||||
}
|
||||
|
||||
Map<Long, BigDecimal> productPromoById = new HashMap<>();
|
||||
if (productTable != null && productTable.getCategories() != null) {
|
||||
for (CompanyOverviewVO.CategorySummary p : productTable.getCategories()) {
|
||||
if (p.getCategoryId() == null || p.getCategoryId() <= 0) {
|
||||
continue;
|
||||
}
|
||||
productPromoById.put(p.getCategoryId(), nz(p.getTotalPromotionFee()));
|
||||
}
|
||||
}
|
||||
|
||||
Map<Long, AggMetrics> bySku = new HashMap<>();
|
||||
Map<Long, Long> skuProductId = new HashMap<>();
|
||||
Map<Long, Map<Long, BigDecimal>> skuSalesByShop = new HashMap<>();
|
||||
for (OverviewAggRows.ShopSkuRow row : skuRows) {
|
||||
long skuId = row.getProductSkuId() != null ? row.getProductSkuId() : 0L;
|
||||
long shopId = row.getShopId() != null ? row.getShopId() : 0L;
|
||||
long productId = row.getProductId() != null && row.getProductId() > 0 ? row.getProductId() : 0L;
|
||||
bySku.merge(skuId, AggMetrics.from(row), AggMetrics::merge);
|
||||
if (productId > 0) {
|
||||
skuProductId.putIfAbsent(skuId, productId);
|
||||
} else if (!skuProductId.containsKey(skuId)) {
|
||||
skuProductId.put(skuId, 0L);
|
||||
}
|
||||
BigDecimal sales = nz(row.getSalesAmount());
|
||||
skuSalesByShop.computeIfAbsent(skuId, k -> new HashMap<>()).merge(shopId, sales, BigDecimal::add);
|
||||
}
|
||||
|
||||
// 同商品下 SKU 净销合计,用于推广二次分摊
|
||||
Map<Long, BigDecimal> salesByProductFromSku = new HashMap<>();
|
||||
for (Map.Entry<Long, AggMetrics> e : bySku.entrySet()) {
|
||||
Long pid = skuProductId.getOrDefault(e.getKey(), 0L);
|
||||
if (pid != null && pid > 0) {
|
||||
salesByProductFromSku.merge(pid, e.getValue().sales, BigDecimal::add);
|
||||
}
|
||||
}
|
||||
|
||||
Map<Long, String> skuNames = loadSkuNameMap(bySku.keySet());
|
||||
List<CompanyOverviewVO.CategorySummary> list = new ArrayList<>();
|
||||
for (Map.Entry<Long, AggMetrics> e : bySku.entrySet()) {
|
||||
Long skuId = e.getKey();
|
||||
AggMetrics m = e.getValue();
|
||||
Long productId = skuProductId.getOrDefault(skuId, 0L);
|
||||
BigDecimal productPromo = productPromoById.getOrDefault(productId, BigDecimal.ZERO);
|
||||
BigDecimal productSkuSalesPool = salesByProductFromSku.getOrDefault(productId, BigDecimal.ZERO);
|
||||
BigDecimal promo = productSkuSalesPool.compareTo(BigDecimal.ZERO) > 0
|
||||
? productPromo.multiply(m.sales).divide(productSkuSalesPool, 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
BigDecimal brush = allocBrushShare(skuSalesByShop.get(skuId), brushByShop);
|
||||
String name = skuId <= 0 ? "未绑定规格" : skuNames.getOrDefault(skuId, "规格" + skuId);
|
||||
list.add(toCategorySummaryFromAgg(skuId, name, m, promo, brush));
|
||||
}
|
||||
list.sort(Comparator.comparing(CompanyOverviewVO.CategorySummary::getSalesAmount,
|
||||
Comparator.nullsFirst(BigDecimal::compareTo)).reversed());
|
||||
CompanyOverviewVO.CategoryOverview o = new CompanyOverviewVO.CategoryOverview();
|
||||
o.setCategories(list);
|
||||
o.setTotal(computeCategoryTotal(list));
|
||||
return o;
|
||||
}
|
||||
|
||||
private CompanyOverviewVO.ShopOverview buildShopOverviewFromAgg(
|
||||
List<ShopPO> shops, Map<Long, AggMetrics> byShop, Map<Long, BigDecimal> promoByShop,
|
||||
Map<Long, BigDecimal> brushByShop, Map<Long, List<ShopMonthlyTargetPO>> targetsByShop) {
|
||||
@@ -736,6 +820,15 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
.stream().collect(Collectors.toMap(ProductPO::getId, ProductPO::getName, (a, b) -> a));
|
||||
}
|
||||
|
||||
private Map<Long, String> loadSkuNameMap(java.util.Set<Long> skuKeys) {
|
||||
List<Long> ids = skuKeys.stream().filter(id -> id != null && id > 0).distinct().toList();
|
||||
if (ids.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
return productSkuMapper.selectList(new LambdaQueryWrapper<ProductSkuPO>().in(ProductSkuPO::getId, ids))
|
||||
.stream().collect(Collectors.toMap(ProductSkuPO::getId, ProductSkuPO::getName, (a, b) -> a));
|
||||
}
|
||||
|
||||
private Map<Long, List<ShopMonthlyTargetPO>> loadTargetsByShop(Long companyId, LocalDate start, LocalDate end) {
|
||||
List<LocalDate> months = new ArrayList<>();
|
||||
LocalDate cur = start.withDayOfMonth(1);
|
||||
@@ -825,6 +918,23 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
return a;
|
||||
}
|
||||
|
||||
static AggMetrics from(OverviewAggRows.ShopSkuRow row) {
|
||||
AggMetrics a = new AggMetrics();
|
||||
a.sales = nz(row.getSalesAmount());
|
||||
a.dealAmount = nz(row.getDealAmount());
|
||||
a.salesOrderCount = row.getOrderCount() != null ? row.getOrderCount().intValue() : 0;
|
||||
a.dealOrderCount = row.getDealOrderCount() != null ? row.getDealOrderCount().intValue() : 0;
|
||||
a.dealQuantitySum = row.getDealQuantitySum() != null ? row.getDealQuantitySum().intValue() : 0;
|
||||
a.salesQuantitySum = row.getSalesQuantitySum() != null ? row.getSalesQuantitySum().intValue() : 0;
|
||||
a.refund = nz(row.getRefundAmount());
|
||||
a.refundOrderCount = row.getRefundOrderCount() != null ? row.getRefundOrderCount().intValue() : 0;
|
||||
a.refundQuantitySum = row.getRefundQuantitySum() != null ? row.getRefundQuantitySum().intValue() : 0;
|
||||
a.productCost = nz(row.getProductCost());
|
||||
a.freightCost = nz(row.getFreightCost());
|
||||
a.otherCost = nz(row.getOtherCost());
|
||||
return a;
|
||||
}
|
||||
|
||||
AggMetrics merge(AggMetrics o) {
|
||||
AggMetrics a = new AggMetrics();
|
||||
a.sales = sales.add(o.sales);
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.skuoverview.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.dto.SkuOverviewQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.vo.SkuOverviewVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.service.BiSkuOverviewService;
|
||||
|
||||
/**
|
||||
* SKU 概览 API:公司下指定 SKU 的经营汇总。
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/bi/sku")
|
||||
public class BiSkuOverviewController {
|
||||
|
||||
private final BiSkuOverviewService biSkuOverviewService;
|
||||
|
||||
public BiSkuOverviewController(BiSkuOverviewService biSkuOverviewService) {
|
||||
this.biSkuOverviewService = biSkuOverviewService;
|
||||
}
|
||||
|
||||
@PostMapping("/overview")
|
||||
@RequirePermission(value = "bi:sku:overview", description = "查询SKU概览", allowSuper = true)
|
||||
public ApiResponse<SkuOverviewVO> overview(@Valid @RequestBody SkuOverviewQueryDTO queryDTO) {
|
||||
return ApiResponse.success("获取SKU概览成功", biSkuOverviewService.getSkuOverview(queryDTO));
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* SKU 概览查询参数。
|
||||
*/
|
||||
@Data
|
||||
public class SkuOverviewQueryDTO {
|
||||
|
||||
@NotNull(message = "SKU ID不能为空")
|
||||
private Long skuId;
|
||||
|
||||
@NotNull(message = "开始日期不能为空")
|
||||
private LocalDate startDate;
|
||||
|
||||
@NotNull(message = "结束日期不能为空")
|
||||
private LocalDate endDate;
|
||||
|
||||
/** 店铺 ID(可选;传入时仅统计该店下该 SKU) */
|
||||
private Long shopId;
|
||||
|
||||
/** 税率(默认 0.04) */
|
||||
private BigDecimal taxRate = new BigDecimal("0.04");
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchKpiItemVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchTrendPointVO;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SKU 概览响应:经营 KPI/趋势与工作台同口径(精简版,无规格表)。
|
||||
*/
|
||||
@Data
|
||||
public class SkuOverviewVO {
|
||||
|
||||
private Long skuId;
|
||||
private String skuName;
|
||||
private String skuCode;
|
||||
private Long productId;
|
||||
private String productName;
|
||||
|
||||
/** 与工作台经营概览同结构的 KPI 列表 */
|
||||
private List<WorkbenchKpiItemVO> kpis;
|
||||
|
||||
/** 成交/净销售/利润日趋势 */
|
||||
private List<WorkbenchTrendPointVO> dealTrend;
|
||||
|
||||
private String momCompareDateStart;
|
||||
private String momCompareDateEnd;
|
||||
private String yoyCompareDateStart;
|
||||
private String yoyCompareDateEnd;
|
||||
|
||||
/** 兼容扁平 KPI 摘要 */
|
||||
private BigDecimal totalSalesAmount;
|
||||
private BigDecimal monthOverMonthGrowth;
|
||||
private Integer totalOrderCount;
|
||||
private BigDecimal averageFeeRatio;
|
||||
private BigDecimal totalNetProfitAfterTax;
|
||||
private BigDecimal totalTaxAmount;
|
||||
private BigDecimal totalROI;
|
||||
private BigDecimal totalPromotionFee;
|
||||
private BigDecimal refundAmount;
|
||||
private BigDecimal averageOrderValue;
|
||||
private BigDecimal totalProfitBeforeTax;
|
||||
|
||||
/** @deprecated 使用 dealTrend */
|
||||
private TrendData salesAmountTrend;
|
||||
/** @deprecated 使用 dealTrend */
|
||||
private TrendData orderCountTrend;
|
||||
|
||||
@Data
|
||||
public static class TrendData {
|
||||
private BigDecimal total;
|
||||
private List<TrendDataItem> dailyDataList;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class TrendDataItem {
|
||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate date;
|
||||
private BigDecimal value;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.skuoverview.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.dto.SkuOverviewQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.vo.SkuOverviewVO;
|
||||
|
||||
/**
|
||||
* BI SKU 概览:订单读 Mongo SKU 日汇聚,推广/刷单从父商品行按销占比分摊。
|
||||
*/
|
||||
public interface BiSkuOverviewService {
|
||||
|
||||
SkuOverviewVO getSkuOverview(SkuOverviewQueryDTO queryDTO);
|
||||
}
|
||||
+304
@@ -0,0 +1,304 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.skuoverview.service.impl;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.document.BiOrderStatDailyProductDoc;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.document.BiOrderStatDailySkuDoc;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.query.BiOrderStatQueryService;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.overview.model.agg.OverviewAggRows;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.overview.model.vo.CompanyOverviewVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.overview.service.CompanyOverviewService;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.dto.SkuOverviewQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.model.vo.SkuOverviewVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.skuoverview.service.BiSkuOverviewService;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchTrendPointVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.support.BiFinanceKpiAssembler;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.support.BiWorkbenchFeeLoader;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.sku.mapper.ProductSkuMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.sku.model.po.ProductSkuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.service.ShopService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* SKU 概览:Mongo SKU 日汇聚 + 父商品推广/刷单按净销占比分摊后的经营 KPI。
|
||||
*/
|
||||
@Service
|
||||
public class BiSkuOverviewServiceImpl implements BiSkuOverviewService {
|
||||
|
||||
private static final BigDecimal DEFAULT_TAX_RATE = new BigDecimal("0.04");
|
||||
|
||||
private final CurrentUserContext currentUserContext;
|
||||
private final ProductSkuMapper productSkuMapper;
|
||||
private final ProductService productService;
|
||||
private final ShopService shopService;
|
||||
private final BiOrderStatQueryService orderStatQueryService;
|
||||
private final BiWorkbenchFeeLoader feeLoader;
|
||||
private final CompanyOverviewService companyOverviewService;
|
||||
|
||||
public BiSkuOverviewServiceImpl(CurrentUserContext currentUserContext,
|
||||
ProductSkuMapper productSkuMapper,
|
||||
ProductService productService,
|
||||
ShopService shopService,
|
||||
BiOrderStatQueryService orderStatQueryService,
|
||||
BiWorkbenchFeeLoader feeLoader,
|
||||
CompanyOverviewService companyOverviewService) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.productSkuMapper = productSkuMapper;
|
||||
this.productService = productService;
|
||||
this.shopService = shopService;
|
||||
this.orderStatQueryService = orderStatQueryService;
|
||||
this.feeLoader = feeLoader;
|
||||
this.companyOverviewService = companyOverviewService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SkuOverviewVO getSkuOverview(SkuOverviewQueryDTO queryDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
ProductSkuPO sku = productSkuMapper.selectById(queryDTO.getSkuId());
|
||||
if (sku == null || !Objects.equals(sku.getCompanyId(), companyId)) {
|
||||
throw new BusinessException("SKU不存在");
|
||||
}
|
||||
if (queryDTO.getStartDate().isAfter(queryDTO.getEndDate())) {
|
||||
throw new BusinessException("开始日期不能晚于结束日期");
|
||||
}
|
||||
|
||||
ProductPO product = sku.getProductId() != null ? productService.getById(sku.getProductId()) : null;
|
||||
if (product != null && !Objects.equals(product.getCompanyId(), companyId)) {
|
||||
product = null;
|
||||
}
|
||||
|
||||
BigDecimal taxRate = queryDTO.getTaxRate() != null ? queryDTO.getTaxRate() : DEFAULT_TAX_RATE;
|
||||
LocalDate start = queryDTO.getStartDate();
|
||||
LocalDate end = queryDTO.getEndDate();
|
||||
Long filterShopId = queryDTO.getShopId();
|
||||
if (filterShopId != null) {
|
||||
ShopPO shop = shopService.getById(filterShopId);
|
||||
if (shop == null || !Objects.equals(shop.getCompanyId(), companyId)) {
|
||||
throw new BusinessException("店铺不存在");
|
||||
}
|
||||
}
|
||||
|
||||
List<Long> shopIdFilter = filterShopId != null ? List.of(filterShopId) : null;
|
||||
Long productId = sku.getProductId();
|
||||
|
||||
PeriodBundle cur = loadPeriod(companyId, sku.getId(), productId, shopIdFilter, filterShopId,
|
||||
start, end, taxRate);
|
||||
LocalDate[] mom = BiFinanceKpiAssembler.lastMonthSameSpan(start, end);
|
||||
LocalDate[] yoy = BiFinanceKpiAssembler.yoySameSpan(start, end);
|
||||
PeriodBundle prev = loadPeriod(companyId, sku.getId(), productId, shopIdFilter, filterShopId,
|
||||
mom[0], mom[1], taxRate);
|
||||
PeriodBundle yoyBundle = loadPeriod(companyId, sku.getId(), productId, shopIdFilter, filterShopId,
|
||||
yoy[0], yoy[1], taxRate);
|
||||
|
||||
BiFinanceKpiAssembler.Totals curT = cur.totals();
|
||||
BiFinanceKpiAssembler.Totals prevT = prev.totals();
|
||||
BiFinanceKpiAssembler.Totals yoyT = yoyBundle.totals();
|
||||
|
||||
List<WorkbenchTrendPointVO> dealTrend = BiFinanceKpiAssembler.buildTrendFromDaily(
|
||||
cur.dailyRows(), start, end, curT);
|
||||
|
||||
SkuOverviewVO vo = new SkuOverviewVO();
|
||||
vo.setSkuId(sku.getId());
|
||||
vo.setSkuName(sku.getName());
|
||||
vo.setSkuCode(sku.getSkuCode());
|
||||
vo.setProductId(productId);
|
||||
vo.setProductName(product != null ? product.getName() : null);
|
||||
vo.setKpis(BiFinanceKpiAssembler.buildKpis(curT, prevT, yoyT, start, end, taxRate));
|
||||
vo.setDealTrend(dealTrend);
|
||||
vo.setMomCompareDateStart(mom[0].toString());
|
||||
vo.setMomCompareDateEnd(mom[1].toString());
|
||||
vo.setYoyCompareDateStart(yoy[0].toString());
|
||||
vo.setYoyCompareDateEnd(yoy[1].toString());
|
||||
fillLegacyFlatFields(vo, curT, prevT);
|
||||
fillLegacyTrends(vo, dealTrend);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载单区间:本 SKU 日汇聚指标 + 父商品推广/刷单按同商品 SKU 净销占比分摊。
|
||||
*/
|
||||
private PeriodBundle loadPeriod(Long companyId, Long skuId, Long productId,
|
||||
List<Long> shopIdFilter, Long filterShopId,
|
||||
LocalDate start, LocalDate end, BigDecimal taxRate) {
|
||||
List<BiOrderStatDailySkuDoc> thisSkuDocs =
|
||||
orderStatQueryService.listSkuDocsBySkuId(companyId, skuId, shopIdFilter, start, end);
|
||||
|
||||
List<OverviewAggRows.DailyRow> dailyRows =
|
||||
orderStatQueryService.toDailyRowsFromSkuDocs(thisSkuDocs);
|
||||
|
||||
List<Long> shopIds;
|
||||
if (filterShopId != null) {
|
||||
shopIds = List.of(filterShopId);
|
||||
} else {
|
||||
shopIds = thisSkuDocs.stream()
|
||||
.map(BiOrderStatDailySkuDoc::getShopId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
BiOrderStatQueryService.SkuPeriodMetrics thisMetrics = findSkuMetrics(
|
||||
orderStatQueryService.sumSkuDocsBySku(thisSkuDocs), skuId);
|
||||
|
||||
if (shopIds.isEmpty() || thisMetrics == null) {
|
||||
return new PeriodBundle(shopIds, BiFinanceKpiAssembler.Totals.empty(), dailyRows);
|
||||
}
|
||||
|
||||
List<ShopPO> shops = shopService.listByIds(shopIds).stream()
|
||||
.filter(s -> s != null && Objects.equals(s.getCompanyId(), companyId))
|
||||
.collect(Collectors.toList());
|
||||
if (shops.isEmpty()) {
|
||||
return new PeriodBundle(shopIds, BiFinanceKpiAssembler.Totals.empty(), dailyRows);
|
||||
}
|
||||
|
||||
Map<Long, String> platformByShopId = shops.stream()
|
||||
.collect(Collectors.toMap(ShopPO::getId,
|
||||
s -> s.getPlatformCode() != null ? s.getPlatformCode() : "",
|
||||
(a, b) -> a));
|
||||
|
||||
// 同商品全部 SKU 净销合计,用于分摊推广/刷单
|
||||
BigDecimal salesPool = BigDecimal.ZERO;
|
||||
if (productId != null) {
|
||||
List<BiOrderStatDailySkuDoc> siblingDocs =
|
||||
orderStatQueryService.listSkuDocs(companyId, productId, shopIds, start, end);
|
||||
salesPool = orderStatQueryService.sumSkuDocsBySku(siblingDocs).stream()
|
||||
.map(BiOrderStatQueryService.SkuPeriodMetrics::salesAmount)
|
||||
.map(BiFinanceKpiAssembler::nz)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
}
|
||||
|
||||
List<BiOrderStatDailyProductDoc> allProductDocs =
|
||||
orderStatQueryService.listProductDocs(companyId, shopIds, start, end);
|
||||
BiWorkbenchFeeLoader.FeeBundle fees = feeLoader.load(companyId, shopIds, start, end);
|
||||
List<OverviewAggRows.ShopProductRow> orderRows =
|
||||
orderStatQueryService.toShopProductRows(allProductDocs, platformByShopId);
|
||||
CompanyOverviewVO.WorkbenchTableBundle tables =
|
||||
companyOverviewService.buildWorkbenchSummaryTablesFromAgg(
|
||||
taxRate, companyId, shops, orderRows,
|
||||
fees.promotionRows(), fees.brushRows(), start, end);
|
||||
|
||||
CompanyOverviewVO.CategorySummary productRow =
|
||||
productId != null ? findProductRow(tables.getProductTable(), productId) : null;
|
||||
|
||||
BigDecimal productPromo = productRow != null
|
||||
? BiFinanceKpiAssembler.nz(productRow.getTotalPromotionFee()) : BigDecimal.ZERO;
|
||||
BigDecimal productBrush = productRow != null
|
||||
? BiFinanceKpiAssembler.nz(productRow.getBrushFee()) : BigDecimal.ZERO;
|
||||
|
||||
BigDecimal skuSales = BiFinanceKpiAssembler.nz(thisMetrics.salesAmount());
|
||||
BigDecimal sharePromo = salesPool.compareTo(BigDecimal.ZERO) > 0
|
||||
? productPromo.multiply(skuSales).divide(salesPool, 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
BigDecimal shareBrush = salesPool.compareTo(BigDecimal.ZERO) > 0
|
||||
? productBrush.multiply(skuSales).divide(salesPool, 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO;
|
||||
|
||||
BiFinanceKpiAssembler.Totals totals = BiFinanceKpiAssembler.buildTotals(
|
||||
skuSales,
|
||||
BiFinanceKpiAssembler.nz(thisMetrics.refundAmount()),
|
||||
BiFinanceKpiAssembler.nz(thisMetrics.productCost()),
|
||||
BiFinanceKpiAssembler.nz(thisMetrics.freightCost()),
|
||||
BiFinanceKpiAssembler.nz(thisMetrics.otherCost()),
|
||||
sharePromo,
|
||||
shareBrush,
|
||||
taxRate,
|
||||
thisMetrics.salesOrderCount(),
|
||||
thisMetrics.salesQuantitySum(),
|
||||
BiFinanceKpiAssembler.nz(thisMetrics.dealAmount()),
|
||||
thisMetrics.dealOrderCount(),
|
||||
thisMetrics.dealQuantitySum());
|
||||
|
||||
return new PeriodBundle(shopIds, totals, dailyRows);
|
||||
}
|
||||
|
||||
private static BiOrderStatQueryService.SkuPeriodMetrics findSkuMetrics(
|
||||
List<BiOrderStatQueryService.SkuPeriodMetrics> list, Long skuId) {
|
||||
if (list == null || skuId == null) {
|
||||
return null;
|
||||
}
|
||||
return list.stream()
|
||||
.filter(m -> m.productSkuId() == skuId)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private static CompanyOverviewVO.CategorySummary findProductRow(
|
||||
CompanyOverviewVO.CategoryOverview table, Long productId) {
|
||||
if (table == null || table.getCategories() == null || productId == null) {
|
||||
return null;
|
||||
}
|
||||
return table.getCategories().stream()
|
||||
.filter(c -> Objects.equals(c.getCategoryId(), productId))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private void fillLegacyFlatFields(SkuOverviewVO vo,
|
||||
BiFinanceKpiAssembler.Totals cur,
|
||||
BiFinanceKpiAssembler.Totals prev) {
|
||||
vo.setTotalSalesAmount(BiFinanceKpiAssembler.yuan2(cur.totalSales()));
|
||||
vo.setTotalOrderCount(cur.orderCount());
|
||||
vo.setRefundAmount(BiFinanceKpiAssembler.yuan2(cur.totalRefund()));
|
||||
vo.setAverageOrderValue(BiFinanceKpiAssembler.yuan2(cur.averageOrderValue()));
|
||||
vo.setTotalPromotionFee(BiFinanceKpiAssembler.yuan2(cur.promotion()));
|
||||
vo.setAverageFeeRatio(cur.totalSales().compareTo(BigDecimal.ZERO) > 0
|
||||
? cur.promotion().divide(cur.totalSales(), 4, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO);
|
||||
vo.setTotalTaxAmount(BiFinanceKpiAssembler.yuan2(cur.tax()));
|
||||
vo.setTotalProfitBeforeTax(BiFinanceKpiAssembler.yuan2(
|
||||
cur.grossOrder().subtract(cur.promotion()).subtract(cur.brush())));
|
||||
vo.setTotalNetProfitAfterTax(BiFinanceKpiAssembler.yuan2(cur.netProfit()));
|
||||
vo.setTotalROI(cur.promotion().compareTo(BigDecimal.ZERO) > 0
|
||||
? cur.totalSales().divide(cur.promotion(), 4, RoundingMode.HALF_UP)
|
||||
: BigDecimal.ZERO);
|
||||
vo.setMonthOverMonthGrowth(BiFinanceKpiAssembler.pctChange(cur.totalSales(), prev.totalSales()));
|
||||
}
|
||||
|
||||
private void fillLegacyTrends(SkuOverviewVO vo, List<WorkbenchTrendPointVO> dealTrend) {
|
||||
SkuOverviewVO.TrendData sales = new SkuOverviewVO.TrendData();
|
||||
SkuOverviewVO.TrendData orders = new SkuOverviewVO.TrendData();
|
||||
List<SkuOverviewVO.TrendDataItem> salesItems = new ArrayList<>();
|
||||
List<SkuOverviewVO.TrendDataItem> orderItems = new ArrayList<>();
|
||||
BigDecimal salesTotal = BigDecimal.ZERO;
|
||||
BigDecimal orderTotal = BigDecimal.ZERO;
|
||||
if (dealTrend != null) {
|
||||
for (WorkbenchTrendPointVO p : dealTrend) {
|
||||
SkuOverviewVO.TrendDataItem s = new SkuOverviewVO.TrendDataItem();
|
||||
s.setDate(LocalDate.parse(p.getDate()));
|
||||
s.setValue(BiFinanceKpiAssembler.nz(p.getSalesYuan()));
|
||||
salesItems.add(s);
|
||||
salesTotal = salesTotal.add(s.getValue());
|
||||
|
||||
SkuOverviewVO.TrendDataItem o = new SkuOverviewVO.TrendDataItem();
|
||||
o.setDate(s.getDate());
|
||||
o.setValue(BigDecimal.valueOf(p.getSalesOrderCount() != null ? p.getSalesOrderCount() : 0));
|
||||
orderItems.add(o);
|
||||
orderTotal = orderTotal.add(o.getValue());
|
||||
}
|
||||
}
|
||||
sales.setDailyDataList(salesItems);
|
||||
sales.setTotal(salesTotal);
|
||||
orders.setDailyDataList(orderItems);
|
||||
orders.setTotal(orderTotal);
|
||||
vo.setSalesAmountTrend(sales);
|
||||
vo.setOrderCountTrend(orders);
|
||||
}
|
||||
|
||||
private record PeriodBundle(List<Long> shopIds,
|
||||
BiFinanceKpiAssembler.Totals totals,
|
||||
List<OverviewAggRows.DailyRow> dailyRows) {
|
||||
}
|
||||
}
|
||||
+8
-4
@@ -115,7 +115,7 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
CompanyOverviewVO.WorkbenchTableBundle summaryTables = companyOverviewService.buildWorkbenchSummaryTablesFromAgg(
|
||||
ctx.taxRate(), ctx.companyId(), ctx.shops(),
|
||||
agg.orderRows(), agg.promotionRows(), agg.brushRows(),
|
||||
ctx.range().startDate(), ctx.range().endDate());
|
||||
ctx.range().startDate(), ctx.range().endDate(), agg.skuRows());
|
||||
|
||||
return WorkbenchFinanceSliceVO.builder().finance(finance).summaryTables(summaryTables).build();
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
SqlAggBundle agg = loadSqlAgg(ctx);
|
||||
CompanyOverviewVO.WorkbenchTableBundle bundle = companyOverviewService.buildWorkbenchSummaryTablesFromAgg(
|
||||
taxRate, companyId, shops, agg.orderRows(), agg.promotionRows(), agg.brushRows(),
|
||||
range.startDate(), range.endDate());
|
||||
range.startDate(), range.endDate(), agg.skuRows());
|
||||
CompanyOverviewVO.ShopSummary total = bundle.getShopTable() != null && bundle.getShopTable().getTotal() != null
|
||||
? bundle.getShopTable().getTotal() : new CompanyOverviewVO.ShopSummary();
|
||||
return WorkbenchPeriodCompareSliceVO.builder()
|
||||
@@ -181,13 +181,13 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
return companyOverviewService.buildWorkbenchSummaryTablesFromAgg(
|
||||
ctx.taxRate(), ctx.companyId(), ctx.shops(),
|
||||
List.of(), List.of(), List.of(),
|
||||
ctx.range().startDate(), ctx.range().endDate());
|
||||
ctx.range().startDate(), ctx.range().endDate(), List.of());
|
||||
}
|
||||
SqlAggBundle agg = loadSqlAgg(ctx);
|
||||
return companyOverviewService.buildWorkbenchSummaryTablesFromAgg(
|
||||
ctx.taxRate(), ctx.companyId(), ctx.shops(),
|
||||
agg.orderRows(), agg.promotionRows(), agg.brushRows(),
|
||||
ctx.range().startDate(), ctx.range().endDate());
|
||||
ctx.range().startDate(), ctx.range().endDate(), agg.skuRows());
|
||||
}
|
||||
|
||||
private WorkbenchFinanceSliceVO emptyFinanceSlice() {
|
||||
@@ -239,10 +239,13 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
ctx.companyId(), ctx.shopIds(), ctx.range().startDate(), ctx.range().endDate());
|
||||
var productDocs = orderStatQueryService.listProductDocs(
|
||||
ctx.companyId(), ctx.shopIds(), ctx.range().startDate(), ctx.range().endDate());
|
||||
var skuDocs = orderStatQueryService.listSkuDocs(
|
||||
ctx.companyId(), null, ctx.shopIds(), ctx.range().startDate(), ctx.range().endDate());
|
||||
BiWorkbenchFeeLoader.FeeBundle fees = feeLoader.load(
|
||||
ctx.companyId(), ctx.shopIds(), ctx.range().startDate(), ctx.range().endDate());
|
||||
return new SqlAggBundle(
|
||||
orderStatQueryService.toShopProductRows(productDocs, platformByShop),
|
||||
orderStatQueryService.toShopSkuRows(skuDocs),
|
||||
orderStatQueryService.toDailyRows(shopDocs),
|
||||
fees.promotionRows(),
|
||||
fees.brushRows(),
|
||||
@@ -421,6 +424,7 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
}
|
||||
|
||||
private record SqlAggBundle(List<OverviewAggRows.ShopProductRow> orderRows,
|
||||
List<OverviewAggRows.ShopSkuRow> skuRows,
|
||||
List<OverviewAggRows.DailyRow> dailyRows,
|
||||
List<OverviewAggRows.PromotionRow> promotionRows,
|
||||
List<OverviewAggRows.BrushRow> brushRows,
|
||||
|
||||
@@ -563,6 +563,14 @@ INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status
|
||||
SELECT 3731, (SELECT id FROM `sys_permission` WHERE `code` = 'bi:product' LIMIT 1), 'bi:product:overview', '商品概览', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 3731 OR `code` = 'bi:product:overview');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 3732, (SELECT id FROM `sys_permission` WHERE `code` = 'bi' LIMIT 1), 'bi:sku', 'SKU分析', 'module', 1, '', 32
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 3732 OR `code` = 'bi:sku');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 3733, (SELECT id FROM `sys_permission` WHERE `code` = 'bi:sku' LIMIT 1), 'bi:sku:overview', 'SKU概览', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 3733 OR `code` = 'bi:sku:overview');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 3740, (SELECT id FROM `sys_permission` WHERE `code` = 'bi' LIMIT 1), 'bi:order-stat', '订单统计', 'module', 1, '', 40
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 3740 OR `code` = 'bi:order-stat');
|
||||
@@ -811,6 +819,10 @@ INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `name`, `route_name`, `title
|
||||
SELECT 54, (SELECT id FROM `sys_menu` WHERE `id` = 6 OR `name` = '电商页面' LIMIT 1), 0, '品类概览', 'CategoryOverview', '品类概览', '/ec/category/overview/:id', '/ec/category/overview/index', '', 'lucide:tags', 'embedded', 0, 1, 1, 5
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 54 OR `name` = '品类概览');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `name`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 55, (SELECT id FROM `sys_menu` WHERE `id` = 6 OR `name` = '电商页面' LIMIT 1), 0, 'SKU概览', 'SkuOverview', 'SKU概览', '/ec/sku/overview/:id', '/ec/sku/overview/index', '', 'lucide:boxes', 'embedded', 0, 1, 1, 6
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 55 OR `name` = 'SKU概览' OR `route_name` = 'SkuOverview');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `name`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 44, (SELECT id FROM `sys_menu` WHERE `id` = 14006 OR `name` = '经营商品' LIMIT 1), 0, '商品类目', 'CategoryConfig', '商品类目', '/ec/product/category', '/ec/product/category/index', '', 'gg:list', 'menu', 1, 1, 1, 2
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 44 OR `name` = '商品类目');
|
||||
|
||||
Reference in New Issue
Block a user