package logic

import (
	"app.yhyue.com/moapp/jybase/common"
	"context"
	"jyBXSubscribe/rpc/model"
	"log"
	"strings"

	"jyBXSubscribe/rpc/internal/svc"
	"jyBXSubscribe/rpc/type/bxsubscribe"

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

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

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

// 手动分发人员
func (l *GetDistributorLogic) GetDistributor(in *bxsubscribe.GetDistributorReq) (*bxsubscribe.DistributorResp, error) {
	// todo: add your logic here and delete this line
	da := new(bxsubscribe.DistributorResp)
	var (
		users []*model.User
	)
	log.Printf("分发人员参数entuserid:%s;entid:%s;region:%s;QueryType:%s", in.EntUserId, in.EntId, in.Region, in.QueryType)
	//搜索分发人员
	if in.QueryType == "1" {
		users = model.AllDistributor(common.IntAll(in.EntId), common.IntAll(in.EntUserId))
	} else {
		//手动分发人员
		users = model.Distributor(strings.Split(in.Region, ","), common.IntAll(in.EntId), common.IntAll(in.EntUserId))
	}
	if len(users) > 0 {
		for _, v := range users {
			var data bxsubscribe.UserResp
			data.Id = common.Int64All(v.Id)
			data.Name = v.Name
			data.Phone = v.Phone
			da.Items = append(da.Items, &data)
		}
	}
	return da, nil
}
