Files

61 lines
1.9 KiB
PHP

<?php
/**
* 人员任职公司列表接口 GET /api/person/companies.php
* 入参:person_id(必填)/ page / limit
* 返回:该人员任职的公司列表(公司名称/部门/岗位/职级/入职日期/是否在岗)
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
checkPermission('person');
$personId = (int)($_REQUEST['person_id'] ?? 0);
if ($personId <= 0) {
Response::error('参数错误', 400);
}
[$page, $limit] = pageParams(5);
$pdo = DB::getInstance()->getPdo();
$chk = $pdo->prepare("SELECT COUNT(*) FROM persons WHERE id = ?");
$chk->execute([$personId]);
if ((int)$chk->fetchColumn() === 0) {
Response::error('人员不存在');
}
$where = ['pwe.person_id = ?', 'pwe.is_active = 1'];
$params = [$personId];
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 companies c ON c.id = pwe.company_id
WHERE $whereSql"
);
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
"SELECT pwe.id AS work_id, pwe.company_id,
CASE WHEN c.name_zh IS NOT NULL AND c.name_zh <> '' THEN c.name_zh
ELSE c.display_name END AS company_name,
c.industry, pwe.department, pwe.position, pwe.job_level,
pwe.start_date, pwe.end_date, pwe.is_current
FROM person_work_experiences pwe
INNER JOIN companies c ON c.id = pwe.company_id
WHERE $whereSql
ORDER BY pwe.is_current DESC, pwe.start_date DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute($params);
$list = $stmt->fetchAll();
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);