@@ -1,9 +1,6 @@
using Dlhis.Basic.Device.Request ;
using Dlhis.Basic.Device.Request ;
using Dlhis.Basic.Device.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 Dlhis.Sys.Api ;
@@ -17,7 +14,6 @@ using Kurisu.AspNetCore.Utils.Extensions;
using Mapster ;
using Newtonsoft.Json ;
using Newtonsoft.Json.Linq ;
using SqlSugar ;
namespace Dlhis.Basic.Device.Services.Implements ;
@@ -37,22 +33,22 @@ public class DeviceService : IDeviceService
public async Task < Pagination < DevicePageResponse > > GetPageAsync ( DevicePageRequest request )
{
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 )
. WhereIF ( request . Type . HasValue , ( d , dt ) = > d . Type = = request . Type )
. WhereIF ( request . Enable . HasValue , ( d , dt ) = > d . Enable = = request . Enable )
. WhereIF ( request . Search . IsPresent ( ) , d = > d . UniqueNo = = request . Search | | d . DeployPlace . Contains ( request . Search ) )
. 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 ) ;
. 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 )
. WhereIF ( request . Type . HasValue , ( d , dt ) = > d . Type = = request . Type )
. WhereIF ( request . Enable . HasValue , ( d , dt ) = > d . Enable = = request . Enable )
. WhereIF ( request . Search . IsPresent ( ) , d = > d . UniqueNo = = request . Search | | d . DeployPlace . Contains ( request . Search ) )
. 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 ) ;
return page ;
}
@@ -95,9 +91,9 @@ public class DeviceService : IDeviceService
if ( request . Type ! = entity . Type )
{
var existsG = await _dbContext . Queryable < DeviceGroupRelationshipEntity > ( )
. LeftJoin < DeviceGroupEntity > ( ( dgr , dg ) = > dgr . GroupCode = = dg . Code )
. Where ( ( dgr , dg ) = > dgr . DeviceCode = = entity . Code )
. AnyAsync ( ) ;
. LeftJoin < DeviceGroupEntity > ( ( dgr , dg ) = > dgr . GroupCode = = dg . Code )
. Where ( ( dgr , dg ) = > dgr . DeviceCode = = entity . Code )
. AnyAsync ( ) ;
existsG . ThrowIfTrue ( "存在设备分组,不可修改设备分类" ) ;
}
@@ -159,9 +155,9 @@ public class DeviceService : IDeviceService
if ( entity . Type is DeviceTypeEnumType . Door or DeviceTypeEnumType . Attendance )
{
var exists = await _dbContext . Queryable < DeviceUploadLogEntity > ( )
. Where ( x = > x . DeviceUniqueNo = = entity . UniqueNo )
. Where ( x = > x . Type = = DeviceUploadLogTypeEnumType . Raise )
. AnyAsync ( ) ;
. Where ( x = > x . DeviceUniqueNo = = entity . UniqueNo )
. Where ( x = > x . Type = = DeviceUploadLogTypeEnumType . Raise )
. AnyAsync ( ) ;
if ( ! exists )
{
@@ -236,9 +232,9 @@ public class DeviceService : IDeviceService
{
//查询设备是否存在
var device = await _dbContext . Queryable < DeviceEntity > ( )
. Where ( x = > x . UniqueNo = = request . UniqueNo )
. WhereIF ( request . Id . HasValue , x = > x . Id ! = request . Id )
. FirstAsync ( ) ;
. Where ( x = > x . UniqueNo = = request . UniqueNo )
. WhereIF ( request . Id . HasValue , x = > x . Id ! = request . Id )
. FirstAsync ( ) ;
if ( device ! = null )
{
@@ -252,7 +248,6 @@ public class DeviceService : IDeviceService
}
catch ( Exception )
{
}
true . ThrowIfTrue ( $"设备已添加{factory}厂区" ) ;
@@ -271,8 +266,8 @@ public class DeviceService : IDeviceService
try
{
return await _dbContext . Queryable < DeviceEntity > ( )
. Where ( x = > x . UniqueNo = = deviceUniqueNo & & x . Enable )
. AnyAsync ( ) ;
. Where ( x = > x . UniqueNo = = deviceUniqueNo & & x . Enable )
. AnyAsync ( ) ;
}
finally
{
@@ -293,7 +288,6 @@ public class DeviceService : IDeviceService
{
FuncsStr = device . Funcs . ToString ( ) ,
ConnectionInfoStr = device . ConnectionInfo
} , true )
. SingleAsync ( ) ;
@@ -328,6 +322,7 @@ public class DeviceService : IDeviceService
} ;
return res ;
}
/// <summary>
/// 获取离线设备
/// </summary>
@@ -349,22 +344,17 @@ public class DeviceService : IDeviceService
} )
. ToListAsync ( ) ;
var onlineDevices = deviceRegistries
. Where ( x = >
{
if ( x . HeartbeatInterval < = 0 )
{
return true ;
}
var onlineDevices = deviceRegistries . Where ( x = >
{
if ( x . HeartbeatInterval < = 0 ) return true ;
return x . LastHeartbeatTime . AddSeconds ( x . HeartbeatInterval * 3 ) > = now ;
} )
. Select ( x = > new
{
x . UniqueNo
} )
. ToList ( ) ;
return x . LastHeartbeatTime . AddSeconds ( x . HeartbeatInterval * 3 ) > = now ;
} )
. Select ( x = > new
{
x . UniqueNo ,
} )
. ToList ( ) ;
var onlineUniqueNos = new List < string > ( ) ;
onlineDevices . ForEach ( x = > onlineUniqueNos . Add ( x . UniqueNo ) ) ;
@@ -377,110 +367,31 @@ public class DeviceService : IDeviceService
public async Task < Pagination < DevicePageResponse > > GetAttendanceTotalAsync ( AttendanceTotalRequest request )
{
var page = await _dbContext . Queryable < DeviceEntity > ( )
. LeftJoin < DeviceTemplateEntity > ( ( d , dt ) = > d . TemplateCode = = dt . Code )
. Where ( d = > d . Type = = DeviceTypeEnumType . Attendance | | ( d . Type = = DeviceTypeEnumType . Door & & d . AsAttendance ) )
. Select ( ( d , dt ) = > new DevicePageResponse
{
FuncsStr = d . Funcs . ToString ( ) ,
Brand = dt . Brand ,
Type = d . Type ,
} , true )
. ToPageListAsync ( request ) ;
. LeftJoin < DeviceTemplateEntity > ( ( d , dt ) = > d . TemplateCode = = dt . Code )
. Where ( d = > d . Type = = DeviceTypeEnumType . Attendance | | ( d . Type = = DeviceTypeEnumType . Door & & d . AsAttendance ) )
. Select ( ( d , dt ) = > new DevicePageResponse
{
FuncsStr = d . Funcs . ToString ( ) ,
Brand = dt . Brand ,
Type = d . Type ,
} , true )
. ToPageListAsync ( request ) ;
return page ;
}
public async Task < Pagination < DevicePageResponse > > GetAttendanceOfflineAsync ( DeviceOnlineAndOfflineRequest request )
{
var devices = await _dbContext . Queryable < DeviceEntity > ( ) . Where ( x = > x . Type = = DeviceTypeEnumType . Attendance | | ( x . Type = = DeviceTypeEnumType . Door & & x . As Attendance) ) . ToListAsync ( ) ;
//离线
var offDevices = await GetOfflineDevicesCount ( devices ) ;
var offids = offDevices . Select ( x = > x . Id ) . ToList ( ) ;
if ( request . IsOnAndOff )
{
//离线
var offpage = await _dbContext . Queryable < DeviceEntity > ( )
. LeftJoin < DeviceTemplateEntity > ( ( d , dt ) = > d . TemplateCode = = dt . Code )
. Where ( d = > d . Type = = DeviceTypeEnumType . Attendance )
. Where ( d = > offids . Contains ( d . Id ) )
. Select ( ( d , dt ) = > new DevicePageResponse
{
FuncsStr = d . Funcs . ToString ( ) ,
Brand = dt . Brand ,
Type = d . Type ,
} , true )
. ToPageListAsync ( request ) ;
return offpage ;
}
//在线
var ids = devices . Select ( x = > x . Id ) . ToList ( ) ;
List < long > onids = ids . Except ( offids ) . ToList ( ) ;
var page = await _dbContext . Queryable < DeviceEntity > ( )
. LeftJoin < DeviceTemplateEntity > ( ( d , dt ) = > d . TemplateCode = = dt . Code )
. Where ( d = > d . Type = = DeviceTypeEnumType . Attendance )
. Where ( d = > onids . Contains ( d . Id ) )
. Select ( ( d , dt ) = > new DevicePageResponse
{
FuncsStr = d . Funcs . ToString ( ) ,
Brand = dt . Brand ,
Type = d . Type ,
} , true )
. ToPageListAsync ( request ) ;
return page ;
return await GetDeviceStatusPageAsync ( request . IsOnAndOff , request , DeviceTypeEnumType . Attendance ) ;
}
public async Task < Pagination < DevicePageResponse > > GetDoorOfflineAsync ( GetDoorOfflineRequest request )
{
var devices = await _dbContext . Queryable < DeviceEntity > ( ) . Where ( x = > x . Type = = DeviceTypeEnumType . Door ) . ToListAsync ( ) ;
var offDevices = await GetOfflineDevicesCount ( devices ) ;
var offids = offDevices . Select ( x = > x . Id ) . ToList ( ) ;
if ( request . IsOnAndOff )
{
//离线
var offpage = await _dbContext . Queryable < DeviceEntity > ( )
. LeftJoin < DeviceTemplateEntity > ( ( d , dt ) = > d . TemplateCode = = dt . Code )
. Where ( d = > d . Type = = DeviceTypeEnumType . Door )
. Where ( d = > offids . Contains ( d . Id ) )
. Select ( ( d , dt ) = > new DevicePageResponse
{
FuncsStr = d . Funcs . ToString ( ) ,
Brand = dt . Brand ,
Type = d . Type ,
} , true )
. ToPageListAsync ( request ) ;
return offpage ;
}
//在线
var ids = devices . Select ( x = > x . Id ) . ToList ( ) ;
List < long > onids = ids . Except ( offids ) . ToList ( ) ;
var page = await _dbContext . Queryable < DeviceEntity > ( )
. LeftJoin < DeviceTemplateEntity > ( ( d , dt ) = > d . TemplateCode = = dt . Code )
. Where ( d = > d . Type = = DeviceTypeEnumType . Door )
. Where ( d = > onids . Contains ( d . Id ) )
. Select ( ( d , dt ) = > new DevicePageResponse
{
FuncsStr = d . Funcs . ToString ( ) ,
Brand = dt . Brand ,
Type = d . Type ,
} , true )
. ToPageListAsync ( request ) ;
return page ;
return await GetDeviceStatusPageAsync ( request . IsOnAndOff , request , DeviceTypeEnumType . Door ) ;
}
public async Task < List < RecentNumberResponse > > GetRecentNumbersAsync ( )
{
//获取近30天并转化为"yyyy-MM-dd"集合
@@ -509,7 +420,35 @@ public class DeviceService : IDeviceService
return list ;
}
}
//************************************************************* help methods ************************************************************************
private async Task < Pagination < DevicePageResponse > > GetDeviceStatusPageAsync ( bool isOnAndOff , PageDto pageDto , DeviceTypeEnumType deviceType )
{
var devices = deviceType = = DeviceTypeEnumType . Attendance
? await _dbContext . Queryable < DeviceEntity > ( ) . Where ( x = > x . Type = = DeviceTypeEnumType . Attendance | | ( x . Type = = DeviceTypeEnumType . Door & & x . AsAttendance ) ) . ToListAsync ( )
: await _dbContext . Queryable < DeviceEntity > ( ) . Where ( x = > x . Type = = DeviceTypeEnumType . Door ) . ToListAsync ( ) ;
var offDevices = await GetOfflineDevicesCount ( devices ) ;
//离线
var ids = offDevices . Select ( x = > x . Id ) . ToList ( ) ;
if ( ! isOnAndOff )
{
ids = devices . Select ( x = > x . Id ) . Except ( ids ) . ToList ( ) ;
}
return await _dbContext . Queryable < DeviceEntity > ( )
. LeftJoin < DeviceTemplateEntity > ( ( d , dt ) = > d . TemplateCode = = dt . Code )
. Where ( d = > d . Type = = DeviceTypeEnumType . Attendance )
. Where ( d = > ids . Contains ( d . Id ) )
. WhereIF ( pageDto . Search . IsPresent ( ) , d = > d . UniqueNo = = pageDto . Search
| | d . DeployPlace . Contains ( pageDto . Search ) )
. Select ( ( d , dt ) = > new DevicePageResponse
{
FuncsStr = d . Funcs . ToString ( ) ,
Brand = dt . Brand ,
Type = d . Type ,
} , true )
. ToPageListAsync ( pageDto ) ;
}
}