143 lines
4.9 KiB
C#
143 lines
4.9 KiB
C#
using Dlhis.Basic.WorkGrids.Request;
|
|
using Dlhis.Basic.WorkGrids.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Dormitory.Entity;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Basic.WorkGrids.Services.Implements;
|
|
|
|
public class EFenceService : IEFenceService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public EFenceService(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
|
|
public async Task SaveAsync(EFenceSaveRequest request)
|
|
{
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<EFenceEntity>();
|
|
entity.Code = Guid.NewGuid();
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<EFenceEntity>().SingleAsync(x => x.Id == request.Id.Value);
|
|
request.Adapt(entity);
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
public async Task<Pagination<EFencePageResponse>> GetEFencePageAsync(EFencePageRequest request)
|
|
{
|
|
var page = await _dbContext.Queryable<EFenceEntity>()
|
|
.WhereIF(request.Search.IsPresent(), x => x.Name.Contains(request.Search))
|
|
.OrderBy(x => x.Id, OrderByType.Desc)
|
|
.Select(x => new EFencePageResponse
|
|
{
|
|
Id = x.Id,
|
|
Code = x.Code,
|
|
Name = x.Name,
|
|
Description = x.Description,
|
|
DeviceCount = SqlFunc.Subqueryable<EFenceDeviceRelationEntity>().Where(p => p.EFenceCode == x.Code).Count(),
|
|
})
|
|
.ToPageListAsync(request);
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task<List<EFenceDormitoryRoomResponse>> GetRoomsAsync(Guid eFenceCode)
|
|
{
|
|
return await _dbContext.Queryable<DormitoryRoomEntity>()
|
|
.Where(x => x.EFenceCode == eFenceCode)
|
|
.Select<EFenceDormitoryRoomResponse>()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<EFenceDormitoryRoomPersonResponse>> GetRoomPersonsAsync(string roomId)
|
|
{
|
|
return await _dbContext.Queryable<DormitoryRoomPersonEntity>()
|
|
.Where(x => x.RoomId == roomId)
|
|
.Select<EFenceDormitoryRoomPersonResponse>()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task AddDevicesAsync(long fenceId, List<Guid> deviceCodes)
|
|
{
|
|
//查询电子围栏
|
|
var fence = await _dbContext.Queryable<EFenceEntity>().SingleAsync(f => f.Id == fenceId);
|
|
fence.ThrowIfNull("未找到电子围栏");
|
|
|
|
//查询设备
|
|
var devices = await _dbContext.Queryable<DeviceEntity>()
|
|
.Where(d => deviceCodes.Contains(d.Code))
|
|
.Where(d => d.DeployDirection.HasValue)
|
|
.Where(d => SqlFunc.Subqueryable<EFenceDeviceRelationEntity>()
|
|
.Where(p => p.EFenceCode == fence.Code && p.DeviceCode == d.Code)
|
|
.NotAny() //不可重复分配
|
|
)
|
|
.ToListAsync();
|
|
|
|
if (devices.Any())
|
|
{
|
|
var eFenceDeviceRelationEntities = devices.Select(x => new EFenceDeviceRelationEntity
|
|
{
|
|
DeviceCode = x.Code,
|
|
EFenceCode = fence.Code
|
|
}).ToList();
|
|
|
|
await _dbContext.InsertAsync(eFenceDeviceRelationEntities);
|
|
}
|
|
}
|
|
|
|
public async Task RemoveDevicesAsync(long fenceId, List<Guid> deviceCodes)
|
|
{
|
|
//查询电子围栏
|
|
var fence = await _dbContext.Queryable<EFenceEntity>().SingleAsync(f => f.Id == fenceId);
|
|
fence.ThrowIfNull("未找到电子围栏");
|
|
|
|
//查询EFenceDeviceRelationEntity的设备
|
|
var relations = await _dbContext.Queryable<EFenceDeviceRelationEntity>()
|
|
.Where(r => r.EFenceCode == fence.Code && deviceCodes.Contains(r.DeviceCode))
|
|
.ToListAsync();
|
|
|
|
if (relations.Any())
|
|
{
|
|
await _dbContext.DeleteAsync(relations);
|
|
}
|
|
}
|
|
|
|
public async Task<List<EFenceDevicePageResponse>> GetDevicesByFenceIdAsync(long fenceId)
|
|
{
|
|
//查询电子围栏
|
|
var fence = await _dbContext.Queryable<EFenceEntity>().SingleAsync(f => f.Id == fenceId);
|
|
fence.ThrowIfNull("未找到电子围栏");
|
|
|
|
var deviceCodes = await _dbContext.Queryable<EFenceDeviceRelationEntity>()
|
|
.Where(x => x.EFenceCode == fence.Code)
|
|
.Select(x => x.DeviceCode)
|
|
.ToListAsync();
|
|
|
|
|
|
var list = await _dbContext.Queryable<DeviceEntity>()
|
|
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
|
|
.Where((d, dt) => deviceCodes.Contains(d.Code))
|
|
.Select((d, dt) => new EFenceDevicePageResponse
|
|
{
|
|
Factory = d.TenantId
|
|
}, true)
|
|
.ToListAsync();
|
|
|
|
return list;
|
|
}
|
|
} |