schangxiang@126.com
2025-09-17 a32e5a5b296cab5ccc20953ca4e801ca4f27bd85
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
using iWareCommon.Common.EnumType;
using iWareCommon.Common.Service;
using iWareCommon.Utils;
using iWareDataCore.ORM;
using iWareDataCore.Properties;
using iWareDataCore.RBAC.Dao;
using iWareDataCore.RBAC.Entity;
using iWareDataCore.RBAC.EnumType;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareDataCore.RBAC.Service
{
   public class MacAuthService: CommonService<MacAuthEntity, RBACMacAuth, DbModelCore>
    {
 
        private static object Lock = new object();
 
        private MacAuthService():base(MacAuthDao.GetInstance()) { }
 
        private static MacAuthService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>角色服务的单例实体</returns>
        public static MacAuthService GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new MacAuthService();
                    }
                }
            }
            return Instance;
        }
 
        public List<MacAuthEntity> GetAuth(string mac, out string msg)
        {
            msg = "";
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    var macAuths = dbModel.RBACMacAuths.Where(x => x.macaddress == mac).ToList();
                    if (macAuths.Count < 1)
                    {
                        msg = "当前用户无权限";
                        return null;
                    }
                    var res = new List<MacAuthEntity>();
                   
                    foreach(var x in macAuths){
                        var m = new MacAuthEntity();
                        EntityPropHelper<MacAuthEntity, RBACMacAuth>.CopyProp(x,m , MacAuthEntity.GetColumnMap());
                        res.Add(m);
                    }
 
                    return res;
                    
                }
                catch (DbEntityValidationException ex)
                {
                    var errs = ex.EntityValidationErrors.SelectMany(validationResult => validationResult.ValidationErrors).Select(m => m.ErrorMessage);
                    msg = string.Join(", ", errs);
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Delete", msg);
                    return null;
                }
 
                catch (Exception ex)
                {
                    msg = ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Delete", ex.Message);
                    return null;
                }
            }
        }
 
 
        /// <summary>
        /// 批量保存角色-菜单,将删除传入roleId的所有绑定后批量添加
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="roleContents">角色菜单列表</param>
        /// <param name="msg">异常错误消息</param>
        /// <returns>生成的角色菜单列表</returns>
        public List<MacAuthEntity> Save(int roleId, List<MacAuthEntity> roleContents, out string msg)
        {
            msg = "";
            using (var dbModel = new DbModelCore())
            {
                try
                {
 
                    var ones = dbModel.RBACMacAuths.Where(x => x.macuserid == roleId).ToList();
                    var mac = dbModel.RBACMacUsers.FirstOrDefault(x => x.id == roleId);
                    var contents = dbModel.RBACContents.Where(x => x.type == (int)EContentType.用于CS端的菜单).ToList();
                    foreach (var one in ones)
                    {
                        dbModel.RBACMacAuths.Remove(one);
                    }
 
                    var rbacRoleContents = new List<RBACMacAuth>();
 
                    foreach (var roleUser in roleContents)
                    {
                        roleUser.MacAddress = mac.macaddress;
                        var content = contents.FirstOrDefault(x => x.id == roleUser.ContentId);
                        roleUser.ContentName = content.name;
                        roleUser.ContentCode = content.url;
                        rbacRoleContents.Add(roleUser.ToOrm());
                    }
 
                    foreach (var one in rbacRoleContents)
                    {
                        dbModel.RBACMacAuths.Add(one);
                    }
 
                    dbModel.SaveChanges();
 
                    var res = new List<MacAuthEntity>();
 
                    foreach (var one in rbacRoleContents)
                    {
                        res.Add(new MacAuthEntity(one));
                    }
 
                    return res;
                }
                catch (Exception ex)
                {
                    msg = ex.HResult == (int)EDbError.记录已存在 ? EDbError.记录已存在.ToString() : ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Save", msg);
                    return new List<MacAuthEntity>();
                }
            }
        }
    }
}