package district

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 GetDistrictChildrenLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

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

func (l *GetDistrictChildrenLogic) GetDistrictChildren(req *types.GetDistrictChildrenReq) (resp []*types.DistrictInfo, err error) {
	rpcReq := &core.DistrictReq{ParentCode: req.ParentCode, QueryType: core.DistrictQueryType_Children}
	if req.ParentCode == "" {
		rpcReq.Level = 1
	}
	rpcResp, err := l.svcCtx.CoreRpc.GetDistrictList(l.ctx, rpcReq)
	if err != nil {
		return nil, err
	}
	resp = make([]*types.DistrictInfo, len(rpcResp.Data))

	for i, item := range rpcResp.Data {
		resp[i] = l.svcCtx.Convert.ConvertDistrict(l.ctx, item)
	}
	return
}
