oa_based/src/modules/contract/contract.service.ts

133 lines
3.9 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';
2024-03-01 15:23:28 +08:00
import { EntityManager, Like, Repository } from 'typeorm';
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-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
) {}
/**
*
*/
async findAll({
page,
pageSize,
contractNumber,
title,
type,
status
}: 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'])
.where({
...(contractNumber ? { contractNumber: Like(`%${contractNumber}%`) } : null),
...(title ? { title: Like(`%${title}%`) } : null),
...(isNumber(type) ? { type } : null),
...(isNumber(status) ? { status } : null)
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(dto: ContractDto): Promise<void> {
await this.contractRepository.insert(dto);
}
/**
*
*/
2024-03-01 15:23:28 +08:00
async update(id: number, { fileIds, ...data }: Partial<ContractUpdateDto>): Promise<void> {
await this.entityManager.transaction(async manager => {
await manager.update(ContractEntity, id, {
...data
});
const contract = await this.contractRepository
.createQueryBuilder('contract')
.leftJoinAndSelect('contract.files', 'files')
.where('contract.id = :id', { id })
.getOne();
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)
.addAndRemove(fileIds, contract.files);
});
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
}