64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import { message } from 'antd';
|
||
|
||
/**
|
||
* 批量操作错误项接口
|
||
*/
|
||
export interface BatchErrorItem {
|
||
identifier: string;
|
||
error: string;
|
||
}
|
||
|
||
/**
|
||
* 批量操作结果接口
|
||
*/
|
||
export interface BatchOperationResult {
|
||
total: number;
|
||
processed: number;
|
||
created?: number;
|
||
updated?: number;
|
||
deleted?: number;
|
||
synced?: number;
|
||
errors?: BatchErrorItem[];
|
||
}
|
||
|
||
/**
|
||
* 显示批量操作结果(导入、删除等)
|
||
* @param result 批量操作结果对象
|
||
* @param operationType 操作类型,用于显示在消息中
|
||
*/
|
||
export function showBatchOperationResult(
|
||
result: BatchOperationResult,
|
||
operationType: string = '操作',
|
||
): string {
|
||
// 从 result.data 中获取实际数据(因为后端返回格式为 { success: true, data: {...} })
|
||
const data = (result as any).data || result;
|
||
const { total, processed, created, updated, deleted, errors } = data;
|
||
|
||
// 构建结果消息
|
||
let messageContent = `${operationType}结果:共 ${total} 条,成功 ${processed} 条`;
|
||
|
||
if (created) {
|
||
messageContent += `,创建 ${created} 条`;
|
||
}
|
||
if (updated) {
|
||
messageContent += `,更新 ${updated} 条`;
|
||
}
|
||
if (deleted) {
|
||
messageContent += `,删除 ${deleted} 条`;
|
||
}
|
||
|
||
// 处理错误情况
|
||
if (errors && errors.length > 0) {
|
||
messageContent += `,失败 ${errors.length} 条`;
|
||
// 显示错误详情
|
||
const errorDetails = errors
|
||
.map((err: BatchErrorItem) => `${err.identifier}: ${err.error}`)
|
||
.join('\n');
|
||
message.warning(messageContent + '\n\n错误详情:\n' + errorDetails);
|
||
} else {
|
||
message.success(messageContent);
|
||
}
|
||
|
||
return messageContent;
|
||
}
|