group_user.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 TGroupUser struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. PermissionCodes string `gorm:"column:permission_codes" json:"permission_codes"`
  14. GroupName string `gorm:"column:group_name" json:"group_name"`
  15. GroupDesc string `gorm:"column:group_desc" json:"group_desc"`
  16. Cid int64 `gorm:"column:cid" json:"cid"`
  17. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  18. IsSuper int64 `gorm:"column:is_super" json:"is_super"`
  19. UserId int64 `gorm:"column:user_id" json:"user_id"`
  20. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  21. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  22. }
  23. func (p *TGroupUser) TableName() string {
  24. return "t_group_user"
  25. }
  26. type GroupItem struct {
  27. TGroupUser
  28. UserCount int64 `gorm:"column:user_count"`
  29. }
  30. func (p *TGroupUser) List(db *gorm.DB, where map[string]interface{}) (list []GroupItem, err error) {
  31. cond, val, err := whereBuildAndOr(where, nil)
  32. if err != nil {
  33. return list, err
  34. }
  35. result := db.Table(p.TableName()).Select([]string{"*, sum(if (user_id > 0,1,0)) as user_count"}).Where(cond, val...).Group("id").Find(&list)
  36. if result.Error != nil {
  37. wherefields, _ := util.MarshalToString(where)
  38. logger.Error("mysql",
  39. zap.String("sql", "select * from "+p.TableName()),
  40. zap.String("where", wherefields),
  41. zap.String("error", result.Error.Error()))
  42. }
  43. return list, result.Error
  44. }