From bc37c44c45d08c2cb42822e08adf429f4bcffb51 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期一, 10 二月 2025 11:36:20 +0800
Subject: [PATCH] 测试 场景分析 bug

---
 models/locationAnalysis.go |  209 ++++++++++++++++++++++-----------------------------
 1 files changed, 90 insertions(+), 119 deletions(-)

diff --git a/models/locationAnalysis.go b/models/locationAnalysis.go
index 1c72444..f0a1958 100644
--- a/models/locationAnalysis.go
+++ b/models/locationAnalysis.go
@@ -219,12 +219,13 @@
 	//logger.Debugf("task %s last result %d", m.Task.Name, len(lastFilter))
 	document_number_list = []string{}
 	for _, i := range keyFilter {
+		//logger.Debugf("keyFilter %s ------------- %s", i.Id, i.PersonType)
 		document_number_list = append(document_number_list, i.Id)
-		person := document_number_map[i.DocumentNumber]
+		person := document_number_map[i.Id]
 		person.PersonType = i.PersonType
-		document_number_map[i.DocumentNumber] = person
+		document_number_map[i.Id] = person
 	}
-	logger.Debugf("document_number_list %s last result %s", m.Task.Name, document_number_list)
+	//logger.Debugf("document_number_map %s last result %s", m.Task.Name, document_number_map)
 	records, err := queryEsLocation(db.GetEsClient(), m, document_number_list)
 	if err != nil {
 		return err
@@ -239,9 +240,11 @@
 	for _, record := range records {
 		tagTypes = []string{}
 		for _, personId := range record.DocumentNumbers {
+			logger.Debugf("document_number_map[personId] %s", document_number_map[personId])
 			tagTypes = append(tagTypes, document_number_map[personId].PersonType)
 			lastAppearanceTime = document_number_map[personId].LastAppearanceTime
 		}
+		logger.Debugf("tagTypes %s ---------------%s--------", tagTypes, record.DocumentNumbers)
 		_, typeNames, err := service.GetPersonTypeNameByTypes(tagTypes)
 		if err != nil {
 			return err
@@ -256,13 +259,15 @@
 			OrgID:         record.OrgId,
 			ObjectIds:     strings.Join(record.DocumentNumbers, ","),
 			Location:      fmt.Sprintf("%s%s%s", domains[record.CommunityId].Name, record.Building, record.Floor),
+			Building:      record.Building,
+			Floor:         record.Floor,
 			PicDate:       time.Unix(lastAppearanceTime, 0).Format("2006-01-02 15:04:05"),
 			FirstPersonID: record.DocumentNumbers[0],
 		}
 		results = append(results, result)
 	}
 	logger.Debugf("task %s last filter result %d", m.Task.Name, len(results))
-	return service.SaveTaskResults(results)
+	return service.SaveLocationTaskResults(results)
 }
 
 func (m *LocationModel) KeepAlive() error {
@@ -282,8 +287,8 @@
 
 func queryEsLocation(esClient *elasticsearch.Client, locationModel *LocationModel, documentNumbers []string) ([]*LocationRecord, error) {
 	var buf bytes.Buffer
-	//nowTime := time.Now()
-	//startTime := nowTime.Add(-time.Duration(locationModel.Duration) * 24 * time.Hour)
+	nowTime := time.Now()
+	startTime := nowTime.Add(-time.Duration(locationModel.Duration) * 24 * time.Hour)
 
 	// 鏋勫缓杩囨护鏉′欢
 	var filters []map[string]interface{}
@@ -324,38 +329,25 @@
 		}
 		filters = append(filters, addrParams)
 	}
+	var queryTimes []map[string]interface{}
+	for date := startTime; date.Before(nowTime); date = date.Add(24 * time.Hour) {
+		start := time.Date(date.Year(), date.Month(), date.Day(), locationModel.StartTime, 0, 0, 0, date.Location())
+		end := time.Date(date.Year(), date.Month(), date.Day(), locationModel.EndTime, 0, 0, 0, date.Location())
 
-	//// 閲嶇偣浜哄憳杩囨护
-	//if len(locationModel.KeyPersonType) > 0 {
-	//	filters = append(filters, map[string]interface{}{
-	//		"terms": map[string]interface{}{
-	//			"keyPersonType": strings.Split(locationModel.KeyPersonType, ","),
-	//		},
-	//	})
-	//}
-
-	// 鏃堕棿鑼冨洿
-	//filters = append(filters, map[string]interface{}{
-	//	"range": map[string]interface{}{
-	//		"picDate": map[string]interface{}{
-	//			"gte": start.Format(time.DateTime),
-	//			"lt":  now.Format(time.DateTime),
-	//		},
-	//	},
-	//})
-	//for date := startTime; date.Before(nowTime); date = date.Add(24 * time.Hour) {
-	//	start := time.Date(date.Year(), date.Month(), date.Day(), locationModel.StartTime, 0, 0, 0, date.Location())
-	//	end := time.Date(date.Year(), date.Month(), date.Day(), locationModel.EndTime, 0, 0, 0, date.Location())
-	//
-	//	filters = append(filters, map[string]interface{}{
-	//		"range": map[string]interface{}{
-	//			"picDate": map[string]interface{}{
-	//				"gte": start.Format("2006-01-02 15:04:05"),
-	//				"lte": end.Format("2006-01-02 15:04:05"),
-	//			},
-	//		},
-	//	})
-	//}
+		queryTimes = append(queryTimes, map[string]interface{}{
+			"range": map[string]interface{}{
+				"picDate": map[string]interface{}{
+					"gte": start.Format(time.DateTime), // "2006-01-02 15:04:05d"
+					"lte": end.Format(time.DateTime),
+				},
+			},
+		})
+	}
+	filters = append(filters, map[string]interface{}{
+		"bool": map[string]interface{}{
+			"should": queryTimes,
+		},
+	})
 	//logger.Debugf("filters--------------------------------------- %s", filters)
 	query := map[string]interface{}{
 		"query": map[string]interface{}{
@@ -401,6 +393,12 @@
 													},
 												},
 											},
+											"pic_date": map[string]interface{}{ // 鏂板鎸� documentNumber 鑱氬悎
+												"terms": map[string]interface{}{
+													"field": "picDate",
+													"size":  10000,
+												},
+											},
 											"document_numbers": map[string]interface{}{ // 鏂板鎸� documentNumber 鑱氬悎
 												"terms": map[string]interface{}{
 													"field": "documentNumber",
@@ -419,14 +417,6 @@
 		"size": 0,
 	}
 
-	query = map[string]interface{}{
-		"query": map[string]interface{}{
-			"bool": map[string]interface{}{
-				"filter": filters,
-			},
-		},
-		"size": 10000,
-	}
 	if err := json.NewEncoder(&buf).Encode(query); err != nil {
 		return nil, fmt.Errorf("error encoding query: %s", err)
 	}
@@ -453,84 +443,65 @@
 	if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
 		return nil, fmt.Errorf("error parsing response body: %s", err)
 	}
-	logger.Debugf("ressss--------------------------------------- %s", result)
+	//logger.Debugf("result--------------------------------------- %s", result)
 	// 瑙f瀽鑱氬悎缁撴灉
 	var records []*LocationRecord
-	//dat, ok := result["hits"].(map[string]interface{})
+	if aggs, ok := result["aggregations"].(map[string]interface{}); ok {
+		if orgBuckets, ok := aggs["orgs"].(map[string]interface{})["buckets"].([]interface{}); ok {
+			for _, orgBucket := range orgBuckets {
+				orgId := orgBucket.(map[string]interface{})["key"].(string)
+				//logger.Debugf("orgId--------------------------------------- %s", orgId)
+				// 瑙f瀽鎸塩ommunityId鐨勮仛鍚堢粨鏋�
+				if communityBuckets, ok := orgBucket.(map[string]interface{})["community"].(map[string]interface{})["buckets"].([]interface{}); ok {
+					for _, communityBucket := range communityBuckets {
+						communityId := communityBucket.(map[string]interface{})["key"].(string)
+						//logger.Debugf("communityId--------------------------------------- %s", communityId)
+						// 瑙f瀽鎸塨uilding鐨勮仛鍚堢粨鏋�
+						if locationBuckets, ok := communityBucket.(map[string]interface{})["location"].(map[string]interface{})["buckets"].([]interface{}); ok {
+							for _, locationBucket := range locationBuckets {
+								building := locationBucket.(map[string]interface{})["key"].(string)
+								//logger.Debugf("building--------------------------------------- %s", building)
+								// 瑙f瀽鎸塮loor鐨勮仛鍚堢粨鏋�
+								if floorBuckets, ok := locationBucket.(map[string]interface{})["floor"].(map[string]interface{})["buckets"].([]interface{}); ok {
+									for _, floorBucket := range floorBuckets {
+										floor := floorBucket.(map[string]interface{})["key"].(string)
+										//logger.Debugf("floor--------------------------------------- %s -----%s ", floor, floorBucket)
+										appearCount := floorBucket.(map[string]interface{})["doc_count"].(float64)
+										//logger.Debugf("appearCount--------------------------------------- %s- --", appearCount)
+										// 鏋勫缓 LocationRecord 缁撴瀯浣�
+										var persons []string
+										if docNumBuckets, ok := floorBucket.(map[string]interface{})["document_numbers"].(map[string]interface{})["buckets"].([]interface{}); ok {
+											for _, docNumBucket := range docNumBuckets {
+												persons = append(persons, docNumBucket.(map[string]interface{})["key"].(string))
+											}
+										}
+										var PicDate string
+										if docNumBuckets, ok := floorBucket.(map[string]interface{})["pic_data"].(map[string]interface{})["buckets"].([]interface{}); ok {
+											for _, docNumBucket := range docNumBuckets {
+												PicDate = docNumBucket.(map[string]interface{})["key"].(string)
+											}
+										}
+										logger.Debugf("floor, persons--,appearCount------------------------------------- %s-- %s --", floor, persons, appearCount)
+										record := &LocationRecord{
+											PicDate:         PicDate,
+											DocumentNumbers: persons,
+											CommunityId:     communityId,
+											Building:        building,
+											Floor:           floor,
+											OrgId:           orgId,
+											AppearCount:     int(appearCount),
+										}
 
-	dat := result["hits"].(map[string]interface{})
-
-	var data = make(map[string]interface{}, 2)
-
-	tmp, b := dat["total"].(map[string]interface{})
-	if b != true {
-		data["total"] = dat["total"].(float64)
-	} else {
-		data["total"] = tmp["value"].(float64)
+										records = append(records, record)
+									}
+								}
+							}
+						}
+					}
+				}
+			}
+		}
 	}
-	sources := []interface{}{}
-	for _, value := range dat["hits"].([]interface{}) {
-		sourceData := value.(map[string]interface{})
-		source := sourceData["_source"].(map[string]interface{})
-		source["_id"] = sourceData["_id"]
-		/*sdkType := source["sdkType"]
-		if sdkType != nil {
-			sdk, _ := strconv.Atoi(sdkType.(string))
-			source["sdkType"] = sdkTypeToValue(sdk)
-		}*/
-		sources = append(sources, source)
-	}
-	data["datalist"] = sources
-	logger.Debugf("ressss--------------data------------------------- %s", data)
-	//if aggs, ok := result["aggregations"].(map[string]interface{}); ok {
-	//	if orgBuckets, ok := aggs["orgs"].(map[string]interface{})["buckets"].([]interface{}); ok {
-	//		for _, orgBucket := range orgBuckets {
-	//			orgId := orgBucket.(map[string]interface{})["key"].(string)
-	//			logger.Debugf("orgId--------------------------------------- %s", orgId)
-	//			// 瑙f瀽鎸塩ommunityId鐨勮仛鍚堢粨鏋�
-	//			if communityBuckets, ok := orgBucket.(map[string]interface{})["community"].(map[string]interface{})["buckets"].([]interface{}); ok {
-	//				for _, communityBucket := range communityBuckets {
-	//					communityId := communityBucket.(map[string]interface{})["key"].(string)
-	//					logger.Debugf("communityId--------------------------------------- %s", communityId)
-	//					// 瑙f瀽鎸塨uilding鐨勮仛鍚堢粨鏋�
-	//					if locationBuckets, ok := communityBucket.(map[string]interface{})["location"].(map[string]interface{})["buckets"].([]interface{}); ok {
-	//						for _, locationBucket := range locationBuckets {
-	//							building := locationBucket.(map[string]interface{})["key"].(string)
-	//							logger.Debugf("building--------------------------------------- %s", building)
-	//							// 瑙f瀽鎸塮loor鐨勮仛鍚堢粨鏋�
-	//							if floorBuckets, ok := locationBucket.(map[string]interface{})["floor"].(map[string]interface{})["buckets"].([]interface{}); ok {
-	//								for _, floorBucket := range floorBuckets {
-	//									floor := floorBucket.(map[string]interface{})["key"].(string)
-	//									appearCount := floorBucket.(map[string]interface{})["filter_floor"].(int)
-	//									// 鏋勫缓 LocationRecord 缁撴瀯浣�
-	//									var persons []string
-	//									if docNumBuckets, ok := floorBucket.(map[string]interface{})["document_numbers"].(map[string]interface{})["buckets"].([]interface{}); ok {
-	//										for _, docNumBucket := range docNumBuckets {
-	//											persons = append(persons, docNumBucket.(map[string]interface{})["key"].(string))
-	//										}
-	//									}
-	//
-	//									logger.Debugf("floor--------------------------------------- %s-- %s --", floor, appearCount)
-	//									record := &LocationRecord{
-	//										//PicDate:        timestamp,
-	//										DocumentNumbers: persons,
-	//										CommunityId:     communityId,
-	//										Building:        building,
-	//										Floor:           floor,
-	//										OrgId:           orgId,
-	//										AppearCount:     appearCount,
-	//									}
-	//
-	//									records = append(records, record)
-	//								}
-	//							}
-	//						}
-	//					}
-	//				}
-	//			}
-	//		}
-	//	}
-	//}
 
 	return records, nil
 }

--
Gitblit v1.8.0