user.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 int64 `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. Feedback string
  32. ApproveTime time.Time
  33. Email string
  34. }
  35. var PageSize = 10
  36. func (TProvider) TableName() string {
  37. return "t_provider"
  38. }
  39. func (p *TProvider) Insert(db *gorm.DB) error {
  40. return db.Create(p).Error
  41. }
  42. func (p *TProvider) Find(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error {
  43. cond, val, err := whereBuildAndOr(where, or)
  44. if err != nil {
  45. return err
  46. }
  47. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  48. }
  49. func (p *TProvider) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  50. cond, val, err := whereBuild(where)
  51. if err != nil {
  52. return err
  53. }
  54. return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  55. }
  56. func (p *TProvider) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  57. cond, val, err := whereBuild(where)
  58. if err != nil {
  59. return err
  60. }
  61. ps := []TProvider{}
  62. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  63. if err != nil {
  64. return err
  65. }
  66. if len(ps) > 0 {
  67. *p = ps[0]
  68. }
  69. return nil
  70. }
  71. func (p *TProvider) Save(db *gorm.DB) error {
  72. return db.Save(p).Error
  73. }
  74. func (p *TProvider) List(db *gorm.DB, where map[string]interface{}, page int) (list []TProvider, err error) {
  75. if len(where) > 0 {
  76. cond, val, err := whereBuild(where)
  77. if err != nil {
  78. return list, err
  79. }
  80. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  81. return list, result.Error
  82. }
  83. result := db.Table(p.TableName()).Limit(10).Offset(page).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{}) (int64, error) {
  91. if len(where) > 0 {
  92. cond, val, err := whereBuild(where)
  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. type CountResult struct {
  115. Count int64
  116. }
  117. if provider != "" {
  118. sql = fmt.Sprintf("%s and t1.name like '%%%s%%'", sql, provider)
  119. countSql = fmt.Sprintf("%s and t1.name like '%%%s%%'", countSql, provider)
  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. }