ipBroadcastDao.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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地址 //服务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. }