infoBoardDao.go 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package dao
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "time"
  5. )
  6. //InfoBoard信息基本屏
  7. type InfoBoard struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. InfoName string `gorm:"type:varchar(60)" json:"infoName"` //名称
  10. SN string `gorm:"type:varchar(60)" json:"sn"` //设备序列号
  11. LampPoleId int `gorm:"type:int" json:"LampPoleId"` //所属灯杆id
  12. LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"` //灯杆名称
  13. LampPoleSN string `gorm:"type:varchar(64)" json:"lampPoleSN"` //灯杆编码
  14. LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
  15. PoleLng float64 `gorm:"type:double(17, 14) " json:"poleLng"` //经度
  16. PoleLat float64 `gorm:"type:double(17, 14) " json:"poleLat"` //纬度
  17. GatewayId string `gorm:"type:varchar(32)" json:"gatewayId"` //所属网关id
  18. GroupId int `gorm:"type:int" json:"groupId"` //所属灯杆分组
  19. Resolution int `gorm:"type:int" json:"resolution"` //分辨率
  20. BrandID int `gorm:"type:int" json:"brandID"` //设备名称
  21. ModelID int `gorm:"type:int" json:"modelID"` //设备型号
  22. InfoSize int `gorm:"type:int" json:"infoSize"` //信息屏尺寸
  23. RatedPower float32 `gorm:"type:float(8, 2); default 0.00" json:"ratedPower"` //额定功率(LED灯)
  24. IPAddress string `gorm:"type:varchar(64)" json:"ipAddress"` //IP地址
  25. BootStartTime string `gorm:"type:time" json:"bootStartTime"` //开机开始时间
  26. BootEndTime string `gorm:"type:time" json:"bootEndTime"` //开机结束时间
  27. NeverCloseDown int `gorm:"type:int" json:"neverCloseDown"` //永不关机:1-选择,2-取消
  28. InstallTime time.Time `gorm:"type:date" json:"installTime"` //安装时间
  29. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  30. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  31. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  32. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  33. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  34. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  35. Tag string `gorm:"type:varchar(255)" json:"tag"` //标签,保留字段(逗号区分)
  36. ExteriorUid int `gorm:"type:bigint" json:"exteriorUid"` //外设ID
  37. IsEnable int `gorm:"type:int; default 2 " json:"isEnable"` //启用禁用:1启用,2禁用
  38. }
  39. func (InfoBoard) TableName() string {
  40. return "t_dev_info_board"
  41. }
  42. func (c InfoBoard) Delete() error {
  43. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  44. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  45. }
  46. func (c InfoBoard) IsExistedBySN() bool {
  47. var count = 0
  48. _ = Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
  49. c.SN, c.IsDeleted).Count(&count).Error
  50. return count > 0
  51. }
  52. func (c InfoBoard) IsExistedByNameAndCode() bool {
  53. var devices []InfoBoard
  54. err := Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
  55. c.SN, c.IsDeleted).Find(&devices).Error
  56. //如果查询不到,返回相应的错误
  57. if gorm.IsRecordNotFoundError(err) {
  58. return false
  59. }
  60. for _, d := range devices {
  61. if d.ID != c.ID {
  62. return true
  63. }
  64. }
  65. return false
  66. }
  67. func (c *InfoBoard) Create() error {
  68. return Db.Debug().Model(&c).Save(&c).Error
  69. }
  70. func (c *InfoBoard) Update() error {
  71. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  72. }
  73. func (c *InfoBoard) GetDevice() error {
  74. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  75. return err
  76. }
  77. func (c InfoBoard) GetDevices(offset, limit int) ([]InfoBoard, error) {
  78. var devices []InfoBoard
  79. err := Db.Debug().Model(&c).Where(" info_name like ? ", "%"+c.InfoName+"%").Offset(offset).Limit(limit).Find(&devices).Error
  80. return devices, err
  81. }
  82. func (c InfoBoard) GetAllDevices() ([]*InfoBoard, error) {
  83. var devices []*InfoBoard
  84. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  85. return devices, err
  86. }