From 9c8d2e6ca31861808b2546bf4a2af362b872390d Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Thu, 29 Feb 2024 16:51:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20contract=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/contract/contract.controller.ts | 88 ++++++++++--------- src/modules/contract/contract.dto.ts | 56 ++++++++++++ src/modules/contract/contract.service.ts | 71 ++++++++++++++- .../system/dict-item/dict-item.controller.ts | 18 ++-- .../system/dict-item/dict-item.service.ts | 5 ++ 5 files changed, 187 insertions(+), 51 deletions(-) create mode 100644 src/modules/contract/contract.dto.ts diff --git a/src/modules/contract/contract.controller.ts b/src/modules/contract/contract.controller.ts index 5755395..5bdc005 100644 --- a/src/modules/contract/contract.controller.ts +++ b/src/modules/contract/contract.controller.ts @@ -1,9 +1,21 @@ -import { Controller, Get } from '@nestjs/common'; +import { + Body, + Controller, + Get, + Query, + Put, + Delete, + Post, + BadRequestException +} from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; -import { definePermission } from '../auth/decorators/permission.decorator'; +import { Perm, definePermission } from '../auth/decorators/permission.decorator'; import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'; import { ContractService } from './contract.service'; import { ApiResult } from '~/common/decorators/api-result.decorator'; +import { ContractEntity } from './contract.entity'; +import { ContractDto, ContractQueryDto, ContractUpdateDto } from './contract.dto'; +import { IdParam } from '~/common/decorators/id-param.decorator'; export const permissions = definePermission('app:contract', { LIST: 'list', CREATE: 'create', @@ -16,48 +28,42 @@ export const permissions = definePermission('app:contract', { @ApiSecurityAuth() @Controller('contract') export class ContractController { - constructor(private menuService: ContractService) {} + constructor(private contractService: ContractService) {} - // @Get() - // @ApiOperation({ summary: '获取合同列表' }) - // @ApiResult({ type: [RoleEntity], isPage: true }) - // @Perm(permissions.LIST) - // async list(@Query() dto: RoleQueryDto) { - // return this.roleService.findAll(dto) - // } + @Get() + @ApiOperation({ summary: '获取合同列表' }) + @ApiResult({ type: [ContractEntity], isPage: true }) + @Perm(permissions.LIST) + async list(@Query() dto: ContractQueryDto) { + return this.contractService.findAll(dto); + } - // @Get(':id') - // @ApiOperation({ summary: '获取角色信息' }) - // @ApiResult({ type: RoleInfo }) - // @Perm(permissions.READ) - // async info(@IdParam() id: number) { - // return this.roleService.info(id) - // } + @Get(':id') + @ApiOperation({ summary: '获取合同信息' }) + @ApiResult({ type: ContractDto }) + @Perm(permissions.READ) + async info(@IdParam() id: number) { + return this.contractService.info(id); + } - // @Post() - // @ApiOperation({ summary: '新增角色' }) - // @Perm(permissions.CREATE) - // async create(@Body() dto: RoleDto): Promise { - // await this.roleService.create(dto) - // } + @Post() + @ApiOperation({ summary: '新增合同' }) + @Perm(permissions.CREATE) + async create(@Body() dto: ContractDto): Promise { + await this.contractService.create(dto); + } - // @Put(':id') - // @ApiOperation({ summary: '更新角色' }) - // @Perm(permissions.UPDATE) - // async update( - // @IdParam() id: number, @Body() dto: RoleUpdateDto): Promise { - // await this.roleService.update(id, dto) - // await this.menuService.refreshOnlineUserPerms() - // } + @Put(':id') + @ApiOperation({ summary: '更新合同' }) + @Perm(permissions.UPDATE) + async update(@IdParam() id: number, @Body() dto: ContractUpdateDto): Promise { + await this.contractService.update(id, dto); + } - // @Delete(':id') - // @ApiOperation({ summary: '删除角色' }) - // @Perm(permissions.DELETE) - // async delete(@IdParam() id: number): Promise { - // if (await this.roleService.checkUserByRoleId(id)) - // throw new BadRequestException('该角色存在关联用户,无法删除') - - // await this.roleService.delete(id) - // await this.menuService.refreshOnlineUserPerms() - // } + @Delete(':id') + @ApiOperation({ summary: '删除合同' }) + @Perm(permissions.DELETE) + async delete(@IdParam() id: number): Promise { + await this.contractService.delete(id); + } } diff --git a/src/modules/contract/contract.dto.ts b/src/modules/contract/contract.dto.ts new file mode 100644 index 0000000..057ea88 --- /dev/null +++ b/src/modules/contract/contract.dto.ts @@ -0,0 +1,56 @@ +import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger'; +import { + IsArray, + IsDate, + IsDateString, + IsIn, + IsInt, + IsNumber, + IsOptional, + IsString, + Matches, + MinLength, + +} from 'class-validator'; +import { PagerDto } from '~/common/dto/pager.dto'; + +export class ContractDto { + @ApiProperty({ description: '合同编号' }) + @Matches(/^[a-z0-9A-Z]+$/, { message: '合同编号只能包含字母和数字' }) + @IsString() + contractNumber: string; + + @ApiProperty({ description: '合同标题' }) + @IsString() + title: string; + + @ApiProperty({ description: '合同类型' }) + @IsNumber() + type: number; + + @ApiProperty({ description: '甲方' }) + @IsString() + partyA: string; + + @ApiProperty({ description: '乙方' }) + @IsString() + partyB: string; + + @ApiProperty({ description: '签订日期' }) + @IsDateString() + signingDate: string; + + @ApiProperty({ description: '交付期限' }) + @IsDateString() + deliveryDeadline: string; + + @ApiProperty({ description: '审核状态(字典)' }) + @IsIn([0, 1, 2]) + status: number; +} + +export class ContractUpdateDto extends PartialType(ContractDto) {} +export class ContractQueryDto extends IntersectionType( + PagerDto, + PartialType(ContractDto) +) {} diff --git a/src/modules/contract/contract.service.ts b/src/modules/contract/contract.service.ts index 87c091d..6eafc10 100644 --- a/src/modules/contract/contract.service.ts +++ b/src/modules/contract/contract.service.ts @@ -1,4 +1,73 @@ import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { ContractEntity } from './contract.entity'; +import { Like, Repository } from 'typeorm'; +import { ContractDto, ContractQueryDto } from './contract.dto'; +import { Pagination } from '~/helper/paginate/pagination'; +import { isNumber } from 'lodash'; +import { paginate } from '~/helper/paginate'; @Injectable() -export class ContractService {} +export class ContractService { + constructor( + @InjectRepository(ContractEntity) + private contractRepository: Repository + ) {} + /** + * 列举所有角色:除去超级管理员 + */ + async findAll({ + page, + pageSize, + contractNumber, + title, + type, + status + }: ContractQueryDto): Promise> { + const queryBuilder = this.contractRepository.createQueryBuilder('contract').where({ + ...(contractNumber ? { contractNumber: Like(`%${contractNumber}%`) } : null), + ...(title ? { title: Like(`%${title}%`) } : null), + ...(isNumber(type) ? { type } : null), + ...(isNumber(status) ? { status } : null) + }); + + return paginate(queryBuilder, { + page, + pageSize + }); + } + + /** + * 新增 + */ + async create(dto: ContractDto): Promise { + await this.contractRepository.insert(dto); + } + + /** + * 更新 + */ + async update(id: number, dto: Partial): Promise { + await this.contractRepository.update(id, dto); + } + + /** + * 删除 + */ + async delete(id: number): Promise { + await this.contractRepository.softDelete(id); + } + + /** + * 获取单个合同信息 + */ + async info(id: number) { + const info = await this.contractRepository + .createQueryBuilder('contract') + .where({ + id + }) + .getOne(); + return info; + } +} diff --git a/src/modules/system/dict-item/dict-item.controller.ts b/src/modules/system/dict-item/dict-item.controller.ts index 4687594..ece718c 100644 --- a/src/modules/system/dict-item/dict-item.controller.ts +++ b/src/modules/system/dict-item/dict-item.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Delete, Get, Post, Query } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { ApiResult } from '~/common/decorators/api-result.decorator'; @@ -34,6 +34,14 @@ export class DictItemController { return this.dictItemService.page(dto); } + @Get('all/:typeId') + @ApiOperation({ summary: '一次性通过字典类型获取所有所属的字典项(不分页)' }) + @ApiResult({ type: [DictItemEntity] }) + @Perm(permissions.LIST) + async getAll(@Param('typeId') typeId: number): Promise { + return this.dictItemService.getAllByType(typeId); + } + @Post() @ApiOperation({ summary: '新增字典项' }) @Perm(permissions.CREATE) @@ -43,14 +51,6 @@ export class DictItemController { await this.dictItemService.create(dto); } - @Get(':id') - @ApiOperation({ summary: '查询字典项信息' }) - @ApiResult({ type: DictItemEntity }) - @Perm(permissions.READ) - async info(@IdParam() id: number): Promise { - return this.dictItemService.findOne(id); - } - @Post(':id') @ApiOperation({ summary: '更新字典项' }) @Perm(permissions.UPDATE) diff --git a/src/modules/system/dict-item/dict-item.service.ts b/src/modules/system/dict-item/dict-item.service.ts index a1f5cac..2ad8221 100644 --- a/src/modules/system/dict-item/dict-item.service.ts +++ b/src/modules/system/dict-item/dict-item.service.ts @@ -42,6 +42,11 @@ export class DictItemService { return paginate(queryBuilder, { page, pageSize }); } + /** 一次性获取所有的字典项 */ + async getAllByType(typeId: number) { + return this.dictItemRepository.find({ where: { type: { id: typeId } } }); + } + /** * 获取参数总数 */