feat: 追溯码支持批量导出 Excel/CSV(按筛选或所选)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsEnum,
|
||||
IsIn,
|
||||
IsInt,
|
||||
IsOptional,
|
||||
IsString,
|
||||
@@ -112,3 +113,37 @@ export class QueryBatchDto extends PaginationDto {
|
||||
@IsString()
|
||||
keyword?: string;
|
||||
}
|
||||
|
||||
export class ExportTraceCodeDto {
|
||||
@ApiPropertyOptional({ description: '导出格式', enum: ['xlsx', 'csv'] })
|
||||
@IsOptional()
|
||||
@IsIn(['xlsx', 'csv'])
|
||||
format?: 'xlsx' | 'csv';
|
||||
|
||||
@ApiPropertyOptional({ description: '分组 ID' })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
groupId?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '批次 ID' })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
batchId?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '状态', enum: TraceCodeStatus })
|
||||
@IsOptional()
|
||||
@IsEnum(TraceCodeStatus)
|
||||
status?: TraceCodeStatus;
|
||||
|
||||
@ApiPropertyOptional({ description: '关键词' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
keyword?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '指定追溯码 ID(逗号分隔,用于导出所选)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
ids?: string;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
BatchGroupDto,
|
||||
BatchStatusDto,
|
||||
BindArtworkDto,
|
||||
ExportTraceCodeDto,
|
||||
GenerateBatchDto,
|
||||
GroupDto,
|
||||
QueryBatchDto,
|
||||
@@ -95,9 +96,9 @@ export class TraceCodeController {
|
||||
|
||||
@Get('trace-codes/export')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '导出追溯码 Excel' })
|
||||
export(@Query('batchId') batchId: string | undefined, @Res() res: Response) {
|
||||
return this.service.exportExcel(batchId ? Number(batchId) : undefined, res);
|
||||
@ApiOperation({ summary: '批量导出追溯码(Excel / CSV,可按筛选或所选)' })
|
||||
export(@Query() query: ExportTraceCodeDto, @Res() res: Response) {
|
||||
return this.service.exportCodes(query, res);
|
||||
}
|
||||
|
||||
@Public()
|
||||
|
||||
@@ -15,6 +15,7 @@ import { JobsService } from '../jobs/jobs.service';
|
||||
import {
|
||||
BatchGroupDto,
|
||||
BatchStatusDto,
|
||||
ExportTraceCodeDto,
|
||||
GenerateBatchDto,
|
||||
GroupDto,
|
||||
QueryBatchDto,
|
||||
@@ -207,12 +208,32 @@ export class TraceCodeService {
|
||||
});
|
||||
}
|
||||
|
||||
async exportExcel(batchId: number | undefined, res: Response): Promise<void> {
|
||||
async exportCodes(dto: ExportTraceCodeDto, res: Response): Promise<void> {
|
||||
const where: Prisma.TraceCodeWhereInput = {};
|
||||
if (batchId) where.batchId = batchId;
|
||||
if (dto.ids) {
|
||||
const idList = dto.ids
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean)
|
||||
.map((s) => BigInt(s));
|
||||
where.id = { in: idList };
|
||||
} else {
|
||||
if (dto.batchId) where.batchId = dto.batchId;
|
||||
if (dto.groupId) where.groupId = dto.groupId;
|
||||
if (dto.status) where.status = dto.status;
|
||||
if (dto.keyword) {
|
||||
where.OR = [
|
||||
{ shortCode: { contains: dto.keyword } },
|
||||
{ code: { contains: dto.keyword } },
|
||||
{ sku: { contains: dto.keyword } },
|
||||
{ archiveNo: { contains: dto.keyword } },
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const codes = await this.prisma.traceCode.findMany({
|
||||
where,
|
||||
include: { batch: { select: { batchNo: true } } },
|
||||
include: { batch: { select: { batchNo: true } }, group: { select: { name: true } } },
|
||||
orderBy: { id: 'asc' },
|
||||
});
|
||||
|
||||
@@ -220,10 +241,11 @@ export class TraceCodeService {
|
||||
const ws = wb.addWorksheet('追溯码');
|
||||
ws.columns = [
|
||||
{ header: '批次编号', key: 'batchNo', width: 22 },
|
||||
{ header: '分组', key: 'group', width: 14 },
|
||||
{ header: '追溯码', key: 'code', width: 24 },
|
||||
{ header: '随机短码', key: 'shortCode', width: 16 },
|
||||
{ header: '短链 URL', key: 'url', width: 40 },
|
||||
{ header: '二维码数据(DataURL)', key: 'qr', width: 50 },
|
||||
{ header: '短链', key: 'url', width: 40 },
|
||||
{ header: '二维码数据', key: 'qr', width: 50 },
|
||||
{ header: '生成时间', key: 'createdAt', width: 22 },
|
||||
];
|
||||
ws.getRow(1).font = { bold: true };
|
||||
@@ -233,6 +255,7 @@ export class TraceCodeService {
|
||||
const qr = await QRCode.toDataURL(url, { margin: 1, width: 200 });
|
||||
ws.addRow({
|
||||
batchNo: c.batch?.batchNo ?? '',
|
||||
group: c.group?.name ?? '',
|
||||
code: c.code,
|
||||
shortCode: c.shortCode,
|
||||
url,
|
||||
@@ -241,12 +264,23 @@ export class TraceCodeService {
|
||||
});
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
res.setHeader(
|
||||
'Content-Disposition',
|
||||
`attachment; filename="trace-codes-${batchId ?? 'all'}.xlsx"`,
|
||||
);
|
||||
await wb.xlsx.write(res);
|
||||
res.end();
|
||||
const format = dto.format === 'csv' ? 'csv' : 'xlsx';
|
||||
const stamp = dayjs().format('YYYYMMDDHHmmss');
|
||||
if (format === 'csv') {
|
||||
const csvBuf = (await wb.csv.writeBuffer()) as unknown as Uint8Array;
|
||||
// 加 UTF-8 BOM 以便 Excel 正确识别中文
|
||||
const out = Buffer.concat([Buffer.from('\uFEFF', 'utf8'), Buffer.from(csvBuf)]);
|
||||
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="trace-codes-${stamp}.csv"`);
|
||||
res.end(out);
|
||||
} else {
|
||||
res.setHeader(
|
||||
'Content-Type',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
);
|
||||
res.setHeader('Content-Disposition', `attachment; filename="trace-codes-${stamp}.xlsx"`);
|
||||
await wb.xlsx.write(res);
|
||||
res.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user