getsearchlistlogic.go 8.5 KB

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