user.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  15. Phone string `gorm:"column:phone" json:"phone"`
  16. Email string `gorm:"column:email" json:"email"`
  17. User string `gorm:"column:user" json:"user"`
  18. Password string `gorm:"column:password" json:"password"`
  19. //Enable int64 `gorm:"column:enable" json:"enable"`
  20. GardenName string `gorm:"column:garden_name" json:"garden_name"`
  21. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  22. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  23. GroupId int64 `gorm:"column:group_id" json:"group_id"`
  24. BasePermission int32 `gorm:"column:base_permission" json:"base_permission"`
  25. DepartmentId int64 `gorm:"column:department_id" json:"department_id"`
  26. Gender int32 `gorm:"column:gender" json:"gender"`
  27. RealName string `gorm:"column:real_name" json:"real_name"`
  28. OpenimId string `gorm:"column:openim_id" json:"openim_id"`
  29. }
  30. func (p *TUser) TableName() string {
  31. return "t_user"
  32. }
  33. func (p *TUser) Find(db *gorm.DB, where map[string]interface{}) error {
  34. err := db.Table(p.TableName()).Where(where).Find(p).Error
  35. if err != nil {
  36. fields, _ := util.MarshalToString(where)
  37. logger.Error("mysql",
  38. zap.String("sql", "select from "+p.TableName()),
  39. zap.String("fields", fields),
  40. zap.String("error", err.Error()))
  41. }
  42. return err
  43. }
  44. func (p *TUser) Last(db *gorm.DB) error {
  45. err := db.Table(p.TableName()).Last(p).Error
  46. if err != nil {
  47. logger.Error("mysql",
  48. zap.String("sql", "select last from "+p.TableName()),
  49. zap.String("fields", ""),
  50. zap.String("error", err.Error()))
  51. }
  52. return err
  53. }
  54. // Insert 插入一条记录
  55. func (p *TUser) Insert(db *gorm.DB) error {
  56. err := db.Create(p).Error
  57. if err != nil {
  58. fields, _ := util.MarshalToString(*p)
  59. logger.Error("mysql",
  60. zap.String("sql", "insert into "+p.TableName()),
  61. zap.String("fields", fields),
  62. zap.String("error", err.Error()))
  63. }
  64. return err
  65. }
  66. func (p *TUser) Delete(db *gorm.DB, filter map[string]interface{}) error {
  67. cond, val, err := whereBuild(filter)
  68. if err != nil {
  69. return err
  70. }
  71. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  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) UpdateByModel(db *gorm.DB) error {
  88. err := db.Model(p).Updates(p).Error
  89. if err != nil {
  90. fields, _ := util.MarshalToString(*p)
  91. logger.Error("mysql",
  92. zap.String("sql", "update "+p.TableName()),
  93. zap.String("fields", fields),
  94. zap.String("error", err.Error()))
  95. }
  96. return err
  97. }
  98. func (p *TUser) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  99. cond, val, err := whereBuildAndOr(where, or)
  100. if err != nil {
  101. return 0, err
  102. }
  103. ret := int64(0)
  104. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  105. if err != nil {
  106. fields, _ := util.MarshalToString(where)
  107. logger.Error("mysql",
  108. zap.String("sql", "select count "+p.TableName()),
  109. zap.String("fields", fields),
  110. zap.String("error", err.Error()))
  111. }
  112. return ret, err
  113. }
  114. func (p *TUser) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUser, err error) {
  115. cond, val, err := whereBuildAndOr(where, or)
  116. if err != nil {
  117. return list, err
  118. }
  119. if pageSize < 0 {
  120. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").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. }
  130. offset := (page - 1) * pageSize
  131. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  132. if result.Error != nil {
  133. wherefields, _ := util.MarshalToString(where)
  134. logger.Error("mysql",
  135. zap.String("sql", "select * from "+p.TableName()),
  136. zap.String("where", wherefields),
  137. zap.String("error", result.Error.Error()))
  138. }
  139. return list, result.Error
  140. }