完善基础数据
This commit is contained in:
+122
@@ -0,0 +1,122 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
using F9WebAiTest.Application.Common;
|
||||
using F9WebAiTest.Application.Dtos.SYS.WuziMulus;
|
||||
using F9WebAiTest.Application.Dtos.SYS.YuanliaoZhuangkous;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.YuanliaoZhuangkous;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.YuanliaoZhuangkous.Queries;
|
||||
|
||||
/// <summary>原料庄口分页查询(年度 + 产地/站点/季别 + 关键字)</summary>
|
||||
public record GetYuanliaoZhuangkouPagedQuery(
|
||||
int Year = 0, string Chandi = "", string Zhandian = "", string Jibie = "", string Like = "",
|
||||
int PageIndex = 1, int PageSize = 20) : IRequest<PageResult<YuanliaoZhuangkouReadDto_Table>>;
|
||||
|
||||
public class GetYuanliaoZhuangkouPagedQueryHandler(IYuanliaoZhuangkouReadRepository readRepository)
|
||||
: IRequestHandler<GetYuanliaoZhuangkouPagedQuery, PageResult<YuanliaoZhuangkouReadDto_Table>>
|
||||
{
|
||||
public async Task<PageResult<YuanliaoZhuangkouReadDto_Table>> Handle(GetYuanliaoZhuangkouPagedQuery query, CancellationToken ct)
|
||||
=> await readRepository.GetPagedAsync(query.Year, query.Chandi, query.Zhandian, query.Jibie,
|
||||
query.Like, query.PageIndex, query.PageSize);
|
||||
}
|
||||
|
||||
/// <summary>按 Id 查询原料庄口(编辑回显)</summary>
|
||||
public record GetYuanliaoZhuangkouByIdQuery(long Id) : IRequest<YuanliaoZhuangkouReadDto_Edit>;
|
||||
|
||||
public class GetYuanliaoZhuangkouByIdQueryHandler(IYuanliaoZhuangkouReadRepository readRepository)
|
||||
: IRequestHandler<GetYuanliaoZhuangkouByIdQuery, YuanliaoZhuangkouReadDto_Edit>
|
||||
{
|
||||
public async Task<YuanliaoZhuangkouReadDto_Edit> Handle(GetYuanliaoZhuangkouByIdQuery query, CancellationToken ct)
|
||||
=> await readRepository.GetByIdForEditAsync(query.Id);
|
||||
}
|
||||
|
||||
/// <summary>物资(茧别)下拉选项:来自物资目录中名称含"茧"的物资,支持关键字过滤</summary>
|
||||
public record GetYuanliaoZhuangkouWuziOptionsQuery(string Keyword = "") : IRequest<List<WuziMuluOptionDto>>;
|
||||
|
||||
public class GetYuanliaoZhuangkouWuziOptionsQueryHandler(IYuanliaoZhuangkouReadRepository readRepository)
|
||||
: IRequestHandler<GetYuanliaoZhuangkouWuziOptionsQuery, List<WuziMuluOptionDto>>
|
||||
{
|
||||
public async Task<List<WuziMuluOptionDto>> Handle(GetYuanliaoZhuangkouWuziOptionsQuery query, CancellationToken ct)
|
||||
=> await readRepository.GetWuziOptionsAsync(query.Keyword);
|
||||
}
|
||||
Reference in New Issue
Block a user