chengsc
2025-04-12 3a3c50e8a4f1bca47daaadd03f4138a5d4a2227c
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
using Model.ModelDto.SysDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
 
namespace WMS.BLL.SysServer
{
    public class RoleWareHouseServer: IRoleWareHouseServer
    {
 
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        public List<SysRoleWareHouse> GetRoleWareHouseOneListById(int id)
        {
            try
            {
                var role = Db.Queryable<SysRoles>().First(m => m.IsDel == "0" && m.Id == id);
                if (role == null) 
                {
                    throw new Exception("没有找到角色信息");
                }
                var roleWare = Db.Queryable<SysRoleWareHouse>().Where(m => m.RoleNo == role.RoleNo && m.IsDel == "0").ToList();
                return roleWare;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 获取仓库信息列表
        /// </summary>
        /// <param name="str">sql语句</param>
        /// <returns></returns>
        public List<SysWareHouse> GetHouseList(string UserId)
        {
            var userEntry = Db.Ado.SqlQuery<SysUserInfor>($"select * from SysUserInfor where Id ={UserId}").FirstOrDefault();
            //获取全部菜单信息
            string strmenu = "select * from SysWareHouse where IsDel = '0'";
             
            //分配权限只能分配当前登录用户有的权限
            if (userEntry != null && userEntry.UserName.ToUpper() != "ADMIN")
            {
                strmenu += $" and WareHouseNo in (select WareHouseNo from SysRoleWareHouse where RoleNo='{userEntry.RoleNo}')";
            }
            strmenu += "  order by WareHouseNo asc";
            List<SysWareHouse> houselist = Db.Ado.SqlQuery<SysWareHouse>(strmenu);
            return houselist;
        }
 
        /// <summary>
        /// 新增角色权限信息
        /// </summary>
        /// <param name="WareHouseNo">仓库号</param>
        /// <param name="RoleNo">角色号</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> InsertRoleWareHouse(string WareHouseNo, string RoleNo, string userId)
        {
            //捕获异常
            try
            {
                //将菜单号进行分割 存储数组中
                var arr = WareHouseNo.Split(',');
                int i = 0;
                //查询该角色是否拥有权限
                List<SysRoleWareHouse> rolelist = await Db.Queryable<SysRoleWareHouse>().Where(m=>m.IsDel == "0" && m.RoleNo == RoleNo).ToListAsync();  
                //有
                if (rolelist.Count > 0)
                {
                    //删除该角色所有菜单信息 
                    i = await Db.Deleteable(rolelist).ExecuteCommandAsync();
                }
                //无
                else if (rolelist.Count == 0)
                {
                    i = 1;
                }
                //开始新增权限
                if (i > 0)
                {
                    
                    //循环将菜单数组分别加入到数据模型中
                    for (int j = 0; j < arr.Count(); j++)
                    {
                        if (string.IsNullOrWhiteSpace(arr[j]) || arr[j] == ",")
                        {
                            continue;
                        }
                        SysRoleWareHouse roleright = new SysRoleWareHouse();
                        roleright.RoleNo = RoleNo;//角色号
                        roleright.WareHouseNo = arr[j];//仓库号
                        roleright.CreateUser = Convert.ToInt32(userId);//创建人
 
                        //成功 将菜单号和角色号新增为新的数据
                        i += await Db.Insertable(roleright).ExecuteCommandAsync();
                    }
                     
                    //返回
                    return i;
 
                }
                else
                {
                    return i;
                }
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("新增角色权限信息异常", ex);
            }
        }
 
 
    }
}