From 46a41328771d3b1b693d911bc22cef3588a54ef0 Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Fri, 8 Mar 2024 13:19:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E4=BB=B6=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=9A=E5=8A=A1=E6=A8=A1=E5=9D=97=E5=92=8C?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E8=AE=B0=E5=BD=95id=E5=86=97=E4=BD=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/adapters/fastify.adapter.ts | 1 + src/main.ts | 1 - src/modules/contract/contract.service.ts | 5 +++-- .../in_out/materials_in_out.dto.ts | 6 ++--- .../in_out/materials_in_out.service.ts | 17 ++++++++++---- src/modules/tools/storage/storage.dto.ts | 12 +++++++++- src/modules/tools/storage/storage.entity.ts | 9 ++++++++ src/modules/tools/storage/storage.service.ts | 4 +++- src/modules/tools/upload/upload.controller.ts | 22 +++++++++---------- src/modules/tools/upload/upload.dto.ts | 2 +- src/modules/tools/upload/upload.service.ts | 11 ++++++++-- 11 files changed, 64 insertions(+), 26 deletions(-) diff --git a/src/common/adapters/fastify.adapter.ts b/src/common/adapters/fastify.adapter.ts index c36f024..65db5c9 100644 --- a/src/common/adapters/fastify.adapter.ts +++ b/src/common/adapters/fastify.adapter.ts @@ -10,6 +10,7 @@ const app: FastifyAdapter = new FastifyAdapter({ export { app as fastifyApp }; app.register(FastifyMultipart, { + attachFieldsToBody:true, limits: { fields: 10, // Max number of non-file fields fileSize: 1024 * 1024 * 6, // limit size 6M diff --git a/src/main.ts b/src/main.ts index 3c6498e..51a2048 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,7 +33,6 @@ async function bootstrap() { // class-validator 的 DTO 类中注入 nest 容器的依赖 (用于自定义验证器) useContainer(app.select(AppModule), { fallbackOnErrors: true }); - app.enableCors({ origin: '*', credentials: true }); app.setGlobalPrefix(globalPrefix); app.useStaticAssets({ root: path.join(__dirname, '..', 'public') }); diff --git a/src/modules/contract/contract.service.ts b/src/modules/contract/contract.service.ts index 587ff1b..ae961dd 100644 --- a/src/modules/contract/contract.service.ts +++ b/src/modules/contract/contract.service.ts @@ -62,11 +62,12 @@ export class ContractService { { fileIds, contractNumber, ...ext }: Partial ): Promise { await this.entityManager.transaction(async manager => { - if (await this.checkIsContractNumberExsit(contractNumber, id)) { + if (contractNumber && (await this.checkIsContractNumberExsit(contractNumber, id))) { throw new BusinessException(ErrorEnum.CONTRACT_NUMBER_EXIST); } await manager.update(ContractEntity, id, { - ...ext + ...ext, + contractNumber }); if (fileIds?.length) { diff --git a/src/modules/materials_inventory/in_out/materials_in_out.dto.ts b/src/modules/materials_inventory/in_out/materials_in_out.dto.ts index ec41f22..0d0fec2 100644 --- a/src/modules/materials_inventory/in_out/materials_in_out.dto.ts +++ b/src/modules/materials_inventory/in_out/materials_in_out.dto.ts @@ -129,9 +129,9 @@ export class MaterialsInOutQueryDto extends PagerDto { remark?: string; @IsOptional() - @IsString() - @ApiProperty({ description: '项目' }) - project?: string; + @IsNumber() + @ApiProperty({ description: '项目Id' }) + projectId?: number; @IsOptional() @IsBoolean() diff --git a/src/modules/materials_inventory/in_out/materials_in_out.service.ts b/src/modules/materials_inventory/in_out/materials_in_out.service.ts index 3c82855..3d2dd77 100644 --- a/src/modules/materials_inventory/in_out/materials_in_out.service.ts +++ b/src/modules/materials_inventory/in_out/materials_in_out.service.ts @@ -35,7 +35,7 @@ export class MaterialsInOutService { page, pageSize, product: productName, - project: projectName, + projectId, isCreateOut, ...ext }: MaterialsInOutQueryDto): Promise> { @@ -46,15 +46,24 @@ export class MaterialsInOutService { .leftJoin('materialsInOut.product', 'product') .leftJoin('product.unit', 'unit') .leftJoin('product.company', 'company') - .addSelect(['files.id','files.path', 'project.name', 'product.name', 'unit.label', 'company.name']) + .addSelect([ + 'files.id', + 'files.path', + 'project.name', + 'product.name', + 'unit.label', + 'company.name' + ]) .where(fieldSearch(ext)) .andWhere('materialsInOut.isDelete = 0') .addOrderBy('materialsInOut.createdAt', 'DESC'); + if (productName) { sqb.andWhere('product.name like :productName', { productName: `%${productName}%` }); } - if (projectName) { - sqb.andWhere('project.name like :projectName', { projectName: `%${projectName}%` }); + + if (projectId) { + sqb.andWhere('project.id = :projectId', { projectId }); } if (isCreateOut) { diff --git a/src/modules/tools/storage/storage.dto.ts b/src/modules/tools/storage/storage.dto.ts index 1dfd010..2ef2924 100644 --- a/src/modules/tools/storage/storage.dto.ts +++ b/src/modules/tools/storage/storage.dto.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; import { Transform } from 'class-transformer'; -import { ArrayNotEmpty, IsArray, IsOptional, IsString } from 'class-validator'; +import { ArrayNotEmpty, IsArray, IsNumber, IsOptional, IsString } from 'class-validator'; import { PagerDto } from '~/common/dto/pager.dto'; @@ -34,6 +34,16 @@ export class StoragePageDto extends PagerDto { @IsOptional() username: string; + @ApiProperty({ description: '文件所属模块' }) + @IsString() + @IsOptional() + businessModule: string; + + @ApiProperty({ description: '文件上传的业务记录ID' }) + @IsNumber() + @IsOptional() + bussinessRecordId: string; + @ApiProperty({ description: '附件' }) @IsOptional() @Transform( diff --git a/src/modules/tools/storage/storage.entity.ts b/src/modules/tools/storage/storage.entity.ts index 588b033..6929c0e 100644 --- a/src/modules/tools/storage/storage.entity.ts +++ b/src/modules/tools/storage/storage.entity.ts @@ -45,6 +45,15 @@ export class Storage extends CommonEntity { @ApiProperty({ description: '用户ID' }) userId: number; + @Column({ nullable: true, name: 'bussiness_module' }) + @ApiProperty({ description: '文件上传的业务模块' }) + bussinessModule: string; + + @Column({ nullable: true, name: 'bussiness_record_id' }) + @ApiProperty({ description: '文件上传的业务记录ID' }) + bussinessRecordId: number; + + @ApiHideProperty() @ManyToMany(() => ContractEntity, contract => contract.files) contracts: Relation; diff --git a/src/modules/tools/storage/storage.service.ts b/src/modules/tools/storage/storage.service.ts index 414af8f..a24c122 100644 --- a/src/modules/tools/storage/storage.service.ts +++ b/src/modules/tools/storage/storage.service.ts @@ -93,7 +93,9 @@ export class StorageService { type: e.storage_type, size: e.storage_size, createdAt: e.storage_created_at, - username: e.user_username + username: e.user_username, + bussinessRecordId:e.storage_bussiness_record_id, + bussinessModule:e.storage_bussiness_module }; }); } diff --git a/src/modules/tools/upload/upload.controller.ts b/src/modules/tools/upload/upload.controller.ts index 31756ab..c01c713 100644 --- a/src/modules/tools/upload/upload.controller.ts +++ b/src/modules/tools/upload/upload.controller.ts @@ -1,4 +1,4 @@ -import { BadRequestException, Controller, Post, Req } from '@nestjs/common'; +import { BadRequestException, Body, Controller, Post, Req, UseInterceptors } from '@nestjs/common'; import { ApiBody, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger'; import { FastifyRequest } from 'fastify'; @@ -27,18 +27,18 @@ export class UploadController { @ApiBody({ type: FileUploadDto }) - async upload(@Req() req: FastifyRequest, @AuthUser() user: IAuthUser) { + async upload(@Req() req: FastifyRequest, @AuthUser() user: IAuthUser, @Body() body: any) { if (!req.isMultipart()) throw new BadRequestException('Request is not multipart'); - - const file = await req.file(); - - // https://github.com/fastify/fastify-multipart - // const parts = req.files() - // for await (const part of parts) - // console.log(part.file) - + const { file } = body; + const bussinessModule = body.bussinessModule?.value; + const bussinessRecordId = Number(body.bussinessRecordId?.value) || null; try { - const savedFile = await this.uploadService.saveFile(file, user.uid); + const savedFile = await this.uploadService.saveFile( + file, + user.uid, + bussinessModule, + bussinessRecordId + ); return { filename: savedFile diff --git a/src/modules/tools/upload/upload.dto.ts b/src/modules/tools/upload/upload.dto.ts index dc28e7c..e25dcdd 100644 --- a/src/modules/tools/upload/upload.dto.ts +++ b/src/modules/tools/upload/upload.dto.ts @@ -1,7 +1,7 @@ import { MultipartFile } from '@fastify/multipart'; import { ApiProperty } from '@nestjs/swagger'; -import { IsDefined } from 'class-validator'; +import { IsDefined, IsNumber, IsOptional, IsString } from 'class-validator'; import { IsFile } from './file.constraint'; diff --git a/src/modules/tools/upload/upload.service.ts b/src/modules/tools/upload/upload.service.ts index 05b2a31..8186817 100644 --- a/src/modules/tools/upload/upload.service.ts +++ b/src/modules/tools/upload/upload.service.ts @@ -25,7 +25,12 @@ export class UploadService { /** * 保存文件上传记录 */ - async saveFile(file: MultipartFile, userId: number): Promise<{ id: number; path: string }> { + async saveFile( + file: MultipartFile, + userId: number, + bussinessModule?: string, + bussinessRecordId?: number + ): Promise<{ id: number; path: string }> { if (isNil(file)) throw new NotFoundException('Have not any file to upload!'); const fileName = file.filename; @@ -44,7 +49,9 @@ export class UploadService { path, type, size, - userId + userId, + bussinessModule, + bussinessRecordId }); return { path, id: storage.id };