12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "time"
- )
- type TGroupUser struct {
- ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
- PermissionCodes string `gorm:"column:permission_codes" json:"permission_codes"`
- GroupName string `gorm:"column:group_name" json:"group_name"`
- GroupDesc string `gorm:"column:group_desc" json:"group_desc"`
- Cid int64 `gorm:"column:cid" json:"cid"`
- GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
- IsSuper int64 `gorm:"column:is_super" json:"is_super"`
- UserId int64 `gorm:"column:user_id" json:"user_id"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- }
- func (p *TGroupUser) TableName() string {
- return "t_group_user"
- }
- type GroupItem struct {
- TGroupUser
- UserCount int64 `gorm:"column:user_count"`
- }
- func (p *TGroupUser) List(db *gorm.DB, where map[string]interface{}) (list []GroupItem, err error) {
- cond, val, err := whereBuildAndOr(where, nil)
- if err != nil {
- return list, err
- }
- result := db.Table(p.TableName()).Select([]string{"*, sum(if (user_id > 0,1,0)) as user_count"}).Where(cond, val...).Group("id").Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
|