using Consul; using EFCore.BulkExtensions; using FlexJobApi.Core; using Furion.DatabaseAccessor; using Furion.DatabaseAccessor.Extensions; 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.CommonServer.Application { /// /// 数据字典命令处理器 /// public class DictionaryDataCommandHandler( IRepository rep, IRepository repDictionaryCategory ) : IRequestHandler, 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($"{category.Code}-")) throw Oops.Oh(EnumErrorCodeType.s400, "编号开头需要包含类别编号-"); var parent = request.ParentId.HasValue ? await rep.AsQueryable().AsNoTracking() .FirstOrDefaultAsync(it => it.Id == request.ParentId) : null; 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, "编号不可修改"); if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s400, "上级Id不可修改"); //DbUtils.UpdateTreeDataChildrenPath( // $"{entity.Path}/{entity.Code}/", // $"{entity.Path}/{request.Code}/", // cancellationToken).Wait(); } else { entity.Deep = request.ParentId == null ? 1 : parent.Deep + 1; } request.Adapt(entity); }, cancellationToken); } /// /// 设置数据字典是否禁用 /// /// /// /// public Task Handle(SetDictionaryDataIsDisabledCommand request, CancellationToken cancellationToken) { return request.SetIsDisabled(cancellationToken: cancellationToken); } /// /// 同步人力资源地区字典数据 /// /// /// /// public async Task Handle(SyncHumanResourcesAreaDictionaryDataCommand request, CancellationToken cancellationToken) { var repHumanResourcesBaseArea = Db.GetRepository(); var areas = await repHumanResourcesBaseArea.AsQueryable().AsNoTracking() .OrderBy(it => it.AreaCode) .ToListAsync(); var entities = new List(); var categoryId = new Guid("B21FE000-BB7F-4498-08E9-08DDD572EF73"); await "DELETE FROM DictionaryData WHERE CategoryId = @CategoryId".SqlNonQueryAsync(new { CategoryId = categoryId }); var parentAreas = areas.Where(it => it.ParentId == 0).ToList(); LoopSyncHumanResourcesAreaDictionaryData(categoryId, entities, areas, parentAreas); await rep.Context.BulkInsertAsync(entities); return entities.Count; } /// /// 递归同步人力资源地区字典数据 /// /// /// /// /// /// private void LoopSyncHumanResourcesAreaDictionaryData(Guid categoryId, List entities, List all, List areas, int deep = 1) { if (areas.IsNotNull()) { foreach (var area in areas) { var entity = new DictionaryData(); entity.CategoryId = categoryId; entity.Id = area.Id; if (area.ParentId != 0) { var parent = entities.FirstOrDefault(it => it.Field1 == area.ParentId.ToString()); if (parent == null) { var parentArea = areas.FirstOrDefault(it => it.AreaCode == area.ParentId); if (parentArea != null) { throw Oops.Oh(EnumErrorCodeType.s404, "上级地区"); } continue; } entity.ParentId = parent.Id; entity.Path = $"{parent.Path}{parent.Code}/"; } else { entity.Path = "/"; } entity.Code = $"70-{area.AreaCode}"; entity.Content = area.AreaName; entity.Field1 = area.AreaCode.ToString(); entity.Field2 = area.QuickQuery; entity.Field3 = area.SimpleSpelling; entity.Field4 = area.Layer.ToString(); entity.Field5 = area.Description; entity.Deep = deep; entity.Sort = area.Sort ?? 0; entities.Add(entity); var children = all.Where(it => it.ParentId == area.AreaCode).ToList(); LoopSyncHumanResourcesAreaDictionaryData(categoryId, entities, all, children, deep + 1); } } } } }