feat: contract模块

This commit is contained in:
louis 2024-02-29 16:51:22 +08:00
parent 6650f9f06a
commit 9c8d2e6ca3
5 changed files with 187 additions and 51 deletions

View File

@ -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(':id')
// @ApiOperation({ summary: '获取角色信息' })
// @ApiResult({ type: RoleInfo })
// @Perm(permissions.READ)
// async info(@IdParam() id: number) {
// return this.roleService.info(id)
// }
// @Post()
// @ApiOperation({ summary: '新增角色' })
// @Perm(permissions.CREATE)
// async create(@Body() dto: RoleDto): Promise<void> {
// await this.roleService.create(dto)
// }
// @Put(':id')
// @ApiOperation({ summary: '更新角色' })
// @Perm(permissions.UPDATE)
// async update(
// @IdParam() id: number, @Body() dto: RoleUpdateDto): Promise<void> {
// await this.roleService.update(id, dto)
// await this.menuService.refreshOnlineUserPerms()
// }
// @Delete(':id')
// @ApiOperation({ summary: '删除角色' })
// @Perm(permissions.DELETE)
// async delete(@IdParam() id: number): Promise<void> {
// if (await this.roleService.checkUserByRoleId(id))
// throw new BadRequestException('该角色存在关联用户,无法删除')
// await this.roleService.delete(id)
// await this.menuService.refreshOnlineUserPerms()
// }
@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: ContractDto })
@Perm(permissions.READ)
async info(@IdParam() id: number) {
return this.contractService.info(id);
}
@Post()
@ApiOperation({ summary: '新增合同' })
@Perm(permissions.CREATE)
async create(@Body() dto: ContractDto): Promise<void> {
await this.contractService.create(dto);
}
@Put(':id')
@ApiOperation({ summary: '更新合同' })
@Perm(permissions.UPDATE)
async update(@IdParam() id: number, @Body() dto: ContractUpdateDto): Promise<void> {
await this.contractService.update(id, dto);
}
@Delete(':id')
@ApiOperation({ summary: '删除合同' })
@Perm(permissions.DELETE)
async delete(@IdParam() id: number): Promise<void> {
await this.contractService.delete(id);
}
}

View File

@ -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<ContractDto>,
PartialType(ContractDto)
) {}

View File

@ -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<ContractEntity>
) {}
/**
*
*/
async findAll({
page,
pageSize,
contractNumber,
title,
type,
status
}: ContractQueryDto): Promise<Pagination<ContractEntity>> {
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<ContractEntity>(queryBuilder, {
page,
pageSize
});
}
/**
*
*/
async create(dto: ContractDto): Promise<void> {
await this.contractRepository.insert(dto);
}
/**
*
*/
async update(id: number, dto: Partial<ContractDto>): Promise<void> {
await this.contractRepository.update(id, dto);
}
/**
*
*/
async delete(id: number): Promise<void> {
await this.contractRepository.softDelete(id);
}
/**
*
*/
async info(id: number) {
const info = await this.contractRepository
.createQueryBuilder('contract')
.where({
id
})
.getOne();
return info;
}
}

View File

@ -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<DictItemEntity[]> {
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<DictItemEntity> {
return this.dictItemService.findOne(id);
}
@Post(':id')
@ApiOperation({ summary: '更新字典项' })
@Perm(permissions.UPDATE)

View File

@ -42,6 +42,11 @@ export class DictItemService {
return paginate(queryBuilder, { page, pageSize });
}
/** 一次性获取所有的字典项 */
async getAllByType(typeId: number) {
return this.dictItemRepository.find({ where: { type: { id: typeId } } });
}
/**
*
*/