123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package logic
- import (
- MC "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "context"
- "encoding/json"
- "fmt"
- IC "jyBXCore/rpc/init"
- "jyBXCore/rpc/util"
- "strings"
- "time"
- "jyBXCore/rpc/internal/svc"
- "jyBXCore/rpc/type/bxcore"
- "github.com/zeromicro/go-zero/core/logx"
- )
- 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),
- }
- }
- // 标讯搜索结果列表数据
- func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
- defer MC.Catch()
- t := time.Now()
- res := &bxcore.SearchData{
- Count: 0,
- List: []*bxcore.SearchList{},
- InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
- }
- if in.UserId != "" {
- //历史记录
- history_str := redis.GetStr("other", "s_"+in.UserId)
- arrs := util.SearchHistory(history_str, in.KeyWords)
- //redis.Del("other", "s_"+in.UserId)
- if len(arrs) > 0 {
- redis.Put("other", "s_"+in.UserId, strings.Join(arrs, ","), -1)
- //res.HistoryKeys = arrs
- }
- }
- //
- if in.SelectType == "" {
- in.SelectType = "title,content"
- }
- in.Industry = strings.TrimSpace(in.Industry)
- userInfo := util.GetVipState(IC.MainMysql, IC.Mgo, in.UserId)
- //付费用户
- if in.UserType != "fType" && !userInfo.IsPayedUser() {
- return &bxcore.SearchResp{
- ErrCode: -1,
- ErrMsg: "无权限",
- }, nil
- }
- if in.UserType == "fType" {
- in.BuyerClass = ""
- in.BuyerTel = ""
- in.WinnerTel = ""
- in.ExclusionWords = ""
- in.City = ""
- }
- in.KeyWords = strings.TrimSpace(in.KeyWords)
- //搜索范围
- queryItems := userInfo.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit)
- in.SelectType = strings.Join(queryItems, ",")
- //b_word, s_word := "", ""
- res.IsLimit = 1
- //以后可能会出现 关键词 C++ 等带+的关键词
- if in.KeyWords != "" {
- kwLimit := MC.IntAllDef(IC.C.KeywordsLimit, 35)
- if in.UserId == "" {
- kwLimit = 25
- }
- res.InterceptKeywords, res.InterceptWord, in.KeyWords = util.InterceptSearchKW(in.KeyWords, kwLimit, len(in.Industry) == 0)
- }
- if in.UserId == "" {
- //未登录用户访信息类型 需要过滤掉 拟建和采购意向问全部
- if in.Subtype == "" {
- in.Subtype = "招标预告,招标公告,招标结果,招标信用信息"
- }
- }
- //查询数据
- if in.KeyWords != "" || in.Industry != "" {
- //查询数据
- searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
- //未登录用户标题、正文都限制,已登录用户只限制正文
- if in.UserId == "" {
- searchLimit = true
- }
- //全文检索限制
- 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 {
- var count int64
- var list = []*bxcore.SearchList{}
- //付费用户搜索优化
- publishTime := in.PublishTime
- t1 := time.Now()
- if b := util.IsOptimize(IC.C, in); b {
- count, list = util.GetBidSearchData(in)
- count += 1 //避免刚好50条 无法加载下一页数据
- }
- logx.Info("1查询耗时", time.Since(t1))
- t2 := time.Now()
- //分词后 第二页数据请求 先获取全部数据 再切割
- if in.SplitKeywords != "" && strings.Contains(in.SplitKeywords, "+&&&") && in.PageNum == 2 {
- in.KeyWords = strings.ReplaceAll(in.KeyWords, "+&&&", "")
- in.PageNum = 1
- }
- //不够配置条 走原始查询
- if count < IC.C.PaySearchLimit.PageSize {
- in.PublishTime = publishTime
- count, list = util.GetBidSearchData(in)
- }
- logx.Info("2查询耗时:", time.Since(t2))
- res.KeyWords = in.KeyWords
- //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
- if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
- if iksk := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); iksk != "" {
- t3 := time.Now()
- iksk_temp := in.KeyWords
- in.KeyWords = iksk
- in.SelectType = "title"
- //最多查两页数据
- in.PageSize = 2 * in.PageSize
- _, secondList := util.GetBidSearchData(in)
- //数据合并 去重 排序
- list = util.DelRepeatSearchData(list, secondList)
- count = int64(len(list))
- switch {
- case count > util.SearchPageSize:
- count = MC.If(count > int64(util.SearchPageSize*2), int64(util.SearchPageSize*2), count).(int64)
- list = list[:util.SearchPageSize]
- if in.SplitKeywords != "" {
- list = list[util.SearchPageSize:count]
- }
- case count <= util.SearchPageSize:
- list = list[:count]
- }
- var kbool = map[string]bool{}
- var karr = []string{}
- for _, v := range strings.Split(fmt.Sprintf("%s+%s", iksk_temp, iksk), "+") {
- if kbool[v] {
- continue
- }
- karr = append(karr, v)
- kbool[v] = true
- }
- //+&&& 作为二次搜索第二页数据请求的标识(临时处理)
- res.KeyWords = strings.Join(karr, "+") + "+&&&"
- logx.Info("3查询耗时:", time.Since(t3))
- }
- }
- limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
- if in.UserId == "" {
- limitCount = util.SearchMaxPageCount_NOLOGIN
- }
- if count > limitCount {
- count = limitCount
- }
- //是否收藏
- //util.MakeCollection(in.UserId, list)
- res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
- res.Count = count
- res.List = list
- }
- logx.Info("关键词 -0- 查询耗时:", time.Since(t).Seconds())
- } else if in.Platform == "PC" {
- var count int64 = 0
- var list = []*bxcore.SearchList{}
- redisDataKey := fmt.Sprintf("PC_SearchDataCache_%s_%d", in.Platform, in.PageNum)
- sCache, err := redis.GetNewBytes(util.RedisName, redisDataKey)
- if err == nil {
- redisCountKey := fmt.Sprintf("PC_SearchCountCache_%s", in.Platform)
- count = int64(redis.GetInt(util.RedisName, redisCountKey))
- if sCache != nil && len(*sCache) > 0 {
- err = json.Unmarshal(*sCache, &list)
- if err != nil {
- return &bxcore.SearchResp{
- ErrCode: -1,
- ErrMsg: "缓存数据序列化异常:" + err.Error(),
- }, nil
- }
- } else {
- pcstime := IC.C.PCSTime * 60 * 60
- //缓存数据
- _in := &bxcore.SearchReq{
- PageNum: in.PageNum,
- PageSize: in.PageSize,
- }
- count, list = util.SearchCahcheData(_in)
- limitCount := int64(util.SearchPageSize * util.SearchMaxPageNum)
- if in.UserId == "" {
- limitCount = util.SearchMaxPageCount_NOLOGIN
- }
- if count > limitCount {
- count = limitCount
- }
- if len(list) > 0 {
- redis.Put(util.RedisName, redisCountKey, count, pcstime)
- b, err := json.Marshal(list)
- if err == nil {
- redis.PutBytes(util.RedisName, redisDataKey, &b, pcstime)
- } else {
- return &bxcore.SearchResp{
- ErrCode: -1,
- ErrMsg: "缓存数据 转化异常:" + err.Error(),
- }, nil
- }
- }
- }
- //是否收藏
- util.MakeCollection(in.UserId, list)
- res.TotalPage = MC.If(in.PageNum == 1, (count+int64(util.SearchPageSize)-1)/int64(util.SearchPageSize), res.TotalPage).(int64)
- res.Count = count
- res.List = list
- } else {
- return &bxcore.SearchResp{
- ErrCode: -1,
- ErrMsg: "查询redis缓存异常:" + err.Error(),
- }, nil
- }
- }
- logx.Info("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
- return &bxcore.SearchResp{
- Data: res,
- ErrMsg: "",
- ErrCode: 0,
- }, nil
- }
|