using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Device.Entities; using Dlhis.Entity.Device.Enums; using Dlhis.Sys.Api; using Dlhis.User.Api.Response; using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.Core.DataAccess.Entity; using Kurisu.SqlSugar.Services; using Newtonsoft.Json; using SqlSugar; namespace Dlhis.ScheduledTask.CommonJobs; public class CreateDefinitelyGroupJob { private readonly ILogger _logger; private readonly ITenantApi _tenantApi; private readonly IDbContext _dbContext; public CreateDefinitelyGroupJob(ILogger logger, ITenantApi tenantApi, IDbContext dbContext, ISqlSugarOptionsService sqlSugarOptionsService) { _logger = logger; _tenantApi = tenantApi; _dbContext = dbContext; sqlSugarOptionsService.IgnoreTenant = true; } public async Task RunAsync() { _dbContext.Client.QueryFilter.ClearAndBackup(); try { //获取所有基地信息 var tenantResult = await _tenantApi.GetTenantsInternalAsync(); var tenants = tenantResult.Data; _logger.LogInformation("获取基地信息:{json}", JsonConvert.SerializeObject(tenants)); //****************************************************系统人员分组**********************************************************/ await CreateGainfoGroupsAsync(tenants); //*****************************************************考勤设备组*********************************************************/ await CreateDeviceGroupAsync(DeviceTypeEnumType.Attendance, new List { "*通用考勤设备组", "*专用考勤设备组" }, tenants); //*****************************************************门禁设备组********************************************************/ await CreateDeviceGroupAsync(DeviceTypeEnumType.Door, new List { "*共用区域", "*非共用区域" }, tenants); } catch (Exception ex) { _logger.LogError(ex, "自动创建系统分组失败:{message}", ex.Message); } finally { _dbContext.Client.QueryFilter.Restore(); } } private async Task CreateGainfoGroupsAsync(List tenants) { var grps = new List { "*自定义人员分组", "*督查组", "*X3及以上领导组" }; var searchGrps = await _dbContext.Queryable().Where(x => grps.Contains(x.Name)).ToListAsync(); foreach (var grp in grps) { if (!searchGrps.Any(x => x.Name == grp)) { await _dbContext.InsertAsync(new GainfoGroupEntity { PCode = null, Code = Guid.NewGuid(), Name = grp, IsDefault = grp.Equals("*自定义人员分组"), Editable = false, CreatedBy = 1, CreateTime = DateTime.Now, ModifiedBy = 1, ModifiedTime = DateTime.Now, TenantId = null }); } } } private async Task CreateDeviceGroupAsync(DeviceTypeEnumType type, List topGroups, List tenants) { foreach (var grp in topGroups) { var topGrp = await _dbContext.Queryable().FirstAsync(x => x.Name == grp); if (topGrp == null) { topGrp = new DeviceGroupEntity { PCode = null, Code = Guid.NewGuid(), Type = type, Name = grp, Editable = false, CreatedBy = 1, CreateTime = DateTime.Now, ModifiedBy = 1, ModifiedTime = DateTime.Now, TenantId = null, }; await _dbContext.InsertAsync(topGrp); } foreach (var workplace in tenants.Where(x => x.PCode.IsEmpty())) { var workplaceName = $"*{type.GetDescription()}工作地系统组({workplace.Name})"; var workplaceGrp = await _dbContext.Queryable().Where(x => x.PCode == topGrp.Code).FirstAsync(x => x.Name == workplaceName); if (workplaceGrp == null) { workplaceGrp = new DeviceGroupEntity { PCode = topGrp.Code, Code = Guid.NewGuid(), Type = type, Name = workplaceName, Editable = false, CreatedBy = 1, CreateTime = DateTime.Now, ModifiedBy = 1, ModifiedTime = DateTime.Now, TenantId = null, }; await _dbContext.InsertAsync(workplaceGrp); } foreach (var factory in tenants.Where(x => x.PCode == workplace.Code)) { var factoryName = $"*{type.GetDescription()}厂区系统组({factory.Name})"; if (!await _dbContext.Queryable().Where(x => x.PCode == workplaceGrp.Code).AnyAsync(x => x.Name == factoryName)) { await _dbContext.InsertAsync(new DeviceGroupEntity { PCode = workplaceGrp.Code, Code = Guid.NewGuid(), Type = type, Name = factoryName, Editable = false, CreatedBy = 1, CreateTime = DateTime.Now, ModifiedBy = 1, ModifiedTime = DateTime.Now, TenantId = factory.Code, }); } } } } } }