commit 528cdd51c05d26437f0b23f415e06311b667e9f7 Author: liguoyang <Ligy.97@foxmail.com> Date: Wed Apr 17 15:37:30 2024 +0800 feat 访客预约 commit e73a3fcd9307f86aba2c39578495c03cde1d41f9 Author: liguoyang <Ligy.97@foxmail.com> Date: Tue Apr 16 16:15:16 2024 +0800 feat 业务curd commit b98b6e2764fea875866e11f790f388ccf3c234b5 Merge: a3dcec3 965e84b Author: liguoyang <Ligy.97@foxmail.com> Date: Tue Apr 16 14:56:30 2024 +0800 Merge branch 'feat-visitorAppointments' of https://git.dlhis.com/liguoyang/jinko.yikatong into feat-visitorAppointments commit a3dcec30e1289fbb09de8f85009d3931d971337a Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:57:49 2024 +0800 fix appsettings commit 766fbec0c45a3852a75a7a37686b885ac3b0a77a Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:37:01 2024 +0800 add dockerfile commit 86731d41fee97ba073065ed1b807987760a371a7 Author: liguoyang <Ligy.97@foxmail.com> Date: Sun Apr 14 20:11:56 2024 +0800 fix 测试websocket链接 commit 1301b126a9ba9a7d97cf45967cf2915e1169c505 Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:57:49 2024 +0800 fix appsettings commit 081f3a67896c67891c3f80edde82a440b8736851 Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:37:01 2024 +0800 add dockerfile commit 271b6c5898b5732a35ccfa0629ce89442ac49143 Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:34:26 2024 +0800 test 提交测试代码 commit 965e84b6f3b7f925f8021c9b996cce961b896dea Merge: 578eb6a 6a6fad1 Author: liguoyang <Ligy.97@foxmail.com> Date: Mon Apr 15 17:06:45 2024 +0800 Merge branch 'feat-visitorAppointments' of https://git.dlhis.com/liguoyang/jinko.yikatong into feat-visitorAppointments commit 578eb6afbd96f0bfe4aaf8058fd4743309c74d62 Author: liguoyang <Ligy.97@foxmail.com> Date: Sun Apr 14 20:11:56 2024 +0800 fix 测试websocket链接 commit 114511f1daea64b7dbf9c3c38b73e28b6d0301e6 Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:57:49 2024 +0800 fix appsettings commit 5eacfa8af8087eade8553266a12a2cf42305716b Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:37:01 2024 +0800 add dockerfile commit b2ffff1f099be343d89690014d05cb859776acbb Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:34:26 2024 +0800 test 提交测试代码 commit 6a6fad1d1b8772eebf5df12cd74cfd5c955ff560 Author: liguoyang <Ligy.97@foxmail.com> Date: Sun Apr 14 20:11:56 2024 +0800 fix 测试websocket链接 commit 9ce3c4d6f63957c175d65370d9a6b80e10f4254f Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:57:49 2024 +0800 fix appsettings commit c637fd89850cf289d7da457c6c6f47354b5beed3 Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:37:01 2024 +0800 add dockerfile commit 58c39815b1c45a99aea0caf503fcb1365c957f03 Author: liguoyang <Ligy.97@foxmail.com> Date: Fri Apr 12 17:34:26 2024 +0800 test 提交测试代码
245 lines
8.7 KiB
C#
245 lines
8.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.Utils.Extensions;
|
|
using Kurisu.Core.User.Abstractions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Device.Services.Implements;
|
|
|
|
public class DeviceGroupService : IDeviceGroupService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly IDevicePermissionService _devicePermissionService;
|
|
private readonly ICurrentUser _currentUser;
|
|
|
|
public DeviceGroupService(IDbContext dbContext, IDevicePermissionService devicePermissionService, ICurrentUser currentUser)
|
|
{
|
|
_dbContext = dbContext;
|
|
_devicePermissionService = devicePermissionService;
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
|
|
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices, DeviceTypeEnumType? type)
|
|
{
|
|
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>()
|
|
.WhereIF(type.HasValue, x => x.Type == type)
|
|
.Select<DeviceGroupTreeResponse>()
|
|
.ToListAsync();
|
|
|
|
var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList();
|
|
tops.ForEach(x =>
|
|
{
|
|
RecursionGetNext(x, deviceGroups, showDevices);
|
|
});
|
|
|
|
return tops;
|
|
}
|
|
|
|
public async Task SaveAsync(DeviceGroupSaveRequest request)
|
|
{
|
|
//检查父级节点
|
|
var pGrp = await CheckPCodeAsync(request);
|
|
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<DeviceGroupEntity>();
|
|
entity.Code = Guid.NewGuid();
|
|
entity.TenantId = _currentUser.GetStringTenantId();
|
|
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
|
entity.ThrowIfNull("设备分组不存在,无法更新");
|
|
entity.Editable.ThrowIfFalse("不可编辑当前分组");
|
|
|
|
if (request.Type != entity.Type)
|
|
{
|
|
//检查下级分组type不一致
|
|
var existsDiffNextGroups = await _dbContext.Queryable<DeviceGroupEntity>()
|
|
.Where(x => x.PCode == entity.Code)
|
|
.Where(x => x.Type != request.Type)
|
|
.AnyAsync();
|
|
|
|
existsDiffNextGroups.ThrowIfTrue("存在下级分组类型不一致,无法修改");
|
|
|
|
var nextDevices = await GetDevicesAsync(entity.Code);
|
|
nextDevices.Any(x => x.Type != request.Type).ThrowIfTrue("分组设备类型不匹配,无法修改类型");
|
|
}
|
|
|
|
request.Adapt(entity);
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
|
|
public async Task AssignDevicesAsync(DeviceAssignRequest request)
|
|
{
|
|
var group = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.GroupCode).FirstAsync();
|
|
group.ThrowIfNull("设备分组不存在,无法进行分配");
|
|
|
|
await CheckNoNextGroupAsync(group.Code);
|
|
await CheckGroupTypeMatchAsync(request, group);
|
|
|
|
//下发任务
|
|
await _devicePermissionService.GenerateDeviceTasksAsync(async () =>
|
|
{
|
|
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
|
await _dbContext.DeleteReallyAsync(relationships);
|
|
|
|
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
|
|
{
|
|
GroupCode = request.GroupCode,
|
|
DeviceCode = x,
|
|
|
|
TenantId = _currentUser.GetStringTenantId(),
|
|
|
|
}).ToList();
|
|
|
|
await _dbContext.InsertAsync(entities);
|
|
|
|
}, null);
|
|
}
|
|
|
|
public async Task<List<DeviceUnderGroupResponse>> GetDevicesAsync(Guid code)
|
|
{
|
|
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
|
.Where(x => x.GroupCode == code)
|
|
.ToListAsync();
|
|
|
|
var deviceCodes = relationships.Select(x => x.DeviceCode);
|
|
|
|
return await _dbContext.Queryable<DeviceEntity>()
|
|
.LeftJoin<DeviceTemplateEntity>((x, dt) => x.TemplateCode == dt.Code)
|
|
.Where(x => deviceCodes.Contains(x.Code))
|
|
.Select((x, dt) => new DeviceUnderGroupResponse
|
|
{
|
|
UniqueNo = x.UniqueNo,
|
|
Type = x.Type,
|
|
Code = x.Code,
|
|
Enable = x.Enable,
|
|
Brand = dt.Brand
|
|
})
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
public async Task<List<DeviceGroupResponse>> GetAppointmentGroupsAsync()
|
|
{
|
|
return await _dbContext.Queryable<DeviceGroupEntity>()
|
|
.Where(x => x.AsAppointment)
|
|
.Where(x => x.Type == DeviceTypeEnumType.Door)
|
|
.Select(x => new DeviceGroupResponse
|
|
{
|
|
Id = x.Id,
|
|
Code = x.Code,
|
|
Name = x.Name,
|
|
})
|
|
.ToListAsync();
|
|
}
|
|
|
|
/****************************************************** help methods *********************************************************/
|
|
|
|
/// <summary>
|
|
/// 检查父级分组
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
private async Task<DeviceGroupEntity> CheckPCodeAsync(DeviceGroupSaveRequest request)
|
|
{
|
|
var pGrp = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.PCode).FirstAsync();
|
|
if (pGrp != null)
|
|
{
|
|
(!pGrp.AbleSetNext && !request.Id.HasValue).ThrowIfTrue($"{pGrp.Name}不可添加下级");
|
|
var exists = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.PCode).AnyAsync();
|
|
exists.ThrowIfTrue("上级分组存在设备");
|
|
}
|
|
|
|
(pGrp != null && pGrp.Type != request.Type).ThrowIfTrue("分组类型必须与上级类型保持一致");
|
|
return pGrp;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查分组是否存在下级
|
|
/// </summary>
|
|
/// <param name="currentCode"></param>
|
|
/// <returns></returns>
|
|
private async Task CheckNoNextGroupAsync(Guid currentCode)
|
|
{
|
|
var exists = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.PCode == currentCode).AnyAsync();
|
|
exists.ThrowIfTrue("存在下级设备分组,无法分配设备");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查分组分类匹配
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="group"></param>
|
|
/// <returns></returns>
|
|
private async Task CheckGroupTypeMatchAsync(DeviceAssignRequest request, DeviceGroupEntity group)
|
|
{
|
|
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => request.DeviceCodes.Contains(x.Code)).ToListAsync();
|
|
//检查分组的分类控制
|
|
devices.Any(x => x.Type != group.Type).ThrowIfTrue($"存在设备类型不一致,无法分配到当前{group.Type.GetDescription()}分组");
|
|
|
|
//重新赋值到请求参数中
|
|
request.DeviceCodes = devices.Select(x => x.Code).ToList();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 递归获取树
|
|
/// </summary>
|
|
/// <param name="current"></param>
|
|
/// <param name="deviceGroups"></param>
|
|
/// <param name="showDevices"></param>
|
|
private void RecursionGetNext(DeviceGroupTreeResponse current, List<DeviceGroupTreeResponse> deviceGroups, bool showDevices)
|
|
{
|
|
var currentTenantId = _currentUser.GetStringTenantId();
|
|
var next = deviceGroups.Where(x =>
|
|
{
|
|
if (x.PCode == current.Code)
|
|
{
|
|
if (x.TenantId.IsEmpty())
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return x.TenantId == currentTenantId;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
|
|
}).ToList();
|
|
current.Next = next;
|
|
foreach (var item in next)
|
|
{
|
|
RecursionGetNext(item, deviceGroups, showDevices);
|
|
}
|
|
|
|
//最后层级附加设备信息
|
|
if (!current.Next.Any() && showDevices)
|
|
{
|
|
current.Next = _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
|
.LeftJoin<DeviceEntity>((relation, device) => relation.DeviceCode == device.Code)
|
|
.Where((relation, device) => relation.GroupCode == current.Code)
|
|
.Select((relation, device) => new DeviceGroupTreeResponse
|
|
{
|
|
Code = device.Code,
|
|
Name = device.UniqueNo,
|
|
PCode = relation.GroupCode
|
|
})
|
|
.Distinct()
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|