import supa from '@/components/supadb/aksupainstance.uts' import { rpcOrValue } from './rpc.uts' export type CustomReportListItem = { id: string title: string description: string period: string updated_at: string } export type CreateCustomReportParams = { title: string description: string period: string metrics: Array chartType: string } export type UpdateCustomReportParams = { reportId: string title: string description: string | null period: string | null } export async function listCustomReports(ownerUserId: string): Promise> { const res: any = await supa .from('analytics_reports') .select('id, title, description, period, updated_at') .eq('type', 'custom') .eq('owner_user_id', ownerUserId) .order('updated_at', { ascending: false } as any) if (res?.error != null) { throw res.error } const rows: Array = Array.isArray(res.data) ? (res.data as Array) : [] const list: Array = [] for (let i = 0; i < rows.length; i++) { const r = rows[i] list.push({ id: `${r.id}`, title: `${r.title}`, description: `${r.description || ''}`, period: `${r.period || ''}`, updated_at: `${r.updated_at || ''}` }) } return list } export async function createCustomReport(params: CreateCustomReportParams): Promise { const data = await rpcOrValue('rpc_create_custom_report', { p_title: params.title, p_description: params.description || '', p_period: params.period, p_metrics: params.metrics, p_chart_type: params.chartType || 'line' } as UTSJSONObject) if (data == null) { throw new Error('保存失败:未返回报表ID') } return `${data}` } export async function updateCustomReport(params: UpdateCustomReportParams): Promise { await rpcOrValue('rpc_update_custom_report', { p_report_id: params.reportId, p_title: params.title, p_description: params.description, p_period: params.period } as UTSJSONObject) return true } export async function deleteCustomReport(reportId: string): Promise { await rpcOrValue('rpc_delete_custom_report', { p_report_id: reportId } as UTSJSONObject) return true }