package logic

import (
	"context"
	"jyBXSubscribe/rpc/type/bxsubscribe"

	"jyBXSubscribe/api/internal/svc"
	"jyBXSubscribe/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DistributorLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDistributorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DistributorLogic {
	return &DistributorLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DistributorLogic) Distributor(req *types.DistributorReq) (resp *types.CommonResp, err error) {
	// todo: add your logic here and delete this line
	res, err := l.svcCtx.Suscribe.GetDistributor(l.ctx, &bxsubscribe.GetDistributorReq{
		AppId:     req.AppId,
		EntId:     req.EntId,
		EntUserId: req.EntUserId,
		Region:    req.Region,
		QueryType: req.QueryType,
	})
	if err != nil {
		return &types.CommonResp{
			Err_code: res.ErrCode,
			Err_msg:  res.ErrMsg,
			Data:     nil,
		}, nil
	}
	var data []map[string]interface{}
	for _, v := range res.Items {
		_d := make(map[string]interface{})
		_d["name"] = v.Name
		_d["id"] = v.Id
		_d["phone"] = v.Phone
		data = append(data, _d)
	}
	return &types.CommonResp{
		Err_code: res.ErrCode,
		Err_msg:  res.ErrMsg,
		Data:     data,
	}, nil
	return
}
