100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
using Dlhis.Basic.Device.Services;
|
|
using Dlhis.Basic.Organization.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Kurisu.SqlSugar.Services;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class GainfoPermissions : IGainfoPermissions
|
|
{
|
|
private readonly IGainfoStore _gainfoStore;
|
|
private readonly IDevicePermissionService _devicePermissionService;
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public GainfoPermissions(IGainfoStore gainfoStore, IDevicePermissionService devicePermissionService, IDbContext dbContext)
|
|
{
|
|
_gainfoStore = gainfoStore;
|
|
_devicePermissionService = devicePermissionService;
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<List<GetPlaceByGainfoResponse>> GetPlaceByGainfoAsync(string userNo)
|
|
{
|
|
var gainfo = await _gainfoStore.Queryable()
|
|
.Where(x => x.No == userNo)
|
|
.FirstAsync();
|
|
|
|
if (gainfo == null)
|
|
{
|
|
return new List<GetPlaceByGainfoResponse>();
|
|
}
|
|
|
|
//用户所有deviceCode权限
|
|
var deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(gainfo.No);
|
|
|
|
return await _dbContext.Queryable<DeviceEntity>()
|
|
.LeftJoin<DeviceRegistryEntity>((x, dr) => x.UniqueNo == dr.DeviceUniqueNo)
|
|
.LeftJoin<BusinessPlaceEntity>((x, dr, place) => dr.BusinessPlaceCode == place.Code)
|
|
.Where(x => x.Type == DeviceTypeEnumType.Meal)
|
|
.Where(x => deviceCodes.Contains(x.Code))
|
|
.Where((x, dr) => !string.IsNullOrEmpty(dr.BusinessPlaceCustomCode))
|
|
.Select((x, dr, place) => new GetPlaceByGainfoResponse
|
|
{
|
|
PlaceCode = place.Code,
|
|
PlaceName = place.Name
|
|
})
|
|
.Distinct()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<KeyValue<string>>> GetDevicesUnderGroupByUserNoAsync(string userNo, string deviceGroupCode)
|
|
{
|
|
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
|
var currentGroup = deviceGroups.Where(x => x.Code.ToString() == deviceGroupCode).First();
|
|
|
|
var groups = RecursionGetNext(currentGroup, deviceGroups).ToList();
|
|
|
|
//用户所有deviceCode权限
|
|
var deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(userNo);
|
|
|
|
var result = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
|
.LeftJoin<DeviceEntity>((x, d) => x.DeviceCode == d.Code)
|
|
.Where(x => groups.Contains(x.GroupCode))
|
|
.Where(x => deviceCodes.Contains(x.DeviceCode))
|
|
.Select((x, d) => new { x.DeviceCode, d.DeployPlace })
|
|
.Distinct()
|
|
.ToListAsync();
|
|
|
|
return result.Select(x => new KeyValue<string>
|
|
{
|
|
Key = x.DeviceCode.ToString(),
|
|
Value = x.DeployPlace
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
public Task<List<Guid>> GetAssignDeviceCodesAsync(string userNo)
|
|
{
|
|
return _devicePermissionService.GetAssignDeviceCodesAsync(userNo);
|
|
}
|
|
|
|
private IEnumerable<Guid> RecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> deviceGroups)
|
|
{
|
|
var next = deviceGroups.Where(x => x.PCode == current.Code).ToList();
|
|
foreach (var item in next)
|
|
{
|
|
RecursionGetNext(item, deviceGroups);
|
|
}
|
|
|
|
yield return current.Code;
|
|
}
|
|
}
|
|
|
|
public class KeyValue<T>
|
|
{
|
|
public string Key { get; set; }
|
|
public T Value { get; set; }
|
|
}
|