user.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TUser struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. Cid int64 `gorm:"column:cid" json:"cid"`
  14. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  15. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  16. Phone string `gorm:"column:phone" json:"phone"`
  17. User string `gorm:"column:user" json:"user"`
  18. Password string `gorm:"column:password" json:"password"`
  19. Email string `gorm:"column:email" json:"email"`
  20. Super bool `gorm:"column:super" json:"super"`
  21. RealName string `gorm:"column:real_name" json:"real_name"`
  22. }
  23. func (p *TUser) TableName() string {
  24. return "t_user"
  25. }
  26. func (p *TUser) Find(db *gorm.DB, where map[string]interface{}) error {
  27. err := db.Table(p.TableName()).Where(where).Find(p).Error
  28. if err != nil {
  29. fields, _ := util.MarshalToString(where)
  30. logger.Error("mysql",
  31. zap.String("sql", "select from "+p.TableName()),
  32. zap.String("fields", fields),
  33. zap.String("error", err.Error()))
  34. }
  35. return err
  36. }
  37. func (p *TUser) Last(db *gorm.DB) error {
  38. err := db.Table(p.TableName()).Last(p).Error
  39. if err != nil {
  40. logger.Error("mysql",
  41. zap.String("sql", "select last from "+p.TableName()),
  42. zap.String("fields", ""),
  43. zap.String("error", err.Error()))
  44. }
  45. return err
  46. }
  47. // Insert 插入一条记录
  48. func (p *TUser) Insert(db *gorm.DB) error {
  49. err := db.Create(p).Error
  50. if err != nil {
  51. fields, _ := util.MarshalToString(*p)
  52. logger.Error("mysql",
  53. zap.String("sql", "insert into "+p.TableName()),
  54. zap.String("fields", fields),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *TUser) Delete(db *gorm.DB, filter map[string]interface{}) error {
  60. err := db.Where(filter).Delete(p).Error
  61. if err != nil {
  62. fields, _ := util.MarshalToString(filter)
  63. logger.Error("mysql",
  64. zap.String("sql", "delete from "+p.TableName()),
  65. zap.String("where", fields),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  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) 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 *TUser) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUser, 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...).Order("id desc").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...).Order("id desc").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. }