abpvnext给基础表abpuser增加扩展表扩展注册用户业务

业务背景:

我的需求是现在有个微信公众号程序,用户会在那里注册用户,然后我想要当前注册的用户名字(电话号码)默认创建一个租户,然后这个用户默认是这个租户的超级管理员。

经过各种试错和查询,具体业务流程应该是如此:先拿用户提交的电话号码或者用户名啥的作为租户名称创建租户(必须要让这个过程有独立的事物,不然他不能提前执行得到租户id),这时候系统已经完成了租户创建,默认管理员账户admin和默认管理角色admin的创建,并且账户admin默认是绑定管理员角色。

然后拿着租户id,用用户提交的用户信息新建一个用户,隶属于当前租户,角色隶属于新建的管理员角色。最后再给自己新建的用户业务扩展表也增加一条相同用户ID的数据。

下面直接上代码

using Volo.Abp;
using Volo.Abp.Application.Dtos;
using JQ.TAHM.Domain.Shared; 
using JQ.TAHM.Domain;
using JQ.TAHM.Application.Contracts;
using Volo.Abp.Users;
using Volo.Abp.Identity;
using Volo.Abp.Data;
using System.Transactions;
using Volo.Abp.TenantManagement;
using Volo.Abp.ObjectExtending;
using Microsoft.AspNetCore.Identity;
using Senparc.NeuChar.Helpers;
using Volo.Abp.Uow;
using IdentityRole = Volo.Abp.Identity.IdentityRole;
using JQ.TAHM.BasicManagement.Roles;
using JQ.TAHM.BasicManagement.Roles.Dtos;
using Volo.Abp.MultiTenancy;

namespace JQ.TAHM.Application;

/// <summary>
/// tahmAbpusers_extension
/// </summary>
public class tahmAbpusers_extensionAppService : ApplicationService, ItahmAbpusers_extensionAppService
{
    private readonly tahmAbpusers_extensionManager _tahmAbpusersExtensionManager;
    private readonly IdentityUserManager _identityUserManager;
    private readonly IIdentityUserAppService  _identityUserAppService;
    //private readonly IIdentityUserRepository _identityuserRepository;
    //private readonly ITenantRepository _tenantrepository;
    private readonly ITenantManager _tenantmanager;
    private readonly ITenantAppService _tenantAppService;
    private readonly IIdentityRoleRepository _identityrolerepository;
    private readonly IIdentityRoleAppService _identityroleappservice;
    private readonly IRoleAppService _iroleappservice;
    private readonly IDataFilter _dataFilter;

    protected readonly IUnitOfWorkManager _myunitofworkmanager;

    public tahmAbpusers_extensionAppService(tahmAbpusers_extensionManager tahmAbpusersExtensionManager, IdentityUserManager identityUserManager, IIdentityUserAppService identityUserAppService, ITenantManager tenantmanager, ITenantAppService tenantAppService, IIdentityRoleRepository identityrolerepository, IIdentityRoleAppService identityroleappservice, IUnitOfWorkManager myunitofworkmanager, IRoleAppService iroleappservice, IDataFilter dataFilter)
    {
        _tahmAbpusersExtensionManager = tahmAbpusersExtensionManager;
        _identityUserManager = identityUserManager;
        _identityUserAppService = identityUserAppService;
        _tenantmanager = tenantmanager;
        _tenantAppService = tenantAppService;
        _identityrolerepository = identityrolerepository;
        _identityroleappservice = identityroleappservice;
        _myunitofworkmanager = myunitofworkmanager;
        _iroleappservice = iroleappservice;
        _dataFilter = dataFilter;
    }

     

    public async Task<tahmAbpusers_extensionDto> CreateOrUpdateAsync(UpdatetahmAbpusers_extensionInput input)
    { 
        tahmAbpusers_extensionDto myabpuserextdto = new tahmAbpusers_extensionDto(); 
        if (input.Id.IsNullOrEmpty())
        {
            TenantDto mytenant_ret = new TenantDto();
            TenantCreateDto tenant_dto = new TenantCreateDto();
            tenant_dto.AdminEmailAddress = input.PhoneNumber + "@qq.com";
            tenant_dto.AdminPassword = input.PasswordHash;
            tenant_dto.Name = input.PhoneNumber;
            using (var myuow = _myunitofworkmanager.Begin(requiresNew: true, isTransactional: false))
            {
                mytenant_ret = await _tenantAppService.CreateAsync(tenant_dto);
                //await CurrentUnitOfWork.SaveChangesAsync();
                await myuow.CompleteAsync();
            }
            //上面这一段是使用单独工作单元事物创建租户。
             
            List<IdentityRole> myRoleretList = null;
            using (_dataFilter.Disable<IMultiTenant>())
            {
                //禁用租户过滤条件下取得所有角色表数据。 
                myRoleretList = await _identityrolerepository.GetListAsync();
            }
            IdentityRole myrole = null;
            //abp封装得比较死,没办法传递租户ID去查询上面创建的租户的角色,所以只能全部查出来再到列表里用linq去查询
            myrole = myRoleretList.Where(x => x.TenantId == mytenant_ret.Id).FirstOrDefault();
            if (mytenant_ret != null&&!mytenant_ret.Id.IsNullOrEmpty()&& myrole != null)
            { 
                Guid userId = GuidGenerator.Create();
                input.TenantId = mytenant_ret.Id.ToString();
                Volo.Abp.Identity.IdentityUser myuser = new Volo.Abp.Identity.IdentityUser(userId, input.UserName, input.UserName + "@qq.com", mytenant_ret.Id);
                myuser.Surname = input.UserName;
                myuser.SetPhoneNumber(input.PhoneNumber, true);
                List<IdentityUserRole> myuserroleList= new List<IdentityUserRole>();
                myuser.AddRole(myrole.Id);  //给已经创建的用户实体加入角色ID
                var myretuser = await _identityUserManager.CreateAsync(myuser, input.PasswordHash); 
                //await _identityUserAppService.UpdateAsync(input.UserId, input.UserInfo);
                if (myretuser != null&& myretuser.Succeeded==true)
                {  
                    //给用户表加完用户之后同步给自己加的用户扩展表也加一行同样用户ID的数据
                    myabpuserextdto = await _tahmAbpusersExtensionManager.CreateAsync(
                    userId,
                    input.TenantId,
                    input.UserName,
                    input.Name,
                    input.Surname,
                    input.UserDeptId,
                    input.UserRoleId,
                    input.Col1,
                    input.Col2,
                    input.Col3,
                    input.Col4,
                    input.Col5,
                    input.Col6,
                    input.Col7,
                    input.Col8,
                    input.Col9,
                    input.Col10,
                    input.Col11,
                    input.Col12,
                    input.Col13,
                    input.Col14,
                    input.Col15,
                    input.Col16,
                    input.Col17,
                    input.Col18,
                    input.Col19,
                    input.Col20,
                 input.PhoneNumber,
                 input.PasswordHash
                           );
                }
            }
        }
        else
        {
            myabpuserextdto = await _tahmAbpusersExtensionManager.UpdateAsync(
         Guid.Parse(input.Id),
         input.TenantId,
         input.UserName,
         input.Name,
         input.Surname,
         input.UserDeptId,
         input.UserRoleId,
         input.Col1,
         input.Col2,
         input.Col3,
         input.Col4,
         input.Col5,
         input.Col6,
         input.Col7,
         input.Col8,
         input.Col9,
         input.Col10,
         input.Col11,
         input.Col12,
         input.Col13,
         input.Col14,
         input.Col15,
         input.Col16,
         input.Col17,
         input.Col18,
         input.Col19,
         input.Col20,
         input.PhoneNumber,
         input.PasswordHash
         );
        }
        return myabpuserextdto;
    }
}

Abp框架提供了一些基本的数据访问方法,如GetAll, Get, Insert, Update, Delete等,它们可以满足大多数应用程序的需求。但是,如果需要执行复杂的SQL查询或操作,可以使用Dapper来执行原生SQL。Dapper是一个轻量级的ORM框架,可以在Abp应用程序中与Entity Framework Core一起使用。以下是一个使用Dapper执行原生SQL查询的示例: 1. 安装Dapper NuGet包 可以使用NuGet包管理器或在项目文件中手动添加以下条目来安装Dapper: ``` Install-Package Dapper ``` 2. 创建DapperRepository 我们可以创建一个名为DapperRepository的仓储类,它将使用Dapper执行原生SQL查询或操作。以下是一个简单的DapperRepository类的示例: ``` public class DapperRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity<int> { private readonly IDbConnection _dbConnection; public DapperRepository(IDbConnection dbConnection) { _dbConnection = dbConnection; } public async Task<List<TEntity>> GetAllListAsync() { return (await _dbConnection.GetListAsync<TEntity>()).ToList(); } public async Task<TEntity> GetAsync(int id) { return await _dbConnection.GetAsync<TEntity>(id); } public async Task<TEntity> InsertAsync(TEntity entity) { entity.Id = await _dbConnection.InsertAsync(entity); return entity; } public async Task<TEntity> UpdateAsync(TEntity entity) { await _dbConnection.UpdateAsync(entity); return entity; } public async Task DeleteAsync(int id) { await _dbConnection.DeleteAsync<TEntity>(id); } public async Task<List<TEntity>> QueryAsync(string sql, object param = null) { return (await _dbConnection.QueryAsync<TEntity>(sql, param)).ToList(); } public async Task<TEntity> QueryFirstOrDefaultAsync(string sql, object param = null) { return await _dbConnection.QueryFirstOrDefaultAsync<TEntity>(sql, param); } } ``` 3. 注册DapperRepository 在应用程序的Startup.cs文件中,我们可以使用依赖注入将DapperRepository注册到容器中: ``` services.AddScoped(typeof(IRepository<>), typeof(DapperRepository<>)); ``` 4. 使用DapperRepository执行查询 现在我们可以在应用程序中使用DapperRepository来执行原生SQL查询或操作。以下是一个使用DapperRepository执行查询的示例: ``` public class MyService : IMyService { private readonly IRepository<MyEntity> _repository; public MyService(IRepository<MyEntity> repository) { _repository = repository; } public async Task<List<MyEntity>> GetEntitiesWithComplexQueryAsync() { string sql = "SELECT * FROM MyEntities WHERE MyProperty = @myParam"; var parameters = new { myParam = "someValue" }; return await _repository.QueryAsync(sql, parameters); } } ``` 在上面的示例中,我们使用DapperRepository执行了一个带有参数的原生SQL查询,并将结果作为MyEntity对象列返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值