aKeyAlarmDao.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package dao
  2. import (
  3. "iot_manager_service/app/record/edge_service"
  4. "time"
  5. )
  6. type AkeyAlarmRecord struct {
  7. ID int `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id" :"ID"`
  8. ServeId int `gorm:"column:serve_id;type:int(11);comment:管理端ID" json:"serveId" :"serveId"`
  9. ServeName string `gorm:"column:serve_name;type:varchar(60);comment:管理端名称" json:"serveName" :"serveName"`
  10. ServeSn string `gorm:"column:serve_sn;type:varchar(60);comment:管理端编码" json:"serveSn" :"serveSn"`
  11. TerminalId int `gorm:"column:terminal_id;type:int(11);comment:客户端ID" json:"terminalId" :"terminalId"`
  12. TerminalName string `gorm:"column:terminal_name;type:varchar(60);comment:客户端名称" json:"terminalName" :"terminalName"`
  13. TerminalSn string `gorm:"column:terminal_sn;type:varchar(60);comment:客户端编码" json:"terminalSn"`
  14. LocalAddress string `gorm:"column:local_address;type:varchar(255);comment:灯杆地址" json:"localAddress"`
  15. AlarmStartTime time.Time `gorm:"column:alarm_start_time;type:datetime;comment:报警开始时间" json:"alarmStartTime"`
  16. AlarmEndTime time.Time `gorm:"column:alarm_end_time;type:datetime;comment:报警结束时间" json:"alarmEndTime"`
  17. AlarmCountTime int `gorm:"column:alarm_count_time;type:int(11);default:0;comment:报警时长(秒)" json:"alarmCountTime"`
  18. IsDispose int `gorm:"column:is_dispose;type:int(11);default:0;comment:是否处理:0-未处理,1-已处理" json:"isDispose"`
  19. Remark string `gorm:"column:remark;type:varchar(1000);comment:备注" json:"remark"`
  20. TenantId string `gorm:"column:tenant_id;type:varchar(12);comment:租户ID" json:"tenantId"`
  21. CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:创建时间" json:"createTime"`
  22. IsDeleted int `gorm:"column:is_deleted;type:int(11);default:0;comment:是否删除:0-否-1删除" json:"isDeleted"`
  23. }
  24. func (a AkeyAlarmRecord) TableName() string {
  25. return "device_a_key_alarm_record_his"
  26. }
  27. func (a AkeyAlarmRecord) GetRecords(offset int, limit int, start string, end string, searchValue string, serveId int) ([]AkeyAlarmRecord, error) {
  28. var records []AkeyAlarmRecord
  29. db := Db.Model(&a)
  30. if serveId > 0 {
  31. db.Where("serve_id=?", serveId)
  32. }
  33. if searchValue != "" {
  34. db = db.Where("terminal_sn like ? or terminal_name like ?", "%"+searchValue+"%", "%"+searchValue+"%")
  35. }
  36. if start != "" {
  37. start = start + " 00:00:00"
  38. end = end + " 23:59:59"
  39. db = db.Where("alarm_start_time >=? and alarm_end_time <= ?", start, end)
  40. }
  41. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&records).Error
  42. return records, err
  43. }
  44. func (a AkeyAlarmRecord) GetMaxIdAndUpDateTime() (int64, string) {
  45. var record AkeyAlarmRecord
  46. err := Db.Order("id desc").First(&record).Error
  47. if err != nil {
  48. return 0, "2020-01-01 00:00:00"
  49. }
  50. return int64(record.ID), record.CreateTime.Format("2006-01-02 15:04:05")
  51. }
  52. func (a AkeyAlarmRecord) BatchCreate(record []edge_service.RecordAlarmSosData) error {
  53. his, err := a.assembleRecordHis(record)
  54. if err != nil {
  55. return err
  56. }
  57. return Db.CreateInBatches(his, 1000).Error
  58. }
  59. func (a AkeyAlarmRecord) assembleRecordHis(records []edge_service.RecordAlarmSosData) ([]AkeyAlarmRecord, error) {
  60. var temRecords []AkeyAlarmRecord
  61. Db.Raw("select a.tenant_id,a.id terminal_id,a.terminal_name,a.terminal_sn, a.lamp_pole_location local_address,a.serve_id, b.serve_name,b.serve_sn FROM device_a_key_alarm_terminal a LEFT JOIN device_a_key_alarm_serve b ON a.serve_id=b.id WHERE a.is_deleted=0").
  62. Scan(&temRecords)
  63. m := make(map[string]AkeyAlarmRecord)
  64. for _, temSosRecord := range temRecords {
  65. m[temSosRecord.TerminalSn] = temSosRecord
  66. }
  67. var realLightRecords []AkeyAlarmRecord
  68. for _, record := range records {
  69. lightRecord := m[record.Code]
  70. if lightRecord.ServeId == 0 {
  71. continue
  72. }
  73. lightRecord.ID = record.ID
  74. lightRecord.AlarmStartTime, _ = time.ParseInLocation("2006-01-02 15:04:05", record.Tstart, time.Local)
  75. lightRecord.AlarmEndTime, _ = time.ParseInLocation("2006-01-02 15:04:05", record.Tend, time.Local)
  76. lightRecord.AlarmCountTime = int(record.Duration)
  77. lightRecord.IsDeleted = 0
  78. lightRecord.IsDispose = 0
  79. lightRecord.TerminalSn = record.Code
  80. lightRecord.CreateTime = time.Now()
  81. realLightRecords = append(realLightRecords, lightRecord)
  82. }
  83. return realLightRecords, nil
  84. }
  85. func (a AkeyAlarmRecord) Get() (*AkeyAlarmRecord, error) {
  86. var record AkeyAlarmRecord
  87. err := Db.Model(&a).First(&record).Error
  88. return &record, err
  89. }
  90. func (a AkeyAlarmRecord) Update() error {
  91. err := Db.Model(&a).Updates(&a).Error
  92. return err
  93. }