sunpengfei
2025-08-18 e171eb5619f2ede099725dc6322fd3d9b8f3e144
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
{
    /// <summary>
    /// 数据字典命令处理器
    /// </summary>
    public class DictionaryDataCommandHandler(
            IRepository<DictionaryData> rep,
            IRepository<DictionaryCategory> repDictionaryCategory
        ) :
        IRequestHandler<SaveDictionaryDataCommand, Guid>,
        IRequestHandler<SetDictionaryDataIsDisabledCommand, int>,
        IRequestHandler<SyncHumanResourcesAreaDictionaryDataCommand, int>
 
    {
        private readonly IRepository<DictionaryData> rep = rep;
        private readonly IRepository<DictionaryCategory> repDictionaryCategory = repDictionaryCategory;
 
        /// <summary>
        /// 保存数据字典
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> 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<DictionaryData, SaveDictionaryDataCommand>(
                null,
                it =>
                    it.CategoryId == request.CategoryId
                    && it.Code == request.Code
                    && it.Id != request.Id,
                (entity) =>
                {
                    entity.Path = DbUtils.GetTreeDataPath<DictionaryData>(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<DictionaryData>(
                        //   $"{entity.Path}/{entity.Code}/",
                        //   $"{entity.Path}/{request.Code}/",
                        //   cancellationToken).Wait();
                    }
                    else
                    {
                        entity.Deep = request.ParentId == null ? 1 : parent.Deep + 1;
                    }
                    request.Adapt(entity);
                },
                cancellationToken);
        }
 
        /// <summary>
        /// 设置数据字典是否禁用
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<int> Handle(SetDictionaryDataIsDisabledCommand request, CancellationToken cancellationToken)
        {
            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>();
            var areas = await repHumanResourcesBaseArea.AsQueryable().AsNoTracking()
                .OrderBy(it => it.AreaCode)
                .ToListAsync();
            var entities = new List<DictionaryData>();
            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;
        }
 
        /// <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())
            {
                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);
                }
            }
        }
    }
}