customizedAnalysis.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package common
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "leadGeneration/util"
  6. "log"
  7. "strings"
  8. "time"
  9. )
  10. var Analysis = []string{"<10万", "10万-50万", "50万-100万", "100万-500万", "500万-1000万", "1000万-1亿", "≥1亿"}
  11. func PotentialCustomizeAnalysis(userid, keyWords string) (map[string]interface{}, error) {
  12. mae := new(MarketAnalysisEntity)
  13. if key := KeyWordFormat(userid, keyWords); key != "" {
  14. if err := json.Unmarshal([]byte(key), &mae.FormatParam.KeysItems); err != nil {
  15. log.Println("关键词格式化失败")
  16. return nil, err
  17. }
  18. }
  19. var aggs []string
  20. aggs = append(aggs, aggs_buyerclass, buyer_procurement_scale, winner_procurement_scale)
  21. mae.Types = 1
  22. mae.FormatParam.STime = time.Now().AddDate(-1, 0, 0).Unix()
  23. finalSql := fmt.Sprintf(mae.GetCommonQuerySqlWithAggs(), strings.Join(aggs, ","), mae.Size, "")
  24. log.Println("定制化分析报告es查询:", finalSql)
  25. res, _, _ := util.GetAggs("projectset", "projectset", finalSql)
  26. if res == nil || len(res) == 0 {
  27. return nil, nil
  28. }
  29. thisRow := Aggregation{}
  30. for name, object := range res {
  31. bArr, err := object.MarshalJSON()
  32. if len(bArr) == 0 || err != nil {
  33. continue
  34. }
  35. if name == "project_amount" {
  36. if json.Unmarshal(bArr, &thisRow.ProjectAmount) != nil {
  37. continue
  38. }
  39. } else if name == "buyer_amount_distribution" {
  40. if json.Unmarshal(bArr, &thisRow.BuyerAmountDistribution) != nil {
  41. continue
  42. }
  43. } else if name == "winner_amount_distribution" {
  44. if json.Unmarshal(bArr, &thisRow.WinnerAmountDistribution) != nil {
  45. continue
  46. }
  47. } else if name == "buyerclass_scale" {
  48. if json.Unmarshal(bArr, &thisRow.BuyerclassScale) != nil {
  49. continue
  50. }
  51. }
  52. }
  53. rMap := make(map[string]interface{})
  54. CustomerDistribute(thisRow, rMap) //客户分布
  55. BuyerAnalysis(thisRow, rMap) //采购单位分布
  56. WinningAnalysis(thisRow, rMap) //中标单位分布
  57. return rMap, nil
  58. }
  59. // CustomerDistribute 客户分布
  60. func CustomerDistribute(thisRow Aggregation, rMap map[string]interface{}) {
  61. var data []map[string]interface{}
  62. for _, v := range thisRow.BuyerclassScale.Buckets {
  63. rM := map[string]interface{}{}
  64. rM["buyclass"] = v.Area
  65. rM["total"] = v.AreaTotal
  66. rM["amount"] = v.BuyclassAmount.Value
  67. data = append(data, rM)
  68. }
  69. rMap["customer_scale"] = data
  70. return
  71. }
  72. // BuyerAnalysis 采购单位分布
  73. func BuyerAnalysis(thisBuyerRow Aggregation, rMap map[string]interface{}) {
  74. type buyer struct {
  75. Name string `json:"key"`
  76. TotalAmount interface{} `json:"total_amount"`
  77. TotalNumber interface{} `json:"total_number"`
  78. }
  79. //采购单位-采购规模分布
  80. buyerMap := []interface{}{}
  81. //计算采购单位各区间金额
  82. buyerA := make(map[string]*distributionTrend)
  83. for _, v := range thisBuyerRow.BuyerAmountDistribution.Buckets {
  84. amountDistribution(v.Amount.Value, buyerA)
  85. }
  86. var count_b int
  87. for _, v := range buyerA {
  88. count_b = count_b + v.Count
  89. }
  90. for _, v := range Analysis {
  91. var data buyer
  92. data.Name = v
  93. if vlu, ok := buyerA[v]; ok && count_b != 0 {
  94. data.TotalNumber = float64(vlu.Count) / float64(count_b)
  95. }
  96. if vlu, ok := buyerA[v]; ok && thisBuyerRow.ProjectAmount.Value != 0 {
  97. data.TotalAmount = vlu.Amount / thisBuyerRow.ProjectAmount.Value
  98. }
  99. buyerMap = append(buyerMap, data)
  100. }
  101. rMap["buyer_time_distribution"] = buyerMap
  102. }
  103. // WinningAnalysis 中标单位分布
  104. func WinningAnalysis(thisWinnerRow Aggregation, rMap map[string]interface{}) {
  105. type s_Winner struct {
  106. Name string `json:"key"`
  107. TotalAmount interface{} `json:"total_amount"`
  108. TotalNumber interface{} `json:"total_number"`
  109. }
  110. //中标单位-中标规模分布
  111. winnerA := make(map[string]*distributionTrend)
  112. for _, v := range thisWinnerRow.WinnerAmountDistribution.Buckets {
  113. amountDistribution(v.Amount.Value, winnerA)
  114. }
  115. var count_b int
  116. for _, v := range winnerA {
  117. count_b = count_b + v.Count
  118. }
  119. buyerMap := []interface{}{}
  120. for _, v := range Analysis {
  121. var data s_Winner
  122. data.Name = v
  123. if vlu, ok := winnerA[v]; ok && count_b != 0 {
  124. data.TotalNumber = float64(vlu.Count) / float64(count_b)
  125. }
  126. if vlu, ok := winnerA[v]; ok && thisWinnerRow.ProjectAmount.Value != 0 {
  127. data.TotalAmount = vlu.Amount / thisWinnerRow.ProjectAmount.Value
  128. }
  129. buyerMap = append(buyerMap, data)
  130. }
  131. rMap["winner_time_distribution"] = buyerMap
  132. }
  133. func amountDistribution(v float64, data map[string]*distributionTrend) {
  134. if v <= 0 {
  135. return
  136. }
  137. if v < 100000 {
  138. if data["<10万"] == nil {
  139. data["<10万"] = new(distributionTrend)
  140. }
  141. data["<10万"].Amount += v
  142. data["<10万"].Count++
  143. } else if v < 500000 {
  144. if data["10万-50万"] == nil {
  145. data["10万-50万"] = new(distributionTrend)
  146. }
  147. data["10万-50万"].Amount += v
  148. data["10万-50万"].Count++
  149. } else if v < 1000000 {
  150. if data["50万-100万"] == nil {
  151. data["50万-100万"] = new(distributionTrend)
  152. }
  153. data["50万-100万"].Amount += v
  154. data["50万-100万"].Count++
  155. } else if v < 1000000*5 {
  156. if data["100万-500万"] == nil {
  157. data["100万-500万"] = new(distributionTrend)
  158. }
  159. data["100万-500万"].Amount += v
  160. data["100万-500万"].Count++
  161. } else if v < 1000000*10 {
  162. if data["500万-1000万"] == nil {
  163. data["500万-1000万"] = new(distributionTrend)
  164. }
  165. data["500万-1000万"].Amount += v
  166. data["500万-1000万"].Count++
  167. } else if v < 100000000 {
  168. if data["1000万-1亿"] == nil {
  169. data["1000万-1亿"] = new(distributionTrend)
  170. }
  171. data["1000万-1亿"].Amount += v
  172. data["1000万-1亿"].Count++
  173. } else if v >= 100000000 {
  174. if data["≥1亿"] == nil {
  175. data["≥1亿"] = new(distributionTrend)
  176. }
  177. data["≥1亿"].Amount += v
  178. data["≥1亿"].Count++
  179. }
  180. }