123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package common
- import (
- "encoding/json"
- "fmt"
- "leadGeneration/util"
- "log"
- "strings"
- "time"
- )
- var Analysis = []string{"<10万", "10万-50万", "50万-100万", "100万-500万", "500万-1000万", "1000万-1亿", "≥1亿"}
- func PotentialCustomizeAnalysis(userid, keyWords string) (map[string]interface{}, error) {
- mae := new(MarketAnalysisEntity)
- if key := KeyWordFormat(userid, keyWords); key != "" {
- if err := json.Unmarshal([]byte(key), &mae.FormatParam.KeysItems); err != nil {
- log.Println("关键词格式化失败")
- return nil, err
- }
- }
- var aggs []string
- aggs = append(aggs, aggs_buyerclass, buyer_procurement_scale, winner_procurement_scale)
- mae.Types = 1
- mae.FormatParam.STime = time.Now().AddDate(-1, 0, 0).Unix()
- finalSql := fmt.Sprintf(mae.GetCommonQuerySqlWithAggs(), strings.Join(aggs, ","), mae.Size, "")
- log.Println("定制化分析报告es查询:", finalSql)
- res, _, _ := util.GetAggs("projectset", "projectset", finalSql)
- if res == nil || len(res) == 0 {
- return nil, nil
- }
- thisRow := Aggregation{}
- for name, object := range res {
- bArr, err := object.MarshalJSON()
- if len(bArr) == 0 || err != nil {
- continue
- }
- if name == "project_amount" {
- if json.Unmarshal(bArr, &thisRow.ProjectAmount) != nil {
- continue
- }
- } else if name == "buyer_amount_distribution" {
- if json.Unmarshal(bArr, &thisRow.BuyerAmountDistribution) != nil {
- continue
- }
- } else if name == "winner_amount_distribution" {
- if json.Unmarshal(bArr, &thisRow.WinnerAmountDistribution) != nil {
- continue
- }
- } else if name == "buyerclass_scale" {
- if json.Unmarshal(bArr, &thisRow.BuyerclassScale) != nil {
- continue
- }
- }
- }
- rMap := make(map[string]interface{})
- CustomerDistribute(thisRow, rMap) //客户分布
- BuyerAnalysis(thisRow, rMap) //采购单位分布
- WinningAnalysis(thisRow, rMap) //中标单位分布
- return rMap, nil
- }
- // CustomerDistribute 客户分布
- func CustomerDistribute(thisRow Aggregation, rMap map[string]interface{}) {
- var data []map[string]interface{}
- for _, v := range thisRow.BuyerclassScale.Buckets {
- rM := map[string]interface{}{}
- rM["buyclass"] = v.Area
- rM["total"] = v.AreaTotal
- rM["amount"] = v.BuyclassAmount.Value
- data = append(data, rM)
- }
- rMap["customer_scale"] = data
- return
- }
- // BuyerAnalysis 采购单位分布
- func BuyerAnalysis(thisBuyerRow Aggregation, rMap map[string]interface{}) {
- type buyer struct {
- Name string `json:"key"`
- TotalAmount interface{} `json:"total_amount"`
- TotalNumber interface{} `json:"total_number"`
- }
- //采购单位-采购规模分布
- buyerMap := []interface{}{}
- //计算采购单位各区间金额
- buyerA := make(map[string]*distributionTrend)
- for _, v := range thisBuyerRow.BuyerAmountDistribution.Buckets {
- amountDistribution(v.Amount.Value, buyerA)
- }
- var count_b int
- for _, v := range buyerA {
- count_b = count_b + v.Count
- }
- for _, v := range Analysis {
- var data buyer
- data.Name = v
- if vlu, ok := buyerA[v]; ok && count_b != 0 {
- data.TotalNumber = float64(vlu.Count) / float64(count_b)
- }
- if vlu, ok := buyerA[v]; ok && thisBuyerRow.ProjectAmount.Value != 0 {
- data.TotalAmount = vlu.Amount / thisBuyerRow.ProjectAmount.Value
- }
- buyerMap = append(buyerMap, data)
- }
- rMap["buyer_time_distribution"] = buyerMap
- }
- // WinningAnalysis 中标单位分布
- func WinningAnalysis(thisWinnerRow Aggregation, rMap map[string]interface{}) {
- type s_Winner struct {
- Name string `json:"key"`
- TotalAmount interface{} `json:"total_amount"`
- TotalNumber interface{} `json:"total_number"`
- }
- //中标单位-中标规模分布
- winnerA := make(map[string]*distributionTrend)
- for _, v := range thisWinnerRow.WinnerAmountDistribution.Buckets {
- amountDistribution(v.Amount.Value, winnerA)
- }
- var count_b int
- for _, v := range winnerA {
- count_b = count_b + v.Count
- }
- buyerMap := []interface{}{}
- for _, v := range Analysis {
- var data s_Winner
- data.Name = v
- if vlu, ok := winnerA[v]; ok && count_b != 0 {
- data.TotalNumber = float64(vlu.Count) / float64(count_b)
- }
- if vlu, ok := winnerA[v]; ok && thisWinnerRow.ProjectAmount.Value != 0 {
- data.TotalAmount = vlu.Amount / thisWinnerRow.ProjectAmount.Value
- }
- buyerMap = append(buyerMap, data)
- }
- rMap["winner_time_distribution"] = buyerMap
- }
- func amountDistribution(v float64, data map[string]*distributionTrend) {
- if v <= 0 {
- return
- }
- if v < 100000 {
- if data["<10万"] == nil {
- data["<10万"] = new(distributionTrend)
- }
- data["<10万"].Amount += v
- data["<10万"].Count++
- } else if v < 500000 {
- if data["10万-50万"] == nil {
- data["10万-50万"] = new(distributionTrend)
- }
- data["10万-50万"].Amount += v
- data["10万-50万"].Count++
- } else if v < 1000000 {
- if data["50万-100万"] == nil {
- data["50万-100万"] = new(distributionTrend)
- }
- data["50万-100万"].Amount += v
- data["50万-100万"].Count++
- } else if v < 1000000*5 {
- if data["100万-500万"] == nil {
- data["100万-500万"] = new(distributionTrend)
- }
- data["100万-500万"].Amount += v
- data["100万-500万"].Count++
- } else if v < 1000000*10 {
- if data["500万-1000万"] == nil {
- data["500万-1000万"] = new(distributionTrend)
- }
- data["500万-1000万"].Amount += v
- data["500万-1000万"].Count++
- } else if v < 100000000 {
- if data["1000万-1亿"] == nil {
- data["1000万-1亿"] = new(distributionTrend)
- }
- data["1000万-1亿"].Amount += v
- data["1000万-1亿"].Count++
- } else if v >= 100000000 {
- if data["≥1亿"] == nil {
- data["≥1亿"] = new(distributionTrend)
- }
- data["≥1亿"].Amount += v
- data["≥1亿"].Count++
- }
- }
|