封装评查点列表的接口,完成删除和查找

This commit is contained in:
2025-04-03 19:59:57 +08:00
parent 2bde2bd76e
commit 145aec6aa6
5 changed files with 959 additions and 342 deletions
+411 -84
View File
@@ -7,8 +7,8 @@ import dayjs from 'dayjs';
export interface RulesQueryParams {
page?: number;
pageSize?: number;
ruleType?: string;
groupId?: string;
ruleType?: string; // 评查点类型ID
groupId?: string; // 规则组ID
isActive?: boolean;
keyword?: string;
orderBy?: string;
@@ -30,12 +30,13 @@ export interface ApiRule {
id: number;
code: string;
name: string;
evaluation_point_groups_id: number;
evaluation_point_groups_id: number | null;
risk: string;
description: string;
is_enabled: boolean;
evaluation_point_groups?: {
name: string;
first_name: string;
second_name: string;
};
references_laws: Record<string, unknown>;
extraction_config: {
@@ -94,23 +95,32 @@ function mapApiRuleToFrontendModel(apiRule: ApiRule): Rule {
'低': 'low'
};
// 规则类型映射(这里根据实际业务逻辑设置一个默认值)
const ruleType = 'essential'; // 实际应用中可能需要从其他字段推断
//评查点类型映射
// 优先使用关联查询获取的分组名称,如果不存在则使用默认值
const groupName = apiRule.evaluation_point_groups?.name || `${apiRule.evaluation_point_groups_id}`;
// 规则类型映射(这里根据实际业务逻辑设置一个默认值
// const ruleType = 'essential'; // 实际应用中可能需要从其他字段推断
// console.log("apiRule.evaluation_point_groups",apiRule);
// 如果evaluation_point_groups_id为null或空,则不显示ruleType和groupName
const isGroupIdEmpty = !apiRule.evaluation_point_groups_id;
// 规则类型只在有分组ID时才显示
const ruleType = isGroupIdEmpty ? '' : (apiRule.evaluation_point_groups?.first_name || '');
// 规则组名称只在有分组ID时才显示
const groupName = isGroupIdEmpty ? '' : (apiRule.evaluation_point_groups?.second_name || `${apiRule.evaluation_point_groups_id}`);
return {
id: apiRule.id.toString(),
code: apiRule.code,
name: apiRule.name,
id: apiRule.id ? apiRule.id.toString() : '', // 添加空值检查
code: apiRule.code || '',
name: apiRule.name || '',
ruleType: ruleType,
groupId: apiRule.evaluation_point_groups_id.toString(),
groupId: isGroupIdEmpty ? '' : apiRule.evaluation_point_groups_id?.toString() || '',
groupName: groupName,
priority: priorityMap[apiRule.risk] || 'medium',
description: apiRule.description,
isActive: apiRule.is_enabled,
createdAt: apiRule.created_at,
description: apiRule.description || '',
isActive: apiRule.is_enabled === undefined ? false : apiRule.is_enabled,
createdAt: apiRule.created_at ,
updatedAt: apiRule.updated_at
};
}
@@ -141,14 +151,12 @@ export async function getRulesList(params: RulesQueryParams): Promise<{data: Rul
// 构建PostgrestParams参数
const postgrestParams: PostgrestParams = {
// 使用PostgREST资源嵌入语法获取关联数据
// 这里使用外键关系自动关联evaluation_point_groups表
// 修改select语句,不使用嵌入查询语法
select: `
id,
code,
name,
evaluation_point_groups_id,
evaluation_point_groups(name),
risk,
description,
is_enabled,
@@ -171,7 +179,7 @@ export async function getRulesList(params: RulesQueryParams): Promise<{data: Rul
}
};
// 添加精确匹配过滤
// 添加精确匹配过滤:规则组ID
if (groupId) {
postgrestParams.filter!['evaluation_point_groups_id'] = `eq.${groupId}`;
}
@@ -180,6 +188,47 @@ export async function getRulesList(params: RulesQueryParams): Promise<{data: Rul
postgrestParams.filter!['is_enabled'] = `eq.${isActive}`;
}
// 如果指定了评查点类型ID,需要先查询该类型下的所有规则组ID
if (ruleType) {
try {
// 先获取该类型下的所有规则组
const groupsParams: PostgrestParams = {
select: 'id',
filter: {
'pid': `eq.${ruleType}`
}
};
const groupsResponse = await postgrestGet<{code: number; msg: string; data: Array<{id: number}>}>('evaluation_point_groups', groupsParams);
if (groupsResponse.error) {
console.error('获取规则组列表失败:', groupsResponse.error);
} else {
let groupIds: number[] = [];
// 处理不同API响应格式
if (groupsResponse.data && 'code' in groupsResponse.data && groupsResponse.data.data) {
if (Array.isArray(groupsResponse.data.data) && groupsResponse.data.data.length > 0) {
groupIds = groupsResponse.data.data.map(group => group.id);
}
} else if (Array.isArray(groupsResponse.data) && groupsResponse.data.length > 0) {
groupIds = groupsResponse.data.map(group => group.id);
}
// 使用in过滤条件,如果找到规则组
if (groupIds.length > 0) {
postgrestParams.filter!['evaluation_point_groups_id'] = `in.(${groupIds.join(',')})`;
}
if (groupId) {
postgrestParams.filter!['evaluation_point_groups_id'] = `eq.${groupId}`;
}
}
} catch (error) {
console.error('获取规则组ID出错:', error);
// 错误不中断流程,继续使用其他筛选条件
}
}
// 添加模糊搜索
if (keyword) {
// 使用PostgREST的or条件查询
@@ -198,66 +247,90 @@ export async function getRulesList(params: RulesQueryParams): Promise<{data: Rul
return { error: response.error, status: response.status };
}
// 确保响应数据存在且符合预期格式
if (!response.data || !response.data.data || !Array.isArray(response.data.data)) {
// 处理不同的API响应格式
let apiRules: ApiRule[] = [];
let totalCount = 0;
// 9000端口格式 {code: number, msg: string, data: ApiRule[]}
if (response.data && 'code' in response.data && response.data.data) {
if (Array.isArray(response.data.data)) {
apiRules = response.data.data;
} else {
return { error: '接口返回数据格式不正确', status: 500 };
}
}
// 3000端口格式 ApiRule[]
else if (Array.isArray(response.data)) {
apiRules = response.data;
}
// 不支持的格式
else {
return { error: '接口返回数据格式不正确', status: 500 };
}
// 获取API返回的所有评查点
const apiRules = response.data.data;
// 从响应头中获取总记录数(如果存在)
// 注意:这里假设总记录数会在接口响应的某个位置返回
// 实际使用时,需要根据 PostgREST 的响应格式进行调整
let totalCount = 0;
// 尝试从响应中获取总数
if (response.headers && response.headers['content-range']) {
let rangeHeader = '';
// 安全地检查头信息是否存在
if (response && 'headers' in response && response.headers && typeof response.headers === 'object') {
rangeHeader = (response.headers as Record<string, string>)['content-range'] || '';
}
if (rangeHeader) {
// 例如 Content-Range: 0-9/42 表示总共有 42 条记录
const range = response.headers['content-range'];
const total = range.split('/')[1];
const total = rangeHeader.split('/')[1];
if (total !== '*') { // '*' 表示未知总数
totalCount = parseInt(total, 10);
}
} else {
// 如果没有响应头,则使用当前返回的数据长度作为默认值
// 这种情况下分页可能不准确
totalCount = apiRules.length;
console.warn('未能从响应中获取总记录数,使用当前页数据长度作为默认值');
}
// 打印第一个数据项来查看结构(仅用于调试)
if (apiRules.length > 0) {
console.log('评查点数据示例:', JSON.stringify(apiRules[0], null, 2));
}
// if (apiRules.length > 0) {
// console.log('评查点数据示例:', JSON.stringify(apiRules[0], null, 2));
// }
// 收集所有分组ID (多进行一步查找表的操作)
// 收集所有规则分组ID(实际上是二级分组) (多进行一步查找表的操作)
const groupIds = [...new Set(apiRules.map(rule => rule.evaluation_point_groups_id))];
// 如果有分组ID,查询分组信息
const groupsMap: Record<string, string> = {};
// 如果有分组ID,查询分组信息 - 更新类型定义
const groupsMap: Record<string, {first_name: string; second_name: string}> = {};
if (groupIds.length > 0) {
try {
// 构建查询参数
const groupsParams: PostgrestParams = {
select: 'id,name',
filter: {
'id': `in.(${groupIds.join(',')})`
}
};
// 过滤null和空值
const validGroupIds = groupIds.filter(id => id != null && id.toString().trim() !== '');
// 查询评查点分组表
const groupsResponse = await postgrestGet<{code: number; msg: string; data: {id: number; name: string}[]}>('evaluation_point_groups', groupsParams);
if (groupsResponse.data?.data) {
// 创建ID到名称的映射
groupsResponse.data.data.forEach(group => {
groupsMap[group.id.toString()] = group.name;
});
if (validGroupIds.length > 0) {
// 使用Promise.all并行查询所有分组信息 - 使用正确的函数名
const groupPromises = validGroupIds.map(id =>
postgrestGet<{code: number; msg: string; data: {id: number; pid: number; name: string; first_name: string; second_name: string}[]}>(
`rpc/get_evaluation_point_group_with_pid?input_id=${id}`
)
);
// 打印分组数据(仅用于调试)
console.log('分组数据:', groupsMap);
const groupResponses = await Promise.all(groupPromises);
// console.log("groupResponsesdddddddddddddddddd",groupResponses);
// 处理响应,填充groupsMap
groupResponses.forEach((response) => {
if (!response.error && response.data) {
// 处理不同API响应格式
if (Array.isArray(response.data.data) && response.data.data.length > 0) {
const groupid = response.data.data[1]?.id || "";
groupsMap[groupid] = {
first_name: response.data.data[0]?.name || "",
second_name: response.data.data[1]?.name || ""
};
}else if(Array.isArray(response.data) && response.data.length > 0){
const groupid = response.data[1]?.id || "";
groupsMap[groupid] = {
first_name: response.data[0]?.name || "",
second_name: response.data[1]?.name || ""
};
}
}
});
}
} catch (error) {
console.error('获取分组数据失败:', error);
@@ -265,33 +338,26 @@ export async function getRulesList(params: RulesQueryParams): Promise<{data: Rul
}
}
// 应用本地过滤 - 只在API不支持的情况下使用
let filteredRules = [...apiRules];
// 如果有ruleType过滤(API暂不支持),在本地过滤
if (ruleType) {
// 注意:这是本地过滤,实际情况下最好在API层面支持
filteredRules = filteredRules.filter(() => {
// 实现一个映射逻辑,比如根据其他字段推导ruleType
const derivedType = 'essential'; // 此处应为实际推导逻辑
return derivedType === ruleType;
});
}
const filteredRules = [...apiRules];
// 不再需要本地过滤,因为已经在API层面添加了评查点类型过滤
// 如果进行了本地过滤,则需要调整总记录数
if (ruleType) {
totalCount = filteredRules.length;
}
// if (ruleType) {
// totalCount = filteredRules.length;
// }
// 将API返回的数据映射到前端模型,并附加分组名称
console.log("groupsMap",groupsMap);
const mappedRules = filteredRules.map(apiRule => {
// 创建修改版的apiRule,添加从分组映射获取的名称
const enhancedApiRule = {
...apiRule,
// 从映射中获取分组名称,如果不存在则使用默认值
evaluation_point_groups: {
name: groupsMap[apiRule.evaluation_point_groups_id.toString()] || `${apiRule.evaluation_point_groups_id}`
// name: apiRule.evaluation_point_groups?.name || `${apiRule.evaluation_point_groups_id}`
first_name: groupsMap[apiRule.evaluation_point_groups_id?.toString() || '']?.first_name || `${apiRule.evaluation_point_groups_id}`,
second_name: groupsMap[apiRule.evaluation_point_groups_id?.toString() || '']?.second_name || `${apiRule.evaluation_point_groups_id}`
}
};
@@ -383,7 +449,10 @@ export async function getRule(id: string): Promise<{data: Rule; error?: never} |
if (groupResponse.data?.data && groupResponse.data.data.length > 0) {
// 将分组信息添加到评查点数据中
const group = groupResponse.data.data[0];
apiRule.evaluation_point_groups = { name: group.name };
apiRule.evaluation_point_groups = {
first_name: group.name,
second_name: group.name
};
}
}
} catch (error) {
@@ -535,23 +604,103 @@ export async function updateRule(id: string, ruleData: Partial<Omit<Rule, 'id' |
*/
export async function deleteRule(id: string): Promise<{data: Rule; error?: never} | {data?: never; error: string; status?: number}> {
try {
console.log(`开始删除评查点, ID: ${id}`);
// 使用 PostgREST 语法,通过查询参数指定要删除的行
const postgrestParams: PostgrestParams = {
filter: {
'id': `eq.${id}`
},
headers: {
'Prefer': 'return=representation' // 请求返回被删除的记录
}
};
// 使用postgrestDelete删除评查点
const response = await postgrestDelete<{code: number; msg: string; data: ApiRule}>(`evaluation_points/${id}`);
const response = await postgrestDelete('evaluation_points', postgrestParams);
console.log('删除请求响应:', JSON.stringify(response, null, 2));
// 检查是否有错误响应
if (response.error) {
console.error('删除评查点API返回错误:', response.error);
return { error: response.error, status: response.status };
}
// 确保响应数据存在且符合预期格式
if (!response.data || !response.data.data) {
return { error: '接口返回数据格式不正确', status: 500 };
// 确保响应数据存在
if (!response.data) {
console.error('API响应缺少数据字段');
return { error: 'API返回数据为空', status: 500 };
}
// 将API返回的数据映射到前端模型
const rule = mapApiRuleToFrontendModel(response.data.data);
// 创建一个模拟的成功删除结果
const createMockSuccessRule = (): Rule => {
return {
id: id,
code: '',
name: '',
ruleType: '',
groupId: '',
groupName: '',
priority: 'medium',
description: '',
isActive: false,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
};
return { data: rule };
// 处理9000端口响应格式
if (typeof response.data === 'object' && response.data !== null && 'code' in response.data) {
const apiResponse = response.data as {code: number; msg: string; data?: ApiRule | ApiRule[]};
// 检查响应的code - 如果code为0则表示操作成功
if (apiResponse.code === 0) {
// 如果data不存在或是空数组,返回模拟数据
if (!apiResponse.data || (Array.isArray(apiResponse.data) && apiResponse.data.length === 0)) {
return { data: createMockSuccessRule() };
}
// 处理存在的data
let apiRule: ApiRule;
if (Array.isArray(apiResponse.data)) {
apiRule = apiResponse.data[0];
} else {
apiRule = apiResponse.data;
}
// 将API返回的数据映射到前端模型
const rule = mapApiRuleToFrontendModel(apiRule);
return { data: rule };
}
// 如果code不为0,则返回错误信息
return {
error: apiResponse.msg || '删除失败,服务器返回错误',
status: 500
};
}
// 处理3000端口响应格式 (直接返回数据)
else {
// 处理数组响应
if (Array.isArray(response.data)) {
if (response.data.length === 0) {
// 空数组表示成功但没有返回数据
return { data: createMockSuccessRule() };
} else {
// 返回数组中的第一个元素
const apiRule = response.data[0] as ApiRule;
const rule = mapApiRuleToFrontendModel(apiRule);
return { data: rule };
}
}
// 处理单一对象响应
else {
const apiRule = response.data as ApiRule;
const rule = mapApiRuleToFrontendModel(apiRule);
return { data: rule };
}
}
} catch (error) {
console.error('删除评查点出错:', error);
return {
@@ -600,4 +749,182 @@ export async function duplicateRule(id: string): Promise<{data: Rule; error?: ne
status: 500
};
}
}
/**
* 评查点类型
*/
export interface RuleType {
id: string;
name: string;
description?: string;
isEnabled: boolean;
}
/**
* 规则组
*/
export interface RuleGroup {
id: string;
name: string;
description?: string;
isEnabled: boolean;
typeId?: string; // 关联的评查点类型ID
}
/**
* 获取评查点类型列表
* @returns 评查点类型列表
*/
export async function getRuleTypes(): Promise<{data: RuleType[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
// 构建PostgrestParams参数
const postgrestParams: PostgrestParams = {
select: `
id,
pid,
code,
name,
description,
is_enabled
`,
// 查询父ID为0的类型(顶级类型)
filter: {
'pid': 'eq.0'
}
};
// 发送请求获取评查点类型列表
const response = await postgrestGet<{code: number; msg: string; data: Array<{
id: number;
pid: number;
code: string;
name: string;
description: string;
is_enabled: boolean;
}>;
}>('evaluation_point_groups', postgrestParams);
// 检查是否有错误响应
if (response.error) {
return { error: response.error, status: response.status };
}
if(response.data && 'code' in response.data && response.data.data){
if(Array.isArray(response.data.data) && response.data.data.length > 0){
// 将API返回的数据映射到前端模型
const ruleTypes = response.data.data.map(item => ({
id: item.id.toString(),
pid: item.pid.toString(),
code: item.code,
name: item.name,
description: item.description,
isEnabled: item.is_enabled
}));
return { data: ruleTypes };
}else{
return { error: '9000接口返回数据格式不正确', status: 500 };
}
}else if(Array.isArray(response.data) && response.data.length > 0){
console.log("评查点类型列表",response.data);
const ruleTypes = response.data.map(item => ({
id: item.id.toString(),
pid: item.pid.toString(),
code: item.code,
name: item.name,
description: item.description,
isEnabled: item.is_enabled
}));
return { data: ruleTypes };
}else{
return { error: '3000接口返回数据格式不正确', status: 500 };
}
} catch (error) {
console.error('获取评查点类型出错:', error);
return {
error: error instanceof Error ? error.message : '获取评查点类型失败',
status: 500
};
}
}
/**
* 根据评查点类型ID获取规则组列表
* @param typeId 评查点类型ID
* @returns 规则组列表
*/
export async function getRuleGroupsByType(typeId: string): Promise<{data: RuleGroup[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
// 如果typeId为空或为"全部",则返回空数组
if (!typeId || typeId === 'all') {
return { data: [] };
}
// 构建PostgrestParams参数
const postgrestParams: PostgrestParams = {
select: `
id,
pid,
code,
name,
description,
is_enabled
`,
// 查询指定类型ID的规则组
filter: {
'pid': `eq.${typeId}`
}
};
// 发送请求获取规则组列表
const response = await postgrestGet<{code: number; msg: string; data: Array<{
id: number;
pid: number;
code: string;
name: string;
description: string;
is_enabled: boolean;
}>;
}>('evaluation_point_groups', postgrestParams);
// 检查是否有错误响应
if (response.error) {
return { error: response.error, status: response.status };
}
// 确保响应数据存在且符合预期格式
if(response.data && 'code' in response.data && response.data.data){
if(Array.isArray(response.data.data) && response.data.data.length > 0){
// 将API返回的数据映射到前端模型
const ruleGroups = response.data.data.map(item => ({
id: item.id.toString(),
name: item.name,
description: item.description,
isEnabled: item.is_enabled,
code: item.code
}));
return { data: ruleGroups };
}else{
return { error: '9000接口返回数据格式不正确', status: 500 };
}
}else if(Array.isArray(response.data) && response.data.length > 0){
console.log("评查点类型列表",response.data);
const ruleGroups = response.data.map(item => ({
id: item.id.toString(),
name: item.name,
description: item.description,
isEnabled: item.is_enabled,
code: item.code
}));
return { data: ruleGroups };
}else{
return { error: '3000接口返回数据格式不正确', status: 500 };
}
} catch (error) {
console.error('获取规则组出错:', error);
return {
error: error instanceof Error ? error.message : '获取规则组失败',
status: 500
};
}
}