Files

60 lines
2.3 KiB
PHP

<?php
/**
* 需求转盘列表接口 GET/POST /api/need/list.php
* 参数:page / limit / keyword / start_date / end_date / company_name / industry / need_category / is_valid
*/
require_once __DIR__ . '/../common/Api.php';
$pdo = Api::boot(['module' => 'need']);
[$page, $limit] = pageParams();
$keyword = trim($_REQUEST['keyword'] ?? '');
$startDate = trim($_REQUEST['start_date'] ?? '');
$endDate = trim($_REQUEST['end_date'] ?? '');
$companyName = trim($_REQUEST['company_name'] ?? '');
$industry = trim($_REQUEST['industry'] ?? '');
$category = trim($_REQUEST['need_category'] ?? '');
$valid = isset($_REQUEST['is_valid']) && $_REQUEST['is_valid'] !== '' ? (int)$_REQUEST['is_valid'] : null;
$where = ['cn.is_valid = 1'];
$params = [];
if ($valid !== null) {
$where = ['cn.is_valid = ?'];
$params = [$valid];
}
if ($keyword !== '') {
$where[] = '(cn.description LIKE ? OR cn.contact_person LIKE ? OR c.display_name LIKE ?)';
$like = "%$keyword%";
array_push($params, $like, $like, $like);
}
if ($startDate !== '') { $where[] = 'DATE(cn.created_at) >= ?'; $params[] = $startDate; }
if ($endDate !== '') { $where[] = 'DATE(cn.created_at) <= ?'; $params[] = $endDate; }
if ($companyName !== '') { $where[] = 'c.display_name LIKE ?'; $params[] = "%$companyName%"; }
if ($industry !== '') { $where[] = 'c.industry = ?'; $params[] = $industry; }
if ($category !== '') { $where[] = 'cn.need_category = ?'; $params[] = $category; }
$whereSql = implode(' AND ', $where);
$pdo = DB::getInstance()->getPdo();
$stmt = $pdo->prepare(
"SELECT COUNT(*) FROM company_needs cn LEFT JOIN companies c ON c.id = cn.company_id WHERE $whereSql"
);
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
"SELECT cn.id, cn.company_id, c.display_name, c.industry, cn.contact_person,
cn.need_category, cn.target_product_category, cn.application_scenario,
cn.description, cn.is_valid, cn.created_at
FROM company_needs cn
LEFT JOIN companies c ON c.id = cn.company_id
WHERE $whereSql
ORDER BY cn.id DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute($params);
$list = $stmt->fetchAll();
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);