zigbeeDao.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package dao
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "time"
  5. )
  6. //Zigbee集控器
  7. type Zigbee struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. Name string `gorm:"type:varchar(64)" json:"name"` //名称
  10. SN string `gorm:"type:varchar(60)" json:"sn"` //唯一编码
  11. GroupId int `gorm:"type:int" json:"groupId"` //所属灯杆分组
  12. LampPoleName string `gorm:"type: varchar(64)" json:"lampPoleName"` //灯杆名称
  13. LampPoleSN string `gorm:"type: varchar(64)" json:"lampPoleSN"` //灯杆编码
  14. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  15. PoleLng float64 `gorm:"type:double(17, 14) " json:"poleLng"` //经度
  16. PoleLat float64 `gorm:"type:double(17, 14) " json:"poleLat"` //纬度
  17. BrandID int `gorm:"type:int" json:"brandID"` //设备名称
  18. ModelID int `gorm:"type:int" json:"modelID"` //设备型号
  19. GatewayId string `gorm:"type:varchar(32)" json:"gatewayId"` //所属网关id
  20. GatewayName string `gorm:"type:varchar(64)" json:"gatewayName"` //所属网关名称
  21. GatewaySN string `gorm:"type:varchar(60)" json:"gatewaySN"` //所属网关编码
  22. ChanelNum int `gorm:"type:int" json:"chanelNum"` //通道号
  23. NetworkNum int `gorm:"type:int" json:"networkNum"` //网络号
  24. InstallTime *time.Time `gorm:"type:date" json:"installTime"` //安装时间
  25. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户id
  26. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  27. CreateUser string `gorm:"type:varchar(60)" json:"createUser"` //新增记录操作用户ID
  28. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  29. UpdateUser string `gorm:"type:varchar(60)" json:"updateUser"` //修改用户
  30. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  31. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,保留字段(逗号区分)
  32. IPAddress string `gorm:"type:varchar(64)" json:"ipAddress"` //IP地址-备用
  33. }
  34. func (Zigbee) TableName() string {
  35. return "t_dev_zigbee"
  36. }
  37. func (c Zigbee) Delete() error {
  38. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  39. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  40. }
  41. func (c Zigbee) IsExistedBySN() bool {
  42. var count = 0
  43. _ = Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
  44. c.SN, c.IsDeleted).Count(&count).Error
  45. return count > 0
  46. }
  47. func (c Zigbee) IsExistedByNameAndCode() bool {
  48. var devices []Zigbee
  49. err := Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
  50. c.SN, c.IsDeleted).Find(&devices).Error
  51. //如果查询不到,返回相应的错误
  52. if gorm.IsRecordNotFoundError(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 *Zigbee) Create() error {
  63. return Db.Debug().Model(&c).Save(&c).Error
  64. }
  65. func (c *Zigbee) Update() error {
  66. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  67. }
  68. func (c *Zigbee) GetDevice() error {
  69. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  70. return err
  71. }
  72. func (c Zigbee) GetDevices(offset, limit int) ([]Zigbee, error) {
  73. var devices []Zigbee
  74. err := Db.Debug().Model(&c).Where(" name like ? ", "%"+c.Name+"%").Offset(offset).Limit(limit).Find(&devices).Error
  75. return devices, err
  76. }
  77. func (c Zigbee) GetAllDevices() ([]*Zigbee, error) {
  78. var devices []*Zigbee
  79. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  80. return devices, err
  81. }