using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Application.Interfaces.SnowflakeID;
using F9WebAiTest.Domain.Entities.SYS.Zq;
using F9WebAiTest.Domain.Interfaces.SYS.Zq;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.Zq.Commands;
/// 新增账期
public record Zhangqi_CreateCommand(int Year, int Month, DateTime StartDate, DateTime EndDate) : IRequest;
public class CreateZhangqiCommandHandler(
IZhangqiWriteRepository zhangqiRepository,
ISnowflakeIdGenerator snowflakeIdGenerator,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(Zhangqi_CreateCommand command, CancellationToken cancellationToken)
{
if (command.StartDate > command.EndDate) throw new F9OperationException("开始日期不能大于截止日期");
var entity = new Zhangqi(snowflakeIdGenerator.CreateId(), command.Year, command.Month,
command.StartDate, command.EndDate);
var result = await zhangqiRepository.AddAsync(entity);
if (result == false) throw new F9OperationException("账期", "新增");
foreach (var de in entity.DomainEvents) await eventBus.Publish(de, cancellationToken);
entity.ClearDomainEvents();
return entity.Id;
}
}
/// 修改账期
public record Zhangqi_UpdateCommand(long Id, int Year, int Month, DateTime StartDate, DateTime EndDate) : IRequest;
public class UpdateZhangqiCommandHandler(
IZhangqiWriteRepository zhangqiRepository,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(Zhangqi_UpdateCommand command, CancellationToken cancellationToken)
{
if (command.StartDate > command.EndDate) throw new F9OperationException("开始日期不能大于截止日期");
var entity = await zhangqiRepository.GetByIdAsync(command.Id, cancellationToken)
?? throw new F9NotFoundException("账期不存在");
if (entity.IsZhaZhang) throw new F9OperationException("账期已扎帐,请先取消扎帐后再修改");
entity.Update(command.Year, command.Month, command.StartDate, command.EndDate);
var result = await zhangqiRepository.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 Zhangqi_DelCommand(long Id) : IRequest;
public class DelZhangqiCommandHandler(
IZhangqiWriteRepository zhangqiRepository,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(Zhangqi_DelCommand command, CancellationToken cancellationToken)
{
var entity = await zhangqiRepository.GetByIdAsync(command.Id, cancellationToken)
?? throw new F9NotFoundException("账期不存在");
if (entity.IsZhaZhang) throw new F9OperationException("账期已扎帐,请先取消扎帐后再删除");
var result = await zhangqiRepository.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;
}
}
/// 扎帐 / 取消扎帐(扎帐后账期内业务数据禁止修改)
public record Zhangqi_ZhaZhangCommand(long Id, bool IsZhaZhang) : IRequest;
public class ZhaZhangCommandHandler(
IZhangqiWriteRepository zhangqiRepository,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(Zhangqi_ZhaZhangCommand command, CancellationToken cancellationToken)
{
var entity = await zhangqiRepository.GetByIdAsync(command.Id, cancellationToken)
?? throw new F9NotFoundException("账期不存在");
if (entity.IsZhaZhang == command.IsZhaZhang)
throw new F9OperationException(command.IsZhaZhang ? "该账期已扎帐" : "该账期未扎帐");
entity.SetZhaZhang(command.IsZhaZhang);
var result = await zhangqiRepository.SetZhaZhangAsync(command.Id, command.IsZhaZhang);
if (result == false) throw new F9OperationException(command.IsZhaZhang ? "扎帐" : "取消扎帐", "操作");
foreach (var de in entity.DomainEvents) await eventBus.Publish(de, cancellationToken);
entity.ClearDomainEvents();
return true;
}
}