user.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package model
  2. import (
  3. "fmt"
  4. "git.getensh.com/common/gopkgs/logger"
  5. "git.getensh.com/common/gopkgs/util"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type TUser struct {
  11. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  12. Phone string `gorm:"column:phone" json:"phone"`
  13. OpenId string `gorm:"column:open_id" json:"open_id"`
  14. UnionId string `gorm:"column:union_id" json:"union_id"`
  15. NickName string `gorm:"column:nick_name" json:"nick_name"`
  16. RealName string `gorm:"column:real_name" json:"real_name"`
  17. IdNumber string `gorm:"column:id_number" json:"id_number"`
  18. IdType int32 `gorm:"column:id_type" json:"id_type"`
  19. Avatar string `gorm:"column:avatar" json:"avatar"`
  20. Password string `gorm:"column:password" json:"password"`
  21. PublicOpenId string `gorm:"column:public_open_id" json:"public_open_id"`
  22. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  23. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  24. OpenimId string `gorm:"column:openim_id" json:"openim_id"`
  25. Gender int32 `gorm:"column:gender" json:"gender"`
  26. }
  27. func (p *TUser) TableName() string {
  28. return "t_user"
  29. }
  30. func (p *TUser) Find(db *gorm.DB, where map[string]interface{}) error {
  31. err := db.Table(p.TableName()).Where(where).Find(p).Error
  32. if err != nil {
  33. fields, _ := util.MarshalToString(where)
  34. logger.Error("mysql",
  35. zap.String("sql", "select from "+p.TableName()),
  36. zap.String("fields", fields),
  37. zap.String("error", err.Error()))
  38. }
  39. return err
  40. }
  41. func (p *TUser) Last(db *gorm.DB) error {
  42. err := db.Table(p.TableName()).Last(p).Error
  43. if err != nil {
  44. logger.Error("mysql",
  45. zap.String("sql", "select last from "+p.TableName()),
  46. zap.String("fields", ""),
  47. zap.String("error", err.Error()))
  48. }
  49. return err
  50. }
  51. // Insert 插入一条记录
  52. func (p *TUser) Insert(db *gorm.DB) error {
  53. err := db.Create(p).Error
  54. if err != nil {
  55. fields, _ := util.MarshalToString(*p)
  56. logger.Error("mysql",
  57. zap.String("sql", "insert into "+p.TableName()),
  58. zap.String("fields", fields),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *TUser) Delete(db *gorm.DB, filter map[string]interface{}) error {
  64. cond, val, err := whereBuild(filter)
  65. if err != nil {
  66. return err
  67. }
  68. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  69. }
  70. func (p *TUser) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  71. cond, val, err := whereBuild(where)
  72. if err != nil {
  73. if err != nil {
  74. fields, _ := util.MarshalToString(values)
  75. logger.Error("mysql",
  76. zap.String("sql", "update "+p.TableName()),
  77. zap.String("fields", fields),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  83. }
  84. func (p *TUser) UpdateByModel(db *gorm.DB) error {
  85. err := db.Model(p).Updates(p).Error
  86. if err != nil {
  87. fields, _ := util.MarshalToString(*p)
  88. logger.Error("mysql",
  89. zap.String("sql", "update "+p.TableName()),
  90. zap.String("fields", fields),
  91. zap.String("error", err.Error()))
  92. }
  93. return err
  94. }
  95. func (p *TUser) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  96. cond, val, err := whereBuildAndOr(where, or)
  97. if err != nil {
  98. return 0, err
  99. }
  100. ret := int64(0)
  101. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  102. if err != nil {
  103. fields, _ := util.MarshalToString(where)
  104. logger.Error("mysql",
  105. zap.String("sql", "select count "+p.TableName()),
  106. zap.String("fields", fields),
  107. zap.String("error", err.Error()))
  108. }
  109. return ret, err
  110. }
  111. func (p *TUser) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUser, err error) {
  112. cond, val, err := whereBuildAndOr(where, or)
  113. if err != nil {
  114. return list, err
  115. }
  116. if pageSize < 0 {
  117. result := db.Table(p.TableName()).Where(cond, val...).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. }
  127. offset := (page - 1) * pageSize
  128. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  129. if result.Error != nil {
  130. wherefields, _ := util.MarshalToString(where)
  131. logger.Error("mysql",
  132. zap.String("sql", "select * from "+p.TableName()),
  133. zap.String("where", wherefields),
  134. zap.String("error", result.Error.Error()))
  135. }
  136. return list, result.Error
  137. }
  138. func (p *TUser) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUser, err error) {
  139. cond, val, err := whereBuildAndOr(where, or)
  140. if err != nil {
  141. return list, err
  142. }
  143. offset := (page - 1) * pageSize
  144. 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)
  145. if cond != "" {
  146. 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)
  147. }
  148. result := db.Raw(sql, val...).Scan(&list)
  149. if result.Error != nil {
  150. wherefields, _ := util.MarshalToString(where)
  151. logger.Error("mysql",
  152. zap.String("sql", "select * from "+p.TableName()),
  153. zap.String("where", wherefields),
  154. zap.String("error", result.Error.Error()))
  155. }
  156. return list, result.Error
  157. }