package department_admin

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

	"git.noahlan.cn/n-admin/n-admin-server/api/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"

	"net/http"
)

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

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

func (l *GetDepartmentListAdminLogic) GetDepartmentListAdmin(req *types.DepartmentReq) (resp *types.DepartmentListResp, err error) {
	rpcResp, err := l.svcCtx.CoreRpc.GetDepartmentList(l.ctx, &core.DepartmentReq{
		ID:           req.ID,
		Name:         req.Name,
		LeaderId:     req.LeaderId,
		ParentId:     req.ParentId,
		WithLeader:   true,
		WithUser:     false,
		WithChildren: true,
		Page:         l.svcCtx.Convert.ConvertPaginationRpc(req.Pagination),
	})
	if err != nil {
		return nil, err
	}
	resp = &types.DepartmentListResp{
		Page: l.svcCtx.Convert.ConvertPaginationApi(rpcResp.Page),
		List: make([]*types.DepartmentInfo, len(rpcResp.Data)),
	}
	return
}
