fix: 采集失败不降级有数据分行+任务领取计数对齐实际分发行数

- 采集失败时按 latest_likes 分流:无数据才标 failed,已有历史数据保持
  success 并注明沿用上次数据,collection_updated_at 不动;run 行照实记失败
- 种子 seedIfEmpty 末尾统一对账 claimed_quantity,杜绝演示数据口径漂移
- 新增 mysql/0021 对账迁移,修复 0020 物理合并遗留的生产计数漂移
- 运营端展示全面改读实时 dist_total(任务卡双面板/项目树/概览/进度),
  project-service 查询补 dist_total 子查询
- 走查实证:卡片 7/18 vs 详情已领取 2 两处口径打架;保存采集计划补采
  失败会把历史成功行改写为 failed
This commit is contained in:
ABAPPLO
2026-10-08 14:30:36 +08:00
parent ec45e7318c
commit 2db06063ee
7 changed files with 131 additions and 14 deletions

View File

@@ -355,14 +355,25 @@ export async function collectDistributionMetrics(
.bind(message, run.id),
db
.prepare(
// 已有历史数据(此前采集成功过)的分行失败时不降级为 failed:
// 保留 success 状态与数据时间,仅在描述里说明本次刷新失败。
`UPDATE distributions
SET collection_status = 'failed',
collection_status_description = ?,
collection_updated_at = CURRENT_TIMESTAMP,
SET collection_status = CASE
WHEN latest_likes IS NULL THEN 'failed'
ELSE 'success'
END,
collection_status_description = CASE
WHEN latest_likes IS NULL THEN ?
ELSE ?
END,
collection_updated_at = CASE
WHEN latest_likes IS NULL THEN CURRENT_TIMESTAMP
ELSE collection_updated_at
END,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?`,
)
.bind(message, distributionId),
.bind(message, `${message},沿用上次采集数据`, distributionId),
]);
throw error;
}

View File

@@ -853,6 +853,18 @@ export async function seedIfEmpty() {
);
}
// 任务卡与详情的「已领取」以 claimed_quantity 为口径,领取/释放路径会
// 增减它;种子阶段的手写数字与实际分发行数容易漂移,最后统一对账。
batch.push(
db
.prepare(
`UPDATE tasks
SET claimed_quantity = (
SELECT COUNT(*) FROM distributions d WHERE d.task_id = tasks.id
)`,
),
);
await db.batch(batch);
}

View File

@@ -51,6 +51,7 @@ export type ProjectTaskRow = {
task_chat_id: string;
recovery_enabled: number;
recovery_due_at: string;
dist_total: number;
recovery_submitted: number;
};
@@ -287,6 +288,7 @@ export async function getProjectDetail(
`SELECT id, name, brand, quantity, claimed_quantity, due_at, status,
task_type, platform, content_format, share_token, owner_userid, task_chat_id,
recovery_enabled, recovery_due_at,
(SELECT COUNT(*) FROM distributions d WHERE d.task_id = t.id) AS dist_total,
(SELECT COUNT(*) FROM distributions d WHERE d.task_id = t.id AND d.result_submitted_at IS NOT NULL) AS recovery_submitted
FROM tasks t WHERE t.project_id = ?
ORDER BY t.created_at DESC`,