using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.WuziMulus;
using F9WebAiTest.Application.Interfaces.SnowflakeID;
using F9WebAiTest.Domain.Entities.SYS.WuziMulus;
using F9WebAiTest.Domain.Enums.SYS.Departments;
using F9WebAiTest.Domain.Enums.SYS.WuziMulus;
using F9WebAiTest.Domain.Interfaces.SYS.WuziMulus;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.WuziMulus.Commands;
// ==================== 物资分组 ====================
/// 新增物资分组(编号由前端调用 new-code 预生成后提交)
public record WuziGroup_CreateCommand(long ParentId, WuziBusiness_Type_Enum BusinessType,
Department_DutyType_Enum DepartmentType, string Code, string Name, int SortOrder, string Description) : IRequest;
public class CreateWuziGroupCommandHandler(
IWuziGroupWriteRepository repository,
IWuziGroupReadRepository readRepository,
ISnowflakeIdGenerator snowflakeIdGenerator,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(WuziGroup_CreateCommand command, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("分组名称不能为空");
var code = (command.Code ?? "").Trim();
if (string.IsNullOrEmpty(code)) code = await readRepository.GetNewCodeAsync(command.ParentId);
var entity = new WuziGroup(snowflakeIdGenerator.CreateId(), command.BusinessType, command.DepartmentType,
code, command.Name.Trim(), command.ParentId, command.SortOrder, command.Description ?? string.Empty);
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;
}
}
/// 修改物资分组(编号不可改)
public record WuziGroup_UpdateCommand(long Id, WuziBusiness_Type_Enum BusinessType,
Department_DutyType_Enum DepartmentType, string Name, int SortOrder, string Description) : IRequest;
public class UpdateWuziGroupCommandHandler(
IWuziGroupWriteRepository repository,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(WuziGroup_UpdateCommand command, CancellationToken cancellationToken)
{
var entity = await repository.GetByIdAsync(command.Id, cancellationToken)
?? throw new F9NotFoundException("物资分组不存在");
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("分组名称不能为空");
entity.Update(command.BusinessType, command.DepartmentType, command.Name.Trim(), command.SortOrder,
command.Description ?? string.Empty);
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 WuziGroup_DelCommand(long Id) : IRequest;
public class DelWuziGroupCommandHandler(
IWuziGroupWriteRepository repository,
IWuziGroupReadRepository readRepository,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(WuziGroup_DelCommand command, CancellationToken cancellationToken)
{
var entity = await repository.GetByIdAsync(command.Id, cancellationToken)
?? throw new F9NotFoundException("物资分组不存在");
if (await readRepository.ExistChildAsync(command.Id)) throw new F9OperationException("存在下级分组,不能删除");
if (await readRepository.ExistMuluAsync(command.Id)) throw new F9OperationException("分组下存在物资,不能删除");
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;
}
}
// ==================== 物资目录 ====================
/// 新增物资
public record WuziMulu_CreateCommand(long GroupId, string Name, string Code, string Unit, string Guige,
string Xinghao, string PinyinName, decimal Kucunyujing, int SortOrder, bool IsYujing, decimal SaleRate) : IRequest;
public class CreateWuziMuluCommandHandler(
IWuziMuluWriteRepository repository,
IWuziMuluReadRepository readRepository,
ISnowflakeIdGenerator snowflakeIdGenerator,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(WuziMulu_CreateCommand command, CancellationToken cancellationToken)
{
if (command.GroupId <= 0) throw new F9OperationException("请选择所属分组");
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("物资名称不能为空");
var code = (command.Code ?? "").Trim();
if (string.IsNullOrEmpty(code)) code = await readRepository.GetNewCodeAsync(command.GroupId);
var entity = new WuziMulu(snowflakeIdGenerator.CreateId(), command.GroupId, command.Name.Trim(), code,
command.Unit ?? string.Empty, command.Guige ?? string.Empty, command.Xinghao ?? string.Empty,
command.PinyinName ?? string.Empty, command.Kucunyujing, command.SortOrder, command.IsYujing, command.SaleRate);
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;
}
}
/// 修改物资(编号不可改)
public record WuziMulu_UpdateCommand(long Id, long GroupId, string Name, string Unit, string Guige,
string Xinghao, string PinyinName, decimal Kucunyujing, int SortOrder, bool IsYujing, decimal SaleRate) : IRequest;
public class UpdateWuziMuluCommandHandler(
IWuziMuluWriteRepository repository,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(WuziMulu_UpdateCommand command, CancellationToken cancellationToken)
{
var entity = await repository.GetByIdAsync(command.Id, cancellationToken)
?? throw new F9NotFoundException("物资不存在");
if (command.GroupId <= 0) throw new F9OperationException("请选择所属分组");
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("物资名称不能为空");
entity.Update(command.GroupId, command.Name.Trim(), command.Unit ?? string.Empty, command.Guige ?? string.Empty,
command.Xinghao ?? string.Empty, command.PinyinName ?? string.Empty, command.Kucunyujing,
command.SortOrder, command.IsYujing, command.SaleRate);
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 WuziMulu_DelCommand(long Id) : IRequest;
public class DelWuziMuluCommandHandler(
IWuziMuluWriteRepository repository,
IPublisher eventBus) : IRequestHandler
{
public async Task Handle(WuziMulu_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;
}
}