diff --git a/app/api/evaluation_points/rule-groups.ts b/app/api/evaluation_points/rule-groups.ts index ee4d4aa..84ba814 100644 --- a/app/api/evaluation_points/rule-groups.ts +++ b/app/api/evaluation_points/rule-groups.ts @@ -937,9 +937,204 @@ async function deleteEvaluationPointsByGroupId(groupId: string, token?: string): return { success: true }; } catch (error) { console.error('删除评查点失败:', error); - return { - success: false, + return { + success: false, error: error instanceof Error ? error.message : '删除评查点失败' }; } +} + +// ==================== 批量操作接口 ==================== + +/** + * 批量更新分组状态(启用/禁用) + * @param ids 分组ID列表 + * @param is_enabled 目标状态 + * @param token JWT token (可选) + * @returns 更新结果 + */ +export async function batchUpdateRuleGroupStatus( + ids: string[], + is_enabled: boolean, + token?: string +): Promise<{ + success: boolean; + updated_count: number; + failed_ids: string[]; + errors?: Array<{ id: string; error: string }>; +}> { + try { + // ========== 1. 参数验证 ========== + + if (!Array.isArray(ids) || ids.length === 0) { + return { + success: false, + updated_count: 0, + failed_ids: [], + errors: [{ id: 'validation', error: 'ID列表不能为空' }] + }; + } + + // 验证每个ID的有效性 + const invalidIds = ids.filter(id => !id || id.trim() === ''); + if (invalidIds.length > 0) { + return { + success: false, + updated_count: 0, + failed_ids: ids, + errors: [{ id: 'validation', error: '存在无效的分组ID' }] + }; + } + + // ========== 2. 逐个更新(确保每个分组都能被正确处理) ========== + + const failedIds: string[] = []; + const errors: Array<{ id: string; error: string }> = []; + let updatedCount = 0; + + for (const id of ids) { + try { + // 验证分组是否存在 + const groupResponse = await getRuleGroup(id, token); + if (groupResponse.error || !groupResponse.data) { + failedIds.push(id); + errors.push({ id, error: '分组不存在或无法访问' }); + continue; + } + + // 执行更新 + const updateResponse = await postgrestPut | RuleGroup, Partial>( + 'evaluation_point_groups', + { is_enabled }, + { id }, + token + ); + + if (updateResponse.error) { + failedIds.push(id); + errors.push({ id, error: updateResponse.error }); + } else { + updatedCount++; + } + } catch (error) { + failedIds.push(id); + errors.push({ + id, + error: error instanceof Error ? error.message : '更新失败' + }); + } + } + + // ========== 3. 返回结果 ========== + + return { + success: failedIds.length === 0, + updated_count: updatedCount, + failed_ids: failedIds, + errors: errors.length > 0 ? errors : undefined + }; + } catch (error) { + console.error('批量更新分组状态失败:', error); + return { + success: false, + updated_count: 0, + failed_ids: ids, + errors: [{ + id: 'batch', + error: error instanceof Error ? error.message : '批量更新失败' + }] + }; + } +} + +/** + * 批量删除分组(安全的阻止删除策略) + * @param ids 分组ID列表 + * @param token JWT token (可选) + * @returns 删除结果 + */ +export async function batchDeleteRuleGroups( + ids: string[], + token?: string +): Promise<{ + success: boolean; + deleted_count: number; + failed_ids: string[]; + errors?: Array<{ id: string; error: string; details?: { hasChildren?: boolean; hasPoints?: boolean } }>; +}> { + try { + // ========== 1. 参数验证 ========== + + if (!Array.isArray(ids) || ids.length === 0) { + return { + success: false, + deleted_count: 0, + failed_ids: [], + errors: [{ id: 'validation', error: 'ID列表不能为空' }] + }; + } + + // 验证每个ID的有效性 + const invalidIds = ids.filter(id => !id || id.trim() === ''); + if (invalidIds.length > 0) { + return { + success: false, + deleted_count: 0, + failed_ids: ids, + errors: [{ id: 'validation', error: '存在无效的分组ID' }] + }; + } + + // ========== 2. 逐个删除(使用安全的阻止删除策略) ========== + + const failedIds: string[] = []; + const errors: Array<{ id: string; error: string; details?: { hasChildren?: boolean; hasPoints?: boolean } }> = []; + let deletedCount = 0; + + for (const id of ids) { + try { + const deleteResult = await deleteRuleGroup(id, token); + + if (!deleteResult.success) { + failedIds.push(id); + errors.push({ + id, + error: deleteResult.error || '删除失败', + details: deleteResult.details ? { + hasChildren: deleteResult.details.hasChildren, + hasPoints: deleteResult.details.hasPoints + } : undefined + }); + } else { + deletedCount++; + } + } catch (error) { + failedIds.push(id); + errors.push({ + id, + error: error instanceof Error ? error.message : '删除失败' + }); + } + } + + // ========== 3. 返回结果 ========== + + return { + success: failedIds.length === 0, + deleted_count: deletedCount, + failed_ids: failedIds, + errors: errors.length > 0 ? errors : undefined + }; + } catch (error) { + console.error('批量删除分组失败:', error); + return { + success: false, + deleted_count: 0, + failed_ids: ids, + errors: [{ + id: 'batch', + error: error instanceof Error ? error.message : '批量删除失败' + }] + }; + } } \ No newline at end of file