search.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package entity
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
  6. "encoding/json"
  7. "fmt"
  8. IC "jyBXCore/rpc/init"
  9. "jyBXCore/rpc/service"
  10. "jyBXCore/rpc/type/bxcore"
  11. "jyBXCore/rpc/util"
  12. "log"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. var (
  18. SearchCacheKey = "searchDataCache_%d_%s_%s_%s"
  19. SearchCacheCount = "searchCountCache_%d_%s_%s_%s"
  20. )
  21. type KeyWordsSearch struct{}
  22. func NewKeyWordsSearch() *KeyWordsSearch {
  23. return &KeyWordsSearch{}
  24. }
  25. // IsEmptySearch 是否是空搜索,如果是空搜索查缓存数据
  26. func (kws *KeyWordsSearch) IsEmptySearch(in *bxcore.SearchReq) bool {
  27. //有主关键词 或 选择了行业,都不是空搜索
  28. if strings.TrimSpace(in.KeyWords) != "" || strings.TrimSpace(in.Industry) != "" || strings.TrimSpace(in.AdditionalWords) != "" {
  29. return false
  30. }
  31. //所有的未登录空搜索 缓存都最多查500条数据
  32. return true
  33. }
  34. // GetBidSearchListByCache 查询缓存数据
  35. // 未登录用户默认搜索和关键词搜索改成500条和免费用户保持一致--需求调整P260来自产品经理杨蘭20220116
  36. func (kws *KeyWordsSearch) GetBidSearchListByCache(in *bxcore.SearchReq) (list []*bxcore.SearchList, count, total int64) {
  37. //缓存数据 最大量是5000条 100页数据
  38. l, c := func(in *bxcore.SearchReq) (list []*bxcore.SearchList, count int64) {
  39. //缓存数据总量 - 当前平台
  40. redisCountKey := fmt.Sprintf(SearchCacheCount, in.SearchGroup, MC.If(in.IsPay, "v", "f").(string), MC.If(in.BidField != "", in.BidField, "n").(string), in.Platform)
  41. count = int64(redis.GetInt(util.RedisNameNew, redisCountKey))
  42. //缓存数据: SearchGroup-全部;招标信息;超前项目信息;kws.PageNum-当前页 免费用户 or 付费用户
  43. redisDataKey := fmt.Sprintf(SearchCacheKey, in.SearchGroup, MC.If(in.IsPay, "v", "f").(string), MC.If(in.BidField != "", in.BidField, "n").(string), in.Platform)
  44. sCache, err := redis.GetNewBytes(util.RedisNameNew, redisDataKey)
  45. log.Println("-------------------------redisDataKey--------------------------------:", redisDataKey)
  46. if err == nil {
  47. if sCache != nil && len(*sCache) > 0 {
  48. err = json.Unmarshal(*sCache, &list)
  49. if err == nil {
  50. return
  51. } else {
  52. log.Println("缓存序列化异常")
  53. }
  54. }
  55. }
  56. //无缓存数据 或 缓存数据查询异常
  57. //查库>存redis缓存
  58. //查询缓存数据 参数初始化
  59. kws.DefaultSearchParamsAuto(in)
  60. //缓存数据
  61. count, list = service.GetBidSearchData(in, true)
  62. if len(list) > 0 {
  63. redis.Put(util.RedisNameNew, redisCountKey, count, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
  64. b, err := json.Marshal(list)
  65. if err == nil {
  66. redis.PutBytes(util.RedisNameNew, redisDataKey, &b, MC.If(IC.C.DefaultSearchCacheTime > 0, IC.C.DefaultSearchCacheTime*60*60, 24*60*60).(int))
  67. } else {
  68. log.Println("默认搜索查询结果保存redis缓存异常")
  69. }
  70. } else {
  71. log.Println("默认搜索 暂无数据")
  72. }
  73. return
  74. }(in)
  75. if len(l) > 0 {
  76. total = c
  77. limitCount := int64(util.SearchPageSize * MC.If(in.IsPay, util.SearchMaxPageNum_PAYED, util.SearchMaxPageNum).(int))
  78. count = c
  79. if count > limitCount {
  80. count = limitCount
  81. }
  82. list = l[(in.PageNum-1)*in.PageSize : in.PageNum*in.PageSize]
  83. //是否收藏
  84. util.MakeCollection(in.UserId, list)
  85. }
  86. return
  87. }
  88. // DefaultSearchParamsAuto 缓存查询条件初始化
  89. func (kws *KeyWordsSearch) DefaultSearchParamsAuto(in *bxcore.SearchReq) {
  90. in.TopType = ""
  91. in.City = ""
  92. in.Industry = ""
  93. in.FileExists = ""
  94. in.WinnerTel = ""
  95. in.BuyerTel = ""
  96. in.BuyerClass = ""
  97. in.Price = ""
  98. in.SelectType = "title"
  99. in.Province = ""
  100. }
  101. // SaveKeyWordsToHistory 保存历史记录
  102. func (kws *KeyWordsSearch) SaveKeyWordsToHistory(in *bxcore.SearchReq) {
  103. if in.KeyWords != "" {
  104. //历史记录
  105. history := redis.GetStr("other", "s_"+in.UserId)
  106. keys := util.SearchHistory(history, in.KeyWords, in.AdditionalWords)
  107. if len(keys) > 0 {
  108. if b := redis.Put("other", "s_"+in.UserId, strings.Join(keys, ","), -1); !b {
  109. log.Println("保存搜索记录异常,用户id:", in.UserId)
  110. }
  111. }
  112. }
  113. }
  114. // GetSearchKeyWordsQueryStr 关键词和附加词处理 获取关键词查询条件;是否进行分词查询
  115. func (kws *KeyWordsSearch) GetSearchKeyWordsQueryStr(in *bxcore.SearchReq) (searchWords []string) {
  116. // in.SearchMode 搜索模式:0:精准搜索;1:模糊搜索
  117. // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
  118. // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
  119. //主关键词词组
  120. if in.KeyWords != "" {
  121. if in.SearchMode == 1 {
  122. if ikWords := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
  123. in.KeyWords = jy.KeywordsProcessing(ikWords, IC.C.JYKeyMark)
  124. }
  125. }
  126. searchWords = append(searchWords, in.KeyWords)
  127. }
  128. //多组附加词,每组间,号隔开。每组内如果关键词中间有空格,自动分词
  129. if in.AdditionalWords != "" {
  130. if in.SearchMode == 1 {
  131. var (
  132. addWords []string
  133. )
  134. for _, awv := range strings.Split(in.AdditionalWords, ",") {
  135. if strings.TrimSpace(awv) != "" {
  136. if ikWords := util.HttpEs(awv, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
  137. addWords = append(addWords, jy.KeywordsProcessing(ikWords, IC.C.JYKeyMark))
  138. }
  139. }
  140. }
  141. if len(addWords) > 0 {
  142. in.AdditionalWords = strings.Join(addWords, ",")
  143. }
  144. }
  145. searchWords = append(searchWords, strings.Split(in.AdditionalWords, ",")...)
  146. }
  147. return
  148. }
  149. // SearchParamsHandle 搜索条件 处理
  150. func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) []string {
  151. baseUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
  152. accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
  153. positionType, _ := strconv.ParseInt(in.PositionType, 10, 64)
  154. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  155. //判断用户身份
  156. userInfo := IC.Middleground.PowerCheckCenter.Check(in.AppId, in.MgoUserId, baseUserId, accountId, in.EntId, positionType, positionId)
  157. //是否是付费用户
  158. in.IsPay = !userInfo.Free.IsFree
  159. //默认搜索范围
  160. if in.SelectType == "" {
  161. in.SelectType = "title,content"
  162. }
  163. queryItems := util.GetQueryItems(in.SelectType, IC.C.BidSearchOldUserLimit, userInfo.Free.Registedate, in.IsPay)
  164. in.SelectType = strings.Join(queryItems, ",")
  165. // in.SearchGroup 搜索分组 搜索分组:默认0:全部;1:招标采购公告;2:超前项目
  166. // 详情页判断是否能使用超前项目 老版超级订阅、大会员、商机管理有权限
  167. if in.SearchGroup < 0 || in.SearchGroup > 2 {
  168. in.SearchGroup = 1
  169. }
  170. //信息类型
  171. if in.Subtype == "" && in.TopType == "" {
  172. //(免费用户和新版超级订阅用户 有搜索权限,但是没有查看权限)免费用户与未登录用户支持 拟建,采购意向搜索
  173. if in.SearchGroup > 0 && len(IC.C.DefaultTopTypes) >= int(in.SearchGroup) {
  174. in.Subtype = IC.C.DefaultTopTypes[in.SearchGroup-1]
  175. }
  176. }
  177. // in.SearchMode 搜索模式 搜索模式:0:精准搜索;1:模糊搜索
  178. // 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
  179. // 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
  180. if in.SearchMode < 0 || in.SearchMode > 1 {
  181. in.SearchMode = 0
  182. }
  183. // in.WordsMode 搜索关键词模式;默认0:包含所有,1:包含任意
  184. if in.WordsMode < 0 || in.WordsMode > 1 {
  185. in.WordsMode = 0
  186. }
  187. //查询时间publishTime
  188. if in.PublishTime == "" {
  189. //付费用户最新5年;免费用户||未登录用户最新1年
  190. in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-1, 0, 0).Unix(), time.Now().Unix())
  191. if in.IsPay {
  192. in.PublishTime = fmt.Sprintf("%d-%d", time.Now().AddDate(-5, 0, 0).Unix(), time.Now().Unix())
  193. }
  194. }
  195. //默认每页数据量
  196. if in.PageSize <= 0 {
  197. in.PageSize = 50
  198. }
  199. //第一页
  200. if in.PageNum <= 0 {
  201. in.PageNum = 1
  202. }
  203. count := MC.If(in.IsPay, IC.C.DefaultBidInfo.PayCount, IC.C.DefaultBidInfo.Count).(int)
  204. if in.PageNum > int64(count)/in.PageSize {
  205. in.PageNum = -1
  206. in.PageSize = -1
  207. }
  208. //行业格式化
  209. if in.Industry != "" {
  210. in.Industry = strings.TrimSpace(in.Industry)
  211. }
  212. //免费用户:高级筛选 采购单位类型、采购单位联系方式、中标企业联系方式、排除词、城市
  213. if userInfo.Free.IsFree {
  214. in.BuyerClass = ""
  215. in.BuyerTel = ""
  216. in.WinnerTel = ""
  217. in.ExclusionWords = ""
  218. in.City = ""
  219. in.ExclusionWords = ""
  220. }
  221. //判断是否有关键词
  222. if in.KeyWords != "" {
  223. //关键词处理
  224. in.KeyWords = strings.TrimSpace(in.KeyWords)
  225. in.InterceptKeyWords, in.InterceptOtherWords, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), true) // len(in.Industry) == 0
  226. }
  227. //附加词 每组附加词不能超过15个字符
  228. if in.AdditionalWords != "" {
  229. var additionalWords []string
  230. for _, ak := range strings.Split(in.AdditionalWords, ",") {
  231. if len([]rune(ak)) > 15 {
  232. additionalWords = append(additionalWords, string([]rune(ak)[:15]))
  233. } else {
  234. additionalWords = append(additionalWords, ak)
  235. }
  236. }
  237. in.AdditionalWords = strings.Join(additionalWords, ",") //分组不变
  238. }
  239. //更新关键词搜索历史记录
  240. go kws.SaveKeyWordsToHistory(in)
  241. //排除词 每组排除词不能超过15个字符
  242. if in.ExclusionWords != "" {
  243. var exclusionWords []string
  244. for _, ak := range strings.Split(in.ExclusionWords, ",") {
  245. if len([]rune(ak)) > 15 {
  246. exclusionWords = append(exclusionWords, string([]rune(ak)[:15]))
  247. } else {
  248. exclusionWords = append(exclusionWords, ak)
  249. }
  250. }
  251. in.ExclusionWords = strings.Join(exclusionWords, IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.ExclusionWords, IC.C.JYKeyMark)
  252. }
  253. return kws.GetSearchKeyWordsQueryStr(in) //格式化关键词
  254. }
  255. // GetBidSearchList 非空搜索 查询
  256. func (kws *KeyWordsSearch) GetBidSearchList(in *bxcore.SearchReq) (count, total int64, list []*bxcore.SearchList) {
  257. //排除异常in.PageNum参数
  258. count, list = service.GetBidSearchData(in, false)
  259. util.MakeCollection(in.UserId, list)
  260. total = count //返回数据总量提示信息
  261. limitCount := MC.If(in.IsPay, int64(util.SearchPageSize*util.SearchMaxPageNum_PAYED), int64(util.SearchPageSize*util.SearchMaxPageNum)).(int64)
  262. if count > limitCount {
  263. count = limitCount //付费用户count 最多5000条,100页数据,每页50条;免费用户count 最多500条,10页数据,每页50条。
  264. }
  265. return
  266. }
  267. // 聚合搜索
  268. func (kws *KeyWordsSearch) PolymerizeSearch(in *bxcore.PolymerizeSearchReq) *bxcore.SearchReturn {
  269. data := &bxcore.SearchReturn{}
  270. //powerCheck := IC.Middleground.PowerCheckCenter.Check(in.AppId, in.UserId, in.NewUserId, in.AccountId, in.EntId, in.PositionType, in.PositionId)
  271. //企业搜索
  272. now1 := time.Now().Unix()
  273. entList := &bxcore.SearchMap{}
  274. entList.Data, entList.Count = service.EntSearch(in.SearchCode)
  275. data.EntList = entList
  276. now2 := time.Now().Unix()
  277. //采购单位搜搜索
  278. procureList := &bxcore.SearchMap{}
  279. procureList.Data, procureList.Count = service.ProcureSearch(in.SearchCode)
  280. data.ProcureList = procureList
  281. now3 := time.Now().Unix()
  282. log.Println("企业查询耗时", now2-now1)
  283. log.Println("采购单位搜搜索", now3-now2)
  284. /*if in.AccountId > 0 {
  285. //菜单搜索
  286. data.MenuList = service.MenuSearch(in)
  287. //标讯搜索
  288. now4 := time.Now().Unix()
  289. subscribeList := &bxcore.SearchMap{}
  290. subscribeList.Data = service.SubscribeSearch(in.SearchCode, powerCheck)
  291. data.SubscribeList = subscribeList
  292. now4 := time.Now().Unix()
  293. log.Println("菜单搜索", now4-now3)
  294. }*/
  295. return data
  296. }