mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 21:49:19 +08:00
feat: 工作台接入 BI 增量计算健康度(滞后/暂停/空闲)与按店明细恢复
This commit is contained in:
+6
@@ -26,4 +26,10 @@ public class BiStatProperties {
|
||||
|
||||
/** 增量店态成功/跳过写库节流秒数 */
|
||||
private long incStatusFlushSeconds = 30;
|
||||
|
||||
/** 增量健康度:滞后阈值(分钟) */
|
||||
private int incLagThresholdMinutes = 10;
|
||||
|
||||
/** 增量健康度:活跃判定窗口(小时);窗内无订单且无成功增量视为空闲 */
|
||||
private int incActivityWindowHours = 24;
|
||||
}
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.orderstat.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.po.BiOrderStatIncCalcPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.vo.BiOrderStatIncCalcStatusVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.vo.BiOrderStatIncHealthVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.service.BiOrderStatCalcTaskService;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.service.BiOrderStatIncHealthService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.mapper.ShopMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** BI 增量计算健康度:公司汇总、按店明细、恢复暂停。 */
|
||||
@RestController
|
||||
@RequestMapping("/api/bi/order-stat/inc")
|
||||
public class BiOrderStatIncHealthController {
|
||||
|
||||
private final BiOrderStatIncHealthService incHealthService;
|
||||
private final BiOrderStatCalcTaskService calcTaskService;
|
||||
private final ShopMapper shopMapper;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public BiOrderStatIncHealthController(BiOrderStatIncHealthService incHealthService,
|
||||
BiOrderStatCalcTaskService calcTaskService,
|
||||
ShopMapper shopMapper,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.incHealthService = incHealthService;
|
||||
this.calcTaskService = calcTaskService;
|
||||
this.shopMapper = shopMapper;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@GetMapping("/health")
|
||||
@RequirePermission(value = "bi:order-stat:calc:read", description = "BI增量计算健康度", allowSuper = true)
|
||||
public ApiResponse<BiOrderStatIncHealthVO.HealthSummary> health() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
return ApiResponse.success(incHealthService.healthSummary(companyId));
|
||||
}
|
||||
|
||||
@GetMapping("/shops")
|
||||
@RequirePermission(value = "bi:order-stat:calc:read", description = "BI增量计算店列表", allowSuper = true)
|
||||
public ApiResponse<List<BiOrderStatIncHealthVO.ShopRow>> listShops() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
return ApiResponse.success(incHealthService.listShopRows(companyId));
|
||||
}
|
||||
|
||||
@PostMapping("/resume-paused")
|
||||
@RequirePermission(value = "bi:order-stat:calc:update", description = "批量恢复BI增量暂停店", allowSuper = true)
|
||||
public ApiResponse<BiOrderStatIncHealthVO.ResumePausedResult> resumePaused() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
return ApiResponse.success("已恢复暂停店", incHealthService.resumeAllPaused(companyId));
|
||||
}
|
||||
|
||||
@PostMapping("/resume")
|
||||
@RequirePermission(value = "bi:order-stat:calc:update", description = "恢复单店BI增量计算", allowSuper = true)
|
||||
public ApiResponse<BiOrderStatIncCalcStatusVO> resumeOne(@RequestParam Long shopId) {
|
||||
ShopPO shop = requireOwnedShop(shopId);
|
||||
BiOrderStatIncCalcPO state = calcTaskService.resumeInc(shop.getCompanyId(), shop.getId());
|
||||
return ApiResponse.success("已恢复计算", BiOrderStatIncCalcStatusVO.builder()
|
||||
.shopId(shop.getId())
|
||||
.status(state.getStatus())
|
||||
.lastSummary(state.getLastSummary())
|
||||
.lastError(state.getLastError())
|
||||
.lastSuccessTime(state.getLastSuccessTime())
|
||||
.lastStatDate(state.getLastStatDate())
|
||||
.lastAppliedOrderId(state.getLastAppliedOrderId())
|
||||
.lastSkipped(state.getLastSkipped())
|
||||
.build());
|
||||
}
|
||||
|
||||
private ShopPO requireOwnedShop(Long shopId) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
ShopPO shop = shopMapper.selectOne(new LambdaQueryWrapper<ShopPO>()
|
||||
.eq(ShopPO::getId, shopId)
|
||||
.eq(ShopPO::getCompanyId, companyId)
|
||||
.last("LIMIT 1"));
|
||||
if (shop == null) {
|
||||
throw new BusinessException("店铺不存在或无权访问");
|
||||
}
|
||||
return shop;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/** BI 增量计算健康度(公司汇总 + 店行)。 */
|
||||
public class BiOrderStatIncHealthVO {
|
||||
|
||||
/** 店健康档:OK / IDLE / LAGGED / PAUSED */
|
||||
public static final String KIND_OK = "OK";
|
||||
public static final String KIND_IDLE = "IDLE";
|
||||
public static final String KIND_LAGGED = "LAGGED";
|
||||
public static final String KIND_PAUSED = "PAUSED";
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class HealthSummary {
|
||||
private int shopCount;
|
||||
private int okCount;
|
||||
private int idleCount;
|
||||
private int laggedCount;
|
||||
private int pausedCount;
|
||||
/** 最差滞后分钟数;无滞后则为 null */
|
||||
private Long worstLagMinutes;
|
||||
private int lagThresholdMinutes;
|
||||
private int activityWindowHours;
|
||||
/** 无暂停且无滞后 */
|
||||
private boolean healthy;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class ShopRow {
|
||||
private Long shopId;
|
||||
private String shopName;
|
||||
private String platformCode;
|
||||
/** 店态:1 正常 2 暂停;无记录则为 null */
|
||||
private Integer status;
|
||||
/** OK / IDLE / LAGGED / PAUSED */
|
||||
private String healthKind;
|
||||
private Long lagMinutes;
|
||||
private LocalDateTime lastSuccessTime;
|
||||
private LocalDate lastStatDate;
|
||||
private String lastSummary;
|
||||
private String lastError;
|
||||
private boolean recentOrderActivity;
|
||||
private boolean recentIncSuccess;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class ResumePausedResult {
|
||||
private int resumedCount;
|
||||
private List<Long> shopIds;
|
||||
}
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.orderstat.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.infra.config.BiStatProperties;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.mapper.BiOrderStatIncCalcMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.po.BiOrderStatIncCalcPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.model.vo.BiOrderStatIncHealthVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.order.mapper.OrderMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.mapper.ShopMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* BI 增量计算健康度:公司汇总 / 按店明细 / 批量恢复暂停。
|
||||
* 滞后仅评估「近活动窗有订单或有成功增量」的店,安静店标 IDLE。
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BiOrderStatIncHealthService {
|
||||
|
||||
private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");
|
||||
|
||||
private final ShopMapper shopMapper;
|
||||
private final OrderMapper orderMapper;
|
||||
private final BiOrderStatIncCalcMapper incCalcMapper;
|
||||
private final BiOrderStatCalcTaskService calcTaskService;
|
||||
private final BiStatProperties biStatProperties;
|
||||
|
||||
public BiOrderStatIncHealthVO.HealthSummary healthSummary(Long companyId) {
|
||||
List<BiOrderStatIncHealthVO.ShopRow> rows = listShopRows(companyId);
|
||||
int ok = 0;
|
||||
int idle = 0;
|
||||
int lagged = 0;
|
||||
int paused = 0;
|
||||
Long worst = null;
|
||||
for (BiOrderStatIncHealthVO.ShopRow row : rows) {
|
||||
switch (row.getHealthKind()) {
|
||||
case BiOrderStatIncHealthVO.KIND_PAUSED -> paused++;
|
||||
case BiOrderStatIncHealthVO.KIND_LAGGED -> {
|
||||
lagged++;
|
||||
if (row.getLagMinutes() != null
|
||||
&& (worst == null || row.getLagMinutes() > worst)) {
|
||||
worst = row.getLagMinutes();
|
||||
}
|
||||
}
|
||||
case BiOrderStatIncHealthVO.KIND_IDLE -> idle++;
|
||||
default -> ok++;
|
||||
}
|
||||
}
|
||||
return BiOrderStatIncHealthVO.HealthSummary.builder()
|
||||
.shopCount(rows.size())
|
||||
.okCount(ok)
|
||||
.idleCount(idle)
|
||||
.laggedCount(lagged)
|
||||
.pausedCount(paused)
|
||||
.worstLagMinutes(worst)
|
||||
.lagThresholdMinutes(lagThresholdMinutes())
|
||||
.activityWindowHours(activityWindowHours())
|
||||
.healthy(paused == 0 && lagged == 0)
|
||||
.build();
|
||||
}
|
||||
|
||||
public List<BiOrderStatIncHealthVO.ShopRow> listShopRows(Long companyId) {
|
||||
if (companyId == null) {
|
||||
return List.of();
|
||||
}
|
||||
List<ShopPO> shops = shopMapper.selectList(new LambdaQueryWrapper<ShopPO>()
|
||||
.eq(ShopPO::getCompanyId, companyId)
|
||||
.orderByAsc(ShopPO::getId));
|
||||
if (shops.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
LocalDateTime now = LocalDateTime.now(ZONE);
|
||||
LocalDateTime activitySince = now.minusHours(activityWindowHours());
|
||||
long lagThreshold = lagThresholdMinutes();
|
||||
|
||||
Set<Long> activeShopIds = new HashSet<>(
|
||||
orderMapper.selectShopIdsWithRecentActivity(companyId, activitySince));
|
||||
|
||||
Map<Long, BiOrderStatIncCalcPO> stateByShop = incCalcMapper.selectList(
|
||||
new LambdaQueryWrapper<BiOrderStatIncCalcPO>()
|
||||
.eq(BiOrderStatIncCalcPO::getCompanyId, companyId))
|
||||
.stream()
|
||||
.filter(s -> s.getShopId() != null)
|
||||
.collect(Collectors.toMap(BiOrderStatIncCalcPO::getShopId, s -> s, (a, b) -> a));
|
||||
|
||||
List<BiOrderStatIncHealthVO.ShopRow> rows = new ArrayList<>();
|
||||
for (ShopPO shop : shops) {
|
||||
BiOrderStatIncCalcPO state = stateByShop.get(shop.getId());
|
||||
boolean recentOrder = activeShopIds.contains(shop.getId());
|
||||
boolean recentInc = state != null
|
||||
&& state.getLastSuccessTime() != null
|
||||
&& !state.getLastSuccessTime().isBefore(activitySince);
|
||||
boolean eligible = recentOrder || recentInc;
|
||||
|
||||
Integer status = state != null ? state.getStatus() : null;
|
||||
boolean paused = status != null && status == BiOrderStatIncCalcPO.STATUS_PAUSED;
|
||||
|
||||
Long lagMinutes = null;
|
||||
if (state != null && state.getLastSuccessTime() != null) {
|
||||
lagMinutes = Math.max(0L,
|
||||
Duration.between(state.getLastSuccessTime(), now).toMinutes());
|
||||
}
|
||||
|
||||
String kind;
|
||||
if (paused) {
|
||||
kind = BiOrderStatIncHealthVO.KIND_PAUSED;
|
||||
} else if (!eligible) {
|
||||
kind = BiOrderStatIncHealthVO.KIND_IDLE;
|
||||
lagMinutes = null;
|
||||
} else if (state == null || state.getLastSuccessTime() == null
|
||||
|| lagMinutes == null || lagMinutes > lagThreshold) {
|
||||
kind = BiOrderStatIncHealthVO.KIND_LAGGED;
|
||||
if (state == null || state.getLastSuccessTime() == null) {
|
||||
lagMinutes = null;
|
||||
}
|
||||
} else {
|
||||
kind = BiOrderStatIncHealthVO.KIND_OK;
|
||||
}
|
||||
|
||||
rows.add(BiOrderStatIncHealthVO.ShopRow.builder()
|
||||
.shopId(shop.getId())
|
||||
.shopName(shop.getName())
|
||||
.platformCode(shop.getPlatformCode())
|
||||
.status(status)
|
||||
.healthKind(kind)
|
||||
.lagMinutes(lagMinutes)
|
||||
.lastSuccessTime(state != null ? state.getLastSuccessTime() : null)
|
||||
.lastStatDate(state != null ? state.getLastStatDate() : null)
|
||||
.lastSummary(state != null ? state.getLastSummary() : null)
|
||||
.lastError(state != null ? state.getLastError() : null)
|
||||
.recentOrderActivity(recentOrder)
|
||||
.recentIncSuccess(recentInc)
|
||||
.build());
|
||||
}
|
||||
|
||||
rows.sort(Comparator
|
||||
.comparingInt((BiOrderStatIncHealthVO.ShopRow r) -> kindRank(r.getHealthKind()))
|
||||
.thenComparing(r -> r.getLagMinutes() == null ? -1L : r.getLagMinutes(),
|
||||
Comparator.reverseOrder())
|
||||
.thenComparing(r -> r.getShopId() == null ? 0L : r.getShopId()));
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** 恢复公司下全部暂停店。 */
|
||||
public BiOrderStatIncHealthVO.ResumePausedResult resumeAllPaused(Long companyId) {
|
||||
List<BiOrderStatIncCalcPO> paused = incCalcMapper.selectList(
|
||||
new LambdaQueryWrapper<BiOrderStatIncCalcPO>()
|
||||
.eq(BiOrderStatIncCalcPO::getCompanyId, companyId)
|
||||
.eq(BiOrderStatIncCalcPO::getStatus, BiOrderStatIncCalcPO.STATUS_PAUSED));
|
||||
List<Long> shopIds = new ArrayList<>();
|
||||
for (BiOrderStatIncCalcPO row : paused) {
|
||||
if (row.getShopId() == null) {
|
||||
continue;
|
||||
}
|
||||
calcTaskService.resumeInc(companyId, row.getShopId());
|
||||
shopIds.add(row.getShopId());
|
||||
}
|
||||
return BiOrderStatIncHealthVO.ResumePausedResult.builder()
|
||||
.resumedCount(shopIds.size())
|
||||
.shopIds(shopIds)
|
||||
.build();
|
||||
}
|
||||
|
||||
private int lagThresholdMinutes() {
|
||||
return Math.max(1, biStatProperties.getIncLagThresholdMinutes());
|
||||
}
|
||||
|
||||
private int activityWindowHours() {
|
||||
return Math.max(1, biStatProperties.getIncActivityWindowHours());
|
||||
}
|
||||
|
||||
private static int kindRank(String kind) {
|
||||
if (BiOrderStatIncHealthVO.KIND_PAUSED.equals(kind)) {
|
||||
return 0;
|
||||
}
|
||||
if (BiOrderStatIncHealthVO.KIND_LAGGED.equals(kind)) {
|
||||
return 1;
|
||||
}
|
||||
if (BiOrderStatIncHealthVO.KIND_IDLE.equals(kind)) {
|
||||
return 2;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
+4
@@ -27,6 +27,10 @@ public interface OrderMapper extends BaseMapper<OrderPO> {
|
||||
@Param("startTime") LocalDateTime startTime,
|
||||
@Param("endTime") LocalDateTime endTime);
|
||||
|
||||
/** 近窗内有订单活动的店铺(按 update_time,覆盖改单/转换) */
|
||||
List<Long> selectShopIdsWithRecentActivity(@Param("companyId") Long companyId,
|
||||
@Param("since") LocalDateTime since);
|
||||
|
||||
/** 新建 SKU 后挂 orphan 明细(仅补 product_sku_id;商品归属经 sku) */
|
||||
@Update("""
|
||||
UPDATE ec_order_item i
|
||||
|
||||
@@ -77,6 +77,8 @@ ecom:
|
||||
inc-max-retry: 20
|
||||
full-listener-concurrency: "2-4"
|
||||
inc-listener-concurrency: "2-4"
|
||||
inc-lag-threshold-minutes: 10
|
||||
inc-activity-window-hours: 24
|
||||
license:
|
||||
license-file: config/license.key
|
||||
revocation-file: config/revoked.jti
|
||||
|
||||
@@ -95,4 +95,11 @@
|
||||
AND o.order_time < #{endTime}
|
||||
ORDER BY o.shop_id, statDate
|
||||
</select>
|
||||
|
||||
<select id="selectShopIdsWithRecentActivity" resultType="long">
|
||||
SELECT DISTINCT o.shop_id
|
||||
FROM ec_order o
|
||||
WHERE o.company_id = #{companyId}
|
||||
AND o.update_time >= #{since}
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user