user_group.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 TUserGroup 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. GroupName string `gorm:"column:group_name" json:"group_name"`
  25. GroupDesc string `gorm:"column:group_desc" json:"group_desc"`
  26. IsSuper int64 `gorm:"column:is_super" json:"is_super"`
  27. Gender int32 `gorm:"column:gender" json:"gender"`
  28. PermissionCodes string `gorm:"column:permission_codes" json:"permission_codes"`
  29. BasePermission int32 `gorm:"column:base_permission" json:"base_permission"`
  30. RealName string `gorm:"column:real_name" json:"real_name"`
  31. DepartmentId int64 `gorm:"column:department_id" json:"department_id"`
  32. DepartmentName string `gorm:"column:department_name" json:"department_name"`
  33. OpenimId string `gorm:"column:openim_id" json:"openim_id"`
  34. }
  35. func (p *TUserGroup) TableName() string {
  36. return "t_user_group"
  37. }
  38. func (p *TUserGroup) Find(db *gorm.DB, where map[string]interface{}) error {
  39. err := db.Table(p.TableName()).Where(where).Find(p).Error
  40. if err != nil {
  41. fields, _ := util.MarshalToString(where)
  42. logger.Error("mysql",
  43. zap.String("sql", "select from "+p.TableName()),
  44. zap.String("fields", fields),
  45. zap.String("error", err.Error()))
  46. }
  47. return err
  48. }
  49. func (p *TUserGroup) Last(db *gorm.DB) error {
  50. err := db.Table(p.TableName()).Last(p).Error
  51. if err != nil {
  52. logger.Error("mysql",
  53. zap.String("sql", "select last from "+p.TableName()),
  54. zap.String("fields", ""),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *TUserGroup) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  60. cond, val, err := whereBuildAndOr(where, or)
  61. if err != nil {
  62. return 0, err
  63. }
  64. ret := int64(0)
  65. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  66. if err != nil {
  67. fields, _ := util.MarshalToString(where)
  68. logger.Error("mysql",
  69. zap.String("sql", "select count "+p.TableName()),
  70. zap.String("fields", fields),
  71. zap.String("error", err.Error()))
  72. }
  73. return ret, err
  74. }
  75. func (p *TUserGroup) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUserGroup, err error) {
  76. cond, val, err := whereBuildAndOr(where, or)
  77. if err != nil {
  78. return list, err
  79. }
  80. if pageSize < 0 {
  81. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  82. if result.Error != nil {
  83. wherefields, _ := util.MarshalToString(where)
  84. logger.Error("mysql",
  85. zap.String("sql", "select * from "+p.TableName()),
  86. zap.String("where", wherefields),
  87. zap.String("error", result.Error.Error()))
  88. }
  89. return list, result.Error
  90. }
  91. offset := (page - 1) * pageSize
  92. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  93. if result.Error != nil {
  94. wherefields, _ := util.MarshalToString(where)
  95. logger.Error("mysql",
  96. zap.String("sql", "select * from "+p.TableName()),
  97. zap.String("where", wherefields),
  98. zap.String("error", result.Error.Error()))
  99. }
  100. return list, result.Error
  101. }