sunpengfei
7 天以前 56af0f468f321d41db70343abf558d61cda58d31
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
173
174
175
using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
 
namespace FlexJobApi.CommonServer.Application
{
    /// <summary>
    /// 数据字典查询处理器
    /// </summary>
    public class DictionaryDatasQueryHandler(
            IRepository<DictionaryData> rep
        ) : IRequestHandler<GetDictionaryDatasQuery, PagedListQueryResult<GetDictionaryDatasQueryResultItem>>,
            IRequestHandler<GetDictionaryDataSelectQuery, List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>>,
            IRequestHandler<GetAreaSelectQuery, List<GetAreaSelectQueryResultOption>>
    {
        private readonly IRepository<DictionaryData> rep = rep;
 
        /// <summary>
        /// 获取数据字典分页列表数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<PagedListQueryResult<GetDictionaryDatasQueryResultItem>> Handle(GetDictionaryDatasQuery request, CancellationToken cancellationToken)
        {
            return request.PageModel.GetPagedListAsync<DictionaryData, GetDictionaryDatasQueryResultItem>(
                q =>
                {
                    q = q.OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime);
                    if (request.CategoryId.HasValue)
                    {
                        q = q.Where(it => it.CategoryId == request.CategoryId);
                    }
                    else if (request.CategoryCode.IsNotNull())
                    {
                        q = q.Where(it => it.Category.Code == request.CategoryCode);
                    }
                    else
                    {
                        throw Oops.Oh(EnumErrorCodeType.s400, "请填写类别Id或编号");
                    }
                    if (request.ParentId.HasValue)
                    {
                        q = q.Where(it => it.ParentId == request.ParentId);
                    }
                    if (request.Keywords.IsNotNull())
                    {
                        q = q.Where(it =>
                            it.Code.Contains(request.Keywords)
                            || it.Content.Contains(request.Keywords)
                            || it.Field1.Contains(request.Keywords)
                            || it.Field2.Contains(request.Keywords)
                            || it.Field3.Contains(request.Keywords)
                            || it.Field4.Contains(request.Keywords)
                            || it.Field5.Contains(request.Keywords));
                    }
                    return q;
                }, cancellationToken: cancellationToken);
        }
 
        /// <summary>
        /// 查询数据字典选择器
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>> Handle(GetDictionaryDataSelectQuery request, CancellationToken cancellationToken)
        {
            var sql = @"SELECT d.* FROM DictionaryData d
INNER JOIN DictionaryCategory c ON d.CategoryId = c.Id
WHERE d.IsDisabled = 0";
            if (request.All != true)
            {
                if (request.ParentId == null)
                {
                    sql += " AND d.ParentId IS NULL";
                }
                else
                {
                    sql += " AND d.ParentId = @ParentId";
                }
            }
            if (request.MaxDeep.HasValue)
            {
                sql += " AND d.Deep <= @MaxDeep";
            }
            if (request.Keywords.IsNotNull())
            {
                sql += @" AND (d.Code LIKE @Keywords
OR d.Content LIKE @Keywords
OR d.Field1 LIKE @Keywords
OR d.Field2 LIKE @Keywords
OR d.Field3 LIKE @Keywords
OR d.Field4 LIKE @Keywords
OR d.Field5 LIKE @Keywords)";
            }
            if (request.CategoryId.HasValue)
            {
                sql += " AND d.CategoryId = @CategoryId";
            }
            else if (request.CategoryCode.IsNotNull())
            {
                sql += " AND c.Code = @CategoryCode";
            }
            else
            {
                throw Oops.Oh(EnumErrorCodeType.s400, "请填写类别Id或编号");
            }
            sql += " ORDER BY d.Sort";
            var models = await rep.SqlQueriesAsync<GetDictionaryDataSelectQueryResultOption>(sql, request, cancellationToken);
            var options = new List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>();
            foreach (var model in models)
            {
                var option = new SelectOption<string, GetDictionaryDataSelectQueryResultOption>();
                option.Data = model;
                option.Value = model.Code;
                option.Label = model.Content;
                options.Add(option);
            }
            if (request.WithChildren == true)
            {
                var parents = options.Where(it => it.Data.ParentId == null).ToList();
                LoopChildrens(parents, options);
                return parents;
            }
            return options;
        }
 
        /// <summary>
        /// 查询地区选择器
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<GetAreaSelectQueryResultOption>> Handle(GetAreaSelectQuery request, CancellationToken cancellationToken)
        {
            var models = await Handle(new GetDictionaryDataSelectQuery
            {
                CategoryCode = "70",
                All = true,
                MaxDeep = request.MaxDeep,
                WithChildren = true
            }, cancellationToken);
            var result = models.Adapt<List<GetAreaSelectQueryResultOption>>();
            return result;
        }
 
        /// <summary>
        /// 递归赋值下级
        /// </summary>
        /// <param name="models"></param>
        /// <param name="all"></param>
        private void LoopChildrens(List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>> models, List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>> all)
        {
            foreach (var item in models)
            {
                item.Data.Children = all.Where(it => it.Data.ParentId == item.Data.Id).ToList();
                if (item.Data.Children.IsNotNull())
                {
                    LoopChildrens(item.Data.Children, all);
                }
            }
        }
    }
}