67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
||
/**
|
||
* 渠道新增计划列表接口 GET /api/channel/plan_list.php
|
||
* v1.0.18 渠道管理 - 渠道新增计划
|
||
* 参数:page / limit / channel_type / status / keyword
|
||
* 返回:{ list, total, page, limit }(list 含 remaining_days 剩余天数)
|
||
*/
|
||
require_once __DIR__ . '/../common/db.php';
|
||
require_once __DIR__ . '/../common/response.php';
|
||
require_once __DIR__ . '/../common/auth.php';
|
||
|
||
checkPermission('channel');
|
||
|
||
[$page, $limit] = pageParams();
|
||
$channelType = trim($_REQUEST['channel_type'] ?? '');
|
||
$status = trim($_REQUEST['status'] ?? '');
|
||
$keyword = trim($_REQUEST['keyword'] ?? '');
|
||
|
||
$where = ['is_active = 1'];
|
||
$params = [];
|
||
if ($channelType !== '') {
|
||
$where[] = 'channel_type = ?';
|
||
$params[] = $channelType;
|
||
}
|
||
if ($status !== '') {
|
||
$where[] = 'status = ?';
|
||
$params[] = $status;
|
||
}
|
||
if ($keyword !== '') {
|
||
$where[] = '(source_detail LIKE ? OR industry LIKE ? OR remark LIKE ?)';
|
||
$like = "%$keyword%";
|
||
array_push($params, $like, $like, $like);
|
||
}
|
||
$whereSql = implode(' AND ', $where);
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
|
||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM channel_plans WHERE $whereSql");
|
||
$stmt->execute($params);
|
||
$total = (int)$stmt->fetchColumn();
|
||
|
||
$offset = ($page - 1) * $limit;
|
||
$stmt = $pdo->prepare(
|
||
"SELECT id, channel_type, source_detail, occurrence_address, industry, start_date, end_date, remark, status, cost, created_at, updated_at
|
||
FROM channel_plans
|
||
WHERE $whereSql
|
||
ORDER BY id DESC
|
||
LIMIT $limit OFFSET $offset"
|
||
);
|
||
$stmt->execute($params);
|
||
$list = $stmt->fetchAll();
|
||
|
||
// 剩余天数:距时间窗口结束日(end_date)的天数,已过期为 0
|
||
$today = strtotime(date('Y-m-d'));
|
||
foreach ($list as &$row) {
|
||
$row['remaining_days'] = 0;
|
||
if (!empty($row['end_date']) && strtotime($row['end_date']) !== false) {
|
||
$diff = (int)ceil((strtotime($row['end_date']) - $today) / 86400);
|
||
$row['remaining_days'] = max(0, $diff);
|
||
}
|
||
// cost 转数字(空为 null)
|
||
$row['cost'] = ($row['cost'] === null || $row['cost'] === '') ? null : (float)$row['cost'];
|
||
}
|
||
unset($row);
|
||
|
||
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);
|