Files
jinko.yikatong/Src/Dlhis.ScheduledTask/CommonJobs/CreateDefinitelyGroupJob.cs
2024-03-05 20:38:00 +08:00

173 lines
6.5 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.Sys.Api;
using Dlhis.Sys.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<CreateDefinitelyGroupJob> _logger;
private readonly ITenantApi _tenantApi;
private readonly IDbContext _dbContext;
public CreateDefinitelyGroupJob(ILogger<CreateDefinitelyGroupJob> logger,
ITenantApi tenantApi,
IDbContext dbContext,
ISqlSugarOptionsService sqlSugarOptionsService)
{
_logger = logger;
_tenantApi = tenantApi;
_dbContext = dbContext;
sqlSugarOptionsService.IgnoreTenant = true;
}
private static readonly string _remark = "系统自动生成";
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
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<string> { "*通用考勤设备组", "*专用考勤设备组" }, tenants);
//*****************************************************门禁设备组********************************************************/
await CreateDeviceGroupAsync(DeviceTypeEnumType.Door, new List<string> { "*共用区域", "*非共用区域" }, tenants);
}
catch (Exception ex)
{
_logger.LogError(ex, "自动创建系统分组失败:{message}", ex.Message);
}
finally
{
_dbContext.Client.QueryFilter.Restore();
}
}
private async Task CreateGainfoGroupsAsync(List<TenantResponse> tenants)
{
var grps = new List<string> { "*自定义人员分组", "*督查组", "*X3及以上领导组" };
var searchGrps = await _dbContext.Queryable<GainfoGroupEntity>().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,
Remark = _remark,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = null
});
}
}
}
private async Task CreateDeviceGroupAsync(DeviceTypeEnumType type, List<string> topGroups, List<TenantResponse> tenants)
{
foreach (var grp in topGroups)
{
var topGrp = await _dbContext.Queryable<DeviceGroupEntity>().FirstAsync(x => x.Name == grp);
if (topGrp == null)
{
topGrp = new DeviceGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Type = type,
Name = grp,
Editable = false,
AbleSetNext = false,
Remark = _remark,
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<DeviceGroupEntity>().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,
AbleSetNext = false,
Remark = _remark,
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<DeviceGroupEntity>().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,
AbleSetNext = true,
Remark = _remark,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
TenantId = factory.Code,
});
}
}
}
}
}
}