[이미지 기반 부서 계층 구조] 및 [실제 데이터] 일괄 구축을 시작합니다.
✔ Existing data cleared.
"; // 2. 부서 계층 구조(Hierarchy) 구축 echo "⏳ Seeding Department Hierarchy...
"; $dept_map = []; // name => id function seedDept($db, $name, $parentId = null, $level = 1, &$map) { $stmt = $db->prepare("INSERT INTO departments (name, parent_id, level) VALUES (?, ?, ?)"); $stmt->execute([$name, $parentId, $level]); $id = $db->lastInsertId(); $map[$name] = $id; echo "✔ Hierarchy seeded successfully.
⏳ Migrating Users & Mapping to Hierarchy...
"; $users_raw = getCSV($files['users']); $user_map = []; // name => [id, emp_id] (for laptop matching with double check) // 추가적인 맵핑 로직 (CSV 명칭 -> 이미지 계층 구조 명칭) $name_aliases = [ '인사·지원팀' => '인사지원팀', '미래전략TF' => '미래전략팀', '경제연구실' => '경제본부', '연구총괄대표 한국경제연구원' => '한국경제연구원', '경제산업본부' => '경제본부', '지속가능성장본부' => '지속가능경영실', '컴플라이언스' => '컴플라이언스팀', '민생경제TF' => '민생경제팀' ]; $user_stmt = $db->prepare("INSERT INTO users (emp_id, name, department_id, position, email, mobile, status, duty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $dept_insert_stmt = $db->prepare("INSERT INTO departments (name, parent_id, level) VALUES (?, ?, ?)"); foreach ($users_raw as $row) { $raw_dept = trim($row['부서'] ?? ''); if ($raw_dept == '') $raw_dept = '미소속'; // Alias 변환 $target_dept = $name_aliases[$raw_dept] ?? $raw_dept; if (!isset($dept_map[$target_dept])) { // 없는 부서라면 루트 아래에 생성 $dept_insert_stmt->execute([$target_dept, $rootId, 1]); $dept_map[$target_dept] = $db->lastInsertId(); } $emp_id = trim($row['사번'] ?? '') ?: 'TEMP_' . bin2hex(random_bytes(3)); $status_raw = trim($row['재직구분'] ?? ''); $status = 'active'; if ($status_raw === '퇴직' || $status_raw === '퇴사') $status = 'retired'; elseif ($status_raw === '휴직') $status = 'on_leave'; $user_stmt->execute([ $emp_id, $row['이름'], $dept_map[$target_dept], $row['직위'] ?? '', $row['메일'] ?? '', $row['개인핸드폰번호'] ?? '', $status, $row['직책'] ?? '' ]); $new_user_id = $db->lastInsertId(); // 중복 이름 대응을 위해 사번 정보도 함께 저장 $user_map[$row['이름']][] = [ 'id' => $new_user_id, 'emp_id' => $emp_id, 'dept' => $raw_dept ]; } echo "✔ " . count($users_raw) . " users processed.
"; // 5. 노트북 모델 & 자산 등록 echo "⏳ Processing Laptop Assets...
"; // 테이블 스키마에 스펙 컬럼이 없다면 추가 (최초 1회만) try { $db->exec("ALTER TABLE laptop_assets ADD COLUMN cpu TEXT"); $db->exec("ALTER TABLE laptop_assets ADD COLUMN ram TEXT"); $db->exec("ALTER TABLE laptop_assets ADD COLUMN storage TEXT"); } catch (Exception $e) { /* 이미 존재하면 무시 */ } $laptops_raw = getCSV($files['laptops']); $model_map = []; $model_stmt = $db->prepare("INSERT INTO laptop_models (model_name, manufacturer) VALUES (?, ?)"); $asset_stmt = $db->prepare("INSERT INTO laptop_assets (asset_tag, model_id, current_user_id, status, serial_number, purchase_date, cpu, ram, storage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); foreach ($laptops_raw as $row) { $model_name = trim($row['자산명'] ?? $row['모델명'] ?? 'Unknown Model'); $manufacturer = trim($row['제조사'] ?? 'Unknown'); if (!isset($model_map[$model_name])) { $model_stmt->execute([$model_name, $manufacturer]); $model_map[$model_name] = $db->lastInsertId(); } $user_name = trim($row['사람'] ?? ''); $raw_emp_id = trim($row['한경협 사번'] ?? ''); $current_user_id = NULL; if ($user_name && isset($user_map[$user_name])) { $candidates = $user_map[$user_name]; if (count($candidates) === 1) { $current_user_id = $candidates[0]['id']; } else { // 사번으로 매칭 시도 foreach ($candidates as $c) { if ($raw_emp_id && $c['emp_id'] === $raw_emp_id) { $current_user_id = $c['id']; break; } } // 사번 매칭 실패시 첫 번째 후보 (또는 로직 고도화 가능) if (!$current_user_id) $current_user_id = $candidates[0]['id']; } } $status = $current_user_id ? 'assigned' : 'stock'; if (strpos($user_name, '업무용') !== false || strpos($user_name, '임시') !== false) { $status = 'stock'; // 공용/업무용은 재고로 간주하거나 별도 상태 부여 가능 } $p_date = trim($row['취득일자'] ?? ''); if ($p_date && strpos($p_date, '/') !== false) { $parts = explode('/', $p_date); if (count($parts) == 3) $p_date = "{$parts[2]}-{$parts[0]}-{$parts[1]}"; } // 스펙 정보 추출 $cpu = $row['CPU'] ?? $row['프로세서'] ?? ''; $ram = $row['RAM'] ?? ''; $storage = $row['HDD 0 / 설치 값'] ?? ''; $asset_stmt->execute([ $row['자산관리번호'], $model_map[$model_name], $current_user_id, $status, $row['시리얼넘버/EX'], $p_date, $cpu, $ram, $storage ]); } echo "✔ Laptop assets imported with specs.
"; // 6. 기타 계정 정보 $mfp_raw = getCSV($files['mfp']); $mfp_stmt = $db->prepare("INSERT INTO mfp_accounts (account_id, account_pw, purpose) VALUES (?, ?, ?)"); foreach ($mfp_raw as $row) { $mfp_stmt->execute([$row['이름'], $row['패스워드'], $row['사용 목적']]); } $cards_raw = getCSV($files['cards']); $card_stmt = $db->prepare("INSERT INTO access_cards (card_number, card_type, nfc_id) VALUES (?, ?, ?)"); foreach ($cards_raw as $row) { $card_stmt->execute([$row['보안실 넘버링'], $row['임시 네이밍'], $row['NFC Sireal']]); } $db->commit(); echo "🎉 Data Migration & Hierarchy Seeding Completed!
"; echo ""; } catch (Exception $e) { if ($db->inTransaction()) $db->rollBack(); echo "❌ Critical Error: " . $e->getMessage() . "
"; } echo "