Files
superlink/api/company/employees.php
T

86 lines
2.9 KiB
PHP

<?php
/**
* 企业员工列表接口 GET /api/company/employees.php
* 入参:company_id(必填)/ page / limit / keyword(姓名)/ job_level(职级)
* 数据源:persons JOIN person_work_experiences(按公司),联系方式取 social_accounts
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
checkPermission('company');
$companyId = (int)($_REQUEST['company_id'] ?? 0);
if ($companyId <= 0) {
Response::error('参数错误', 400);
}
[$page, $limit] = pageParams(10);
$keyword = trim($_REQUEST['keyword'] ?? '');
$jobLevel = trim($_REQUEST['job_level'] ?? '');
$pdo = DB::getInstance()->getPdo();
// 公司存在性校验
$chk = $pdo->prepare("SELECT COUNT(*) FROM companies WHERE id = ?");
$chk->execute([$companyId]);
if ((int)$chk->fetchColumn() === 0) {
Response::error('企业不存在');
}
$where = ['pwe.company_id = ?', 'pwe.is_active = 1'];
$params = [$companyId];
if ($keyword !== '') {
$where[] = 'p.full_name LIKE ?';
$params[] = "%$keyword%";
}
if ($jobLevel !== '') {
$where[] = 'pwe.job_level = ?';
$params[] = $jobLevel;
}
if (isset($_REQUEST['is_current']) && $_REQUEST['is_current'] !== '') {
$where[] = 'pwe.is_current = ?';
$params[] = (int)$_REQUEST['is_current'] ? 1 : 0;
}
$whereSql = implode(' AND ', $where);
// 总数
$stmt = $pdo->prepare(
"SELECT COUNT(*) FROM person_work_experiences pwe
INNER JOIN persons p ON p.id = pwe.person_id
WHERE $whereSql"
);
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
// 列表
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
"SELECT p.id AS person_id, p.full_name, pwe.id AS work_id, pwe.position, pwe.department,
pwe.job_level, pwe.start_date, pwe.end_date, pwe.is_current,
(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_primary DESC LIMIT 1) AS phone,
(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_primary DESC LIMIT 1) AS email
FROM person_work_experiences pwe
INNER JOIN persons p ON p.id = pwe.person_id
WHERE $whereSql
ORDER BY pwe.is_current DESC, pwe.start_date DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute($params);
$list = $stmt->fetchAll();
// 职级字典(该公司范围内,供筛选下拉)
$lv = $pdo->prepare(
"SELECT DISTINCT job_level FROM person_work_experiences
WHERE company_id = ? AND is_active = 1 AND job_level IS NOT NULL AND job_level <> ''
ORDER BY job_level"
);
$lv->execute([$companyId]);
$levels = $lv->fetchAll(PDO::FETCH_COLUMN);
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit, 'levels' => $levels]);