API/src/dto/batch.dto.ts

210 lines
6.0 KiB
TypeScript

import { ApiProperty } from '@midwayjs/swagger';
import { Rule, RuleType } from '@midwayjs/validate';
/**
* 批量操作错误项
*/
export interface BatchErrorItem {
// 错误项标识(可以是ID、邮箱等)
identifier: string;
// 错误信息
error: string;
}
/**
* 批量操作结果基础接口
*/
export interface BatchOperationResult {
// 总处理数量
total: number;
// 成功处理数量
processed: number;
// 创建数量
created?: number;
// 更新数量
updated?: number;
// 删除数量
deleted?: number;
// 跳过的数量(如数据已存在或无需处理)
skipped?: number;
// 错误列表
errors: BatchErrorItem[];
}
/**
* 同步操作结果接口
*/
export interface SyncOperationResult extends BatchOperationResult {
// 同步成功数量
synced: number;
}
/**
* 批量操作错误项DTO
*/
export class BatchErrorItemDTO {
@ApiProperty({ description: '错误项标识(如ID、邮箱等)', type: String })
@Rule(RuleType.string().required())
identifier: string;
@ApiProperty({ description: '错误信息', type: String })
@Rule(RuleType.string().required())
error: string;
}
/**
* 批量操作结果基础DTO
*/
export class BatchOperationResultDTO {
@ApiProperty({ description: '总处理数量', type: Number })
total: number;
@ApiProperty({ description: '成功处理数量', type: Number })
processed: number;
@ApiProperty({ description: '创建数量', type: Number, required: false })
created?: number;
@ApiProperty({ description: '更新数量', type: Number, required: false })
updated?: number;
@ApiProperty({ description: '删除数量', type: Number, required: false })
deleted?: number;
@ApiProperty({ description: '跳过的数量', type: Number, required: false })
skipped?: number;
@ApiProperty({ description: '错误列表', type: [BatchErrorItemDTO] })
errors: BatchErrorItemDTO[];
}
/**
* 同步操作结果DTO
*/
export class SyncOperationResultDTO extends BatchOperationResultDTO {
@ApiProperty({ description: '同步成功数量', type: Number })
synced: number;
}
/**
* 批量创建DTO
*/
export class BatchCreateDTO<T = any> {
@ApiProperty({ description: '要创建的数据列表', type: Array })
@Rule(RuleType.array().required())
items: T[];
}
/**
* 批量更新DTO
*/
export class BatchUpdateDTO<T = any> {
@ApiProperty({ description: '要更新的数据列表', type: Array })
@Rule(RuleType.array().required())
items: T[];
}
/**
* 批量删除DTO
*/
export class BatchDeleteDTO {
@ApiProperty({ description: '要删除的ID列表', type: [String, Number] })
@Rule(RuleType.array().items(RuleType.alternatives().try(RuleType.string(), RuleType.number())).required())
ids: Array<string | number>;
}
/**
* 批量操作请求DTO(包含增删改)
*/
export class BatchOperationDTO<T = any> {
@ApiProperty({ description: '要创建的数据列表', type: Array, required: false })
@Rule(RuleType.array().optional())
create?: T[];
@ApiProperty({ description: '要更新的数据列表', type: Array, required: false })
@Rule(RuleType.array().optional())
update?: T[];
@ApiProperty({ description: '要删除的ID列表', type: [String, Number], required: false })
@Rule(RuleType.array().items(RuleType.alternatives().try(RuleType.string(), RuleType.number())).optional())
delete?: Array<string | number>;
}
/**
* 分页批量操作DTO
*/
export class PaginatedBatchOperationDTO<T = any> {
@ApiProperty({ description: '页码', type: Number, required: false, default: 1 })
@Rule(RuleType.number().integer().min(1).optional())
page?: number = 1;
@ApiProperty({ description: '每页数量', type: Number, required: false, default: 100 })
@Rule(RuleType.number().integer().min(1).max(1000).optional())
pageSize?: number = 100;
@ApiProperty({ description: '要创建的数据列表', type: Array, required: false })
@Rule(RuleType.array().optional())
create?: T[];
@ApiProperty({ description: '要更新的数据列表', type: Array, required: false })
@Rule(RuleType.array().optional())
update?: T[];
@ApiProperty({ description: '要删除的ID列表', type: [String, Number], required: false })
@Rule(RuleType.array().items(RuleType.alternatives().try(RuleType.string(), RuleType.number())).optional())
delete?: Array<string | number>;
}
/**
* 同步参数DTO
*/
export class SyncParamsDTO {
@ApiProperty({ description: '页码', type: Number, required: false, default: 1 })
@Rule(RuleType.number().integer().min(1).optional())
page?: number = 1;
@ApiProperty({ description: '每页数量', type: Number, required: false, default: 100 })
@Rule(RuleType.number().integer().min(1).max(1000).optional())
pageSize?: number = 100;
@ApiProperty({ description: '开始时间', type: String, required: false })
@Rule(RuleType.string().optional())
startDate?: string;
@ApiProperty({ description: '结束时间', type: String, required: false })
@Rule(RuleType.string().optional())
endDate?: string;
@ApiProperty({ description: '强制同步(忽略缓存)', type: Boolean, required: false, default: false })
@Rule(RuleType.boolean().optional())
force?: boolean = false;
}
/**
* 批量查询DTO
*/
export class BatchQueryDTO {
@ApiProperty({ description: 'ID列表', type: [String, Number] })
@Rule(RuleType.array().items(RuleType.alternatives().try(RuleType.string(), RuleType.number())).required())
ids: Array<string | number>;
@ApiProperty({ description: '包含关联数据', type: Boolean, required: false, default: false })
@Rule(RuleType.boolean().optional())
includeRelations?: boolean = false;
}
/**
* 批量操作结果类(泛型支持)
*/
export class BatchOperationResultDTOGeneric<T> extends BatchOperationResultDTO {
@ApiProperty({ description: '操作成功的数据列表', type: Array })
data?: T[];
}
/**
* 同步操作结果类(泛型支持)
*/
export class SyncOperationResultDTOGeneric<T> extends SyncOperationResultDTO {
@ApiProperty({ description: '同步成功的数据列表', type: Array })
data?: T[];
}