import { ApiResponse, BatchGoalOperationRequest, BatchGoalOperationResult, CompleteGoalRequest, CreateGoalRequest, GetGoalCompletionsQuery, GetGoalsQuery, Goal, GoalCompletion, GoalDetailResponse, GoalListItem, GoalStats, PaginatedResponse, UpdateGoalRequest, } from '@/types/goals'; import { api } from './api'; // 目标管理API服务 /** * 创建目标 */ export const createGoal = async (goalData: CreateGoalRequest): Promise => { return api.post('/goals', goalData); }; /** * 获取目标列表 */ export const getGoals = async (query: GetGoalsQuery = {}): Promise> => { const searchParams = new URLSearchParams(); Object.entries(query).forEach(([key, value]) => { if (value !== undefined && value !== null) { searchParams.append(key, String(value)); } }); const queryString = searchParams.toString(); const path = queryString ? `/goals?${queryString}` : '/goals'; return api.get>(path); }; /** * 获取目标详情 */ export const getGoalById = async (goalId: string): Promise> => { return api.get>(`/goals/${goalId}`); }; /** * 更新目标 */ export const updateGoal = async (goalId: string, goalData: UpdateGoalRequest): Promise => { return api.put(`/goals/${goalId}`, goalData); }; /** * 删除目标 */ export const deleteGoal = async (goalId: string): Promise> => { return api.delete>(`/goals/${goalId}`); }; /** * 记录目标完成 */ export const completeGoal = async (goalId: string, completionData: CompleteGoalRequest = {}): Promise> => { return api.post>(`/goals/${goalId}/complete`, completionData); }; /** * 获取目标完成记录 */ export const getGoalCompletions = async ( goalId: string, query: GetGoalCompletionsQuery = {} ): Promise>> => { const searchParams = new URLSearchParams(); Object.entries(query).forEach(([key, value]) => { if (value !== undefined && value !== null) { searchParams.append(key, String(value)); } }); const queryString = searchParams.toString(); const path = queryString ? `/goals/${goalId}/completions?${queryString}` : `/goals/${goalId}/completions`; return api.get>>(path); }; /** * 获取目标统计信息 */ export const getGoalStats = async (): Promise> => { return api.get>('/goals/stats/overview'); }; /** * 批量操作目标 */ export const batchOperateGoals = async (operationData: BatchGoalOperationRequest): Promise> => { return api.post>('/goals/batch', operationData); }; // 导出所有API方法 export const goalsApi = { createGoal, getGoals, getGoalById, updateGoal, deleteGoal, completeGoal, getGoalCompletions, getGoalStats, batchOperateGoals, }; export default goalsApi;