checkPointDao.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package dao
  2. import (
  3. "time"
  4. )
  5. // CheckPoint 卡口
  6. type CheckPoint struct {
  7. ID int `gorm:"primary_key" json:"id"` //编号
  8. PointName string `gorm:"type:varchar(64)" json:"pointName"` //卡口名称
  9. PointSN string `gorm:"type:varchar(60)" json:"pointSn"` //卡口编码
  10. PointLocation string `gorm:"type:varchar(150)" json:"pointLocation"` //卡口位置
  11. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户ID
  12. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  13. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  14. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  15. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  16. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  17. }
  18. func (CheckPoint) TableName() string {
  19. return "device_check_point"
  20. }
  21. func (c CheckPoint) IsExistedBySN() bool {
  22. var count int64
  23. _ = Db.Model(&c).Where("point_sn = ? and is_deleted = ?",
  24. c.PointSN, c.IsDeleted).Count(&count).Error
  25. return count > 0
  26. }
  27. func (c *CheckPoint) Create() error {
  28. return Db.Model(&c).Save(&c).Error
  29. }
  30. func (c *CheckPoint) Update() error {
  31. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  32. }
  33. func (c *CheckPoint) GetDevice() error {
  34. err := Db.Model(&c).Where(" id = ? ", c.ID).First(&c).Error
  35. return err
  36. }
  37. func (c CheckPoint) GetDevices(offset, limit int) ([]CheckPoint, int64, error) {
  38. var Points []CheckPoint
  39. var count int64
  40. db := Db.Model(&c)
  41. if c.PointSN != "" {
  42. db = db.Where("point_name like ? or point_sn like ?", "%"+c.PointSN+"%", "%"+c.PointSN+"%")
  43. }
  44. db = db.Where("is_deleted = 0")
  45. db.Count(&count)
  46. err := db.Offset(offset).Limit(limit).Find(&Points).Error
  47. return Points, count, err
  48. }
  49. func (c *CheckPoint) Delete() error {
  50. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  51. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  52. }
  53. func (c CheckPoint) GetAllDevices() ([]*CheckPoint, error) {
  54. var Points []*CheckPoint
  55. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&Points).Error
  56. return Points, err
  57. }