import { invoke } from '@tauri-apps/api/core' export interface ResourceCategory { id: string title: string ai_prompt: string color: string created_at: string updated_at: string is_active: boolean } export interface CreateCategoryRequest { title: string ai_prompt: string color: string } export interface UpdateCategoryRequest { title?: string ai_prompt?: string color?: string is_active?: boolean } export interface ApiResponse { status: boolean data?: T msg?: string } export class ResourceCategoryService { /** * 获取所有分类 */ static async getAllCategories(): Promise> { try { const result = await invoke('get_all_resource_categories') return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse } catch (error) { console.error('Failed to get all categories:', error) return { status: false, msg: error instanceof Error ? error.message : 'Unknown error' } } } /** * 根据ID获取分类 */ static async getCategoryById(categoryId: string): Promise> { try { const result = await invoke('get_resource_category_by_id', { categoryId }) return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse } catch (error) { console.error('Failed to get category by id:', error) return { status: false, msg: error instanceof Error ? error.message : 'Unknown error' } } } /** * 创建新分类 */ static async createCategory(request: CreateCategoryRequest): Promise> { try { const result = await invoke('create_resource_category', { request }) return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse } catch (error) { console.error('Failed to create category:', error) return { status: false, msg: error instanceof Error ? error.message : 'Unknown error' } } } static tryJsonParse(str: any) { try { if (typeof str === 'string') { return JSON.parse(str) } return str; } catch (e) { return str; } } /** * 更新分类 */ static async updateCategory( categoryId: string, request: UpdateCategoryRequest ): Promise> { try { const result = await invoke('update_resource_category', { categoryId, request }) return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse } catch (error) { console.error('Failed to update category:', error) return { status: false, msg: error instanceof Error ? error.message : 'Unknown error' } } } /** * 删除分类 */ static async deleteCategory(categoryId: string): Promise> { try { const result = await invoke('delete_resource_category', { categoryId }) return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse } catch (error) { console.error('Failed to delete category:', error) return { status: false, msg: error instanceof Error ? error.message : 'Unknown error' } } } /** * 搜索分类 */ static async searchCategories(keyword: string): Promise> { try { const result = await invoke('search_resource_categories', { keyword }) return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse } catch (error) { console.error('Failed to search categories:', error) return { status: false, msg: error instanceof Error ? error.message : 'Unknown error' } } } }