ipBroadcastDao.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "iot_manager_service/util/common"
  5. "time"
  6. )
  7. type IpBroadcast struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. CastName string `gorm:"type:varchar(64)" json:"castName"` //设备名称
  10. CastSn string `gorm:"type:varchar(60)" json:"castSn"` //设备序列号
  11. LampPoleId int `gorm:"type:int" json:"lampPoleId"` //所属灯杆id
  12. LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"` //所属灯杆sn
  13. GroupId int `gorm:"type:int" json:"groupId"` //灯杆分组ID
  14. GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关id
  15. GatewaySn string `gorm:"type:varchar(64)" json:"gatewaySn"` //所属网关sn
  16. BrandId int `gorm:"type:int" json:"brandId"` //设备名称
  17. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  18. RatedPower float32 `gorm:"type:float(8, 2); default 0.00" json:"ratedPower"` //额定功率(LED灯)
  19. IPAddress string `gorm:"type:varchar(50)" json:"ipAddress"` //IP地址
  20. PlayState string `gorm:"-" json:"playState"` //播放状态
  21. ServiceIpAddress string `gorm:"type:varchar(50)" json:"serviceIpAddress"` //服务IP地址
  22. ServerPort int `gorm:"type:int" json:"serverPort"` //服务端口
  23. SoundVolume int `gorm:"type:int;default=0" json:"soundVolume"` //音量0-100换算
  24. InstallTime common.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 int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  28. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  29. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  30. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  31. }
  32. func (IpBroadcast) TableName() string {
  33. return "device_ip_broadcast"
  34. }
  35. func (c IpBroadcast) Delete() error {
  36. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  37. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  38. }
  39. func (c IpBroadcast) IsExistedBySN() bool {
  40. var count int64
  41. _ = Db.Model(&c).Where(" cast_sn = ? and is_deleted = ?",
  42. c.CastSn, c.IsDeleted).Count(&count).Error
  43. return count > 0
  44. }
  45. func (c IpBroadcast) IsExistedByNameAndCode() bool {
  46. var devices []IpBroadcast
  47. err := Db.Model(&c).Where(" cast_sn = ? and is_deleted = ?",
  48. c.CastSn, c.IsDeleted).Find(&devices).Error
  49. //如果查询不到,返回相应的错误
  50. if gorm.ErrRecordNotFound == err {
  51. return false
  52. }
  53. for _, d := range devices {
  54. if d.ID != c.ID {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. func (c *IpBroadcast) Create() error {
  61. return Db.Model(&c).Save(&c).Error
  62. }
  63. func (c *IpBroadcast) Update() error {
  64. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  65. }
  66. func (c *IpBroadcast) GetDevice() error {
  67. err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  68. return err
  69. }
  70. func (c IpBroadcast) GetDevices(offset, limit int) ([]IpBroadcast, int64, error) {
  71. var devices []IpBroadcast
  72. var count int64
  73. db := Db.Model(&c).Where("is_deleted = 0 and cast_name like ? ", "%"+c.CastName+"%")
  74. db.Count(&count)
  75. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  76. return devices, count, err
  77. }
  78. func (c IpBroadcast) GetAllDevices() ([]IpBroadcast, error) {
  79. var devices []IpBroadcast
  80. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId,
  81. c.IsDeleted).Scan(&devices).Error
  82. return devices, err
  83. }
  84. func (c IpBroadcast) GetDevicesByGateway() []IpBroadcast {
  85. var devices []IpBroadcast
  86. Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  87. c.GatewayId).Find(&devices)
  88. return devices
  89. }
  90. func (c IpBroadcast) GetDevicesByLampPole() []IpBroadcast {
  91. var devices []IpBroadcast
  92. Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  93. c.LampPoleId).Find(&devices)
  94. return devices
  95. }
  96. func (c IpBroadcast) GetAll() []IpBroadcast {
  97. var devices []IpBroadcast
  98. Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId,
  99. c.IsDeleted).Scan(&devices)
  100. return devices
  101. }
  102. func (c IpBroadcast) GetByIds(ids string) []IpBroadcast {
  103. var devices []IpBroadcast
  104. Db.Model(&c).Where(" tenant_id = ? and is_deleted = ?", c.TenantId,
  105. c.IsDeleted).Where("id in (" + ids + ")").Scan(&devices)
  106. return devices
  107. }
  108. func (c IpBroadcast) GetAllDevicesNotTenant() ([]*IpBroadcast, error) {
  109. var devices []*IpBroadcast
  110. db := Db.Model(&c)
  111. if c.ID > 0 {
  112. db.Where("id=?", c.ID)
  113. }
  114. err := db.Where("is_deleted = 0 ").Scan(&devices).Error
  115. return devices, err
  116. }
  117. func (c IpBroadcast) DeviceCount1() (int64, int64) {
  118. var all, fault int64
  119. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  120. if err != nil {
  121. all = 0
  122. }
  123. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  124. if err != nil {
  125. fault = 0
  126. }
  127. return all, fault
  128. }
  129. func (c IpBroadcast) ListDevices1() []Device {
  130. var devices []IpBroadcast
  131. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  132. var rsp = make([]Device, 0, len(devices))
  133. for _, v := range devices {
  134. var d = Device{
  135. Name: v.CastName,
  136. Code: v.CastSn,
  137. Model: "模型",
  138. }
  139. rsp = append(rsp, d)
  140. }
  141. return rsp
  142. }