aKeyAlarmDao.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. UpdateTime time.Time `gorm:"column:update_time;type:timestamp;comment:更新时间" json:"updateTime"`
  23. UpdateUser string `gorm:"column:update_user;type:varchar(255);comment:修改用户ID" json:"updateUser"`
  24. IsDeleted int `gorm:"column:is_deleted;type:int(11);default:0;comment:是否删除:0-否-1删除" json:"isDeleted"`
  25. }
  26. func (a AkeyAlarmRecord) TableName() string {
  27. return "device_a_key_alarm_record_his"
  28. }
  29. func (a AkeyAlarmRecord) GetRecords(offset int, limit int, start string, end string, searchValue string, serveId int) ([]AkeyAlarmRecord, error) {
  30. var records []AkeyAlarmRecord
  31. db := Db.Debug().Model(&a)
  32. if serveId > 0 {
  33. db.Where("serve_id=?", serveId)
  34. }
  35. if searchValue != "" {
  36. db = db.Where("terminal_sn like ? or terminal_name like ?", "%"+searchValue+"%", "%"+searchValue+"%")
  37. }
  38. if start != "" {
  39. start = start + " 00:00:00"
  40. end = end + " 23:59:59"
  41. db = db.Where("alarm_start_time >=? and alarm_end_time <= ?", start, end)
  42. }
  43. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&records).Error
  44. return records, err
  45. }
  46. func (a AkeyAlarmRecord) GetMaxIdAndUpDateTime() (int64, string) {
  47. var record AkeyAlarmRecord
  48. err := Db.Debug().Order("id desc").First(&record).Error
  49. if err != nil {
  50. return 0, "2020-01-01 00:00:00"
  51. }
  52. return int64(record.ID), record.CreateTime.Format("2006-01-02 15:04:05")
  53. }
  54. func (a AkeyAlarmRecord) BatchCreate(record []edge_service.RecordAlarmSosData) error {
  55. his, err := a.assembleRecordHis(record)
  56. //fmt.Printf("his = %v", his)
  57. if err != nil {
  58. return err
  59. }
  60. return Db.Debug().CreateInBatches(his, 1000).Error
  61. }
  62. func (a AkeyAlarmRecord) assembleRecordHis(records []edge_service.RecordAlarmSosData) ([]AkeyAlarmRecord, error) {
  63. var temRecords []AkeyAlarmRecord
  64. Db.Debug().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").
  65. Scan(&temRecords)
  66. m := make(map[string]AkeyAlarmRecord)
  67. for _, temSosRecord := range temRecords {
  68. m[temSosRecord.TerminalSn] = temSosRecord
  69. }
  70. var realLightRecords []AkeyAlarmRecord
  71. for _, record := range records {
  72. lightRecord := m[record.Code]
  73. if lightRecord.ServeId == 0 {
  74. continue
  75. }
  76. lightRecord.ID = record.ID
  77. lightRecord.AlarmStartTime, _ = time.ParseInLocation("2006-01-02 15:04:05", record.Tstart, time.Local)
  78. lightRecord.AlarmEndTime, _ = time.ParseInLocation("2006-01-02 15:04:05", record.Tend, time.Local)
  79. lightRecord.AlarmCountTime = int(record.Duration)
  80. lightRecord.IsDeleted = 0
  81. lightRecord.IsDispose = 0
  82. lightRecord.TerminalSn = record.Code
  83. lightRecord.CreateTime = time.Now()
  84. lightRecord.UpdateTime = time.Now()
  85. realLightRecords = append(realLightRecords, lightRecord)
  86. }
  87. return realLightRecords, nil
  88. }
  89. func (a AkeyAlarmRecord) Get() (*AkeyAlarmRecord, error) {
  90. var record AkeyAlarmRecord
  91. err := Db.Debug().Model(&a).First(&record).Error
  92. return &record, err
  93. }
  94. func (a AkeyAlarmRecord) Update() error {
  95. err := Db.Debug().Model(&a).Updates(&a).Error
  96. return err
  97. }