customizedAnalysis.go 5.1 KB

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