lightControlDao.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "iot_manager_service/util/common"
  5. "time"
  6. )
  7. //LightControl 灯控
  8. type LightControl struct {
  9. ID int `gorm:"primary_key" json:"id"` //编号
  10. Name string `gorm:"type:varchar(64)" json:"name"` //名称
  11. Sn string `gorm:"type:varchar(60)" json:"sn"` //设备序列号
  12. ControlType int `gorm:"type:int" json:"controlType"` //控制器类型 type:0=485灯控,1=NB-iot箱
  13. GroupId int `gorm:"type:int" json:"groupId"` //所属灯杆分组
  14. GatewayId int `gorm:"type:varchar(32)" json:"gatewayId"` //所属网关id
  15. LampPoleId int `gorm:"type:int" json:"lampPoleId"` //所属灯杆 灯杆ID
  16. LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"` //灯杆名称
  17. LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"` //灯杆SN
  18. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  19. LampLat float64 `gorm:"type:double(17,14)" json:"lampLat"` //纬度
  20. LampLng float64 `gorm:"type:double(17,14)" json:"lampLng"` //经度
  21. RatedPower float32 `gorm:"type:double(8,2)" json:"ratedPower"` //额定功率(LED灯)
  22. BrandId int `gorm:"type:int" json:"brandId"` //设备品牌
  23. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  24. InstallTime common.Time `gorm:"type:date" json:"installTime"` //安装时间
  25. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  26. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  27. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  28. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  29. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  30. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  31. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  32. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,(备用,逗号区分)
  33. ControlNo string `gorm:"type:varchar(10)" json:"controlNo"` //编号
  34. ChannelNum int `gorm:"type:int" json:"channelNum"` //通道号
  35. NetworkNum int `gorm:"type:int" json:"networkNum"` //网络号
  36. ZigbeeId int `gorm:"type:int" json:"zigbeeId"` //zigbeeID
  37. ZigbeeName string `gorm:"type:varchar(10)" json:"zigbeeName"` //zigbee名称
  38. ZigbeeSn string `gorm:"type:varchar(10)" json:"zigbeeSn"` //zigbee编码
  39. IsEnable int `gorm:"type:int;default 2" json:"isEnable"` //启用禁用:1启用2禁用
  40. IsOnDemand int `gorm:"type:int; default 0" json:"isOnDemand"` //灯控类型:0-普通灯控,1-灯随车走灯控
  41. }
  42. func (LightControl) TableName() string {
  43. return "device_light_control"
  44. }
  45. func (c *LightControl) Delete() error {
  46. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  47. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  48. }
  49. func (c LightControl) IsExistedBySN() bool {
  50. var count int64
  51. _ = Db.Debug().Model(&c).Where("sn = ? and is_deleted = ?",
  52. c.Sn, c.IsDeleted).Count(&count).Error
  53. return count > 0
  54. }
  55. func (c LightControl) IsExistedByNameAndCode() bool {
  56. var devices []LightControl
  57. err := Db.Debug().Model(&c).Where("pole_sn = ? and is_deleted = ?",
  58. c.TenantId, c.IsDeleted).Find(&devices).Error
  59. if gorm.ErrRecordNotFound == err {
  60. return false
  61. }
  62. for _, d := range devices {
  63. if d.ID != c.ID {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. func (c *LightControl) Create() error {
  70. return Db.Debug().Model(&c).Save(&c).Error
  71. }
  72. func (c *LightControl) Update() error {
  73. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  74. }
  75. func (c *LightControl) UpdateEnable() error {
  76. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"is_enable": c.IsEnable}).Error
  77. }
  78. func (c *LightControl) GetDevice() error {
  79. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).First(&c).Error
  80. return err
  81. }
  82. func (c *LightControl) GetDevicesByGroup() ([]LightControl, error) {
  83. var devices []LightControl
  84. err := Db.Debug().Model(&c).Where(" group_id = ? and is_deleted = 0", c.GroupId).Find(&devices).Error
  85. return devices, err
  86. }
  87. func (c LightControl) GetDevices(offset, limit int) ([]LightControl, error) {
  88. var devices []LightControl
  89. db := Db.Debug().Model(&c)
  90. if c.Sn != "" {
  91. db = db.Where("name like ? or sn like ?", "%"+c.Sn+"%", "%"+c.Sn+"%")
  92. }
  93. if c.ControlType != 0 {
  94. db = db.Where("control_type = ?", c.ControlType)
  95. }
  96. if c.ZigbeeId != 0 {
  97. db = db.Where("zigbee_id = ?", c.ZigbeeId)
  98. }
  99. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
  100. return devices, err
  101. }
  102. func (c LightControl) GetAllDevices() ([]LightControl, error) {
  103. var devices []LightControl
  104. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  105. return devices, err
  106. }
  107. func (c LightControl) GetDevicesByGateway() []LightControl {
  108. var devices []LightControl
  109. Db.Debug().Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  110. c.GatewayId).Find(&devices)
  111. return devices
  112. }
  113. func (c LightControl) GetDevicesByLampPole() []LightControl {
  114. var devices []LightControl
  115. Db.Debug().Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  116. c.LampPoleId).Find(&devices)
  117. return devices
  118. }