bridgeSensorDao.go 4.0 KB

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