| | |
| | | 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<DictionaryData, SaveDictionaryDataCommand>( |
| | | null, |
| | | it => |
| | |
| | | if (request.Id.HasValue) |
| | | { |
| | | if (entity.Code != request.Code) throw Oops.Oh(EnumErrorCodeType.s400, "编号不可修改"); |
| | | DbUtils.UpdateTreeDataChildrenPath<DictionaryData>( |
| | | $"{entity.Path}/{entity.Code}/", |
| | | $"{entity.Path}/{request.Code}/", |
| | | cancellationToken).Wait(); |
| | | if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s400, "上级Id不可修改"); |
| | | //DbUtils.UpdateTreeDataChildrenPath<DictionaryData>( |
| | | // $"{entity.Path}/{entity.Code}/", |
| | | // $"{entity.Path}/{request.Code}/", |
| | | // cancellationToken).Wait(); |
| | | } |
| | | else |
| | | { |
| | | entity.Deep = request.ParentId == null ? 1 : parent.Deep + 1; |
| | | } |
| | | request.Adapt(entity); |
| | | }, |
| | |
| | | return request.SetIsDisabled<DictionaryData>(cancellationToken: cancellationToken); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 同步人力资源地区字典数据 |
| | | /// </summary> |
| | | /// <param name="request"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | public async Task<int> Handle(SyncHumanResourcesAreaDictionaryDataCommand request, CancellationToken cancellationToken) |
| | | { |
| | | var repHumanResourcesBaseArea = Db.GetRepository<BaseArea, HumanResourcesDbContextLocator>(); |
| | |
| | | CategoryId = categoryId |
| | | }); |
| | | var parentAreas = areas.Where(it => it.ParentId == 0).ToList(); |
| | | LoopAdd(categoryId, entities, areas, parentAreas); |
| | | LoopSyncHumanResourcesAreaDictionaryData(categoryId, entities, areas, parentAreas); |
| | | await rep.Context.BulkInsertAsync(entities); |
| | | return entities.Count; |
| | | } |
| | | |
| | | public void LoopAdd(Guid categoryId, List<DictionaryData> entities, List<BaseArea> all, List<BaseArea> areas) |
| | | /// <summary> |
| | | /// 递归同步人力资源地区字典数据 |
| | | /// </summary> |
| | | /// <param name="categoryId"></param> |
| | | /// <param name="entities"></param> |
| | | /// <param name="all"></param> |
| | | /// <param name="areas"></param> |
| | | /// <param name="deep"></param> |
| | | private void LoopSyncHumanResourcesAreaDictionaryData(Guid categoryId, List<DictionaryData> entities, List<BaseArea> all, List<BaseArea> areas, int deep = 1) |
| | | { |
| | | if (areas.IsNotNull()) |
| | | { |
| | |
| | | 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(); |
| | | LoopAdd(categoryId, entities, all, children); |
| | | LoopSyncHumanResourcesAreaDictionaryData(categoryId, entities, all, children, deep + 1); |
| | | } |
| | | } |
| | | } |