ipBroadcastDao.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package dao
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "time"
  5. )
  6. type IpBroadcast struct {
  7. ID int `gorm:"primary_key" json:"id"` //编号
  8. CastName string `gorm:"type:varchar(64)" json:"castName"` //设备名称
  9. CastSn string `gorm:"type:varchar(60)" json:"castSn"` //设备序列号
  10. LampPoleId int `gorm:"type:int" json:"LampPoleId"` //所属灯杆id
  11. LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"` //所属灯杆sn
  12. GroupId int `gorm:"type:int" json:"groupId"` //灯杆分组ID
  13. GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关id
  14. GatewaySn string `gorm:"type:varchar(64)" json:"gatewaySn"` //所属网关sn
  15. BrandId int `gorm:"type:int" json:"brandId"` //设备名称
  16. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  17. RatedPower float32 `gorm:"type:float(8, 2); default 0.00" json:"ratedPower"` //额定功率(LED灯)
  18. IPAddress string `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
  19. ServiceIpAddress string `gorm:"type:varchar(50)" json:"serviceIpAddress"` //服务IP地址
  20. ServerPort int `gorm:"type:int" json:"serverPort"` //服务端口
  21. SoundVolume int `gorm:"type:int" json:"soundVolume"` //音量0-100换算
  22. InstallTime *time.Time `gorm:"type:date" json:"installTime"` //安装时间
  23. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  24. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  25. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  26. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  27. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  28. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  29. }
  30. func (IpBroadcast) TableName() string {
  31. return "device_ip_broadcast"
  32. }
  33. func (c IpBroadcast) Delete() error {
  34. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  35. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  36. }
  37. func (c IpBroadcast) IsExistedBySN() bool {
  38. var count = 0
  39. _ = Db.Debug().Model(&c).Where(" cast_sn = ? and is_deleted = ?",
  40. c.CastSn, c.IsDeleted).Count(&count).Error
  41. return count > 0
  42. }
  43. func (c IpBroadcast) IsExistedByNameAndCode() bool {
  44. var devices []IpBroadcast
  45. err := Db.Debug().Model(&c).Where(" cast_sn = ? and is_deleted = ?",
  46. c.CastSn, c.IsDeleted).Find(&devices).Error
  47. //如果查询不到,返回相应的错误
  48. if gorm.IsRecordNotFoundError(err) {
  49. return false
  50. }
  51. for _, d := range devices {
  52. if d.ID != c.ID {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. func (c *IpBroadcast) Create() error {
  59. return Db.Debug().Model(&c).Save(&c).Error
  60. }
  61. func (c *IpBroadcast) Update() error {
  62. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  63. }
  64. func (c *IpBroadcast) GetDevice() error {
  65. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  66. return err
  67. }
  68. func (c IpBroadcast) GetDevices(offset, limit int) ([]IpBroadcast, error) {
  69. var devices []IpBroadcast
  70. err := Db.Debug().Model(&c).Where(" cast_name like ? ", "%"+c.CastName+"%").Offset(offset).Limit(limit).Find(&devices).Error
  71. return devices, err
  72. }
  73. func (c IpBroadcast) GetAllDevices() ([]IpBroadcast, error) {
  74. var devices []IpBroadcast
  75. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId,
  76. c.IsDeleted).Scan(&devices).Error
  77. return devices, err
  78. }
  79. func (c IpBroadcast) GetDevicesByGateway() []IpBroadcast {
  80. var devices []IpBroadcast
  81. Db.Debug().Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  82. c.GatewayId).Find(&devices)
  83. return devices
  84. }
  85. func (c IpBroadcast) GetDevicesByLampPole() []IpBroadcast {
  86. var devices []IpBroadcast
  87. Db.Debug().Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  88. c.LampPoleId).Find(&devices)
  89. return devices
  90. }