user.go 4.1 KB

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