using iWareCommon.Common.Entity; 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.Data.Entity.Validation; using System.Linq; using System.Text; using System.Threading.Tasks; namespace iWareDataCore.RBAC.Service { public class ContentService : CommonService { private static object Lock = new object(); private ContentService() : base(ContentDao.GetInstance()) { } private static ContentService Instance = null; /// /// 获取单例的方法 /// /// 菜单服务的单例实体 public static ContentService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new ContentService(); } } } return Instance; } public override int Delete(int id, out string msg) { msg = ""; using (var dbModel = new DbModelCore()) { try { var roleContents = dbModel.RBACRoleContents.Where(x => x.contentid == id).ToList(); foreach (var roleContent in roleContents) { dbModel.RBACRoleContents.Remove(roleContent); } var one = dbModel.RBACContents.First(x => x.id == id); var count = dbModel.RBACContents.Where(x => x.parentid == id).ToList().Count; if (count > 0) { msg = "该菜单有子菜单,请先删除子菜单!"; return -1; } dbModel.RBACContents.Remove(one); dbModel.SaveChanges(); return id; } 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 -1; } catch (Exception ex) { msg = ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Delete", ex.Message); return -1; } } } /// /// 判断souce能否移动到target /// /// 起始位置 /// 目标位置 /// 异常错误信息 /// 能否移动 private bool CanMove(int sourceId, int targetId, DbModelCore dbModel, out string msg) { msg = ""; var contents = ContentDao.GetInstance().QueryByParam(new QueryParam { Order = new Dictionary { { "ContentIndex", "ASC" } } }, dbModel); if (contents == null) { return false; } var ids = new List(); foreach (var content in contents) { ids.Add(content.Id); } var childrenDictionary = TreeHelper.GetChildrenDictionary(contents); if (!ids.Contains(sourceId) && !sourceId.Equals(-1)) { msg = "起始菜单不存在"; return false; } if (!ids.Contains(targetId) && !targetId.Equals(-1)) { msg = "目标菜单不存在"; return false; } if (TreeHelper.IsChildOrEq(targetId, sourceId, childrenDictionary)) { msg = "不能将菜单移动到本身或其子菜单下"; return false; } return true; } public override int Update(ContentEntity t, out string msg) { using (var dbModel = new DbModelCore()) { try { if (!CanMove(t.Id, t.ParentId, dbModel, out msg)) { return -1; } return ContentDao.GetInstance().Update(t, dbModel); } 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(), "Update", msg); return -1; } catch (Exception ex) { msg = ex.HResult == (int)EDbError.记录已存在 ? "此纪录已存在" : ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Update", msg); return -1; } } } } }