package adminser import ( "time" "91porn-server/common/pageopt" "91porn-server/models/v/adminmod" "91porn-server/models/v/authoritymod" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) type M = bson.M // Record Record type Record struct { ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"` Name *string `json:"name,omitempty" bson:"name,omitempty"` //账号 Password *string `json:"password,omitempty" bson:"password,omitempty"` //密码 Nickname *string `json:"nickname,omitempty" bson:"nickname,omitempty"` //昵称 Email *string `json:"email,omitempty" bson:"email,omitempty"` //邮箱 PrivilegeID *primitive.ObjectID `json:"privilegeID,omitempty" bson:"privilegeID,omitempty"` //权限ID Role *string `json:"role,omitempty" bson:"role,omitempty"` //角色 HasLocked *bool `json:"hasLocked,omitempty" bson:"hasLocked,omitempty"` //已禁止登陆 LockReason *string `json:"lockReason,omitempty" bson:"lockReason,omitempty"` //禁止登陆的原因 CreatedAt *time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"` LastLoginAt *time.Time `json:"lastLoginAt,omitempty" bson:"lastLoginAt,omitempty"` } type Page struct { Total int64 `json:"total" bson:"total"` List []Record `json:"list" bson:"list"` } // NameMatch type NameMatch struct { Name *string } func (n *NameMatch) New() pageopt.Matcher { return pageopt.NewAssignMatch("name", n.Name) } // NotSuperAdminMatch type NotSuperAdminMatch struct { } func (n *NotSuperAdminMatch) New() pageopt.Matcher { return pageopt.NewAssignMatch("name", M{"$ne": "superAdmin"}) } // AdminPages AdminPages func AdminPages(skip, limit int64, name *string) (Page, error) { matList := []pageopt.Matcher{(&NotSuperAdminMatch{}).New()} if name != nil { matList = []pageopt.Matcher{(&NameMatch{name}).New()} } adminList, err := adminmod.List(skip, limit, matList...) if err != nil { return Page{}, err } roles := make([]string, len(adminList)) for i, admin := range adminList { roles[i] = admin.Role } roleIDMap, err := authoritymod.IDMapByRoles(roles) if err != nil { return Page{}, err } recordList := make([]Record, len(adminList)) for i, admin := range adminList { _admin := admin privilegeID := roleIDMap[_admin.Role] recordList[i] = Record{ ID: _admin.ID, Name: &_admin.Name, Password: &_admin.Password, Nickname: &_admin.Nickname, Email: &_admin.Email, PrivilegeID: &privilegeID, Role: &_admin.Role, HasLocked: &_admin.HasLocked, LockReason: &_admin.LockReason, CreatedAt: &_admin.CreatedAt, LastLoginAt: &_admin.LastLoginAt, } } count, err := adminmod.Count(matList...) if err != nil { return Page{}, err } return Page{ Total: count, List: recordList, }, nil }