diff --git a/controllers/device.go b/controllers/device.go index f457eb610..0d9811259 100644 --- a/controllers/device.go +++ b/controllers/device.go @@ -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 + +} diff --git a/routers/router.go b/routers/router.go index a75e04f61..b519b9fc5 100644 --- a/routers/router.go +++ b/routers/router.go @@ -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"), diff --git a/services/device_service.go b/services/device_service.go index 8ab83f7c2..98c595668 100644 --- a/services/device_service.go +++ b/services/device_service.go @@ -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} +} diff --git a/services/tp_ota_service.go b/services/tp_ota_service.go index fa97f1cd2..3296a0972 100644 --- a/services/tp_ota_service.go +++ b/services/tp_ota_service.go @@ -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) diff --git a/validate/device_validate.go b/validate/device_validate.go index 52313b0fe..51b6e535a 100644 --- a/validate/device_validate.go +++ b/validate/device_validate.go @@ -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-离线 +}