getsearchlistlogic.go 8.2 KB

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