getsearchlistlogic.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package logic
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  5. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/service"
  6. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/util"
  7. "context"
  8. "strings"
  9. "time"
  10. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/internal/svc"
  11. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/type/bxcore"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. "log"
  14. )
  15. type GetSearchListLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewGetSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSearchListLogic {
  21. return &GetSearchListLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. // GetSearchList 标讯搜索结果列表数据
  28. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  29. defer MC.Catch()
  30. res := &bxcore.SearchData{
  31. Count: 0,
  32. List: []*bxcore.SearchList{},
  33. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  34. }
  35. //初始化搜索对象
  36. ks := service.NewKeyWordsSearch()
  37. // 未登录用户只允许翻页和筛选搜索范围 其他参数无效
  38. if in.UserId == "" {
  39. in = &bxcore.SearchReq{
  40. AppId: in.AppId,
  41. PageNum: in.PageNum,
  42. PageSize: in.PageSize,
  43. SelectType: in.SelectType,
  44. KeyWords: in.KeyWords,
  45. Platform: in.Platform,
  46. UserType: in.UserType,
  47. SearchGroup: in.SearchGroup,
  48. Subtype: in.Subtype,
  49. TopType: in.TopType,
  50. }
  51. // 搜索范围只允许筛选 标题和正文 其他的过滤掉
  52. var selectType []string
  53. selectTypeSplit := strings.Split(in.SelectType, ",")
  54. // 未登录用户只能搜标题和正文Spli
  55. for i := 0; i < len(selectTypeSplit); i++ {
  56. if selectTypeSplit[i] == "title" || selectTypeSplit[i] == "content" {
  57. selectType = append(selectType, selectTypeSplit[i])
  58. }
  59. }
  60. if len(selectType) == 0 {
  61. selectType = []string{"title", "content"}
  62. }
  63. //
  64. in.SelectType = strings.Join(selectType, ",")
  65. }
  66. isWhite, _ := service.IsOnTheWhitelist(in.UserId, in.MgoUserId)
  67. //模糊检索数重新格式化
  68. nin := &bxcore.SearchReq{
  69. AppId: in.AppId,
  70. UserId: in.UserId,
  71. Phone: in.Phone,
  72. NewUserId: in.NewUserId,
  73. EntId: in.EntId,
  74. EntUserId: in.EntUserId,
  75. PageNum: in.PageNum,
  76. PageSize: in.PageSize,
  77. Province: in.Province,
  78. City: in.City,
  79. Subtype: in.Subtype,
  80. TopType: in.TopType,
  81. PublishTime: in.PublishTime,
  82. SelectType: in.SelectType,
  83. Price: in.Price,
  84. Industry: in.Industry,
  85. BuyerClass: in.BuyerClass,
  86. BuyerTel: in.BuyerTel,
  87. WinnerTel: in.WinnerTel,
  88. FileExists: in.FileExists,
  89. SearchGroup: in.SearchGroup,
  90. SearchMode: 1,
  91. WordsMode: in.WordsMode,
  92. KeyWords: in.KeyWords,
  93. AdditionalWords: in.AdditionalWords,
  94. ExclusionWords: in.ExclusionWords,
  95. UserType: in.UserType,
  96. Platform: in.Platform,
  97. BidField: in.BidField,
  98. PositionType: in.PositionType,
  99. PositionId: in.PositionId,
  100. AccountId: in.AccountId,
  101. MgoUserId: in.MgoUserId,
  102. LimitFlag: in.LimitFlag,
  103. IsNew: in.IsNew,
  104. District: in.District,
  105. Buyer: in.Buyer,
  106. Winner: in.Winner,
  107. Agency: in.Agency,
  108. IsFuzzyNumber: true,
  109. PropertyForm: in.PropertyForm,
  110. ExpireTime: in.ExpireTime,
  111. SubInformation: in.SubInformation,
  112. Period: in.Period,
  113. ChangeHand: in.ChangeHand,
  114. Scale: in.Scale,
  115. MobileTag: in.MobileTag,
  116. EntAccountId: in.EntAccountId,
  117. UserAgent: in.UserAgent,
  118. }
  119. //处理搜索条件
  120. heightWords := ks.SearchParamsHandle(in, isWhite)
  121. if (in.PageNum < 0 && in.PageSize < 0) || ks.IsSearch == "F" { //没有权限
  122. return &bxcore.SearchResp{
  123. Data: res,
  124. ErrMsg: "",
  125. ErrCode: 0,
  126. }, nil
  127. }
  128. //判断是否是空搜索,如果是空搜索,查缓存数据
  129. if ks.IsEmptySearch(in, isWhite) {
  130. log.Println("空查询分组", in.SearchGroup)
  131. res.List, res.Count, res.Total = ks.GetBidSearchListByCache(in)
  132. return &bxcore.SearchResp{
  133. Data: res,
  134. ErrMsg: "",
  135. ErrCode: 0,
  136. }, nil
  137. }
  138. //异常付费用户 参数不是免费fType,但是又不是付费用户;还有一种是未登录用户 稍后处理
  139. if in.UserType != "fType" && !in.IsPay {
  140. return &bxcore.SearchResp{
  141. ErrCode: -1,
  142. ErrMsg: "无权限",
  143. }, nil
  144. }
  145. t := time.Now()
  146. //招标信息有效查询
  147. res.IsLimit = 1
  148. //查询数据
  149. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  150. //全文检索限制
  151. if searchLimit {
  152. var warnMsg string
  153. //res.IsLimit = util.IsLimited(in.LimitFlag, in.UserId, in.UserType != "fType", in.IsNew)
  154. res.IsLimit, warnMsg = util.CheckLimit(util.UserIdentMap[func(userId string, isPay bool) string {
  155. switch {
  156. case isPay:
  157. return "payer"
  158. case userId != "":
  159. return "free"
  160. default:
  161. return "noLogin"
  162. }
  163. }(in.UserId, in.IsPay)], in.LimitFlag, in.IsNew)
  164. if res.IsLimit == 1 { //没有被限制
  165. defer util.Limit()
  166. }
  167. if warnMsg != "" {
  168. util.SendMsgByWXURL(warnMsg)
  169. }
  170. }
  171. //无限制
  172. if res.IsLimit == 1 {
  173. //付费用户搜索优化--默认搜索5年数据,数据量太多,接口反应太慢,前两页数据 时间范围根据配置缩小查询以达到快速查询的目的。
  174. publishTime := in.PublishTime
  175. if b := util.IsOptimize(IC.C, in); b {
  176. t1 := time.Now()
  177. res.Count, res.Total, res.List = ks.GetBidSearchList(in) // util.GetBidSearchData(in)
  178. log.Println("1查询耗时:", time.Since(t1))
  179. }
  180. //如果优化查询数据量太少,和配置数据量作比较,不够的话走原始查询
  181. if res.Count < IC.C.PaySearchLimit.PageSize {
  182. t2 := time.Now()
  183. in.PublishTime = publishTime
  184. res.Count, res.Total, res.List = ks.GetBidSearchList(in)
  185. log.Println("2查询耗时:", time.Since(t2))
  186. }
  187. // 精准模式不足50条的非空标讯查询 匹配模糊查询数
  188. ikWords := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr)
  189. log.Println(ikWords)
  190. var isReset bool
  191. if res.Count < IC.C.PaySearchLimit.PrecisionNum && in.SearchMode == 0 && in.KeyWords != "" {
  192. isReset = true
  193. ks.SearchParamsHandle(nin, isWhite)
  194. res.BCount = service.GetBidSearchCount(nin)
  195. log.Println("精准查询数据不足 匹配模糊查询数:", res.BCount)
  196. }
  197. //4.3.新增:“项目名称/标的物”搜索结果广告
  198. if (in.UserType == "fType" || !in.IsPay) && res.Count > IC.C.SearchLimit {
  199. switch isReset {
  200. case true:
  201. nin.SelectType = "purchasing,projectname.pname"
  202. case false:
  203. nin.SelectType = "ppa"
  204. ks.SearchParamsHandle(nin, isWhite)
  205. }
  206. nin.PageSize, nin.PageNum = 1, 1
  207. _, total, data := ks.GetBidSearchList(nin)
  208. if total > IC.C.SubjectLimit {
  209. res.SCount, res.SList = total, data
  210. }
  211. }
  212. res.KeyWords = strings.Join(heightWords, " ")
  213. res.InterceptOtherWords = in.InterceptOtherWords
  214. res.InterceptKeyWords = in.InterceptKeyWords
  215. }
  216. log.Println("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  217. return &bxcore.SearchResp{
  218. Data: res,
  219. ErrMsg: "",
  220. ErrCode: 0,
  221. }, nil
  222. }