user.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. User string `gorm:"column:user" json:"user"`
  14. Password string `gorm:"column:password" json:"password"`
  15. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  16. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  17. }
  18. func (p *TUser) TableName() string {
  19. return "t_user"
  20. }
  21. func (p *TUser) Find(db *gorm.DB, where map[string]interface{}) error {
  22. err := db.Table(p.TableName()).Where(where).Find(p).Error
  23. if err != nil {
  24. fields, _ := util.MarshalToString(where)
  25. logger.Error("mysql",
  26. zap.String("sql", "select from "+p.TableName()),
  27. zap.String("fields", fields),
  28. zap.String("error", err.Error()))
  29. }
  30. return err
  31. }
  32. func (p *TUser) Last(db *gorm.DB) error {
  33. err := db.Table(p.TableName()).Last(p).Error
  34. if err != nil {
  35. logger.Error("mysql",
  36. zap.String("sql", "select last from "+p.TableName()),
  37. zap.String("fields", ""),
  38. zap.String("error", err.Error()))
  39. }
  40. return err
  41. }
  42. // Insert 插入一条记录
  43. func (p *TUser) Insert(db *gorm.DB) error {
  44. err := db.Create(p).Error
  45. if err != nil {
  46. fields, _ := util.MarshalToString(*p)
  47. logger.Error("mysql",
  48. zap.String("sql", "insert into "+p.TableName()),
  49. zap.String("fields", fields),
  50. zap.String("error", err.Error()))
  51. }
  52. return err
  53. }
  54. func (p *TUser) Delete(db *gorm.DB, filter map[string]interface{}) error {
  55. err := db.Where(filter).Delete(p).Error
  56. if err != nil {
  57. fields, _ := util.MarshalToString(filter)
  58. logger.Error("mysql",
  59. zap.String("sql", "delete from "+p.TableName()),
  60. zap.String("where", fields),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. func (p *TUser) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  66. cond, val, err := whereBuild(where)
  67. if err != nil {
  68. if err != nil {
  69. fields, _ := util.MarshalToString(values)
  70. logger.Error("mysql",
  71. zap.String("sql", "update "+p.TableName()),
  72. zap.String("fields", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  78. }
  79. func (p *TUser) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  80. cond, val, err := whereBuildAndOr(where, or)
  81. if err != nil {
  82. return 0, err
  83. }
  84. ret := int64(0)
  85. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  86. if err != nil {
  87. fields, _ := util.MarshalToString(where)
  88. logger.Error("mysql",
  89. zap.String("sql", "select count "+p.TableName()),
  90. zap.String("fields", fields),
  91. zap.String("error", err.Error()))
  92. }
  93. return ret, err
  94. }
  95. func (p *TUser) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUser, err error) {
  96. cond, val, err := whereBuildAndOr(where, or)
  97. if err != nil {
  98. return list, err
  99. }
  100. if pageSize < 0 {
  101. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  102. if result.Error != nil {
  103. wherefields, _ := util.MarshalToString(where)
  104. logger.Error("mysql",
  105. zap.String("sql", "select * from "+p.TableName()),
  106. zap.String("where", wherefields),
  107. zap.String("error", result.Error.Error()))
  108. }
  109. return list, result.Error
  110. }
  111. offset := (page - 1) * pageSize
  112. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  113. if result.Error != nil {
  114. wherefields, _ := util.MarshalToString(where)
  115. logger.Error("mysql",
  116. zap.String("sql", "select * from "+p.TableName()),
  117. zap.String("where", wherefields),
  118. zap.String("error", result.Error.Error()))
  119. }
  120. return list, result.Error
  121. }