123 lines
6.3 KiB
C#
123 lines
6.3 KiB
C#
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;
|
||
|
||
/// <summary>新增原料庄口(编号按年度自动生成)</summary>
|
||
public record YuanliaoZhuangkou_CreateCommand(
|
||
int Year, string Name, string Jibie, BendiWaidi_Enum BendiWaidi, string Chandi,
|
||
string Canpinzhong, long WzmlId, string Zhandian, int Pici) : IRequest<long>;
|
||
|
||
public class CreateYuanliaoZhuangkouCommandHandler(
|
||
IYuanliaoZhuangkouWriteRepository repository,
|
||
IYuanliaoZhuangkouReadRepository readRepository,
|
||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||
IPublisher eventBus) : IRequestHandler<YuanliaoZhuangkou_CreateCommand, long>
|
||
{
|
||
public async Task<long> 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;
|
||
}
|
||
|
||
/// <summary>生成编号:{年度} + 3 位流水(如 2025001)</summary>
|
||
private static async Task<string> 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}";
|
||
}
|
||
}
|
||
|
||
/// <summary>修改原料庄口(编号不随修改变化)</summary>
|
||
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<bool>;
|
||
|
||
public class UpdateYuanliaoZhuangkouCommandHandler(
|
||
IYuanliaoZhuangkouWriteRepository repository,
|
||
IYuanliaoZhuangkouReadRepository readRepository,
|
||
IPublisher eventBus) : IRequestHandler<YuanliaoZhuangkou_UpdateCommand, bool>
|
||
{
|
||
public async Task<bool> 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;
|
||
}
|
||
}
|
||
|
||
/// <summary>删除原料庄口</summary>
|
||
public record YuanliaoZhuangkou_DelCommand(long Id) : IRequest<bool>;
|
||
|
||
public class DelYuanliaoZhuangkouCommandHandler(
|
||
IYuanliaoZhuangkouWriteRepository repository,
|
||
IPublisher eventBus) : IRequestHandler<YuanliaoZhuangkou_DelCommand, bool>
|
||
{
|
||
public async Task<bool> 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;
|
||
}
|
||
}
|