service.go 15 KB

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