123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- package entity
- import (
- MC "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "encoding/json"
- "fmt"
- "github.com/zeromicro/go-zero/core/logx"
- "jyBXCore/rpc/bxcore"
- IC "jyBXCore/rpc/init"
- "jyBXCore/rpc/service"
- "jyBXCore/rpc/util"
- "strings"
- "time"
- )
- var (
- SearchCacheKey = "searchDataCache_%s_%s"
- SearchCacheCount = "searchCountCache_%s_%s"
- )
- type KeyWordsSearch struct{}
- func NewKeyWordsSearch() *KeyWordsSearch {
- return &KeyWordsSearch{}
- }
- // IsEmptySearch 是否是空搜索,如果是空搜索查缓存数据
- func (kws *KeyWordsSearch) IsEmptySearch(in *bxcore.SearchReq) bool {
- //有主关键词 或 选择了行业,都不是空搜索
- if in.KeyWords != "" || in.Industry != "" {
- return false
- }
- return true
- }
- // GetBidSearchListByCache 查询缓存数据
- //未登录用户默认搜索和关键词搜索改成500条和免费用户保持一致--需求调整P260来自产品经理杨蘭20220116
- func (kws *KeyWordsSearch) GetBidSearchListByCache(in *bxcore.SearchReq) (list []*bxcore.SearchList, count int64) {
- //缓存数据 最大量是5000条 100页数据
- l, c := func(in *bxcore.SearchReq) (list []*bxcore.SearchList, count int64) {
- //缓存数据: kws.Platform-平台;kws.PageNum-当前页 免费用户 or 付费用户
- redisDataKey := fmt.Sprintf(SearchCacheKey, in.Platform, MC.If(in.IsPay, "V", "F").(string))
- //缓存数据总量 - 当前平台
- redisCountKey := fmt.Sprintf(SearchCacheCount, in.Platform, MC.If(in.IsPay, "V", "F").(string))
- sCache, err := redis.GetNewBytes(util.RedisNameNew, redisDataKey)
- count = int64(redis.GetInt(util.RedisNameNew, redisCountKey))
- if err == nil {
- if sCache != nil && len(*sCache) > 0 {
- err = json.Unmarshal(*sCache, &list)
- if err == nil {
- return
- }
- }
- }
- //无缓存数据 或 缓存数据查询异常
- //查库>存redis缓存
- //查询缓存数据 参数初始化
- kws.DefaultSearchParamsAuto(in)
- //缓存数据
- count, list = service.GetBidSearchData(in, true)
- limitCount := int64(util.SearchPageSize * MC.If(in.IsPay, util.SearchMaxPageNum_PAYED, util.SearchMaxPageNum).(int))
- if count > limitCount {
- count = limitCount
- }
- if len(list) > 0 {
- redis.Put(util.RedisNameNew, redisCountKey, count, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
- b, err := json.Marshal(list)
- if err == nil {
- redis.PutBytes(util.RedisNameNew, redisDataKey, &b, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
- } else {
- logx.Info("默认搜索查询结果保存redis缓存异常")
- }
- } else {
- logx.Info("默认搜索 暂无数据")
- }
- return
- }(in)
- if len(l) > 0 {
- list = l[(in.PageNum-1)*in.PageSize : in.PageNum*in.PageSize]
- count = c
- //是否收藏
- util.MakeCollection(in.UserId, list)
- }
- return
- }
- // DefaultSearchParamsAuto 缓存查询条件初始化
- func (kws *KeyWordsSearch) DefaultSearchParamsAuto(in *bxcore.SearchReq) {
- //缓存数据 默认查询分组 searchGroup = 1 ,
- //不查超前信息--招标信息类型为拟建和采购意向的信息
- in.SearchGroup = 1
- in.Subtype = IC.C.DefaultTopTypes[in.SearchGroup-1]
- in.TopType = ""
- in.City = ""
- in.Industry = ""
- in.FileExists = ""
- in.WinnerTel = ""
- in.BuyerTel = ""
- in.BuyerClass = ""
- in.Price = ""
- in.SelectType = "title"
- in.Province = ""
- //in.PublishTime = ""
- }
- // SaveKeyWordsToHistory 保存历史记录
- func (kws *KeyWordsSearch) SaveKeyWordsToHistory(in *bxcore.SearchReq) {
- if in.KeyWords != "" {
- //历史记录
- history := redis.GetStr("other", "s_"+in.UserId)
- keys := util.SearchHistory(history, in.KeyWords)
- if len(keys) > 0 {
- if b := redis.Put("other", "s_"+in.UserId, strings.Join(keys, ","), -1); !b {
- logx.Info("保存搜索记录异常,用户id:", in.UserId)
- }
- }
- }
- }
- // SearchParamsHandle 搜索条件 处理
- func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) {
- //判断用户身份
- userInfo := util.GetVipState(IC.MainMysql, IC.Mgo, in.UserId, in.EntId)
- //是否是付费用户
- in.IsPay = userInfo.IsPayedUser()
- //默认搜索范围
- if in.SelectType == "" {
- in.SelectType = "title,content"
- }
- queryItems := userInfo.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit)
- in.SelectType = strings.Join(queryItems, ",")
- // in.SearchGroup 搜索分组 搜索分组:默认0:全部;1:招标采购公告;2:超前项目
- if in.SearchGroup < 0 || in.SearchGroup > 2 {
- in.SearchGroup = 1
- }
- //判断是否能使用超前项目 老版超级订阅、大会员、商机管理有权限
- if !(userInfo.IsOldVip || userInfo.BigMember > 0 || userInfo.EntMember > 0) {
- in.SearchGroup = 1
- //Subtype 包含:拟建,采购意向,则需要排除
- if in.Subtype != "" {
- var subtype []string
- for _, sv := range strings.Split(in.Subtype, ",") {
- if sv == "拟建" || sv == "采购意向" {
- continue
- }
- subtype = append(subtype, sv)
- }
- in.Subtype = MC.If(len(subtype) > 0, strings.Join(subtype, ","), "").(string)
- }
- }
- //信息类型参数为空 根据搜索分组 初始化信息类型
- //in.SearchGroup = 0
- if in.TopType == "" && in.Subtype == "" {
- if in.SearchGroup > 0 && len(IC.C.DefaultTopTypes) > int(in.SearchGroup) {
- in.Subtype = IC.C.DefaultTopTypes[in.SearchGroup-1]
- }
- }
- // in.SearchMode 搜索模式 搜索模式:0:精准搜索;1:模糊搜索
- // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
- // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
- if in.SearchMode < 0 || in.SearchMode > 1 {
- in.SearchMode = 0
- }
- // in.WordsMode 搜索关键词模式;默认0:包含所有,1:包含任意
- if in.WordsMode < 0 || in.WordsMode > 1 {
- in.WordsMode = 0
- }
- //查询时间publishTime
- if in.PublishTime == "" {
- //付费用户最新5年;免费用户最新3年
- in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-3, 0, 0).Unix(), time.Now().Unix())
- if userInfo.IsPayedUser() {
- in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-5, 0, 0).Unix(), time.Now().Unix())
- }
- }
- //默认每页数据量
- if in.PageSize <= 0 {
- in.PageSize = 50
- }
- //第一页
- if in.PageNum <= 0 {
- in.PageNum = 1
- }
- //行业格式化
- if in.Industry != "" {
- in.Industry = strings.TrimSpace(in.Industry)
- }
- //免费用户:高级筛选 采购单位类型、采购单位联系方式、中标企业联系方式、排除词、城市
- if !userInfo.IsPayedUser() {
- in.BuyerClass = ""
- in.BuyerTel = ""
- in.WinnerTel = ""
- in.ExclusionWords = ""
- in.City = ""
- in.SearchGroup = MC.If(in.SearchGroup > 1, 0, in.SearchGroup).(int64) //搜索分组:默认0:全部;1:招标采购公告;2:超前项目
- in.ExclusionWords = ""
- }
- //判断是否有关键词
- if in.KeyWords != "" {
- //关键词处理
- in.KeyWords = strings.TrimSpace(in.KeyWords)
- //以后可能会出现 关键词 C++ 等带+的关键词
- in.InterceptKeyWords, in.InterceptOtherWords, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), len(in.Industry) == 0)
- //附加词在关键词的基础上 有效
- //附加词 每组附加词不能超过15个字符
- if in.AdditionalWords != "" {
- var additionalWords []string
- for _, ak := range strings.Split(in.AdditionalWords, ",") {
- if len([]rune(ak)) > 15 {
- additionalWords = append(additionalWords, string([]rune(ak)[:15]))
- } else {
- additionalWords = append(additionalWords, ak)
- }
- }
- in.AdditionalWords = strings.ReplaceAll(strings.Join(additionalWords, IC.C.JYKeyMark), " ", IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.AdditionalWords, IC.C.JYKeyMark)
- }
- //格式化关键词
- in.KeyWords = util.GetSearchKeyWordsQueryStr(in)
- //更新关键词搜索历史记录
- go kws.SaveKeyWordsToHistory(in)
- } else {
- in.AdditionalWords = ""
- }
- //排除词 每组排除词不能超过15个字符
- if in.ExclusionWords != "" {
- var exclusionWords []string
- for _, ak := range strings.Split(in.ExclusionWords, ",") {
- if len([]rune(ak)) > 15 {
- exclusionWords = append(exclusionWords, string([]rune(ak)[:15]))
- } else {
- exclusionWords = append(exclusionWords, ak)
- }
- }
- in.ExclusionWords = strings.Join(exclusionWords, IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.ExclusionWords, IC.C.JYKeyMark)
- }
- }
- // GetBidSearchList 非空搜索 查询
- func (kws *KeyWordsSearch) GetBidSearchList(in *bxcore.SearchReq) (count, total int64, list []*bxcore.SearchList) {
- //排除异常in.PageNum参数
- count, list = service.GetBidSearchData(in, false)
- util.MakeCollection(in.UserId, list)
- total = count //返回数据总量提示信息
- limitCount := MC.If(in.IsPay, int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
- if count > limitCount {
- count = limitCount //付费用户count 最多5000条,100页数据,每页50条;免费用户count 最多500条,10页数据,每页50条。
- }
- return
- }
|