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

145 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { Injectable } from '@nestjs/common';
2024-03-01 15:23:28 +08:00
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
2024-02-29 16:51:22 +08:00
import { ContractEntity } from './contract.entity';
import { EntityManager, Like, Not, Repository } from 'typeorm';
2024-03-01 15:23:28 +08:00
import { ContractDto, ContractQueryDto, ContractUpdateDto } from './contract.dto';
2024-02-29 16:51:22 +08:00
import { Pagination } from '~/helper/paginate/pagination';
import { isNumber } from 'lodash';
import { paginate } from '~/helper/paginate';
2024-03-01 15:23:28 +08:00
import { Storage } from '../tools/storage/storage.entity';
import { BusinessException } from '~/common/exceptions/biz.exception';
import { ErrorEnum } from '~/constants/error-code.constant';
2024-03-04 16:34:54 +08:00
import { fieldSearch } from '~/shared/database/field-search';
2024-02-28 17:02:46 +08:00
@Injectable()
2024-02-29 16:51:22 +08:00
export class ContractService {
constructor(
2024-03-01 15:23:28 +08:00
@InjectEntityManager() private entityManager: EntityManager,
2024-02-29 16:51:22 +08:00
@InjectRepository(ContractEntity)
2024-03-01 15:23:28 +08:00
private contractRepository: Repository<ContractEntity>,
@InjectRepository(Storage)
private storageRepository: Repository<Storage>
2024-02-29 16:51:22 +08:00
) {}
2024-03-04 16:34:54 +08:00
2024-02-29 16:51:22 +08:00
/**
2024-03-04 16:34:54 +08:00
*
2024-02-29 16:51:22 +08:00
*/
async findAll({
page,
pageSize,
2024-03-04 16:34:54 +08:00
...fields
2024-02-29 16:51:22 +08:00
}: ContractQueryDto): Promise<Pagination<ContractEntity>> {
2024-03-01 15:23:28 +08:00
const queryBuilder = this.contractRepository
.createQueryBuilder('contract')
.leftJoin('contract.files', 'files')
.addSelect(['files.id', 'files.path'])
2024-03-04 16:34:54 +08:00
.where(fieldSearch(fields))
2024-03-01 15:31:33 +08:00
.andWhere('contract.isDelete = 0');
2024-02-29 16:51:22 +08:00
return paginate<ContractEntity>(queryBuilder, {
page,
pageSize
});
}
/**
*
*/
async create({ contractNumber, ...ext }: ContractDto): Promise<void> {
if (await this.checkIsContractNumberExsit(contractNumber)) {
throw new BusinessException(ErrorEnum.CONTRACT_NUMBER_EXIST);
}
await this.contractRepository.insert(
this.contractRepository.create({ contractNumber, ...ext })
);
2024-02-29 16:51:22 +08:00
}
/**
*
*/
async update(
id: number,
{ fileIds, contractNumber, ...ext }: Partial<ContractUpdateDto>
): Promise<void> {
2024-03-01 15:23:28 +08:00
await this.entityManager.transaction(async manager => {
if (contractNumber && (await this.checkIsContractNumberExsit(contractNumber, id))) {
throw new BusinessException(ErrorEnum.CONTRACT_NUMBER_EXIST);
}
2024-03-01 15:23:28 +08:00
await manager.update(ContractEntity, id, {
...ext,
contractNumber
2024-03-01 15:23:28 +08:00
});
2024-03-01 16:54:21 +08:00
if (fileIds?.length) {
const count = await this.storageRepository
.createQueryBuilder('storage')
.where('storage.id in(:fileIds)', { fileIds })
.getCount();
if (count !== fileIds?.length) {
throw new BusinessException(ErrorEnum.STORAGE_NOT_FOUND);
}
// 附件要批量插入
await manager.createQueryBuilder().relation(ContractEntity, 'files').of(id).add(fileIds);
2024-03-01 15:23:28 +08:00
}
});
2024-02-29 16:51:22 +08:00
}
/**
*
* @param contractNumber
*/
async checkIsContractNumberExsit(contractNumber: string, id?: number): Promise<Boolean> {
return !!(await this.contractRepository.findOne({
where: {
contractNumber: contractNumber,
id: Not(id)
}
}));
}
2024-02-29 16:51:22 +08:00
/**
*
*/
async delete(id: number): Promise<void> {
2024-03-01 15:31:33 +08:00
// 合同比较重要,做逻辑删除
await this.contractRepository.update(id, { isDelete: 1 });
2024-02-29 16:51:22 +08:00
}
/**
*
*/
async info(id: number) {
const info = await this.contractRepository
.createQueryBuilder('contract')
.where({
id
})
2024-03-01 15:31:33 +08:00
.andWhere('contract.isDelete = 0')
2024-02-29 16:51:22 +08:00
.getOne();
return info;
}
2024-03-01 15:23:28 +08:00
/**
*
* @param id ID
* @param fileIds ID
*/
async unlinkAttachments(id: number, fileIds: number[]) {
await this.entityManager.transaction(async manager => {
const contract = await this.contractRepository
.createQueryBuilder('contract')
.leftJoinAndSelect('contract.files', 'files')
.where('contract.id = :id', { id })
.getOne();
const linkedFiles = contract.files
.map(item => item.id)
.filter(item => !fileIds.includes(item));
// 附件要批量更新
await manager
.createQueryBuilder()
.relation(ContractEntity, 'files')
.of(id)
.addAndRemove(linkedFiles, contract.files);
});
}
2024-02-29 16:51:22 +08:00
}