localhost_oa_based/src/modules/contract/contract.service.ts

74 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { Injectable } from '@nestjs/common';
2024-02-29 16:51:22 +08:00
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';
2024-02-28 17:02:46 +08:00
@Injectable()
2024-02-29 16:51:22 +08:00
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;
}
}