using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Common.Exceptions; using F9WebAiTest.Application.Interfaces.Repositories.SYS.YuanliaoZhuangkous; using F9WebAiTest.Application.Interfaces.SnowflakeID; using F9WebAiTest.Domain.Entities.SYS.YuanliaoZhuangkous; using F9WebAiTest.Domain.Enums.SYS.YuanliaoZhuangkous; using F9WebAiTest.Domain.Interfaces.SYS.YuanliaoZhuangkous; using MediatR; namespace F9WebAiTest.Application.CQRS.SYS.YuanliaoZhuangkous.Commands; /// 新增原料庄口(编号按年度自动生成) public record YuanliaoZhuangkou_CreateCommand( int Year, string Name, string Jibie, BendiWaidi_Enum BendiWaidi, string Chandi, string Canpinzhong, long WzmlId, string Zhandian, int Pici) : IRequest; public class CreateYuanliaoZhuangkouCommandHandler( IYuanliaoZhuangkouWriteRepository repository, IYuanliaoZhuangkouReadRepository readRepository, ISnowflakeIdGenerator snowflakeIdGenerator, IPublisher eventBus) : IRequestHandler { public async Task Handle(YuanliaoZhuangkou_CreateCommand command, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("庄口名称不能为空"); if (command.Year <= 0) throw new F9OperationException("年度不能为空"); if (string.IsNullOrWhiteSpace(command.Chandi)) throw new F9OperationException("产地不能为空"); if (string.IsNullOrWhiteSpace(command.Zhandian)) throw new F9OperationException("站点不能为空"); if (string.IsNullOrWhiteSpace(command.Jibie)) throw new F9OperationException("季别不能为空"); if (string.IsNullOrWhiteSpace(command.Canpinzhong)) throw new F9OperationException("蚕品种不能为空"); if (command.WzmlId <= 0) throw new F9OperationException("请选择物资(茧别)"); if (string.IsNullOrEmpty(await readRepository.GetWuziNameAsync(command.WzmlId))) throw new F9OperationException("所选物资不存在,请重新选择"); var code = await GenerateCodeAsync(readRepository, command.Year); var entity = new YuanliaoZhuangkou(snowflakeIdGenerator.CreateId(), command.Year, command.Name.Trim(), code, command.Jibie.Trim(), command.BendiWaidi, command.Chandi.Trim(), command.Canpinzhong.Trim(), command.WzmlId, command.Zhandian.Trim(), command.Pici); var result = await repository.AddAsync(entity); if (result == false) throw new F9OperationException("原料庄口", "新增"); foreach (var de in entity.DomainEvents) await eventBus.Publish(de, cancellationToken); entity.ClearDomainEvents(); return entity.Id; } /// 生成编号:{年度} + 3 位流水(如 2025001) private static async Task GenerateCodeAsync(IYuanliaoZhuangkouReadRepository readRepository, int year) { var codes = await readRepository.GetCodesByYearAsync(year); var maxSeq = 0; foreach (var code in codes) { if (string.IsNullOrWhiteSpace(code) || code.Length <= 4) continue; if (int.TryParse(code.Substring(4), out var seq) && seq > maxSeq) maxSeq = seq; } return $"{year}{maxSeq + 1:000}"; } } /// 修改原料庄口(编号不随修改变化) public record YuanliaoZhuangkou_UpdateCommand( long Id, int Year, string Name, string Jibie, BendiWaidi_Enum BendiWaidi, string Chandi, string Canpinzhong, long WzmlId, string Zhandian, int Pici) : IRequest; public class UpdateYuanliaoZhuangkouCommandHandler( IYuanliaoZhuangkouWriteRepository repository, IYuanliaoZhuangkouReadRepository readRepository, IPublisher eventBus) : IRequestHandler { public async Task Handle(YuanliaoZhuangkou_UpdateCommand command, CancellationToken cancellationToken) { var entity = await repository.GetByIdAsync(command.Id, cancellationToken) ?? throw new F9NotFoundException("原料庄口不存在"); if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("庄口名称不能为空"); if (string.IsNullOrWhiteSpace(command.Chandi)) throw new F9OperationException("产地不能为空"); if (string.IsNullOrWhiteSpace(command.Zhandian)) throw new F9OperationException("站点不能为空"); if (string.IsNullOrWhiteSpace(command.Jibie)) throw new F9OperationException("季别不能为空"); if (string.IsNullOrWhiteSpace(command.Canpinzhong)) throw new F9OperationException("蚕品种不能为空"); if (command.WzmlId <= 0) throw new F9OperationException("请选择物资(茧别)"); if (string.IsNullOrEmpty(await readRepository.GetWuziNameAsync(command.WzmlId))) throw new F9OperationException("所选物资不存在,请重新选择"); entity.Update(command.Year, command.Name.Trim(), command.Jibie.Trim(), command.BendiWaidi, command.Chandi.Trim(), command.Canpinzhong.Trim(), command.WzmlId, command.Zhandian.Trim(), command.Pici); var result = await repository.UpdateAsync(entity); if (result == false) throw new F9OperationException("原料庄口", "修改"); foreach (var de in entity.DomainEvents) await eventBus.Publish(de, cancellationToken); entity.ClearDomainEvents(); return true; } } /// 删除原料庄口 public record YuanliaoZhuangkou_DelCommand(long Id) : IRequest; public class DelYuanliaoZhuangkouCommandHandler( IYuanliaoZhuangkouWriteRepository repository, IPublisher eventBus) : IRequestHandler { public async Task Handle(YuanliaoZhuangkou_DelCommand command, CancellationToken cancellationToken) { var entity = await repository.GetByIdAsync(command.Id, cancellationToken) ?? throw new F9NotFoundException("原料庄口不存在"); var result = await repository.SoftDeleteAsync(command.Id); if (result == false) throw new F9OperationException("原料庄口", "删除"); entity.Deleted(); foreach (var de in entity.DomainEvents) await eventBus.Publish(de, cancellationToken); entity.ClearDomainEvents(); return true; } }