72 lines
3.0 KiB
PHP
72 lines
3.0 KiB
PHP
<?php
|
||
/**
|
||
* 人员列表接口 GET/POST /api/person/list.php
|
||
* 参数:page / limit / keyword(姓名/证件号/手机/邮箱)/ gender / nationality / work_location / is_active
|
||
* 返回:{ list, total, page, limit }
|
||
* v1.0.16:联系方式列改为“手机”“邮箱”两个字段(仅返回常用:is_defult=1 优先,其次 is_primary=1)
|
||
*/
|
||
require_once __DIR__ . '/../common/db.php';
|
||
require_once __DIR__ . '/../common/response.php';
|
||
require_once __DIR__ . '/../common/auth.php';
|
||
|
||
checkPermission('person');
|
||
|
||
[$page, $limit] = pageParams();
|
||
$keyword = trim($_REQUEST['keyword'] ?? '');
|
||
$gender = trim($_REQUEST['gender'] ?? '');
|
||
$nationality = trim($_REQUEST['nationality'] ?? '');
|
||
$location = trim($_REQUEST['work_location'] ?? '');
|
||
$active = isset($_REQUEST['is_active']) && $_REQUEST['is_active'] !== '' ? (int)$_REQUEST['is_active'] : null;
|
||
|
||
$where = ['p.is_active = 1'];
|
||
$params = [];
|
||
if ($active !== null) {
|
||
$where = ['p.is_active = ?'];
|
||
$params = [$active];
|
||
}
|
||
if ($keyword !== '') {
|
||
// 姓名/证件号直查;手机/邮箱通过 social_accounts 关联
|
||
$where[] = "(p.full_name LIKE ? OR p.id_number LIKE ? OR p.union_id LIKE ? OR EXISTS (
|
||
SELECT 1 FROM social_accounts sa
|
||
WHERE sa.owner_type = 'person' AND sa.owner_id = p.id AND sa.is_active = 1
|
||
AND sa.account_id LIKE ?
|
||
))";
|
||
$like = "%$keyword%";
|
||
array_push($params, $like, $like, $like, $like);
|
||
}
|
||
if ($gender !== '') { $where[] = 'p.gender = ?'; $params[] = $gender; }
|
||
if ($nationality !== '') { $where[] = 'p.nationality = ?'; $params[] = $nationality; }
|
||
if ($location !== '') { $where[] = 'p.work_location LIKE ?'; $params[] = "%$location%"; }
|
||
$whereSql = implode(' AND ', $where);
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
|
||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM persons p WHERE $whereSql");
|
||
$stmt->execute($params);
|
||
$total = (int)$stmt->fetchColumn();
|
||
|
||
// 常用手机/邮箱:优先 is_defult=1,其次 is_primary=1,最后取任意一条
|
||
$phoneSql = "(SELECT sa.account_id FROM social_accounts sa
|
||
WHERE sa.owner_type = 'person' AND sa.owner_id = p.id AND sa.platform = 'phone' AND sa.is_active = 1
|
||
ORDER BY sa.is_defult DESC, sa.is_primary DESC, sa.id DESC LIMIT 1)";
|
||
$emailSql = "(SELECT sa.account_id FROM social_accounts sa
|
||
WHERE sa.owner_type = 'person' AND sa.owner_id = p.id AND sa.platform = 'email' AND sa.is_active = 1
|
||
ORDER BY sa.is_defult DESC, sa.is_primary DESC, sa.id DESC LIMIT 1)";
|
||
|
||
$offset = ($page - 1) * $limit;
|
||
$stmt = $pdo->prepare(
|
||
"SELECT p.id, p.union_id, p.full_name, p.gender, p.nationality, p.id_type, p.id_number,
|
||
p.education, p.graduated_from, p.hometown, p.work_location, p.is_active, p.created_at,
|
||
p.source_channel, p.source_detail,
|
||
$phoneSql AS phone,
|
||
$emailSql AS email
|
||
FROM persons p
|
||
WHERE $whereSql
|
||
ORDER BY p.id DESC
|
||
LIMIT $limit OFFSET $offset"
|
||
);
|
||
$stmt->execute($params);
|
||
$list = $stmt->fetchAll();
|
||
|
||
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);
|