Browse Source

Revert "update 完善优化代码"

This reverts commit 8a94686b409fc65c80a91671746b0f673b58abb9.
Alvin 8 months ago
parent
commit
383022ab13

+ 1 - 4
game/game_cluster/nodes/webadmin/common/packResponse.go

@@ -33,10 +33,7 @@ func PackOkResult(c *gin.Context, statusCode int32, data ...interface{}) {
 	result := &code.Result{
 		Code:    statusCode,
 		Message: code.GetMessage(statusCode),
-	}
-
-	if len(data) > 0 {
-		result.Data = data[0]
+		Data:    data,
 	}
 
 	c.JSON(http.StatusOK, result)

+ 17 - 0
game/game_cluster/nodes/webadmin/controller/admin.go

@@ -86,6 +86,23 @@ func (a *Admin) Del(ctx *gin.Context) {
 	common.PackOkResult(ctx, code.OK, nil)
 }
 
+// Find 获取管理员信息
+func (a *Admin) Find(ctx *gin.Context) {
+	var req entity.AdminFindReq
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		common.PackOkResult(ctx, code.ParamError)
+		return
+	}
+
+	resp, err := a.sev.Find(ctx, req)
+	if err != nil {
+		common.PackOkResult(ctx, err.Code)
+		return
+	}
+
+	common.PackOkResult(ctx, code.OK, resp)
+}
+
 // FindAll 获取所有管理员
 func (a *Admin) FindAll(ctx *gin.Context) {
 	var req entity.AdminFindAllReq

+ 1 - 1
game/game_cluster/nodes/webadmin/controller/synthesis.go

@@ -114,7 +114,7 @@ func (s *Synthesis) WithdrawalStatusBatch(ctx *gin.Context) {
 	common.PackOkResult(ctx, code.OK, nil)
 }
 
-// FindUserLevel 用户等级统计
+// FindUserLevel 查询用户等级
 func (s *Synthesis) FindUserLevel(ctx *gin.Context) {
 	resp, err := s.sev.FindUserLevel()
 	if err != nil {

+ 6 - 1
game/game_cluster/nodes/webadmin/entity/admin.go

@@ -49,12 +49,17 @@ type AdminDelReq struct {
 	Username string `json:"username"`
 }
 
-type AdminFindAllReq struct {
+type AdminFindReq struct {
 	Page     int    `json:"page"`
 	Size     int    `json:"size"`
 	Username string `json:"username"`
 }
 
+type AdminFindAllReq struct {
+	Page int `json:"page"`
+	Size int `json:"size"`
+}
+
 type AdminUpdateStatusReq struct {
 	Username string `json:"username"`
 	Status   int    `json:"status"`

+ 1 - 0
game/game_cluster/nodes/webadmin/entity/user_lavel_count.go

@@ -2,6 +2,7 @@ package entity
 
 type UserLevelCountResp struct {
 	Details []*UserLevelCountDetail `json:"details"`
+	Total   int64                   `json:"total"`
 }
 
 type UserLevelCountDetail struct {

+ 2 - 0
game/game_cluster/nodes/webadmin/entity/user_reg_count.go

@@ -3,6 +3,7 @@ package entity
 // UserRetentionResp 用于存储用户的留存数据
 type UserRetentionResp struct {
 	Details []*UserRetentionDetail `json:"details"`
+	Total   int64                  `json:"total"`
 }
 
 type UserRetentionDetail struct {
@@ -16,6 +17,7 @@ type UserRetentionReq struct {
 	EndTime   int64 `json:"end_time"`   // 结束时间
 	Page      int   `json:"page"`       // 页码
 	Size      int   `json:"size"`       // 每页数量
+	Total     int64 `json:"total"`
 }
 
 // UserRetentionData 用于存储用户的留存数据

+ 2 - 1
game/game_cluster/nodes/webadmin/router/router.go

@@ -48,7 +48,8 @@ func (c *Controller) InitApiRouter(u *gin.RouterGroup) {
 	u.POST("/user/changePassword", controller.NewAdmin().ChangePassword)
 	u.POST("/user/add", controller.NewAdmin().Add)
 	u.POST("/user/del", controller.NewAdmin().Del)
-	u.POST("/user/find", controller.NewAdmin().FindAll)
+	u.POST("/user/find", controller.NewAdmin().Find)
+	u.POST("/user/findAll", controller.NewAdmin().FindAll)
 	u.POST("/user/update", controller.NewAdmin().UpdateStatus)
 	u.POST("/user/serverStatus", controller.NewAdmin().GetServerStatus)
 	u.POST("/role/add", controller.NewRole().Add)

+ 93 - 5
game/game_cluster/nodes/webadmin/service/admin.go

@@ -110,7 +110,7 @@ func (a *Admin) Login(ctx *gin.Context, username string, password string) (*enti
 		ToKen:  generateToken,
 		RoleID: user.RoleId,
 	}
-
+	mhayaLogger.Debugf("Login result:", resp)
 	return resp, nil
 }
 
@@ -277,10 +277,10 @@ func (a *Admin) UpdateStatus(ctx context.Context, username string, status int) *
 	return nil
 }
 
-// FindAll 查找所有管理员信息
-func (a *Admin) FindAll(ctx context.Context, req entity.AdminFindAllReq) (*entity.AdminListResp, *code.Result) {
+// Find 获取管理员信息
+func (a *Admin) Find(ctx context.Context, req entity.AdminFindReq) (*entity.AdminListResp, *code.Result) {
 	// 日志记录
-	mhayaLogger.Warnf("FindAll req: %#v", req)
+	mhayaLogger.Warnf("Find req: %#v", req)
 
 	// 验证参数
 	page := req.Page
@@ -300,7 +300,7 @@ func (a *Admin) FindAll(ctx context.Context, req entity.AdminFindAllReq) (*entit
 	// 查询总数
 	count, err := mdb.MDB.Collection("admin").CountDocuments(ctx, filter)
 	if err != nil {
-		mhayaLogger.Warnf("FindAll CountDocuments error:", err)
+		mhayaLogger.Warnf("Find CountDocuments error:", err)
 		return nil, common.NewResult(code.InternalError)
 	}
 
@@ -311,6 +311,94 @@ func (a *Admin) FindAll(ctx context.Context, req entity.AdminFindAllReq) (*entit
 
 	// 执行查询
 	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)
+	}
+
+	// 设置分页选项
+	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)
+	cursor, err := mdb.MDB.Collection("admin").Find(ctx, map[string]interface{}{}, findOptions)
 	if err != nil {
 		mhayaLogger.Warnf("FindAll Find error:", err)
 		return nil, common.NewResult(code.InternalError)

+ 5 - 5
game/game_cluster/nodes/webadmin/service/synthesis.go

@@ -365,6 +365,7 @@ func (s *Synthesis) FindUserRetention(req entity.UserRetentionReq) (*entity.User
 	var results []*entity.UserRetentionDetail
 
 	for key, v := range playerPreserve {
+		var resp entity.UserRetentionDetail
 		var retention entity.Retention
 		for _, vv := range v {
 			if vv.ID == 1 {
@@ -398,15 +399,14 @@ func (s *Synthesis) FindUserRetention(req entity.UserRetentionReq) (*entity.User
 				}
 			}
 		}
-
-		results = append(results, &entity.UserRetentionDetail{
-			RegistrationDate: key,
-			RetentionData:    retention,
-		})
+		resp.RegistrationDate = key
+		resp.RetentionData = retention
+		results = append(results, &resp)
 	}
 
 	return &entity.UserRetentionResp{
 		Details: results,
+		Total:   req.Total,
 	}, nil
 }