fix 处理已知问题
This commit is contained in:
@@ -17,6 +17,7 @@ public class GainfoUpdateService : IGainfoUpdateService
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly ILogger<GainfoUpdateService> _logger;
|
||||
private readonly ITenantApi _tenantApi;
|
||||
private readonly IIdentityUserSyncService _identityUserSyncService;
|
||||
private readonly IGainfoPermissionApi _gainfoPermissionApi;
|
||||
private readonly IDevicePermissionApi _devicePermissionApi;
|
||||
private readonly IJinkoSapService _jinkoSapService;
|
||||
@@ -26,6 +27,7 @@ public class GainfoUpdateService : IGainfoUpdateService
|
||||
public GainfoUpdateService(IDbContext dbContext,
|
||||
ILogger<GainfoUpdateService> logger,
|
||||
ITenantApi tenantApi,
|
||||
IIdentityUserSyncService identityUserSyncService,
|
||||
IGainfoPermissionApi gainfoPermissionApi,
|
||||
IDevicePermissionApi devicePermissionApi,
|
||||
IJinkoSapService jinkoSapService)
|
||||
@@ -34,6 +36,7 @@ public class GainfoUpdateService : IGainfoUpdateService
|
||||
_dbContext.Client.QueryFilter.Clear();
|
||||
_logger = logger;
|
||||
_tenantApi = tenantApi;
|
||||
_identityUserSyncService = identityUserSyncService;
|
||||
_gainfoPermissionApi = gainfoPermissionApi;
|
||||
_devicePermissionApi = devicePermissionApi;
|
||||
_jinkoSapService = jinkoSapService;
|
||||
@@ -85,9 +88,9 @@ public class GainfoUpdateService : IGainfoUpdateService
|
||||
|
||||
if (modifiedPersons.Count > 0)
|
||||
{
|
||||
var d = modifiedPersons.First();
|
||||
_logger.LogInformation("差异信息:{json}", d.ToJson());
|
||||
_logger.LogInformation("差异信息:{json}", (await _dbContext.Queryable<GainfoSapEntity>().Where(x => x.ename == d.ZWORKCODE).FirstAsync())?.ToJson());
|
||||
// var d = modifiedPersons.First();
|
||||
// _logger.LogInformation("差异信息:{json}", d.ToJson());
|
||||
// _logger.LogInformation("差异信息:{json}", (await _dbContext.Queryable<GainfoSapEntity>().Where(x => x.ename == d.ZWORKCODE).FirstAsync())?.ToJson());
|
||||
|
||||
await SavePersonAsync(modifiedPersons);
|
||||
}
|
||||
@@ -214,26 +217,25 @@ public class GainfoUpdateService : IGainfoUpdateService
|
||||
if (item.isowner == "T")
|
||||
{
|
||||
await _dbContext.InsertAsync(info);
|
||||
await _identityUserSyncService.SyncAccountAsync(info.Code, info.Name, info.IdNo, info.Factory);
|
||||
await _devicePermissionApi.RePermissionUsersInfoAsync(new PermissionUsersInfoDto
|
||||
{
|
||||
UserNos = new List<string> { info.No },
|
||||
PermissionInfos = new Dictionary<string, List<UserTenantDeviceCodeDto>>
|
||||
{
|
||||
[info.No] = new List<UserTenantDeviceCodeDto>()
|
||||
[info.No] = new()
|
||||
}
|
||||
});
|
||||
|
||||
return info.No;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!await _dbContext.Queryable<GainfoLeaveEntity>().AnyAsync(x => x.Code == item.ename))
|
||||
{
|
||||
await _dbContext.Client.Insertable(info).AS("GainfoLeave").ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
return null;
|
||||
if (!await _dbContext.Queryable<GainfoLeaveEntity>().AnyAsync(x => x.Code == item.ename))
|
||||
{
|
||||
await _dbContext.Client.Insertable(info).AS("GainfoLeave").ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private GainfoEntity GainfoSapToGainfo(GainfoSapEntity item)
|
||||
@@ -274,31 +276,29 @@ public class GainfoUpdateService : IGainfoUpdateService
|
||||
private async Task<string> UpdatePersonAsync(GainfoSapEntity item)
|
||||
{
|
||||
await _dbContext.Updateable<GainfoSapEntity>().SetColumns(g => new GainfoSapEntity
|
||||
{
|
||||
PERNR = item.PERNR,
|
||||
gname = item.gname,
|
||||
phone = item.phone,
|
||||
appellation = item.appellation,
|
||||
isowner = item.isowner,
|
||||
remark = item.remark,
|
||||
email = item.email,
|
||||
WERKS = item.WERKS,
|
||||
KOSTL = item.KOSTL,
|
||||
STELL = item.STELL,
|
||||
Z_ZJ = item.Z_ZJ,
|
||||
ZZ_ZD = item.ZZ_ZD,
|
||||
MNTXT = item.MNTXT,
|
||||
MASSN = item.MASSN,
|
||||
ZSTATUS = item.ZSTATUS,
|
||||
ZLZRQ = item.ZLZRQ,
|
||||
STELL_NM = item.STELL_NM,
|
||||
ZZ_KLX = item.ZZ_KLX,
|
||||
createdate = item.createdate,
|
||||
ZZ_GZDJDNM = item.ZZ_GZDJDNM,
|
||||
ZZ_GZDCQNM = item.ZZ_GZDCQNM,
|
||||
})
|
||||
.Where(x => x.ename == item.ename)
|
||||
.ExecuteCommandAsync();
|
||||
{
|
||||
PERNR = item.PERNR,
|
||||
gname = item.gname,
|
||||
phone = item.phone,
|
||||
appellation = item.appellation,
|
||||
isowner = item.isowner,
|
||||
remark = item.remark,
|
||||
email = item.email,
|
||||
WERKS = item.WERKS,
|
||||
KOSTL = item.KOSTL,
|
||||
STELL = item.STELL,
|
||||
Z_ZJ = item.Z_ZJ,
|
||||
ZZ_ZD = item.ZZ_ZD,
|
||||
MNTXT = item.MNTXT,
|
||||
MASSN = item.MASSN,
|
||||
ZSTATUS = item.ZSTATUS,
|
||||
ZLZRQ = item.ZLZRQ,
|
||||
STELL_NM = item.STELL_NM,
|
||||
ZZ_KLX = item.ZZ_KLX,
|
||||
createdate = item.createdate,
|
||||
ZZ_GZDJDNM = item.ZZ_GZDJDNM,
|
||||
ZZ_GZDCQNM = item.ZZ_GZDCQNM,
|
||||
}).Where(x => x.ename == item.ename).ExecuteCommandAsync();
|
||||
|
||||
//获取当前在职人员
|
||||
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == item.ename).FirstAsync();
|
||||
@@ -309,30 +309,30 @@ public class GainfoUpdateService : IGainfoUpdateService
|
||||
var workplace = _tenants.FirstOrDefault(x => x.Name == item.ZZ_GZDJDNM)?.Code ?? item.ZZ_GZDJDNM;
|
||||
var factory = _tenants.FirstOrDefault(x => x.Name == item.ZZ_GZDCQNM)?.Code ?? item.ZZ_GZDCQNM;
|
||||
await _dbContext.Updateable<GainfoEntity>().SetColumns(g => new GainfoEntity
|
||||
{
|
||||
SapNo = item.PERNR,
|
||||
Name = item.gname,
|
||||
PositionNo = item.STELL,
|
||||
Grade = item.ZZ_ZD,
|
||||
Title = item.Z_ZJ,
|
||||
Phone = item.phone,
|
||||
DepartmentNo = item.appellation,
|
||||
Remark = item.remark,
|
||||
Degree = item.email,
|
||||
Werks = item.WERKS,
|
||||
CostId = item.KOSTL,
|
||||
EntryDate = item.createdate,
|
||||
ResignationDate = item.ZLZRQ,
|
||||
Workplace = workplace,
|
||||
Factory = factory
|
||||
})
|
||||
.Where(x => x.Code == item.ename)
|
||||
.ExecuteCommandAsync();
|
||||
{
|
||||
SapNo = item.PERNR,
|
||||
Name = item.gname,
|
||||
PositionNo = item.STELL,
|
||||
Grade = item.ZZ_ZD,
|
||||
Title = item.Z_ZJ,
|
||||
Phone = item.phone,
|
||||
DepartmentNo = item.appellation,
|
||||
Remark = item.remark,
|
||||
Degree = item.email,
|
||||
Werks = item.WERKS,
|
||||
CostId = item.KOSTL,
|
||||
EntryDate = item.createdate,
|
||||
ResignationDate = item.ZLZRQ,
|
||||
Workplace = workplace,
|
||||
Factory = factory
|
||||
}).Where(x => x.Code == item.ename).ExecuteCommandAsync();
|
||||
await _identityUserSyncService.SyncAccountAsync(gainfo.Code, gainfo.Name, gainfo.IdNo, factory);
|
||||
}
|
||||
else
|
||||
{
|
||||
var info = GainfoSapToGainfo(item);
|
||||
await _dbContext.InsertAsync(info);
|
||||
await _identityUserSyncService.SyncAccountAsync(info.Code, info.Name, info.IdNo, info.Factory);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
using Dlhis.Entity.Sys.Entities;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace Dlhis.ScheduledTask.Common.Services.Implements;
|
||||
|
||||
public class IdentityUserSyncService : IIdentityUserSyncService
|
||||
{
|
||||
private readonly IDbContext _identityDbContext;
|
||||
|
||||
public IdentityUserSyncService(IDbContext dbContext)
|
||||
{
|
||||
_identityDbContext = dbContext.ChangeDb("identity");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SyncAccountAsync(string userName, string name, string idNo, string tenant)
|
||||
{
|
||||
if (tenant.IsEmpty()) return;
|
||||
|
||||
try
|
||||
{
|
||||
var password = idNo.TakeLast(6).Join("");
|
||||
|
||||
var user = await _identityDbContext.Queryable<UserEntity>().SingleAsync(x => x.UserName == userName);
|
||||
if (user == null)
|
||||
{
|
||||
var hasher = new PasswordHasher<UserEntity>();
|
||||
|
||||
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);
|
||||
user.Id = id;
|
||||
await AddUserClaim(_identityDbContext, id, "jinko-code", userName);
|
||||
await AddUserClaim(_identityDbContext, id, "name", name);
|
||||
}
|
||||
|
||||
|
||||
//todo 考虑角色?
|
||||
var userTenants = await _identityDbContext.Queryable<UserTenantEntity>().Where(x => x.UserId == user.Id).ToListAsync();
|
||||
if (userTenants.Count is 0 or 1)
|
||||
{
|
||||
var userTenant = userTenants.FirstOrDefault();
|
||||
if (userTenant == null)
|
||||
{
|
||||
userTenant = new UserTenantEntity
|
||||
{
|
||||
UserId = user.Id,
|
||||
TenantCode = tenant
|
||||
};
|
||||
await _identityDbContext.InsertAsync(userTenant);
|
||||
}
|
||||
else
|
||||
{
|
||||
userTenant.TenantCode = tenant;
|
||||
await _identityDbContext.UpdateAsync(userTenant);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
|
||||
//************************************************************************** help method **************************************************************************************
|
||||
|
||||
private async Task AddUserClaim(IDbContext dbContext, int userId, string type, string value)
|
||||
{
|
||||
var claim = await _identityDbContext.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user