From 334e50dcefc34b1625c8fa65a3774cdc6c795f94 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期六, 08 二月 2025 19:30:57 +0800
Subject: [PATCH] 测试 场景分析 bug

---
 models/locationAnalysis.go |  175 ++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 108 insertions(+), 67 deletions(-)

diff --git a/models/locationAnalysis.go b/models/locationAnalysis.go
index 79eaf36..347927f 100644
--- a/models/locationAnalysis.go
+++ b/models/locationAnalysis.go
@@ -28,6 +28,7 @@
 	KeyPersonType  string       // 浜哄憳绫诲瀷
 	PersonLabel    string       // 浜哄憳鏍囩
 	PersonIdentity []string     // 浜哄憳韬唤
+	KeyTypes       []string     // 浜哄憳韬唤
 	Duration       int          // 鏃堕棿鑼冨洿
 	Appearances    int          // 鍑虹幇娆℃暟,
 	StartTime      int          // 鏃堕棿鑼冨洿, 寮�濮嬫椂闂�
@@ -54,15 +55,19 @@
 	m.Building = task.Building
 	m.AlarmType = task.AlarmType
 	m.PersonIdentity = []string{"stranger", "visitor", "resident"} //task.IdentityType
+	m.KeyTypes = []string{"1", "2", "3", "4", "5"}                 //task.IdentityType
 	m.KeyPersonType = task.PersonType
 	m.PersonLabel = task.PersonLabel
 	if task.IdentityType != "" {
 		m.PersonIdentity = strings.Split(task.IdentityType, ",")
 	}
-
-	if len(m.AreaIds) == 0 {
-		m.AreaIds = m.OrgIds
+	if task.PersonType != "" {
+		m.KeyTypes = strings.Split(task.PersonType, ",")
 	}
+
+	//if len(m.AreaIds) == 0 {
+	//	m.AreaIds = m.OrgIds
+	//}
 	for _, v := range task.Rules {
 		if v.Alias == "appearances" {
 			if val, ok := v.Value.(float64); ok {
@@ -128,9 +133,13 @@
 	// 鏍规嵁閰嶇疆鐨勬椂闂存澶╂暟, 姣忓ぉ鐨勬椂闂磋寖鍥村唴锛� 閲嶇偣浜哄憳绫诲瀷鎴栬�呯壒瀹氭爣绛句汉鍛樺嚭鐜扮殑妤煎眰娆℃暟瓒呰繃闃堝��
 
 	results := make([]*db.ModelTaskResults, 0)
-	var baseFilter, labelFilter, keyFilter, lastFilter []LocationPersonInfo
+	var baseFilter, labelFilter, keyFilter []LocationPersonInfo
 	var document_number_map = make(map[string]LocationPersonInfo)
 	var document_number_list []string
+	community_ids := m.AreaIds
+	if len(community_ids) == 0 {
+		community_ids = m.OrgIds
+	}
 	err := db.GetDB().Raw(`
 		SELECT
 		    p.id,
@@ -143,7 +152,7 @@
 			p.id_card != "" 
 			AND (s.community_id IN ? OR s.org_id IN ?)
 			AND s.status IN ?
-		`, m.AreaIds, m.OrgIds, m.PersonIdentity).Scan(&baseFilter).Error
+		`, community_ids, m.OrgIds, m.PersonIdentity).Scan(&baseFilter).Error
 	if err != nil {
 		logger.Warnf(err.Error())
 	}
@@ -181,17 +190,13 @@
 		}
 
 		logger.Debugf("task %s match label result %d", m.Task.Name, len(labelFilter))
+		document_number_list = []string{}
+		for _, i := range labelFilter {
+			document_number_list = append(document_number_list, i.Id)
+		}
 	}
-
-	document_number_list = []string{}
-	for _, i := range labelFilter {
-
-		document_number_list = append(document_number_list, i.Id)
-
-	}
-
-	if m.KeyPersonType != "" {
-		keyTypes := strings.Split(m.KeyPersonType, ",")
+	if len(m.KeyTypes) > 0 {
+		//keyTypes := strings.Split(m.KeyPersonType, ",")
 		err := db.GetDB().Raw(`
 		SELECT
 			p.id,
@@ -202,18 +207,16 @@
 		WHERE
 			p.id IN ?
 			AND k.person_type IN ?
-		`, document_number_list, keyTypes).Scan(&keyFilter).Error
+		`, document_number_list, m.KeyTypes).Scan(&keyFilter).Error
 		if err != nil {
 			logger.Warnf(err.Error())
 		}
 		if len(keyFilter) == 0 {
-			return fmt.Errorf("no results found that match the key condition %s", m.KeyPersonType)
+			return fmt.Errorf("no results found that match the key condition%s------ %s", document_number_list, m.KeyTypes)
 		}
-
 		logger.Debugf("task %s match key person result %d", m.Task.Name, len(keyFilter))
 	}
-
-	logger.Debugf("task %s last result %d", m.Task.Name, len(lastFilter))
+	//logger.Debugf("task %s last result %d", m.Task.Name, len(lastFilter))
 	document_number_list = []string{}
 	for _, i := range keyFilter {
 		document_number_list = append(document_number_list, i.Id)
@@ -221,10 +224,12 @@
 		person.PersonType = i.PersonType
 		document_number_map[i.DocumentNumber] = person
 	}
+	logger.Debugf("document_number_list %s last result %s", m.Task.Name, document_number_list)
 	records, err := queryEsLocation(db.GetEsClient(), m, document_number_list)
 	if err != nil {
 		return err
 	}
+	logger.Debugf("records %s last result %s", m.Task.Name, records)
 	domains, err := domainToLocation(records)
 	if err != nil {
 		return err
@@ -345,13 +350,13 @@
 		filters = append(filters, map[string]interface{}{
 			"range": map[string]interface{}{
 				"picDate": map[string]interface{}{
-					"gte": start.Format(time.RFC3339),
-					"lte": end.Format(time.RFC3339),
+					"gte": start.Format("2006-01-02 15:04:05"),
+					"lte": end.Format("2006-01-02 15:04:05"),
 				},
 			},
 		})
 	}
-
+	//logger.Debugf("filters--------------------------------------- %s", filters)
 	query := map[string]interface{}{
 		"query": map[string]interface{}{
 			"bool": map[string]interface{}{
@@ -414,6 +419,14 @@
 		"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)
 	}
@@ -440,56 +453,84 @@
 	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)
 	// 瑙f瀽鑱氬悎缁撴灉
 	var records []*LocationRecord
-	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)
+	//dat, ok := result["hits"].(map[string]interface{})
 
-				// 瑙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)
+	dat := result["hits"].(map[string]interface{})
 
-						// 瑙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)
+	var data = make(map[string]interface{}, 2)
 
-								// 瑙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))
-											}
-										}
-										record := &LocationRecord{
-											//PicDate:        timestamp,
-											DocumentNumbers: persons,
-											CommunityId:     communityId,
-											Building:        building,
-											Floor:           floor,
-											OrgId:           orgId,
-											AppearCount:     appearCount,
-										}
-
-										records = append(records, record)
-									}
-								}
-							}
-						}
-					}
-				}
-			}
-		}
+	tmp, b := dat["total"].(map[string]interface{})
+	if b != true {
+		data["total"] = dat["total"].(float64)
+	} else {
+		data["total"] = tmp["value"].(float64)
 	}
+	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