using FlexJobApi.Core; using Furion.DatabaseAccessor; using Furion.FriendlyException; using Mapster; using MediatR; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FlexJobApi.Application { /// /// 数据字典命令处理器 /// public class DictionaryDataCommandHandler( IRepository rep, IRepository repDictionaryCategory ) : IRequestHandler, IRequestHandler { private readonly IRepository rep = rep; private readonly IRepository repDictionaryCategory = repDictionaryCategory; /// /// 保存数据字典 /// /// /// /// public async Task Handle(SaveDictionaryDataCommand request, CancellationToken cancellationToken) { var category = await repDictionaryCategory .Where(it => it.Id == request.CategoryId || it.Code == request.CategoryCode) .Select(it => new { it.Id, it.Code }) .FirstOrDefaultAsync(); if (category == null) throw Oops.Oh(EnumErrorCodeType.s404, "数据字典类别"); request.CategoryId = category.Id; if (!request.Code.StartsWith($"{request.CategoryCode}-")) throw Oops.Oh(EnumErrorCodeType.s400, "编号开头需要包含类别编号-"); return await request.SaveData( null, it => it.CategoryId == request.CategoryId && it.Code == request.Code && it.Id != request.Id, (entity) => { entity.Path = DbUtils.GetTreeDataPath(request.ParentId, cancellationToken).Result; if (request.Id.HasValue) { if (entity.Code != request.Code) throw Oops.Oh(EnumErrorCodeType.s400, "编号不可修改"); DbUtils.UpdateTreeDataChildrenPath( $"{entity.Path}/{entity.Code}/", $"{entity.Path}/{request.Code}/", cancellationToken).Wait(); } request.Adapt(entity); }, cancellationToken); } /// /// 设置数据字典是否禁用 /// /// /// /// public Task Handle(SetDictionaryDataIsDisabledCommand request, CancellationToken cancellationToken) { return request.SetIsDisabled(cancellationToken: cancellationToken); } } }