provider_device.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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:ProviderId" json:"provider_id" form:"provider_id"`
  11. DeviceCode int32 `gorm:"column:DeviceCode" json:"device_code" form:"device_code"`
  12. Status int32 `gorm:"column:Status" json:"status" form:"status"`
  13. CreatedAt time.Time `gorm:"column:CreatedAt"`
  14. Feedback string `gorm:"column:Feedback"`
  15. Comment string `gorm:"column:Comment"`
  16. Appendix string `gorm:"column:Appendix"`
  17. Agent string `gorm:"column:Agent"`
  18. AgentPhone string `gorm:"column:AgentPhone"`
  19. ApproveTime time.Time `gorm:"column:ApproveTime"`
  20. Reviewer string `gorm:"column:Reviewer"`
  21. }
  22. func (TProviderDevice) TableName() string {
  23. return "ProviderDevice"
  24. }
  25. func (p *TProviderDevice) Insert(db *gorm.DB) error {
  26. return db.Create(p).Error
  27. }
  28. func (p *TProviderDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  29. cond, val, err := whereBuild(where)
  30. if err != nil {
  31. return err
  32. }
  33. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  34. }
  35. func (p *TProviderDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  36. cond, val, err := whereBuild(where)
  37. if err != nil {
  38. return err
  39. }
  40. return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  41. }
  42. func (p *TProviderDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  43. cond, val, err := whereBuild(where)
  44. if err != nil {
  45. return err
  46. }
  47. ps := []TProviderDevice{}
  48. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  49. if err != nil {
  50. return err
  51. }
  52. if len(ps) > 0 {
  53. *p = ps[0]
  54. }
  55. return nil
  56. }
  57. func (p *TProviderDevice) Save(db *gorm.DB) error {
  58. return db.Save(p).Error
  59. }
  60. func (p *TProviderDevice) List(db *gorm.DB, where map[string]interface{}) (list []TProviderDevice, err error) {
  61. if len(where) > 0 {
  62. cond, val, err := whereBuild(where)
  63. if err != nil {
  64. return list, err
  65. }
  66. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  67. return list, result.Error
  68. }
  69. result := db.Table(p.TableName()).Find(&list)
  70. return list, result.Error
  71. }
  72. func (p *TProviderDevice) All(db *gorm.DB) (list []TProviderDevice, err error) {
  73. result := db.Table(p.TableName()).Find(&list)
  74. return list, result.Error
  75. }
  76. func (p *TProviderDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  77. if len(where) > 0 {
  78. cond, val, err := whereBuild(where)
  79. if err != nil {
  80. return 0, err
  81. }
  82. ret := int64(0)
  83. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  84. return ret, err
  85. }
  86. ret := int64(0)
  87. err := db.Table(p.TableName()).Count(&ret).Error
  88. return ret, err
  89. }
  90. func (p *TProviderDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  91. cond, val, err := whereBuild(where)
  92. if err != nil {
  93. return err
  94. }
  95. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  96. }