|
|
@@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
@@ -744,6 +745,147 @@ public class CommissionService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 团队关联订单 / 关联金额(按商城商品-SKU 维度汇总)
|
|
|
+ * <p>
|
|
|
+ * 与 {@link #getTeamConsumption} 同源:先用 referral_tree(parentId = userId 且 level <= maxLevel)
|
|
|
+ * 取下级 memberIds,再查 product_orders(buyerId IN memberIds 且 fulfillStatus = 'completed'),
|
|
|
+ * 然后把订单按 (productId, skuId) 分组统计:
|
|
|
+ * <ul>
|
|
|
+ * <li>orderCount:组内 orderNo 去重计数(同一订单含多 SKU 时该 SKU 只计一次)</li>
|
|
|
+ * <li>amountYuan:组内 totalAmount 累加后由分换算为元(BigDecimal 2 位,HALF_UP)</li>
|
|
|
+ * <li>团队级 totalOrderCount 用 orderNo 去重计数,避免按 SKU 求和导致一单多 SKU 重复计数</li>
|
|
|
+ * </ul>
|
|
|
+ * 本期不做退款排除,与团队消费额口径保持一致。
|
|
|
+ *
|
|
|
+ * @param userId 当前用户ID
|
|
|
+ * @param maxLevel 团队层级上限,默认 3,上限 10
|
|
|
+ * @return {totalOrderCount, totalAmountYuan(元), maxLevel, items[{productId, productName, skuId, orderCount, amountYuan}]}
|
|
|
+ */
|
|
|
+ public Map<String, Object> getTeamProductPerformance(Long userId, Integer maxLevel) {
|
|
|
+ if (maxLevel == null || maxLevel <= 0) maxLevel = 3;
|
|
|
+ if (maxLevel > 10) maxLevel = 10; // 安全上限
|
|
|
+
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ List<Map<String, Object>> items = new ArrayList<>();
|
|
|
+ result.put("totalOrderCount", 0);
|
|
|
+ result.put("totalAmountYuan", toYuan(0));
|
|
|
+ result.put("maxLevel", maxLevel);
|
|
|
+ result.put("items", items);
|
|
|
+
|
|
|
+ // 1. 查 referral_tree: parentId = userId 且 level <= maxLevel(与 getTeamConsumption 完全一致)
|
|
|
+ List<ReferralTree> treeNodes = referralTreeMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<ReferralTree>()
|
|
|
+ .eq(ReferralTree::getParentId, userId)
|
|
|
+ .le(ReferralTree::getLevel, maxLevel)
|
|
|
+ .orderByAsc(ReferralTree::getLevel)
|
|
|
+ );
|
|
|
+ if (treeNodes.isEmpty()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Long> memberIds = treeNodes.stream()
|
|
|
+ .map(ReferralTree::getMemberId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (memberIds.isEmpty()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 查 product_orders: buyerId IN memberIds 且 fulfillStatus = 'completed'
|
|
|
+ List<ProductOrder> orders = productOrderMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<ProductOrder>()
|
|
|
+ .in(ProductOrder::getBuyerId, memberIds)
|
|
|
+ .eq(ProductOrder::getFulfillStatus, "completed")
|
|
|
+ );
|
|
|
+ if (orders.isEmpty()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 批量查商品名,取不到时回退订单行 productName 快照
|
|
|
+ List<Long> productIds = orders.stream()
|
|
|
+ .map(ProductOrder::getProductId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ Map<Long, String> productNameMap = new HashMap<>();
|
|
|
+ if (!productIds.isEmpty()) {
|
|
|
+ productNameMap.putAll(productMapper.selectBatchIds(productIds).stream()
|
|
|
+ .filter(p -> p.getId() != null)
|
|
|
+ .collect(Collectors.toMap(Product::getId, p -> p.getName() == null ? "" : p.getName(), (a, b) -> a)));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 团队级汇总:订单去重 + 总额(分)
|
|
|
+ int totalOrderNoCount = countDistinctOrderNos(orders);
|
|
|
+ long totalAmountFen = orders.stream()
|
|
|
+ .mapToLong(o -> o.getTotalAmount() != null ? o.getTotalAmount() : 0L)
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ // 5. 按 (productId, skuId) 分组,组内订单号去重
|
|
|
+ Map<String, List<ProductOrder>> grouped = new LinkedHashMap<>();
|
|
|
+ for (ProductOrder o : orders) {
|
|
|
+ Long skuId = o.getSkuId() == null ? 0L : o.getSkuId();
|
|
|
+ String groupKey = (o.getProductId() == null ? 0L : o.getProductId()) + ":" + skuId;
|
|
|
+ grouped.computeIfAbsent(groupKey, k -> new ArrayList<>()).add(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Map.Entry<String, List<ProductOrder>> entry : grouped.entrySet()) {
|
|
|
+ List<ProductOrder> groupOrders = entry.getValue();
|
|
|
+ ProductOrder sample = groupOrders.get(0);
|
|
|
+
|
|
|
+ long orderCount = countDistinctOrderNos(groupOrders);
|
|
|
+ long amountFen = groupOrders.stream()
|
|
|
+ .mapToLong(o -> o.getTotalAmount() != null ? o.getTotalAmount() : 0L)
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
+ item.put("productId", sample.getProductId() == null ? 0L : sample.getProductId());
|
|
|
+ item.put("productName", resolveProductName(sample, productNameMap));
|
|
|
+ item.put("skuId", sample.getSkuId() == null ? 0L : sample.getSkuId());
|
|
|
+ item.put("orderCount", (int) orderCount);
|
|
|
+ item.put("amountYuan", toYuan(amountFen));
|
|
|
+ items.add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ result.put("totalOrderCount", (int) totalOrderNoCount);
|
|
|
+ result.put("totalAmountYuan", toYuan(totalAmountFen));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计订单号去重计数;空/无订单号的行按单行一单计入,避免订单数被漏计
|
|
|
+ */
|
|
|
+ private int countDistinctOrderNos(List<ProductOrder> orders) {
|
|
|
+ long distinct = orders.stream()
|
|
|
+ .map(ProductOrder::getOrderNo)
|
|
|
+ .filter(orderNo -> orderNo != null && !orderNo.isEmpty())
|
|
|
+ .distinct()
|
|
|
+ .count();
|
|
|
+ long blank = orders.stream()
|
|
|
+ .filter(o -> o.getOrderNo() == null || o.getOrderNo().isEmpty())
|
|
|
+ .count();
|
|
|
+ return (int) (distinct + blank);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 金额由「分」换算为「元」,保留 2 位(HALF_UP)
|
|
|
+ */
|
|
|
+ private BigDecimal toYuan(long fen) {
|
|
|
+ return BigDecimal.valueOf(fen).divide(BigDecimal.valueOf(100), 2, java.math.RoundingMode.HALF_UP);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商品名解析:优先 products.name,取不到时回退订单行 productName 快照
|
|
|
+ */
|
|
|
+ private String resolveProductName(ProductOrder order, Map<Long, String> productNameMap) {
|
|
|
+ String name = order.getProductId() == null ? null : productNameMap.get(order.getProductId());
|
|
|
+ if (name == null || name.isEmpty()) {
|
|
|
+ name = order.getProductName();
|
|
|
+ }
|
|
|
+ return name == null ? "" : name;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取我的推荐人信息(直推人),支持按家庭成员维度查询
|
|
|
* memberId 不为空时按成员查其关联用户的推荐人;为空时直接用 userId
|