user.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Table(p.TableName()).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. cond, val, err := whereBuild(filter)
  60. if err != nil {
  61. return err
  62. }
  63. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  64. if err != nil {
  65. fields, _ := util.MarshalToString(filter)
  66. logger.Error("mysql",
  67. zap.String("sql", "delete from "+p.TableName()),
  68. zap.String("where", fields),
  69. zap.String("error", err.Error()))
  70. }
  71. return err
  72. }
  73. func (p *TUser) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  74. cond, val, err := whereBuild(where)
  75. if err != nil {
  76. if err != nil {
  77. fields, _ := util.MarshalToString(values)
  78. logger.Error("mysql",
  79. zap.String("sql", "update "+p.TableName()),
  80. zap.String("fields", fields),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  86. }
  87. func (p *TUser) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  88. cond, val, err := whereBuildAndOr(where, or)
  89. if err != nil {
  90. return 0, err
  91. }
  92. ret := int64(0)
  93. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  94. if err != nil {
  95. fields, _ := util.MarshalToString(where)
  96. logger.Error("mysql",
  97. zap.String("sql", "select count "+p.TableName()),
  98. zap.String("fields", fields),
  99. zap.String("error", err.Error()))
  100. }
  101. return ret, err
  102. }
  103. func (p *TUser) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUser, err error) {
  104. cond, val, err := whereBuildAndOr(where, or)
  105. if err != nil {
  106. return list, err
  107. }
  108. if pageSize < 0 {
  109. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  110. if result.Error != nil {
  111. wherefields, _ := util.MarshalToString(where)
  112. logger.Error("mysql",
  113. zap.String("sql", "select * from "+p.TableName()),
  114. zap.String("where", wherefields),
  115. zap.String("error", result.Error.Error()))
  116. }
  117. return list, result.Error
  118. }
  119. offset := (page - 1) * pageSize
  120. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  121. if result.Error != nil {
  122. wherefields, _ := util.MarshalToString(where)
  123. logger.Error("mysql",
  124. zap.String("sql", "select * from "+p.TableName()),
  125. zap.String("where", wherefields),
  126. zap.String("error", result.Error.Error()))
  127. }
  128. return list, result.Error
  129. }