import React, { useState, useEffect } from 'react'; import { type MetaFunction, type LoaderFunctionArgs, redirect } from "@remix-run/node"; import { useLoaderData, useSearchParams, useSubmit, Link } from "@remix-run/react"; import { Button } from '~/components/ui/Button'; import { Card } from '~/components/ui/Card'; import { Tag } from '~/components/ui/Tag'; import { StatusDot } from '~/components/ui/StatusDot'; import rulesStyles from "~/styles/pages/rules_index.css?url"; import type { Rule, RuleType, RulePriority } from '~/models/rule'; import { RULE_TYPE_COLORS, RULE_PRIORITY_LABELS, RULE_PRIORITY_COLORS } from '~/models/rule'; import type { TagColor } from '~/components/ui/Tag'; import { Table } from '~/components/ui/Table'; import { FilterPanel, FilterSelect, SearchFilter } from '~/components/ui/FilterPanel'; import { Pagination } from '~/components/ui/Pagination'; import { getRulesList, deleteRule, duplicateRule, getRuleTypes, getRuleGroupsByType, type RuleType as ApiRuleType, type RuleGroup } from '~/api/evaluation_points/rules'; export const links = () => [ { rel: "stylesheet", href: rulesStyles } ]; export const meta: MetaFunction = () => { return [ { title: "中国烟草AI合同及卷宗审核系统 - 评查点列表" }, { name: "rules", content: "管理评查点规则,支持根据类型、规则组和状态进行筛选" }, { name: "keywords", content: "评查点,合同审核,规则管理,中国烟草" } ]; }; // 声明loader返回的数据类型 export type LoaderData = { rules: Rule[]; totalCount: number; currentPage: number; pageSize: number; totalPages: number; ruleTypes: ApiRuleType[]; // 添加评查点类型 }; // API返回的数据映射到前端模型 interface ApiRule { id: string; code: string; name: string; ruleType: string; groupId: string; groupName: string; priority: string; description: string; isActive: boolean; createdAt: string; updatedAt: string; } function mapApiRuleToModel(apiRule: ApiRule): Rule { return { id: apiRule.id, code: apiRule.code, name: apiRule.name, ruleType: apiRule.ruleType as RuleType, // 类型转换 ruleGroupId: apiRule.groupId, groupName: apiRule.groupName, priority: apiRule.priority as RulePriority, // 类型转换 description: apiRule.description, checkMethod: 'automatic', // 默认值 prompt: apiRule.description, // 使用描述作为默认prompt isActive: apiRule.isActive, createdAt: apiRule.createdAt, updatedAt: apiRule.updatedAt }; } export async function loader({ request }: LoaderFunctionArgs) { const url = new URL(request.url); // 从 URL 参数中提取查询条件 const params = { ruleType: url.searchParams.get("ruleType") || undefined, groupId: url.searchParams.get("groupId") || undefined, isActive: url.searchParams.get("isActive") ? url.searchParams.get("isActive") === "true" : undefined, keyword: url.searchParams.get("keyword") || undefined, page: parseInt(url.searchParams.get("page") || "1", 10), pageSize: parseInt(url.searchParams.get("pageSize") || "10", 10) }; try { // 获取评查点类型列表 const typeResponse = await getRuleTypes(); if (typeResponse.error) { console.error('获取评查点类型失败:', typeResponse.error); } const ruleTypes = typeResponse.error ? [] : typeResponse.data; // 使用API调用获取数据 const response = await getRulesList(params); // API错误处理集中在rules.ts中,这里只需检查是否有错误 if (response.error) { throw new Error(response.error); } if (!response.data) { throw new Error('API返回数据为空'); } const apiRules = response.data.rules; const totalCount = response.data.totalCount; const rules = apiRules.map((apiRule: ApiRule) => mapApiRuleToModel(apiRule)); // 计算总页数 const totalPages = Math.ceil(totalCount / params.pageSize); // 验证页码范围 if (params.page < 1 || (totalCount > 0 && params.page > totalPages)) { const newUrl = new URL(request.url); newUrl.searchParams.set('page', '1'); return redirect(newUrl.pathname + newUrl.search); } return Response.json({ rules, totalCount, currentPage: params.page, pageSize: params.pageSize, totalPages, ruleTypes }, { headers: { "Cache-Control": "max-age=60, s-maxage=180" } }); } catch (error) { console.error('加载评查点列表失败:', error); throw new Response('加载评查点列表失败', { status: 500 }); } } export async function action({ request }: LoaderFunctionArgs) { const formData = await request.formData(); const _action = formData.get('_action'); const ruleId = formData.get('ruleId'); if (!ruleId) { return Response.json({ success: false, error: "缺少评查点ID" }, { status: 400 }); } try { if (_action === 'delete') { // 调用API删除评查点 console.log(`删除评查点 ${ruleId}`); try { const deleteResponse = await deleteRule(ruleId as string); if (deleteResponse.error) { throw new Error(deleteResponse.error); } // 删除成功,获取当前URL const url = new URL(request.url); // 返回重定向响应,以刷新页面数据 return redirect(`${url.pathname}${url.search}`); } catch (error) { console.error('删除评查点失败:', error); return Response.json({ success: false, error: error instanceof Error ? error.message : "删除失败" }, { status: 500 }); } } if (_action === 'duplicate') { // 复制评查点 console.log(`复制评查点 ${ruleId}`); try { const duplicateResponse = await duplicateRule(ruleId as string); if (duplicateResponse.error) { throw new Error(duplicateResponse.error); } // 复制成功,获取当前URL const url = new URL(request.url); // 返回重定向响应,以刷新页面数据 return redirect(`${url.pathname}${url.search}`); } catch (error) { console.error('复制评查点失败:', error); return Response.json({ success: false, error: error instanceof Error ? error.message : "复制失败" }, { status: 500 }); } } return Response.json({ success: false, error: "未知操作" }, { status: 400 }); } catch (error) { console.error('操作评查点失败:', error); return Response.json({ success: false, error: "操作失败" }, { status: 500 }); } } export function ErrorBoundary() { return (
加载评查点列表时发生错误。请稍后再试,或联系管理员。
确定要删除评查点“{ruleToDelete.name}”吗?