From 9c31527d8606df99e45438cd0e1fcf47dcfff9cf Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期一, 10 二月 2025 18:46:46 +0800
Subject: [PATCH] 完善规律出行模型逻辑

---
 models/accessRegularity.go |   78 ++++++++++++++++++++++++++++++---------
 1 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/models/accessRegularity.go b/models/accessRegularity.go
index 54d5850..5d1b59b 100644
--- a/models/accessRegularity.go
+++ b/models/accessRegularity.go
@@ -134,27 +134,25 @@
 		}
 
 		// 鏍规嵁鎶撴媿鏃堕棿鍜屽嚭鍏ユ柟鍚戯紝璁$畻绗﹀悎瑙勫垯鍐呯殑鍑哄叆娆℃暟
-		hitCount := 0
+		hitCount := countValidDays(captures, m.StartHour, m.EndHour, m.LastDirection)
 
-		//for _, c := range captures {
-		//	captureTime, err := time.ParseInLocation(time.DateTime, c.PicDate, time.Local)
-		//}
+		if hitCount > m.Appearances {
+			// 鍐欐暟鎹簱
+			result := &db.ModelTaskResults{
+				Title:         m.Task.Name,
+				Event:         fmt.Sprintf("%s %d娆�", m.Task.Name, hitCount),
+				ModelID:       m.Task.ModelID,
+				ModelTaskID:   m.Task.ID,
+				CommunityId:   p.CommunityId,
+				OrgID:         p.OrgId,
+				ObjectIds:     p.DocumentNumber,
+				Location:      p.FrequentAddress,
+				PicDate:       time.Unix(p.LastAppearanceTime, 0).Format(time.DateTime),
+				FirstPersonID: p.DocumentNumber,
+			}
 
-		// 鍐欐暟鎹簱
-		result := &db.ModelTaskResults{
-			Title:         m.Task.Name,
-			Event:         fmt.Sprintf("%s %d娆�", m.Task.Name, hitCount),
-			ModelID:       m.Task.ModelID,
-			ModelTaskID:   m.Task.ID,
-			CommunityId:   p.CommunityId,
-			OrgID:         p.OrgId,
-			ObjectIds:     p.DocumentNumber,
-			Location:      p.FrequentAddress,
-			PicDate:       time.Unix(p.LastAppearanceTime, 0).Format(time.DateTime),
-			FirstPersonID: p.DocumentNumber,
+			results = append(results, result)
 		}
-
-		results = append(results, result)
 	}
 
 	logger.Debugf("task %s last filter result %d", m.Task.Name, len(results))
@@ -175,3 +173,47 @@
 func (m *RegularityModel) eventFormat() string {
 	return ""
 }
+
+func countValidDays(records []*service.ESRecordInfo, startHour, endHort int, direction string) int {
+	layout := "2006-01-02 15:04:05"             // 鏃堕棿鏍煎紡
+	lastDirectionMap := make(map[string]string) // 璁板綍鏈�鍚庝竴鏉� Direction
+	lastTimeMap := make(map[string]time.Time)   // 璁板綍鏈�鍚庝竴鏉℃椂闂�
+
+	for _, record := range records {
+		// 瑙f瀽鏃堕棿
+		t, err := time.Parse(layout, record.PicDate)
+		if err != nil {
+			fmt.Println("瑙f瀽鏃堕棿澶辫触:", err)
+			continue
+		}
+
+		// 鑾峰彇灏忔椂
+		hour := t.Hour()
+		var key string
+
+		// 鍒ゆ柇鏃堕棿鑼冨洿锛屽苟褰掑睘鍒版煇涓�澶�
+		if hour >= startHour { // 21:00-23:59 褰掑睘褰撳ぉ
+			key = t.Format("2006-01-02")
+		} else if hour < endHort { // 00:00-02:59 褰掑睘鍓嶄竴澶�
+			key = t.AddDate(0, 0, -1).Format("2006-01-02")
+		} else {
+			continue // 涓嶅湪缁熻鑼冨洿鍐�
+		}
+
+		// 璁板綍璇ユ椂闂存鍐呯殑鏈�鍚庝竴鏉℃暟鎹�
+		if lastTime, exists := lastTimeMap[key]; !exists || t.After(lastTime) {
+			lastTimeMap[key] = t
+			lastDirectionMap[key] = record.CameraLocation.Direction
+		}
+	}
+
+	// 缁熻绗﹀悎鏉′欢
+	count := 0
+	for _, dir := range lastDirectionMap {
+		if dir == direction || direction == "" {
+			count++
+		}
+	}
+
+	return count
+}

--
Gitblit v1.8.0