|
@@ -19,9 +19,14 @@ const (
|
|
|
)
|
|
|
|
|
|
type scaleRefineData struct {
|
|
|
- Data []*scaleRefineRow
|
|
|
- Total simpleTotal
|
|
|
- Amount simpleSum
|
|
|
+ Data []*scaleRefineRow
|
|
|
+ Total simpleTotal
|
|
|
+ Amount simpleSum
|
|
|
+ IdSwitch map[string]string
|
|
|
+ ReturnData struct {
|
|
|
+ Overall []map[string]interface{}
|
|
|
+ TotalTop, AmountTop []*returnItem
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
type simpleTotal struct {
|
|
@@ -39,18 +44,16 @@ type scaleRefineRow struct {
|
|
|
AmountProp float64 `json:"amount_prop"`
|
|
|
WinnerTotalTop struct {
|
|
|
Buckets []*struct {
|
|
|
- EntId string `json:"key"`
|
|
|
- TopMsg topMsgStruct `json:"topMsg"`
|
|
|
- EntName string `json:"entName"`
|
|
|
- Total int64 `json:"doc_count"`
|
|
|
- Prop float64 `json:"prop"`
|
|
|
+ EntId string `json:"key"`
|
|
|
+ EntName string `json:"entName"`
|
|
|
+ Total int64 `json:"doc_count"`
|
|
|
+ Prop float64 `json:"prop"`
|
|
|
} `json:"buckets"`
|
|
|
} `json:"winner_total_top"`
|
|
|
WinnerAmountTop struct {
|
|
|
Buckets []*struct {
|
|
|
- EntId string `json:"key"`
|
|
|
- EntName string `json:"entName"`
|
|
|
- TopMsg topMsgStruct `json:"topMsg"`
|
|
|
+ EntId string `json:"key"`
|
|
|
+ EntName string `json:"entName"`
|
|
|
Amount struct {
|
|
|
Value float64 `json:"value"`
|
|
|
} `json:"refine_winner_amount"`
|
|
@@ -61,43 +64,18 @@ type scaleRefineRow struct {
|
|
|
UpdateTime int64 `json:"updateTime"`
|
|
|
}
|
|
|
|
|
|
-type topMsgStruct struct {
|
|
|
- Hits struct {
|
|
|
- Total int `json:"total"`
|
|
|
- MaxScore float64 `json:"max_score"`
|
|
|
- Hits []struct {
|
|
|
- Source struct {
|
|
|
- EntIdList []string `json:"entidlist"`
|
|
|
- WinnerName string `json:"s_winner"`
|
|
|
- } `json:"_source"`
|
|
|
- } `json:"hits"`
|
|
|
- } `json:"hits"`
|
|
|
+type returnItem struct {
|
|
|
+ Name string `json:"name"`
|
|
|
+ Value interface{} `json:"value"`
|
|
|
+ Prop interface{} `json:"prop"`
|
|
|
+ TopList []*entValue `json:"topList"`
|
|
|
}
|
|
|
|
|
|
-//getReallyName 根据企业id或企业名称
|
|
|
-func (tms topMsgStruct) getReallyName(entId string) string {
|
|
|
- for _, rs := range tms.Hits.Hits {
|
|
|
- //仅只有一个中标企业
|
|
|
- if len(rs.Source.EntIdList) == 1 {
|
|
|
- if rs.Source.EntIdList[0] == entId {
|
|
|
- return rs.Source.WinnerName
|
|
|
- }
|
|
|
- } else { //多个中标企业,根据顺序返回对应名称
|
|
|
- flag := -1
|
|
|
- for index, tId := range rs.Source.EntIdList {
|
|
|
- if tId == entId {
|
|
|
- flag = index
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- if flag > -1 {
|
|
|
- if names := strings.Split(rs.Source.WinnerName, ","); len(names)-1 >= flag {
|
|
|
- return names[flag]
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return ""
|
|
|
+type entValue struct {
|
|
|
+ Id string `json:"id"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ Value interface{} `json:"value"`
|
|
|
+ Prop interface{} `json:"prop"`
|
|
|
}
|
|
|
|
|
|
//marketScaleRefineQuery 细化聚合
|
|
@@ -116,18 +94,18 @@ func (mae *MarketAnalysisEntity) marketScaleRefineQuery() (rMap map[string]inter
|
|
|
}
|
|
|
}
|
|
|
if len(bools) > 0 {
|
|
|
- aggsGroup = append(aggsGroup, fmt.Sprintf(`"%s":{"filter":{"query":{"bool":{"should":[%s],"minimum_should_match": 1}}},"aggs":{"project_count":{"filter":{}},"project_amount":{"sum":{"field":"sortprice"}},"winner_total_top":{"terms":{"field":"entidlist","order":[{"refine_winner_total":"desc"}],"size":%d},"aggs":{"refine_winner_total":{"filter":{}},"topMsg":{"top_hits":{"_source":{"includes":["entidlist","s_winner"]},"size":1}}}},"winner_amount_top":{"terms":{"field":"entidlist","order":[{"refine_winner_amount":"desc"}],"size":%d},"aggs":{"refine_winner_amount":{"sum":{"field":"sortprice"}},"topMsg":{"top_hits":{"_source":{"includes":["entidlist","s_winner"]},"size":1}}}}}}`, group.ItemName, strings.Join(bools, ","), topWinnerLimit, topWinnerLimit))
|
|
|
+ aggsGroup = append(aggsGroup, fmt.Sprintf(`"%s":{"filter":{"query":{"bool":{"should":[%s],"minimum_should_match": 1}}},"aggs":{"project_count":{"filter":{}},"project_amount":{"sum":{"field":"sortprice"}},"winner_total_top":{"terms":{"field":"entidlist","exclude":["-"],"order":[{"refine_winner_total":"desc"}],"size":%d},"aggs":{"refine_winner_total":{"filter":{}}}},"winner_amount_top":{"terms":{"field":"entidlist","exclude":["-"],"order":[{"refine_winner_amount":"desc"}],"size":%d},"aggs":{"refine_winner_amount":{"sum":{"field":"sortprice"}}}}}}`, group.ItemName, strings.Join(bools, ","), topWinnerLimit, topWinnerLimit))
|
|
|
}
|
|
|
itemDataMap[group.ItemName] = group.UpdateTime
|
|
|
}
|
|
|
finalSql := fmt.Sprintf(mae.GetCommonQuerySqlWithAggs(), strings.Join(aggsGroup, ","))
|
|
|
- //log.Println("finalSql", finalSql)
|
|
|
+ //fmt.Println("finalSql-----4", finalSql)
|
|
|
rMap = map[string]interface{}{}
|
|
|
res, _ := util.GetAggs("projectset", "projectset", finalSql)
|
|
|
if res == nil || len(res) == 0 {
|
|
|
return
|
|
|
}
|
|
|
- scale := scaleRefineData{}
|
|
|
+ scale := scaleRefineData{IdSwitch: map[string]string{}}
|
|
|
for name, object := range res {
|
|
|
bArr, err := object.MarshalJSON()
|
|
|
if len(bArr) == 0 || err != nil {
|
|
@@ -154,13 +132,37 @@ func (mae *MarketAnalysisEntity) marketScaleRefineQuery() (rMap map[string]inter
|
|
|
thisRow.UpdateTime = itemDataMap[name]
|
|
|
scale.Data = append(scale.Data, &thisRow)
|
|
|
}
|
|
|
- //计算占比
|
|
|
- scale.formatData()
|
|
|
+ scale.formatData() //获取所需数据
|
|
|
+ scale.doIdSwitch() //补充企业名称
|
|
|
+ return map[string]interface{}{
|
|
|
+ "scaleRefineAll": scale.ReturnData.Overall,
|
|
|
+ "scaleRefineTotalTop": scale.ReturnData.TotalTop,
|
|
|
+ "scaleRefineAmountTop": scale.ReturnData.AmountTop,
|
|
|
+ }, nil
|
|
|
+}
|
|
|
|
|
|
- rMap["scaleRefineAll"] = scale.sortBy(totalAndAmount).getFormatTop(-1, totalAndAmount)
|
|
|
- rMap["scaleRefineTotalTop"] = scale.sortBy(onlyTotal).getFormatTop(topItemLimit, onlyTotal)
|
|
|
- rMap["scaleRefineAmountTop"] = scale.sortBy(onlyAmount).getFormatTop(topItemLimit, onlyAmount)
|
|
|
- return
|
|
|
+//doIdSwitch 补充企业名称
|
|
|
+func (srd *scaleRefineData) doIdSwitch() {
|
|
|
+ if len(srd.IdSwitch) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var ids []string
|
|
|
+ for id, _ := range srd.IdSwitch {
|
|
|
+ ids = append(ids, id)
|
|
|
+ }
|
|
|
+ srd.IdSwitch = GetEntNameByIds(ids)
|
|
|
+ for _, v := range srd.ReturnData.TotalTop {
|
|
|
+ for _, vv := range v.TopList {
|
|
|
+ vv.Name, _ = srd.IdSwitch[vv.Id]
|
|
|
+ vv.Id = util.EncodeId(vv.Id)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, v := range srd.ReturnData.AmountTop {
|
|
|
+ for _, vv := range v.TopList {
|
|
|
+ vv.Name, _ = srd.IdSwitch[vv.Id]
|
|
|
+ vv.Id = util.EncodeId(vv.Id)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//formatData 计算百分比&获取企业对应名称
|
|
@@ -171,62 +173,67 @@ func (srd *scaleRefineData) formatData() {
|
|
|
|
|
|
for _, vv := range v.WinnerTotalTop.Buckets {
|
|
|
vv.Prop = float64(vv.Total) / float64(v.Total)
|
|
|
- vv.EntName = vv.TopMsg.getReallyName(vv.EntId)
|
|
|
}
|
|
|
for _, vv := range v.WinnerAmountTop.Buckets {
|
|
|
vv.Prop = vv.Amount.Value / v.Amount.Value
|
|
|
- vv.EntName = vv.TopMsg.getReallyName(vv.EntId)
|
|
|
}
|
|
|
}
|
|
|
+ srd.ReturnData.Overall = srd.sortBy(totalAndAmount).getOverallData()
|
|
|
+ srd.ReturnData.AmountTop = srd.sortBy(onlyAmount).getFormatTop(topItemLimit, onlyAmount)
|
|
|
+ srd.ReturnData.TotalTop = srd.sortBy(onlyTotal).getFormatTop(topItemLimit, onlyTotal)
|
|
|
+}
|
|
|
+
|
|
|
+//getOverallData 获取分类总览数据
|
|
|
+func (srd *scaleRefineData) getOverallData() (rData []map[string]interface{}) {
|
|
|
+ for _, Value := range srd.Data {
|
|
|
+ rData = append(rData, map[string]interface{}{
|
|
|
+ "name": Value.ItemName,
|
|
|
+ "total": Value.Total,
|
|
|
+ "amount": Value.Amount.Value,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
//getArrayFormatTop 获取前n个数据,并格式化
|
|
|
//limit 获取前x条
|
|
|
-//flag 1仅返回数量统计 2仅返回金额统计 3返回数量和金额统计,但不返回各项Top数据
|
|
|
-func (srd *scaleRefineData) getFormatTop(limit, flag int) (rData []map[string]interface{}) {
|
|
|
+//flag 1仅返回数量统计 2仅返回金额统计
|
|
|
+func (srd *scaleRefineData) getFormatTop(limit, flag int) (rData []*returnItem) {
|
|
|
for index, Value := range srd.Data {
|
|
|
if index >= limit && limit > -1 {
|
|
|
return
|
|
|
}
|
|
|
- thisRow := map[string]interface{}{
|
|
|
- "name": Value.ItemName,
|
|
|
+ thisRow := &returnItem{
|
|
|
+ Name: Value.ItemName,
|
|
|
}
|
|
|
if flag == 1 || flag == 3 { //返回数量相关数据
|
|
|
- thisRow["total"] = Value.Total
|
|
|
+ thisRow.Value = Value.Total
|
|
|
if flag != 3 {
|
|
|
- topArr := []map[string]interface{}{}
|
|
|
for _, v := range Value.WinnerTotalTop.Buckets {
|
|
|
- if len(v.EntId) <= 1 || len(v.EntName) <= 1 { //数据库过滤id为-,及未关联到企业名称的数据
|
|
|
- continue
|
|
|
- }
|
|
|
- topArr = append(topArr, map[string]interface{}{
|
|
|
- "id": util.EncodeId(v.EntId),
|
|
|
- "name": v.EntName,
|
|
|
- "value": v.Total,
|
|
|
- "prop": v.Prop,
|
|
|
+ thisRow.TopList = append(thisRow.TopList, &entValue{
|
|
|
+ Id: v.EntId,
|
|
|
+ Name: v.EntName,
|
|
|
+ Value: v.Total,
|
|
|
+ Prop: v.Prop,
|
|
|
})
|
|
|
+ srd.IdSwitch[v.EntId] = ""
|
|
|
}
|
|
|
- thisRow["prop"] = Value.TotalProp
|
|
|
- thisRow["totalTop"] = topArr
|
|
|
+ thisRow.Prop = Value.TotalProp
|
|
|
}
|
|
|
}
|
|
|
if flag == 2 || flag == 3 { //返回金额相关数据
|
|
|
- thisRow["amount"] = Value.Amount.Value
|
|
|
+ thisRow.Value = Value.Amount.Value
|
|
|
if flag != 3 {
|
|
|
- topArr := []map[string]interface{}{}
|
|
|
for _, v := range Value.WinnerAmountTop.Buckets {
|
|
|
- if len(v.EntId) <= 1 || len(v.EntName) <= 1 { //数据库过滤id为-,及未关联到企业名称的数据
|
|
|
- continue
|
|
|
- }
|
|
|
- topArr = append(topArr, map[string]interface{}{
|
|
|
- "id": util.EncodeId(v.EntId),
|
|
|
- "name": v.EntName,
|
|
|
- "value": v.Amount.Value,
|
|
|
- "prop": v.Prop,
|
|
|
+ thisRow.TopList = append(thisRow.TopList, &entValue{
|
|
|
+ Id: v.EntId,
|
|
|
+ Name: v.EntName,
|
|
|
+ Value: v.Amount.Value,
|
|
|
+ Prop: v.Prop,
|
|
|
})
|
|
|
+ srd.IdSwitch[v.EntId] = ""
|
|
|
}
|
|
|
- thisRow["prop"] = Value.AmountProp
|
|
|
- thisRow["amountTop"] = topArr
|
|
|
+ thisRow.Prop = Value.TotalProp
|
|
|
}
|
|
|
}
|
|
|
rData = append(rData, thisRow)
|