count(); if ($existing > 0) { $this->command->info(' ⚠ departments: 이미 '.$existing.'개 존재 (스킵)'); return; } // 1레벨: 본부 (3개) $divisions = [ ['code' => 'DIV01', 'name' => '경영본부', 'description' => '경영 및 기획 업무'], ['code' => 'DIV02', 'name' => '기술본부', 'description' => '기술 개발 및 연구'], ['code' => 'DIV03', 'name' => '영업본부', 'description' => '영업 및 마케팅'], ]; // 2레벨: 부서 (본부별 2~3개씩) $departments = [ 'DIV01' => [ ['code' => 'HR', 'name' => '인사팀', 'description' => '인사 및 채용 관리'], ['code' => 'FIN', 'name' => '재무팀', 'description' => '재무 및 회계 관리'], ['code' => 'ADM', 'name' => '총무팀', 'description' => '총무 및 시설 관리'], ], 'DIV02' => [ ['code' => 'DEV', 'name' => '개발팀', 'description' => '소프트웨어 개발'], ['code' => 'QA', 'name' => 'QA팀', 'description' => '품질 보증 및 테스트'], ['code' => 'INFRA', 'name' => '인프라팀', 'description' => '서버 및 인프라 관리'], ], 'DIV03' => [ ['code' => 'SALES', 'name' => '영업팀', 'description' => '영업 및 고객 관리'], ['code' => 'MKT', 'name' => '마케팅팀', 'description' => '마케팅 및 홍보'], ], ]; $count = 0; $divisionIds = []; // 본부 생성 foreach ($divisions as $index => $division) { $dept = Department::create([ 'tenant_id' => $tenantId, 'parent_id' => null, 'code' => $division['code'], 'name' => $division['name'], 'description' => $division['description'], 'is_active' => true, 'sort_order' => $index + 1, 'created_by' => $userId, ]); $divisionIds[$division['code']] = $dept->id; $count++; } // 부서 생성 foreach ($departments as $divCode => $depts) { $parentId = $divisionIds[$divCode] ?? null; foreach ($depts as $index => $dept) { Department::create([ 'tenant_id' => $tenantId, 'parent_id' => $parentId, 'code' => $dept['code'], 'name' => $dept['name'], 'description' => $dept['description'], 'is_active' => true, 'sort_order' => $index + 1, 'created_by' => $userId, ]); $count++; } } // 사용자-부서 연결 $this->assignUsersToDepartments($tenantId); $this->command->info(' ✓ departments: '.$count.'개 생성 (본부 3개, 부서 8개)'); } private function assignUsersToDepartments(int $tenantId): void { // 테넌트 소속 사용자 조회 $userIds = DB::table('user_tenants') ->where('tenant_id', $tenantId) ->pluck('user_id') ->toArray(); // 부서 조회 (2레벨만) $departments = Department::where('tenant_id', $tenantId) ->whereNotNull('parent_id') ->get(); if ($departments->isEmpty() || empty($userIds)) { return; } // 사용자를 부서에 분배 foreach ($userIds as $index => $userId) { $dept = $departments[$index % $departments->count()]; $isPrimary = ($index % $departments->count() === 0); // 첫 번째만 primary // 이미 연결되어 있는지 확인 $exists = DB::table('department_user') ->where('user_id', $userId) ->where('department_id', $dept->id) ->exists(); if (! $exists) { DB::table('department_user')->insert([ 'tenant_id' => $tenantId, 'user_id' => $userId, 'department_id' => $dept->id, 'is_primary' => $isPrimary, 'joined_at' => now(), 'created_at' => now(), 'updated_at' => now(), ]); } } } }