systemOnly() ->withCount('fields') ->withTrashed(); // 검색 필터 if (! empty($filters['search'])) { $search = $filters['search']; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%{$search}%") ->orWhere('board_code', 'like', "%{$search}%") ->orWhere('description', 'like', "%{$search}%"); }); } // 게시판 유형 필터 if (! empty($filters['board_type'])) { $query->where('board_type', $filters['board_type']); } // 활성 상태 필터 if (isset($filters['is_active']) && $filters['is_active'] !== '') { $query->where('is_active', $filters['is_active']); } // Soft Delete 필터 if (isset($filters['trashed'])) { if ($filters['trashed'] === 'only') { $query->onlyTrashed(); } elseif ($filters['trashed'] === 'with') { $query->withTrashed(); } } // 정렬 $sortBy = $filters['sort_by'] ?? 'id'; $sortDirection = $filters['sort_direction'] ?? 'desc'; $query->orderBy($sortBy, $sortDirection); return $query->paginate($perPage); } /** * 시스템 게시판 목록 (드롭다운용) */ public function getActiveBoardList(): Collection { return Board::query() ->systemOnly() ->active() ->orderBy('name') ->get(['id', 'board_code', 'name', 'board_type']); } /** * 특정 게시판 조회 */ public function getBoardById(int $id, bool $withTrashed = false): ?Board { $query = Board::query() ->systemOnly() ->with('fields') ->withCount('fields'); if ($withTrashed) { $query->withTrashed(); } return $query->find($id); } /** * 게시판 생성 */ public function createBoard(array $data): Board { // 시스템 게시판 설정 $data['is_system'] = true; $data['tenant_id'] = null; $data['created_by'] = auth()->id(); return Board::create($data); } /** * 게시판 수정 */ public function updateBoard(int $id, array $data): bool { $board = Board::systemOnly()->findOrFail($id); $data['updated_by'] = auth()->id(); return $board->update($data); } /** * 게시판 삭제 (Soft Delete) */ public function deleteBoard(int $id): bool { $board = Board::systemOnly()->findOrFail($id); $board->deleted_by = auth()->id(); $board->save(); return $board->delete(); } /** * 게시판 복원 */ public function restoreBoard(int $id): bool { $board = Board::systemOnly()->onlyTrashed()->findOrFail($id); $board->deleted_by = null; return $board->restore(); } /** * 게시판 영구 삭제 */ public function forceDeleteBoard(int $id): bool { $board = Board::systemOnly()->withTrashed()->findOrFail($id); // 관련 필드 삭제 $board->fields()->delete(); return $board->forceDelete(); } /** * 게시판 코드 중복 체크 */ public function isCodeExists(string $code, ?int $excludeId = null): bool { $query = Board::where('board_code', $code) ->where('is_system', true); if ($excludeId) { $query->where('id', '!=', $excludeId); } return $query->exists(); } /** * 게시판 활성/비활성 토글 */ public function toggleActive(int $id): Board { $board = Board::systemOnly()->findOrFail($id); $board->is_active = ! $board->is_active; $board->updated_by = auth()->id(); $board->save(); return $board; } /** * 게시판 통계 */ public function getBoardStats(): array { return [ 'total' => Board::systemOnly()->count(), 'active' => Board::systemOnly()->active()->count(), 'inactive' => Board::systemOnly()->where('is_active', false)->count(), 'trashed' => Board::systemOnly()->onlyTrashed()->count(), ]; } // ========================================================================= // 필드 관리 // ========================================================================= /** * 게시판 필드 목록 조회 */ public function getBoardFields(int $boardId): Collection { return BoardSetting::where('board_id', $boardId) ->orderBy('sort_order') ->get(); } /** * 게시판 필드 추가 */ public function addBoardField(int $boardId, array $data): BoardSetting { $data['board_id'] = $boardId; $data['created_by'] = auth()->id(); // 기본 정렬 순서 설정 if (! isset($data['sort_order'])) { $maxOrder = BoardSetting::where('board_id', $boardId)->max('sort_order') ?? 0; $data['sort_order'] = $maxOrder + 1; } return BoardSetting::create($data); } /** * 게시판 필드 수정 */ public function updateBoardField(int $fieldId, array $data): bool { $field = BoardSetting::findOrFail($fieldId); $data['updated_by'] = auth()->id(); return $field->update($data); } /** * 게시판 필드 삭제 */ public function deleteBoardField(int $fieldId): bool { $field = BoardSetting::findOrFail($fieldId); return $field->delete(); } /** * 게시판 필드 순서 변경 */ public function reorderBoardFields(int $boardId, array $fieldIds): bool { foreach ($fieldIds as $order => $fieldId) { BoardSetting::where('id', $fieldId) ->where('board_id', $boardId) ->update(['sort_order' => $order + 1]); } return true; } /** * 게시판 유형 목록 (사용 중인 유형들) */ public function getBoardTypes(): array { return Board::systemOnly() ->whereNotNull('board_type') ->distinct() ->pluck('board_type') ->toArray(); } }