bridgeSensorDao.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dao
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "time"
  5. )
  6. type BridgeSensor struct {
  7. ID int `gorm:"primary_key" json:"id"` //编号
  8. SensorType int `gorm:"type:int" json:"sensorType"` //传感器类型
  9. Name string `gorm:"type:varchar(64)" json:"name"` //名称
  10. SN string `gorm:"type:varchar(60)" json:"sn"` //唯一编码
  11. BrandID int `gorm:"type:int" json:"brandID"` //设备名称
  12. ModelID int `gorm:"type:int" json:"modelID"` //设备型号
  13. GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关id
  14. GatewaySN string `gorm:"type:varchar(60)" json:"gatewaySN"` //所属网关编码
  15. InstallPosition string `gorm:"type:varchar(100)" json:"installPosition"` //安装位置
  16. InstallTime *time.Time `gorm:"type:date" json:"installTime"` //安装时间
  17. IPAddress string `gorm:"type:varchar(64)" json:"ipAddress"` //IP地址-备用
  18. TenantID string `gorm:"type:varchar(12)" json:"tenantID"` //租户id
  19. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  20. CreateUser string `gorm:"type:varchar(60)" json:"createUser"` //新增记录操作用户ID
  21. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  22. UpdateUser string `gorm:"type:varchar(60)" json:"updateUser"` //修改用户
  23. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  24. Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常
  25. IsEnable int `gorm:"type:tinyint; default 1" json:"isEnable"` //启用禁用:1启用,2禁用
  26. TerrainClearance float32 `gorm:"double(4, 2)" json:"terrainClearance"` //状态 0=正常,1=异常
  27. }
  28. func (BridgeSensor) TableName() string {
  29. return "t_dev_bridge_sensor"
  30. }
  31. func (c BridgeSensor) Delete() error {
  32. return GDb.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  33. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  34. }
  35. func (c BridgeSensor) IsExistedBySN() bool {
  36. var count = 0
  37. _ = GDb.Model(&c).Where(" tsn = ? and is_deleted = ?",
  38. c.SN, c.IsDeleted).Count(&count).Error
  39. return count > 0
  40. }
  41. func (c BridgeSensor) IsExistedByNameAndCode() bool {
  42. var devices []BridgeSensor
  43. err := GDb.Model(&c).Where(" sn = ? and is_deleted = ?",
  44. c.SN, c.IsDeleted).Find(&devices).Error
  45. //如果查询不到,返回相应的错误
  46. if gorm.IsRecordNotFoundError(err) {
  47. return false
  48. }
  49. for _, d := range devices {
  50. if d.ID != c.ID {
  51. return true
  52. }
  53. }
  54. return false
  55. }
  56. func (c *BridgeSensor) Create() error {
  57. return GDb.Model(&c).Save(&c).Error
  58. }
  59. func (c *BridgeSensor) Update() error {
  60. return GDb.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  61. }
  62. func (c *BridgeSensor) GetDevice() error {
  63. err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  64. return err
  65. }
  66. func (c BridgeSensor) GetDevices(offset, limit int) ([]BridgeSensor, error) {
  67. var devices []BridgeSensor
  68. err := GDb.Model(&c).Where(" name like ? and is_deleted = 0", "%"+c.Name+"%").Offset(offset).Limit(limit).Find(&devices).Error
  69. return devices, err
  70. }
  71. func (c BridgeSensor) GetAllDevices() ([]*BridgeSensor, error) {
  72. var devices []*BridgeSensor
  73. err := GDb.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantID, c.IsDeleted).Scan(&devices).Error
  74. return devices, err
  75. }
  76. func (c *BridgeSensor) UpdateEnable() error {
  77. return GDb.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"is_enable": c.IsEnable}).Error
  78. }