Эх сурвалжийг харах

Merge commit 'a8495082418c25b3cf1fd5896faf8f3b2dca0e93'

* commit 'a8495082418c25b3cf1fd5896faf8f3b2dca0e93':
  缓存处理
  潜客列表缓存处理
Jianghan 9 сар өмнө
parent
commit
a4d0a7c9e9
1 өөрчлөгдсөн 140 нэмэгдсэн , 86 устгасан
  1. 140 86
      api/internal/service/owner.go

+ 140 - 86
api/internal/service/owner.go

@@ -1,9 +1,11 @@
 package service
 
 import (
+	"app.yhyue.com/moapp/jybase/redis"
 	"context"
 	"encoding/json"
 	"fmt"
+	"log"
 	"sort"
 	"strings"
 	"time"
@@ -75,104 +77,166 @@ type Project struct {
 	Money      decimal.Decimal `ch:"money"`
 }
 
+// OwnerlList 执行主要的业务逻辑
 func (t *OwnerService) OwnerlList() map[string]interface{} {
-	//先查询采购单位列表
+	// 先查询采购单位列表
 	startTime := time.Now().Unix()
 	dataMap := &map[string]map[string]interface{}{}
 	projectMap := &map[string]map[string]interface{}{}
 	businessStr := FindBusiness(t.EntId, t.UserId)
-	returnData, connectionsNumber, highSuccessNumber, monitorNumber := []BuyerProject{}, int64(0), int64(0), int64(0)
+	// 初始化分页参数
+	if t.PageSize == 0 {
+		t.PageSize = 10
+	}
+	if t.PageIndex == 0 {
+		t.PageIndex = 1
+	}
+	startIndex := (t.PageIndex - 1) * t.PageSize
+	endIndex := t.PageIndex * t.PageSize
+	returnData := []BuyerProject{}
+	// 如果没有业务字符串,直接返回空数据
 	if businessStr == "" {
 		return map[string]interface{}{
-			"connectionsNumber": connectionsNumber,
-			"highSuccessNumber": highSuccessNumber,
-			"monitorNumber":     monitorNumber,
-			"list":              returnData,
+			"list": returnData,
 		}
 	}
-	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{},
+	key := fmt.Sprintf("%+v", t)
+	key = fmt.Sprintf("%s%s", key, fmt.Sprintf(" business:%s", businessStr))
+	listKey := "ownerlList-" + common.GetMd5String(fmt.Sprintf("%+v", key))
+	// 检查 Redis 中是否存在列表缓存
+	if ok, err := redis.Exists("newother", listKey); ok && err == nil && t.SourceType != "2" {
+		returnData = handleRedisCache(listKey)
+		// 处理监控状态
+	}
+	log.Println("111", fmt.Sprintf("%+v", key))
+	key = "companyList-" + common.GetMd5String(fmt.Sprintf("%+v", key))
+	// 检查 Redis 中是否存在公司缓存
+	if ok, err := redis.Exists("newother", key); ok && err == nil && t.SourceType != "2" {
+		// 处理可介绍业主人脉
+		returnData = handleRedisCache(key)
+	} 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 len(*dataMap) == 0 {
+			return map[string]interface{}{}
+		}
+		// 采购单位和项目合并
+		a1 := time.Now().Unix()
+		returnData = BuyerProjectMerge(dataMap, projectMap, t.SourceType)
+		a2 := time.Now().Unix()
+		fmt.Println("组合数据", a2-a1)
+		if len(returnData) > 0 {
+			redis.Put("newother", key, 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)
 		}
+
+	}
+
+	buyerIdArr := getBuyerIdArr(returnData)
+	// 可介绍业主人脉处理
+	a3 := time.Now().Unix()
+	routeList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
+	a4 := time.Now().Unix()
+	fmt.Println("组合数1111据", a4-a3)
+	updateReturnDataWithRoutes(returnData, routeList)
+	if len(returnData) > 0 {
 		//监控状态处理
-		//MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
-		//项目数量查询
-		buyerArr := []string{}
-		for _, value := range *dataMap {
-			buyerArr = append(buyerArr, fmt.Sprintf(`"%s"`, gconv.String(value["buyerName"])))
+		monitorMap := NetworkCom.EntMonitor(gconv.String(t.PositionId))
+		for k, value := range returnData {
+			if _, ok := monitorMap[value.BuyerName]; ok {
+				returnData[k].IsMonitor = true
+			}
 		}
-		//项目数量处理
-		projectMap, _, dataMap = ProjectHandle(buyerArr, t.EntAccountId, t.SearchEntName, t.Area, businessStr, dataMap)
-	} else {
-		return map[string]interface{}{}
+		redis.Put("newother", listKey, returnData, C.CacheTimeOut)
 	}
-	endTime := time.Now().Unix()
-	fmt.Println("用时时间:", endTime-startTime)
-	if len(*dataMap) == 0 {
-		return map[string]interface{}{}
+	// 返回数据组装
+	return map[string]interface{}{
+		"list": returnData,
 	}
-	//采购单位和项目合并
-	a1 := time.Now().Unix()
-	returnData, connectionsNumber, highSuccessNumber, monitorNumber = BuyerProjectMerge(dataMap, projectMap, t.SourceType)
-	a2 := time.Now().Unix()
-	fmt.Println("组合数据", a2-a1)
-	//数组排序
-	//分页数据处理
-	if t.PageSize == 0 {
-		t.PageSize = 10
+}
+
+// handleRedisCache 处理从 Redis 中获取缓存数据的逻辑
+func handleRedisCache(key string) []BuyerProject {
+	returnData := []BuyerProject{}
+	if bytes, err := redis.GetBytes("newother", key); err == nil && bytes != nil {
+		err = json.Unmarshal(*bytes, &returnData)
+		if err != nil {
+			// 处理解组错误
+			fmt.Println("Error unmarshaling from Redis:", err)
+		}
 	}
-	if t.PageIndex == 0 {
-		t.PageIndex = 1
+	return returnData
+}
+
+// getBuyerArr 从数据映射中获取买家数组
+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"])))
 	}
-	startIndex := (t.PageIndex - 1) * t.PageSize
-	endIndex := t.PageIndex * t.PageSize
-	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 startIndex < gconv.Int64(len(returnData)) {
-		if endIndex > gconv.Int64(len(returnData)) {
-			endIndex = gconv.Int64(len(returnData))
+	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]
-	} else {
-		return map[string]interface{}{}
+		*returnData = (*returnData)[startIndex:endIndex]
 	}
+}
+
+// getBuyerIdArr 从返回数据中获取企业ID 数组
+func getBuyerIdArr(returnData []BuyerProject) []string {
 	buyerIdArr := []string{}
 	for _, value := range returnData {
 		if value.BuyerId != "" {
 			buyerIdArr = append(buyerIdArr, fmt.Sprintf("'%s'", value.BuyerId))
 		}
 	}
-	//可介绍业主人脉处理
-	a3 := time.Now().Unix()
-	routeList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
-	a4 := time.Now().Unix()
-	fmt.Println("组合数1111据", a4-a3)
+	return buyerIdArr
+}
+
+// updateReturnDataWithRoutes 将路线信息更新到返回数据中
+func updateReturnDataWithRoutes(returnData []BuyerProject, routeList []map[string]interface{}) {
 	for _, v := range routeList {
 		buyerId := gconv.String(v["a_id"])
 		for i, v1 := range returnData {
 			aBuyerId := v1.BuyerId
 			if buyerId == aBuyerId {
-				//组装数据
+				// 组装数据
 				returnData[i].Project.Connections = append(returnData[i].Project.Connections, v)
 			}
 		}
 	}
-	//返回数据组装
-	return map[string]interface{}{
-		"connectionsNumber": connectionsNumber,
-		"highSuccessNumber": highSuccessNumber,
-		"monitorNumber":     monitorNumber,
-		"list":              returnData,
-	}
 }
 
 // 可介绍业主人脉列表
@@ -521,25 +585,16 @@ func Findfirstparty(buyerArr []string, returnData []map[string]interface{}) []ma
 }
 
 // 采购单位和项目合并
-func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, sourceType string) ([]BuyerProject, int64, int64, int64) {
+func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, sourceType string) []BuyerProject {
 	returnData := []BuyerProject{}
-	connectionsNumber := int64(0)
-	highSuccessNumber := int64(0)
-	monitorNumber := int64(0)
 	for buyerId, buyerMap := range *dataMap {
 		buyerName := gconv.String(buyerMap["buyerName"])
 		count := int64(0)
 		if _, ok := (*projectMap)[buyerName]; ok {
 			projectMap := (*projectMap)[buyerName]
-			if isMonitor := gconv.Bool(buyerMap["isMonitor"]); isMonitor {
-				monitorNumber++
-			}
-			count = gconv.Int64(projectMap["count"])
 			money := gconv.Int64(projectMap["money"])
+			count = gconv.Int64(projectMap["count"])
 			zbtime := gconv.Int64(projectMap["zbtime"])
-			if count >= 2 {
-				highSuccessNumber++
-			}
 			returnData = append(returnData, BuyerProject{
 				BuyerId:   buyerId,
 				BuyerName: buyerName,
@@ -555,11 +610,11 @@ func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, s
 				CId:       gconv.String(buyerMap["cId"]),
 			})
 		} else {
-			/*if sourceType == "1" {
+			if sourceType == "1" {
 				if count < 2 {
 					continue
 				}
-			}*/
+			}
 			returnData = append(returnData, BuyerProject{
 				BuyerId:   buyerId,
 				BuyerName: buyerName,
@@ -576,9 +631,8 @@ func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, s
 			})
 
 		}
-		connectionsNumber++
 	}
-	return returnData, connectionsNumber, highSuccessNumber, monitorNumber
+	return returnData
 }
 
 // 已监控数据处理
@@ -601,16 +655,16 @@ func MonitorStatusInit(positionId int64, dataMap *map[string]map[string]interfac
 			v["isMonitor"] = true
 			(*newMap)[k] = v
 		} else {
-			/*if sourceType != "2" {
+			if sourceType != "2" {
 				v["isMonitor"] = false
 				(*newMap)[k] = v
-			}*/
+			}
 		}
 	}
-	/*if sourceType == "2" {
+	if sourceType == "2" {
 		*dataMap = *newMap
 		return
-	}*/
+	}
 	if newMap != nil {
 		*dataMap = *newMap
 	}
@@ -718,7 +772,7 @@ 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{}, *map[string]map[string]interface{}) {
+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{}) {
 	projectMap := &map[string]map[string]interface{}{}
 	returnMap := &map[string]map[string]interface{}{}
 	sql := ""
@@ -738,7 +792,7 @@ func ProjectHandle(buyerArr []string, entAccountId int64, entName, area, busines
 	endTime := time.Now().Unix()
 	fmt.Println("es用时时间:", endTime-startTime)
 	if len(data) == 0 {
-		return projectMap, returnMap, dataMap
+		return projectMap, dataMap
 	}
 	for _, object := range data {
 		buyerArr := BuyerAggStruct{}
@@ -796,7 +850,7 @@ func ProjectHandle(buyerArr []string, entAccountId int64, entName, area, busines
 		}
 
 	}
-	return projectMap, returnMap, dataMap
+	return projectMap, dataMap
 }
 
 type BuyerAggStruct struct {