import axios from 'axios'; import { WorkOrder, WorkOrderSettings, WorkOrderStatus, WorkOrderPriority } from '../../share/monitorTypes.ts' // 分类数据缓存 let categoriesCache: string[] | null = null; let cacheExpireTime = 0; const CACHE_EXPIRE_DURATION = 5 * 60 * 1000; // 5分钟缓存 interface WorkOrderListParams { page?: number; pageSize?: number; status?: WorkOrderStatus; priority?: WorkOrderPriority; device_id?: number; creator_id?: number; } interface WorkOrderListResponse { data: WorkOrder[]; pagination: { total: number; current: number; pageSize: number; totalPages: number; }; } interface WorkOrderDetailResponse { data: WorkOrder; message?: string; } interface WorkOrderCreateResponse { message: string; data: WorkOrder; } interface WorkOrderUpdateResponse { message: string; data: WorkOrder; } interface WorkOrderStatusChangeResponse { message: string; status: string; history_id?: string; } interface WorkOrderDeadlineResponse { data: { deadline: string; remaining_hours: number; is_overdue: boolean; }; } interface WorkOrderHistoryItem { id: string; status_from: string; status_to: string; operator: string; comment?: string; created_at: string; } interface WorkOrderHistoryResponse { data: WorkOrderHistoryItem[]; pagination: { total: number; current: number; pageSize: number; }; } interface WorkOrderAssignResponse { message: string; assignee: string; } interface WorkOrderSettingsResponse { data: WorkOrderSettings; message?: string; } interface WorkOrderCategoryResponse { data: string[]; message?: string; } interface WorkOrderAttachmentResponse { message: string; data: { id: string; url: string; name: string; }; } interface WorkOrderCommentResponse { message: string; data: { id: string; content: string; created_at: string; author: string; }[]; } export const WorkOrderAPI = { getList: async (params?: WorkOrderListParams): Promise => { try { const response = await axios.get('/work-orders', { params }); return response.data; } catch (error) { if (axios.isAxiosError(error)) { const errorMessage = error.response?.data?.message || error.message; throw new Error(`获取工单列表失败: ${errorMessage}`); } throw new Error('获取工单列表失败: 未知错误'); } }, create: async (data: { title: string; device_id?: number; problem_desc: string; problem_type: string; priority: WorkOrderPriority; deadline?: Date; attachments?: Array<{ id: string; url: string; name: string; }>; }): Promise => { try { const response = await axios.post('/work-orders', data); return response.data; } catch (error) { throw error; } }, getDetail: async (id: string): Promise => { try { const response = await axios.get(`/work-orders/${id}`); return response.data; } catch (error) { throw error; } }, update: async (id: string, data: { status?: WorkOrderStatus; priority?: WorkOrderPriority; feedback?: string; assignee_id?: number; deadline?: Date; }): Promise => { try { const response = await axios.put(`/work-orders/${id}`, data); return response.data; } catch (error) { throw error; } }, changeStatus: async (id: string, status: string, operator: string, comment?: string): Promise => { try { const response = await axios.post(`/work-orders/${id}/status`, { status, operator, comment }); return response.data; } catch (error) { throw error; } }, getDeadline: async (id: string): Promise => { try { const response = await axios.get(`/work-orders/${id}/deadline`); return response.data; } catch (error) { throw error; } }, getHistory: async (id: string, page = 1, pageSize = 10): Promise => { try { const response = await axios.get(`/work-orders/${id}/history`, { params: { page, pageSize } }); return response.data; } catch (error) { throw error; } }, getStatusHistory: async (id: string): Promise => { try { const response = await axios.get(`/work-orders/${id}/status-history`); return response.data; } catch (error) { throw error; } }, assign: async (id: string, assignee: string): Promise => { try { const response = await axios.post(`/work-orders/${id}/assign`, { assignee }); return response.data; } catch (error) { throw error; } }, getSettings: async (): Promise => { try { const response = await axios.get('/work-orders/settings'); return response.data; } catch (error) { throw error; } }, updateSettings: async (data: WorkOrderSettings): Promise => { try { const response = await axios.post('/work-orders/settings', data); return response.data; } catch (error) { throw error; } }, // 新增分类相关API getCategories: async (): Promise => { // 检查缓存是否有效 if (categoriesCache && Date.now() < cacheExpireTime) { return { data: categoriesCache }; } try { const response = await axios.get('/work-orders/categories'); categoriesCache = response.data.data; cacheExpireTime = Date.now() + CACHE_EXPIRE_DURATION; return response.data; } catch (error) { if (axios.isAxiosError(error)) { const errorMessage = error.response?.data?.message || error.message; throw new Error(`获取分类失败: ${errorMessage}`); } throw new Error('获取分类失败: 未知错误'); } }, // 新增附件相关API uploadAttachment: async (id: string, file: File): Promise => { try { const formData = new FormData(); formData.append('file', file); const response = await axios.post(`/work-orders/${id}/attachments`, formData, { headers: { 'Content-Type': 'multipart/form-data' } }); return response.data; } catch (error) { throw error; } }, // 新增评论相关API getComments: async (id: string): Promise => { try { const response = await axios.get(`/work-orders/${id}/comments`); return response.data; } catch (error) { throw error; } }, addComment: async (id: string, content: string): Promise => { try { const response = await axios.post(`/work-orders/${id}/comments`, { content }); return response.data; } catch (error) { throw error; } }, exportList: async (params?: WorkOrderListParams): Promise => { try { const response = await axios.get('/work-orders/export', { params, responseType: 'blob' }); return response.data; } catch (error) { if (axios.isAxiosError(error)) { const errorMessage = error.response?.data?.message || error.message; throw new Error(`导出工单失败: ${errorMessage}`); } throw new Error('导出工单失败: 未知错误'); } }, };