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 { 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; } }