feat: 开发broker相关代码,开发全局代码
This commit is contained in:
256
apps/api/src/modules/broker/broker.service.ts
Normal file
256
apps/api/src/modules/broker/broker.service.ts
Normal file
@@ -0,0 +1,256 @@
|
||||
import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
ConflictException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, FindOptionsWhere } from 'typeorm';
|
||||
import { Broker } from './broker.entity';
|
||||
import { CreateBrokerDto } from './dto/create-broker.dto';
|
||||
import { UpdateBrokerDto } from './dto/update-broker.dto';
|
||||
import { QueryBrokerDto } from './dto/query-broker.dto';
|
||||
import { BatchCreateBrokerDto } from './dto/batch-create-broker.dto';
|
||||
|
||||
@Injectable()
|
||||
export class BrokerService {
|
||||
constructor(
|
||||
@InjectRepository(Broker)
|
||||
private readonly brokerRepository: Repository<Broker>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 单独创建 broker
|
||||
*/
|
||||
async create(createBrokerDto: CreateBrokerDto): Promise<Broker> {
|
||||
// 检查同一地区的 broker_code 是否已存在
|
||||
const existingByCode = await this.brokerRepository.findOne({
|
||||
where: {
|
||||
brokerCode: createBrokerDto.brokerCode,
|
||||
region: createBrokerDto.region,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingByCode) {
|
||||
throw new ConflictException(
|
||||
`Broker with code "${createBrokerDto.brokerCode}" already exists in region "${createBrokerDto.region}"`,
|
||||
);
|
||||
}
|
||||
|
||||
// 检查同一地区的 broker_name 是否已存在
|
||||
const existingByName = await this.brokerRepository.findOne({
|
||||
where: {
|
||||
brokerName: createBrokerDto.brokerName,
|
||||
region: createBrokerDto.region,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingByName) {
|
||||
throw new ConflictException(
|
||||
`Broker with name "${createBrokerDto.brokerName}" already exists in region "${createBrokerDto.region}"`,
|
||||
);
|
||||
}
|
||||
|
||||
const broker = this.brokerRepository.create({
|
||||
...createBrokerDto,
|
||||
sortOrder: createBrokerDto.sortOrder ?? 0,
|
||||
isActive: createBrokerDto.isActive ?? true,
|
||||
});
|
||||
|
||||
return this.brokerRepository.save(broker);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量创建 broker
|
||||
*/
|
||||
async batchCreate(
|
||||
batchCreateBrokerDto: BatchCreateBrokerDto,
|
||||
): Promise<Broker[]> {
|
||||
const brokers = batchCreateBrokerDto.brokers.map((dto) =>
|
||||
this.brokerRepository.create({
|
||||
...dto,
|
||||
sortOrder: dto.sortOrder ?? 0,
|
||||
isActive: dto.isActive ?? true,
|
||||
}),
|
||||
);
|
||||
|
||||
// 检查是否有重复的 broker_code + region 组合
|
||||
const codeRegionPairs = brokers.map((b) => ({
|
||||
brokerCode: b.brokerCode,
|
||||
region: b.region,
|
||||
}));
|
||||
|
||||
const existingBrokers = await this.brokerRepository.find({
|
||||
where: codeRegionPairs.map((pair) => ({
|
||||
brokerCode: pair.brokerCode,
|
||||
region: pair.region,
|
||||
})),
|
||||
});
|
||||
|
||||
if (existingBrokers.length > 0) {
|
||||
const conflicts = existingBrokers.map(
|
||||
(b) => `${b.brokerCode} in ${b.region}`,
|
||||
);
|
||||
throw new ConflictException(
|
||||
`The following brokers already exist: ${conflicts.join(', ')}`,
|
||||
);
|
||||
}
|
||||
|
||||
// 检查批量数据内部是否有重复
|
||||
const uniquePairs = new Set(
|
||||
codeRegionPairs.map((p) => `${p.brokerCode}-${p.region}`),
|
||||
);
|
||||
if (uniquePairs.size !== codeRegionPairs.length) {
|
||||
throw new ConflictException(
|
||||
'Duplicate broker_code and region combinations in batch data',
|
||||
);
|
||||
}
|
||||
|
||||
return this.brokerRepository.save(brokers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 broker(支持多种查询条件)
|
||||
*/
|
||||
async findAll(queryDto: QueryBrokerDto): Promise<Broker[]> {
|
||||
const where: FindOptionsWhere<Broker> = {};
|
||||
|
||||
if (queryDto.brokerId) {
|
||||
where.brokerId = queryDto.brokerId;
|
||||
}
|
||||
|
||||
if (queryDto.brokerCode) {
|
||||
where.brokerCode = queryDto.brokerCode;
|
||||
}
|
||||
|
||||
if (queryDto.brokerName) {
|
||||
where.brokerName = queryDto.brokerName;
|
||||
}
|
||||
|
||||
if (queryDto.region) {
|
||||
where.region = queryDto.region;
|
||||
}
|
||||
|
||||
if (queryDto.isActive !== undefined) {
|
||||
where.isActive = queryDto.isActive;
|
||||
}
|
||||
|
||||
return this.brokerRepository.find({
|
||||
where,
|
||||
order: {
|
||||
sortOrder: 'ASC',
|
||||
brokerId: 'ASC',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询单个 broker
|
||||
*/
|
||||
async findOne(id: number): Promise<Broker> {
|
||||
const broker = await this.brokerRepository.findOne({
|
||||
where: { brokerId: id },
|
||||
});
|
||||
|
||||
if (!broker) {
|
||||
throw new NotFoundException(`Broker with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return broker;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据条件查询单个 broker(返回第一个匹配的)
|
||||
*/
|
||||
async findOneByCondition(queryDto: QueryBrokerDto): Promise<Broker> {
|
||||
const where: FindOptionsWhere<Broker> = {};
|
||||
|
||||
if (queryDto.brokerId) {
|
||||
where.brokerId = queryDto.brokerId;
|
||||
}
|
||||
|
||||
if (queryDto.brokerCode) {
|
||||
where.brokerCode = queryDto.brokerCode;
|
||||
}
|
||||
|
||||
if (queryDto.brokerName) {
|
||||
where.brokerName = queryDto.brokerName;
|
||||
}
|
||||
|
||||
if (queryDto.region) {
|
||||
where.region = queryDto.region;
|
||||
}
|
||||
|
||||
if (queryDto.isActive !== undefined) {
|
||||
where.isActive = queryDto.isActive;
|
||||
}
|
||||
|
||||
const broker = await this.brokerRepository.findOne({ where });
|
||||
|
||||
if (!broker) {
|
||||
throw new NotFoundException(
|
||||
'Broker not found with the given conditions',
|
||||
);
|
||||
}
|
||||
|
||||
return broker;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新 broker
|
||||
*/
|
||||
async update(
|
||||
id: number,
|
||||
updateBrokerDto: UpdateBrokerDto,
|
||||
): Promise<Broker> {
|
||||
const broker = await this.findOne(id);
|
||||
|
||||
// 如果更新 broker_code 或 region,检查是否冲突
|
||||
if ('brokerCode' in updateBrokerDto || 'region' in updateBrokerDto) {
|
||||
const newCode = updateBrokerDto.brokerCode ?? broker.brokerCode;
|
||||
const newRegion = updateBrokerDto.region ?? broker.region;
|
||||
|
||||
const existing = await this.brokerRepository.findOne({
|
||||
where: {
|
||||
brokerCode: newCode,
|
||||
region: newRegion,
|
||||
},
|
||||
});
|
||||
|
||||
if (existing && existing.brokerId !== id) {
|
||||
throw new ConflictException(
|
||||
`Broker with code "${newCode}" already exists in region "${newRegion}"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果更新 broker_name 或 region,检查是否冲突
|
||||
if ('brokerName' in updateBrokerDto || 'region' in updateBrokerDto) {
|
||||
const newName = updateBrokerDto.brokerName ?? broker.brokerName;
|
||||
const newRegion = updateBrokerDto.region ?? broker.region;
|
||||
|
||||
const existing = await this.brokerRepository.findOne({
|
||||
where: {
|
||||
brokerName: newName,
|
||||
region: newRegion,
|
||||
},
|
||||
});
|
||||
|
||||
if (existing && existing.brokerId !== id) {
|
||||
throw new ConflictException(
|
||||
`Broker with name "${newName}" already exists in region "${newRegion}"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(broker, updateBrokerDto);
|
||||
return this.brokerRepository.save(broker);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 broker
|
||||
*/
|
||||
async remove(id: number): Promise<void> {
|
||||
const broker = await this.findOne(id);
|
||||
await this.brokerRepository.remove(broker);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user