[이미지 기반 부서 계층 구조] 및 [실제 데이터] 일괄 구축을 시작합니다.
✔ 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 with Detailed Specs...
"; // 테이블 스키마 상세 사양 컬럼 확장 $spec_columns = [ 'cpu' => 'TEXT', 'npu' => 'TEXT', 'hdd0_model' => 'TEXT', 'hdd0_capacity' => 'TEXT', 'hdd1_model' => 'TEXT', 'hdd1_capacity' => 'TEXT', 'ram' => 'TEXT', 'asset_status' => 'TEXT', 'assigned_user_name' => 'TEXT', 'fixed_ip' => 'TEXT', 'options' => 'TEXT', 'power_rating' => 'TEXT', 'manufacturer' => 'TEXT', 'vendor' => 'TEXT', 'product_name' => 'TEXT' ]; foreach ($spec_columns as $col => $type) { try { $db->exec("ALTER TABLE laptop_assets ADD COLUMN $col $type"); } catch (Exception $e) { /* 컬럼 이미 존재 시 무시 */ } } $laptops_raw = getCSV($files['laptops']); $model_map = []; $model_stmt = $db->prepare("INSERT INTO laptop_models (model_name, manufacturer) VALUES (?, ?)"); // 확장된 INSERT 문 $asset_sql = "INSERT INTO laptop_assets ( asset_tag, model_id, current_user_id, status, serial_number, purchase_date, remarks, cpu, npu, hdd0_model, hdd0_capacity, hdd1_model, hdd1_capacity, ram, asset_status, assigned_user_name, fixed_ip, options, power_rating, manufacturer, vendor, product_name ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"; $asset_stmt = $db->prepare($asset_sql); 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']; } } $internal_status = $current_user_id ? 'assigned' : 'stock'; if (strpos($user_name, '업무용') !== false || strpos($user_name, '임시') !== false) { $internal_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['프로세서'] ?? ''; $npu = $row['NPU'] ?? ''; $hdd0_model = $row['HDD 0 / 타입'] ?? ''; $hdd0_capacity = $row['HDD 0 / 설치 값'] ?? ''; $hdd1_model = $row['HDD 1 / 타입'] ?? ''; $hdd1_capacity = $row['HDD 1 / 설치 값'] ?? ''; $ram_val = $row['RAM'] ?? ''; $asset_status = $row['자산상태'] ?? ''; $assigned_user_name = $row['배정 사용자'] ?? ''; $fixed_ip = $row['고정IP'] ?? $row['고정 IP'] ?? ''; $options = $row['옵션'] ?? ''; $power_rating = $row['정격입력/출력'] ?? ''; $vendor = $row['판매사'] ?? ''; $product_name = $row['품명'] ?? ''; $remarks = $row['비고'] ?? $row['특기사항'] ?? ''; $asset_stmt->execute([ $row['자산관리번호'], $model_map[$model_name], $current_user_id, $internal_status, $row['시리얼넘버/EX'], $p_date, $remarks, $cpu, $npu, $hdd0_model, $hdd0_capacity, $hdd1_model, $hdd1_capacity, $ram_val, $asset_status, $assigned_user_name, $fixed_ip, $options, $power_rating, $manufacturer, $vendor, $product_name ]); } echo "✔ Laptop assets imported with all detailed 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 "