feat: 实现出库导入、区块链存证、查询日志、看板与系统设置
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
import { Queue } from 'bullmq';
|
||||
import { Response } from 'express';
|
||||
import * as ExcelJS from 'exceljs';
|
||||
import { ErrorCode, JobType, OUTBOUND_IMPORT_COLUMNS, QUEUE_NAMES } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { JobsService } from '../jobs/jobs.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { OutboundDto } from './dto/outbound.dto';
|
||||
|
||||
@Injectable()
|
||||
export class OutboundService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly storage: StorageService,
|
||||
private readonly jobs: JobsService,
|
||||
@InjectQueue(QUEUE_NAMES.IMPORT_OUTBOUND) private readonly queue: Queue,
|
||||
) {}
|
||||
|
||||
async updateSingle(traceCodeId: bigint, dto: OutboundDto) {
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id: traceCodeId } });
|
||||
if (!code) throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '追溯码不存在', 404);
|
||||
return this.prisma.traceCode.update({
|
||||
where: { id: traceCodeId },
|
||||
data: {
|
||||
sku: dto.sku,
|
||||
archiveNo: dto.archiveNo,
|
||||
outTime: dto.outTime ? new Date(dto.outTime) : undefined,
|
||||
outImage: dto.outImageKey,
|
||||
remark: dto.remark,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async generateTemplate(res: Response): Promise<void> {
|
||||
const wb = new ExcelJS.Workbook();
|
||||
const ws = wb.addWorksheet('导入模板');
|
||||
ws.columns = OUTBOUND_IMPORT_COLUMNS.map((c) => ({ header: c.header, key: c.key, width: 22 }));
|
||||
ws.getRow(1).font = { bold: true };
|
||||
const example: Record<string, string> = {};
|
||||
OUTBOUND_IMPORT_COLUMNS.forEach((c) => (example[c.key] = c.example));
|
||||
ws.addRow(example);
|
||||
|
||||
const note = wb.addWorksheet('字段说明');
|
||||
note.columns = [
|
||||
{ header: '列名', key: 'header', width: 18 },
|
||||
{ header: '是否必填', key: 'required', width: 12 },
|
||||
{ header: '说明', key: 'note', width: 50 },
|
||||
];
|
||||
note.getRow(1).font = { bold: true };
|
||||
OUTBOUND_IMPORT_COLUMNS.forEach((c) =>
|
||||
note.addRow({ header: c.header, required: c.required ? '是' : '否', note: c.note }),
|
||||
);
|
||||
|
||||
res.setHeader(
|
||||
'Content-Type',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
);
|
||||
res.setHeader('Content-Disposition', 'attachment; filename="outbound-import-template.xlsx"');
|
||||
await wb.xlsx.write(res);
|
||||
res.end();
|
||||
}
|
||||
|
||||
async startImport(file: Express.Multer.File, userId: number) {
|
||||
if (!file) throw new BusinessException(ErrorCode.BAD_REQUEST, '请上传导入文件');
|
||||
const saved = await this.storage.save(file.buffer, file.originalname || 'import.xlsx', file.mimetype);
|
||||
const job = await this.jobs.create(JobType.IMPORT_OUTBOUND, 0, userId, { fileKey: saved.key });
|
||||
await this.queue.add('import', { jobId: job.id, fileKey: saved.key });
|
||||
return { jobId: job.id };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user