package logic

import (
	MC "app.yhyue.com/moapp/jybase/common"
	"context"
	"jyBXCore/rpc/entity"
	IC "jyBXCore/rpc/init"
	"jyBXCore/rpc/util"
	"strings"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
	"jyBXCore/rpc/internal/svc"
	"jyBXCore/rpc/type/bxcore"

	"log"
)

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

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

// GetSearchList 标讯搜索结果列表数据
// GetSearchList 标讯搜索结果列表数据
func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
	defer MC.Catch()
	res := &bxcore.SearchData{
		Count:          0,
		List:           []*bxcore.SearchList{},
		InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
	}
	//初始化搜索对象
	ks := entity.NewKeyWordsSearch()
	//处理搜索条件
	heightWords := ks.SearchParamsHandle(in)
	if in.PageNum < 0 && in.PageSize < 0 {
		return &bxcore.SearchResp{
			Data:    res,
			ErrMsg:  "",
			ErrCode: 0,
		}, nil
	}
	//判断是否是空搜索，如果是空搜索，查缓存数据
	if ks.IsEmptySearch(in) {
		res.List, res.Count, res.Total = ks.GetBidSearchListByCache(in)
		return &bxcore.SearchResp{
			Data:    res,
			ErrMsg:  "",
			ErrCode: 0,
		}, nil
	}
	//异常付费用户  参数不是免费fType，但是又不是付费用户；还有一种是未登录用户 稍后处理
	if in.UserType != "fType" && !in.IsPay {
		return &bxcore.SearchResp{
			ErrCode: -1,
			ErrMsg:  "无权限",
		}, nil
	}
	//p353包含采购意向与拟建
	//未登录用户访问全部信息类型 需要过滤掉 拟建和采购意向
	if in.UserId == "" {
		if strings.Contains(in.Subtype, "采购意向") {
			var ss []string
			for _, v := range strings.Split(in.Subtype, ",") {
				if v != "采购意向" {
					ss = append(ss, v)
				}
			}
			in.Subtype = strings.Join(ss, ",")
		}
		if in.Subtype == "" {
			in.Subtype = "拟建,招标预告,招标公告,招标结果,招标信用信息"
		}
	}

	t := time.Now()
	//招标信息有效查询
	res.IsLimit = 1
	//查询数据
	searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
	//全文检索限制
	if searchLimit {
		res.IsLimit = util.IsLimited(in.LimitFlag, in.UserId, in.UserType != "fType", in.IsNew)
		if res.IsLimit == 1 { //没有被限制
			defer util.Limit()
		}
	}
	//无限制
	if res.IsLimit == 1 {
		//付费用户搜索优化--默认搜索5年数据，数据量太多，接口反应太慢，前两页数据 时间范围根据配置缩小查询以达到快速查询的目的。
		publishTime := in.PublishTime
		if b := util.IsOptimize(IC.C, in); b {
			t1 := time.Now()
			res.Count, res.Total, res.List = ks.GetBidSearchList(in) // util.GetBidSearchData(in)
			log.Println("1查询耗时：", time.Since(t1))
		}
		//如果优化查询数据量太少，和配置数据量作比较，不够的话走原始查询
		if res.Count < IC.C.PaySearchLimit.PageSize {
			t2 := time.Now()
			in.PublishTime = publishTime
			res.Count, res.Total, res.List = ks.GetBidSearchList(in) //util.GetBidSearchData(in)
			log.Println("2查询耗时：", time.Since(t2))
		}
		res.KeyWords = strings.Join(heightWords, " ")
		res.InterceptOtherWords = in.InterceptOtherWords
		res.InterceptKeyWords = in.InterceptKeyWords
	}
	log.Println("关键词 -全部- 查询耗时：", time.Since(t).Seconds())
	return &bxcore.SearchResp{
		Data:    res,
		ErrMsg:  "",
		ErrCode: 0,
	}, nil
}
