1
dengjunjie
2025-05-28 01030b05f7ea9b14878102718a2004b4f908dcfc
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_DTO.System;
using WIDESEA_ISystemRepository;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.System;
 
namespace WIDESEA_SystemRepository
{
    public class Sys_DictionaryRepository : RepositoryBase<Sys_Dictionary>, ISys_DictionaryRepository
    {
 
 
        public Sys_DictionaryRepository(IUnitOfWorkManage unitOfWorkManage) : base(unitOfWorkManage)
        {
 
        }
 
        #region 查询数据库select方法
        public class SourceKeyVaule
        {
            public int id { get; set; }
            public int parentId { get; set; }
            public string DicName { get; set; }
            public int DicValue { get; set; }
        }
        public IEnumerable<VueDictionaryDTO> GetDictionaries(IEnumerable<string> dicNos, List<RoleNodes> roleNodes, bool executeSql = true)
        {
            List<Sys_Dictionary> Dictionaries = GetAllDictionary();
            List<VueDictionaryDTO> DictionaryDTO = new List<VueDictionaryDTO>();
 
 
            foreach (var item in Dictionaries.Where(x => dicNos.Contains(x.DicNo)))
            {
                VueDictionaryDTO dictionaryDTO = new VueDictionaryDTO();
                dictionaryDTO.DicNo = item.DicNo;
                dictionaryDTO.Config = item.Config;
                dictionaryDTO.Data = item.DicList.OrderByDescending(o => o.OrderNo).Select(list => new { key = list.DicValue, value = list.DicName });
                if (executeSql)
                {
                    //  2020.05.01增加根据用户信息加载字典数据源sql
                    string sql = item.Sql;
                    if (!string.IsNullOrEmpty(sql))
                    {
                        dictionaryDTO.Data = item.DicNo == "tree_roles" ?
                            QueryRole(GetCustomDBSql(item.DicNo, sql, roleNodes)).OrderBy(x => x.id).Select(list => new { id = list.id, parentId = list.parentId, key = list.DicValue, value = list.DicName }) : Query(GetCustomDBSql(item.DicNo, sql, roleNodes)).OrderByDescending(o => o.OrderNo).Select(list => new { key = list.DicValue, value = list.DicName });
                    }
                }
                DictionaryDTO.Add(dictionaryDTO);
            }
            return DictionaryDTO;
        }
 
        List<SourceKeyVaule> QueryRole(string sql)
        {
            try
            {
                return base.QueryDynamicDataBySql(sql, null).Select(s => new SourceKeyVaule()
                {
                    id = s.id,
                    parentId = s.parentId,
                    DicName = s.value,
                    DicValue = s.key
                }).ToList();
            }
            catch (Exception ex)
            {
                return new List<SourceKeyVaule>();
            }
        }
        /// <summary>
        /// 获取自定义数据源sql
        /// </summary>
        /// <param name="dicNo"></param>
        /// <param name="originalSql"></param>
        /// <returns></returns>
        public static string GetCustomDBSql(string dicNo, string originalSql, List<RoleNodes> roleNodes)
        {
            switch (dicNo)
            {
                case "roles":
                //2020.05.24增加绑定table表时,获取所有的角色列表
                //注意,如果是2020.05.24之前获取的数据库脚本
                //请在菜单【下拉框绑定设置】添加一个字典编号【t_roles】,除了字典编号,其他内容随便填写
                case "t_roles":
                case "tree_roles":
                    originalSql = GetRolesSql(originalSql, roleNodes);
                    break;
                default:
                    break;
            }
            return originalSql;
        }
        /// <summary>
        /// 获取解决的数据源,只能看到自己与下级所有角色
        /// </summary>
        /// <param name="context"></param>
        /// <param name="originalSql"></param>
        /// <returns></returns>
        public static string GetRolesSql(string originalSql, List<RoleNodes> roleNodes)
        {
            if (App.User.IsSuperAdmin)
            {
                return originalSql;
            }
 
            var RoleNodes = roleNodes.Where(x => x.Id == App.User.RoleId)
                   .Select(s => new RoleNodes()
                   {
                       Id = s.Id,
                       ParentId = 0,//将自己的角色作为root节点
                       RoleName = s.RoleName
                   }).ToList();
            List<int> roleIds = roleNodes.Select(x => x.Id).ToList();
            //roleIds.Add(App.User.RoleId);
 
            return originalSql = $@"SELECT RoleId as 'key',RoleId AS id,ParentId AS parentId,RoleName as 'value' FROM Sys_Role 
                      WHERE Enable=1  and RoleId in ({string.Join(',', roleIds)})";
        }
        #endregion
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dicNos"></param>
        /// <param name="executeSql">是否执行自定义sql</param>
        /// <returns></returns>
        public IEnumerable<Sys_Dictionary> GetDictionaries(IEnumerable<string> dicNos, bool executeSql = true)
        {
            List<Sys_Dictionary> Dictionaries = GetDictionaries(dicNos.ToList());
 
            foreach (var item in Dictionaries)
            {
                if (executeSql)
                {
                    //  2020.05.01增加根据用户信息加载字典数据源sql
                    string sql = item.Sql;
                    if (!string.IsNullOrEmpty(sql))
                    {
                        item.DicList = Query(sql);
                    }
                }
            }
 
            return Dictionaries;
        }
 
        List<Sys_DictionaryList> Query(string sql)
        {
            try
            {
                return base.QueryDynamicDataBySql(sql, null).Select(s => new Sys_DictionaryList()
                {
                    DicName = s.value,
                    DicValue = s.key.ToString()
                }).ToList();
            }
            catch (Exception ex)
            {
                return new List<Sys_DictionaryList>();
            }
        }
 
        private List<Sys_Dictionary> GetDictionaries(List<string> dicNos)
        {
            //base.QueryData().ToList();
            List<Sys_Dictionary> _dictionaries = Db.Queryable<Sys_Dictionary>().Where(x => x.Enable == 1 && dicNos.Contains(x.DicNo)).Includes(x => x.DicList).ToList();
            return _dictionaries;
        }
 
        private List<Sys_Dictionary> GetAllDictionary()
        {
            //base.QueryData().ToList();
            List<Sys_Dictionary> _dictionaries = Db.Queryable<Sys_Dictionary>().Includes(x => x.DicList).Where(x => x.Enable == 1).ToList();
            return _dictionaries;
        }
    }
 
    public class SourceKeyVaule
    {
        public object Key { get; set; }
        public string Value { get; set; }
    }
}