onDemandSensorDao.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package dao
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "time"
  5. )
  6. // OnDemandSensor 灯随车走
  7. type OnDemandSensor struct {
  8. ID int `gorm:"primary key" json:"id"` //编号
  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. LampPoleID int `gorm:"type:int" json:"lampPoleID"` //所属灯杆
  14. SensorPhoto string `gorm:"type:varchar(255)" json:"sensorPhoto"` //传感器照片
  15. InstallTime time.Time `gorm:"type:date" json:"installTime"` //安装时间
  16. TenantId int `gorm:"type:int" json:"tenantId"` //租户ID
  17. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  18. CreateUser int64 `gorm:"type:bigint" json:"createUser"` //新增记录操作用户ID
  19. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  20. UpdateUser int64 `gorm:"type:bigint" json:"updateUser"` //修改用户
  21. IsDeleted int `gorm:"type:int; default 0" json:"isDeleted"` //是否删除:0=未删除,1=删除
  22. Status int `gorm:"type:int; default 0" json:"status"` //状态0=正常,1=异常
  23. }
  24. func (OnDemandSensor) TableName() string {
  25. return "t_dev_on_demand_sensor"
  26. }
  27. func (c OnDemandSensor) Delete() error {
  28. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  29. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  30. }
  31. func (c OnDemandSensor) IsExistedBySN() bool {
  32. var count = 0
  33. _ = Db.Debug().Model(&c).Where(" sn = ? and is_deleted = ?",
  34. c.SN, c.IsDeleted).Count(&count).Error
  35. return count > 0
  36. }
  37. func (c OnDemandSensor) IsExistedByNameAndCode() bool {
  38. var devices []OnDemandSensor
  39. err := Db.Debug().Model(&c).Where("sn = ? and is_deleted = ?",
  40. c.SN, c.IsDeleted).Find(&devices).Error
  41. //如果查询不到,返回相应的错误
  42. if gorm.IsRecordNotFoundError(err) {
  43. return false
  44. }
  45. for _, d := range devices {
  46. if d.ID != c.ID {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. func (c *OnDemandSensor) Create() error {
  53. return Db.Debug().Model(&c).Save(&c).Error
  54. }
  55. func (c *OnDemandSensor) Update() error {
  56. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  57. }
  58. func (c *OnDemandSensor) GetDevice() error {
  59. err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  60. return err
  61. }
  62. func (c OnDemandSensor) GetDevices(offset, limit int) ([]OnDemandSensor, error) {
  63. var devices []OnDemandSensor
  64. err := Db.Debug().Model(&c).Where(" name like ? ", "%"+c.Name+"%").Offset(offset).Limit(limit).Find(&devices).Error
  65. return devices, err
  66. }
  67. func (c OnDemandSensor) GetAllDevices() ([]*OnDemandSensor, error) {
  68. var devices []*OnDemandSensor
  69. err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId,
  70. c.IsDeleted).Scan(&devices).Error
  71. return devices, err
  72. }