Files
edu/ajax/pros_users_inup.php

104 lines
4.1 KiB
PHP

<?php
// pros_users_inup.php
/**
* 사용자 정보 동기화 및 이력 저장 함수
* @param PDO $pdo DB연결 객체
* @param array $userData API로부터 받은 사용자 배열 데이터
* @return array 처리 결과 카운트
*/
function processUserSync($pdo, $userData) {
$insCount = 0;
$updCount = 0;
$errCount = 0;
foreach ($userData as $user) {
$mid = $user['member_id'];
$scc = $user['sys_comp_code'];
if (empty($mid) || empty($scc)) continue; // 필수 키 없으면 패스
try {
// 1. 존재 여부 확인 (복합키 기준)
$checkSql = "SELECT COUNT(*) FROM edu_users_temp WHERE member_id = ? AND sys_comp_code = ?";
$stmt = $pdo->prepare($checkSql);
$stmt->execute([$mid, $scc]);
$exists = $stmt->fetchColumn() > 0;
// 날짜 세팅 (비어있거나 잘못된 날짜 처리)
$join_date = (!empty($user['join_date']) && $user['join_date'] != "0000-00-00") ? $user['join_date'] : null;
$end_date = (!empty($user['end_date']) && $user['end_date'] != "0000-00-00") ? $user['end_date'] : null;
if ($exists) {
// 2-1. UPDATE (기존 정보 갱신)
$sql = "UPDATE edu_users_temp SET
name = :name, dept_name = :dept_name, rank_name = :rank_name,
join_date = :join_date, belong_comp = :belong_comp, working_comp = :working_comp,
end_date = :end_date, belong_comp_id = :belong_comp_id, working_comp_id = :working_comp_id,
updated_at = NOW()
WHERE member_id = :member_id AND sys_comp_code = :sys_comp_code";
$action = "UPDATE";
} else {
// 2-2. INSERT (신규 등록)
$sql = "INSERT INTO edu_users_temp (
member_id, sys_comp_code, name, dept_name, rank_name,
join_date, belong_comp, working_comp, end_date,
belong_comp_id, working_comp_id, created_at, updated_at
) VALUES (
:member_id, :sys_comp_code, :name, :dept_name, :rank_name,
:join_date, :belong_comp, :working_comp, :end_date,
:belong_comp_id, :working_comp_id, NOW(), NOW()
)";
$action = "INSERT";
}
$stmt = $pdo->prepare($sql);
$res = $stmt->execute([
':member_id' => $mid,
':sys_comp_code' => $scc,
':name' => $user['name'] ?? '',
':dept_name' => $user['dept_name'] ?? '',
':rank_name' => $user['rank_name'] ?? '',
':join_date' => $join_date,
':belong_comp' => $user['belong_comp'] ?? '',
':working_comp' => $user['working_comp'] ?? '',
':end_date' => $end_date,
':belong_comp_id' => $user['belong_comp_id'] ?? '',
':working_comp_id'=> $user['working_comp_id'] ?? ''
]);
if ($res) {
if ($action == "INSERT") {
$insCount++;
// [INSERT 전용 이력] 생성일시만 기록
$hisSql = "INSERT INTO edu_userinfo_history (
uh_member_id, uh_sys_comp_code, uh_created_at, uh_updated_at, uh_etc_01
) VALUES (
:mid, :scc, NOW(), NULL, :etc
)";
} else {
$updCount++;
// [UPDATE 전용 이력] 수정일시만 기록
$hisSql = "INSERT INTO edu_userinfo_history (
uh_member_id, uh_sys_comp_code, uh_created_at, uh_updated_at, uh_etc_01
) VALUES (
:mid, :scc, NULL, NOW(), :etc
)";
}
$hisStmt = $pdo->prepare($hisSql);
$hisStmt->execute([
':mid' => $mid,
':scc' => $scc,
':etc' => $action . " 수행 완료"
]);
}
} catch (PDOException $e) {
error_log("Sync Error ($mid): " . $e->getMessage());
$errCount++;
}
}
return ['ins' => $insCount, 'upd' => $updCount, 'err' => $errCount];
}