zhaoqingang
2025-02-10 bc37c44c45d08c2cb42822e08adf429f4bcffb51
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,29 +207,30 @@
      WHERE
         p.id IN ?
         AND k.person_type IN ?
      `, m.StartTime, 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 {
      //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_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
   }
   logger.Debugf("records %s last result %s", m.Task.Name, records)
   domains, err := domainToLocation(records)
   if err != nil {
      return err
@@ -234,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
@@ -251,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 {
@@ -319,39 +329,26 @@
      }
      filters = append(filters, addrParams)
   }
   //// 重点人员过滤
   //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),
   //      },
   //   },
   //})
   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())
      filters = append(filters, map[string]interface{}{
      queryTimes = append(queryTimes, map[string]interface{}{
         "range": map[string]interface{}{
            "picDate": map[string]interface{}{
               "gte": start.Format(time.RFC3339),
               "lte": end.Format(time.RFC3339),
               "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{}{
         "bool": map[string]interface{}{
@@ -394,6 +391,12 @@
                                             "threshold": locationModel.Appearances,
                                          },
                                       },
                                    },
                                 },
                                 "pic_date": map[string]interface{}{ // 新增按 documentNumber 聚合
                                    "terms": map[string]interface{}{
                                       "field": "picDate",
                                       "size":  10000,
                                    },
                                 },
                                 "document_numbers": map[string]interface{}{ // 新增按 documentNumber 聚合
@@ -440,29 +443,31 @@
   if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
      return nil, fmt.Errorf("error parsing response body: %s", err)
   }
   //logger.Debugf("result--------------------------------------- %s", result)
   // 解析聚合结果
   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)
            //logger.Debugf("orgId--------------------------------------- %s", orgId)
            // 解析按communityId的聚合结果
            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)
                  // 解析按building的聚合结果
                  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)
                        // 解析按floor的聚合结果
                        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)
                              //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 {
@@ -470,14 +475,21 @@
                                    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:        timestamp,
                                 PicDate:         PicDate,
                                 DocumentNumbers: persons,
                                 CommunityId:     communityId,
                                 Building:        building,
                                 Floor:           floor,
                                 OrgId:           orgId,
                                 AppearCount:     appearCount,
                                 AppearCount:     int(appearCount),
                              }
                              records = append(records, record)