feat 创建系统默认分组

This commit is contained in:
2024-02-22 14:30:03 +08:00
parent 9345cc6441
commit 170b6a1202
8 changed files with 212 additions and 20 deletions

View File

@@ -0,0 +1,163 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.Sys.Api;
using Kurisu.Core.DataAccess.Entity;
using Kurisu.SqlSugar.Services;
namespace Dlhis.ScheduledTask.CommonJobs;
public class CreateDefinitelyGroupJob
{
private readonly ILogger<CreateDefinitelyGroupJob> _logger;
private readonly ITenantApi _tenantApi;
private readonly IDbContext _dbContext;
public CreateDefinitelyGroupJob(ILogger<CreateDefinitelyGroupJob> logger, ITenantApi tenantApi, IDbContext dbContext)
{
_logger = logger;
_tenantApi = tenantApi;
_dbContext = dbContext;
}
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
try
{
var tenantResult = await _tenantApi.GetTenantsInternalAsync();
var tenants = tenantResult.Data;
foreach (var tenant in tenants)
{
if (!await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.TenantId == tenant.Code).AnyAsync(x => x.IsDefault))
{
var customGroup = new GainfoGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Name = "*自定义人员分组",
IsDefault = true,
Editable = false,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = tenant.Code
};
await _dbContext.InsertAsync(customGroup);
var groups = new[] { "*督查组", "*X3及以上领导组", "*外派人员组" };
foreach (var group in groups)
{
var grp = new GainfoGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Name = group,
IsDefault = false,
Editable = false,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = tenant.Code
};
await _dbContext.InsertAsync(grp);
}
}
//**************************************************************************************************************/
if (!await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.TenantId == tenant.Code).AnyAsync(x => x.Name == "*通用考勤设备组"))
{
await _dbContext.InsertAsync(new DeviceGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Type = DeviceTypeEnumType.Attendance,
Name = "*通用考勤设备组",
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = tenant.Code,
});
}
if (!await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.TenantId == tenant.Code).AnyAsync(x => x.Name == "*专用考勤设备组"))
{
await _dbContext.InsertAsync(new DeviceGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Type = DeviceTypeEnumType.Attendance,
Name = "*专用考勤设备组",
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = tenant.Code,
});
}
//**************************************************************************************************************/
if (!await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.TenantId == tenant.Code)
.Where(x => x.Type == DeviceTypeEnumType.Door)
.AnyAsync(x => x.Name == "*通用考勤设备组")
)
{
await _dbContext.InsertAsync(new DeviceGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Type = DeviceTypeEnumType.Door,
Name = "*非公共区域",
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = tenant.Code,
});
}
if (!await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.TenantId == tenant.Code)
.Where(x => x.Type == DeviceTypeEnumType.Door)
.AnyAsync(x => x.Name == "*专用考勤设备组")
)
{
await _dbContext.InsertAsync(new DeviceGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Type = DeviceTypeEnumType.Door,
Name = "*公共区域",
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = tenant.Code,
});
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "自动创建系统分组失败:{message}", ex.Message);
}
finally
{
_dbContext.Client.QueryFilter.Restore();
}
}
}