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 System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace iWareDataCore.RBAC.Service { public class RoleContentService : CommonService { private static object Lock = new object(); private RoleContentService() : base(RoleContentDao.GetInstance()) { } private static RoleContentService Instance = null; /// /// 获取单例的方法 /// /// 角色-菜单服务的单例实体 public static RoleContentService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new RoleContentService(); } } } return Instance; } /// /// 批量保存角色-菜单,将删除传入roleId的所有绑定后批量添加 /// /// 角色Id /// 角色菜单列表 /// 异常错误消息 /// 生成的角色菜单列表 public List Save(int roleId, List roleContents, out string msg) { msg = ""; using (var dbModel = new DbModelCore()) { try { var ones = dbModel.RBACRoleContents.Where(x => x.roleid == roleId).ToList(); foreach (var one in ones) { dbModel.RBACRoleContents.Remove(one); } var rbacRoleContents = new List(); foreach (var roleUser in roleContents) { rbacRoleContents.Add(roleUser.ToOrm()); } foreach (var one in rbacRoleContents) { dbModel.RBACRoleContents.Add(one); } dbModel.SaveChanges(); var res = new List(); foreach (var one in rbacRoleContents) { res.Add(new RoleContentEntity(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(); } } } } }