fix 处理已知问题

This commit is contained in:
2024-06-22 18:19:13 +08:00
parent cf04fb01ad
commit 49201f2e49
16 changed files with 219 additions and 318 deletions

View File

@@ -1,83 +0,0 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Dto;
using Kurisu.AspNetCore.Utils;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 缓存山西档案数据
/// </summary>
[RecurringJob(nameof(SyncShanxiGainfoJob), "0 0 3 * * ?", Name = "缓存山西档案数据")]
public class SyncShanxiGainfoJob : BaseJob
{
private readonly RedisCache _redisCache;
public SyncShanxiGainfoJob(IServiceProvider serviceProvider, RedisCache redisCache) : base(serviceProvider)
{
_redisCache = redisCache;
}
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var key = "gainfo:shanxi";
await _redisCache.KeyDeleteAsync(key);
var gainfos = await DbContext.Queryable<GainfoEntity>()
.LeftJoin<DepartmentEntity>((x, d) => x.DepartmentNo == d.No)
.Where(x => x.Factory == "2401")
.Select((x, d) => new GainfoOpenResponse
{
Code = x.Code,
DepartmentNo = x.DepartmentNo,
DepartmentName = d.Name,
Name = x.Name,
Gender = x.Gender,
Status = GainfoStatusEnumType.Working,
})
.ToListAsync();
foreach (var item in gainfos)
{
await _redisCache.ListLeftPushAsync(key, item);
}
var gainfoLeaves = await DbContext.Queryable<GainfoEntity>().AS("GainfoLeave")
.LeftJoin<DepartmentEntity>((x, d) => x.DepartmentNo == d.No)
.Where(x => x.Factory == "2401")
.Select((x, d) => new GainfoOpenResponse
{
Code = x.Code,
DepartmentNo = x.DepartmentNo,
DepartmentName = d.Name,
Name = x.Name,
Gender = x.Gender,
Status = GainfoStatusEnumType.Leave,
})
.ToListAsync();
foreach (var item in gainfoLeaves)
{
await _redisCache.ListLeftPushAsync(key, item);
}
var departmentKey = "department:all";
await _redisCache.KeyDeleteAsync(departmentKey);
var departments = await DbContext.Queryable<DepartmentEntity>()
.Select((x) => new DepartmentOpenResponse
{
Name = x.Name,
No = x.No,
PNo = x.PNo,
})
.ToListAsync();
foreach (var item in departments)
{
await _redisCache.ListLeftPushAsync(departmentKey, item);
}
}
}

View File

@@ -1,108 +0,0 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Sys.Entities;
using Dlhis.ScheduledTask.Attributes;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Microsoft.AspNetCore.Identity;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 用户创建
/// </summary>
[RecurringJob(nameof(UpdateUserPasswordJob), "0 0 6 * * ?", Name = "用户创建")]
public class UpdateUserPasswordJob : BaseJob
{
public UpdateUserPasswordJob(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var identityDbContext = DbContext.ChangeDb("identity");
var whitelist = new List<string>() { "admin", "WB-0741", "WB-1091", "0123456" };
var users = await identityDbContext.Queryable<UserEntity>()
.Where(x => !whitelist.Contains(x.UserName))
.ToListAsync();
var gainfos = await DbContext.Queryable<GainfoEntity>().Where(x => !whitelist.Contains(x.Code)).ToListAsync();
var hasher = new PasswordHasher<UserEntity>();
foreach (var gainfo in gainfos)
{
if (gainfo.IdNo.IsEmpty()) continue;
try
{
var password = gainfo.IdNo.TakeLast(6).Join("");
var user = users.FirstOrDefault(x => x.UserName == gainfo.Code);
if (user != null) continue;
user = new UserEntity
{
UserName = gainfo.Code,
NormalizedUserName = gainfo.Code.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 AddUserClaim(identityDbContext, id, "jinko-code", gainfo.Code);
await AddUserClaim(identityDbContext, id, "name", gainfo.Name);
if (string.IsNullOrEmpty(gainfo.Factory)) continue;
var userTenant = await identityDbContext.Queryable<UserTenantEntity>().Where(x => x.UserId == user.Id).FirstAsync();
if (userTenant == null)
{
userTenant = new UserTenantEntity
{
UserId = user.Id,
TenantCode = gainfo.Factory
};
await identityDbContext.InsertAsync(userTenant);
}
else
{
userTenant.TenantCode = gainfo.Factory;
await identityDbContext.UpdateAsync(userTenant);
}
// else
// {
// password = hasher.HashPassword(user, password);
// user.PasswordHash = password;
// await identityDbContext.UpdateAsync(user);
// }
}
catch (Exception)
{
//ignore
}
}
}
private async Task AddUserClaim(IDbContext dbContext, int userId, string type, string value)
{
var claim = await dbContext.Queryable<UserClaimEntity>().Where(x => x.UserId == userId).FirstAsync(x => x.ClaimType == type);
if (claim == null)
{
claim = new UserClaimEntity()
{
UserId = userId,
ClaimType = type,
ClaimValue = value
};
await dbContext.InsertAsync(claim);
}
else
{
claim.ClaimValue = value;
await dbContext.UpdateAsync(claim);
}
}
}