diff --git a/app/api/evaluation_points/rules.ts b/app/api/evaluation_points/rules.ts index 5db0c44..29eb3a7 100644 --- a/app/api/evaluation_points/rules.ts +++ b/app/api/evaluation_points/rules.ts @@ -745,9 +745,45 @@ export async function updateRule(id: string, ruleData: Partial { try { - // console.log(`开始删除评查点, ID: ${id}`); - - // 使用 PostgREST 语法,通过查询参数指定要删除的行 + // 1. 验证评查点是否存在 + const existingRuleResponse = await getRule(id, token); + if (existingRuleResponse.error || !existingRuleResponse.data) { + return { error: '评查点不存在', status: 404 }; + } + + // 2. 检查是否有关联的评查结果 + // 注意:这里假设存在 evaluation_results 表,如果不存在可以跳过此检查 + try { + const resultsParams: PostgrestParams = { + select: 'id', + filter: { + 'evaluation_point_id': `eq.${id}` + }, + limit: 1, // 只需要知道是否存在,不需要全部 + token + }; + + const resultsResponse = await postgrestGet<{code: number; msg: string; data: Array<{id: number}>}>('evaluation_results', resultsParams); + + let hasResults = false; + if (resultsResponse.data && 'code' in resultsResponse.data && resultsResponse.data.data) { + hasResults = Array.isArray(resultsResponse.data.data) && resultsResponse.data.data.length > 0; + } else if (Array.isArray(resultsResponse.data)) { + hasResults = resultsResponse.data.length > 0; + } + + if (hasResults) { + return { + error: '该评查点已被使用,无法删除。如需停用,请使用禁用功能。', + status: 400 + }; + } + } catch (error) { + // 如果 evaluation_results 表不存在,忽略此错误并继续删除 + console.warn('检查评查结果关联时出错(表可能不存在):', error); + } + + // 3. 使用 PostgREST 语法,通过查询参数指定要删除的行 const postgrestParams: PostgrestParams = { filter: { 'id': `eq.${id}` @@ -757,7 +793,7 @@ export async function deleteRule(id: string, token?: string): Promise<{data: Rul }, token }; - + // 使用postgrestDelete删除评查点 const response = await postgrestDelete('evaluation_points', postgrestParams);