Esempio n. 1
0
        public async Task <MessageModel <string> > Put([FromBody] SysUserInfoDto sysUserInfo)
        {
            // 这里使用事务处理

            var data = new MessageModel <string>();

            try
            {
                _unitOfWork.BeginTran();

                if (sysUserInfo != null && sysUserInfo.uID > 0)
                {
                    // 无论 Update Or Add , 先删除当前用户的全部 U_R 关系
                    var usreroles = (await _userRoleServices.Query(d => d.UserId == sysUserInfo.uID)).Select(d => d.Id.ToString()).ToArray();
                    if (usreroles.Any())
                    {
                        var isAllDeleted = await _userRoleServices.DeleteByIds(usreroles);

                        if (!isAllDeleted)
                        {
                            return(Failed("服务器更新异常"));
                        }
                    }

                    // 然后再执行添加操作
                    if (sysUserInfo.RIDs.Count > 0)
                    {
                        var userRolsAdd = new List <UserRole>();
                        sysUserInfo.RIDs.ForEach(rid =>
                        {
                            userRolsAdd.Add(new UserRole(sysUserInfo.uID, rid));
                        });

                        await _userRoleServices.Add(userRolsAdd);
                    }

                    data.success = await _sysUserInfoServices.Update(_mapper.Map <SysUserInfo>(sysUserInfo));

                    _unitOfWork.CommitTran();

                    if (data.success)
                    {
                        data.msg      = "更新成功";
                        data.response = sysUserInfo?.uID.ObjToString();
                    }
                }
            }
            catch (Exception e)
            {
                _unitOfWork.RollbackTran();
                _logger.LogError(e, e.Message);
            }

            return(data);
        }
        public async Task <BaseResponse <string> > Put([FromBody] UserInfo userInfo)
        {
            // 这里使用事务处理

            var data = new BaseResponse <string>();

            try
            {
                if (userInfo != null && userInfo.Id > 0)
                {
                    if (userInfo.RIDs.Count > 0)
                    {
                        _unitOfWork.BeginTran();
                        // 无论 Update Or Add , 先删除当前用户的全部 U_R 关系
                        var usreroles = (await _userRoleServices.Query(d => d.UserId == userInfo.Id)).Select(d => d.RoleId.ToString()).ToArray();
                        if (usreroles.Count() > 0)
                        {
                            var isAllDeleted = await _userRoleServices.DeleteByIds(usreroles);
                        }

                        // 然后再执行添加操作
                        var userRolsAdd = new List <UserRole>();
                        userInfo.RIDs.ForEach(async rid =>
                        {
                            await _userRoleServices.Add(new UserRole((int)userInfo.Id, rid));;
                        });
                    }
                    var success = await _sysUserInfoServices.Update(userInfo);

                    _unitOfWork.CommitTran();

                    if (success)
                    {
                        data.Msg = "更新成功";
                    }
                }
            }
            catch (Exception ex)
            {
                _unitOfWork.RollbackTran();
                //_logger.LogError(ex, ex.Message);
            }
            return(data);
        }