advancedProject.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package common
  2. import (
  3. qutil "app.yhyue.com/moapp/jybase/common"
  4. "encoding/json"
  5. "fmt"
  6. "leadGeneration/public"
  7. "leadGeneration/util"
  8. "log"
  9. "regexp"
  10. "strings"
  11. "time"
  12. )
  13. func AdvancedProject(userid, keyWords string) (map[string]interface{}, error) {
  14. mae := new(MarketAnalysisEntity)
  15. if key := KeyWordFormat(userid, keyWords); key != "" {
  16. if err := json.Unmarshal([]byte(key), &mae.FormatParam.KeysItems); err != nil {
  17. log.Println("关键词格式化失败")
  18. return nil, err
  19. }
  20. }
  21. mae.Types = 2
  22. mae.FormatParam.SubType = []string{"拟建", "采购意向"}
  23. mae.Size = 2
  24. mae.FormatParam.STime = time.Now().AddDate(0, -3, 0).Unix()
  25. finalSql := fmt.Sprintf(mae.GetCommonQuerySqlWithAggs(), projectsNumber, mae.Size, projectsSort)
  26. log.Println("超前项目es查询:", finalSql)
  27. res, _, data := util.GetAggs("bidding", "bidding", finalSql)
  28. if res == nil || len(res) == 0 || data == nil || len(data) == 0 {
  29. return nil, nil
  30. }
  31. var thisRow SuperProjects
  32. for name, object := range res {
  33. bArr, err := object.MarshalJSON()
  34. if len(bArr) == 0 || err != nil {
  35. continue
  36. }
  37. if name == "projects_number" {
  38. if json.Unmarshal(bArr, &thisRow.ProjectsNumber) != nil {
  39. continue
  40. }
  41. }
  42. }
  43. redisData := make(map[string]interface{})
  44. for _, v := range thisRow.ProjectsNumber.Buckets {
  45. if v.DocCount == 0 {
  46. continue
  47. }
  48. redisData[v.Key] = v.DocCount
  49. }
  50. if len(redisData) == 0 {
  51. return nil, nil
  52. }
  53. resData := make([]map[string]interface{}, len(data))
  54. for i, v := range data {
  55. if json.Unmarshal(*v.Source, &resData[i]) != nil {
  56. continue
  57. }
  58. }
  59. log.Println("AdvancedProject redis data :", resData)
  60. return map[string]interface{}{
  61. "projectTop2": resData,
  62. "projectCount": resData,
  63. }, nil
  64. }
  65. func KeyWordFormat(userid, keyWords string) string {
  66. var (
  67. arryMap []map[string]interface{}
  68. key string
  69. )
  70. if keyWords != "" {
  71. var aItems []map[string]interface{}
  72. for _, v := range strings.Split(keyWords, ",") {
  73. arryMap = append(arryMap, map[string]interface{}{"from": 1, "appendkey": nil, "key": processKeyword(v), "notkey": nil, "updatetime": time.Now().Unix()})
  74. }
  75. item := map[string]interface{}{
  76. "a_key": arryMap,
  77. "s_item": "未分类",
  78. "updatetime": time.Now().Unix(),
  79. }
  80. aItems = append(aItems, item)
  81. dataType, _ := json.Marshal(aItems)
  82. key = string(dataType)
  83. } else {
  84. data, ok := public.MQFW.FindById("user", userid, `{"o_jy":1,"o_vipjy":1,"o_member_jy":1,"i_vip_status":1,"i_member_status":1,"s_phone":1,"s_m_phone":1}`)
  85. if ok && data != nil && len(*data) > 0 {
  86. oJy, _ := (*data)["o_jy"].(map[string]interface{}) //免费用户关键词
  87. vipJy, _ := (*data)["o_vipjy"].(map[string]interface{}) //超级订阅关键词
  88. memberJy, _ := (*data)["o_member_jy"].(map[string]interface{}) //大会员关键词
  89. var aItems []interface{}
  90. if qutil.IntAll((*data)["i_member_status"]) > 0 {
  91. aItems, _ = memberJy["a_items"].([]interface{})
  92. } else if qutil.IntAll((*data)["i_vip_status"]) > 0 {
  93. aItems, _ = vipJy["a_items"].([]interface{})
  94. } else {
  95. if oJy["a_key"] != nil && len(oJy["a_key"].([]interface{})) > 0 {
  96. a_key, _ := oJy["a_key"].([]interface{})
  97. for _, v := range a_key {
  98. v1, _ := v.(map[string]interface{})
  99. arryMap = append(arryMap, map[string]interface{}{"from": 1, "appendkey": nil, "key": processKeyword(qutil.InterfaceToStr(v1["key"])), "notkey": nil, "updatetime": time.Now().Unix()})
  100. }
  101. item := map[string]interface{}{
  102. "a_key": arryMap,
  103. "s_item": "未分类",
  104. "updatetime": time.Now().Unix(),
  105. }
  106. aItems = append(aItems, item)
  107. }
  108. }
  109. if len(aItems) > 0 {
  110. dataType, _ := json.Marshal(aItems)
  111. key = string(dataType)
  112. }
  113. }
  114. }
  115. return key
  116. }
  117. // 处理订阅的关键词
  118. func processKeyword(keyword string) []string {
  119. keywordReg := regexp.MustCompile("([\\s\u3000\u2003\u00a0+,,])+")
  120. spaceReg := regexp.MustCompile("\\s+")
  121. keyword = keywordReg.ReplaceAllString(keyword, " ")
  122. keyword = spaceReg.ReplaceAllString(keyword, " ")
  123. keyword = strings.Trim(keyword, " ")
  124. if keyword == "" {
  125. return nil
  126. }
  127. return strings.Split(keyword, " ")
  128. }