204 lines
5.5 KiB
TypeScript
204 lines
5.5 KiB
TypeScript
import { postgrestGet, postgrestPost, postgrestPut, postgrestDelete } from '../postgrest-client';
|
|
|
|
/**
|
|
* 评查点列表查询参数
|
|
*/
|
|
export interface RulesQueryParams {
|
|
page?: number;
|
|
pageSize?: number;
|
|
ruleType?: string;
|
|
groupId?: string;
|
|
isActive?: boolean;
|
|
keyword?: string;
|
|
}
|
|
|
|
/**
|
|
* 评查点列表响应数据
|
|
*/
|
|
export interface RulesListResponse {
|
|
rules: Rule[];
|
|
totalCount: number;
|
|
}
|
|
|
|
/**
|
|
* 评查点详情
|
|
*/
|
|
export interface Rule {
|
|
id: string;
|
|
code: string;
|
|
name: string;
|
|
ruleType: string;
|
|
groupId: string;
|
|
groupName: string;
|
|
priority: string;
|
|
description: string;
|
|
isActive: boolean;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
/**
|
|
* 评查点分组
|
|
*/
|
|
export interface RuleGroup {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
isActive: boolean;
|
|
}
|
|
|
|
/**
|
|
* 获取评查点列表
|
|
* @param params 查询参数
|
|
* @returns 评查点列表、总数和评查点组
|
|
*/
|
|
export async function getRulesList(params: RulesQueryParams): Promise<{data: RulesListResponse; error?: never} | {data?: never; error: string; status?: number}> {
|
|
try {
|
|
// 构建 PostgREST 查询参数
|
|
const { page = 1, pageSize = 10, ...filters } = params;
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
// 构建过滤条件
|
|
const filterArray: Record<string, unknown> = {};
|
|
|
|
if (filters.ruleType) {
|
|
filterArray['rule_type'] = `eq.${filters.ruleType}`;
|
|
}
|
|
|
|
if (filters.groupId) {
|
|
filterArray['group_id'] = `eq.${filters.groupId}`;
|
|
}
|
|
|
|
if (filters.isActive !== undefined) {
|
|
filterArray['is_active'] = `eq.${filters.isActive}`;
|
|
}
|
|
|
|
if (filters.keyword) {
|
|
// 关键字搜索
|
|
filterArray['or'] = `name.ilike.*${filters.keyword}*,code.ilike.*${filters.keyword}*`;
|
|
}
|
|
|
|
// 执行多个 API 调用(获取评查点列表、总数和评查点组)
|
|
const [rulesResponse, countResponse] = await Promise.all([
|
|
postgrestGet<Rule[]>('/evaluation_points', {
|
|
select: '*',
|
|
order: 'created_at.desc',
|
|
limit: pageSize,
|
|
offset,
|
|
filter: filterArray
|
|
}),
|
|
|
|
postgrestGet<[{count: number}]>('/evaluation_points/count', {
|
|
filter: filterArray
|
|
}),
|
|
|
|
]);
|
|
|
|
// 处理错误情况
|
|
if (rulesResponse.error) {
|
|
return { error: `获取评查点列表失败: ${rulesResponse.error}`, status: 500 };
|
|
}
|
|
|
|
if (countResponse.error) {
|
|
return { error: `获取评查点总数失败: ${countResponse.error}`, status: 500 };
|
|
}
|
|
|
|
|
|
// 确保数据不为 undefined,提供默认值
|
|
const rules = rulesResponse.data || [];
|
|
const totalCount = countResponse.data && countResponse.data[0] ? countResponse.data[0].count : 0;
|
|
|
|
// 成功返回数据
|
|
return {
|
|
data: {
|
|
rules,
|
|
totalCount,
|
|
}
|
|
};
|
|
|
|
} catch (error) {
|
|
console.error('获取评查点列表出错:', error);
|
|
return {
|
|
error: error instanceof Error ? error.message : '获取评查点列表失败',
|
|
status: 500
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取单个评查点详情
|
|
* @param id 评查点ID
|
|
* @returns 评查点详情
|
|
*/
|
|
export async function getRule(id: string): Promise<{data: Rule; error?: never} | {data?: never; error: string; status?: number}> {
|
|
return postgrestGet<Rule>(`/evaluation_points/${id}`);
|
|
}
|
|
|
|
/**
|
|
* 创建新评查点
|
|
* @param ruleData 评查点数据
|
|
* @returns 创建的评查点
|
|
*/
|
|
export async function createRule(ruleData: Omit<Rule, 'id' | 'createdAt' | 'updatedAt'>): Promise<{data: Rule; error?: never} | {data?: never; error: string; status?: number}> {
|
|
return postgrestPost<Rule, Omit<Rule, 'id' | 'createdAt' | 'updatedAt'>>('/evaluation_points', ruleData);
|
|
}
|
|
|
|
/**
|
|
* 更新评查点
|
|
* @param id 评查点ID
|
|
* @param ruleData 评查点数据
|
|
* @returns 更新后的评查点
|
|
*/
|
|
export async function updateRule(id: string, ruleData: Partial<Omit<Rule, 'id' | 'createdAt' | 'updatedAt'>>): Promise<{data: Rule; error?: never} | {data?: never; error: string; status?: number}> {
|
|
return postgrestPut<Rule, Partial<Omit<Rule, 'id' | 'createdAt' | 'updatedAt'>>>(`/evaluation_points/${id}`, ruleData);
|
|
}
|
|
|
|
/**
|
|
* 删除评查点
|
|
* @param id 评查点ID
|
|
* @returns 删除结果
|
|
*/
|
|
export async function deleteRule(id: string): Promise<{data: Rule; error?: never} | {data?: never; error: string; status?: number}> {
|
|
return postgrestDelete<Rule>(`/evaluation_points/${id}`);
|
|
}
|
|
|
|
/**
|
|
* 复制评查点
|
|
* @param id 评查点ID
|
|
* @returns 新创建的评查点
|
|
*/
|
|
export async function duplicateRule(id: string): Promise<{data: Rule; error?: never} | {data?: never; error: string; status?: number}> {
|
|
try {
|
|
// 1. 获取原评查点详情
|
|
const ruleResponse = await getRule(id);
|
|
|
|
if (ruleResponse.error || !ruleResponse.data) {
|
|
return { error: ruleResponse.error || '获取评查点详情失败', status: 500 };
|
|
}
|
|
|
|
// 2. 准备新评查点数据
|
|
const rule = ruleResponse.data;
|
|
|
|
// 创建新评查点对象
|
|
const newRuleData = {
|
|
code: `${rule.code}-COPY`,
|
|
name: `${rule.name} (复制)`,
|
|
ruleType: rule.ruleType,
|
|
groupId: rule.groupId,
|
|
groupName: rule.groupName,
|
|
priority: rule.priority,
|
|
description: rule.description,
|
|
isActive: rule.isActive
|
|
};
|
|
|
|
// 3. 创建新评查点
|
|
return createRule(newRuleData);
|
|
|
|
} catch (error) {
|
|
console.error('复制评查点出错:', error);
|
|
return {
|
|
error: error instanceof Error ? error.message : '复制评查点失败',
|
|
status: 500
|
|
};
|
|
}
|
|
}
|