t_user_wx_public.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package model
  2. import (
  3. "git.getensh.com/common/gopkgs/logger"
  4. "git.getensh.com/common/gopkgs/util"
  5. "go.uber.org/zap"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. type TUserWxPublic struct {
  10. ID int64 `gorm:"column:id" json:"id"`
  11. OpenId string `gorm:"column:open_id" json:"open_id"`
  12. UnionId string `gorm:"column:union_id" json:"union_id"`
  13. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  14. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  15. }
  16. func (p *TUserWxPublic) TableName() string {
  17. return "t_user_wx_public"
  18. }
  19. func (p *TUserWxPublic) Find(db *gorm.DB, where map[string]interface{}) error {
  20. err := db.Table(p.TableName()).Where(where).First(p).Error
  21. if err != nil {
  22. fields, _ := util.MarshalToString(where)
  23. logger.Error("mysql",
  24. zap.String("sql", "select from "+p.TableName()),
  25. zap.String("fields", fields),
  26. zap.String("error", err.Error()))
  27. }
  28. return err
  29. }
  30. func (p *TUserWxPublic) Last(db *gorm.DB) error {
  31. err := db.Table(p.TableName()).Last(p).Error
  32. if err != nil {
  33. logger.Error("mysql",
  34. zap.String("sql", "select last from "+p.TableName()),
  35. zap.String("fields", ""),
  36. zap.String("error", err.Error()))
  37. }
  38. return err
  39. }
  40. // Insert 插入一条记录
  41. func (p *TUserWxPublic) Insert(db *gorm.DB) error {
  42. err := db.Create(p).Error
  43. if err != nil {
  44. fields, _ := util.MarshalToString(*p)
  45. logger.Error("mysql",
  46. zap.String("sql", "insert into "+p.TableName()),
  47. zap.String("fields", fields),
  48. zap.String("error", err.Error()))
  49. }
  50. return err
  51. }
  52. func (p *TUserWxPublic) Delete(db *gorm.DB, filter map[string]interface{}) error {
  53. cond, val, err := whereBuild(filter)
  54. if err != nil {
  55. return err
  56. }
  57. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  58. }
  59. func (p *TUserWxPublic) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  60. cond, val, err := whereBuild(where)
  61. if err != nil {
  62. if err != nil {
  63. fields, _ := util.MarshalToString(values)
  64. logger.Error("mysql",
  65. zap.String("sql", "update "+p.TableName()),
  66. zap.String("fields", fields),
  67. zap.String("error", err.Error()))
  68. }
  69. return err
  70. }
  71. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  72. }
  73. func (p *TUserWxPublic) UpdateByModel(db *gorm.DB) error {
  74. err := db.Model(p).Updates(p).Error
  75. if err != nil {
  76. fields, _ := util.MarshalToString(*p)
  77. logger.Error("mysql",
  78. zap.String("sql", "update "+p.TableName()),
  79. zap.String("fields", fields),
  80. zap.String("error", err.Error()))
  81. }
  82. return err
  83. }
  84. func (p *TUserWxPublic) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  85. cond, val, err := whereBuildAndOr(where, or)
  86. if err != nil {
  87. return 0, err
  88. }
  89. ret := int64(0)
  90. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  91. if err != nil {
  92. fields, _ := util.MarshalToString(where)
  93. logger.Error("mysql",
  94. zap.String("sql", "select count "+p.TableName()),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return ret, err
  99. }
  100. func (p *TUserWxPublic) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUserWxPublic, err error) {
  101. cond, val, err := whereBuildAndOr(where, or)
  102. if err != nil {
  103. return list, err
  104. }
  105. if pageSize < 0 {
  106. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  107. if result.Error != nil {
  108. wherefields, _ := util.MarshalToString(where)
  109. logger.Error("mysql",
  110. zap.String("sql", "select * from "+p.TableName()),
  111. zap.String("where", wherefields),
  112. zap.String("error", result.Error.Error()))
  113. }
  114. return list, result.Error
  115. }
  116. offset := (page - 1) * pageSize
  117. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  118. if result.Error != nil {
  119. wherefields, _ := util.MarshalToString(where)
  120. logger.Error("mysql",
  121. zap.String("sql", "select * from "+p.TableName()),
  122. zap.String("where", wherefields),
  123. zap.String("error", result.Error.Error()))
  124. }
  125. return list, result.Error
  126. }