company.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TCompany struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. Applicant string `gorm:"column:applicant" json:"applicant"`
  15. ApplicantPhone string `gorm:"column:applicant_phone" json:"applicant_phone"`
  16. CompanyName string `gorm:"column:company_name" json:"company_name"`
  17. CompanyPhone string `gorm:"column:company_phone" json:"company_phone"`
  18. ApplicantDuty string `gorm:"column:applicant_duty" json:"applicant_duty"`
  19. LegalPerson string `gorm:"column:legal_person" json:"legal_person"`
  20. CompanyAddr string `gorm:"column:company_addr" json:"company_addr"`
  21. SocialCode string `gorm:"column:social_code" json:"social_code"`
  22. BusinessLicense string `gorm:"column:business_license" json:"business_license"`
  23. ApplicantIdentification string `gorm:"column:applicant_identification" json:"applicant_identification"`
  24. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  25. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  26. ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
  27. ApproveStatus int64 `gorm:"column:approve_status" json:"approve_status"`
  28. LicenseType int64 `gorm:"column:license_type" json:"license_type"`
  29. FreeGardenCount int64 `gorm:"column:free_garden_count" json:"free_garden_count"`
  30. GardenCount int64 `gorm:"column:garden_count" json:"garden_count"`
  31. User string `gorm:"column:user" json:"user"`
  32. Password string `gorm:"column:password" json:"password"`
  33. Expire int64 `gorm:"column:expire" json:"expire"`
  34. UniqFlag int `gorm:"column:uniq_flag" json:"uniq_flag"`
  35. Feedback string `gorm:"column:feedback" json:"feedback"`
  36. Logo string `gorm:"column:logo" json:"logo"`
  37. Site string `gorm:"column:site" json:"site"`
  38. Desc string `gorm:"column:desc" json:"desc"`
  39. //PayMode int32 `gorm:"column:pay_mode" json:"pay_mode"`
  40. }
  41. func (p *TCompany) TableName() string {
  42. return "t_company"
  43. }
  44. func (p *TCompany) Find(db *gorm.DB, where map[string]interface{}) error {
  45. err := db.Table(p.TableName()).Where(where).Find(p).Error
  46. if err != nil {
  47. fields, _ := util.MarshalToString(where)
  48. logger.Error("mysql",
  49. zap.String("sql", "select from "+p.TableName()),
  50. zap.String("fields", fields),
  51. zap.String("error", err.Error()))
  52. }
  53. return err
  54. }
  55. func (p *TCompany) Last(db *gorm.DB) error {
  56. err := db.Table(p.TableName()).Last(p).Error
  57. if err != nil {
  58. logger.Error("mysql",
  59. zap.String("sql", "select last from "+p.TableName()),
  60. zap.String("fields", ""),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. // Insert 插入一条记录
  66. func (p *TCompany) Insert(db *gorm.DB) error {
  67. err := db.Create(p).Error
  68. if err != nil {
  69. fields, _ := util.MarshalToString(*p)
  70. logger.Error("mysql",
  71. zap.String("sql", "insert into "+p.TableName()),
  72. zap.String("fields", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. func (p *TCompany) Delete(db *gorm.DB, filter map[string]interface{}) error {
  78. err := db.Where(filter).Delete(p).Error
  79. if err != nil {
  80. fields, _ := util.MarshalToString(filter)
  81. logger.Error("mysql",
  82. zap.String("sql", "delete from "+p.TableName()),
  83. zap.String("where", fields),
  84. zap.String("error", err.Error()))
  85. }
  86. return err
  87. }
  88. func (p *TCompany) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  89. cond, val, err := whereBuild(where)
  90. if err != nil {
  91. if err != nil {
  92. fields, _ := util.MarshalToString(values)
  93. logger.Error("mysql",
  94. zap.String("sql", "update "+p.TableName()),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return err
  99. }
  100. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  101. }
  102. func (p *TCompany) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  103. cond, val, err := whereBuildAndOr(where, or)
  104. if err != nil {
  105. return 0, err
  106. }
  107. ret := int64(0)
  108. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  109. if err != nil {
  110. fields, _ := util.MarshalToString(where)
  111. logger.Error("mysql",
  112. zap.String("sql", "select count "+p.TableName()),
  113. zap.String("fields", fields),
  114. zap.String("error", err.Error()))
  115. }
  116. return ret, err
  117. }
  118. func (p *TCompany) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TCompany, err error) {
  119. cond, val, err := whereBuildAndOr(where, or)
  120. if err != nil {
  121. return list, err
  122. }
  123. if pageSize < 0 {
  124. result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").Find(&list)
  125. if result.Error != nil {
  126. wherefields, _ := util.MarshalToString(where)
  127. logger.Error("mysql",
  128. zap.String("sql", "select * from "+p.TableName()),
  129. zap.String("where", wherefields),
  130. zap.String("error", result.Error.Error()))
  131. }
  132. return list, result.Error
  133. }
  134. offset := (page - 1) * pageSize
  135. result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").Limit(pageSize).Offset(offset).Find(&list)
  136. if result.Error != nil {
  137. wherefields, _ := util.MarshalToString(where)
  138. logger.Error("mysql",
  139. zap.String("sql", "select * from "+p.TableName()),
  140. zap.String("where", wherefields),
  141. zap.String("error", result.Error.Error()))
  142. }
  143. return list, result.Error
  144. }
  145. func (p *TCompany) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TCompany, err error) {
  146. cond, val, err := whereBuildAndOr(where, or)
  147. if err != nil {
  148. return list, err
  149. }
  150. offset := (page - 1) * pageSize
  151. sql := fmt.Sprintf("select * from %s t1 join (select id from %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), pageSize, offset)
  152. if cond != "" {
  153. sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), cond, pageSize, offset)
  154. }
  155. result := db.Raw(sql, val...).Scan(&list)
  156. if result.Error != nil {
  157. wherefields, _ := util.MarshalToString(where)
  158. logger.Error("mysql",
  159. zap.String("sql", "select * from "+p.TableName()),
  160. zap.String("where", wherefields),
  161. zap.String("error", result.Error.Error()))
  162. }
  163. return list, result.Error
  164. }