246 lines
9.7 KiB
C#
246 lines
9.7 KiB
C#
using Dlhis.Basic.Device.Request;
|
|
using Dlhis.Basic.Device.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Basic.Device.Services.Implements;
|
|
|
|
public class DeviceTimePeriodService : IDeviceTimePeriodService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public DeviceTimePeriodService(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Pagination<DeviceTimePeriodPageResponse>> GetPageAsync(DeviceTimePeriodPageRequest request)
|
|
{
|
|
var page = await _dbContext.Queryable<DeviceTimePeriodEntity>()
|
|
.Where(x => x.AssignType == request.AssignType)
|
|
.GroupBy(x => new { x.AssignType, x.Value })
|
|
.Select(x => new DeviceTimePeriodPageResponse
|
|
{
|
|
AssignType = x.AssignType,
|
|
Value = x.Value
|
|
})
|
|
.ToPageListAsync(request);
|
|
|
|
var values = page.Data.Select(x => x.Value);
|
|
var data = request.AssignType == DeviceTimePeriodAssignEnumType.Device
|
|
? await _dbContext.Queryable<DeviceEntity>().Where(x => values.Contains(x.Code)).Select(x => new { x.Code, Name = x.DeployPlace }).ToListAsync()
|
|
: await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => values.Contains(x.Code)).Select(x => new { x.Code, x.Name }).ToListAsync();
|
|
|
|
page.Data.ForEach(item => { item.Name = data.FirstOrDefault(x => x.Code == item.Value)?.Name; });
|
|
|
|
return page;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task SaveAsync(DeviceTimePeriodSaveRequest request)
|
|
{
|
|
(await _dbContext.Queryable<BasicTypeEntity>().Where(x => x.Type == "timeperiod").AnyAsync(x => x.Code == request.CardType.ToString()))
|
|
.ThrowIfFalse($"卡类型{request.CardType}不支持");
|
|
|
|
var existsData = request.AssignType == DeviceTimePeriodAssignEnumType.Device
|
|
? await _dbContext.Queryable<DeviceEntity>().Where(x => x.Code == request.Value).AnyAsync()
|
|
: await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => x.Code == request.Value).AnyAsync();
|
|
existsData.ThrowIfFalse("数据不匹配分配类型");
|
|
|
|
(await _dbContext.Queryable<DeviceTimePeriodEntity>()
|
|
.Where(x => x.CardType == request.CardType)
|
|
.Where(x => x.AssignType == request.AssignType)
|
|
.Where(x => x.Value == request.Value)
|
|
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
|
.AnyAsync()).ThrowIfTrue("请勿重复添加");
|
|
|
|
await GenerateDeviceTimePeriodTasksAsync(async () =>
|
|
{
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<DeviceTimePeriodEntity>();
|
|
entity.CardType = request.CardType;
|
|
entity.AssignType = request.AssignType;
|
|
entity.Value = request.Value;
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<DeviceTimePeriodEntity>().Where(x => x.Id == request.Id).SingleAsync();
|
|
request.Adapt(entity);
|
|
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}, "时段码变更");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<DeviceTimePeriodResponse>> GetListAsync(DeviceTimePeriodAssignEnumType assignType, Guid value)
|
|
{
|
|
var data = await _dbContext.Queryable<DeviceTimePeriodEntity>()
|
|
.Where(x => x.AssignType == assignType && x.Value == value)
|
|
.Select<DeviceTimePeriodEntity>()
|
|
.ToListAsync();
|
|
|
|
var result = data.Adapt<List<DeviceTimePeriodResponse>>();
|
|
|
|
var values = data.Select(x => x.Value);
|
|
var assignData = assignType == DeviceTimePeriodAssignEnumType.Device
|
|
? await _dbContext.Queryable<DeviceEntity>().Where(x => values.Contains(x.Code)).Select(x => new { x.Code, Name = x.DeployPlace }).ToListAsync()
|
|
: await _dbContext.Queryable<DeviceTemplateEntity>().Where(x => values.Contains(x.Code)).Select(x => new { x.Code, x.Name }).ToListAsync();
|
|
|
|
var basicTypes = await _dbContext.Queryable<BasicTypeEntity>().Where(x => x.Type == "timeperiod").ToListAsync();
|
|
|
|
result.ForEach(item =>
|
|
{
|
|
item.Name = assignData.FirstOrDefault(x => x.Code == item.Value)?.Name;
|
|
item.CardTypeDesc = basicTypes.FirstOrDefault(x => x.Code == item.CardType.ToString())?.Display;
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task DeleteCardTypeAsync(DeviceTimePeriodDeleteCardTypeRequest request)
|
|
{
|
|
await GenerateDeviceTimePeriodTasksAsync(async () =>
|
|
{
|
|
var entity = await _dbContext.Queryable<DeviceTimePeriodEntity>()
|
|
.Where(x => x.CardType == request.CardType)
|
|
.Where(x => x.AssignType == request.AssignType)
|
|
.Where(x => x.Value == request.Value)
|
|
.SingleAsync();
|
|
|
|
entity.ThrowIfNull("数据不存在");
|
|
await _dbContext.DeleteReallyAsync(entity);
|
|
}, "删除时段码");
|
|
}
|
|
|
|
|
|
public async Task GenerateDeviceTimePeriodTasksAsync(Func<Task> func, string source)
|
|
{
|
|
var cardTypes = await _dbContext.Queryable<BasicTypeEntity>().Where(x => x.Type == "timeperiod").Select(x => SqlFunc.ToInt32(x.Code)).ToListAsync();
|
|
|
|
var before = await GetSettingsAsync();
|
|
await func();
|
|
var current = await GetSettingsAsync();
|
|
|
|
var tasks = new List<DeviceTaskEntity>();
|
|
|
|
var deviceUniqueNos = before.Keys.ToList().Concat(current.Keys).Distinct().ToList();
|
|
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceUniqueNos.Contains(x.UniqueNo)).ToListAsync();
|
|
foreach (var device in devices)
|
|
{
|
|
var deviceUniqueNo = device.UniqueNo;
|
|
|
|
before.TryGetValue(deviceUniqueNo, out var beforeDtos);
|
|
beforeDtos ??= new List<DeviceTimePeriodEntity>(0);
|
|
|
|
current.TryGetValue(deviceUniqueNo, out var currentDtos);
|
|
currentDtos ??= new List<DeviceTimePeriodEntity>(0);
|
|
|
|
if (beforeDtos.Count != currentDtos.Count)
|
|
{
|
|
tasks.Add(new DeviceTaskEntity
|
|
{
|
|
Type = beforeDtos.Count < currentDtos.Count ? DeviceTaskEnumType.TimePeriodAdd : DeviceTaskEnumType.TimePeriodDelete,
|
|
DeviceType = device.Type,
|
|
UserId = null,
|
|
UserNo = null,
|
|
DeviceUniqueNo = deviceUniqueNo,
|
|
Status = DeviceTaskStatusEnumType.Wait,
|
|
Version = 0,
|
|
Source = source,
|
|
Priority = 90,
|
|
TenantId = device.TenantId
|
|
});
|
|
|
|
continue;
|
|
}
|
|
|
|
foreach (var cardType in cardTypes)
|
|
{
|
|
var b = beforeDtos.FirstOrDefault(x => x.CardType == cardType);
|
|
if (b == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var c = currentDtos.First(x => x.CardType == cardType);
|
|
|
|
if (b.ToJson() == c.ToJson())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
tasks.Add(new DeviceTaskEntity
|
|
{
|
|
Type = DeviceTaskEnumType.TimePeriodAdd,
|
|
DeviceType = device.Type,
|
|
UserId = null,
|
|
UserNo = null,
|
|
DeviceUniqueNo = deviceUniqueNo,
|
|
Status = DeviceTaskStatusEnumType.Wait,
|
|
Version = 0,
|
|
ResultMessage = null,
|
|
Source = source,
|
|
Priority = 90,
|
|
TenantId = device.TenantId,
|
|
});
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
await _dbContext.InsertAsync(tasks);
|
|
}
|
|
|
|
private async Task<Dictionary<string, List<DeviceTimePeriodEntity>>> GetSettingsAsync()
|
|
{
|
|
var timePeriods = await _dbContext.Queryable<DeviceTimePeriodEntity>().ToListAsync();
|
|
|
|
var templateCodes = timePeriods.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Template).Select(x => x.Value).Distinct().ToList();
|
|
var deviceCodes = timePeriods.Where(x => x.AssignType == DeviceTimePeriodAssignEnumType.Device).Select(x => x.Value).ToList();
|
|
|
|
var devices = await _dbContext.Queryable<DeviceEntity>()
|
|
.Where(x => !SqlFunc.IsNullOrEmpty(x.TemplateCode))
|
|
.Where(x => templateCodes.Contains(x.TemplateCode) || deviceCodes.Contains(x.Code))
|
|
.Where(x => x.Enable && x.EnableTimePeriod)
|
|
.ToListAsync();
|
|
|
|
return devices.Select(x =>
|
|
{
|
|
var tSettings = timePeriods
|
|
.Where(item => item.AssignType == DeviceTimePeriodAssignEnumType.Template && item.Value == x.TemplateCode)
|
|
.ToList();
|
|
|
|
var dSettings = timePeriods
|
|
.Where(item => item.AssignType == DeviceTimePeriodAssignEnumType.Device && item.Value == x.Code)
|
|
.ToList();
|
|
|
|
foreach (var setting in dSettings)
|
|
{
|
|
var s = tSettings.FirstOrDefault(item => item.CardType == setting.CardType);
|
|
if (s != null)
|
|
{
|
|
tSettings.Remove(s);
|
|
}
|
|
|
|
tSettings.Add(setting);
|
|
}
|
|
|
|
return new
|
|
{
|
|
x.UniqueNo, tSettings
|
|
};
|
|
}).ToDictionary(x => x.UniqueNo, x => x.tSettings);
|
|
}
|
|
} |