package logic

import (
	"app.yhyue.com/moapp/jybase/common"
	"context"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/zeromicro/go-zero/core/logx"
	"jyBXSubscribe/api/internal/svc"
	"jyBXSubscribe/api/internal/types"
	"jyBXSubscribe/rpc/type/bxsubscribe"
	"net/http"
	"strings"
)

type SubscribeListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

func NewSubscribeListLogic(ctx context.Context, svcCtx *svc.ServiceContext, r *http.Request) *SubscribeListLogic {
	return &SubscribeListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
		r:      r,
	}
}

func (l *SubscribeListLogic) SubscribeList(req *types.SubscribeReq) (resp *types.CommonResp, err error) {
	//关键词处理
	//分类
	matchitemsArr := []string{}
	matchitems := ""
	//关键词
	matchkeysArr := []string{}
	matchkeys := ""
	for k, v := range req.Item {
		//类型换换
		keyArr := gconv.Strings(v)
		if len(keyArr) == 0 {
			matchitemsArr = append(matchitemsArr, common.InterfaceToStr(k))
		} else {
			for _, s := range keyArr {
				matchkeysArr = append(matchkeysArr, s)
			}
		}
	}
	if len(matchitemsArr) != 0 {
		matchitems = strings.Join(matchitemsArr, ",")
	}
	if len(matchkeysArr) != 0 {
		matchkeys = strings.Join(matchkeysArr, ",")
		matchkeys = strings.ReplaceAll(matchkeys, " ", ",")
	}
	res, err := l.svcCtx.Suscribe.GetSubList(l.ctx, &bxsubscribe.SubscribeInfosReq{
		PageNum:        req.PageNum,
		PageSize:       req.PageSize,
		SelectTime:     req.SelectTime,
		Area:           req.Area,
		City:           req.City,
		Industry:       req.Industry,
		BuyerClass:     req.BuyerClass,
		KeyWords:       matchkeys,
		Subtype:        req.Subtype,
		UserType:       req.UserType,
		Price:          req.Price,
		FileExists:     req.FileExists,
		IsRead:         req.IsRead,
		Source:         req.Source,
		Staffs:         req.Staffs,
		UserId:         req.UserId,
		EntId:          req.EntId,
		EntUserId:      req.EntUserId,
		DeptId:         req.DeptId,
		NewUserId:      req.NewUserId,
		IsEnt:          req.IsEnt,
		PositionType:   req.PositionType,
		NotReturnCount: req.NotReturnCount,
		Item:           matchitems,
		AccountId:      req.AccountId,
		PositionId:     req.PositionId,
		MgoUserId:      req.MgoUserId,
	})
	if err != nil {
		return &types.CommonResp{
			Err_code: res.ErrCode,
			Err_msg:  res.ErrMsg,
			Data:     nil,
		}, nil
	}
	return &types.CommonResp{
		Err_code: res.ErrCode,
		Err_msg:  res.ErrMsg,
		Data:     res.Data,
	}, nil
}
