lightControlDao.go 6.7 KB

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