platformAlarmDao.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package dao
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. "gorm.io/gorm/clause"
  6. "iot_manager_service/app/warn/model"
  7. "iot_manager_service/util/common"
  8. "time"
  9. )
  10. // PlatformAlarm 平台所有告警-来源边缘端接口
  11. type PlatformAlarm struct {
  12. ID int `gorm:"comment:ID" json:"ID"`
  13. ArmClassify int `gorm:"column:arm_classify;type:int(11);comment:告警分类1运维2业务" json:"armClassify"` // 告警分类 1运维 2业务
  14. ArmDeviceType int `gorm:"column:arm_device_type;type:int(11);comment:设备类型1" json:"armDeviceType"` // 告警设备类型1摄像头2网关3灯控4配电5信息屏...15
  15. ArmDeviceTypeName string `gorm:";comment:设备类型1" json:"armDeviceTypeName"`
  16. ArmDeviceId int `gorm:"column:arm_device_id;type:int(11);comment:设备id" json:"armDeviceId"` // 告警设备id
  17. ArmDeviceName string `gorm:"column:arm_device_name;type:varchar(64);comment:设备名称" json:"armDeviceName"` // 告警设备名称
  18. ArmDeviceSn string `gorm:"column:arm_device_sn;type:varchar(60);comment:设备编码" json:"armDeviceSn"` // 告警设备编码
  19. LampPoleId int `gorm:"column:lamp_pole_id;type:int(11);comment:灯杆ID" json:"lampPoleId"` // 所属灯杆ID
  20. LampPoleName string `gorm:"column:lamp_pole_name;type:varchar(64);comment:灯杆名称" json:"lampPoleName"` // 所属灯杆名称
  21. LampPoleSn string `gorm:"column:lamp_pole_sn;type:varchar(60);comment:灯杆唯一编码" json:"lampPoleSn"` // 灯杆唯一编码
  22. LampPoleLocation string `gorm:"column:lamp_pole_location;type:varchar(255);comment:灯杆地址" json:"lampPoleLocation"` // 灯杆地址
  23. ArmSource int `gorm:"column:arm_source;type:int(11);comment:告警来源 1运维2业务" json:"armSource"`
  24. ArmTime common.Time `gorm:"column:arm_time;type:datetime;comment:告警开始时间" json:"armTime"` // 告警开始时间
  25. ArmEndTime common.Time `gorm:"column:arm_end_time;type:datetime;comment:告警结束时间" json:"armEndTime"` // 告警结束时间
  26. ArmHandle int `gorm:"column:arm_handle;type:int(11);comment:当前处理状态1待处理2忽略3已处理4其他" json:"armHandle"` // 当前处理状态1待处理2忽略3已处理4其他
  27. ArmLevel int `gorm:"column:arm_level;type:int(11);comment: 告警类型 1紧急2严重3普通" json:"armLevel"` // 告警类型 1紧急2严重3普通
  28. ArmContent string `gorm:"column:arm_content;type:varchar(255);comment:告警内容" json:"armContent"` // 告警内容
  29. ArmUrl string `gorm:"column:arm_url;type:varchar(1000);comment:运维告警图片地址" json:"armUrl"` // 运维告警图片地址
  30. ArmHanleRemark string `gorm:"column:arm_handle_remark;type:text;comment:处理内容" json:"armHanleRemark"`
  31. ArmHandleTime common.Time `gorm:"column:arm_handle_time;type:datetime" json:"armHandleTime"` // 告警处理时间
  32. Status int `gorm:"column:status;type:int(11);comment:通知发送 1已发送 0未发送" json:"status"`
  33. TenantId string `gorm:"column:tenant_id;type:varchar(12);comment:租户ID" json:"tenantId"` // 租户ID
  34. ProcessInstanceId string `gorm:"column:process_instance_id;type:varchar(255);comment:流程实例主键" json:"processInstanceId"` // 流程实例主键
  35. BusinessId string `gorm:"column:business_id;type:varchar(64);comment:流程实例主键ID" json:"businessId"` // 流程实例主键ID
  36. Threshold float64 `gorm:"column:threshold;type:float(11,2);comment:阈值-业务告警" json:"threshold"` // 阈值-业务告警
  37. Value float64 `gorm:"column:value;type:float(11,2);comment:告警时的值-业务告警" json:"value"` // 告警时的值-业务告警
  38. Sid int `gorm:"column:sid;type:int(11);comment:物模型sid" json:"sid"` // 物模型sid
  39. Cid uint `gorm:"column:cid;type:int(10) unsigned;comment:告警策略编号" json:"cid"` // 告警策略编号
  40. Cname string `gorm:"column:cname;type:varchar(100);comment:告警策略名称" json:"cname"` // 告警策略名称
  41. ExtendId int `gorm:"column:extend_id;type:int(11);comment:扩展id"json:"extendId"` // 扩展id
  42. }
  43. func (PlatformAlarm) TableName() string {
  44. return "warn_platform_alarm"
  45. }
  46. func (a PlatformAlarm) Gets(filter model.RequestPlatFormAlartFilter) ([]PlatformAlarm, int64, error) {
  47. var list []PlatformAlarm
  48. db := Db.Debug().Model(&a).Where(&PlatformAlarm{TenantId: a.TenantId})
  49. db = db.Scopes(common.Paginate(filter.Current, filter.Size))
  50. if filter.ArmHandle > 0 {
  51. handle := filter.ArmHandle
  52. db = db.Where(&PlatformAlarm{ArmHandle: handle})
  53. }
  54. if filter.ArmDeviceType > 0 {
  55. db = db.Where(&PlatformAlarm{ArmDeviceType: filter.ArmDeviceType})
  56. }
  57. if filter.ArmDeviceName != "" {
  58. db = db.Where(&PlatformAlarm{ArmDeviceName: filter.ArmDeviceName})
  59. }
  60. if filter.ArmSource != 0 {
  61. db = db.Where(&PlatformAlarm{ArmSource: filter.ArmSource})
  62. }
  63. err := db.Order("id desc").Find(&list).Error
  64. if errors.Is(err, gorm.ErrRecordNotFound) {
  65. err = nil
  66. }
  67. var count int64
  68. db.Count(&count)
  69. //fmt.Printf("count = %v", count)
  70. return list, count, err
  71. }
  72. func (a PlatformAlarm) GetMaxIdAndUpDateTime() (int64, string) {
  73. var platformAlarm PlatformAlarm
  74. Db.Debug().Where(&PlatformAlarm{TenantId: a.TenantId}).Last(&platformAlarm)
  75. return int64(platformAlarm.ID), platformAlarm.ArmTime.String()
  76. }
  77. func (a PlatformAlarm) BatchCreate(his []PlatformAlarm) error {
  78. db := Db.Debug()
  79. db = db.Clauses(clause.OnConflict{
  80. Columns: []clause.Column{{Name: "ID"}}, // 这里的列必须是唯一的,比如主键或是唯一索引
  81. })
  82. return db.CreateInBatches(his, 1000).Error
  83. }
  84. func (a PlatformAlarm) Update() error {
  85. err := Db.Debug().Model(&a).Updates(a).Error
  86. return err
  87. }
  88. func (a PlatformAlarm) GetWarnCount() (int64, error) {
  89. var count int64
  90. nowTime := time.Now()
  91. getTime := nowTime.AddDate(0, 0, -30)
  92. err := Db.Debug().Model(&a).Where(&PlatformAlarm{
  93. TenantId: a.TenantId,
  94. ArmDeviceType: a.ArmDeviceType,
  95. ArmHandle: 1,
  96. }).Where("arm_time>?", getTime).Count(&count).Error
  97. return count, err
  98. }
  99. func (a PlatformAlarm) GetsAlarmByTime(start string) ([]PlatformAlarm, error) {
  100. var list []PlatformAlarm
  101. err := Db.Debug().Model(&a).Where("status=0 and arm_time>?", start).Find(&list).Order("id desc").Error
  102. return list, err
  103. }