service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. elastic "app.yhyue.com/moapp/jybase/es"
  7. "fmt"
  8. "jyBXBuyer/rpc/type/bxbuyer"
  9. "log"
  10. "math/rand"
  11. "time"
  12. )
  13. func GetRelatesInfo(in *bxbuyer.RelatesInformationReq) *bxbuyer.RelatesInformationResp {
  14. relatesInfo := &bxbuyer.RelatesInformationResp{Data: &bxbuyer.RelatesInformation{BiddingList: []*bxbuyer.InfoList{}, BuyerList: []*bxbuyer.InfoList{}}}
  15. if err := Checking(in); err != nil {
  16. relatesInfo.ErrCode = -1
  17. relatesInfo.ErrMsg = err.Error()
  18. return relatesInfo
  19. }
  20. query := `{"query": {"bool": {"must": [{"exists": {"field": "name"}}%s%s}]}}%s%s}`
  21. //获取随机数
  22. // 使用当前时间作为随机数生成器的种子
  23. randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
  24. // 获取一个范围在 [0, 100) 的随机整数
  25. start := randGen.Intn(100)
  26. //关联采购单位:同省份采购单位名称随机展示30个
  27. if in.Area != "" {
  28. in.Area = fmt.Sprintf(`,{"term": {"province": "%s"}`, in.Area)
  29. }
  30. //
  31. if in.City != "" {
  32. in.City = fmt.Sprintf(`,{"term": {"city": "%s"}}`, in.City)
  33. }
  34. countQuery := fmt.Sprintf(query, in.Area, in.City, "", "")
  35. if c := elastic.Count("buyer", "buyer", countQuery); c > 0 {
  36. //数据量不够30
  37. if c-in.BuyerCount < int64(start) {
  38. start = common.If(c-in.BuyerCount > 0, int(c-in.BuyerCount), 0).(int)
  39. }
  40. fromQuery := fmt.Sprintf(`,"from": %d`, start)
  41. sizeQuery := fmt.Sprintf(`,"size": %d`, in.BuyerCount)
  42. buyerQuery := fmt.Sprintf(query, in.Area, in.City, fromQuery, sizeQuery)
  43. log.Println("buyerQuery:", buyerQuery)
  44. buyerList := elastic.Get("buyer", "buyer", buyerQuery)
  45. if len(*buyerList) > 0 {
  46. log.Println("--------:", len(*buyerList))
  47. var buyers []*bxbuyer.InfoList
  48. for _, b := range *buyerList {
  49. buyers = append(buyers, &bxbuyer.InfoList{
  50. Name: common.ObjToString(b["name"]),
  51. Id: encrypt.EncodeArticleId2ByCheck(common.ObjToString(b["_id"])),
  52. })
  53. }
  54. relatesInfo.Data.BuyerList = buyers
  55. }
  56. }
  57. //关联动态:采购单位标讯动态
  58. if in.Buyer != "" {
  59. biddingQuery := fmt.Sprintf(`{"query":{"bool":{"must":[{"term":{"buyer":"%s"}}],"must_not":[{"terms":{"subtype":["采购意向","拟建"]}}]}},"sort": [{"publishtime": "desc"}],"from":0,"size":%d }`, in.Buyer, in.BuyerCount)
  60. biddingList := elastic.Get("bidding", "bidding", biddingQuery)
  61. if len(*biddingList) > 0 {
  62. var biddingInfos []*bxbuyer.InfoList
  63. for _, b := range *biddingList {
  64. publishtime := common.Int64All(b["publishtime"])
  65. biddingInfos = append(biddingInfos, &bxbuyer.InfoList{
  66. Name: common.ObjToString(b["title"]),
  67. Id: encrypt.EncodeArticleId2ByCheck(common.ObjToString(b["id"])),
  68. PublishTime: date.FormatDateByInt64(&publishtime, date.Date_Small_Layout),
  69. })
  70. }
  71. relatesInfo.Data.BiddingList = biddingInfos
  72. }
  73. }
  74. log.Println("relatesinfo:", relatesInfo)
  75. return relatesInfo
  76. }
  77. func Checking(in *bxbuyer.RelatesInformationReq) error {
  78. if in.Buyer == "" && in.Area == "" {
  79. return fmt.Errorf("参数异常")
  80. }
  81. //默认30个采购单位
  82. if in.BuyerCount == 0 {
  83. in.BuyerCount = 30
  84. }
  85. //默认10条招标信息
  86. if in.BiddingCount == 0 {
  87. in.BiddingCount = 10
  88. }
  89. return nil
  90. }