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 var list = []*bxcore.SearchList{} //付费用户搜索优化 publishTime := in.PublishTime if b := util.IsOptimize(IC.C, in); b { count, list = util.GetBidSearchData(in) count += 1 //避免刚好50条 无法加载下一页数据 } //分词后 第二页数据请求 先获取全部数据 再切割 if in.SplitKeywords != "" && strings.Contains(in.SplitKeywords, "+&&&") && in.PageNum == 2 { in.KeyWords = strings.ReplaceAll(in.KeyWords, "+&&&", "") in.PageNum = 1 } //不够五十条 走原始查询 if count < util.SearchPageSize { in.PublishTime = publishTime count, list = util.GetBidSearchData(in) } 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 != "" { 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, "+") + "+&&&" } } 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 }