Files
jinko.yikatong/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs

172 lines
5.7 KiB
C#

using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Core.DataAccess.Dto;
using Kurisu.SqlSugar.Extensions;
using Kurisu.SqlSugar.Services;
using Mapster;
namespace Dlhis.Basic.Device.Services.Implements;
public class DeviceGroupService : IDeviceGroupService
{
private readonly IDbContext _dbContext;
public DeviceGroupService(IDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<Pagination<DeviceGroupPageResponse>> GetPageAsync(DeviceGroupPageRequest request)
{
return await _dbContext.Queryable<DeviceGroupEntity>()
.WhereIF(request.Search.IsPresent(), x => x.Name.Contains(request.Search))
.Select<DeviceGroupPageResponse>()
.ToPageListAsync(request);
}
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices)
{
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>()
.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)
{
//检查父级节点
await CheckPCodeExistsDeviceAsync(request.PCode);
if (!request.Id.HasValue)
{
var entity = request.Adapt<DeviceGroupEntity>();
entity.Code = Guid.NewGuid();
await _dbContext.InsertAsync(entity);
}
else
{
var entity = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Id == request.Id).FirstAsync();
entity.ThrowIfNull("设备组不存在,无法更新");
await _dbContext.UpdateAsync(entity);
}
}
public async Task DeleteAsync(long id)
{
var deviceGroup = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Id == id).FirstAsync();
deviceGroup.ThrowIfNull("设备组不存在,无需删除");
await _dbContext.DeleteAsync(deviceGroup);
}
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);
if (!request.DeviceCodes.Any())
{
//todo 删除时候判断权限是否分配
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
await _dbContext.DeleteAsync(relationships);
}
else
{
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
{
GroupCode = request.GroupCode,
DeviceCode = x
}).ToList();
await _dbContext.InsertAsync(entities);
}
//todo 生成下发任务
}
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>()
.Where(x => deviceCodes.Contains(x.Code))
.Select<DeviceUnderGroupResponse>()
.ToListAsync();
}
/****************************************************** help methods *********************************************************/
/// <summary>
/// 检查
/// </summary>
/// <param name="pCode"></param>
/// <returns></returns>
private async Task CheckPCodeExistsDeviceAsync(Guid? pCode)
{
if (pCode.HasValue)
{
var exists = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == pCode).AnyAsync();
exists.ThrowIfTrue("父级分组存在设备");
}
}
/// <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="current"></param>
/// <param name="departments"></param>
/// <param name="showDevices"></param>
private void RecursionGetNext(DeviceGroupTreeResponse current, List<DeviceGroupTreeResponse> departments, bool showDevices)
{
var next = departments.Where(x => x.PCode == current.Code).ToList();
current.Next = next;
foreach (var item in next)
{
RecursionGetNext(item, departments, 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
})
.ToList();
}
}
}