Files
superlink/api/fragment/stats_overview.php
T

78 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 碎片处理中心 - 顶部概览接口 GET /api/fragment/stats_overview.php
* 指标:
* - hard_total 硬碎片待处理数(preliminary_data 未转换)
* - soft_total 软碎片待处理数(四主表 is_incomplete=1)
* - month_new 本月新增(硬碎片本月新增 + 软碎片本月新增)
* - month_processed 本月处理(本月已转换硬碎片 + 本月已补全软碎片)
* - cumulative 累计处理(历史已转换硬碎片 + 历史已补全软碎片)
* - pending 待处理碎片(硬待处理 + 软待处理)
* 说明:v1.0.50 起 preliminary_data 无 status 字段,已转换以 converted_id 非空标识
*/
require_once __DIR__ . '/../common/Api.php';
$pdo = Api::boot(['module' => 'preliminary']);
$pdo = DB::getInstance()->getPdo();
$monthStart = "DATE_FORMAT(CURDATE(), '%Y-%m-01')";
// 硬碎片待处理数(未转换)
$hardTotal = (int)$pdo->query(
"SELECT COUNT(*) FROM preliminary_data WHERE is_active = 1 AND converted_id IS NULL"
)->fetchColumn();
// 软碎片待处理数(四主表 is_incomplete=1 之和)
$softTotal = 0;
foreach (['companies', 'persons', 'social_accounts', 'media_commercial_attributes'] as $t) {
$softTotal += (int)$pdo->query("SELECT COUNT(*) FROM `$t` WHERE is_incomplete = 1")->fetchColumn();
}
// 本月新增硬碎片(created_at 在本月)
$hardMonthNew = (int)$pdo->query(
"SELECT COUNT(*) FROM preliminary_data WHERE is_active = 1 AND created_at >= $monthStart"
)->fetchColumn();
// 本月新增软碎片(is_incomplete=1 且本月创建;media 的创建时间取关联 social_accounts)
$softMonthNew = 0;
foreach (['companies', 'persons', 'social_accounts'] as $t) {
$softMonthNew += (int)$pdo->query(
"SELECT COUNT(*) FROM `$t` WHERE is_incomplete = 1 AND created_at >= $monthStart"
)->fetchColumn();
}
$softMonthNew += (int)$pdo->query(
"SELECT COUNT(*) FROM media_commercial_attributes m
WHERE m.is_incomplete = 1
AND EXISTS (SELECT 1 FROM social_accounts sa WHERE sa.id = m.social_account_id AND sa.created_at >= $monthStart)"
)->fetchColumn();
// 本月已转换硬碎片
$hardConvertedMonth = (int)$pdo->query(
"SELECT COUNT(*) FROM preliminary_data
WHERE is_active = 1 AND converted_id IS NOT NULL AND processed_at >= $monthStart"
)->fetchColumn();
// 本月已补全软碎片(fragment 模块 complete 日志)
$softCompletedMonth = (int)$pdo->query(
"SELECT COUNT(*) FROM system_logs WHERE action = 'complete' AND module = 'fragment' AND created_at >= $monthStart"
)->fetchColumn();
// 历史累计已转换硬碎片
$hardConvertedTotal = (int)$pdo->query(
"SELECT COUNT(*) FROM preliminary_data WHERE is_active = 1 AND converted_id IS NOT NULL"
)->fetchColumn();
// 历史累计已补全软碎片
$softCompletedTotal = (int)$pdo->query(
"SELECT COUNT(*) FROM system_logs WHERE action = 'complete' AND module = 'fragment'"
)->fetchColumn();
Response::success([
'hard_total' => $hardTotal,
'soft_total' => $softTotal,
'month_new' => $hardMonthNew + $softMonthNew,
'month_processed' => $hardConvertedMonth + $softCompletedMonth,
'cumulative' => $hardConvertedTotal + $softCompletedTotal,
'pending' => $hardTotal + $softTotal,
]);