91 lines
3.4 KiB
PHP
91 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* 人员编辑接口 POST /api/person/update.php
|
|
* 入参:id + 白名单字段;可选 contacts(JSON 数组,整体替换联系方式)
|
|
*/
|
|
require_once __DIR__ . '/../common/db.php';
|
|
require_once __DIR__ . '/../common/response.php';
|
|
require_once __DIR__ . '/../common/auth.php';
|
|
require_once __DIR__ . '/../common/logger.php';
|
|
require_once __DIR__ . '/../common/helpers.php';
|
|
require_once __DIR__ . '/../common/completeness.php';
|
|
require_once __DIR__ . '/../common/duplicate_check.php';
|
|
|
|
checkAjax();
|
|
checkPermission('person');
|
|
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
Response::error('参数错误', 400);
|
|
}
|
|
|
|
$data = extractFields(PERSON_FIELDS);
|
|
unset($data['union_id']); // 不允许修改 union_id
|
|
|
|
$pdo = DB::getInstance()->getPdo();
|
|
$check = $pdo->prepare("SELECT * FROM persons WHERE id = ?");
|
|
$check->execute([$id]);
|
|
$old = $check->fetch();
|
|
if (!$old) {
|
|
Response::error('人员不存在');
|
|
}
|
|
|
|
// 唯一性校验(排除自身):身份证号码 / 联系方式,收集全部重复
|
|
$dups = [];
|
|
if (!empty($data['id_number'])) {
|
|
$dup = findDuplicate($pdo, 'id_number', $data['id_number'], [$id]);
|
|
if ($dup) $dups[] = $dup;
|
|
}
|
|
$contacts = null;
|
|
if (isset($_POST['contacts'])) {
|
|
$contacts = json_decode($_POST['contacts'], true);
|
|
if (!is_array($contacts)) {
|
|
Response::error('联系方式格式错误', 400);
|
|
}
|
|
// 整体替换:排除人员自身既有联系方式ID
|
|
$ownIds = $pdo->query("SELECT id FROM social_accounts WHERE owner_type = 'person' AND owner_id = $id")->fetchAll(PDO::FETCH_COLUMN);
|
|
$dups = array_merge($dups, checkAccountsDuplicates($pdo, $contacts, $ownIds));
|
|
}
|
|
if (!empty($dups)) Response::duplicates($dups);
|
|
|
|
$contactsChanged = false;
|
|
if ($contacts !== null) {
|
|
$contactsChanged = true;
|
|
// 整体替换联系方式:先删旧(保留非联系人性质?此处直接删除全部后重建)
|
|
$pdo->prepare("DELETE FROM social_accounts WHERE owner_type = 'person' AND owner_id = ?")->execute([$id]);
|
|
if (count($contacts) > 0) {
|
|
$ins = $pdo->prepare(
|
|
"INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, profile_url, remark, is_primary, is_defult)
|
|
VALUES ('person', ?, ?, ?, ?, ?, ?, ?)"
|
|
);
|
|
foreach ($contacts as $c) {
|
|
$platform = trim($c['platform'] ?? '');
|
|
$accountId = trim($c['account_id'] ?? '');
|
|
if ($platform === '' || $accountId === '') continue;
|
|
$ins->execute([$id, $platform, $accountId, $c['profile_url'] ?? null, $c['remark'] ?? null, !empty($c['is_primary']) ? 1 : 0, !empty($c['is_defult']) ? 1 : 0]);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($data)) {
|
|
[$sets, $params] = buildUpdate($data);
|
|
$params[] = $id;
|
|
$pdo->prepare("UPDATE persons SET $sets WHERE id = ?")->execute($params);
|
|
}
|
|
|
|
// 工作履历(v1.0.18:可选,整表替换)
|
|
if (array_key_exists('experiences', $_POST)) {
|
|
$experiences = json_decode($_POST['experiences'], true);
|
|
if (!is_array($experiences)) {
|
|
Response::error('工作履历格式错误', 400);
|
|
}
|
|
savePersonExperiences($pdo, $id, $experiences);
|
|
}
|
|
|
|
// 完整度检查:人员主表 + 其 social_accounts
|
|
updateIncomplete($pdo, 'persons', $id);
|
|
updateAccountsIncomplete($pdo, 'person', $id);
|
|
|
|
logCurrent('update', 'person', 'persons', $id, ['before' => $old, 'after' => $data, 'contacts_replaced' => $contactsChanged]);
|
|
Response::success(null, '更新成功');
|