完善基础数据

This commit is contained in:
yanpeng200212
2026-10-08 15:44:07 +08:00
parent 8430ba0452
commit 2ffb83830c
95 changed files with 37759 additions and 5 deletions
@@ -0,0 +1,182 @@
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;
// ==================== 物资分组 ====================
/// <summary>新增物资分组(编号由前端调用 new-code 预生成后提交)</summary>
public record WuziGroup_CreateCommand(long ParentId, WuziBusiness_Type_Enum BusinessType,
Department_DutyType_Enum DepartmentType, string Code, string Name, int SortOrder, string Description) : IRequest<long>;
public class CreateWuziGroupCommandHandler(
IWuziGroupWriteRepository repository,
IWuziGroupReadRepository readRepository,
ISnowflakeIdGenerator snowflakeIdGenerator,
IPublisher eventBus) : IRequestHandler<WuziGroup_CreateCommand, long>
{
public async Task<long> 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;
}
}
/// <summary>修改物资分组(编号不可改)</summary>
public record WuziGroup_UpdateCommand(long Id, WuziBusiness_Type_Enum BusinessType,
Department_DutyType_Enum DepartmentType, string Name, int SortOrder, string Description) : IRequest<bool>;
public class UpdateWuziGroupCommandHandler(
IWuziGroupWriteRepository repository,
IPublisher eventBus) : IRequestHandler<WuziGroup_UpdateCommand, bool>
{
public async Task<bool> 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;
}
}
/// <summary>删除物资分组(存在下级分组或物资时不允许删除)</summary>
public record WuziGroup_DelCommand(long Id) : IRequest<bool>;
public class DelWuziGroupCommandHandler(
IWuziGroupWriteRepository repository,
IWuziGroupReadRepository readRepository,
IPublisher eventBus) : IRequestHandler<WuziGroup_DelCommand, bool>
{
public async Task<bool> 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;
}
}
// ==================== 物资目录 ====================
/// <summary>新增物资</summary>
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<long>;
public class CreateWuziMuluCommandHandler(
IWuziMuluWriteRepository repository,
IWuziMuluReadRepository readRepository,
ISnowflakeIdGenerator snowflakeIdGenerator,
IPublisher eventBus) : IRequestHandler<WuziMulu_CreateCommand, long>
{
public async Task<long> 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;
}
}
/// <summary>修改物资(编号不可改)</summary>
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<bool>;
public class UpdateWuziMuluCommandHandler(
IWuziMuluWriteRepository repository,
IPublisher eventBus) : IRequestHandler<WuziMulu_UpdateCommand, bool>
{
public async Task<bool> 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;
}
}
/// <summary>删除物资</summary>
public record WuziMulu_DelCommand(long Id) : IRequest<bool>;
public class DelWuziMuluCommandHandler(
IWuziMuluWriteRepository repository,
IPublisher eventBus) : IRequestHandler<WuziMulu_DelCommand, bool>
{
public async Task<bool> 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;
}
}
@@ -0,0 +1,91 @@
using F9WebAiTest.Application.Common;
using F9WebAiTest.Application.Dtos.SYS.WuziMulus;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.WuziMulus;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.WuziMulus.Queries;
// ==================== 物资分组 ====================
/// <summary>全部物资分组(用于构建分组树)</summary>
public record GetWuziGroupAllQuery() : IRequest<List<WuziGroupReadDto_View>>;
public class GetWuziGroupAllQueryHandler(IWuziGroupReadRepository readRepository)
: IRequestHandler<GetWuziGroupAllQuery, List<WuziGroupReadDto_View>>
{
public async Task<List<WuziGroupReadDto_View>> Handle(GetWuziGroupAllQuery query, CancellationToken ct)
=> await readRepository.GetAllAsync();
}
/// <summary>按 Id 查询分组(编辑回显)</summary>
public record GetWuziGroupByIdQuery(long Id) : IRequest<WuziGroupReadDto_Edit>;
public class GetWuziGroupByIdQueryHandler(IWuziGroupReadRepository readRepository)
: IRequestHandler<GetWuziGroupByIdQuery, WuziGroupReadDto_Edit>
{
public async Task<WuziGroupReadDto_Edit> Handle(GetWuziGroupByIdQuery query, CancellationToken ct)
=> await readRepository.GetByIdForEditAsync(query.Id);
}
/// <summary>分组新编号</summary>
public record GetWuziGroupNewCodeQuery(long ParentId) : IRequest<string>;
public class GetWuziGroupNewCodeQueryHandler(IWuziGroupReadRepository readRepository)
: IRequestHandler<GetWuziGroupNewCodeQuery, string>
{
public async Task<string> Handle(GetWuziGroupNewCodeQuery query, CancellationToken ct)
=> await readRepository.GetNewCodeAsync(query.ParentId);
}
/// <summary>是否存在下级(删除前校验)</summary>
public record GetWuziGroupExistChildQuery(long Id) : IRequest<bool>;
public class GetWuziGroupExistChildQueryHandler(IWuziGroupReadRepository readRepository)
: IRequestHandler<GetWuziGroupExistChildQuery, bool>
{
public async Task<bool> Handle(GetWuziGroupExistChildQuery query, CancellationToken ct)
=> await readRepository.ExistChildAsync(query.Id);
}
// ==================== 物资目录 ====================
/// <summary>按分组分页查询物资</summary>
public record GetWuziMuluPagedQuery(long GroupId = 0, string Keyword = "", int PageIndex = 1, int PageSize = 20)
: IRequest<PageResult<WuziMuluReadDto_View>>;
public class GetWuziMuluPagedQueryHandler(IWuziMuluReadRepository readRepository)
: IRequestHandler<GetWuziMuluPagedQuery, PageResult<WuziMuluReadDto_View>>
{
public async Task<PageResult<WuziMuluReadDto_View>> Handle(GetWuziMuluPagedQuery query, CancellationToken ct)
=> await readRepository.GetPagedAsync(query.GroupId, query.Keyword, query.PageIndex, query.PageSize);
}
/// <summary>按 Id 查询物资(编辑回显)</summary>
public record GetWuziMuluByIdQuery(long Id) : IRequest<WuziMuluReadDto_Edit>;
public class GetWuziMuluByIdQueryHandler(IWuziMuluReadRepository readRepository)
: IRequestHandler<GetWuziMuluByIdQuery, WuziMuluReadDto_Edit>
{
public async Task<WuziMuluReadDto_Edit> Handle(GetWuziMuluByIdQuery query, CancellationToken ct)
=> await readRepository.GetByIdForEditAsync(query.Id);
}
/// <summary>物资新编号</summary>
public record GetWuziMuluNewCodeQuery(long GroupId) : IRequest<string>;
public class GetWuziMuluNewCodeQueryHandler(IWuziMuluReadRepository readRepository)
: IRequestHandler<GetWuziMuluNewCodeQuery, string>
{
public async Task<string> Handle(GetWuziMuluNewCodeQuery query, CancellationToken ct)
=> await readRepository.GetNewCodeAsync(query.GroupId);
}
/// <summary>物资选项(供其它模块选择物资)</summary>
public record GetWuziMuluOptionsQuery(string Keyword = "", int Limit = 200) : IRequest<List<WuziMuluOptionDto>>;
public class GetWuziMuluOptionsQueryHandler(IWuziMuluReadRepository readRepository)
: IRequestHandler<GetWuziMuluOptionsQuery, List<WuziMuluOptionDto>>
{
public async Task<List<WuziMuluOptionDto>> Handle(GetWuziMuluOptionsQuery query, CancellationToken ct)
=> await readRepository.GetOptionsAsync(query.Keyword, query.Limit);
}