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 { private static object Lock = new object(); private MacAuthService():base(MacAuthDao.GetInstance()) { } private static MacAuthService Instance = null; /// /// 获取单例的方法 /// /// 角色服务的单例实体 public static MacAuthService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new MacAuthService(); } } } return Instance; } public List 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(); foreach(var x in macAuths){ var m = new MacAuthEntity(); EntityPropHelper.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; } } } /// /// 批量保存角色-菜单,将删除传入roleId的所有绑定后批量添加 /// /// 角色Id /// 角色菜单列表 /// 异常错误消息 /// 生成的角色菜单列表 public List Save(int roleId, List 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(); 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(); 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(); } } } } }