tunnel.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "server/global"
  5. )
  6. type Tunnel struct {
  7. global.GVA_MODEL
  8. TunnelSn string `json:"tunnelSn" gorm:"comment:隧道序号"`
  9. Name string `json:"name" gorm:"comment:名称"`
  10. Tactics int `json:"tactics" gorm:"comment:策略"`
  11. RegionId int `json:"regionId" gorm:"column:region_id"`
  12. Region Region `json:"region" gorm:"foreignKey:RegionId"` // 关系
  13. TimeId int `json:"timeId" gorm:"comment:定时id"`
  14. SwitchType string `json:"switchType" gorm:"comment:开关类型"`
  15. LampValue1 int `json:"lampValue1" gorm:"comment:1路单灯值"`
  16. LampValue2 int `json:"lampValue2" gorm:"comment:2路单灯值"`
  17. TunnelTime TunnelTime `json:"tunnelTime" gorm:"foreignKey:TimeId"`
  18. Users []SysUser `json:"users" gorm:"many2many:user_tunnel"`
  19. Devices []Device `json:"devices" gorm:"foreignKey:TunnelId"`
  20. EnvData []EnvData `json:"envData" gorm:"foreignKey:TunnelSn;references:TunnelSn"`
  21. OpticalData []OpticalData `json:"opticalData" gorm:"foreignKey:TunnelSn;references:TunnelSn"`
  22. }
  23. func (Tunnel) TableName() string {
  24. return "tunnel"
  25. }
  26. func QueryAllTunnels() (tunnels []Tunnel, err error) {
  27. err = global.GVA_DB.Find(&tunnels).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Devices.DeviceRelays").Preload("Devices.Tunnel").Preload("Region").Preload("EnvData").Preload("OpticalData").Error
  28. return tunnels, err
  29. }
  30. func QueryTunnelList(name string, regionId, userId, limit, offset int) (tunnels []Tunnel, total int64, err error) {
  31. db := global.GVA_DB.Model(Tunnel{})
  32. if name != "" {
  33. db = db.Where("name LIKE ?", "%"+name+"%")
  34. }
  35. if regionId != 0 {
  36. db = db.Where("region_id = ?", regionId)
  37. }
  38. db = db.Joins("JOIN user_tunnel ON user_tunnel.tunnel_id = tunnel.id").
  39. Where("user_tunnel.sys_user_id = ?", userId)
  40. err = db.Count(&total).Error
  41. if err != nil {
  42. return
  43. }
  44. err = db.Limit(limit).Offset(offset).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Devices.DeviceRelays").Preload("Devices.Tunnel").Preload("Region").Preload("EnvData").Preload("OpticalData").Find(&tunnels).Error
  45. return
  46. }
  47. func QueryNoRegionTunnels() (tunnels []Tunnel, err error) {
  48. err = global.GVA_DB.Where("region_id =?", 0).Find(&tunnels).Error
  49. return
  50. }
  51. func QueryTunnelById(id int) (tunnel Tunnel, err error) {
  52. err = global.GVA_DB.Where("id =?", id).Preload("Users").Preload("TunnelTime").Preload("Devices").Preload("Devices.DeviceRelays").Preload("Devices.Tunnel").Preload("Region").Preload("EnvData").Preload("OpticalData").First(&tunnel).Error
  53. return
  54. }
  55. func QueryTunnelByRegionId(id int) (tunnels []Tunnel, err error) {
  56. err = global.GVA_DB.Where("region_id =?", id).Find(&tunnels).Error
  57. return
  58. }
  59. func (t Tunnel) CreateTunnel() error {
  60. err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  61. err := tx.Create(&t).Error
  62. if err != nil {
  63. return err
  64. }
  65. time := TunnelTime{
  66. TunnelSn: t.TunnelSn,
  67. StartTime: "00:00",
  68. EndTime: "00:00",
  69. }
  70. err = tx.Create(&time).Error
  71. if err != nil {
  72. return err
  73. }
  74. return nil
  75. })
  76. return err
  77. }
  78. func (t Tunnel) UpdateTunnel() error {
  79. return global.GVA_DB.Where("id = ?", t.ID).Updates(&t).Error
  80. }
  81. func UpdateTunnelLamp(id, lamp1, lamp2 int) error {
  82. return global.GVA_DB.Model(&Tunnel{}).Where("id =?", id).Update("lamp_value1", lamp1).Update("lamp_value2", lamp2).Error
  83. }
  84. func UpdateTactics(sn string, tactics int) error {
  85. return global.GVA_DB.Model(&Tunnel{}).Where("tunnel_sn =?", sn).Update("tactics", tactics).Error
  86. }
  87. func UpdateTunnelsRegion(tunnelIds []int, regionId int) error {
  88. return global.GVA_DB.Model(&Tunnel{}).Where("id in (?)", tunnelIds).Update("region_id", regionId).Error
  89. }
  90. func DeleteTunnel(id int) error {
  91. return global.GVA_DB.Unscoped().Where("id = ?", id).Delete(&Tunnel{}).Error
  92. }
  93. // 权限相关