device.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "server/global"
  5. )
  6. type Device struct {
  7. global.GVA_MODEL
  8. Sn string `json:"sn" gorm:"comment:设备sn"`
  9. Name string `json:"name" gorm:"comment:名称"`
  10. Address string `json:"address" gorm:"comment:设备地址"`
  11. TaskTime uint `json:"taskTime" gorm:"comment:采集时间"`
  12. WaitTime uint `json:"waitTime" gorm:"comment:延迟时间"`
  13. RadarId int8 `json:"radarId" gorm:"comment:雷达id"`
  14. SerialId int8 `json:"serialId" gorm:"comment:串口id"`
  15. Genre int `json:"genre" gorm:"index;comment:类型id"`
  16. DeviceGenre DeviceGenre `json:"deviceGenre" gorm:"foreignKey:Genre;"`
  17. TunnelId int `json:"tunnelId" gorm:"column:tunnel_id"`
  18. Tunnel Tunnel `json:"tunnel" gorm:"foreignKey:TunnelId"`
  19. DeviceRelays []DeviceRelay `json:"deviceRelays" gorm:"foreignKey:DeviceId"`
  20. }
  21. func (Device) TableName() string {
  22. return "device"
  23. }
  24. func QueryAllDevices() (devices []Device, err error) {
  25. err = global.GVA_DB.Model(Device{}).Preload("DeviceGenre").Preload("DeviceRelays").Preload("Tunnel").Find(&devices).Error
  26. return
  27. }
  28. func QueryDeviceList(sn, name string, genre, limit, offset int) (devices []Device, total int64, err error) {
  29. db := global.GVA_DB.Model(Device{})
  30. if sn != "" {
  31. db = db.Where("sn LIKE ?", "%"+sn+"%")
  32. }
  33. if name != "" {
  34. db = db.Where("name LIKE ?", "%"+name+"%")
  35. }
  36. if genre != 0 {
  37. db = db.Where("genre = ?", genre)
  38. }
  39. err = db.Count(&total).Error
  40. if err != nil {
  41. return
  42. }
  43. err = db.Limit(limit).Offset(offset).Preload("DeviceGenre").Preload("DeviceRelays").Preload("Tunnel").Find(&devices).Error
  44. return
  45. }
  46. func QueryDeviceByTunnelId(id int) (devices []Device, err error) {
  47. err = global.GVA_DB.Where("tunnel_id =?", id).Preload("DeviceGenre").Preload("DeviceRelays").Preload("Tunnel").Find(&devices).Error
  48. return
  49. }
  50. func QueryGenreByTunnel(id int) (devices []Device, err error) {
  51. err = global.GVA_DB.Model(&Device{}).
  52. Select("device.*").
  53. Joins("JOIN device_genre ON device.genre = device_genre.id").
  54. Where("device.tunnel_id = ? AND device_genre.type = ?", id, "开关设备").
  55. Find(&devices).Error
  56. return
  57. }
  58. func QueryDeviceByTunnelSnAndRadarId(sn string, id int) (device Device, err error) {
  59. err = global.GVA_DB.Model(&Device{}).
  60. Select("device.*").
  61. Joins("JOIN tunnel ON device.tunnel_id = tunnel.id").
  62. Where("device.radar_id = ? AND tunnel.tunnel_sn = ?", id, sn).
  63. First(&device).Error
  64. return
  65. }
  66. func (d Device) CreateDevice() error {
  67. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  68. err := tx.Create(&d).Error
  69. if err != nil {
  70. return err
  71. }
  72. if d.Genre != 6 {
  73. return nil
  74. }
  75. var deviceRelays []DeviceRelay
  76. for i := 1; i <= 4; i++ {
  77. relay := DeviceRelay{
  78. RelayID: i,
  79. DeviceId: int(d.ID),
  80. State: false,
  81. }
  82. deviceRelays = append(deviceRelays, relay)
  83. }
  84. return tx.Create(&deviceRelays).Error
  85. })
  86. }
  87. func (d Device) UpdateDevice() error {
  88. return global.GVA_DB.Where("id = ?", d.ID).Updates(&d).Error
  89. }
  90. func DeleteDevice(id int) error {
  91. return global.GVA_DB.Unscoped().Where("id = ?", id).Delete(&Device{}).Error
  92. }