From 64a63e2e49bf1bff792cee5f62f7d105d5f2b81e Mon Sep 17 00:00:00 2001 From: liguoyang Date: Sun, 10 Mar 2024 00:55:51 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=90=8C=E6=AD=A5=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sys/Entities/UserTenantEntity.cs | 1 + .../CommonJobs/TempCreateUserJob.cs | 99 +++++++++++++++++++ Src/Dlhis.ScheduledTask/Packs/JobPack.cs | 7 +- Src/Dlhis.ScheduledTask/Packs/SqlSugarPack.cs | 24 ++++- Src/Dlhis.ScheduledTask/appsettings.json | 1 + 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs diff --git a/Src/Dlhis.Entity/Sys/Entities/UserTenantEntity.cs b/Src/Dlhis.Entity/Sys/Entities/UserTenantEntity.cs index 4f50204f..e98b06ba 100644 --- a/Src/Dlhis.Entity/Sys/Entities/UserTenantEntity.cs +++ b/Src/Dlhis.Entity/Sys/Entities/UserTenantEntity.cs @@ -8,6 +8,7 @@ namespace Dlhis.Entity.Sys.Entities; [SugarTable(TableName = "UserTenants")] public class UserTenantEntity { + [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] public int Id { get; set; } public int UserId { get; set; } diff --git a/Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs b/Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs new file mode 100644 index 00000000..15093082 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs @@ -0,0 +1,99 @@ +using Dlhis.Entity.Basic.Entity; +using Dlhis.Entity.Sys.Entities; +using Kurisu.AspNetCore.Authentication.Options; +using Kurisu.Core.DataAccess.Entity; +using Kurisu.SqlSugar.Services; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Options; + +namespace Dlhis.ScheduledTask.CommonJobs +{ + public class TempCreateUserJob + { + private readonly IDbContext _dbContext; + private readonly ILogger _logger; + private readonly IdentityServerOptions _identityServerOptions; + private readonly IHttpClientFactory _httpClientFactory; + + public TempCreateUserJob( + IDbContext dbContext, + ILogger logger, + IOptions options, + IHttpClientFactory httpClientFactory) + { + _dbContext = dbContext; + _logger = logger; + _httpClientFactory = httpClientFactory; + _identityServerOptions = options.Value; + } + + public async Task RunAsync() + { + _dbContext.Client.QueryFilter.ClearAndBackup(); + try + { + var identityDbContext = _dbContext.ChangeDb("identity"); + + var userNames = await identityDbContext.Queryable().Select(x => x.UserName).ToListAsync(); + var newUser = await _dbContext.Queryable() + .Where(x => !string.IsNullOrEmpty(x.Code)) + .Where(x => !userNames.Contains(x.Code)) + .Select(x => new + { + x.Code, + x.Factory, + x.Name, + }) + .ToListAsync(); + + var httpClient = _httpClientFactory.CreateClient(); + httpClient.BaseAddress = new Uri(_identityServerOptions.Authority); + + var hasher = new PasswordHasher(); + foreach (var item in newUser) + { + var userName = item.Code; + var password = item.Code; + var factory = item.Factory; + + if (password.Length > 6) + { + password = string.Join("", password.TakeLast(6)); + } + else + { + password = password.PadLeft(6, '0'); + } + var user = new UserEntity + { + UserName = userName, + NormalizedUserName = userName.ToUpper(), + ConcurrencyStamp = Guid.NewGuid().ToString(), + SecurityStamp = Guid.NewGuid().ToString(), + }; + + password = hasher.HashPassword(user, password); + user.PasswordHash = password; + var id = (int)await identityDbContext.InsertReturnIdentityAsync(user); + + await identityDbContext.InsertAsync(new UserTenantEntity() { UserId = id, TenantCode = factory }); + await identityDbContext.InsertAsync(new List + { + new UserClaimEntity{ UserId = id , ClaimType ="preferred_username", ClaimValue = item.Name}, + new UserClaimEntity{ UserId = id , ClaimType ="jinko-code", ClaimValue = item.Code}, + }); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "创建用户失败:{message}", ex.Message); + } + finally + { + _dbContext.Client.QueryFilter.Restore(); + } + } + } + + +} diff --git a/Src/Dlhis.ScheduledTask/Packs/JobPack.cs b/Src/Dlhis.ScheduledTask/Packs/JobPack.cs index e74aa037..d7391825 100644 --- a/Src/Dlhis.ScheduledTask/Packs/JobPack.cs +++ b/Src/Dlhis.ScheduledTask/Packs/JobPack.cs @@ -11,7 +11,6 @@ public class JobPack : BaseAppPack public override int Order => 999; public override bool IsBeforeUseRouting { get; } - public override void Invoke(IServiceProvider serviceProvider) { RecurringJob.AddOrUpdate("SyncSap", s => s.RunAsync(), "0 0 2 * * ?", new RecurringJobOptions @@ -53,5 +52,11 @@ public class JobPack : BaseAppPack { TimeZone = TimeZoneInfo.Local }); + + RecurringJob.AddOrUpdate("TempCreateUser", s => s.RunAsync(), "0 0 5 * * ?", new RecurringJobOptions + { + TimeZone = TimeZoneInfo.Local + }); + } } diff --git a/Src/Dlhis.ScheduledTask/Packs/SqlSugarPack.cs b/Src/Dlhis.ScheduledTask/Packs/SqlSugarPack.cs index 24a3f080..6ba87c85 100644 --- a/Src/Dlhis.ScheduledTask/Packs/SqlSugarPack.cs +++ b/Src/Dlhis.ScheduledTask/Packs/SqlSugarPack.cs @@ -1,5 +1,6 @@ using Kurisu.AspNetCore.Startup; using Kurisu.SqlSugar.Extensions; +using SqlSugar; namespace Dlhis.ScheduledTask.Packs; @@ -9,6 +10,27 @@ public class SqlSugarPack : BaseAppPack public override void ConfigureServices(IServiceCollection services) { - services.AddSqlSugar(); + services.AddSqlSugar(sp => + { + var configuration = sp.GetService(); + + return new List + { + new ConnectionConfig + { + ConfigId = "identity", + + ConnectionString = configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value, + DbType = DbType.MySql, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + + MoreSettings = new ConnMoreSettings + { + DisableNvarchar = true, + } + }, + }; + }); } } diff --git a/Src/Dlhis.ScheduledTask/appsettings.json b/Src/Dlhis.ScheduledTask/appsettings.json index 7b148077..caf7f060 100644 --- a/Src/Dlhis.ScheduledTask/appsettings.json +++ b/Src/Dlhis.ScheduledTask/appsettings.json @@ -17,6 +17,7 @@ "DefaultConnectionString": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=yikatong;charset=utf8mb4;", "SlaveConnectionString": [], "FaceConnectionString": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=face;charset=utf8mb4;", + "IdentityConnectionString": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=identity;charset=utf8mb4;", "Timeout": 30, "SlowSqlTime": 1, "EnableSqlLog": false