fix 优化细节和tenant问题
This commit is contained in:
@@ -43,6 +43,7 @@ public class DeviceGroupsController : ControllerBase
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("save")]
|
[HttpPost("save")]
|
||||||
|
[IgnoreTenant]
|
||||||
public async Task SaveAsync(DeviceGroupSaveRequest request)
|
public async Task SaveAsync(DeviceGroupSaveRequest request)
|
||||||
{
|
{
|
||||||
await _deviceGroupService.SaveAsync(request);
|
await _deviceGroupService.SaveAsync(request);
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class DevicePermissionSaveReuqest
|
|||||||
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 分配权限值不能为空
|
/// 分配权限值
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<Guid> Codes { get; set; } = new List<Guid>();
|
public List<Guid> Codes { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,11 +43,7 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
public async Task SaveAsync(DeviceGroupSaveRequest request)
|
public async Task SaveAsync(DeviceGroupSaveRequest request)
|
||||||
{
|
{
|
||||||
//检查父级节点
|
//检查父级节点
|
||||||
await CheckPCodeExistsDeviceAsync(request.PCode);
|
var pGrp = await CheckPCodeAsync(request);
|
||||||
|
|
||||||
var pGrp = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.PCode).FirstAsync();
|
|
||||||
pGrp.AbleSetNext.ThrowIfFalse($"分组{pGrp.Name}不可添加下级");
|
|
||||||
(pGrp != null && pGrp.Type != request.Type).ThrowIfTrue("分组类型必须与上级类型保持一致");
|
|
||||||
|
|
||||||
if (!request.Id.HasValue)
|
if (!request.Id.HasValue)
|
||||||
{
|
{
|
||||||
@@ -59,8 +55,8 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var entity = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
var entity = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
||||||
entity.ThrowIfNull("设备组不存在,无法更新");
|
entity.ThrowIfNull("设备分组不存在,无法更新");
|
||||||
entity.Editable.ThrowIfFalse("不可编辑当前组");
|
entity.Editable.ThrowIfFalse("不可编辑当前分组");
|
||||||
|
|
||||||
if (request.Type != entity.Type)
|
if (request.Type != entity.Type)
|
||||||
{
|
{
|
||||||
@@ -84,7 +80,7 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
public async Task DeleteAsync(long id)
|
public async Task DeleteAsync(long id)
|
||||||
{
|
{
|
||||||
var deviceGroup = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Id == id).FirstAsync();
|
var deviceGroup = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Id == id).FirstAsync();
|
||||||
deviceGroup.ThrowIfNull("设备组不存在,无需删除");
|
deviceGroup.ThrowIfNull("设备分组不存在,无需删除");
|
||||||
|
|
||||||
await _dbContext.DeleteAsync(deviceGroup);
|
await _dbContext.DeleteAsync(deviceGroup);
|
||||||
}
|
}
|
||||||
@@ -131,28 +127,35 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
/****************************************************** help methods *********************************************************/
|
/****************************************************** help methods *********************************************************/
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检查父级分组存在设备
|
/// 检查父级分组
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pCode"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private async Task CheckPCodeExistsDeviceAsync(Guid? pCode)
|
private async Task<DeviceGroupEntity> CheckPCodeAsync(DeviceGroupSaveRequest request)
|
||||||
{
|
{
|
||||||
if (pCode.HasValue)
|
|
||||||
|
var pGrp = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.PCode).FirstAsync();
|
||||||
|
if (pGrp != null)
|
||||||
{
|
{
|
||||||
var exists = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == pCode).AnyAsync();
|
(!pGrp.AbleSetNext && !request.Id.HasValue).ThrowIfTrue($"{pGrp.Name}不可添加下级");
|
||||||
|
var exists = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.PCode).AnyAsync();
|
||||||
exists.ThrowIfTrue("父级分组存在设备");
|
exists.ThrowIfTrue("父级分组存在设备");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(pGrp != null && pGrp.Type != request.Type).ThrowIfTrue("分组类型必须与上级类型保持一致");
|
||||||
|
|
||||||
|
return pGrp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
/// 检查分组是否存在下级
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="currentCode"></param>
|
/// <param name="currentCode"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private async Task CheckNoNextGroupAsync(Guid currentCode)
|
private async Task CheckNoNextGroupAsync(Guid currentCode)
|
||||||
{
|
{
|
||||||
var exists = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.PCode == currentCode).AnyAsync();
|
var exists = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.PCode == currentCode).AnyAsync();
|
||||||
exists.ThrowIfTrue("存在下级设备分组");
|
exists.ThrowIfTrue("存在下级设备分组,无法分配设备");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -165,7 +168,7 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
{
|
{
|
||||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => request.DeviceCodes.Contains(x.Code)).ToListAsync();
|
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => request.DeviceCodes.Contains(x.Code)).ToListAsync();
|
||||||
//检查分组的分类控制
|
//检查分组的分类控制
|
||||||
devices.Any(x => x.Type != group.Type).ThrowIfTrue("存在设备类型有误,无法分配到当前分组");
|
devices.Any(x => x.Type != group.Type).ThrowIfTrue($"存在设备类型不一致,无法分配到当前{group.Type.GetDescription()}分组");
|
||||||
|
|
||||||
//重新赋值到请求参数中
|
//重新赋值到请求参数中
|
||||||
request.DeviceCodes = devices.Select(x => x.Code).ToList();
|
request.DeviceCodes = devices.Select(x => x.Code).ToList();
|
||||||
|
|||||||
@@ -47,19 +47,25 @@ public class DeviceService : IDeviceService
|
|||||||
|
|
||||||
var deviceUniqueNos = page.Data.Select(x => x.UniqueNo).ToList();
|
var deviceUniqueNos = page.Data.Select(x => x.UniqueNo).ToList();
|
||||||
|
|
||||||
var balanceRecords = await _dbContext.Queryable<BalanceRecordEntity>().Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
var balanceInfos = await _dbContext.Queryable<BalanceRecordEntity>().Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||||||
.Where(x => deviceUniqueNos.Contains(x.DeviceInfo))
|
.Where(x => deviceUniqueNos.Contains(x.DeviceInfo))
|
||||||
.Select(x => new
|
.Select(x => new
|
||||||
{
|
{
|
||||||
x.Amount,
|
x.DeviceInfo,
|
||||||
x.UserCode
|
Amount = SqlFunc.AggregateSum(x.Amount),
|
||||||
|
UserCount = SqlFunc.AggregateDistinctCount(x.UserCode)
|
||||||
})
|
})
|
||||||
|
.GroupBy(x => x.DeviceInfo)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
page.Data.ForEach(device =>
|
page.Data.ForEach(device =>
|
||||||
{
|
{
|
||||||
device.Amount = balanceRecords.Sum(x => x.Amount);
|
var info = balanceInfos.FirstOrDefault(x => x.DeviceInfo == device.UniqueNo);
|
||||||
device.UserCount = balanceRecords.DistinctBy(x => x.UserCode).Count();
|
if (info != null)
|
||||||
|
{
|
||||||
|
device.Amount = info.Amount;
|
||||||
|
device.UserCount = info.UserCount;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return page;
|
return page;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Dlhis.Entity.Sys.Entities;
|
|||||||
public class DevicePermissionEntity : SugarBaseEntity, ITenantId
|
public class DevicePermissionEntity : SugarBaseEntity, ITenantId
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 分配权限类型
|
/// 分配权限类型(User,Role,Group...)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SugarColumn(ColumnDataType = "varchar(50)", ColumnDescription = "分配权限类型", SqlParameterDbType = typeof(EnumToStringConvert))]
|
[SugarColumn(ColumnDataType = "varchar(50)", ColumnDescription = "分配权限类型", SqlParameterDbType = typeof(EnumToStringConvert))]
|
||||||
public AssignTypeEnumType AssignType { get; set; }
|
public AssignTypeEnumType AssignType { get; set; }
|
||||||
@@ -25,7 +25,7 @@ public class DevicePermissionEntity : SugarBaseEntity, ITenantId
|
|||||||
public string AssignValue { get; set; }
|
public string AssignValue { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备权限类型
|
/// 设备权限类型(Device/DeviceGroup)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SugarColumn(ColumnDataType = "varchar(50)", ColumnDescription = "设备权限类型", SqlParameterDbType = typeof(EnumToStringConvert))]
|
[SugarColumn(ColumnDataType = "varchar(50)", ColumnDescription = "设备权限类型", SqlParameterDbType = typeof(EnumToStringConvert))]
|
||||||
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||||
|
|||||||
@@ -38,11 +38,11 @@ public class HeartbeatJob
|
|||||||
await _dbContext.Updateable<DeviceRegistryEntity>().SetColumns(x => x.LastHeartbeatTime == DateTime.Now)
|
await _dbContext.Updateable<DeviceRegistryEntity>().SetColumns(x => x.LastHeartbeatTime == DateTime.Now)
|
||||||
.Where(x => x.DeviceUniqueNo == device.UniqueNo).ExecuteCommandAsync();
|
.Where(x => x.DeviceUniqueNo == device.UniqueNo).ExecuteCommandAsync();
|
||||||
|
|
||||||
_logger.LogInformation("心跳设备成功:{no}", device.UniqueNo);
|
// _logger.LogInformation("心跳设备成功:{no}", device.UniqueNo);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "心跳设备失败:{no}. {message}", device.UniqueNo, ex.Message);
|
//_logger.LogError(ex, "心跳设备失败:{no}. {message}", device.UniqueNo, ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user