79 lines
3.2 KiB
PHP
79 lines
3.2 KiB
PHP
<?php
|
||
/**
|
||
* 碎片处理中心 - 顶部概览接口 GET /api/fragment/stats_overview.php
|
||
* 指标:
|
||
* - hard_total 硬碎片待处理数(preliminary_data 未终结)
|
||
* - soft_total 软碎片待处理数(四主表 is_incomplete=1)
|
||
* - month_new 本月新增(硬碎片本月新增 + 软碎片本月新增)
|
||
* - month_processed 本月处理(本月已转换硬碎片 + 本月已补全软碎片)
|
||
* - cumulative 累计处理(历史已转换硬碎片 + 历史已补全软碎片)
|
||
* - pending 待处理碎片(硬待处理 + 软待处理)
|
||
*/
|
||
require_once __DIR__ . '/../common/db.php';
|
||
require_once __DIR__ . '/../common/response.php';
|
||
require_once __DIR__ . '/../common/auth.php';
|
||
|
||
checkPermission('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 status NOT IN ('已转换','已废弃')"
|
||
)->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 status = '已转换' 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 status = '已转换'"
|
||
)->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,
|
||
]);
|