newestBidding.go 8.8 KB

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