company.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "fmt"
  8. )
  9. type TCompany 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. BusinessLicense string `gorm:"column:business_license" json:"business_license" form:"business_license"`
  16. UserName string `gorm:"column:user_name" json:"user_name" form:"user_name"`
  17. Passwd string `gorm:"column:passwd" json:"passwd" form:"passwd"`
  18. BusinessContact string `gorm:"column:business_contact" json:"business_contact" form:"business_contact"`
  19. BusinessContactPhone string `gorm:"column:business_contact_phone" json:"business_contact_phone" form:"business_contact_phone"`
  20. BusinessResponsible string `gorm:"column:business_responsible" json:"business_responsible" form:"business_responsible"`
  21. BusinessResponsiblePhone string `gorm:"column:business_responsible_phone" json:"business_responsible_phone" form:"business_responsible_phone"`
  22. EmergencyContact string `gorm:"column:emergency_contact" json:"emergency_contact" form:"emergency_contact"`
  23. EmergencyContactPhone string `gorm:"column:emergency_contact_phone" json:"emergency_contact_phone" form:"emergency_contact_phone"`
  24. Province string `gorm:"column:province" json:"province" form:"province"`
  25. City string `gorm:"column:city" json:"city" form:"city"`
  26. Zone string `gorm:"column:zone" json:"zone" form:"zone"`
  27. ApproveTime time.Time
  28. Feedback string
  29. Status int32 `gorm:"column:status" json:"status" form:"status"`
  30. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  31. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  32. }
  33. func (TCompany) TableName() string {
  34. return "t_company"
  35. }
  36. func (p *TCompany) Insert(db *gorm.DB) error {
  37. return db.Create(p).Error
  38. }
  39. func (p *TCompany) 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 *TCompany) 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 *TCompany) 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 := []TCompany{}
  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 *TCompany) Save(db *gorm.DB) error {
  69. return db.Save(p).Error
  70. }
  71. func (p *TCompany) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32) (list []TCompany, err error) {
  72. offset := (page - 1) *PageSize
  73. if len(where) > 0 || len(or) > 0{
  74. cond, val, err := whereBuildAndOr(where, or)
  75. if err != nil {
  76. return list, err
  77. }
  78. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(PageSize).Offset(offset).Find(&list)
  79. return list, result.Error
  80. }
  81. result := db.Table(p.TableName()).Order("created_at desc").Limit(10).Offset(offset).Find(&list)
  82. return list, result.Error
  83. }
  84. func (p *TCompany) All(db *gorm.DB) (list []TCompany, err error) {
  85. result := db.Table(p.TableName()).Find(&list)
  86. return list, result.Error
  87. }
  88. func (p *TCompany) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  89. if len(where) > 0 || len(or) > 0{
  90. cond, val, err := whereBuildAndOr(where, or)
  91. if err != nil {
  92. return 0, err
  93. }
  94. ret := int64(0)
  95. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  96. return ret, err
  97. }
  98. ret := int64(0)
  99. err := db.Table(p.TableName()).Count(&ret).Error
  100. return ret, err
  101. }
  102. func (p *TCompany) Del(db *gorm.DB, where map[string]interface{}) error {
  103. cond, val, err := whereBuild(where)
  104. if err != nil {
  105. return err
  106. }
  107. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  108. }
  109. type CompanyStatistcData struct {
  110. Total int64
  111. Increase int64
  112. }
  113. func CompanyStatistic(db *gorm.DB) (CompanyStatistcData, error) {
  114. ret := CompanyStatistcData{}
  115. start := GetIncreaseStart(true)
  116. sql := fmt.Sprintf("select count(1) as count, sum(created_at >= '%s') as increase from t_company where status = 1", start)
  117. array := []CompanyStatistcData{}
  118. err := db.Raw(sql).Scan(&array).Error
  119. if err != nil {
  120. return ret, err
  121. }
  122. if len(array) > 0 {
  123. return array[0], nil
  124. }
  125. return ret, nil
  126. }