wx_merchant.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 TWxMerchantInfo struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. Cid int64 `gorm:"column:cid" json:"cid"`
  15. MerchantName string `gorm:"column:merchant_name" json:"merchant_name"`
  16. SubjectInfo string `gorm:"column:subject_info" json:"subject_info"`
  17. BusinessInfo string `gorm:"column:business_info" json:"business_info"`
  18. BankAccountInfo string `gorm:"column:bank_account_info" json:"bank_account_info"`
  19. State string `gorm:"column:state" json:"state"`
  20. StateMsg string `gorm:"column:state_msg" json:"state_msg"`
  21. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  22. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  23. Reason string `gorm:"column:reason" json:"reason"`
  24. ContactInfo string `gorm:"column:contact_info" json:"contact_info"`
  25. ApplyId string `gorm:"column:apply_id" json:"apply_id"`
  26. SignUrl string `gorm:"column:sign_url" json:"sign_url"`
  27. BusinessCode string `gorm:"column:business_code" json:"business_code"`
  28. MchId string `gorm:"column:mch_id" json:"mch_id"`
  29. }
  30. func (p *TWxMerchantInfo) TableName() string {
  31. return "t_wx_merchant_info"
  32. }
  33. func (p *TWxMerchantInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  34. err := db.Table(p.TableName()).Where(where).Find(p).Error
  35. if err != nil {
  36. fields, _ := util.MarshalToString(where)
  37. logger.Error("mysql",
  38. zap.String("sql", "select from "+p.TableName()),
  39. zap.String("fields", fields),
  40. zap.String("error", err.Error()))
  41. }
  42. return err
  43. }
  44. func (p *TWxMerchantInfo) Last(db *gorm.DB) error {
  45. err := db.Table(p.TableName()).Last(p).Error
  46. if err != nil {
  47. logger.Error("mysql",
  48. zap.String("sql", "select last from "+p.TableName()),
  49. zap.String("fields", ""),
  50. zap.String("error", err.Error()))
  51. }
  52. return err
  53. }
  54. // Insert 插入一条记录
  55. func (p *TWxMerchantInfo) Insert(db *gorm.DB) error {
  56. err := db.Create(p).Error
  57. if err != nil {
  58. fields, _ := util.MarshalToString(*p)
  59. logger.Error("mysql",
  60. zap.String("sql", "insert into "+p.TableName()),
  61. zap.String("fields", fields),
  62. zap.String("error", err.Error()))
  63. }
  64. return err
  65. }
  66. func (p *TWxMerchantInfo) Delete(db *gorm.DB, filter map[string]interface{}) error {
  67. err := db.Where(filter).Delete(p).Error
  68. if err != nil {
  69. fields, _ := util.MarshalToString(filter)
  70. logger.Error("mysql",
  71. zap.String("sql", "delete from "+p.TableName()),
  72. zap.String("where", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. func (p *TWxMerchantInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  78. cond, val, err := whereBuild(where)
  79. if err != nil {
  80. if err != nil {
  81. fields, _ := util.MarshalToString(values)
  82. logger.Error("mysql",
  83. zap.String("sql", "update "+p.TableName()),
  84. zap.String("fields", fields),
  85. zap.String("error", err.Error()))
  86. }
  87. return err
  88. }
  89. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  90. }
  91. func (p *TWxMerchantInfo) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  92. cond, val, err := whereBuildAndOr(where, or)
  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. if err != nil {
  99. fields, _ := util.MarshalToString(where)
  100. logger.Error("mysql",
  101. zap.String("sql", "select count "+p.TableName()),
  102. zap.String("fields", fields),
  103. zap.String("error", err.Error()))
  104. }
  105. return ret, err
  106. }
  107. func (p *TWxMerchantInfo) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TWxMerchantInfo, err error) {
  108. cond, val, err := whereBuildAndOr(where, or)
  109. if err != nil {
  110. return list, err
  111. }
  112. if pageSize < 0 {
  113. result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").Find(&list)
  114. if result.Error != nil {
  115. wherefields, _ := util.MarshalToString(where)
  116. logger.Error("mysql",
  117. zap.String("sql", "select * from "+p.TableName()),
  118. zap.String("where", wherefields),
  119. zap.String("error", result.Error.Error()))
  120. }
  121. return list, result.Error
  122. }
  123. offset := (page - 1) * pageSize
  124. result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").Limit(pageSize).Offset(offset).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. func (p *TWxMerchantInfo) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TWxMerchantInfo, err error) {
  135. cond, val, err := whereBuildAndOr(where, or)
  136. if err != nil {
  137. return list, err
  138. }
  139. offset := (page - 1) * pageSize
  140. sql := fmt.Sprintf("select * from %s t1 join (select id from %s order by created_at limit %d offset %d ) t2 on t1.id=t2.id", p.TableName(), p.TableName(), pageSize, offset)
  141. if cond != "" {
  142. sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s order by created_at limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), cond, pageSize, offset)
  143. }
  144. result := db.Raw(sql, val...).Scan(&list)
  145. if result.Error != nil {
  146. wherefields, _ := util.MarshalToString(where)
  147. logger.Error("mysql",
  148. zap.String("sql", "select * from "+p.TableName()),
  149. zap.String("where", wherefields),
  150. zap.String("error", result.Error.Error()))
  151. }
  152. return list, result.Error
  153. }