Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
node
/
app-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 01ea76a8
authored
Sep 27, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
重构服务类
1 parent
d01edbac
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
237 additions
and
183 deletions
src/dto/base.dto.ts
src/dto/config.dto.ts
src/dto/device.dto.ts
src/dto/device.status.dto.ts
src/modules/broadcast/broadcast.service.ts
src/modules/config/config.controller.ts
src/modules/config/config.service.ts
src/modules/device/device.controller.ts
src/modules/device/device.service.ts
src/modules/fields/fields.service.ts
src/modules/file/file.service.ts
src/typings/device.d.ts
src/typings/device.status.d.ts
src/typings/relations.d.ts
src/typings/userFileds.d.ts
src/typings/ws.d.ts
test.http
src/dto/base.dto.ts
View file @
01ea76a
...
...
@@ -13,11 +13,10 @@ export function IsNumberOrNumberString(validationOptions?: ValidationOptions) {
validator
:
{
validate
(
value
:
any
,
args
:
ValidationArguments
)
{
// 如果值为空(null, undefined, ''),则验证通过
if
(
value
===
''
)
return
false
if
(
value
===
''
)
return
false
if
(
value
===
null
||
value
===
undefined
)
{
return
true
}
// 尝试将值转换为数字,如果转换成功或原本就是数字,则验证通过
// 注意:这里使用了 Number.isNaN 来检查转换结果是否为 NaN,从而排除非数字字符串
return
!
Number
.
isNaN
(
Number
(
value
))
...
...
src/dto/config.dto.ts
View file @
01ea76a
...
...
@@ -16,13 +16,6 @@ model log_config {
export
class
LogConfigDto
{
@
ApiProperty
({
type
:
Number
,
description
:
'设备信息ID'
,
required
:
false
,
default
:
0
})
id
:
number
@
ApiProperty
({
type
:
Number
,
description
:
'设备ID'
,
required
:
true
,
default
:
0
...
...
@@ -55,12 +48,15 @@ export class LogConfigDto {
})
updated_at
:
Date
|
string
}
export
class
LogConfigUpdateDto
{
@
ApiProperty
({
type
:
Array
,
description
:
'
需要上传的用户字段
'
,
type
:
Number
,
description
:
'
日志保存天数
'
,
required
:
false
,
default
:
[]
default
:
''
})
user_fields
:
[]
log_saved_days
:
number
}
\ No newline at end of file
src/dto/device.dto.ts
View file @
01ea76a
import
{
IsString
,
IsNumber
,
IsNotEmpty
,
MaxLength
,
IsMobilePhone
,
IsOptional
,
isNotEmpty
}
from
'class-validator'
import
{
ApiProperty
}
from
'@nestjs/swagger'
import
{
BaseDto
}
from
'./base.dto'
import
{
LogConfigUpdateDto
}
from
'./config.dto'
export
class
UserBaseDto
{
@
ApiProperty
({
type
:
String
,
description
:
'设备用户'
,
default
:
''
})
user
?:
string
@
IsMobilePhone
(
'zh-CN'
,
{},
{
message
:
'手机号格式不正确'
})
@
IsOptional
()
@
ApiProperty
({
type
:
String
,
description
:
'使用者手机号'
,
default
:
''
})
mobile
?:
string
@
ApiProperty
({
type
:
String
,
description
:
'使用者商户名称'
,
default
:
''
})
merchant_name
?:
string
}
export
class
DeviceQueryDto
extends
BaseDto
{
@
ApiProperty
({
...
...
@@ -19,6 +45,8 @@ export class DeviceQueryDto extends BaseDto {
})
user
?:
string
@
IsMobilePhone
(
'zh-CN'
,
{},
{
message
:
'手机号格式不正确'
})
@
IsOptional
()
@
ApiProperty
({
type
:
String
,
description
:
'使用者手机号'
,
...
...
@@ -26,6 +54,13 @@ export class DeviceQueryDto extends BaseDto {
default
:
''
})
mobile
?:
string
@
ApiProperty
({
type
:
Number
,
description
:
'在线状态 1:在线,2:离线,0:弃用'
,
required
:
false
})
status
?:
number
}
export
class
DeviceFindOneDto
{
...
...
@@ -48,7 +83,7 @@ export class DeviceFindOneDto {
mobile
:
string
}
export
class
AddDeviceDto
{
export
class
AddDeviceDto
extends
UserBaseDto
{
@
ApiProperty
({
type
:
String
,
description
:
'设备SN'
,
...
...
@@ -59,28 +94,47 @@ export class AddDeviceDto {
@
IsNotEmpty
({
message
:
'设备SN不能为空'
})
sn
:
string
}
/**
* model device_info {
id Int @id @default(autoincrement())
device_id Int @unique(map: "device_id") @default(0)
user String? @db.VarChar(20)
status Int? @db.TinyInt
last_log_upload_time String? @db.VarChar(30)
last_log_remove_time String? @db.VarChar(30)
created_at DateTime @default(now()) @db.Timestamp(0)
updated_at DateTime @default(now()) @db.Timestamp(0)
mobile String? @db.VarChar(20)
merchant_name String? @db.VarChar(50)
log_config log_config?
export
class
AddBaseDto
{
}
*/
export
class
AddDeviceInfoDto
{
@
ApiProperty
({
description
:
'SN 设备表的SN'
})
sn
?:
string
@
ApiProperty
({
description
:
'设备信息 包括 user mobile merchant_name'
})
device_info
?:
UserBaseDto
@
ApiProperty
({
description
:
'设备配置信息 包括 log_saved_days'
})
log_config
?:
LogConfigUpdateDto
@
ApiProperty
({
description
:
'设备对应的日志字段集合 '
})
user_fields
?:
Array
<
Number
>
@
ApiProperty
({
type
:
Number
,
description
:
'
设备信息ID
'
,
default
:
0
description
:
'
创建时间
'
,
default
:
''
})
id
:
number
created_at
:
Date
@
ApiProperty
({
type
:
Date
,
description
:
'更新时间'
,
default
:
''
})
updated_at
:
Date
}
export
class
UpdateDeviceInfoDto
{
@
ApiProperty
({
type
:
Number
,
description
:
'设备ID'
,
...
...
@@ -91,32 +145,23 @@ export class AddDeviceInfoDto {
device_id
:
number
@
ApiProperty
({
type
:
String
,
description
:
'设备用户'
,
default
:
''
description
:
'SN 设备表的SN'
})
user
:
string
sn
?
:
string
@
ApiProperty
({
type
:
Number
,
description
:
'设备状态'
,
default
:
0
description
:
'设备信息 包括 user mobile merchant_name'
})
status
:
number
device_info
?:
UserBaseDto
@
ApiProperty
({
type
:
String
,
description
:
'最后一次上传日志的时间'
,
default
:
''
description
:
'设备配置信息 包括 log_saved_days'
})
l
ast_log_upload_time
:
string
l
og_config
?:
LogConfigUpdateDto
@
ApiProperty
({
type
:
String
,
description
:
'最后一次移除日志的时间'
,
default
:
''
description
:
'设备对应的日志字段集合 '
})
last_log_remove_time
:
string
user_fields
?:
Array
<
Number
>
@
ApiProperty
({
type
:
Number
,
...
...
@@ -131,27 +176,5 @@ export class AddDeviceInfoDto {
default
:
''
})
updated_at
:
Date
@
IsMobilePhone
(
'zh-CN'
,
{},
{
message
:
'手机号格式不正确'
})
@
IsOptional
()
@
ApiProperty
({
type
:
String
,
description
:
'使用者手机号'
,
default
:
''
})
mobile
:
string
@
ApiProperty
({
type
:
String
,
description
:
'使用者商户名称'
,
default
:
''
})
merchant_name
:
string
@
ApiProperty
({
type
:
Object
,
description
:
'日志配置'
,
default
:
{}
})
log_config
:
{}
}
src/dto/device.status.dto.ts
View file @
01ea76a
...
...
@@ -5,26 +5,44 @@ export class DeviceStatusDto {
@
ApiProperty
({
type
:
Number
,
description
:
'设备ID'
,
default
:
0
required
:
true
,
})
@
IsNumber
({},
{
message
:
'设备ID必须是数字'
})
@
IsNotEmpty
({
message
:
'设备ID不能为空'
})
device_id
:
number
@
IsNumber
({},
{
message
:
'设备状态必须是数字'
})
@
IsNotEmpty
({
message
:
'设备状态不能为空'
})
@
ApiProperty
({
type
:
Number
,
description
:
'设备状态'
,
default
:
0
required
:
false
,
})
status
:
number
status
?
:
number
@
IsNotEmpty
({
message
:
'SocketID不能为空'
})
@
ApiProperty
({
type
:
String
,
description
:
'
SocketID
'
,
default
:
''
description
:
'
最后一次上传日志的时间
'
,
required
:
false
,
})
socket_id
:
string
last_log_upload_time
?:
string
@
ApiProperty
({
type
:
String
,
description
:
'最后一次移除日志的时间'
,
required
:
false
,
})
last_log_remove_time
?:
string
@
ApiProperty
({
type
:
String
,
description
:
'最后一次登录时间'
,
required
:
false
,
})
last_login_time
?:
string
@
ApiProperty
({
type
:
String
,
description
:
'最后一次离线时间'
,
required
:
false
,
})
last_offline_time
?:
string
}
src/modules/broadcast/broadcast.service.ts
View file @
01ea76a
...
...
@@ -17,8 +17,9 @@ export class BroadcastService {
}
// // 广播消息
async
onMessage
(
param
:
any
):
Promise
<
CommonTypes
.
IResData
>
{
if
(
param
?.
code
===
RESPONSE_CODE
.
SUCCESS
&&
param
?.
data
?.
command
===
Command
.
RemoveLog
)
{
this
.
eventEmitter
.
emit
(
Events
.
OnLogFileRemoved
,
{
device_id
:
param
.
device_id
,
last_log_remove_time
:
new
Date
()
})
if
(
param
?.
code
==
RESPONSE_CODE
.
SUCCESS
&&
param
?.
data
?.
command
===
Command
.
RemoveLog
)
{
const
statusInfo
:
DeviceStatus
.
IDeviceStatus
=
{
device_id
:
param
.
device_id
,
last_log_remove_time
:
new
Date
()
}
this
.
eventEmitter
.
emit
(
Events
.
OnLogFileRemoved
,
statusInfo
)
return
}
const
res
:
AxiosResponse
<
any
,
any
>
=
await
this
.
httpService
...
...
src/modules/config/config.controller.ts
View file @
01ea76a
...
...
@@ -16,12 +16,12 @@ export class ConfigController {
@
UsePipes
(
new
PagesPipe
())
@
UsePipes
(
new
ParseDeviceIdPipe
())
@
ApiOperation
({
summary
:
'根据设备号获取配置'
})
async
findAll
(@
Query
()
params
:
LogConfigDto
):
Promise
<
CommonTypes
.
IResData
<
ConfigTypes
.
LogConfigRes
ponse
[]
>>
{
async
findAll
(@
Query
()
params
:
LogConfigDto
):
Promise
<
CommonTypes
.
IResData
<
ConfigTypes
.
LogConfigRes
ult
[]
>>
{
return
this
.
configService
.
findAll
(
params
)
}
@
Post
(
'/add'
)
@
ApiOperation
({
summary
:
'新增
设备配置
'
})
add
Config
(@
Body
()
param
:
LogConfigDto
):
Promise
<
CommonTypes
.
IResData
>
{
return
this
.
configService
.
add
Config
(
param
)
@
ApiOperation
({
summary
:
'新增
或更新设备配置,de
'
})
upsert
Config
(@
Body
()
param
:
LogConfigDto
):
Promise
<
CommonTypes
.
IResData
>
{
return
this
.
configService
.
upsert
Config
(
param
)
}
}
src/modules/config/config.service.ts
View file @
01ea76a
...
...
@@ -27,7 +27,7 @@ export class ConfigService {
return
resBody
}
async
add
Config
(
config
:
ConfigTypes
.
ILogConfig
):
Promise
<
CommonTypes
.
IResData
>
{
async
upsert
Config
(
config
:
ConfigTypes
.
ILogConfig
):
Promise
<
CommonTypes
.
IResData
>
{
const
now
=
new
Date
()
await
this
.
prisma
.
log_config
.
upsert
({
where
:
{
device_id
:
config
.
device_id
},
update
:
{
...
config
,
updated_at
:
now
},
create
:
{
...
config
,
created_at
:
now
,
updated_at
:
now
}
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
...
...
src/modules/device/device.controller.ts
View file @
01ea76a
import
{
Controller
,
Get
,
Post
,
Body
,
UseInterceptors
,
Query
,
UsePipes
,
Inject
}
from
'@nestjs/common'
import
{
DeviceService
}
from
'./device.service'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
import
{
AddDeviceDto
,
AddDeviceInfoDto
,
DeviceFindOneDto
,
DeviceQueryDto
,
DeviceStatusDto
}
from
'../../dto'
import
{
AddDeviceDto
,
AddDeviceInfoDto
,
DeviceFindOneDto
,
DeviceQueryDto
,
DeviceStatusDto
,
UpdateDeviceInfoDto
}
from
'../../dto'
import
{
PagesPipe
,
ValidationPipe
}
from
'../../common/pipes'
import
{
OnEvent
,
EventEmitter2
}
from
'@nestjs/event-emitter'
import
{
WsAccess
,
Events
,
WsMessageType
,
Command
,
RESPONSE_CODE
}
from
'../../common/enums'
...
...
@@ -49,7 +49,7 @@ export class DeviceController {
*/
@
Post
(
'/add'
)
@
ApiOperation
({
summary
:
'添加设备'
})
async
addDevice
(@
Body
()
param
:
AddDeviceDto
):
Promise
<
CommonTypes
.
IResponseBase
>
{
async
addDevice
(@
Body
()
param
:
AddDevice
Info
Dto
):
Promise
<
CommonTypes
.
IResponseBase
>
{
// console.log(param)
return
await
this
.
deviceService
.
addDevice
(
param
)
}
...
...
@@ -61,7 +61,7 @@ export class DeviceController {
*/
@
Post
(
'/addInfo'
)
@
ApiOperation
({
summary
:
'添加设备信息'
})
async
addDeviceInfo
(@
Body
()
param
:
AddDeviceInfoDto
):
Promise
<
CommonTypes
.
IResponseBase
>
{
async
addDeviceInfo
(@
Body
()
param
:
UpdateDeviceInfoDto
):
Promise
<
CommonTypes
.
IResData
>
{
// console.log(param)
return
await
this
.
deviceService
.
addDeviceInfo
(
param
)
}
...
...
@@ -74,8 +74,7 @@ export class DeviceController {
*/
@
Post
(
'/updateInfo'
)
@
ApiOperation
({
summary
:
'修改设备信息'
})
@
OnEvent
(
Events
.
OnLogFileRemoved
)
async
updateDeviceInfo
(@
Body
()
param
:
AddDeviceInfoDto
):
Promise
<
CommonTypes
.
IResponseBase
>
{
async
updateDeviceInfo
(@
Body
()
param
:
UpdateDeviceInfoDto
):
Promise
<
CommonTypes
.
IResData
>
{
return
await
this
.
deviceService
.
updateDeviceInfo
(
param
)
}
...
...
@@ -86,9 +85,10 @@ export class DeviceController {
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@
OnEvent
(
Events
.
OnConnected
)
@
OnEvent
(
Events
.
OnLogFileRemoved
)
@
Post
(
'/upsertStatus'
)
@
ApiOperation
({
summary
:
'更新设备状态'
})
async
upsertDeviceStatus
(@
Body
()
param
:
DeviceStatusDto
):
Promise
<
void
|
CommonTypes
.
IRes
ponseBase
>
{
async
upsertDeviceStatus
(@
Body
()
param
:
DeviceStatusDto
):
Promise
<
void
|
CommonTypes
.
IRes
Data
>
{
return
await
this
.
deviceService
.
upsertDeviceStatus
(
param
).
catch
(
error
=>
{
const
out
:
WsTypes
.
WsConnError
=
{
message
:
WsAccess
.
ConnFailure
,
device_id
:
param
.
device_id
}
this
.
eventEmitter
.
emit
(
Events
.
OnConnectError
,
out
)
...
...
@@ -104,7 +104,7 @@ export class DeviceController {
@
Post
(
'/offlineAll'
)
@
OnEvent
(
Events
.
OnOfflineAll
)
@
ApiOperation
({
summary
:
'修改所有设备状态为离线'
})
async
resetAllDeviceStatus
():
Promise
<
void
|
CommonTypes
.
IRes
ponseBase
>
{
async
resetAllDeviceStatus
():
Promise
<
void
|
CommonTypes
.
IRes
Data
>
{
return
await
this
.
deviceService
.
resetAllDeviceStatus
()
}
...
...
@@ -116,7 +116,14 @@ export class DeviceController {
}
@
Get
(
'/addAll'
)
@
ApiOperation
({
summary
:
'批量导入设备,用于初始化数据'
})
async
addAll
(
devices
:
Array
<
any
>
):
Promise
<
void
|
CommonTypes
.
IRes
ponseBase
>
{
async
addAll
(
devices
:
Array
<
any
>
):
Promise
<
void
|
CommonTypes
.
IRes
Data
>
{
// return this.deviceService.addAll(devices)
}
@
Get
(
'/upsertFields'
)
@
ApiOperation
({
summary
:
'添加设备关联的日志字段'
})
upsertFields
()
{
}
}
src/modules/device/device.service.ts
View file @
01ea76a
...
...
@@ -18,7 +18,7 @@ export class DeviceService implements OnModuleInit {
async
onModuleInit
()
{
this
.
resetAllDeviceStatus
()
}
async
findAll
({
user
,
mobile
,
sn
,
skip
,
take
}:
DeviceTypes
.
IQueryDevices
):
Promise
<
CommonTypes
.
IResData
<
DeviceTypes
.
T
Device
[]
>>
{
async
findAll
({
user
,
mobile
,
sn
,
skip
,
take
,
status
}:
DeviceTypes
.
IQueryDevices
):
Promise
<
CommonTypes
.
IResData
<
DeviceTypes
.
I
Device
[]
>>
{
const
where
=
{
sn
:
{
contains
:
sn
...
...
@@ -30,6 +30,9 @@ export class DeviceService implements OnModuleInit {
user
:
{
contains
:
user
}
},
device_status
:
{
status
:
status
||
status
==
0
?
+
status
:
undefined
}
}
const
result
:
DeviceTypes
.
IDeviceResult
[]
=
await
this
.
prisma
.
device
.
findMany
({
...
...
@@ -64,12 +67,13 @@ export class DeviceService implements OnModuleInit {
select
:
{
status
:
true
,
last_log_remove_time
:
true
,
last_log_upload_time
:
true
,
last_login_time
:
true
,
last_offline_time
:
true
}
}
},
where
where
,
orderBy
:
{
created_at
:
'desc'
}
})
const
total
:
number
=
await
this
.
prisma
.
device
.
count
({
where
})
const
data
:
DeviceTypes
.
TDevice
[]
=
result
.
reduce
((
pre
:
DeviceTypes
.
T
Device
[],
cur
:
DeviceTypes
.
IDeviceResult
)
=>
{
const
data
:
DeviceTypes
.
IDevice
[]
=
result
.
reduce
((
pre
:
DeviceTypes
.
I
Device
[],
cur
:
DeviceTypes
.
IDeviceResult
)
=>
{
pre
.
push
({
id
:
cur
.
id
,
sn
:
cur
.
sn
,
...
...
@@ -86,7 +90,7 @@ export class DeviceService implements OnModuleInit {
})
return
pre
},
[])
const
resBody
:
CommonTypes
.
IResData
<
DeviceTypes
.
T
Device
[]
>
=
{
total
,
data
}
const
resBody
:
CommonTypes
.
IResData
<
DeviceTypes
.
I
Device
[]
>
=
{
total
,
data
}
return
resBody
}
...
...
@@ -97,55 +101,97 @@ export class DeviceService implements OnModuleInit {
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
data
:
deviceInfo
}
}
async
addDevice
(
device
:
CommonTypes
.
ISn
):
Promise
<
CommonTypes
.
IResData
>
{
async
addDevice
(
device
:
Omit
<
DeviceTypes
.
IDeviceSaves
,
'device_id'
>
):
Promise
<
CommonTypes
.
IResData
>
{
const
{
device_info
,
log_config
,
user_fields
,
sn
}
=
device
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
const
{
id
}
=
await
prisma
.
device
.
create
({
data
:
device
,
select
:
{
id
:
true
}
})
// await this.addDeviceInfo({device_id:id})
await
prisma
.
device_info
.
create
({
data
:
{
device_id
:
id
,
created_at
:
new
Date
(),
updated_at
:
new
Date
()
}
})
const
now
=
new
Date
()
const
{
id
}
=
await
prisma
.
device
.
create
({
data
:
{
sn
,
created_at
:
now
},
select
:
{
id
:
true
}
})
const
device_id
=
id
if
(
device_info
)
{
await
prisma
.
device_info
.
create
({
data
:
{
device_id
,
user
:
device_info
.
user
,
mobile
:
device_info
.
mobile
,
merchant_name
:
device_info
.
merchant_name
,
updated_at
:
now
,
created_at
:
now
}
})
}
if
(
log_config
)
{
await
prisma
.
log_config
.
create
({
data
:
{
log_saved_days
:
log_config
.
log_saved_days
,
device_id
,
created_at
:
now
,
updated_at
:
now
}
})
}
if
(
user_fields
)
{
await
prisma
.
field_relation
.
deleteMany
({
where
:
{
device_id
}
})
await
prisma
.
field_relation
.
createMany
({
data
:
user_fields
.
map
((
item
:
number
)
=>
({
device_id
,
field_id
:
item
}))
})
}
await
prisma
.
device_status
.
create
({
data
:
{
device_id
:
id
,
status
:
WsStatus
.
Offline
}
})
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'添加设备成功'
}
}
async
addDeviceInfo
(
deviceInfo
:
DeviceTypes
.
TOneDeviceInfo
):
Promise
<
CommonTypes
.
IResData
>
{
deviceInfo
.
created_at
=
new
Date
()
deviceInfo
.
updated_at
=
new
Date
()
async
addDeviceInfo
(
deviceInfo
:
DeviceTypes
.
IDeviceSaves
):
Promise
<
CommonTypes
.
IResData
>
{
const
now
=
new
Date
()
const
{
device_id
,
...
data
}
=
deviceInfo
const
{
device_info
,
log_config
,
user_fields
,
sn
}
=
data
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
await
prisma
.
device_info
.
create
({
data
:
deviceInfo
})
if
(
sn
)
{
await
prisma
.
device
.
update
({
where
:
{
id
:
device_id
},
data
:
{
sn
}
})
}
if
(
device_info
)
{
await
prisma
.
device_info
.
create
({
data
:
{
device_id
,
user
:
device_info
.
user
,
mobile
:
device_info
.
mobile
,
merchant_name
:
device_info
.
merchant_name
,
updated_at
:
now
,
created_at
:
now
}
})
}
if
(
log_config
)
{
await
prisma
.
log_config
.
create
({
data
:
{
log_saved_days
:
log_config
.
log_saved_days
,
device_id
,
created_at
:
now
,
updated_at
:
now
}
})
}
if
(
user_fields
)
{
await
prisma
.
field_relation
.
deleteMany
({
where
:
{
device_id
}
})
await
prisma
.
field_relation
.
createMany
({
data
:
user_fields
.
map
((
item
:
number
)
=>
({
device_id
,
field_id
:
item
}))
})
}
await
prisma
.
device_status
.
create
({
data
:
{
device_id
:
deviceInfo
.
device_id
,
status
:
WsStatus
.
Offline
}
})
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'添加设备信息成功'
}
}
async
updateDeviceInfo
(
deviceInfo
:
DeviceTypes
.
TOneDeviceInfo
):
Promise
<
CommonTypes
.
IResData
>
{
async
updateDeviceInfo
(
deviceInfo
:
DeviceTypes
.
IDeviceSaves
):
Promise
<
CommonTypes
.
IResData
>
{
const
{
device_id
,
...
data
}
=
deviceInfo
data
.
updated_at
=
new
Date
()
await
this
.
prisma
.
device_info
.
update
({
where
:
{
device_id
:
deviceInfo
.
device_id
},
data
const
where
=
{
device_id
}
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
const
now
=
new
Date
()
const
{
device_info
,
log_config
,
user_fields
,
sn
}
=
data
if
(
sn
)
{
await
prisma
.
device
.
update
({
where
:
{
id
:
device_id
},
data
:
{
sn
}
})
}
if
(
device_info
)
{
device_info
.
updated_at
=
now
await
prisma
.
device_info
.
update
({
where
,
data
:
{
user
:
device_info
.
user
,
mobile
:
device_info
.
mobile
,
merchant_name
:
device_info
.
merchant_name
,
updated_at
:
now
}
})
}
if
(
log_config
)
{
await
prisma
.
log_config
.
update
({
where
,
data
:
{
log_saved_days
:
log_config
.
log_saved_days
,
updated_at
:
now
}
})
}
if
(
user_fields
)
{
await
prisma
.
field_relation
.
deleteMany
({
where
})
await
prisma
.
field_relation
.
createMany
({
data
:
user_fields
.
map
((
item
:
number
)
=>
({
device_id
,
field_id
:
item
}))
})
}
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备信息更新成功'
}
}
async
upsertDeviceStatus
(
data
:
DeviceStatus
.
IDeviceStatus
):
Promise
<
CommonTypes
.
IResData
>
{
await
this
.
prisma
.
device_status
.
upsert
({
where
:
{
device_id
:
data
.
device_id
},
create
:
data
,
update
:
{
status
:
data
.
status
}
where
:
{
device_id
:
data
.
device_id
},
create
:
{
device_id
:
data
.
device_id
,
status
:
WsStatus
.
Offline
},
update
:
{
status
:
data
.
status
,
last_login_time
:
data
.
status
===
WsStatus
.
Online
?
new
Date
()
:
undefined
,
last_offline_time
:
data
.
status
===
WsStatus
.
Offline
?
new
Date
()
:
undefined
,
last_log_remove_time
:
data
.
last_log_remove_time
,
last_log_upload_time
:
data
.
last_log_upload_time
}
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
}
async
resetAllDeviceStatus
():
Promise
<
CommonTypes
.
IResData
>
{
Logger
.
warn
(
'========重置所有设备状态========'
)
await
this
.
prisma
.
device_status
.
updateMany
({
data
:
{
status
:
WsStatus
.
Offline
}
data
:
{
status
:
WsStatus
.
Offline
}
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'设备状态更新成功'
}
}
...
...
src/modules/fields/fields.service.ts
View file @
01ea76a
...
...
@@ -25,7 +25,8 @@ export class FieldsService {
},
type
}
const
result
:
FieldTypes
.
IFiled
[]
=
await
this
.
prismaService
.
field
.
findMany
({
skip
,
take
,
where
})
const
result
:
FieldTypes
.
IFiled
[]
=
await
this
.
prismaService
.
field
.
findMany
({
skip
,
take
,
where
,
orderBy
:
{
created_at
:
'desc'
}
})
const
total
=
await
this
.
prismaService
.
field
.
count
({
where
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
message
:
'查询成功'
,
total
,
data
:
result
}
...
...
src/modules/file/file.service.ts
View file @
01ea76a
...
...
@@ -37,7 +37,8 @@ export class FileService {
contains
:
filename
}
}
const
files
:
FileTypes
.
IFile
<
DocTypes
|
string
>
[]
=
await
this
.
prisma
.
file
.
findMany
({
skip
,
take
,
where
})
const
files
:
FileTypes
.
IFile
<
DocTypes
|
string
>
[]
=
await
this
.
prisma
.
file
.
findMany
({
skip
,
take
,
where
,
orderBy
:
{
created_at
:
'desc'
}
})
const
total
:
number
=
await
this
.
prisma
.
file
.
count
({
where
})
...
...
src/typings/device.d.ts
View file @
01ea76a
declare
namespace
DeviceTypes
{
/**
* 查询设备信息的查询对象接口
*
* @param user - 用户
* @param mobile - 手机号
* @param sn - 设备号
* @param skip - 可选,跳过的记录数量
* @param take - 可选,获取的记录数量
*/
type
IQueryDevices
=
Partial
<
CommonTypes
.
IQueryBase
&
CommonTypes
.
ISn
&
CommonTypes
.
IUser
>
type
IQueryDevices
=
Partial
<
CommonTypes
.
IQueryBase
&
CommonTypes
.
ISn
&
CommonTypes
.
IUser
&
Pick
<
DeviceStatus
.
Status
,
'status'
>>
type
IDeviceInfo
=
CommonTypes
.
ITime
&
CommonTypes
.
IUser
type
TDeviceInfo
=
CommonTypes
.
ITime
&
Partial
<
CommonTypes
.
IUser
>
type
T
DeviceInfoResult
=
Omit
<
IDeviceInfo
,
'created_at'
|
'updated_at'
|
'device_id'
>
type
T
OneDeviceInfo
=
TDeviceInfo
&
CommonTypes
.
IDeviceId
/**
* model device_info {
id Int @id @default(autoincrement())
device_id Int @unique(map: "device_id") @default(0)
user String? @db.VarChar(20)
status Int? @db.TinyInt
last_log_upload_time String? @db.VarChar(30)
last_log_remove_time String? @db.VarChar(30)
created_at DateTime @default(now()) @db.Timestamp(0)
updated_at DateTime @default(now()) @db.Timestamp(0)
mobile String? @db.VarChar(20)
merchant_name String? @db.VarChar(50)
log_config log_config?
}*/
type
TOneDeviceInfo
=
IDeviceInfo
&
CommonTypes
.
IDeviceId
/**
* 表示设备结果的接口
*/
type
TDevice
=
{
interface
IDevice
extends
CommonTypes
.
IId
,
CommonTypes
.
ISn
,
DeviceStatus
.
Status
,
Partial
<
TDeviceInfo
>
,
Partial
<
ConfigTypes
.
ILogConfig
>
{
fields
:
FieldTypes
.
TFieldRequird
[]
}
&
CommonTypes
.
IId
&
CommonTypes
.
IUser
&
DeviceStatus
.
Status
&
Partial
<
IDeviceInfo
>
&
Partial
<
ConfigTypes
.
ILogConfig
>
&
CommonTypes
.
ISn
}
type
IDeviceResult
=
{
device_info
:
Partial
<
I
DeviceInfo
>
interface
IDeviceResult
extends
CommonTypes
.
IId
,
CommonTypes
.
ISn
,
Partial
<
Pick
<
CommonTypes
.
ITime
,
'created_at'
>>
{
device_info
:
Partial
<
T
DeviceInfo
>
log_config
:
Partial
<
ConfigTypes
.
ILogConfig
>
device_status
:
DeviceStatus
.
Status
fields
:
IFields
[]
}
&
CommonTypes
.
IId
&
CommonTypes
.
ISn
&
Partial
<
Pick
<
CommonTypes
.
ITime
,
'created_at'
>>
/**
* 表示日志用户字段的接口
*/
}
interface
IDeviceSaves
extends
CommonTypes
.
IDeviceId
,
Partial
<
CommonTypes
.
ISn
>
{
device_info
?:
Partial
<
TDeviceInfo
>
,
log_config
?:
Partial
<
ConfigTypes
.
ILogConfig
>
,
user_fields
?:
Array
<
Number
>
}
interface
IFields
{
/**
* 日志用户字段的详细信息
*/
field
?:
FieldTypes
.
TFieldRequird
}
}
src/typings/device.status.d.ts
View file @
01ea76a
declare
namespace
DeviceStatus
{
interface
IDeviceStatus
extends
CommonTypes
.
IDeviceId
{
status
:
number
,
status
?
:
number
,
last_log_upload_time
?:
string
|
Date
last_log_remove_time
?:
string
|
Date
last_login_time
?:
string
|
Date
...
...
src/typings/relations.d.ts
View file @
01ea76a
...
...
@@ -2,5 +2,4 @@ declare namespace RelationsTypes {
interface
IRelation
extends
CommonTypes
.
IId
,
CommonTypes
.
IDeviceId
{
field_id
:
number
}
type
LogConfigUserFieldsRel
=
Omit
<
IRelation
,
'id'
>
}
src/typings/userFileds.d.ts
deleted
100644 → 0
View file @
d01edba
declare
namespace
UserFieldTypes
{
interface
ILogUserFields
extends
CommonTypes
.
IId
{
name
:
string
}
}
src/typings/ws.d.ts
View file @
01ea76a
...
...
@@ -18,7 +18,7 @@ declare namespace WsTypes {
status
:
T
message
:
U
}
type
LoginValidate
<
T
>
=
{
isValidate
:
boolean
;
errMsg
:
T
,
errorCode
:
number
}
type
LoginValidate
<
T
>
=
{
isValidate
:
boolean
;
errMsg
:
T
;
errorCode
:
number
}
type
MessageData
<
T
>
=
{
command
:
T
...
...
test.http
View file @
01ea76a
# http://localhost:3000/device?
pageIndex=1&pageSize=5
# http://localhost:3000/device?
status=1
# http://localhost:3000/device/addAll
# http://localhost:3000/device/task
...
...
@@ -39,9 +39,10 @@ POST http://localhost:3000/broadcast/cmd HTTP/1.1
{
"event":"private",
"device_id":"10000
20
",
"device_id":"10000
69
",
"data":{
"command":"stopPolling",
"command":"getLog",
"days":7,
"interval":1000,
"force":true
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment