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
hezhuozhuo committed Oct 25, 2023
2 parents 4d3ce68 + a5538e6 commit 766254a
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 1 deletion.
54 changes: 54 additions & 0 deletions controllers/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,3 +1146,57 @@ func (c *DeviceController) DeviceCommandSend() {

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

func (c *DeviceController) GetBusinessIdAssetIdByDevice() {
var inputData struct {
ID string `json:"id" alias:"设备ID" valid:"Required;MaxSize(36)"`
}

err := json.Unmarshal(c.Ctx.Input.RequestBody, &inputData)
if err != nil {
response.SuccessWithMessage(400, err.Error(), (*context2.Context)(c.Ctx))
return
}

var d services.DeviceService
device, i := d.Token(inputData.ID)
if i == 0 {
response.SuccessWithMessage(400, "no device", (*context2.Context)(c.Ctx))
return
}

var a services.AssetService
asset, i := a.GetAssetById(device.AssetID)
if i == 0 {
response.SuccessWithMessage(400, "no asset", (*context2.Context)(c.Ctx))
return
}

r := make(map[string]string)
r["device_id"] = inputData.ID
r["asset_id"] = device.AssetID
r["business_id"] = asset.BusinessID

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

}
20 changes: 20 additions & 0 deletions controllers/tp_warning_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,23 @@ func (c *TpWarningInformationController) BatchProcessing() {
utils.SuccessWithMessage(1000, err.Error(), (*context2.Context)(c.Ctx))
}
}

func (c *TpWarningInformationController) TenantCount() {
reqData := valid.WarningInformationTenantCountType{}
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.TpWarningInformationService
dd := s.GetTenantWarningInformationCount(tenantId, reqData.ProcessingResult)

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

}
4 changes: 4 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ func init() {
web.NSRouter("/device/cascade", &controllers.DeviceController{}, "*:GetDeviceByCascade"),
web.NSRouter("/device/map", &controllers.DeviceController{}, "*:DeviceMapList"),
web.NSRouter("/device/status", &controllers.DeviceController{}, "*:DeviceStatus"),

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
42 changes: 42 additions & 0 deletions services/device_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,3 +1663,45 @@ 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{"all": 0}
}
}
return map[string]int64{"all": 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{"online": 0}
}
}
return map[string]int64{"online": count}
default: //默认全部
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) {
count = 0
}
}
var oncount int64
sql := `select count(distinct entity_id) from ts_kv_latest where tenant_id = ? and key='SYS_ONLINE' and str_v='1' `
result2 := psql.Mydb.Raw(sql, tenantId).Count(&oncount)
if result2.Error != nil {
if errors.Is(result2.Error, gorm.ErrRecordNotFound) {
oncount = 0
}
}
return map[string]int64{"all": count, "online": oncount}

}
}
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
38 changes: 38 additions & 0 deletions services/tp_warning_information_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,41 @@ func (*TpWarningInformationService) BatchProcessing(batchProcessing valid.BatchP
tx.Commit()
return nil
}

// 获取告警总数
func (*TpWarningInformationService) GetTenantWarningInformationCount(tenantId string, processingresult string) map[string]int64 {
var count int64
switch {
case processingresult == "0": //未处理消息数
result := psql.Mydb.Model(&models.TpWarningInformation{}).Where("tenant_id = ? and processing_result=?", tenantId, processingresult).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return map[string]int64{"unprocessed": 0}
}
}
return map[string]int64{"unprocessed": count}
case processingresult == "1": //已处理消息数
result := psql.Mydb.Model(&models.TpWarningInformation{}).Where("tenant_id = ? and processing_result=?", tenantId, processingresult).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return map[string]int64{"processed": 0}
}
}
return map[string]int64{"processed": count}
default:
var dcount, ucount int64
result := psql.Mydb.Model(&models.TpWarningInformation{}).Where("tenant_id = ? and processing_result='1'", tenantId, processingresult).Count(&dcount)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
dcount = 0
}
}
result2 := psql.Mydb.Model(&models.TpWarningInformation{}).Where("tenant_id = ? and processing_result='0'", tenantId, processingresult).Count(&ucount)
if result2.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
ucount = 0
}
}
return map[string]int64{"processed": dcount, "unprocessed": ucount}
}
}
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:"MaxSize(1)"` //0-全部,1-在线设备数,2-离线
}
4 changes: 4 additions & 0 deletions validate/tp_warning_information_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ type BatchProcessingValidate struct {
ProcessingResult string `json:"processing_result,omitempty" valid:"Required;MaxSize(1)"` // 处理结果 0-未处理 1-已处理 2-已忽略
ProcessingInstructions string `json:"processing_instructions,omitempty" valid:"MaxSize(255)"` // 处理说明
}

type WarningInformationTenantCountType struct {
ProcessingResult string `json:"processing_result,omitempty" valid:"MaxSize(1)"` // 处理结果 0-未处理 1-已处理 2-已忽略
}

0 comments on commit 766254a

Please sign in to comment.