provider_device.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package model
  4. import (
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. type TProviderDevice struct {
  9. Id int64 `gorm:"column:id" json:"id" form:"id"`
  10. ProviderId int64 `gorm:"column:provider_id" json:"provider_id" form:"provider_id"`
  11. DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
  12. Status int32 `gorm:"column:status" json:"status" form:"status"`
  13. CreatedAt time.Time
  14. UpdatedAt time.Time
  15. }
  16. func (TProviderDevice) TableName() string {
  17. return "t_provider_device"
  18. }
  19. func (p *TProviderDevice) Insert(db *gorm.DB) error {
  20. return db.Create(p).Error
  21. }
  22. func (p *TProviderDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  23. cond, val, err := whereBuild(where)
  24. if err != nil {
  25. return err
  26. }
  27. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  28. }
  29. func (p *TProviderDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  30. cond, val, err := whereBuild(where)
  31. if err != nil {
  32. return err
  33. }
  34. return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  35. }
  36. func (p *TProviderDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  37. cond, val, err := whereBuild(where)
  38. if err != nil {
  39. return err
  40. }
  41. ps := []TProviderDevice{}
  42. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  43. if err != nil {
  44. return err
  45. }
  46. if len(ps) > 0 {
  47. *p = ps[0]
  48. }
  49. return nil
  50. }
  51. func (p *TProviderDevice) Save(db *gorm.DB) error {
  52. return db.Save(p).Error
  53. }
  54. func (p *TProviderDevice) List(db *gorm.DB, where map[string]interface{}) (list []TProviderDevice, err error) {
  55. if len(where) > 0 {
  56. cond, val, err := whereBuild(where)
  57. if err != nil {
  58. return list, err
  59. }
  60. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  61. return list, result.Error
  62. }
  63. result := db.Table(p.TableName()).Find(&list)
  64. return list, result.Error
  65. }
  66. func (p *TProviderDevice) All(db *gorm.DB) (list []TProviderDevice, err error) {
  67. result := db.Table(p.TableName()).Find(&list)
  68. return list, result.Error
  69. }
  70. func (p *TProviderDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  71. if len(where) > 0 {
  72. cond, val, err := whereBuild(where)
  73. if err != nil {
  74. return 0, err
  75. }
  76. ret := int64(0)
  77. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  78. return ret, err
  79. }
  80. ret := int64(0)
  81. err := db.Table(p.TableName()).Count(&ret).Error
  82. return ret, err
  83. }
  84. func (p *TProviderDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  85. cond, val, err := whereBuild(where)
  86. if err != nil {
  87. return err
  88. }
  89. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  90. }