94 lines
3.1 KiB
C#
94 lines
3.1 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 SaveAsync(DeviceGroupSaveRequest request)
|
|
{
|
|
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(AssignDevicesRequest request)
|
|
{
|
|
var group = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.GroupCode).FirstAsync();
|
|
group.ThrowIfNull("设备组不存在");
|
|
|
|
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();
|
|
|
|
}
|
|
}
|