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