Administrator
3 天以前 8bd4e49b8ffb54b8039c528f347f3996e766d8a9
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Model.ModelDto.SysDto;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
using Model.ModelVm.SysVm;
using System.Linq;
using SqlSugar;
using WMS.DAL;
using WMS.Entity.Context;
 
namespace WMS.BLL.SysServer
{
    /// <summary>
    /// 菜单服务实践
    /// </summary>
    public class MenuServer : DbHelper<SysFunctionMenu>, IMenuServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        /// <summary>
        /// 依赖注入
        /// </summary>
        public IMenuRepository _menu { get; set; }
        readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="menu">菜单仓储</param>
        /// <param name="mapper">automapper</param>
        public MenuServer(IMenuRepository menu, IMapper mapper) : base(Db)
        {
            _menu = menu;
            _mapper = mapper;
        }
 
 
        /// <summary>
        /// 获取菜单信息列表
        /// </summary>
        /// <param name="menuDto">菜单Dto</param>
        /// <returns></returns>
        public async Task<List<FunctionMenuVm>> GetMenuList(string MenuName, string pMenuNo, string MenuNo, string level = "")
        {
            //string str = "select menu1.Id,menu1.MenuNo,menu1.MenuName,menu2.MenuName ParentName,menu1.ParentNo,menu1.Ord,menu1.Url,menu1.level,menu1.IsEnable,menu1.Demo,menu1.IsDel,menu1.CreateTime,menu1.CreateUser,menu1.UpdateTime,menu1.UpdateUser,user1.RealName CreateUserName from SysFunctionMenu menu1 left join sysfunctionmenu menu2 on menu1.parentNo = menu2.MenuNo join SysUserInfor user1 on menu1.CreateUser = user1.id  where menu1.IsDel = '0' and menu1.IsEnable = '0' ";
            var queryAble = Db.Queryable<SysFunctionMenu>()
                .LeftJoin<SysFunctionMenu>((a, b) => a.ParentNo == b.MenuNo)
                .LeftJoin<SysUserInfor>((a, b, c) => a.CreateUser == c.Id);
            var exp = Expressionable.Create<SysFunctionMenu, SysFunctionMenu, SysUserInfor>();
            //判断页面菜单
            if (!string.IsNullOrEmpty(MenuNo))
            {
                //判断层级是否为空
                if (!string.IsNullOrEmpty(level))
                {
                    //判断层级
                    if (level == "1")
                    {
                        //页面
                        exp.And((a, b, c) => a.MenuNo == MenuNo);
                        //str += $" and menu1.MenuNo = '{MenuNo}'";
                    }
                    if (level == "2")
                    {
                        //按钮
                        exp.And((a, b, c) => a.ParentNo == MenuNo);
                        //str += $" and menu1.ParentNo = '{MenuNo}'";
                    }
                    if (level == "0")
                    {
                        //模块
                        exp.And((a, b, c) => a.ParentNo == MenuNo);
                        //str += $" and menu1.ParentNo = '{MenuNo}'";
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(MenuName))
                    {
                        exp.And((a, b, c) => a.ParentNo == MenuNo);
                        //str += $" and menu1.ParentNo = '{MenuNo}'";
                    }
                    else
                    {
                        exp.And((a, b, c) => a.ParentNo == MenuNo || a.MenuNo == MenuNo);
                        //str += $" and (menu1.ParentNo = '{MenuNo}' or menu1.MenuNo = '{MenuNo}')";
 
                    }
                }
            }
            //判断模块菜单
            if (!string.IsNullOrEmpty(pMenuNo))
            {
                //判断层级是否为空
                if (!string.IsNullOrEmpty(level))
                {
                    //判断层级
                    if (level == "1")
                    {
                        //页面
                        exp.And((a, b, c) => a.ParentNo == pMenuNo);
                        //str += $" and menu1.ParentNo = '{pMenuNo}'";
                    }
                    if (level == "2")
                    {
                        //按钮
                        exp.And((a, b, c) => SqlFunc.Subqueryable<SysFunctionMenu>().Where(d => d.ParentNo == pMenuNo && a.ParentNo == d.MenuNo).Any());
                        //str += $" and menu1.ParentNo in (select MenuNo from SysFunctionMenu where ParentNo = '{pMenuNo}')";
                    }
                    if (level == "0")
                    {
                        //模块
                        exp.And((a, b, c) => a.MenuNo == pMenuNo);
                        //str += $" and menu1.MenuNo= '{pMenuNo}'";
                    }
                }
                else
                {
                    //判断菜单名称是否为空
                    if (!string.IsNullOrEmpty(MenuName) && string.IsNullOrEmpty(MenuNo))
                    {
                        queryAble.Where(a => SqlFunc.Subqueryable<SysFunctionMenu>().Where(d => d.ParentNo == pMenuNo && a.ParentNo == d.MenuNo).Any());
                        //str += $" and menu1.ParentNo in (select MenuNo from SysFunctionMenu where ParentNo = '{pMenuNo}')";
                    }
                    else
                    {
                        exp.And((a, b, c) => a.ParentNo == pMenuNo || a.MenuNo == pMenuNo || SqlFunc.Subqueryable<SysFunctionMenu>().Where(d => d.ParentNo == pMenuNo && a.ParentNo == d.MenuNo).Any());
                        //str += $" and  ( menu1.ParentNo = '{pMenuNo}' or menu1.MenuNo = '{pMenuNo}' or menu1.ParentNo in (select MenuNo from SysFunctionmenu where ParentNo = {pMenuNo}) )";
                    }
                }
            }
            //判断菜单名称与编号是否为空
            if (!string.IsNullOrEmpty(MenuName))
            {
                exp.And((a, b, c) => a.MenuName.Contains(MenuName));
                //str += $" and menu1.MenuName like '%{MenuName}%'";
                //判断父级菜单是否为空
                if (!string.IsNullOrEmpty(pMenuNo) && string.IsNullOrEmpty(MenuNo))
                {
                    exp.Or((a, b, c) => SqlFunc.Subqueryable<SysFunctionMenu>().Where(e => (e.ParentNo == pMenuNo || e.MenuNo == pMenuNo && MenuName.Contains(MenuName)) && e.MenuNo == a.MenuNo).Any());
                    //str += $" or menu1.MenuNo in (select MenuNo from SysFunctionMenu where ( ParentNo = '{pMenuNo}' or MenuNo='{pMenuNo}' ) and MenuName like '%{MenuName}%')";
                }
            }
            //判断菜单层级
            if (!string.IsNullOrEmpty(level))
            {
                exp.And((a, b, c) => a.Level == level);
                //str += $" and menu1.Level = '{level}'";
            }
            exp.And((a, b, c) => a.IsDel == "0");
            return await queryAble.Where(exp.ToExpression()).OrderBy((a, b, c) => SqlFunc.ToInt32(a.MenuNo)).OrderBy((a, b, c) => a.Ord)
                .Select<FunctionMenuVm>((a, b, c) => new FunctionMenuVm()
                {
                    Id = a.Id,
                    MenuNo = a.MenuNo,
                    MenuName = a.MenuName,
                    ParentNo = a.ParentNo,
                    ParentName = b.MenuName,
                    Ord = a.Ord,
                    Url = a.Url,
                    Level = a.Level,
                    IsEnable = a.IsEnable,
                    Demo = a.Demo,
                    CreateTime = a.CreateTime,
                    CreateUser = a.CreateUser,
                    CreateUserName = c.RealName,
                }, true).ToListAsync();
        }
 
        /// <summary>
        /// 根据层级获取菜单列表
        /// </summary>
        /// <param name="level"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<FunctionMenuVm> GetMenuListForRight(string level, string UserId)
        {
            var userEntry = Db.Ado.SqlQuery<SysUserInfor>($"select * from SysUserInfor where Id ={UserId}").FirstOrDefault();
            string sqlStr = "select * from SysFunctionMenu where IsDel='0' ";
            if (!string.IsNullOrEmpty(level))
            {
                sqlStr += $" and level='{level}' ";
            }
            //分配权限只能分配当前登录用户有的权限
            if (userEntry != null && userEntry.UserName.ToUpper() != "ADMIN")
            {
                sqlStr += $"and MenuNo in (select MenuNo from SysRoleRight where RoleNo='{userEntry.RoleNo}') ";
            }
            List<FunctionMenuVm> menulist = Db.Ado.SqlQuery<FunctionMenuVm>(sqlStr);
            return menulist;
        }
 
        /// <summary>
        /// 获取模块菜单
        /// </summary>
        /// <returns></returns>
        public List<SysFunctionMenu> GetParentMenuList()
        {
            List<SysFunctionMenu> menulist = _menu.GetParentMenuList();
            return menulist;
        }
 
        /// <summary>
        ///  根据id获取菜单信息列表
        /// </summary>
        /// <param name="menuid">菜单id</param>
        /// <returns></returns>
        public SysFunctionMenu GetMenuListById(int menuid)
        {
            SysFunctionMenu menu = _menu.GetMenuListById(menuid);
            return menu;
        }
 
        /// <summary>
        ///  根据编号获取菜单信息列表
        /// </summary>
        /// <param name="menuno">菜单编号</param>
        /// <returns></returns>
        public int GetMenuListByNo(string menuno)
        {
            string str = $"select * from SysFunctionMenu where MenuNo = '{menuno}'";
            List<SysFunctionMenu> menulist = _menu.GetMenuListByNo(str);
            return menulist.Count;
        }
 
        /// <summary>
        /// 新增菜单信息
        /// </summary>
        /// <param name="menudto">菜单DTO模型</param>
        /// <param name="userId">当前操作用户id</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> InsertMenu(FunctionMenuDto menudto)
        {
            //捕获异常
            try
            {
                //模型映射
                SysFunctionMenu menu = _mapper.Map<SysFunctionMenu>(menudto);
                //是否可用
                menu.IsEnable = "0";
                int i = 0;
                int count = GetMenuListByNo(menu.MenuNo);
                //判断菜单号是否唯一
                if (count > 0)
                {
                    i = 3;
                }
                else if (count == 0)
                {
                    //验证层级 0:模块、1:页面、2:按钮
                    switch (menu.Level)
                    {
                        case "0":
                            if (!string.IsNullOrWhiteSpace(menu.ParentNo))
                            {
                                throw new Exception("模块类型为模块,父级菜单应为空");
                            }
                            break;
                        case "1":
                            if (string.IsNullOrWhiteSpace(menu.ParentNo))
                            {
                                throw new Exception("模块类型为页面,父级菜单不能为空");
                            }
 
                            var menuData = Db.Queryable<SysFunctionMenu>().First(m => m.MenuNo == menu.ParentNo);
                            if (menuData == null)
                            {
                                throw new Exception("未查询到父级菜单信息,请核实");
                            }
 
                            if (menuData.Level != "0")
                            {
                                throw new Exception("当前模块类型是页面,父级菜单类型应为模块");
                            }
                            break;
                        case "2":
                            if (string.IsNullOrWhiteSpace(menu.ParentNo))
                            {
                                throw new Exception("模块类型为页面,父级菜单不能为空");
                            }
 
                            var menuData2 = Db.Queryable<SysFunctionMenu>().First(m => m.MenuNo == menu.ParentNo);
                            if (menuData2 == null)
                            {
                                throw new Exception("未查询到父级菜单信息,请核实");
                            }
 
                            if (menuData2.Level != "1")
                            {
                                throw new Exception("当前模块类型是按钮,父级菜单类型应为页面");
                            }
                            break;
                        default:
                            throw new Exception("模块类型异常");
                    }
                    i = await _menu.InsertMenu(menu);
                }
                return i;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception(ex.Message);
            }
 
        }
 
        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="id">菜单id</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> DeleteMenu(SysFunctionMenu menu)
        {
            //捕获异常
            try
            {
                int i = await _menu.DeleteMenu(menu);
                return i;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("删除菜单信息异常", ex);
            }
 
        }
 
        /// <summary>
        /// 编辑菜单信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> UpdateMenu(FunctionMenuVm menuvm)
        {
            //捕获异常
            try
            {
                SysFunctionMenu menu = _mapper.Map<SysFunctionMenu>(menuvm);
                //验证层级 0:模块、1:页面、2:按钮
                switch (menu.Level)
                {
                    case "0":
                        if (!string.IsNullOrWhiteSpace(menu.ParentNo))
                        {
                            throw new Exception("模块类型为模块,父级菜单应为空");
                        }
                        break;
                    case "1":
                        if (string.IsNullOrWhiteSpace(menu.ParentNo))
                        {
                            throw new Exception("模块类型为页面,父级菜单不能为空");
                        }
 
                        var menuData = Db.Queryable<SysFunctionMenu>().First(m => m.MenuNo == menu.ParentNo);
                        if (menuData == null)
                        {
                            throw new Exception("未查询到父级菜单信息,请核实");
                        }
 
                        if (menuData.Level != "0")
                        {
                            throw new Exception("当前模块类型是页面,父级菜单类型应为模块");
                        }
                        break;
                    case "2":
                        if (string.IsNullOrWhiteSpace(menu.ParentNo))
                        {
                            throw new Exception("模块类型为页面,父级菜单不能为空");
                        }
 
                        var menuData2 = Db.Queryable<SysFunctionMenu>().First(m => m.MenuNo == menu.ParentNo);
                        if (menuData2 == null)
                        {
                            throw new Exception("未查询到父级菜单信息,请核实");
                        }
 
                        if (menuData2.Level != "1")
                        {
                            throw new Exception("当前模块类型是按钮,父级菜单类型应为页面");
                        }
                        break;
                    default:
                        throw new Exception("模块类型异常");
                }
                int i = await _menu.UpdateMenu(menu);
                return i;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception(ex.Message);
            }
 
        }
 
        /// <summary>
        /// 根据父级菜单号获取菜单信息
        /// </summary>
        /// <param name="MenuNo">菜单号</param>
        /// <returns></returns>
        public List<FunctionMenuVm> GetMenuByParentNo(string MenuNo)
        {
            List<FunctionMenuVm> menulist = _menu.GetMenuByParentNo(MenuNo);
            return menulist;
        }
    }
}