bklLiudl
2025-04-07 4e8f58cb41c7b6d570fd1979d80f74ab8a4d00c2
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using Dm;
using Model.ModelDto.SysDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IDAL.ISysInterface;
 
namespace WMS.DAL.SysInfrastructure
{
    public class DictionaryRepository : BaseRepository<SysDictionary>, IDictionaryRepository
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        public DictionaryRepository() : base(Db)
        {
        }
 
        /// <summary>
        /// 获取字典信息列表
        /// </summary>
        /// <param name="DictName">字典名称</param>
        /// <param name="DictNo">父级字典号</param>
        /// <param name="Level">层级</param>
        /// <param name="IsEdit">允许编辑</param>
        /// <param name="IsAdd">允许新增</param>
        /// <returns></returns>
        public List<DictionaryDto> GetDicList(string DictName, string DictNo, string Level, string IsEdit, string IsAdd)
        {
            string str = "select dic.*,user1.RealName CreateName from SysDictionary dic left join SysUserInfor user1 on dic.CreateUser = user1.Id where dic.IsDel = @isdel";
            //判断层级
            if (Level == "1")
            {
                str += " and dic.Level = @level";
            }
            //判断允许编辑
            if (!string.IsNullOrEmpty(IsEdit))
            {
                str += " and dic.IsEdit = @isedit";
            }
            //判断允许新增
            if (!string.IsNullOrEmpty(IsAdd))
            {
                str += " and dic.IsAdd = @isadd";
            }
            //判断字典名称是否为空
            if (!string.IsNullOrEmpty(DictName))
            {
                if (Level == "1")
                {
                    str += " and dic.ParentNo in (select DictNo from SysDictionary where DictName like @dictname)";
                }
                if (Level == "0")
                {
                    str += " and dic.DictName like @dictname";
                }
                if (string.IsNullOrEmpty(Level))
                {
                    str += " and dic.DictName like @dictname or dic.ParentNo in (select DictNo from SysDictionary where DictName like @dictname)";
                }
            }
            //判断父级字典号是否为空
            if (!string.IsNullOrEmpty(DictNo))
            {
                if (Level == "1")
                {
                    str += " and dic.ParentNo = @dictno";
                }
                if (Level == "0")
                {
                    str += " and dic.DictNo = @dictno";
                }
                if (string.IsNullOrEmpty(Level))
                {
                    str += " and dic.DictNo = @dictno or dic.ParentNo = @dictno";
                }
            }
            //判断层级
            if (Level == "0")
            {
                str += " and dic.Level = @level";
            }
            //排序(根据ord显示顺序)
            str += " and dic.IsPublic ='0'";
            List<DictionaryDto> diclist = Db.Ado.SqlQuery<DictionaryDto>(str, new
            {
                isdel = "0", //是否删除
                dictname = "%" + DictName + "%", //字典名称
                dictno = DictNo, //字典号
                level = Level, //层级
                isedit = IsEdit, //允许编辑
                isadd = IsAdd //允许新增
            });
            diclist = diclist.OrderBy(a => int.Parse(a.Ord)).ToList();
            return diclist;
        }
 
        /// <summary>
        /// 根据id获取字典信息
        /// </summary>
        /// <param name="id">字典id</param>
        /// <returns></returns>
        public SysDictionary GetDicById(int id)
        {
            string str = "select dic.*,user1.UserName CreateName from SysDictionary dic left join SysUserInfor user1 on dic.CreateUser = user1.Id where dic.IsDel = @isdel and dic.Id = @id";
            SysDictionary dic = Db.Ado.SqlQuery<SysDictionary>(str, new
            {
                isdel = "0", //是否删除
                id //id
            }).First();
            return dic;
        }
 
        /// <summary>
        /// 获取父级字典号(根据层级根目录)
        /// </summary>
        /// <returns></returns>
        public List<SysDictionary> GetDicParentListByLevel()
        {
            string str = "select Id,DictNo,DictName,ParentNo,Level from SysDictionary where Level = @level and IsDel = @isdel and IsAdd = @isadd";
            List<SysDictionary> diclist = Db.Ado.SqlQuery<SysDictionary>(str, new
            {
                level = "0", //层级
                isdel = "0", //是否删除
                isadd = "0" //允许添加
            });
            return diclist;
        }
 
        /// <summary>
        /// 根据编号查询字典信息
        /// </summary>
        /// <param name="DictNo">字典编号</param>
        /// <returns></returns>
        public List<SysDictionary> GetDicByNo(string DictNo)
        {
            string str = $"select * from SysDictionary where DictNo = @dictno";
            List<SysDictionary> diclist = Db.Ado.SqlQuery<SysDictionary>(str, new
            {
                dictno = DictNo //字典编号
            });
            return diclist;
        }
 
        /// <summary>
        /// 新增字典信息
        /// </summary>
        /// <param name="dic">数据字典实体模型</param>
        /// <returns></returns>
        public async Task<int> AddDic(SysDictionary dic)
        {
            //获取是否拥有相同字典名称
            if (!string.IsNullOrEmpty(dic.DictName))
            {
                var dicno = Db.Queryable<SysDictionary>().First(a => a.DictName == dic.DictName);
                if (dicno != null)
                {
                    throw new Exception("获取是否拥有相同字典名称");
                }
            }
            else
            {
                throw new Exception("字典名称为空,请核实!");
            }
            string str = "insert into SysDictionary values(@dictno, @dictname, @parentno, @ord, @level, @ispublic, @isedit, @isadd, @isdel, @createtime, @createuser, null, null)";
            //int i = await CudAsync(str, new
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                dictno = dic.DictNo, //字典编号
                dictname = dic.DictName, //字典名称
                parentno = dic.ParentNo, //父级字典号
                ord = dic.Ord, //显示顺序
                level = dic.Level, //层级
                ispublic = dic.IsPublic, //是否公开
                isedit = dic.IsEdit, //允许编辑
                isadd = dic.IsAdd, //允许添加
                isdel = "0", //是否删除
                createtime = Db.GetDate(), //创建时间
                createuser = dic.CreateUser //创建人
            });
            return i;
        }
 
        /// <summary>
        /// 删除字典信息
        /// </summary>
        /// <param name="dic">数据字典实体模型</param>
        /// <returns></returns>
        public async Task<int> DelDic(SysDictionary dic)
        {
            string str = $"update SysDictionary set IsDel = @isdel, UpdateTime = @updatetime, UpdateUser = @updateuser where id = @id";
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                isdel = "1", //是否删除
                updatetime = Db.GetDate(), //更改时间
                updateuser = dic.UpdateUser, //更改人
                id = dic.Id //id
            });
            return i;
        }
 
        /// <summary>
        /// 编辑字典信息
        /// </summary>
        /// <param name="dic">数据字典实体模型</param>
        /// <returns></returns>
        public async Task<int> ExitDic(SysDictionary dic)
        {
            //获取是否拥有相同字典名称
            if (!string.IsNullOrEmpty(dic.DictName))
            {
                var dicno = Db.Queryable<SysDictionary>().First(a => a.DictName == dic.DictName);
                if (dicno != null)
                {
                    throw new Exception("获取是否拥有相同字典名称");
                }
            }
            else
            {
                throw new Exception("字典名称为空,请核实!");
            }
            string str = "update SysDictionary set DictNo = @dictno, DictName = @dictname, ParentNo = @parentno, Ord = @ord, IsPublic = @ispublic, IsEdit = @isedit, IsAdd = @isadd, UpdateTime = @updatetime, UpdateUser = @updateuser where id = @id";
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                dictno = dic.DictNo, //字典编号
                dictname = dic.DictName, //字典名称
                parentno = dic.ParentNo, //父级字典号
                ord = dic.Ord, //显示顺序
                //level = dic.Level, //层级
                ispublic = dic.IsPublic, //是否公开
                isedit = dic.IsEdit, //允许编辑
                isadd = dic.IsAdd, //允许添加
                updatetime = Db.GetDate(), //更改时间
                updateuser = dic.UpdateUser, //更改人
                id = dic.Id //id
            });
            return i;
        }
    }
 
    internal class NewClass
    {
        public NewClass()
        {
        }
 
        public override bool Equals(object obj)
        {
            return obj is NewClass other;
        }
 
        public override int GetHashCode()
        {
            return 0;
        }
    }
}