fix 设备组重组
This commit is contained in:
@@ -6,13 +6,13 @@ namespace Dlhis.Basic.Device.Services;
|
||||
|
||||
public interface IDeviceGroupService : IScopeDependency
|
||||
{
|
||||
Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices, DeviceTypeEnumType? type);
|
||||
Task<List<DeviceGroupTreeResponse>> GetTreeAsync(DeviceTypeEnumType type);
|
||||
|
||||
Task SaveAsync(DeviceGroupSaveRequest request);
|
||||
|
||||
Task<List<DeviceUnderGroupResponse>> GetDevicesAsync(Guid code);
|
||||
Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code);
|
||||
|
||||
Task AssignDevicesAsync(DeviceAssignRequest requests);
|
||||
|
||||
Task<List<DeviceGroupResponse>> GetAppointmentGroupsAsync();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.Sys.Api;
|
||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
@@ -15,27 +16,42 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly IDevicePermissionService _devicePermissionService;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly ITenantApi _tenantApi;
|
||||
|
||||
public DeviceGroupService(IDbContext dbContext, IDevicePermissionService devicePermissionService, ICurrentUser currentUser)
|
||||
public DeviceGroupService(IDbContext dbContext,
|
||||
IDevicePermissionService devicePermissionService,
|
||||
ICurrentUser currentUser,
|
||||
ITenantApi tenantApi
|
||||
)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_devicePermissionService = devicePermissionService;
|
||||
_currentUser = currentUser;
|
||||
_tenantApi = tenantApi;
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices, DeviceTypeEnumType? type)
|
||||
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(DeviceTypeEnumType type)
|
||||
{
|
||||
var tenantResult = await _tenantApi.GetMyTenantsAsync();
|
||||
var tenantIds = tenantResult.Data.Select(x => x.Code).ToList();
|
||||
tenantIds.AddRange(tenantResult.Data.Select(x => x.PCode));
|
||||
|
||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>()
|
||||
.WhereIF(type.HasValue, x => x.Type == type)
|
||||
.Where(x => tenantIds.Contains(x.TenantId))
|
||||
.Where(x => x.Type == type)
|
||||
.Select<DeviceGroupTreeResponse>()
|
||||
.ToListAsync();
|
||||
|
||||
var others = await _dbContext.Queryable<DeviceGroupEntity>()
|
||||
.Where(x => x.Type == type)
|
||||
.Where(x => x.Name == "*共用区域" || x.Name == "*非共用区域" || x.Name == "*通用考勤设备组" || x.Name == "*专用考勤设备组")
|
||||
.Select<DeviceGroupTreeResponse>()
|
||||
.ToListAsync();
|
||||
deviceGroups.AddRange(others);
|
||||
|
||||
var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList();
|
||||
tops.ForEach(x =>
|
||||
{
|
||||
RecursionGetNext(x, deviceGroups, showDevices);
|
||||
});
|
||||
tops.ForEach(x => { RecursionGetNext(x, deviceGroups); });
|
||||
|
||||
return tops;
|
||||
}
|
||||
@@ -69,7 +85,7 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
|
||||
existsDiffNextGroups.ThrowIfTrue("存在下级分组类型不一致,无法修改");
|
||||
|
||||
var nextDevices = await GetDevicesAsync(entity.Code);
|
||||
var nextDevices = await GetDevicesByGroupCodeAsync(entity.Code);
|
||||
nextDevices.Any(x => x.Type != request.Type).ThrowIfTrue("分组设备类型不匹配,无法修改类型");
|
||||
}
|
||||
|
||||
@@ -99,15 +115,13 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
DeviceCode = x,
|
||||
|
||||
TenantId = _currentUser.GetStringTenantId(),
|
||||
|
||||
}).ToList();
|
||||
|
||||
await _dbContext.InsertAsync(entities);
|
||||
|
||||
}, null);
|
||||
}
|
||||
|
||||
public async Task<List<DeviceUnderGroupResponse>> GetDevicesAsync(Guid code)
|
||||
public async Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code)
|
||||
{
|
||||
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
||||
.Where(x => x.GroupCode == code)
|
||||
@@ -117,31 +131,31 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
|
||||
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();
|
||||
.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();
|
||||
.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 *********************************************************/
|
||||
@@ -198,47 +212,14 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
/// </summary>
|
||||
/// <param name="current"></param>
|
||||
/// <param name="deviceGroups"></param>
|
||||
/// <param name="showDevices"></param>
|
||||
private void RecursionGetNext(DeviceGroupTreeResponse current, List<DeviceGroupTreeResponse> deviceGroups, bool showDevices)
|
||||
private static void RecursionGetNext(DeviceGroupTreeResponse current, List<DeviceGroupTreeResponse> deviceGroups)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
var next = deviceGroups.Where(x => x.PCode == current.Code).ToList();
|
||||
|
||||
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();
|
||||
RecursionGetNext(item, deviceGroups);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user