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