user.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 string `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. }
  32. var PageSize = 10
  33. func (TProvider) TableName() string {
  34. return "t_provider"
  35. }
  36. func (p *TProvider) Insert(db *gorm.DB) error {
  37. return db.Create(p).Error
  38. }
  39. func (p *TProvider) Find(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error {
  40. cond, val, err := whereBuildAndOr(where, or)
  41. if err != nil {
  42. return err
  43. }
  44. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  45. }
  46. func (p *TProvider) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  47. cond, val, err := whereBuild(where)
  48. if err != nil {
  49. return err
  50. }
  51. return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  52. }
  53. func (p *TProvider) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  54. cond, val, err := whereBuild(where)
  55. if err != nil {
  56. return err
  57. }
  58. ps := []TProvider{}
  59. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  60. if err != nil {
  61. return err
  62. }
  63. if len(ps) > 0 {
  64. *p = ps[0]
  65. }
  66. return nil
  67. }
  68. func (p *TProvider) Save(db *gorm.DB) error {
  69. return db.Save(p).Error
  70. }
  71. func (p *TProvider) List(db *gorm.DB, where map[string]interface{}, page int) (list []TProvider, err error) {
  72. if len(where) > 0 {
  73. cond, val, err := whereBuild(where)
  74. if err != nil {
  75. return list, err
  76. }
  77. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  78. return list, result.Error
  79. }
  80. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  81. return list, result.Error
  82. }
  83. func (p *TProvider) All(db *gorm.DB) (list []TProvider, err error) {
  84. result := db.Table(p.TableName()).Find(&list)
  85. return list, result.Error
  86. }
  87. func (p *TProvider) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  88. if len(where) > 0 {
  89. cond, val, err := whereBuild(where)
  90. if err != nil {
  91. return 0, err
  92. }
  93. ret := int64(0)
  94. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  95. return ret, err
  96. }
  97. ret := int64(0)
  98. err := db.Table(p.TableName()).Count(&ret).Error
  99. return ret, err
  100. }
  101. func (p *TProvider) Del(db *gorm.DB, where map[string]interface{}) error {
  102. cond, val, err := whereBuild(where)
  103. if err != nil {
  104. return err
  105. }
  106. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  107. }
  108. func PublicityList(db *gorm.DB, page int32) ([]TProvider, int64, error) {
  109. 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 limit %d offset %d", PageSize, (int(page) - 1) *PageSize)
  110. 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 ")
  111. type CountResult struct {
  112. Count int64
  113. }
  114. array := []CountResult{}
  115. err := db.Raw(countSql).Scan(&array).Error
  116. if err != nil {
  117. return nil, 0, err
  118. }
  119. total := array[0].Count
  120. if total == 0 {
  121. return nil, 0, nil
  122. }
  123. list := []TProvider{}
  124. err = db.Raw(sql).Scan(&list).Error
  125. if err != nil {
  126. return nil, 0, err
  127. }
  128. return list, total, nil
  129. }