/// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="dto"></param>
        public void SetUserRole(UserSetRoleDto dto)
        {
            var roleKey  = SysRelevanceEnum.SysUser_SysRole.ToString();
            var roleList = dbClint.Queryable <SysRelevance>().Where(o => o.Key == roleKey && o.FirstId == dto.UserId && o.IsDeleted == false).Select(o => new
            {
                o.RelevanceId,
                UserId = o.FirstId,
                RoleId = o.SecondId
            }).Distinct().ToArray();

            List <SysRelevance> addList = new List <SysRelevance>();
            List <long>         delList = new List <long>();
            var addRoles = dto.RoleId.Except(roleList.Select(r => r.RoleId).ToList()).ToList();

            if (addRoles.Count > 0)
            {
                addRoles.ForEach(r =>
                {
                    addList.Add(new SysRelevance()
                    {
                        Key            = roleKey,
                        FirstId        = dto.UserId,
                        SecondId       = r,
                        CreateDate     = DateTime.Now,
                        CreateUserId   = _userManager.UserId,
                        CreateUserName = _userManager.UserName,
                        IsDeleted      = false
                    });
                });
            }
            var delRoles = roleList.Select(r => r.RoleId).ToList().Except(dto.RoleId).ToList();

            if (delRoles.Count > 0)
            {
                delList.AddRange(roleList.Where(r => delRoles.Contains(r.RoleId)).Select(r => r.RelevanceId).ToList());
            }
            var result = dbClint.Ado.UseTran(() =>
            {
                if (addList.Count > 0)
                {
                    dbClint.Insertable(addList).ExecuteCommand();
                }
                if (delList.Count > 0)
                {
                    dbClint.Updateable <SysRelevance>().SetColumns(r => new SysRelevance()
                    {
                        DeleteDate     = DateTime.Now,
                        DeleteUserId   = _userManager.UserId,
                        DeleteUserName = _userManager.UserName,
                        IsDeleted      = true
                    }).Where(r => delList.Contains(r.RelevanceId)).ExecuteCommand();
                }
            });

            if (!result.IsSuccess)
            {
                throw Oops.Oh(result.ErrorMessage);
            }
        }
示例#2
0
 public bool UserSetRole([FromBody] UserSetRoleDto dto)
 {
     _sysRelevanceService.SetUserRole(dto);
     return(true);
 }