示例#1
0
文件: RoleService.cs 项目: udkin/WJ
        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool Update(JObject data, ref string errorMsg)
        {
            using (SqlSugar.SqlSugarClient db = DbInstance)
            {
                try
                {
                    int roleId = data["Id"].ToObject <int>();
                    if (IsExits(p => p.Role_Name == data["Role_Name"].ToString().Trim() && p.Id != roleId && p.Role_State == 1))
                    {
                        errorMsg = "存在相同角色名称";
                    }
                    else
                    {
                        string menuIds = GetMenuId(data["Role_Menu"]);
                        if (string.IsNullOrWhiteSpace(menuIds))
                        {
                            errorMsg = "";
                            return(false);
                        }

                        BeginTran();

                        WJ_T_Role role = GetSingle(p => p.Id == roleId);
                        role.Role_Name = data["Role_Name"].ToString();
                        role.Role_Sort = data["Role_Sort"].ToObject <int>();
                        bool flag = Update(role);

                        if (flag)
                        {
                            //string oldMenuIds = RoleMenuService.Instance.GetRoleMenuId(id);

                            RoleMenuService.Instance.Delete(roleId, db); //删除旧的角色树记录

                            foreach (var menuId in menuIds.TrimEnd(',').Split(','))
                            {
                                WJ_T_RoleMenu roleMneu = new WJ_T_RoleMenu();
                                roleMneu.RoleId = roleId;
                                roleMneu.MenuId = Convert.ToInt32(menuId);

                                if (RoleMenuService.Instance.Add(roleMneu))
                                {
                                    RollbackTran();
                                    return(false);
                                }
                            }

                            CommitTran();
                            return(true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    RollbackTran();
                    LogHelper.DbServiceLog(ex.Message);
                    errorMsg = ex.Message;
                }
                return(false);
            }
        }
示例#2
0
文件: RoleService.cs 项目: udkin/WJ
        /// <summary>
        /// 增加角色信息
        /// </summary>
        /// <param name="data">提交的表单数据</param>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public bool Add(int userId, JObject data, ref string errorMsg)
        {
            try
            {
                if (IsExits(p => p.Role_Name == data["Role_Name"].ToString().Trim() && p.Role_State == 1))
                {
                    errorMsg = "存在相同角色名称";
                }
                else
                {
                    WJ_T_Role role = new WJ_T_Role();
                    role.Role_Name       = data["Role_Name"].ToString();
                    role.Role_Sort       = data["Role_Sort"].ToObject <int>();
                    role.Role_Creator    = userId;
                    role.Role_CreateTime = DateTime.Now;
                    role.Role_State      = 1;

                    string menuIds = GetMenuId(data["Role_Menu"]);
                    if (string.IsNullOrWhiteSpace(menuIds))
                    {
                        errorMsg = "";
                        return(false);
                    }

                    BeginTran();
                    int roleId = AddReturnIdentity(role);

                    if (roleId > 0)
                    {
                        foreach (var menuId in menuIds.TrimEnd(',').Split(','))
                        {
                            WJ_T_RoleMenu roleMneu = new WJ_T_RoleMenu();
                            roleMneu.RoleId = roleId;
                            roleMneu.MenuId = Convert.ToInt32(menuId);

                            if (!RoleMenuService.Instance.Add(roleMneu))
                            {
                                RollbackTran();
                                return(false);
                            }
                        }

                        CommitTran();
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.DbServiceLog(ex.Message);
                errorMsg = ex.Message;
            }

            RollbackTran();
            return(false);
        }