zhangzengfei
2025-02-10 9c31527d8606df99e45438cd0e1fcf47dcfff9cf
models/accessRegularity.go
@@ -134,12 +134,9 @@
      }
      // 根据抓拍时间和出入方向,计算符合规则内的出入次数
      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,
@@ -155,6 +152,7 @@
      }
      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 {
      // 解析时间
      t, err := time.Parse(layout, record.PicDate)
      if err != nil {
         fmt.Println("解析时间失败:", 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
}