search.go 12 KB

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