예제 #1
0
        /// <summary>
        /// 添加或修改角色
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="RoleName">新名字</param>
        /// <param name="ParentId">父级组</param>
        /// <param name="appid">appid</param>
        /// <returns></returns>
        public ActionResult Save(int Id = 0, string RoleName = "", int?ParentId = null, string appid = "")
        {
            ParentId = ParentId == 0 ? null : ParentId;
            Role role = RoleBll.GetById(Id);

            if (role != null)
            {
                //修改
                if (RoleBll.Any(a => a.RoleName.Equals(RoleName) && !a.RoleName.Equals(role.RoleName)))
                {
                    return(ResultData(null, false, $"{RoleName} 角色已经存在!"));
                }

                role.RoleName = RoleName;
                role.ParentId = ParentId;
                bool b = RoleBll.UpdateEntitySaved(role);
                return(ResultData(null, b, b ? "修改成功" : "修改失败!"));
            }

            //添加
            if (RoleBll.Any(a => a.RoleName.Equals(RoleName)))
            {
                return(ResultData(null, false, $"{RoleName} 角色已经存在!"));
            }

            role = new Role()
            {
                RoleName = RoleName, ParentId = ParentId
            };
            if (!string.IsNullOrEmpty(appid) && ClientAppBll.Any(a => a.AppId.Equals(appid)))
            {
                var app = ClientAppBll.GetFirstEntity(a => a.AppId.Equals(appid));
                app.Roles.Add(role);
                bool b = ClientAppBll.UpdateEntitySaved(app);
                return(ResultData(null, b, b ? "角色添加成功!" : "角色添加失败!"));
            }

            role = RoleBll.AddEntitySaved(role);
            return(role != null?ResultData(role, true, "角色添加成功!") : ResultData(null, false, "角色添加失败!"));
        }