Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
November-12 committed Oct 24, 2023
2 parents 004eb37 + a734647 commit a498db3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
21 changes: 21 additions & 0 deletions controllers/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,3 +1179,24 @@ func (c *DeviceController) GetBusinessIdAssetIdByDevice() {

response.SuccessWithDetailed(200, "success", r, map[string]string{}, (*context2.Context)(c.Ctx))
}

// 租户设备总数
func (c *DeviceController) DeviceTenantCount() {
reqData := valid.DeviceTenantCountType{}
if err := valid.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
response.SuccessWithMessage(1000, err.Error(), (*context2.Context)(c.Ctx))
return
}
//获取租户id
tenantId, ok := c.Ctx.Input.GetData("tenant_id").(string)
if !ok {
response.SuccessWithMessage(400, "代码逻辑错误", (*context2.Context)(c.Ctx))
return
}
var s services.DeviceService
dd := s.GetTenantDeviceCount(tenantId, reqData.CountType)

utils.SuccessWithDetailed(200, "success", dd, map[string]string{}, (*context2.Context)(c.Ctx))
return

}
2 changes: 2 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func init() {
web.NSRouter("/device/business/asset", &controllers.DeviceController{}, "*:GetBusinessIdAssetIdByDevice"),
web.NSRouter("/device/business/asset/permissions", &controllers.DeviceController{}, "*:OpenApiPageListTree"),

web.NSRouter("/device/tenant/count", &controllers.DeviceController{}, "*:DeviceTenantCount"),

// 设备事件上报/命令下发历史列表
web.NSRouter("/device/event/history/list", &controllers.DeviceController{}, "*:DeviceEventHistoryList"),
web.NSRouter("/device/command/history/list", &controllers.DeviceController{}, "*:DeviceCommandHistoryList"),
Expand Down
26 changes: 26 additions & 0 deletions services/device_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,3 +1663,29 @@ func saveCommandSendHistory(
errors.Is(err.Error, gorm.ErrRecordNotFound)
}
}

//获取租户设备数
// 获取全部Device
func (*DeviceService) GetTenantDeviceCount(tenantId string, deviceType string) map[string]int64 {
var count int64
switch {
case deviceType == "0": //全部设备不包含子设备
result := psql.Mydb.Model(&models.Device{}).Where("tenant_id = ? and parent_id=''", tenantId).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return map[string]int64{"count": 0}
}
}
return map[string]int64{"count": count}
case deviceType == "1": //在线设备总数
sql := `select count(distinct entity_id) from ts_kv_latest where tenant_id = ? and key='SYS_ONLINE' and str_v='1' `
result := psql.Mydb.Raw(sql, tenantId).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return map[string]int64{"count": 0}
}
}
return map[string]int64{"count": count}
}
return map[string]int64{"count": 0}
}
2 changes: 1 addition & 1 deletion services/tp_ota_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (*TpOtaService) GetTpOtaList(PaginationValidate valid.TpOtaPaginationValida
values = append(values, PaginationValidate.ProductId)
where += " and o.product_id = ?"
}
sqlWhere += where
sqlWhere = sqlWhere + where + "order by o.CreatedAt desc"
sqlWhereCount += where
var count int64
result := psql.Mydb.Raw(sqlWhereCount, values...).Scan(&count)
Expand Down
5 changes: 5 additions & 0 deletions validate/device_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,8 @@ type DeviceCommandSendValid struct {
CommandName string `json:"command_name,omitempty"`
Desc string `json:"desc,omitempty"`
}

// 总数 校验
type DeviceTenantCountType struct {
CountType string `json:"count_type" alias:"counttype" valid:"Required;MaxSize(1)"` //0-全部,1-在线设备数,2-离线
}

0 comments on commit a498db3

Please sign in to comment.