device.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package dao
  2. import "server/global"
  3. type Device struct {
  4. global.GVA_MODEL
  5. Sn string `json:"sn" gorm:"comment:设备sn"`
  6. Name string `json:"name" gorm:"comment:名称"`
  7. Address string `json:"address" gorm:"comment:设备地址"`
  8. TaskTime uint `json:"taskTime" gorm:"comment:采集时间"`
  9. WaitTime uint `json:"waitTime" gorm:"comment:延迟时间"`
  10. RadarId int8 `json:"radarId" gorm:"comment:雷达id"`
  11. SerialId int8 `json:"serialId" gorm:"comment:串口id"`
  12. Genre int `json:"genre" gorm:"index;comment:类型id"`
  13. DeviceGenre DeviceGenre `json:"deviceGenre" gorm:"foreignKey:Genre;"`
  14. TunnelId int `json:"tunnelId" gorm:"column:tunnel_id"`
  15. Tunnel Tunnel `json:"tunnel" gorm:"foreignKey:TunnelId"`
  16. }
  17. func (Device) TableName() string {
  18. return "device"
  19. }
  20. func QueryAllDevices() (devices []Device, err error) {
  21. err = global.GVA_DB.Model(Device{}).Preload("DeviceGenre").Preload("Tunnel").Find(&devices).Error
  22. return
  23. }
  24. func QueryDeviceList(sn, name string, genre, limit, offset int) (devices []Device, total int64, err error) {
  25. db := global.GVA_DB.Model(Device{})
  26. if sn != "" {
  27. db = db.Where("sn LIKE ?", "%"+sn+"%")
  28. }
  29. if name != "" {
  30. db = db.Where("name LIKE ?", "%"+name+"%")
  31. }
  32. if genre != 0 {
  33. db = db.Where("genre = ?", genre)
  34. }
  35. err = db.Count(&total).Error
  36. if err != nil {
  37. return
  38. }
  39. err = db.Limit(limit).Offset(offset).Preload("DeviceGenre").Preload("Tunnel").Find(&devices).Error
  40. return
  41. }
  42. func QueryDeviceByTunnelId(id int) (devices []Device, err error) {
  43. err = global.GVA_DB.Where("tunnel_id =?", id).Preload("DeviceGenre").Preload("Tunnel").Find(&devices).Error
  44. return
  45. }
  46. func (d Device) CreateDevice() error {
  47. return global.GVA_DB.Create(&d).Error
  48. }
  49. func (d Device) UpdateDevice() error {
  50. return global.GVA_DB.Where("id = ?", d.ID).Updates(&d).Error
  51. }
  52. func DeleteDevice(id int) error {
  53. return global.GVA_DB.Unscoped().Where("id = ?", id).Delete(&Device{}).Error
  54. }