lightControlDao.go 5.4 KB

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