fix 根据档案号 设备组查询设备优化

This commit is contained in:
cheche
2024-03-12 10:34:31 +08:00
parent 15f89a82a2
commit 7bf97dbe5e
5 changed files with 170 additions and 38 deletions

View File

@@ -1,7 +1,11 @@
using System.ComponentModel.DataAnnotations;
using Dlhis.Basic.BusinessPlace.Response;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Basic.Organization.Services;
using Dlhis.Basic.Organization.Services.Implements;
using Kurisu.Core.DataAccess.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -23,12 +27,12 @@ public class GainfoPermissionsController : ControllerBase
/// <summary>
/// 根据用户查看餐饮点
/// </summary>
/// <param name="userNo"></param>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet("placesByGainfo")]
public async Task<List<GetPlaceByGainfoResponse>> GetPlaceByGainfoAsync([Required] string userNo)
public async Task<Pagination<BusinessPlaceResponse>> GetPlaceByGainfoAsync([FromQuery]DeviceAssRequest request)
{
return await _gainfoPermissions.GetPlaceByGainfoAsync(userNo);
return await _gainfoPermissions.GetPlaceByGainfoAsync(request);
}
/// <summary>
@@ -38,7 +42,7 @@ public class GainfoPermissionsController : ControllerBase
/// <param name="deviceGroupCode"></param>
/// <returns></returns>
[HttpGet("devicesUnderGroupByUserNo")]
public async Task<List<KeyValue<string>>> GetDevicesUnderGroupByUserNoAsync([Required] string userNo, [Required] string deviceGroupCode)
public async Task<List<GetDevicesUnderGroupByUserNoResponse>> GetDevicesUnderGroupByUserNoAsync(string userNo, string deviceGroupCode)
{
return await _gainfoPermissions.GetDevicesUnderGroupByUserNoAsync(userNo, deviceGroupCode);
}
@@ -46,11 +50,11 @@ public class GainfoPermissionsController : ControllerBase
/// <summary>
/// 获取用户所有设备权限
/// </summary>
/// <param name="userNo"></param>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet("assignDeviceCodes")]
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userNo)
public async Task<Pagination<DevicePageResponse>> GetAssignDeviceCodesAsync([FromQuery]DeviceAssRequest request)
{
return await _gainfoPermissions.GetAssignDeviceCodesAsync(userNo);
return await _gainfoPermissions.GetAssignDeviceCodesAsync(request);
}
}

View File

@@ -0,0 +1,11 @@
using Kurisu.Core.DataAccess.Dto;
using System.ComponentModel.DataAnnotations;
namespace Dlhis.Basic.Organization.Request
{
public class DeviceAssRequest : PageDto
{
[Required]
public string UserNo { get; set; }
}
}

View File

@@ -0,0 +1,10 @@
using Dlhis.Basic.Device.Response;
namespace Dlhis.Basic.Organization.Response
{
public class GetDevicesUnderGroupByUserNoResponse
{
public Guid GroupCode { get; set; }
public List<DevicePageResponse> Devices { get; set; }
}
}

View File

@@ -1,13 +1,17 @@
using Dlhis.Basic.Organization.Response;
using Dlhis.Basic.BusinessPlace.Response;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Basic.Organization.Services.Implements;
using Kurisu.Core.DataAccess.Dto;
namespace Dlhis.Basic.Organization.Services;
public interface IGainfoPermissions : IScopeDependency
{
Task<List<GetPlaceByGainfoResponse>> GetPlaceByGainfoAsync(string userNo);
Task<Pagination<BusinessPlaceResponse>> GetPlaceByGainfoAsync(DeviceAssRequest request);
Task<List<KeyValue<string>>> GetDevicesUnderGroupByUserNoAsync(string userNo, string deviceGroupCode);
Task<List<GetDevicesUnderGroupByUserNoResponse>> GetDevicesUnderGroupByUserNoAsync(string userNo, string deviceGroupCode);
Task<List<Guid>> GetAssignDeviceCodesAsync(string userNo);
Task<Pagination<DevicePageResponse>> GetAssignDeviceCodesAsync(DeviceAssRequest request);
}

View File

@@ -1,9 +1,19 @@
using Dlhis.Basic.Device.Services;
using Dlhis.Basic.BusinessPlace.Response;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Device.Services;
using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.Core.DataAccess.Dto;
using Kurisu.SqlSugar.Extensions;
using Kurisu.SqlSugar.Services;
using Org.BouncyCastle.Asn1.Ocsp;
using Org.BouncyCastle.Crypto;
using SqlSugar;
namespace Dlhis.Basic.Organization.Services.Implements;
@@ -20,15 +30,15 @@ public class GainfoPermissions : IGainfoPermissions
_dbContext = dbContext;
}
public async Task<List<GetPlaceByGainfoResponse>> GetPlaceByGainfoAsync(string userNo)
public async Task<Pagination<BusinessPlaceResponse>> GetPlaceByGainfoAsync(DeviceAssRequest request)
{
var gainfo = await _gainfoStore.Queryable()
.Where(x => x.No == userNo)
.Where(x => x.No == request.UserNo)
.FirstAsync();
if (gainfo == null)
{
return new List<GetPlaceByGainfoResponse>();
return new Pagination<BusinessPlaceResponse>();
}
//用户所有deviceCode权限
@@ -37,19 +47,29 @@ public class GainfoPermissions : IGainfoPermissions
return await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceRegistryEntity>((x, dr) => x.UniqueNo == dr.DeviceUniqueNo)
.LeftJoin<BusinessPlaceEntity>((x, dr, place) => dr.BusinessPlaceCode == place.Code)
.LeftJoin<BusinessPlaceShiftRelationshipEntity>((x,dr,place, ps) => x.Code == ps.PlaceCode)
.LeftJoin<BusinessShiftEntity>((x, dr, place, ps,s) => s.Code == ps.ShiftCode)
.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();
.Select((x, dr, place, ps, s) => new BusinessPlaceResponse
{
Id = place.Id,
Code = place.Code,
Status = place.Status,
Name = place.Name,
Catalog = place.Catalog,
CustomCode = place.CustomCode,
Source = place.Source,
ShiftCode = s.Code,
ShiftName = s.Name
})
.Distinct()
.ToPageListAsync(request);
}
public async Task<List<KeyValue<string>>> GetDevicesUnderGroupByUserNoAsync(string userNo, string deviceGroupCode)
public async Task<List<GetDevicesUnderGroupByUserNoResponse>> GetDevicesUnderGroupByUserNoAsync(string userNo, string deviceGroupCode)
{
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
var currentGroup = deviceGroups.Where(x => x.Code.ToString() == deviceGroupCode).First();
@@ -59,25 +79,108 @@ public class GainfoPermissions : IGainfoPermissions
//用户所有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>
var lst = new List<GetDevicesUnderGroupByUserNoResponse>();
foreach (var groupcode in groups)
{
Key = x.DeviceCode.ToString(),
Value = x.DeployPlace
})
.ToList();
var nos = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
.LeftJoin<DeviceEntity>((x, d) => x.DeviceCode == d.Code)
.Where(x => groupcode == x.GroupCode)
.Select((x, d) => d.UniqueNo)
.ToListAsync();
var page = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.LeftJoin<DeviceRegistryEntity>((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.LeftJoin<BusinessPlaceEntity>((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code)
.Where(d => nos.Contains(d.UniqueNo))
.Select((d, dt, dr, bp) => new DevicePageResponse
{
FuncsStr = d.Funcs.ToString(),
Brand = dt.Brand,
Type = d.Type,
BusinessPlaceCode = bp.Code,
BusinessPlaceName = bp.Name,
LastHeartbeatTime = dr.LastHeartbeatTime
})
.ToListAsync();
var deviceUniqueNos = page.Select(x => x.UniqueNo).ToList();
var balanceInfos = await _dbContext.Queryable<BalanceRecordEntity>().Where(x => x.Type == BalanceRecordTypeEnumType.Out)
.Where(x => deviceUniqueNos.Contains(x.DeviceInfo))
.Select(x => new
{
x.DeviceInfo,
Amount = SqlFunc.AggregateSum(x.Amount),
UserCount = SqlFunc.AggregateDistinctCount(x.UserCode)
})
.GroupBy(x => x.DeviceInfo)
.ToListAsync();
page.ForEach(device =>
{
var info = balanceInfos.FirstOrDefault(x => x.DeviceInfo == device.UniqueNo);
if (info != null)
{
device.Amount = info.Amount;
device.UserCount = info.UserCount;
}
});
var res = new GetDevicesUnderGroupByUserNoResponse
{
GroupCode = groupcode,
Devices = page
};
lst.Add(res);
}
return lst;
}
public Task<List<Guid>> GetAssignDeviceCodesAsync(string userNo)
public async Task<Pagination<DevicePageResponse>> GetAssignDeviceCodesAsync(DeviceAssRequest request)
{
return _devicePermissionService.GetAssignDeviceCodesAsync(userNo);
var ids = await _devicePermissionService.GetAssignDeviceCodesAsync(request.UserNo);
var page = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.LeftJoin<DeviceRegistryEntity>((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.LeftJoin<BusinessPlaceEntity>((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code)
.Where(d => ids.Contains(d.Code))
.Select((d, dt, dr, bp) => new DevicePageResponse
{
FuncsStr = d.Funcs.ToString(),
Brand = dt.Brand,
Type = d.Type,
BusinessPlaceCode = bp.Code,
BusinessPlaceName = bp.Name,
LastHeartbeatTime = dr.LastHeartbeatTime
}, true)
.ToPageListAsync(request);
var deviceUniqueNos = page.Data.Select(x => x.UniqueNo).ToList();
var balanceInfos = await _dbContext.Queryable<BalanceRecordEntity>().Where(x => x.Type == BalanceRecordTypeEnumType.Out)
.Where(x => deviceUniqueNos.Contains(x.DeviceInfo))
.Select(x => new
{
x.DeviceInfo,
Amount = SqlFunc.AggregateSum(x.Amount),
UserCount = SqlFunc.AggregateDistinctCount(x.UserCode)
})
.GroupBy(x => x.DeviceInfo)
.ToListAsync();
page.Data.ForEach(device =>
{
var info = balanceInfos.FirstOrDefault(x => x.DeviceInfo == device.UniqueNo);
if (info != null)
{
device.Amount = info.Amount;
device.UserCount = info.UserCount;
}
});
return page;
}
private IEnumerable<Guid> RecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> deviceGroups)