45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
/**
|
|
* 渠道年度统计接口 GET /api/channel/stats.php?year=2025
|
|
* 返回:指定年度(默认今年)各渠道效率统计 + 年度汇总
|
|
*/
|
|
require_once __DIR__ . '/../common/db.php';
|
|
require_once __DIR__ . '/../common/response.php';
|
|
require_once __DIR__ . '/../common/auth.php';
|
|
|
|
checkPermission('channel');
|
|
|
|
$year = (int)($_REQUEST['year'] ?? date('Y'));
|
|
if ($year < 2000 || $year > 2100) {
|
|
$year = (int)date('Y');
|
|
}
|
|
|
|
$pdo = DB::getInstance()->getPdo();
|
|
|
|
$rows = $pdo->prepare(
|
|
"SELECT id, channel_name, channel_type, efficiency_score, created_at
|
|
FROM channels
|
|
WHERE is_active = 1 AND YEAR(created_at) = ?
|
|
ORDER BY efficiency_score DESC"
|
|
);
|
|
$rows->execute([$year]);
|
|
$list = $rows->fetchAll();
|
|
|
|
$summary = [
|
|
'channel_count' => count($list),
|
|
'avg_score' => 0,
|
|
'max_score' => 0,
|
|
'min_score' => 0,
|
|
];
|
|
if ($list) {
|
|
$scores = array_column($list, 'efficiency_score');
|
|
$summary['avg_score'] = round(array_sum($scores) / count($scores), 2);
|
|
$summary['max_score'] = (float)max($scores);
|
|
$summary['min_score'] = (float)min($scores);
|
|
}
|
|
|
|
// 可用年度(用于前端下拉)
|
|
$years = $pdo->query("SELECT DISTINCT YEAR(created_at) AS y FROM channels WHERE is_active = 1 ORDER BY y DESC")->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
Response::success(['year' => $year, 'list' => $list, 'summary' => $summary, 'years' => $years]);
|