newestBidding.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package model
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. ME "app.yhyue.com/moapp/jybase/encrypt"
  5. elastic "app.yhyue.com/moapp/jybase/esv1"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. "fmt"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "jyBXBase/rpc/bxbase"
  11. IC "jyBXBase/rpc/init"
  12. "strings"
  13. "time"
  14. )
  15. const (
  16. search_index = "bidding"
  17. search_type = "bidding"
  18. mongodb_fields = `{"_id":1,"area":1,"publishtime":1,"s_subscopeclass":1,"subtype":1,"title":1,"toptype":1,"type":1, "buyerclass":1,"budget":1,"bidamount":1,"s_winner":1,"bidopentime":1,"buyer":1,"projectname":1,"spidercode":1,"site":1}`
  19. query = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","spidercode","site"],"from":0,"size":%d}`
  20. multi_match = `{"multi_match": {"query": %s,"type": "phrase", "fields": ["title"]}}`
  21. query_bool_must = `{"terms":{"%s":[%s]}}`
  22. query_bool_must_and = `{"bool":{"must":[%s],"must_not":[%s]}}`
  23. search_field = `"_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","spidercode","site"`
  24. query_city_hkeys = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": 1}},"highlight": {"pre_tags": ["<a>"],"post_tags": ["</a>"],"fields": {"title": {"fragment_size": 0,"number_of_fragments": 1}}},"_source":[` + search_field + `],"sort":[{"publishtime":"desc"},{"budget":"desc"}],"from":0,"size":20}`
  25. )
  26. type NewestInfo struct {
  27. TableName string
  28. UserId string
  29. }
  30. var mysqlTables = map[string]string{
  31. "f": "pushsubscribe",
  32. "v": "pushsubscribe",
  33. "m": "pushmember",
  34. "e": "pushentniche",
  35. }
  36. func GetNewestInfo(userId, userType string) *NewestInfo {
  37. return &NewestInfo{
  38. UserId: userId,
  39. TableName: mysqlTables[userType],
  40. }
  41. }
  42. func (n *NewestInfo) GetPushHistory() (res []*bxbase.NewestList) {
  43. findSQL := "select infoid,matchkeys,attachment_count,budget,bidamount from " + n.TableName + " where userid = '" + n.UserId + "' and date >= %d order by id desc limit 50"
  44. list := &[]map[string]interface{}{}
  45. if n.TableName == "pushmember" { //大会员
  46. list = IC.BigmemberPushMysql.SelectBySql(fmt.Sprintf(findSQL, time.Now().AddDate(0, 0, -7).Unix()))
  47. } else if n.TableName == "pushentniche" { //商机管理
  48. list = IC.EntnichePushMysql.SelectBySql(fmt.Sprintf(findSQL, time.Now().AddDate(0, 0, -7).Unix()))
  49. } else {
  50. list = IC.PushMysql.SelectBySql(fmt.Sprintf(findSQL, time.Now().AddDate(0, 0, -7).Unix()))
  51. }
  52. if len(*list) > 0 {
  53. m := map[string]bool{}
  54. es_ids := []string{}
  55. infos := map[string]*bxbase.NewestList{}
  56. for _, v := range *list {
  57. infoId := MC.ObjToString(v["infoid"])
  58. if m[infoId] {
  59. continue
  60. }
  61. es_ids = append(es_ids, infoId)
  62. m[MC.ObjToString(v["infoid"])] = true
  63. //
  64. infos[infoId] = &bxbase.NewestList{
  65. Id: ME.EncodeArticleId2ByCheck(MC.ObjToString(v["infoid"])),
  66. Matchkeys: MC.ObjToString(v["matchkeys"]),
  67. Budget: MC.Int64All(v["budget"]),
  68. Bidamount: MC.Int64All(v["bidamount"]),
  69. FileExists: MC.Int64All(v["attachment_count"]) > 0,
  70. }
  71. }
  72. if len(es_ids) > 0 {
  73. list := elastic.Get(search_index, search_type, fmt.Sprintf(query, strings.Join(es_ids, `","`), len(es_ids)))
  74. if list != nil {
  75. for _, v := range *list {
  76. _id := MC.ObjToString(v["_id"])
  77. bn := infos[_id]
  78. bn.Title = MC.ObjToString(v["title"])
  79. bn.PublishTime = MC.Int64All(v["publishtime"])
  80. bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
  81. bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
  82. bn.Buyerclass = MC.ObjToString(v["buyerclass"])
  83. bn.City = MC.ObjToString(v["city"])
  84. bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
  85. bn.SpiderCode = MC.ObjToString(v["spidercode"])
  86. bn.Site = MC.ObjToString(v["site"])
  87. }
  88. }
  89. }
  90. //mongodb bidding
  91. mgo_ids := []primitive.ObjectID{}
  92. for _, v := range es_ids {
  93. if infos[v] == nil {
  94. _id, _ := primitive.ObjectIDFromHex(v)
  95. mgo_ids = append(mgo_ids, _id)
  96. }
  97. }
  98. if len(mgo_ids) > 0 {
  99. list, ok := IC.MgoBidding.Find("bidding", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_ids}}, nil, mongodb_fields, false, -1, -1)
  100. if ok && *list != nil {
  101. for _, v := range *list {
  102. _id := mongodb.BsonIdToSId(v["_id"])
  103. bn := infos[_id]
  104. bn.Title = MC.ObjToString(v["title"])
  105. bn.PublishTime = MC.Int64All(v["publishtime"])
  106. bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
  107. bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
  108. bn.Buyerclass = MC.ObjToString(v["buyerclass"])
  109. bn.City = MC.ObjToString(v["city"])
  110. bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
  111. bn.SpiderCode = MC.ObjToString(v["spidercode"])
  112. bn.Site = MC.ObjToString(v["site"])
  113. }
  114. }
  115. }
  116. //mongodb bidding_back
  117. mgo_back_ids := []primitive.ObjectID{}
  118. for _, v := range mgo_ids {
  119. if infos[mongodb.BsonIdToSId(v)] == nil {
  120. mgo_back_ids = append(mgo_back_ids, v)
  121. }
  122. }
  123. if len(mgo_back_ids) > 0 {
  124. list, ok := IC.MgoBidding.Find("bidding_back", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_back_ids}}, nil, mongodb_fields, false, -1, -1)
  125. if ok && *list != nil {
  126. for _, v := range *list {
  127. _id := mongodb.BsonIdToSId(v["_id"])
  128. bn := infos[_id]
  129. bn.Title = MC.ObjToString(v["title"])
  130. bn.PublishTime = MC.Int64All(v["publishtime"])
  131. bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
  132. bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
  133. bn.Buyerclass = MC.ObjToString(v["buyerclass"])
  134. bn.City = MC.ObjToString(v["city"])
  135. bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
  136. bn.SpiderCode = MC.ObjToString(v["spidercode"])
  137. bn.Site = MC.ObjToString(v["site"])
  138. }
  139. }
  140. }
  141. //
  142. for _, v := range infos {
  143. res = append(res, v)
  144. }
  145. }
  146. return
  147. }
  148. //根据定位或者搜索历史 查es
  149. func NewestQuery(city, keys string) (str string) {
  150. var musts, bools []string
  151. if keys != "" {
  152. for _, v := range strings.Split(keys, ",") {
  153. keys := strings.Split(v, " ") //历史搜索 空格划分
  154. must_tmp := []string{}
  155. for _, key := range keys {
  156. must_tmp = append(must_tmp, fmt.Sprintf(multi_match, "\""+key+"\""))
  157. }
  158. bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(must_tmp, ","), ""))
  159. }
  160. }
  161. if city != "" {
  162. musts = append(musts, fmt.Sprintf(query_bool_must, "city", `"`+city+`"`))
  163. }
  164. str = fmt.Sprintf(query_city_hkeys, strings.Join(musts, ","), strings.Join(bools, ","))
  165. logx.Info("str:", str)
  166. return
  167. }
  168. //es查询
  169. func NewestES(doSearchStr string) (res []*bxbase.NewestList) {
  170. list := elastic.Get(search_index, search_type, doSearchStr)
  171. if list != nil && len(*list) > 0 {
  172. for _, v := range *list {
  173. _id := mongodb.BsonIdToSId(v["_id"])
  174. res = append(res, &bxbase.NewestList{
  175. Id: ME.EncodeArticleId2ByCheck(_id),
  176. Title: MC.ObjToString(v["title"]),
  177. Subtype: MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string),
  178. Area: MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string),
  179. Buyerclass: MC.ObjToString(v["buyerclass"]),
  180. City: MC.ObjToString(v["city"]),
  181. Industry: MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string),
  182. Budget: MC.Int64All(v["budget"]),
  183. Bidamount: MC.Int64All(v["bidamount"]),
  184. FileExists: v["filetext"] != nil,
  185. PublishTime: MC.Int64All(v["publishtime"]),
  186. Site: MC.ObjToString(v["site"]),
  187. SpiderCode: MC.ObjToString(v["spidercode"]),
  188. })
  189. }
  190. }
  191. return
  192. }