diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs index d4bd5f66..8e43ca33 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs @@ -107,7 +107,7 @@ public class DeviceGroupService : IDeviceGroupService await _devicePermissionService.GenerateDeviceTasksAsync(async () => { var relationships = await _dbContext.Queryable().Where(x => x.GroupCode == request.GroupCode).ToListAsync(); - await _dbContext.DeleteReallyAsync(relationships); + await _dbContext.DeleteAsync(relationships); var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity { diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs index 58884ed9..30366caa 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs @@ -49,7 +49,7 @@ public class DevicePermissionService : IDevicePermissionService .Where(x => x.TenantId == tenantId) .ToListAsync(); - await _dbContext.DeleteReallyAsync(p); + await _dbContext.DeleteAsync(p); //保存配置的权限 if (request.Codes.IsPresent()) @@ -189,6 +189,7 @@ public class DevicePermissionService : IDevicePermissionService { throw new Exception("系统维护中,暂不可操作"); } + //获取所有人的权限 if (userNos.IsEmpty()) userNos = await GetPermissionUserNosAsync(); @@ -273,7 +274,7 @@ public class DevicePermissionService : IDevicePermissionService _logger.LogInformation("{tenant}任务产生数量{count}", tenantId, waitTasks.Count); if (waitTasks.Any()) { - // await _dbContext.InsertAsync(waitTasks); + // await _dbContext.InsertAsync(waitTasks); } } @@ -423,10 +424,7 @@ public class DevicePermissionService : IDevicePermissionService public async Task GenerateDeviceTasksAsync(string userNo, DeviceTaskEnumType taskType) { var deviceCodes = await GetAssignDeviceCodesAsync(userNo); - if (!deviceCodes.Any()) - { - return; - } + if (!deviceCodes.Any()) return; var devices = await _dbContext.Queryable() .Where(x => x.Enable) diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoGroupService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoGroupService.cs index 7d3740b8..37f1cdf8 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoGroupService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoGroupService.cs @@ -98,7 +98,7 @@ public class GainfoGroupService : IGainfoGroupService await _devicePermissionService.GenerateDeviceTasksAsync(async () => { var relations = await _dbContext.Queryable().Where(x => x.GroupCode == request.GroupCode).ToListAsync(); - await _dbContext.DeleteReallyAsync(relations); + await _dbContext.DeleteAsync(relations); request.UserNos = await _dbContext.Queryable().Where(x => request.UserNos.Contains(x.No) || request.UserNos.Contains(x.Code)) .Select(x => x.No) diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs index 4aa4a34c..d474a652 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/OrderService.cs @@ -1,4 +1,5 @@ -using Dlhis.Basic.Api; +using System.Data; +using Dlhis.Basic.Api; using Dlhis.Canteen.Dishes.Request; using Dlhis.Canteen.Setting.Services; using Dlhis.Entity.Basic.Entity; @@ -39,7 +40,7 @@ public class OrderService : BaseOrderService, IOrderService { return await AddAsync(request); } - + public async Task> AddAsync(OrderAddRequest request) { @@ -247,7 +248,6 @@ public class OrderService : BaseOrderService, IOrderService Version = 0, UserNo = order.UserNo, UserCode = order.UserCode, - }; await DbContext.InsertAsync(gainfoMeal); @@ -269,7 +269,6 @@ public class OrderService : BaseOrderService, IOrderService //写入记录 var balanceRecord = order.Adapt(); await DbContext.InsertAsync(balanceRecord); - }, 5); } @@ -592,30 +591,40 @@ public class OrderService : BaseOrderService, IOrderService var allowNoBalance = await GetSettingAsync(order.TenantId, CanteenSettingItemEnumType.AllowNoBalance); - await CommonHelper.RetryAsync(async () => + //降级到读已提交 + await DbContext.Client.Ado.BeginTranAsync(IsolationLevel.ReadCommitted); + try { - if (!allowNoBalance) + await CommonHelper.RetryAsync(async () => { - var gainfoMeal = await DbContext.Queryable().Where(x => x.UserNo == order.UserNo).FirstAsync(); - //金额+- - gainfoMeal.Balance += order.Amount; - if (gainfoMeal.Balance < 0) + if (!allowNoBalance) { - throw new UserFriendlyException("余额不足,无法消费"); + var gainfoMeal = await DbContext.Queryable().Where(x => x.UserNo == order.UserNo).SingleAsync(); + //金额+- + gainfoMeal.Balance += order.Amount; + + (gainfoMeal.Balance < 0).ThrowIfTrue("余额不足,无法消费"); + + await DbContext.Updateable() + .SetColumns(x => x.Balance == gainfoMeal.Balance) + .SetColumns(x => x.Version == x.Version + 1) + .SetColumns(x => x.ModifiedTime == DateTime.Now) + .Where(x => x.UserNo == order.UserNo && x.Version == gainfoMeal.Version) //必须与查询时的version一致 + .ExecuteCommandThrowVersionAsync(); } - await DbContext.Updateable() - .SetColumns(x => x.Balance == gainfoMeal.Balance) - .SetColumns(x => x.Version == x.Version + 1) - .SetColumns(x => x.ModifiedTime == DateTime.Now) - .Where(x => x.UserNo == order.UserNo && x.Version == gainfoMeal.Version) //必须与查询时的version一致 - .ExecuteCommandThrowVersionAsync(); - } + //写入记录 + var balanceRecord = order.Adapt(); + await DbContext.InsertAsync(balanceRecord); + }, 5); - //写入记录 - var balanceRecord = order.Adapt(); - await DbContext.InsertAsync(balanceRecord); - }, 5); + await DbContext.Client.Ado.CommitTranAsync(); + } + catch (Exception) + { + await DbContext.Client.Ado.RollbackTranAsync(); + throw; + } } public async Task ReCalcOfflineAsync(ReCalcOfflineRequest request) @@ -641,7 +650,6 @@ public class OrderService : BaseOrderService, IOrderService Version = 0, UserNo = gainfo.No, UserCode = gainfo.Code, - }; await DbContext.InsertAsync(gainfoMeal); @@ -668,7 +676,7 @@ public class OrderService : BaseOrderService, IOrderService }, 5); } } - + /// /// 根据二维码获取消费用户 /// diff --git a/Src/Dlhis.Entity/Basic/Entity/GainfoGroupRelationshipEntity.cs b/Src/Dlhis.Entity/Basic/Entity/GainfoGroupRelationshipEntity.cs index 360e7f29..11bf1b05 100644 --- a/Src/Dlhis.Entity/Basic/Entity/GainfoGroupRelationshipEntity.cs +++ b/Src/Dlhis.Entity/Basic/Entity/GainfoGroupRelationshipEntity.cs @@ -1,4 +1,5 @@ -using Kurisu.AspNetCore.DataAccess.SqlSugar; +using Kurisu.AspNetCore.DataAccess.Entity; +using Kurisu.AspNetCore.DataAccess.SqlSugar; using SqlSugar; namespace Dlhis.Entity.Basic.Entity @@ -7,7 +8,7 @@ namespace Dlhis.Entity.Basic.Entity /// 档案人员组关系 /// [SugarTable(TableName = "GainfoGroupRelationship", TableDescription = "档案人员组关系")] - public class GainfoGroupRelationshipEntity : SugarBaseEntity + public class GainfoGroupRelationshipEntity : SugarBaseEntity , ISoftDeleted { /// /// 分组code @@ -19,5 +20,9 @@ namespace Dlhis.Entity.Basic.Entity /// [SugarColumn(ColumnDescription = "人员档案号")] public string UserNo { get; set; } + + + /// + public bool IsDeleted { get; set; } } } \ No newline at end of file diff --git a/Src/Dlhis.Entity/Device/Entities/DeviceGroupRelationshipEntity.cs b/Src/Dlhis.Entity/Device/Entities/DeviceGroupRelationshipEntity.cs index b93ffc62..74d42d26 100644 --- a/Src/Dlhis.Entity/Device/Entities/DeviceGroupRelationshipEntity.cs +++ b/Src/Dlhis.Entity/Device/Entities/DeviceGroupRelationshipEntity.cs @@ -8,7 +8,7 @@ namespace Dlhis.Entity.Device.Entities; /// 设备组/设备关系 /// [SugarTable(TableName = "DeviceGroupRelationship", TableDescription = "设备组/设备关系")] -public class DeviceGroupRelationshipEntity : SugarBaseEntity, ITenantId +public class DeviceGroupRelationshipEntity : SugarBaseEntity, ITenantId, ISoftDeleted { /// /// 设备组code @@ -23,4 +23,7 @@ public class DeviceGroupRelationshipEntity : SugarBaseEntity, ITenantId public Guid DeviceCode { get; set; } public string TenantId { get; set; } + + /// + public bool IsDeleted { get; set; } } \ No newline at end of file diff --git a/Src/Dlhis.Entity/Sys/Entities/DevicePermissionEntity.cs b/Src/Dlhis.Entity/Sys/Entities/DevicePermissionEntity.cs index 04c56024..307b9e61 100644 --- a/Src/Dlhis.Entity/Sys/Entities/DevicePermissionEntity.cs +++ b/Src/Dlhis.Entity/Sys/Entities/DevicePermissionEntity.cs @@ -10,7 +10,7 @@ namespace Dlhis.Entity.Sys.Entities; /// 设备权限 /// [SugarTable(TableName = "DevicePermission", TableDescription = "设备权限")] -public class DevicePermissionEntity : SugarBaseEntity, ITenantId +public class DevicePermissionEntity : SugarBaseEntity, ITenantId, ISoftDeleted { /// /// 分配权限类型(User,Role,Group...) @@ -38,4 +38,7 @@ public class DevicePermissionEntity : SugarBaseEntity, ITenantId public string TenantId { get; set; } + + /// + public bool IsDeleted { get; set; } } \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Dto/DepartmentOpenResponse.cs b/Src/Dlhis.ScheduledTask/Common/Dto/DepartmentOpenResponse.cs deleted file mode 100644 index 5070c610..00000000 --- a/Src/Dlhis.ScheduledTask/Common/Dto/DepartmentOpenResponse.cs +++ /dev/null @@ -1,18 +0,0 @@ -using SqlSugar; - -namespace Dlhis.ScheduledTask.Common.Dto; - -public class DepartmentOpenResponse -{ - public string No { get; set; } - - /// - /// 上级部门号 - /// - public string PNo { get; set; } - - /// - /// 部门名称 - /// - public string Name { get; set; } -} diff --git a/Src/Dlhis.ScheduledTask/Common/Dto/GainfoOpenResponse.cs b/Src/Dlhis.ScheduledTask/Common/Dto/GainfoOpenResponse.cs deleted file mode 100644 index ac745401..00000000 --- a/Src/Dlhis.ScheduledTask/Common/Dto/GainfoOpenResponse.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Dlhis.Entity.Basic.Enums; - -namespace Dlhis.ScheduledTask.Common.Dto; - -public class GainfoOpenResponse -{ - public string Code { get; set; } - - public string Name { get; set; } - - public GenderEnumType Gender { get; set; } - - public string DepartmentNo { get; set; } - - public string DepartmentName { get; set; } - - public GainfoStatusEnumType Status { get; set; } -} diff --git a/Src/Dlhis.ScheduledTask/Common/Services/IIdentityUserSyncService.cs b/Src/Dlhis.ScheduledTask/Common/Services/IIdentityUserSyncService.cs new file mode 100644 index 00000000..c5d80dcb --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Common/Services/IIdentityUserSyncService.cs @@ -0,0 +1,6 @@ +namespace Dlhis.ScheduledTask.Common.Services; + +public interface IIdentityUserSyncService : IScopeDependency +{ + Task SyncAccountAsync(string userName, string name, string idNo, string tenant); +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs index 07b510fc..50660180 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs @@ -17,6 +17,7 @@ public class GainfoUpdateService : IGainfoUpdateService private readonly IDbContext _dbContext; private readonly ILogger _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 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().Where(x => x.ename == d.ZWORKCODE).FirstAsync())?.ToJson()); + // var d = modifiedPersons.First(); + // _logger.LogInformation("差异信息:{json}", d.ToJson()); + // _logger.LogInformation("差异信息:{json}", (await _dbContext.Queryable().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 { info.No }, PermissionInfos = new Dictionary> { - [info.No] = new List() + [info.No] = new() } }); return info.No; } - else - { - if (!await _dbContext.Queryable().AnyAsync(x => x.Code == item.ename)) - { - await _dbContext.Client.Insertable(info).AS("GainfoLeave").ExecuteCommandAsync(); - } - return null; + if (!await _dbContext.Queryable().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 UpdatePersonAsync(GainfoSapEntity item) { await _dbContext.Updateable().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().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().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 diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/IdentityUserSyncService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/IdentityUserSyncService.cs new file mode 100644 index 00000000..ab488671 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/IdentityUserSyncService.cs @@ -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"); + } + + /// + 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().SingleAsync(x => x.UserName == userName); + if (user == null) + { + var hasher = new PasswordHasher(); + + 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().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().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); + } + } +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Jobs/SyncShanxiGainfoJob.cs b/Src/Dlhis.ScheduledTask/Jobs/SyncShanxiGainfoJob.cs deleted file mode 100644 index a3a231d0..00000000 --- a/Src/Dlhis.ScheduledTask/Jobs/SyncShanxiGainfoJob.cs +++ /dev/null @@ -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; - -/// -/// 缓存山西档案数据 -/// -[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() - .LeftJoin((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().AS("GainfoLeave") - .LeftJoin((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() - .Select((x) => new DepartmentOpenResponse - { - Name = x.Name, - No = x.No, - PNo = x.PNo, - }) - .ToListAsync(); - - foreach (var item in departments) - { - await _redisCache.ListLeftPushAsync(departmentKey, item); - } - } -} diff --git a/Src/Dlhis.ScheduledTask/Jobs/UpdateUserPasswordJob.cs b/Src/Dlhis.ScheduledTask/Jobs/UpdateUserPasswordJob.cs deleted file mode 100644 index 1eead22f..00000000 --- a/Src/Dlhis.ScheduledTask/Jobs/UpdateUserPasswordJob.cs +++ /dev/null @@ -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; - -/// -/// 用户创建 -/// -[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() { "admin", "WB-0741", "WB-1091", "0123456" }; - - var users = await identityDbContext.Queryable() - .Where(x => !whitelist.Contains(x.UserName)) - .ToListAsync(); - - var gainfos = await DbContext.Queryable().Where(x => !whitelist.Contains(x.Code)).ToListAsync(); - - var hasher = new PasswordHasher(); - 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().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().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); - } - } -} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs index f0d5a9bf..51d07e7f 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs @@ -32,6 +32,11 @@ public class DeviceTaskAssignmentRequest : PageDto, IValidatableObject public string Factory { get; set; } + /// + /// 是否离线 + /// + public bool? IsOffline { get; set; } + /// /// 搜索范围 /// diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs index 1c479977..b0193071 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs @@ -44,6 +44,10 @@ public class DeviceTaskAssignmentService : IDeviceTaskAssignmentService || ga.Code == request.Search || deviceTask.DeviceUniqueNo == request.Search ) + .WhereIF(request.IsOffline.HasValue && request.IsOffline.Value, (deviceTask, device, ga, gaLeave, gaEx, deviceRegistry) => + SqlFunc.DateAdd(deviceRegistry.LastHeartbeatTime, device.HeartbeatInterval, DateType.Second) < DateTime.Now) + .WhereIF(request.IsOffline.HasValue && !request.IsOffline.Value, (deviceTask, device, ga, gaLeave, gaEx, deviceRegistry) => + SqlFunc.DateAdd(deviceRegistry.LastHeartbeatTime, device.HeartbeatInterval, DateType.Second) >= DateTime.Now) .OrderBy(deviceTask => deviceTask.CreateTime, OrderByType.Desc) .Select((deviceTask, device, ga, gaLeave, gaEx, deviceRegistry) => new DeviceTaskAssignmentResponse {