user.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. type TCompany struct {
  9. Id int64 `gorm:"column:ID;primary_key" json:"id" form:"id"`
  10. Name string `gorm:"column:Name" json:"name" form:"name"`
  11. SocialCode string `gorm:"column:SocialCode" json:"social_code" form:"social_code"`
  12. LegalPerson string `gorm:"column:LegalPerson" json:"legal_person" form:"legal_person"`
  13. IdCert string `gorm:"column:IdCert" json:"id_cert" form:"id_cert"`
  14. BusinessLicense string `gorm:"column:BusinessLicense" json:"business_license" form:"business_license"`
  15. UserName string `gorm:"column:UserName" json:"user_name" form:"user_name"`
  16. Passwd string `gorm:"column:Passwd" json:"passwd" form:"passwd"`
  17. BusinessContact string `gorm:"column:BusinessContact" json:"business_contact" form:"business_contact"`
  18. BusinessContactPhone string `gorm:"column:BusinessContactPhone" json:"business_contact_phone" form:"business_contact_phone"`
  19. BusinessResponsible string `gorm:"column:BusinessResponsible" json:"business_responsible" form:"business_responsible"`
  20. BusinessResponsiblePhone string `gorm:"column:BusinessResponsiblePhone" json:"business_responsible_phone" form:"business_responsible_phone"`
  21. EmergencyContact string `gorm:"column:EmergencyContact" json:"emergency_contact" form:"emergency_contact"`
  22. EmergencyContactPhone string `gorm:"column:EmergencyContactPhone" json:"emergency_contact_phone" form:"emergency_contact_phone"`
  23. Province string `gorm:"column:Province" json:"province" form:"province"`
  24. City string `gorm:"column:City" json:"city" form:"city"`
  25. Zone string `gorm:"column:Zone" json:"zone" form:"zone"`
  26. Status int64 `gorm:"column:Status" json:"status" form:"status"`
  27. Feedback string `gorm:"column:Feedback"`
  28. ApproveTime time.Time `gorm:"column:ApproveTime"`
  29. Email string `gorm:"column:Email"`
  30. CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at" form:"created_at"`
  31. UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at" form:"updated_at"`
  32. Reviewer string `gorm:"column:Reviewer"`
  33. }
  34. var PageSize = 10
  35. func (TCompany) TableName() string {
  36. return "Company"
  37. }
  38. func (p *TCompany) Insert(db *gorm.DB) error {
  39. return db.Table(p.TableName()).Create(p).Error
  40. }
  41. func (p *TCompany) Find(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error {
  42. cond, val, err := whereBuildAndOr(where, or)
  43. if err != nil {
  44. return err
  45. }
  46. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  47. }
  48. func (p *TCompany) 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 *TCompany) 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 := []TCompany{}
  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 *TCompany) List(db *gorm.DB, where map[string]interface{}, page int) (list []TCompany, err error) {
  71. offset := (page - 1) *PageSize
  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(offset).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 *TCompany) All(db *gorm.DB) (list []TCompany, err error) {
  84. result := db.Table(p.TableName()).Find(&list)
  85. return list, result.Error
  86. }
  87. func (p *TCompany) 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 *TCompany) 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. }