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

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

func (l *GetDistrictListLogic) GetDistrictList(req *types.GetDistrictListReq) (resp []*types.DistrictInfo, err error) {
	rpcReq := &core.DistrictReq{
		Province: req.Province,
		City:     req.City,
		Area:     req.Area,
		Street:   req.Street,
		Level:    req.Level,
		Name:     req.Name,
	}
	list, err := l.svcCtx.CoreRpc.GetDistrictList(l.ctx, rpcReq)
	if err != nil {
		return nil, err
	}
	resp = make([]*types.DistrictInfo, len(list.Data))

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

	return
}
