provider.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "fmt"
  6. "time"
  7. "github.com/jinzhu/gorm"
  8. )
  9. type TProvider struct {
  10. Id int64 `gorm:"column:id" json:"id" form:"id"`
  11. Name string `gorm:"column:name" json:"name" form:"name"`
  12. SocialCode string `gorm:"column:social_code" json:"social_code" form:"social_code"`
  13. LegalPerson string `gorm:"column:legal_person" json:"legal_person" form:"legal_person"`
  14. IdCert string `gorm:"column:id_cert" json:"id_cert" form:"id_cert"`
  15. EnterpriseLocation int32 `gorm:"column:enterprise_location" json:"enterprise_location" form:"enterprise_location"`
  16. BusinessLicense string `gorm:"column:business_license" json:"business_license" form:"business_license"`
  17. IntegrityManagementLetter string `gorm:"column:integrity_management_letter" json:"integrity_management_letter" form:"integrity_management_letter"`
  18. LegalPersonLetter string `gorm:"column:legal_person_letter" json:"legal_person_letter" form:"legal_person_letter"`
  19. BusinessResponsibleLetter string `gorm:"column:business_responsible_letter" json:"business_responsible_letter" form:"business_responsible_letter"`
  20. UserName string `gorm:"column:user_name" json:"user_name" form:"user_name"`
  21. Passwd string `gorm:"column:passwd" json:"passwd" form:"passwd"`
  22. BusinessContact string `gorm:"column:business_contact" json:"business_contact" form:"business_contact"`
  23. BusinessContactPhone string `gorm:"column:business_contact_phone" json:"business_contact_phone" form:"business_contact_phone"`
  24. BusinessResponsible string `gorm:"column:business_responsible" json:"business_responsible" form:"business_responsible"`
  25. BusinessResponsiblePhone string `gorm:"column:business_responsible_phone" json:"business_responsible_phone" form:"business_responsible_phone"`
  26. EmergencyContact string `gorm:"column:emergency_contact" json:"emergency_contact" form:"emergency_contact"`
  27. EmergencyContactPhone string `gorm:"column:emergency_contact_phone" json:"emergency_contact_phone" form:"emergency_contact_phone"`
  28. Status int32 `gorm:"column:status" json:"status" form:"status"`
  29. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  30. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  31. ApproveTime time.Time
  32. Feedback string
  33. }
  34. var PageSize = int32(10)
  35. func (TProvider) TableName() string {
  36. return "t_provider"
  37. }
  38. func (p *TProvider) Insert(db *gorm.DB) error {
  39. return db.Create(p).Error
  40. }
  41. func (p *TProvider) Find(db *gorm.DB, where map[string]interface{}) error {
  42. cond, val, err := whereBuild(where)
  43. if err != nil {
  44. return err
  45. }
  46. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  47. }
  48. func (p *TProvider) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  49. cond, val, err := whereBuild(where)
  50. if err != nil {
  51. return err
  52. }
  53. return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  54. }
  55. func (p *TProvider) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  56. cond, val, err := whereBuild(where)
  57. if err != nil {
  58. return err
  59. }
  60. ps := []TProvider{}
  61. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  62. if err != nil {
  63. return err
  64. }
  65. if len(ps) > 0 {
  66. *p = ps[0]
  67. }
  68. return nil
  69. }
  70. func (p *TProvider) Save(db *gorm.DB) error {
  71. return db.Save(p).Error
  72. }
  73. func (p *TProvider) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32) (list []TProvider, err error) {
  74. offset := (page - 1) *PageSize
  75. if len(where) > 0 || len(or) > 0 {
  76. cond, val, err := whereBuildAndOr(where, or)
  77. if err != nil {
  78. return list, err
  79. }
  80. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(PageSize).Offset(offset).Find(&list)
  81. return list, result.Error
  82. }
  83. result := db.Table(p.TableName()).Order("created_at desc").Limit(10).Offset(offset).Find(&list)
  84. return list, result.Error
  85. }
  86. func (p *TProvider) All(db *gorm.DB) (list []TProvider, err error) {
  87. result := db.Table(p.TableName()).Find(&list)
  88. return list, result.Error
  89. }
  90. func (p *TProvider) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  91. if len(where) > 0 || len(or) > 0 {
  92. cond, val, err := whereBuildAndOr(where, or)
  93. if err != nil {
  94. return 0, err
  95. }
  96. ret := int64(0)
  97. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  98. return ret, err
  99. }
  100. ret := int64(0)
  101. err := db.Table(p.TableName()).Count(&ret).Error
  102. return ret, err
  103. }
  104. func (p *TProvider) Del(db *gorm.DB, where map[string]interface{}) error {
  105. cond, val, err := whereBuild(where)
  106. if err != nil {
  107. return err
  108. }
  109. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  110. }
  111. func PublicityList(db *gorm.DB, provider string) ([]TProvider, int64, error) {
  112. sql := fmt.Sprintf("select t1.* from t_provider as t1 where(select count(1) from t_provider_device where provider_id=t1.id and status = 3) > 0 and t1.status = 1")
  113. countSql := fmt.Sprintf("select count(1) as count from t_provider as t1 where(select count(1) from t_provider_device where provider_id=t1.id and status = 3) > 0 ")
  114. if provider != "" {
  115. sql = fmt.Sprintf("%s and t1.name like '%%%s%%'", sql, provider)
  116. countSql = fmt.Sprintf("%s and t1.name like '%%%s%%'", countSql, provider)
  117. }
  118. type CountResult struct {
  119. Count int64
  120. }
  121. array := []CountResult{}
  122. err := db.Raw(countSql).Scan(&array).Error
  123. if err != nil {
  124. return nil, 0, err
  125. }
  126. total := array[0].Count
  127. if total == 0 {
  128. return nil, 0, nil
  129. }
  130. list := []TProvider{}
  131. err = db.Raw(sql).Scan(&list).Error
  132. if err != nil {
  133. return nil, 0, err
  134. }
  135. return list, total, nil
  136. }