Browse Source

企业名称搜索

WH01243 1 year ago
parent
commit
a0f2327b09
1 changed files with 93 additions and 143 deletions
  1. 93 143
      api/internal/service/owner.go

+ 93 - 143
api/internal/service/owner.go

@@ -89,19 +89,17 @@ func (t *OwnerService) OwnerlList() map[string]interface{} {
 			buyerArr = append(buyerArr, fmt.Sprintf(`"%s"`, gconv.String(value["buyerName"])))
 		}
 		//项目数量处理
-		projectMap = ProjectHandle(buyerArr, "", t.Area)
+		_, projectMap = ProjectHandle(buyerArr, t.PositionId, t.SearchEntName, t.Area, t.SourceType, t.ProcessingStatus)
 		logx.Info(projectMap)
 	} else if t.SearchEntName != "" {
 		//1 只看转介绍成功率高2只看已监控的
-		if t.SourceType == "2" {
-			buyerArr := FindMonitor(t.PositionId, t.SearchEntName)
-			if len(buyerArr) > 0 {
-				projectMap = ProjectHandle(buyerArr, "", t.Area)
-			}
-		} else {
-			projectMap = ProjectHandle([]string{}, t.SearchEntName, t.Area)
+		//先查找采购单位数据
+		dataMap, projectMap = ProjectHandle([]string{}, t.PositionId, t.SearchEntName, t.Area, t.SourceType, t.ProcessingStatus)
+		if t.ProcessingStatus != "" {
+			ProcessingStatusInit(t.PositionId, dataMap, t.ProcessingStatus)
 		}
-		dataMap = FindBuyer(t.SearchEntName)
+		//监控状态处理
+		MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
 	}
 	if len(*dataMap) == 0 {
 		return map[string]interface{}{}
@@ -240,6 +238,9 @@ func ConnectionsHandle(buyerArr []string, positionId int64, isAll bool) []map[st
 			}
 		}
 	}
+	if isAll && len(returnData) > 0 {
+		return returnData
+	}
 	//供应商  采购单位 供应商     招标代理机构  采购单位  招标代理机构  需要计算合作次数
 	winnerSql := fmt.Sprintf(`SELECT DISTINCT a.project_name as  project_name,a.project_id as project_id , a.zbtime as zbtime ,a.buyer_id as buyer_id,a.buyer as  buyer,a.agency_id as agency_id,a.agency as  agency,c.company_name as company_name ,b.name as personName ,a.winnerId  as awinnerId
 	FROM 
@@ -371,8 +372,8 @@ func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, s
 	monitorNumber := int64(0)
 	for buyerId, buyerMap := range *dataMap {
 		buyerName := gconv.String(buyerMap["buyerName"])
-		if _, ok := (*projectMap)[buyerId]; ok {
-			projectMap := (*projectMap)[buyerId]
+		if _, ok := (*projectMap)[buyerName]; ok {
+			projectMap := (*projectMap)[buyerName]
 			if isMonitor := gconv.Bool(buyerMap["isMonitor"]); isMonitor {
 				monitorNumber++
 			}
@@ -462,19 +463,48 @@ func MonitorStatusInit(positionId int64, dataMap *map[string]map[string]interfac
 }
 
 // 监控数据查询初始化
-func FindMonitor(positionId int64, entName string) []string {
-	nameArr := []string{}
+func FindMonitor(positionId int64, entName string) map[string]bool {
+	nameMap := map[string]bool{}
 	clist, ok := Mgo.Find("", map[string]interface{}{
 		"userId": positionId,
 		"name":   fmt.Sprintf(" /.*%s.*/i", entName),
 	}, "", `{"name":1}`, false, -1, -1)
 	if ok && clist != nil && len(*clist) > 0 {
 		for _, v := range *clist {
-			nameArr = append(nameArr, fmt.Sprintf(`"%s"`, gconv.String(v["name"])))
+			nameMap[gconv.String(v)] = true
 		}
-
 	}
-	return nameArr
+	return nameMap
+}
+func FindProcessingStatus(positionId int64) (map[string]bool, map[string]bool, map[string]bool) {
+	//未处理
+	untreatedMap := map[string]bool{}
+	//已忽略
+	ignoredMap := map[string]bool{}
+	//已创建
+	createdMap := map[string]bool{}
+	processingArr := CrmMysql.Find(entity.CONNECTION_STATUS, map[string]interface{}{
+		"type":        1,
+		"position id": positionId,
+	}, "relate_id,is_handle,is_ignore,is_create", "", -1, -1)
+	if len(*processingArr) > 0 {
+		for _, v := range *processingArr {
+			entId := gconv.String(v["relate_id"])
+			handle := gconv.Int64(v["is_handle"])
+			ignore := gconv.Int64(v["is_ignore"])
+			create := gconv.Int64(v["is_create"])
+			if handle == 1 {
+				untreatedMap[entId] = true
+			}
+			if ignore == 1 {
+				ignoredMap[entId] = true
+			}
+			if create == 1 {
+				createdMap[entId] = true
+			}
+		}
+	}
+	return untreatedMap, ignoredMap, createdMap
 }
 
 // 处理状态初始化
@@ -581,149 +611,83 @@ func BuyerList(partyA, supplier, heterotophy, intermediary, agency string) *map[
 }
 
 // 项目数量查询
-func ProjectHandle(buyerArr []string, entName, area string) *map[string]map[string]interface{} {
+func ProjectHandle(buyerArr []string, positionId int64, entName, area, sourceType, processingStatus string) (*map[string]map[string]interface{}, *map[string]map[string]interface{}) {
+
 	projectMap := &map[string]map[string]interface{}{}
+	returnMap := &map[string]map[string]interface{}{}
 	sql := ""
 	sqlStr := ""
 	if entName != "" {
-		sql = `{"query":{"bool":{"must":[{"terms":{"buyer":[%s]%s}}]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"project_count":{"terms":{"field":"winner"}}}},"area_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}}}},"money_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"money_count":{"sum":{"field":"bidendtime"}}}},"zbtime_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":%d}`
-		sqlStr = fmt.Sprintf(sql, strings.Join(buyerArr, ","), common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""), len(buyerArr))
-
+		sql = `{"query":{"bool":{"must":[{"multi_match":{"query":"%s","type":"phrase","fields":["buyer.mbuyer"]}}%s]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"zbtime_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}},"buyer_id":{"terms":{"field":"id"}},"money_count":{"sum":{"field":"bidendtime"}},"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":0}`
+		sqlStr = fmt.Sprintf(sql, entName, common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""))
 	} else {
-		sql = `{"query":{"bool":{"must":[{"terms":{"buyer":[%s]%s}}]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"project_count":{"terms":{"field":"winner"}}}},"area_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}}}},"money_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"money_count":{"sum":{"field":"bidendtime"}}}},"zbtime_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":%d}`
-		sqlStr = fmt.Sprintf(sql, strings.Join(buyerArr, ","), common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""), len(buyerArr))
-
+		sql = `{"query":{"bool":{"must":[{"terms":{"buyer":[%s]}}%s]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"zbtime_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}},"buyer_id":{"terms":{"field":"id"}},"money_count":{"sum":{"field":"bidendtime"}},"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":0}`
+		sqlStr = fmt.Sprintf(sql, strings.Join(buyerArr, ","), common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""))
 	}
 	data, _, _ := elastic.GetAggs(INDEX, TYPE, sqlStr)
-	for name, object := range data {
-		switch name {
-		case "buyer_count":
-			buyerArr := BuyerAggStruct{}
-			bs, ok := object.MarshalJSON()
-			if ok != nil {
-				logx.Error("解析项目数据失败", object)
-				continue
-			}
-			json.Unmarshal(bs, &buyerArr)
-			for _, v := range buyerArr.Buckets {
-				key := v.Key
-				if (*projectMap)[key] != nil {
-					value := (*projectMap)[key]
-					value["count"] = v.Doc_count
-					(*projectMap)[key] = value
-				} else {
-					(*projectMap)[key] = map[string]interface{}{
-						"count": v.Doc_count,
-					}
-				}
-			}
-		case "area_count":
-			areaArr := AreaAggStruct{}
-			bs, ok := object.MarshalJSON()
-			if ok != nil {
-				logx.Error("解析项目数据失败", object)
-				continue
-			}
-			json.Unmarshal(bs, &areaArr)
-			logx.Info(areaArr)
-			for _, v := range areaArr.Buckets {
-				key := v.Key
-				area := ""
-				for _, v1 := range v.Area_count.Buckets {
-					area = Deduplication(area, v1.Key)
-				}
-				if (*projectMap)[key] != nil {
-					value := (*projectMap)[key]
-					value["area"] = area
-					(*projectMap)[key] = value
-				} else {
-
-					(*projectMap)[key] = map[string]interface{}{
-						"area": area,
-					}
-				}
-			}
-		case "money_count":
-			moneyArr := MoneyAggStruct{}
-			bs, ok := object.MarshalJSON()
-			if ok != nil {
-				logx.Error("解析项目数据失败", object)
-				continue
+	if len(data) == 0 {
+		return projectMap, returnMap
+	}
+	for _, object := range data {
+		buyerArr := BuyerAggStruct{}
+		bs, ok := object.MarshalJSON()
+		if ok != nil {
+			logx.Error("解析项目数据失败", object)
+			continue
+		}
+		A := gconv.Map(bs)
+		logx.Info(A)
+		err := json.Unmarshal(bs, &buyerArr)
+		logx.Info(err)
+		for _, v := range buyerArr.Buckets {
+			buyerName := v.Key
+			area := ""
+			buyerId := ""
+			for _, areaEntity := range v.Area_count.Buckets {
+				area = Deduplication(area, areaEntity.Key)
 			}
-			json.Unmarshal(bs, &moneyArr)
-			logx.Info(moneyArr)
-			for _, v := range moneyArr.Buckets {
-				key := v.Key
-				if (*projectMap)[key] != nil {
-					value := (*projectMap)[key]
-					value["money"] = v.Money_count.Value
-					(*projectMap)[key] = value
-				} else {
-					(*projectMap)[key] = map[string]interface{}{
-						"money": v.Money_count.Value,
-					}
+			for _, buyerIdEntity := range v.Buyer_id.Buckets {
+				if buyerIdEntity.Key != "" {
+					buyerId = buyerIdEntity.Key
 				}
 			}
-		case "zbtime_count":
-			timeArr := TimeAggStruct{}
-			bs, ok := object.MarshalJSON()
-			if ok != nil {
-				logx.Error("解析项目数据失败", object)
-				continue
+			(*projectMap)[buyerName] = map[string]interface{}{
+				"area":    area,
+				"zbtime":  v.Zbtime_count.Value,
+				"money":   v.Money_count.Value,
+				"count":   v.Doc_count,
+				"buyerId": buyerId,
 			}
-			a := json.Unmarshal(bs, &timeArr)
-			logx.Info(timeArr, a)
-			for _, v := range timeArr.Buckets {
-				key := v.Key
-				if (*projectMap)[key] != nil {
-					value := (*projectMap)[key]
-					value["zbtime"] = v.Zbtime_count.Value
-					(*projectMap)[key] = value
-				} else {
-					(*projectMap)[key] = map[string]interface{}{
-						"zbtime": v.Zbtime_count.Value,
-					}
-				}
+			(*returnMap)[buyerId] = map[string]interface{}{
+				"buyerName": buyerName,
 			}
 		}
 	}
-	return projectMap
+	return projectMap, returnMap
 }
 
-type TimeAggStruct struct {
+type BuyerAggStruct struct {
 	Buckets []struct {
 		Key          string `json:"key,omitempty"`
+		Doc_count    int64  `json:"doc_count,omitempty"`
 		Zbtime_count struct {
 			Value interface{} `json:"value,omitempty"`
 		} `json:"zbtime_count,omitempty"`
-	}
-}
-type MoneyAggStruct struct {
-	Buckets []struct {
-		Key         string `json:"key,omitempty"`
 		Money_count struct {
 			Value interface{} `json:"value,omitempty"`
 		} `json:"money_count,omitempty"`
-	}
-}
-type BuyerAggStruct struct {
-	Buckets []struct {
-		Key         string `json:"key,omitempty"`
-		Doc_count   int64  `json:"doc_count,omitempty"`
-		Buyer_count struct {
-			value string `json:"value,omitempty"`
-		} `json:"buyer_count,omitempty"`
-	}
-}
-type AreaAggStruct struct {
-	Buckets []struct {
-		Key        string `json:"key,omitempty"`
 		Area_count struct {
 			Buckets []struct {
 				Key       string `json:"key,omitempty"`
 				Doc_count int64  `json:"doc_count,omitempty"`
 			}
 		} `json:"area_count"`
+		Buyer_id struct {
+			Buckets []struct {
+				Key       string `json:"key,omitempty"`
+				Doc_count int64  `json:"doc_count,omitempty"`
+			}
+		} `json:"buyer_id"`
 	}
 }
 
@@ -830,20 +794,6 @@ func Deduplication(a, b string) string {
 	}
 	return strings.Join(newArr, ",")
 }
-func FindBuyer(searchName string) *map[string]map[string]interface{} {
-	sql := `{"query":{"bool":{"must":[{"match":{"company_name":"%s"}}]}},"sort":[{"_id":"asc"}],"_source":["id","company_name"],"from":0,"size":10}`
-	sqlStr := fmt.Sprintf(sql, searchName)
-	data := elastic.Get(EntINDEX, EntTYPE, sqlStr)
-	dataMap := &map[string]map[string]interface{}{}
-	for _, m := range *data {
-		id := gconv.String(m["id"])
-		name := gconv.String(m["company_name"])
-		(*dataMap)[id] = map[string]interface{}{
-			"buyerName": name,
-		}
-	}
-	return dataMap
-}
 
 // 路径列表
 func (t *OwnerService) OwnerRoute() []map[string]interface{} {