gatewayDao.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "time"
  5. )
  6. //Gateway 网关设备
  7. type Gateway struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. GatewayName string `gorm:"type:varchar(64)" json:"gatewayName"` //设备名称
  10. GatewaySn string `gorm:"type:varchar(60)" json:"gatewaySn"` //设备序列号
  11. LampPoleId int `gorm:"type:int" json:"lampPoleId"` //所属灯杆
  12. LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"` //所属灯杆SN
  13. LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"` //灯杆名称
  14. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  15. LampLat float64 `gorm:"type:double(17,14)" json:"lampLat"` //纬度
  16. LampLng float64 `gorm:"type:double(17,14)" json:"lampLng"` //经度
  17. BrandId int `gorm:"type:int" json:"brandId"` //设备品牌
  18. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  19. GatewayInstallTime time.Time `gorm:"type:date" json:"gatewayInstallTime"` //安装时间
  20. RatedPower float32 `gorm:"type:double(8,2);default:0.00" json:"ratedPower"` //功率
  21. IpAddress string `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
  22. DiNum int `gorm:"type:int;default 0" json:"diNum"` //DI口数量
  23. DoNum int `gorm:"type:int;default 0" json:"doNum"` //DO口数量
  24. Num485 int `gorm:"type:int;default 0" json:"num485"` //485口数量
  25. NetworkNum int `gorm:"type:int;default 0" json:"networkNum"` //网口数量
  26. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  27. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  28. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  29. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  30. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  31. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  32. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  33. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,(备用,逗号区分)
  34. }
  35. func (Gateway) TableName() string {
  36. return "device_wisdom_gateway"
  37. }
  38. func (c *Gateway) Delete() error {
  39. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  40. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  41. }
  42. func (c Gateway) IsExistedBySN() bool {
  43. var count int64
  44. _ = Db.Debug().Model(&c).Where(" gateway_sn = ? and is_deleted = ?",
  45. c.GatewaySn, c.IsDeleted).Count(&count).Error
  46. return count > 0
  47. }
  48. func (c Gateway) IsExistedByNameAndCode() bool {
  49. var devices []Gateway
  50. err := Db.Debug().Model(&c).Where("gateway_sn = ? and is_deleted = ?",
  51. c.TenantId, c.IsDeleted).Find(&devices).Error
  52. if gorm.ErrRecordNotFound == err {
  53. return false
  54. }
  55. for _, d := range devices {
  56. if d.ID != c.ID {
  57. return true
  58. }
  59. }
  60. return false
  61. }
  62. func (c *Gateway) Create() error {
  63. return Db.Debug().Model(&c).Save(&c).Error
  64. }
  65. func (c *Gateway) Update() error {
  66. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  67. }
  68. func (c *Gateway) GetDevice() error {
  69. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).First(&c).Error
  70. return err
  71. }
  72. func (c Gateway) GetDevices(offset, limit int) ([]Gateway, error) {
  73. var devices []Gateway
  74. db := Db.Debug().Model(&c)
  75. if c.GatewaySn != "" {
  76. db = db.Where("gateway_name like ? or gateway_sn like ?", "%"+c.GatewaySn+"%", "%"+c.GatewaySn+"%")
  77. }
  78. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
  79. return devices, err
  80. }
  81. func (c Gateway) GetAllDevices() ([]*Gateway, error) {
  82. var devices []*Gateway
  83. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  84. return devices, err
  85. }
  86. func (c Gateway) GetDevicesByLampPole() []Gateway {
  87. var devices []Gateway
  88. Db.Debug().Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  89. c.LampPoleId).Find(&devices)
  90. return devices
  91. }