zigbeeDao.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "iot_manager_service/util/common"
  5. "time"
  6. )
  7. // Zigbee集控器
  8. type Zigbee 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. GroupId int `gorm:"type:int" json:"groupId"` //所属灯杆分组id
  13. LampPoleId int `gorm:"type:int" json:"lampPoleId"` //所属灯杆id
  14. LampPoleName string `gorm:"type: varchar(64)" json:"lampPoleName"` //灯杆名称
  15. LampPoleSn string `gorm:"type: varchar(64)" json:"lampPoleSn"` //灯杆编码
  16. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  17. PoleLng float64 `gorm:"type:double(17, 14)" json:"poleLng"` //经度
  18. PoleLat float64 `gorm:"type:double(17, 14)" json:"poleLat"` //纬度
  19. BrandId int `gorm:"type:int" json:"brandId"` //设备名称
  20. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  21. GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关id
  22. GatewayName string `gorm:"type:varchar(64)" json:"gatewayName"` //所属网关名称
  23. GatewaySn string `gorm:"type:varchar(60)" json:"gatewaySn"` //所属网关编码
  24. ChannelNum int `gorm:"type:int" json:"chanelNum"` //通道号
  25. NetworkNum int `gorm:"type:int" json:"networkNum"` //网络号
  26. InstallTime common.Time `gorm:"type:date" json:"installTime"` //安装时间
  27. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户id
  28. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  29. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  30. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  31. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  32. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  33. Status int `gorm:"type:int" json:"status"` //状态0=正常 状态0=正常,1=异常
  34. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,保留字段(逗号区分)
  35. IPAddress string `gorm:"type:varchar(64)" json:"ipAddress"` //IP地址-备用
  36. CountRel int `gorm:"-;default 0" json:"countRel"` //关联终端数-暂无数据 先写个0
  37. }
  38. func (Zigbee) TableName() string {
  39. return "device_zigbee"
  40. }
  41. func (c Zigbee) Delete() error {
  42. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  43. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  44. }
  45. func (c Zigbee) IsExistedBySN() bool {
  46. var count int64
  47. _ = Db.Model(&c).Where(" sn = ? and is_deleted = ?",
  48. c.Sn, c.IsDeleted).Count(&count).Error
  49. return count > 0
  50. }
  51. func (c Zigbee) IsExistedByNameAndCode() bool {
  52. var devices []Zigbee
  53. err := Db.Model(&c).Where(" sn = ? and is_deleted = ?",
  54. c.Sn, c.IsDeleted).Find(&devices).Error
  55. //如果查询不到,返回相应的错误
  56. if gorm.ErrRecordNotFound == err {
  57. return false
  58. }
  59. for _, d := range devices {
  60. if d.ID != c.ID {
  61. return true
  62. }
  63. }
  64. return false
  65. }
  66. func (c *Zigbee) Create() error {
  67. return Db.Model(&c).Save(&c).Error
  68. }
  69. func (c *Zigbee) Update() error {
  70. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  71. }
  72. func (c *Zigbee) GetDevice() error {
  73. err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  74. return err
  75. }
  76. func (c Zigbee) GetDevices(offset, limit int) ([]Zigbee, int64, error) {
  77. var devices []Zigbee
  78. var count int64
  79. db := Db.Model(&c).Where("is_deleted=0 and name like ? ", "%"+c.Name+"%")
  80. db.Count(&count)
  81. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  82. return devices, count, err
  83. }
  84. func (c Zigbee) GetAllDevices() ([]Zigbee, error) {
  85. var devices []Zigbee
  86. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  87. return devices, err
  88. }
  89. func (c Zigbee) GetDevicesByGateway() []Zigbee {
  90. var devices []Zigbee
  91. Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  92. c.GatewayId).Find(&devices)
  93. return devices
  94. }
  95. func (c Zigbee) GetDevicesByLampPole() []Zigbee {
  96. var devices []Zigbee
  97. Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  98. c.LampPoleId).Find(&devices)
  99. return devices
  100. }
  101. func (c Zigbee) DeviceCount1() (int64, int64) {
  102. var all, fault int64
  103. err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
  104. if err != nil {
  105. all = -1
  106. }
  107. err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
  108. if err != nil {
  109. fault = -1
  110. }
  111. return all, fault
  112. }
  113. func (c Zigbee) ListDevices1() []Device {
  114. var devices []Zigbee
  115. Db.Model(&c).Where("is_deleted = 0").Find(&devices)
  116. var rsp = make([]Device, 0, len(devices))
  117. for _, v := range devices {
  118. var d = Device{
  119. Lng: v.PoleLng,
  120. Lat: v.PoleLat,
  121. Status: v.Status,
  122. Name: v.Name,
  123. Code: v.Sn,
  124. LampPoleName: v.LampPoleName,
  125. Model: "模型",
  126. LinkNum: getLinkNum(v.LampPoleSn),
  127. }
  128. rsp = append(rsp, d)
  129. }
  130. return rsp
  131. }