| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- import { GenericCrudService } from '@d8d/shared-crud';
- import { DataSource, Repository, DataSourceOptions, In, Not } from 'typeorm';
- import { EmploymentOrder } from '../entities/employment-order.entity';
- import { OrderPerson } from '../entities/order-person.entity';
- import { OrderPersonAsset, AssetType, AssetFileType } from '../entities/order-person-asset.entity';
- import { FileService, File } from '@d8d/file-module';
- import { OrderStatus, WorkStatus } from '@d8d/allin-enums';
- export class OrderService extends GenericCrudService<EmploymentOrder> {
- private readonly orderPersonRepository: Repository<OrderPerson>;
- private readonly orderPersonAssetRepository: Repository<OrderPersonAsset>;
- private readonly fileRepository: Repository<File>;
- private fileService: FileService;
- constructor(dataSource: DataSource) {
- super(dataSource, EmploymentOrder);
- this.orderPersonRepository = dataSource.getRepository(OrderPerson);
- this.orderPersonAssetRepository = dataSource.getRepository(OrderPersonAsset);
- this.fileRepository = dataSource.getRepository(File);
- this.fileService = new FileService(dataSource);
- }
- /**
- * 创建订单 - 覆盖父类方法,添加验证和业务逻辑
- */
- async create(data: Partial<EmploymentOrder>, userId?: string | number): Promise<EmploymentOrder> {
- // 验证枚举值
- if (data.orderStatus && !Object.values(OrderStatus).includes(data.orderStatus)) {
- throw new Error('订单状态无效');
- }
- if (data.workStatus && !Object.values(WorkStatus).includes(data.workStatus)) {
- throw new Error('工作状态无效');
- }
- // 设置默认值
- const orderData = {
- ...data,
- orderStatus: data.orderStatus || OrderStatus.DRAFT,
- workStatus: data.workStatus || WorkStatus.NOT_WORKING,
- };
- return super.create(orderData, userId);
- }
- /**
- * 更新订单 - 覆盖父类方法,添加验证
- */
- async update(id: number, data: Partial<EmploymentOrder>, userId?: string | number): Promise<EmploymentOrder | null> {
- // 验证枚举值
- if (data.orderStatus && !Object.values(OrderStatus).includes(data.orderStatus)) {
- throw new Error('订单状态无效');
- }
- if (data.workStatus && !Object.values(WorkStatus).includes(data.workStatus)) {
- throw new Error('工作状态无效');
- }
- return super.update(id, data, userId);
- }
- /**
- * 分页查询订单 - 自定义方法,返回源服务的格式
- */
- async findAll(query: {
- orderName?: string;
- platformId?: number;
- companyId?: number;
- channelId?: number;
- orderStatus?: OrderStatus;
- page?: number;
- limit?: number;
- }): Promise<{ data: any[]; total: number }> {
- const {
- orderName,
- platformId,
- companyId,
- channelId,
- orderStatus,
- page = 1,
- limit = 10
- } = query;
- const queryBuilder = this.repository.createQueryBuilder('order');
- // 构建查询条件
- if (orderName) {
- queryBuilder.andWhere('order.orderName LIKE :orderName', { orderName: `%${orderName}%` });
- }
- if (platformId) {
- queryBuilder.andWhere('order.platformId = :platformId', { platformId });
- }
- if (companyId) {
- queryBuilder.andWhere('order.companyId = :companyId', { companyId });
- }
- if (channelId) {
- queryBuilder.andWhere('order.channelId = :channelId', { channelId });
- }
- if (orderStatus) {
- queryBuilder.andWhere('order.orderStatus = :orderStatus', { orderStatus });
- }
- // 获取总数
- const total = await queryBuilder.getCount();
- // 获取数据
- const data = await queryBuilder
- .skip((page - 1) * limit)
- .take(limit)
- .orderBy('order.createTime', 'DESC')
- .getMany();
- // 获取每个订单的人员数量
- const orderIds = data.map(order => order.id);
- let personCounts: Array<{ orderId: number; count: number }> = [];
- if (orderIds.length > 0) {
- const personCountQuery = await this.orderPersonRepository
- .createQueryBuilder('orderPerson')
- .select('orderPerson.orderId', 'orderId')
- .addSelect('COUNT(orderPerson.id)', 'count')
- .where('orderPerson.orderId IN (:...orderIds)', { orderIds })
- .groupBy('orderPerson.orderId')
- .getRawMany();
- personCounts = personCountQuery.map(item => ({
- orderId: item.orderId,
- count: parseInt(item.count)
- }));
- }
- // 格式化返回数据
- const formattedData = data.map(order => {
- const personCount = personCounts.find(pc => pc.orderId === order.id)?.count || 0;
- return {
- orderId: order.id,
- orderName: order.orderName,
- platformId: order.platformId,
- companyId: order.companyId,
- channelId: order.channelId,
- expectedStartDate: order.expectedStartDate,
- actualStartDate: order.actualStartDate,
- actualEndDate: order.actualEndDate,
- orderStatus: order.orderStatus,
- workStatus: order.workStatus,
- createTime: order.createTime,
- updateTime: order.updateTime,
- personCount
- };
- });
- return { data: formattedData, total };
- }
- /**
- * 查询单个订单详情 - 自定义方法
- */
- async findOne(id: number): Promise<any | null> {
- const order = await this.repository.findOne({
- where: { id },
- relations: ['orderPersons']
- });
- if (!order) {
- return null;
- }
- // 获取订单人员详情(这里简化处理,实际可能需要关联更多信息)
- const orderPersons = order.orderPersons || [];
- return {
- orderId: order.id,
- orderName: order.orderName,
- platformId: order.platformId,
- companyId: order.companyId,
- channelId: order.channelId,
- expectedStartDate: order.expectedStartDate,
- actualStartDate: order.actualStartDate,
- actualEndDate: order.actualEndDate,
- orderStatus: order.orderStatus,
- workStatus: order.workStatus,
- createTime: order.createTime,
- updateTime: order.updateTime,
- orderPersons: orderPersons.map(person => ({
- opId: person.id,
- orderId: person.orderId,
- personId: person.personId,
- joinDate: person.joinDate,
- leaveDate: person.leaveDate,
- workStatus: person.workStatus,
- salaryDetail: person.salaryDetail
- }))
- };
- }
- /**
- * 订单激活 - 自定义业务方法
- */
- async activateOrder(orderId: number): Promise<boolean> {
- const queryRunner = this.dataSource.createQueryRunner();
- await queryRunner.connect();
- await queryRunner.startTransaction();
- try {
- // 查询订单
- const order = await queryRunner.manager.findOne(EmploymentOrder, {
- where: { id: orderId }
- });
- if (!order) {
- throw new Error(`订单ID ${orderId} 不存在`);
- }
- if (order.orderStatus !== OrderStatus.DRAFT) {
- throw new Error(`只有草稿状态的订单才能激活,当前订单状态为: ${order.orderStatus}`);
- }
- // 更新订单状态为已确认
- order.orderStatus = OrderStatus.CONFIRMED;
- order.actualStartDate = new Date();
- await queryRunner.manager.save(order);
- // 更新订单人员状态为待就业
- await queryRunner.manager.update(OrderPerson,
- { orderId: orderId },
- { workStatus: WorkStatus.PRE_WORKING }
- );
- await queryRunner.commitTransaction();
- return true;
- } catch (error) {
- await queryRunner.rollbackTransaction();
- throw error;
- } finally {
- await queryRunner.release();
- }
- }
- /**
- * 订单关闭 - 自定义业务方法
- */
- async closeOrder(orderId: number): Promise<boolean> {
- const queryRunner = this.dataSource.createQueryRunner();
- await queryRunner.connect();
- await queryRunner.startTransaction();
- try {
- // 查询订单
- const order = await queryRunner.manager.findOne(EmploymentOrder, {
- where: { id: orderId }
- });
- if (!order) {
- throw new Error(`订单ID ${orderId} 不存在`);
- }
- if (order.orderStatus !== OrderStatus.CONFIRMED && order.orderStatus !== OrderStatus.IN_PROGRESS) {
- throw new Error(`只有已确认或进行中的订单才能关闭,当前订单状态为: ${order.orderStatus}`);
- }
- // 更新订单状态为已完成
- order.orderStatus = OrderStatus.COMPLETED;
- order.actualEndDate = new Date();
- await queryRunner.manager.save(order);
- // 更新订单人员状态为已离职
- await queryRunner.manager.update(OrderPerson,
- { orderId: orderId },
- {
- workStatus: WorkStatus.RESIGNED,
- leaveDate: new Date()
- }
- );
- await queryRunner.commitTransaction();
- return true;
- } catch (error) {
- await queryRunner.rollbackTransaction();
- throw error;
- } finally {
- await queryRunner.release();
- }
- }
- /**
- * 批量添加人员到订单 - 自定义业务方法
- */
- async batchAddPersons(orderId: number, persons: Array<{
- personId: number;
- joinDate: Date;
- salaryDetail: number;
- }>): Promise<{ success: boolean; message: string; addedCount: number }> {
- // 验证订单是否存在
- const order = await this.repository.findOne({
- where: { id: orderId }
- });
- if (!order) {
- throw new Error(`订单ID ${orderId} 不存在`);
- }
- if (order.orderStatus === OrderStatus.COMPLETED || order.orderStatus === OrderStatus.CANCELLED) {
- throw new Error(`订单ID ${orderId} 已结束或已取消,无法添加人员`);
- }
- // 检查哪些人员已经在订单中
- const existingPersons = await this.orderPersonRepository.find({
- where: { orderId }
- });
- const existingPersonIds = existingPersons.map(p => p.personId);
- // 过滤掉已存在的人员
- const newPersons = persons.filter(p => !existingPersonIds.includes(p.personId));
- if (newPersons.length === 0) {
- return { success: true, message: '所有人员都已在此订单中', addedCount: 0 };
- }
- // 使用事务添加新人员
- const queryRunner = this.dataSource.createQueryRunner();
- await queryRunner.connect();
- await queryRunner.startTransaction();
- try {
- // 创建订单人员关联
- const orderPersons = newPersons.map(person => {
- return queryRunner.manager.create(OrderPerson, {
- orderId: orderId,
- personId: person.personId,
- joinDate: person.joinDate,
- workStatus: WorkStatus.NOT_WORKING,
- salaryDetail: person.salaryDetail,
- });
- });
- await queryRunner.manager.save(orderPersons);
- await queryRunner.commitTransaction();
- return {
- success: true,
- message: `成功添加 ${newPersons.length} 个人员到订单`,
- addedCount: newPersons.length
- };
- } catch (error) {
- await queryRunner.rollbackTransaction();
- throw error;
- } finally {
- await queryRunner.release();
- }
- }
- /**
- * 验证文件ID是否存在
- */
- async validateFileId(fileId: number): Promise<boolean> {
- try {
- const file = await this.fileRepository.findOne({ where: { id: fileId } });
- return !!file;
- } catch (error) {
- console.error('验证文件ID失败:', error);
- return false;
- }
- }
- /**
- * 创建订单人员资产 - 自定义业务方法
- */
- async createOrderPersonAsset(data: {
- orderId: number;
- personId: number;
- assetType: AssetType;
- assetFileType: AssetFileType;
- fileId: number;
- relatedTime?: Date;
- }): Promise<OrderPersonAsset> {
- // 验证文件ID是否存在
- const fileExists = await this.validateFileId(data.fileId);
- if (!fileExists) {
- throw new Error('文件不存在');
- }
- // 验证订单和人员关联是否存在
- const orderPerson = await this.orderPersonRepository.findOne({
- where: { orderId: data.orderId, personId: data.personId }
- });
- if (!orderPerson) {
- throw new Error(`人员ID ${data.personId} 不在订单ID ${data.orderId} 中`);
- }
- // 创建资产记录
- const asset = new OrderPersonAsset({
- orderId: data.orderId,
- personId: data.personId,
- assetType: data.assetType,
- assetFileType: data.assetFileType,
- fileId: data.fileId,
- relatedTime: data.relatedTime || new Date()
- });
- return await this.orderPersonAssetRepository.save(asset);
- }
- /**
- * 查询订单人员资产 - 自定义业务方法
- */
- async queryOrderPersonAsset(query: {
- orderId?: number;
- personId?: number;
- assetType?: string;
- assetFileType?: string;
- page?: number;
- limit?: number;
- }): Promise<{ data: OrderPersonAsset[]; total: number }> {
- const {
- orderId,
- personId,
- assetType,
- assetFileType,
- page = 1,
- limit = 10
- } = query;
- const queryBuilder = this.orderPersonAssetRepository.createQueryBuilder('asset');
- // 构建查询条件
- if (orderId) {
- queryBuilder.andWhere('asset.orderId = :orderId', { orderId });
- }
- if (personId) {
- queryBuilder.andWhere('asset.personId = :personId', { personId });
- }
- if (assetType) {
- queryBuilder.andWhere('asset.assetType = :assetType', { assetType });
- }
- if (assetFileType) {
- queryBuilder.andWhere('asset.assetFileType = :assetFileType', { assetFileType });
- }
- // 获取总数
- const total = await queryBuilder.getCount();
- // 获取数据
- const data = await queryBuilder
- .skip((page - 1) * limit)
- .take(limit)
- .orderBy('asset.updateTime', 'DESC')
- .getMany();
- return { data, total };
- }
- /**
- * 删除订单人员资产 - 自定义业务方法
- */
- async deleteOrderPersonAsset(id: number): Promise<{ success: boolean; message: string }> {
- const asset = await this.orderPersonAssetRepository.findOne({
- where: { id }
- });
- if (!asset) {
- throw new Error(`资产ID ${id} 不存在`);
- }
- await this.orderPersonAssetRepository.delete({ id });
- return {
- success: true,
- message: `成功删除资产ID ${id}`
- };
- }
- }
|