123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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{},
- }
- 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 != "" {
- _, _, in.KeyWords = util.InterceptSearchKW(in.KeyWords, false, len(in.Industry) == 0)
- }
- //查询数据
- if in.KeyWords != "" || in.Industry != "" {
- //查询数据
- searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
- //全文检索限制
- if searchLimit {
- res.IsLimit = util.IsLimited(in.UserId, in.UserType != "fType")
- if res.IsLimit == 1 { //没有被限制
- defer util.Limit()
- }
- }
- //无限制
- if res.IsLimit == 1 {
- var count int64 = 0
- var list = []*bxcore.SearchList{}
- if !in.SecondSearch {
- count, list = util.GetBidSearchData(in)
- }
- //二次搜索- 一次搜索结果少于一页数据;关键词长度大于三;第一,二页请求;搜索范围包括title;四个条件
- if len([]rune(in.KeyWords)) > 3 && int(count) < util.SearchPageSize && in.PageNum < 3 && strings.Contains(in.SelectType, "title") {
- if in.KeyWords = util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); in.KeyWords != "" {
- in.SelectType = "title,content"
- _count, _list := util.GetBidSearchData(in)
- if count < _count {
- count = _count
- list = _list
- }
- //最多两页数据
- if count > util.SearchPageSize*2 {
- count = util.SearchPageSize * 2
- }
- res.KeyWords = in.KeyWords
- if in.PageNum == 1 {
- res.SecondSearch = true
- }
- }
- }
- limitCount := MC.If(in.UserType != "fType", int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
- 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 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
- }
|