|
@@ -277,115 +277,26 @@ func (a *Admin) UpdateStatus(ctx context.Context, username string, status int) *
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// Find 获取管理员信息
|
|
|
-func (a *Admin) Find(ctx context.Context, req entity.AdminFindReq) (*entity.AdminListResp, *code.Result) {
|
|
|
+// FindAll 查找所有管理员信息
|
|
|
+func (a *Admin) FindAll(ctx context.Context, page int, pageSize int, username string) (*entity.AdminListResp, *code.Result) {
|
|
|
// 日志记录
|
|
|
- mhayaLogger.Warnf("Find req: %#v", req)
|
|
|
+ mhayaLogger.Warnf("Finding admins with page %d and page size %d, username: %s", page, pageSize, maskUsername(username))
|
|
|
|
|
|
// 验证参数
|
|
|
- page := req.Page
|
|
|
- if req.Page <= 0 {
|
|
|
+ if page <= 0 {
|
|
|
page = 1
|
|
|
}
|
|
|
- pageSize := req.Size
|
|
|
- if req.Size <= 0 {
|
|
|
+ if pageSize <= 0 {
|
|
|
pageSize = 10
|
|
|
}
|
|
|
|
|
|
// 构建查询条件
|
|
|
filter := bson.M{}
|
|
|
- if req.Username != "" {
|
|
|
- filter["username"] = bson.M{"$regex": escapeRegex(req.Username), "$options": "i"}
|
|
|
+ if username != "" {
|
|
|
+ filter["username"] = bson.M{"$regex": escapeRegex(username), "$options": "i"}
|
|
|
}
|
|
|
// 查询总数
|
|
|
count, err := mdb.MDB.Collection("admin").CountDocuments(ctx, filter)
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("Find CountDocuments error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
- }
|
|
|
-
|
|
|
- // 设置分页选项
|
|
|
- skip := (page - 1) * pageSize
|
|
|
- limit := pageSize
|
|
|
- findOptions := options.Find().SetSkip(int64(skip)).SetLimit(int64(limit))
|
|
|
-
|
|
|
- // 执行查询
|
|
|
- cursor, err := mdb.MDB.Collection("admin").Find(ctx, filter, findOptions)
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("Find Find error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
- }
|
|
|
- defer func() {
|
|
|
- if closeErr := cursor.Close(ctx); closeErr != nil {
|
|
|
- mhayaLogger.Warnf("Error closing cursor: %v", closeErr)
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
- // 解析结果
|
|
|
- admins := make([]*model.Admin, 0)
|
|
|
- for cursor.Next(ctx) {
|
|
|
- var admin model.Admin
|
|
|
- err := cursor.Decode(&admin)
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("Find Decode error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
- }
|
|
|
- admins = append(admins, &admin)
|
|
|
- }
|
|
|
-
|
|
|
- if err := cursor.Err(); err != nil {
|
|
|
- mhayaLogger.Warnf("Find cursor error:", err)
|
|
|
- return nil, common.NewResult(code.InternalError)
|
|
|
- }
|
|
|
-
|
|
|
- var details []*entity.AdminListDetail
|
|
|
- for _, admin := range admins {
|
|
|
- roleName := ""
|
|
|
- roleName, _ = a.GetRoleName(admin.RoleId)
|
|
|
-
|
|
|
- details = append(details, &entity.AdminListDetail{
|
|
|
- Id: admin.GetID(),
|
|
|
- Username: admin.Username,
|
|
|
- RealName: admin.RealName,
|
|
|
- RoleId: admin.RoleId,
|
|
|
- RoleName: roleName,
|
|
|
- Status: admin.Status,
|
|
|
- CreatedAt: admin.CreatedAt,
|
|
|
- UpdatedAt: admin.UpdatedAt,
|
|
|
- LastLoginIp: admin.LastLoginIp,
|
|
|
- LastLoginTime: admin.LastLoginTime,
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- return &entity.AdminListResp{
|
|
|
- Details: details,
|
|
|
- Total: count,
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-// FindAll 查找所有管理员信息
|
|
|
-func (a *Admin) FindAll(ctx context.Context, req entity.AdminFindAllReq) (*entity.AdminListResp, *code.Result) {
|
|
|
- // 日志记录
|
|
|
- mhayaLogger.Warnf("FindAll req: %#v", req)
|
|
|
-
|
|
|
- // 验证参数
|
|
|
- page := req.Page
|
|
|
- if req.Page <= 0 {
|
|
|
- page = 1
|
|
|
- }
|
|
|
- pageSize := req.Size
|
|
|
- if req.Size <= 0 {
|
|
|
- pageSize = 10
|
|
|
- }
|
|
|
-
|
|
|
- // 构建查询条件
|
|
|
- // filter := bson.M{}
|
|
|
- // if req.Username != "" {
|
|
|
- // filter["username"] = bson.M{"$regex": escapeRegex(req.Username), "$options": "i"}
|
|
|
- // }
|
|
|
- // 查询总数
|
|
|
- // count, err := mdb.MDB.Collection("admin").CountDocuments(ctx, filter)
|
|
|
- count, err := mdb.MDB.Collection("admin").CountDocuments(ctx, map[string]interface{}{})
|
|
|
if err != nil {
|
|
|
mhayaLogger.Warnf("FindAll CountDocuments error:", err)
|
|
|
return nil, common.NewResult(code.InternalError)
|
|
@@ -397,8 +308,7 @@ func (a *Admin) FindAll(ctx context.Context, req entity.AdminFindAllReq) (*entit
|
|
|
findOptions := options.Find().SetSkip(int64(skip)).SetLimit(int64(limit))
|
|
|
|
|
|
// 执行查询
|
|
|
- // cursor, err := mdb.MDB.Collection("admin").Find(ctx, filter, findOptions)
|
|
|
- cursor, err := mdb.MDB.Collection("admin").Find(ctx, map[string]interface{}{}, findOptions)
|
|
|
+ cursor, err := mdb.MDB.Collection("admin").Find(ctx, filter, findOptions)
|
|
|
if err != nil {
|
|
|
mhayaLogger.Warnf("FindAll Find error:", err)
|
|
|
return nil, common.NewResult(code.InternalError)
|