瀏覽代碼

数据库修改

WH01243 9 月之前
父節點
當前提交
055e0b1f61
共有 1 個文件被更改,包括 124 次插入210 次删除
  1. 124 210
      api/internal/service/owner.go

+ 124 - 210
api/internal/service/owner.go

@@ -1,22 +1,21 @@
 package service
 
 import (
-	"context"
-	"encoding/json"
-	"fmt"
-	"sort"
-	"strings"
-	"time"
-
 	"app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/encrypt"
-	elastic "app.yhyue.com/moapp/jybase/es"
 	"app.yhyue.com/moapp/jybase/redis"
 	. "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
+	T "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
 	"bp.jydev.jianyu360.cn/CRM/networkManage/entity"
+	"context"
+	"encoding/json"
+	"fmt"
 	"github.com/gogf/gf/v2/util/gconv"
 	"github.com/shopspring/decimal"
 	"github.com/zeromicro/go-zero/core/logx"
+	"sort"
+	"strings"
+	"time"
 )
 
 const (
@@ -24,7 +23,6 @@ const (
 	EntTYPE                  = "ent_info"
 	NetworkManageOwnerlList  = "networkManage_ownerlList_%d_%s"
 	NetworkManageOwnerlCount = "networkManage_ownerlCount_%d_%s"
-	NetworkManageCompanyList = "networkManage_companyList_%d_%s"
 )
 
 type OwnerService struct {
@@ -94,7 +92,6 @@ func (t *OwnerService) OwnerlList() map[string]interface{} {
 		t.PageIndex = 1
 	}
 	startIndex := (t.PageIndex - 1) * t.PageSize
-	endIndex := t.PageIndex * t.PageSize
 	returnData, connectionsNumber := []BuyerProject{}, int64(0)
 	// 如果没有业务字符串,直接返回空数据
 	if businessStr == "" {
@@ -112,55 +109,44 @@ func (t *OwnerService) OwnerlList() map[string]interface{} {
 		connectionsNumber = gconv.Int64(redis.GetInt("newother", countKey))
 		// 处理监控状态
 	} else {
-		clKey := fmt.Sprintf(NetworkManageCompanyList, t.PositionId, comKey)
-		// 检查 Redis 中是否存在公司缓存
-		if ok, err := redis.Exists("newother", clKey); ok && err == nil && t.SourceType != "2" {
-			// 处理可介绍业主人脉
-			returnData = handleRedisCache(clKey)
-			sortAndPaginateReturnData(&returnData, startIndex, endIndex)
-		} else {
-			// 没有缓存时的处理逻辑
-			if t.PartyA != "" || t.Supplier != "" || t.Heterotophy != "" || t.Intermediary != "" || t.Agency != "" {
-				dataMap = BuyerList(t.PartyA, t.Supplier, t.Heterotophy, t.Intermediary, t.Agency, t.PositionId)
-				if len(*dataMap) == 0 {
-					return map[string]interface{}{
-						"connectionsNumber": 0,
-						"highSuccessNumber": 0,
-						"monitorNumber":     0,
-						"list":              []BuyerProject{},
-					}
-				}
-				// 监控状态处理
-				MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
-				// 项目数量查询
-				buyerArr := getBuyerArr(dataMap)
-				// 项目数量处理
-				projectMap, dataMap = ProjectHandle(buyerArr, t.EntAccountId, t.SearchEntName, t.Area, businessStr, dataMap)
-			} else {
-				return map[string]interface{}{}
-			}
-			endTime := time.Now().Unix()
-			fmt.Println("用时时间:", endTime-startTime)
+		// 没有缓存时的处理逻辑
+		if t.PartyA != "" || t.Supplier != "" || t.Heterotophy != "" || t.Intermediary != "" || t.Agency != "" {
+			dataMap = BuyerList(t.PartyA, t.Supplier, t.Heterotophy, t.Intermediary, t.Agency, t.PositionId)
 			if len(*dataMap) == 0 {
-				return map[string]interface{}{}
-			}
-			// 采购单位和项目合并
-			a1 := time.Now().Unix()
-			returnData, connectionsNumber = BuyerProjectMerge(dataMap, projectMap, t.SourceType)
-			a2 := time.Now().Unix()
-			fmt.Println("组合数据", a2-a1)
-			if len(returnData) > 0 {
-				redis.Put("newother", comKey, returnData, C.CacheTimeOut)
-				sort.Slice(returnData, func(i, j int) bool {
-					aa := fmt.Sprintf("%d%s", (returnData)[i].Zbtime, (returnData)[i].BuyerId)
-					bb := fmt.Sprintf("%d%s", (returnData)[j].Zbtime, (returnData)[j].BuyerId)
-					return aa > bb
-				})
-				// 数组分页处理
-				sortAndPaginateReturnData(&returnData, startIndex, endIndex)
+				return map[string]interface{}{
+					"connectionsNumber": 0,
+					"highSuccessNumber": 0,
+					"monitorNumber":     0,
+					"list":              []BuyerProject{},
+				}
 			}
-
+			// 监控状态处理
+			MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
+			// 项目数量查询
+			buyerArr := getBuyerArr(dataMap)
+			// 项目数量处理
+			projectMap, connectionsNumber = ProjectHandle(buyerArr, t.Area, businessStr, dataMap, startIndex, t.PageSize, t.SourceType)
+		} else {
+			return map[string]interface{}{}
 		}
+		endTime := time.Now().Unix()
+		fmt.Println("用时时间:", endTime-startTime)
+		if len(*projectMap) == 0 {
+			return map[string]interface{}{}
+		}
+		// 采购单位和项目合并
+		a1 := time.Now().Unix()
+		returnData = BuyerProjectMerge(dataMap, projectMap)
+		a2 := time.Now().Unix()
+		fmt.Println("组合数据", a2-a1)
+		/*if len(returnData) > 0 {
+			//redis.Put("newother", comKey, returnData, C.CacheTimeOut)
+			sort.Slice(returnData, func(i, j int) bool {
+				aa := fmt.Sprintf("%d%s", (returnData)[i].Zbtime, (returnData)[i].BuyerId)
+				bb := fmt.Sprintf("%d%s", (returnData)[j].Zbtime, (returnData)[j].BuyerId)
+				return aa > bb
+			})
+		}*/
 	}
 	if ok, err := redis.Exists("newother", listKey); !ok || err != nil || t.SourceType == "2" {
 		buyerIdArr := getBuyerIdArr(returnData)
@@ -183,7 +169,6 @@ func (t *OwnerService) OwnerlList() map[string]interface{} {
 		}
 		redis.Put("newother", listKey, returnData, C.CacheTimeOut)
 		redis.Put("newother", countKey, connectionsNumber, C.CacheTimeOut)
-
 	}
 	// 返回数据组装
 	return map[string]interface{}{
@@ -209,22 +194,11 @@ func handleRedisCache(key string) []BuyerProject {
 func getBuyerArr(dataMap *map[string]map[string]interface{}) []string {
 	buyerArr := []string{}
 	for _, value := range *dataMap {
-		buyerArr = append(buyerArr, fmt.Sprintf(`"%s"`, gconv.String(value["buyerName"])))
+		buyerArr = append(buyerArr, fmt.Sprintf(`%s`, gconv.String(value["buyerName"])))
 	}
 	return buyerArr
 }
 
-// sortAndPaginateReturnData 对返回数据进行分页处理
-func sortAndPaginateReturnData(returnData *[]BuyerProject, startIndex, endIndex int64) {
-
-	if startIndex < int64(len(*returnData)) {
-		if endIndex > int64(len(*returnData)) {
-			endIndex = int64(len(*returnData))
-		}
-		*returnData = (*returnData)[startIndex:endIndex]
-	}
-}
-
 // getBuyerIdArr 从返回数据中获取企业ID 数组
 func getBuyerIdArr(returnData []BuyerProject) []string {
 	buyerIdArr := []string{}
@@ -596,56 +570,27 @@ func Findfirstparty(buyerArr []string, returnData []map[string]interface{}) []ma
 }
 
 // 采购单位和项目合并
-func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, sourceType string) ([]BuyerProject, int64) {
+func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}) []BuyerProject {
 	returnData := []BuyerProject{}
-	connectionsNumber := int64(0)
-	for buyerId, buyerMap := range *dataMap {
-		buyerName := gconv.String(buyerMap["buyerName"])
-		count := int64(0)
-		if _, ok := (*projectMap)[buyerName]; ok {
-			projectMap := (*projectMap)[buyerName]
-			money := gconv.Int64(projectMap["money"])
-			count = gconv.Int64(projectMap["count"])
-			zbtime := gconv.Int64(projectMap["zbtime"])
-			returnData = append(returnData, BuyerProject{
-				BuyerId:   buyerId,
-				BuyerName: buyerName,
-				BuyerType: gconv.String(buyerMap["buyerType"]),
-				Project: ProjectEntity{
-					Number:          count,
-					EstimatedAmount: money,
-					Connections:     []map[string]interface{}{},
-				},
-				Area:      gconv.String(projectMap["area"]),
-				Zbtime:    zbtime,
-				IsMonitor: gconv.Bool(buyerMap["isMonitor"]),
-				CId:       gconv.String(buyerMap["cId"]),
-			})
-		} else {
-			if sourceType == "1" {
-				if count < 2 {
-					continue
-				}
-			}
-			returnData = append(returnData, BuyerProject{
-				BuyerId:   buyerId,
-				BuyerName: buyerName,
-				BuyerType: gconv.String(buyerMap["buyerType"]),
-				Project: ProjectEntity{
-					Number:          0,
-					EstimatedAmount: 0,
-					Connections:     []map[string]interface{}{},
-				},
-				Area:      "",
-				Zbtime:    0,
-				IsMonitor: gconv.Bool(buyerMap["isMonitor"]),
-				CId:       gconv.String(buyerMap["cId"]),
-			})
-
-		}
-		connectionsNumber++
+	for buyerName, project := range *projectMap {
+		buyerId := gconv.String(project["buyerId"])
+		data := (*dataMap)[buyerId]
+		returnData = append(returnData, BuyerProject{
+			BuyerId:   buyerId,
+			BuyerName: buyerName,
+			BuyerType: gconv.String(data["buyerType"]),
+			Project: ProjectEntity{
+				Number:          gconv.Int64(project["count"]),
+				EstimatedAmount: gconv.Int64(project["money"]),
+				Connections:     []map[string]interface{}{},
+			},
+			Area:      gconv.String(project["area"]),
+			Zbtime:    gconv.Int64(project["area"]),
+			IsMonitor: gconv.Bool(data["isMonitor"]),
+			CId:       gconv.String(data["cId"]),
+		})
 	}
-	return returnData, connectionsNumber
+	return returnData
 }
 
 // 已监控数据处理
@@ -785,110 +730,70 @@ func BuyerList(partyA, supplier, heterotophy, intermediary, agency string, posit
 }
 
 // 项目数量查
-func ProjectHandle(buyerArr []string, entAccountId int64, entName, area, businessStr string, dataMap *map[string]map[string]interface{}) (*map[string]map[string]interface{}, *map[string]map[string]interface{}) {
+func ProjectHandle(buyerArr []string, area, businessStr string, dataMap *map[string]map[string]interface{}, startIndex, PageSize int64, sourceType string) (*map[string]map[string]interface{}, int64) {
 	projectMap := &map[string]map[string]interface{}{}
-	returnMap := &map[string]map[string]interface{}{}
-	sql := ""
-	sqlStr := ""
-	propertyFormStr := `"should":[`
-	for k, v := range strings.Split(businessStr, ",") {
-		if k > 0 {
-			propertyFormStr += `,`
-		}
-		propertyFormStr += `{"term": {"topscopeclass": "` + v + `"}}`
+	count := int64(0)
+	sqlStr := "select buyer,buyer_id,groupUniqArray(area) as area,count(1) as ocount,SUM(project_money) as project_money ,MAX(zbtime) as zbtime from transaction_info_all " +
+		"where"
+	countSql := "select  count(1) as  count from  (%s) b   "
+	if len(buyerArr) > 0 {
+		buyer, buyerArgs := ConditionHandle(buyerArr)
+		sqlStr += fmt.Sprintf(fmt.Sprintf(" buyer   in   (%s)", buyer), buyerArgs...)
 	}
-	propertyFormStr += `],"minimum_should_match": 1`
-	sql = `{"query":{"bool":{"must_not":[{"term":{"project_bidstatus":1}}],%s,"must":[{"terms":{"buyer":[%s]}}%s]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":500,"order":{"zbtime_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}},"buyer_id":{"terms":{"field":"buyer_id"}},"money_count":{"sum":{"field":"project_money"}},"zbtime_count":{"max":{"field":"zbtime"}}}}},"size":0}`
-	sqlStr = fmt.Sprintf(sql, propertyFormStr, strings.Join(buyerArr, ","), common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""))
-	startTime := time.Now().Unix()
-	data, _, _ := elastic.GetAggs(INDEX_1, INDEX_1, sqlStr)
-	endTime := time.Now().Unix()
-	fmt.Println("es用时时间:", endTime-startTime)
-	if len(data) == 0 {
-		return projectMap, dataMap
+	if len(area) > 0 && area != "" {
+		areaStr, areaArgs := ConditionHandle(strings.Split(area, ","))
+		sqlStr += fmt.Sprintf(fmt.Sprintf(" area   in   (%s)", areaStr), areaArgs...)
 	}
-	for _, object := range data {
-		buyerArr := BuyerAggStruct{}
-		bs, ok := object.MarshalJSON()
-		if ok != nil {
-			logx.Error("解析项目数据失败", object)
-			continue
-		}
-
-		err := json.Unmarshal(bs, &buyerArr)
-		if err != nil {
+	if len(businessStr) > 0 {
+		business, businessArgs := ConditionHandle(strings.Split(businessStr, ","))
+		sqlStr += fmt.Sprintf(fmt.Sprintf(" and   hasAny(topscopeclass,[%s])>0", business), businessArgs...)
+	}
+	sqlStr += " GROUP  by    buyer,buyer_id   "
+	if sourceType == "1" {
+		sqlStr += "  HAVING   ocount>2 "
+	}
+	sqlStr += " order by  zbtime desc"
+	countSql = fmt.Sprintf(countSql, sqlStr)
+	sqlStr += "  limit  ?,? "
+	//总数查询
+	rows, err1 := ClickhouseConn.Query(context.TODO(), sqlStr, startIndex, PageSize)
+	count = T.NetworkCom.Count(countSql)
+	if err1 != nil {
+		logx.Error(err1)
+		return projectMap, count
+	}
+	ass := []BuyerAggStruct{}
+	for rows.Next() {
+		as := BuyerAggStruct{}
+		if err := rows.ScanStruct(&as); err != nil {
 			logx.Error(err)
+			continue
 		}
-		for _, v := range buyerArr.Buckets {
-			buyerName := v.Key
-			area := ""
-			buyerId := ""
-			for _, areaEntity := range v.Area_count.Buckets {
-				area = Deduplication(area, areaEntity.Key)
-			}
-			for _, buyerIdEntity := range v.Buyer_id.Buckets {
-				if buyerIdEntity.Key != "" {
-					buyerId = buyerIdEntity.Key
-				}
-			}
-			(*projectMap)[buyerName] = map[string]interface{}{
-				"area":    area,
-				"zbtime":  v.Zbtime_count.Value,
-				"money":   common.RetainDecimal(common.Float64All(v.Money_count.Value)/10000, 2),
-				"count":   v.Doc_count,
-				"buyerId": buyerId,
-			}
-			(*returnMap)[buyerId] = map[string]interface{}{
-				"buyerName": buyerName,
-				"buyerType": "firstparty",
-			}
-
-		}
+		ass = append(ass, as)
 	}
-	if area != "" {
-		if entName == "" {
-			for buyerId, v := range *dataMap {
-				buyerName := gconv.String(v["buyerName"])
-				if _, ok := (*projectMap)[buyerName]; !ok {
-					delete(*dataMap, buyerId)
-				}
-			}
-		} else {
-			for buyerId, v := range *returnMap {
-				buyerName := gconv.String(v["buyerName"])
-				if _, ok := (*projectMap)[buyerName]; !ok {
-					delete(*returnMap, buyerId)
-				}
-			}
+	defer rows.Close()
+	for _, object := range ass {
+		buyerName := object.Buyer
+		buyerId := object.BuyerId
+		(*projectMap)[buyerName] = map[string]interface{}{
+			"area":      area,
+			"zbtime":    object.Zbtime,
+			"money":     common.RetainDecimal(common.Float64All(object.ProjectMoney)/10000, 2),
+			"count":     object.Ocount,
+			"buyerId":   buyerId,
+			"buyerName": buyerName,
 		}
-
 	}
-	return projectMap, dataMap
+	return projectMap, count
 }
 
 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"`
-		Money_count struct {
-			Value interface{} `json:"value,omitempty"`
-		} `json:"money_count,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"`
-	}
+	Buyer        string          `ch:"buyer"`
+	BuyerId      string          `ch:"buyer_id"`
+	Area         []string        `ch:"area"`
+	ProjectMoney decimal.Decimal `ch:"project_money"`
+	Zbtime       int64           `ch:"zbtime"`
+	Ocount       uint64          `ch:"ocount"`
 }
 
 // 采购单位查询sql
@@ -1119,3 +1024,12 @@ func FindBusiness(endId int64, userId string) string {
 	}
 	return businessStr
 }
+func ConditionHandle(args []string) (string, []interface{}) {
+	newArgs := make([]interface{}, len(args))
+	wh := make([]string, len(args))
+	for k, v := range args {
+		newArgs[k] = v
+		wh[k] = "'%s'"
+	}
+	return strings.Join(wh, ","), newArgs
+}