infoBoardDao.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package dao
  2. import (
  3. "fmt"
  4. "gorm.io/gorm"
  5. "iot_manager_service/util/common"
  6. "time"
  7. )
  8. // InfoBoard InfoBoard信息基本屏
  9. type InfoBoard struct {
  10. ID int `gorm:"primary_key" json:"id"` //编号
  11. InfoName string `gorm:"type:varchar(60)" json:"infoName"` //名称
  12. Sn string `gorm:"type:varchar(60)" json:"sn"` //设备序列号
  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. GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关id
  20. GroupId int `gorm:"type:int" json:"groupId"` //所属灯杆分组
  21. Resolution int `gorm:"type:int" json:"resolution"` //分辨率
  22. BrandId int `gorm:"type:int" json:"brandId"` //设备名
  23. ModelId int `gorm:"type:int" json:"modelId"` //设备型号
  24. InfoSize int `gorm:"type:int" json:"infoSize"` //信息屏尺寸
  25. RatedPower float32 `gorm:"type:float(8, 2); default 0.00" json:"ratedPower"` //额定功率(LED灯)
  26. IPAddress string `gorm:"type:varchar(64)" json:"ipAddress"` //IP地址
  27. BootStartTime string `gorm:"type:varchar(64)" json:"bootStartTime"` //开机开始时间
  28. BootEndTime string `gorm:"type:varchar(64)" json:"bootEndTime"` //开机结束时间
  29. NeverCloseDown int `gorm:"type:int" json:"neverCloseDown"` //永不关机:1-选择,2-取消
  30. InstallTime common.Time `gorm:"type:date" json:"installTime"` //安装时间
  31. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  32. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  33. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  34. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  35. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  36. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  37. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,保留字段(逗号区分)
  38. ExteriorUid int `gorm:"type:bigint" json:"exteriorUid"` //外设ID
  39. IsEnable int `gorm:"type:int; default 2 " json:"isEnable"` //启用禁用:1启用,2禁用
  40. }
  41. func (InfoBoard) TableName() string {
  42. return "device_info_board"
  43. }
  44. func (c InfoBoard) 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 InfoBoard) IsExistedBySN() bool {
  49. var count int64
  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 InfoBoard) IsExistedByNameAndCode() bool {
  55. var devices []InfoBoard
  56. err := Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
  57. c.Sn, c.IsDeleted).Find(&devices).Error
  58. //如果查询不到,返回相应的错误
  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 *InfoBoard) Create() error {
  70. return Db.Debug().Model(&c).Save(&c).Error
  71. }
  72. func (c *InfoBoard) Update() error {
  73. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  74. }
  75. func (c *InfoBoard) GetDevice() error {
  76. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  77. return err
  78. }
  79. func (c InfoBoard) GetDevices(offset, limit int) ([]InfoBoard, error) {
  80. var devices []InfoBoard
  81. err := Db.Debug().Model(&c).Where(" info_name like ? ", "%"+c.InfoName+"%").Offset(offset).Limit(limit).Find(&devices).Error
  82. return devices, err
  83. }
  84. func (c InfoBoard) GetAllDevices() ([]*InfoBoard, error) {
  85. var devices []*InfoBoard
  86. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  87. return devices, err
  88. }
  89. func (c InfoBoard) GetDevicesByGateway() []InfoBoard {
  90. var devices []InfoBoard
  91. Db.Debug().Model(&c).Where(" gateway_id = ? and is_deleted = 0",
  92. c.GatewayId).Find(&devices)
  93. return devices
  94. }
  95. func (c InfoBoard) GetDevicesByLampPole() []InfoBoard {
  96. var devices []InfoBoard
  97. Db.Debug().Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
  98. c.LampPoleId).Find(&devices)
  99. return devices
  100. }
  101. func (c InfoBoard) GetDevicesByResolution() []InfoBoard {
  102. var devices []InfoBoard
  103. Db.Debug().Model(&c).Where("resolution = ? and tenant_id = ? and is_deleted = 0",
  104. c.Resolution, c.TenantId).Find(&devices)
  105. return devices
  106. }
  107. func (c InfoBoard) GetDevicesByIds(ids string) []InfoBoard {
  108. var devices []InfoBoard
  109. Db.Debug().Model(&c).Where("id in ("+ids+")"+" and tenant_id = ? and is_deleted = 0",
  110. c.TenantId).Scan(&devices)
  111. fmt.Printf("devices = %v", devices)
  112. return devices
  113. }