package role_admin

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"

	"net/http"
)

type GetRoleListAdminLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

func NewGetRoleListAdminLogic(r *http.Request, ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleListAdminLogic {
	return &GetRoleListAdminLogic{
		r:      r,
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetRoleListAdminLogic) GetRoleListAdmin(req *types.RoleReq) (resp *types.RoleListResp, err error) {
	rpcResp, err := l.svcCtx.CoreRpc.GetRoleList(l.ctx, &core.RoleReq{
		ID:   req.ID,
		Name: req.Name,
		Code: req.Code,
		Page: l.svcCtx.Convert.ConvertPaginationRpc(req.Pagination),
	})
	if err != nil {
		return nil, err
	}
	resp = &types.RoleListResp{
		Page: l.svcCtx.Convert.ConvertPaginationApi(rpcResp.Page),
		List: make([]*types.RoleInfo, len(rpcResp.Data)),
	}
	for i, item := range rpcResp.Data {
		resp.List[i] = l.svcCtx.Convert.ConvertRole(l.ctx, item)
	}
	return
}
