alarmDao.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package dao
  2. import "iot_manager_service/app/operation/model"
  3. type Alarm struct {
  4. TenantId string
  5. DeviceType int `gorm:"column:arm_device_type"` //设备类型
  6. Count int //总数
  7. NotHandlerCount int //未处理报警
  8. CountTime string `json:"countTime"` //统一时间
  9. }
  10. func (a Alarm) TableName() string {
  11. return "warn_platform_alarm"
  12. }
  13. func (a Alarm) Gets(req model.RequestAlarmFilter) ([]Alarm, error) {
  14. var list []Alarm
  15. countTimeFormat := "%Y-%m-%d"
  16. if req.QueryType == "month" {
  17. countTimeFormat = "%Y-%m"
  18. }
  19. deviceType := ""
  20. if a.rewirteDeviceType(req.QueryDeviceType) != "" {
  21. deviceType = " arm_device_type as device_type,"
  22. }
  23. sql := "SELECT count(*) AS count, date_format( arm_time, '" + countTimeFormat + "' ) AS count_time, " + deviceType + " sum(if(arm_handle=1, 1,0)) as not_handler_count FROM warn_platform_alarm WHERE "
  24. where := " arm_time>=? and arm_time<=?"
  25. if a.rewirteDeviceType(req.QueryDeviceType) != "" {
  26. where = " arm_time>=? and arm_time<=? and arm_device_type=" + a.rewirteDeviceType(req.QueryDeviceType)
  27. }
  28. sql += where
  29. group := " group by count_time"
  30. if a.rewirteDeviceType(req.QueryDeviceType) != "" {
  31. group = " GROUP BY count_time,device_type"
  32. }
  33. sql += group
  34. sql += " order by count_time desc"
  35. err := Db.Debug().Raw(sql, req.StartTime, req.EndTime).Scan(&list).Error
  36. return list, err
  37. }
  38. //重写前端传过来的数据,由于前端修改工程大,先这样处理了
  39. func (a Alarm) rewirteDeviceType(queryDeviceType string) string {
  40. switch queryDeviceType {
  41. case "count":
  42. return ""
  43. case "camera":
  44. return "1" //摄像机
  45. case "gateway":
  46. return "2" //网关
  47. case "lightcontrol":
  48. return "3" //灯控
  49. case "infoboard":
  50. return "5" //信息屏
  51. case "sensor":
  52. return "6" //环境监测
  53. case "zigbee":
  54. return "7" //集控器
  55. case "terminal":
  56. return "8" //一键告警终端
  57. case "capture":
  58. return "12" //抓拍单元
  59. case "bridgesensor": //雪水传感器
  60. return "13"
  61. case "ipcast":
  62. return "14" //IP音柱
  63. }
  64. return ""
  65. }