zhaoqingang
2025-02-11 d0083d7bb85155fee5d929d436fb9c05625fd5c2
models/accessRegularity.go
@@ -15,16 +15,17 @@
type RegularityModel struct {
   OrgIds        []interface{} `json:"-"`
   AreaIds       []interface{} `json:"-"`
   IdentityType  string        // 人员身份类型 陌生人, 访客, 住户
   IdentityType  []string      // 人员身份类型 陌生人, 访客, 住户
   KeyPersonType string        // 重点人员类型
   PersonLabel   string        // 人员身份标签
   Task          *db.ModelTask
   AlarmType     db.AlarmType // 预警方式
   Appearances int // 出现次数
   Duration    int // 时间范围, 单位天
   StartHour   int // 开始计算的时间 配置为小时 23 - 02 表示第一天23点 - 第二天的2点
   EndHour     int // 结束时间
   Appearances   int    // 出现次数
   Duration      int    // 时间范围, 单位天
   LastDirection string // 最后一次出行的方向
   StartHour     int    // 开始计算的时间 配置为小时 23 - 02 表示第一天23点 - 第二天的2点
   EndHour       int    // 结束时间
}
func (m *RegularityModel) Init(task *db.ModelTask) error {
@@ -42,7 +43,19 @@
   m.AlarmType = task.AlarmType
   m.KeyPersonType = task.PersonType
   m.PersonLabel = task.PersonLabel
   m.IdentityType = task.IdentityType
   m.LastDirection = "out"
   if task.IdentityType != "" {
      for _, t := range strings.Split(task.IdentityType, ",") {
         if t == "all" {
            m.IdentityType = []string{"stranger", "visitor", "resident"}
            break
         } else {
            m.IdentityType = append(m.IdentityType, t)
         }
      }
   } else {
      m.IdentityType = []string{"stranger", "visitor", "resident"}
   }
   for _, v := range task.Rules {
      if v.Alias == "timeRange" {
@@ -64,6 +77,12 @@
            m.Duration = int(val)
         }
      }
      if v.Alias == "direction" {
         if val, ok := v.Value.(string); ok {
            m.LastDirection = val
         }
      }
   }
   // 默认计算30天的数据
@@ -82,6 +101,61 @@
func (m *RegularityModel) Run() error {
   results := make([]*db.ModelTaskResults, 0)
   baseFilter := make([]PersonInfo, 0)
   // 查找指定时间范围内出行过的档案编号
   now := time.Now()
   startDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -m.Duration)
   err := db.GetDB().Raw(`
      SELECT
         document_number,
         frequent_address,
         community_id,
      FROM
         snapshot_count_summary
      WHERE
         last_appearance_time > ? ""
         AND (p.community_id IN ?
         OR p.org_id IN ?)
         AND p.status IN ?
      `, startDate.Unix(), m.AreaIds, m.OrgIds, m.IdentityType).Scan(&baseFilter).Error
   if err != nil {
      logger.Warnf(err.Error())
   }
   esCli := db.GetEsClient()
   for _, p := range baseFilter {
      // 调用es分析此人的出行规律是否符合条件, 返回符合条件的次数和最后一次符合条件的时间
      sTime := startDate.Format(time.DateTime)
      eTime := time.Now().Format(time.DateTime)
      captures, err := service.QueryEsRecord(esCli, sTime, eTime, nil, []interface{}{p.CommunityId}, []string{p.DocumentNumber})
      if len(captures) == 0 || err != nil {
         continue
      }
      // 根据抓拍时间和出入方向,计算符合规则内的出入次数
      hitCount := 0
      //for _, c := range captures {
      //   captureTime, err := time.ParseInLocation(time.DateTime, c.PicDate, time.Local)
      //}
      // 写数据库
      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)
   }
   logger.Debugf("task %s last filter result %d", m.Task.Name, len(results))
   return service.SaveTaskResults(results)