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]; }