Files
jinko.yikatong/Src/Dlhis.ScheduledTask/Jobs/CreateDefinitelyGroupJob.cs
2024-07-04 18:38:39 +08:00

159 lines
6.3 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.Sys.Api;
using Dlhis.Sys.Api.Response;
using Kurisu.AspNetCore.Utils.Extensions;
using Newtonsoft.Json;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 创建系统默认分组
/// </summary>
[RecurringJob(nameof(CreateDefinitelyGroupJob), "0 0 5 * * ?", Name = "创建系统默认分组")]
public class CreateDefinitelyGroupJob : BaseJob
{
private readonly ITenantApi _tenantApi;
public CreateDefinitelyGroupJob(
ITenantApi tenantApi,
IServiceProvider serviceProvider) : base(serviceProvider)
{
_tenantApi = tenantApi;
}
private const string _remark = "系统自动生成";
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
//获取所有基地信息
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);
}
private async Task CreateGainfoGroupsAsync(List<TenantResponse> tenants)
{
var groups = new List<string> { "*自定义人员分组", "*督查组", "*X3及以上领导组" };
var searchGroups = await DbContext.Queryable<GainfoGroupEntity>().Where(x => groups.Contains(x.Name)).ToListAsync();
foreach (var grp in groups)
{
if (searchGroups.All(x => x.Name != grp))
{
await DbContext.InsertAsync(new GainfoGroupEntity
{
PCode = null,
Code = Guid.NewGuid(),
Name = grp,
IsDefault = grp.Equals("*自定义人员分组"),
Editable = false,
Remark = _remark,
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,
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,
TenantId = workplace.Code,
};
await DbContext.InsertAsync(workplaceGrp);
}
foreach (var factory in tenants.Where(x => x.PCode == workplace.Code))
{
var factoryName = $"*{type.GetDescription()}厂区系统组({factory.Name})";
var factoryGrp = await DbContext.Queryable<DeviceGroupEntity>().Where(x => x.PCode == workplaceGrp.Code).FirstAsync(x => x.Name == factoryName);
if (factoryGrp == null)
{
factoryGrp = new DeviceGroupEntity
{
PCode = workplaceGrp.Code,
Code = Guid.NewGuid(),
Type = type,
Name = factoryName,
Editable = false,
AbleSetNext = true,
Remark = _remark,
TenantId = factory.Code,
};
await DbContext.InsertAsync(factoryGrp);
}
if (type == DeviceTypeEnumType.Door)
{
if (!await DbContext.Queryable<DeviceGroupEntity>().Where(x => x.PCode == factoryGrp.Code).AnyAsync(x => x.IsBusinessTrip))
{
await DbContext.InsertAsync(new DeviceGroupEntity
{
PCode = factoryGrp.Code,
Code = Guid.NewGuid(),
Type = type,
Name = "*出差组",
Editable = false,
AbleSetNext = false,
IsBusinessTrip = true,
Remark = _remark,
TenantId = factory.Code,
});
}
}
}
}
}
}
}