fix 设备组重组

This commit is contained in:
2024-06-09 01:34:48 +08:00
parent cf5d1348f0
commit 0a7898cd73
8 changed files with 98 additions and 109 deletions

View File

@@ -1,4 +1,5 @@
using Dlhis.Basic.Device.Request;
using System.ComponentModel.DataAnnotations;
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Device.Services;
using Dlhis.Entity.Device.Enums;
@@ -26,14 +27,16 @@ public class DeviceGroupsController : ControllerBase
/// <summary>
/// 获取设备组树
/// </summary>
/// <param name="showDevices">是否显示设备</param>
/// <param name="type">分组类型</param>
/// <returns></returns>
[HttpGet("tree")]
[IgnoreTenant]
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices, DeviceTypeEnumType? type)
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(
[EnumDataType(typeof(DeviceTypeEnumType), ErrorMessage = "请选择正确的类型")]
DeviceTypeEnumType type
)
{
return await _deviceGroupService.GetTreeAsync(showDevices, type);
return await _deviceGroupService.GetTreeAsync(type);
}
/// <summary>
@@ -54,9 +57,9 @@ public class DeviceGroupsController : ControllerBase
/// <param name="code"></param>
/// <returns></returns>
[HttpGet("devices")]
public async Task<List<DeviceUnderGroupResponse>> GetDevicesAsync(Guid code)
public async Task<List<DeviceUnderGroupResponse>> GetDevicesByGroupCodeAsync(Guid code)
{
return await _deviceGroupService.GetDevicesAsync(code);
return await _deviceGroupService.GetDevicesByGroupCodeAsync(code);
}
/// <summary>
@@ -81,4 +84,4 @@ public class DeviceGroupsController : ControllerBase
{
return await _deviceGroupService.GetAppointmentGroupsAsync();
}
}
}

View File

@@ -4,6 +4,9 @@ namespace Dlhis.Basic.Device.Response;
public class DeviceUnderGroupResponse
{
/// <summary>
/// 设备code
/// </summary>
public Guid Code { get; set; }
/// <summary>
@@ -20,8 +23,7 @@ public class DeviceUnderGroupResponse
/// 设备类型
/// </summary>
public DeviceTypeEnumType Type { get; set; }
/// <summary>
/// 是否启用
/// </summary>

View File

@@ -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();
}
}

View File

@@ -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);
}
}
}
}