provider_device.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.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. Feedback string
  16. }
  17. func (TProviderDevice) TableName() string {
  18. return "t_provider_device"
  19. }
  20. func (p *TProviderDevice) Insert(db *gorm.DB) error {
  21. return db.Create(p).Error
  22. }
  23. func (p *TProviderDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  24. cond, val, err := whereBuild(where)
  25. if err != nil {
  26. return err
  27. }
  28. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  29. }
  30. func (p *TProviderDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  31. cond, val, err := whereBuild(where)
  32. if err != nil {
  33. return err
  34. }
  35. return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  36. }
  37. func (p *TProviderDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  38. cond, val, err := whereBuild(where)
  39. if err != nil {
  40. return err
  41. }
  42. ps := []TProviderDevice{}
  43. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  44. if err != nil {
  45. return err
  46. }
  47. if len(ps) > 0 {
  48. *p = ps[0]
  49. }
  50. return nil
  51. }
  52. func (p *TProviderDevice) Save(db *gorm.DB) error {
  53. return db.Save(p).Error
  54. }
  55. func (p *TProviderDevice) List(db *gorm.DB, where map[string]interface{}) (list []TProviderDevice, err error) {
  56. if len(where) > 0 {
  57. cond, val, err := whereBuild(where)
  58. if err != nil {
  59. return list, err
  60. }
  61. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  62. return list, result.Error
  63. }
  64. result := db.Table(p.TableName()).Find(&list)
  65. return list, result.Error
  66. }
  67. func (p *TProviderDevice) All(db *gorm.DB) (list []TProviderDevice, err error) {
  68. result := db.Table(p.TableName()).Find(&list)
  69. return list, result.Error
  70. }
  71. func (p *TProviderDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  72. if len(where) > 0 {
  73. cond, val, err := whereBuild(where)
  74. if err != nil {
  75. return 0, err
  76. }
  77. ret := int64(0)
  78. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  79. return ret, err
  80. }
  81. ret := int64(0)
  82. err := db.Table(p.TableName()).Count(&ret).Error
  83. return ret, err
  84. }
  85. func (p *TProviderDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  86. cond, val, err := whereBuild(where)
  87. if err != nil {
  88. return err
  89. }
  90. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  91. }