zhaoqingang
2025-02-10 e0ac037f3d30e5fe50c884789eef97746a9e990f
models/locationAnalysis.go
@@ -242,6 +242,7 @@
         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
@@ -256,6 +257,8 @@
         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],
      }
@@ -282,8 +285,8 @@
func queryEsLocation(esClient *elasticsearch.Client, locationModel *LocationModel, documentNumbers []string) ([]*LocationRecord, error) {
   var buf bytes.Buffer
   nowTime := time.Now()
   startTime := nowTime.Add(-time.Duration(locationModel.Duration) * 24 * time.Hour)
   //nowTime := time.Now()
   //startTime := nowTime.Add(-time.Duration(locationModel.Duration) * 24 * time.Hour)
   // 构建过滤条件
   var filters []map[string]interface{}
@@ -324,38 +327,34 @@
      }
      filters = append(filters, addrParams)
   }
   //// 重点人员过滤
   //if len(locationModel.KeyPersonType) > 0 {
   //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())
   //
   //   //queryTimes = append(queryTimes, map[string]interface{}{
   //   //   "range": map[string]interface{}{
   //   //      "picDate": map[string]interface{}{
   //   //         "gte": start.Format(time.DateTime), // "2006-01-02 15:04:05d"
   //   //         "lte": end.Format(time.DateTime),
   //   //      },
   //   //   },
   //   //})
   //
   //   filters = append(filters, map[string]interface{}{
   //      "terms": map[string]interface{}{
   //         "keyPersonType": strings.Split(locationModel.KeyPersonType, ","),
   //      "range": map[string]interface{}{
   //         "picDate": map[string]interface{}{
   //            "gte": start.Format(time.DateTime),
   //            "lte": end.Format(time.DateTime),
   //         },
   //      },
   //   })
   //}
   // 时间范围
   //filters = append(filters, map[string]interface{}{
   //   "range": map[string]interface{}{
   //      "picDate": map[string]interface{}{
   //         "gte": start.Format(time.DateTime),
   //         "lt":  now.Format(time.DateTime),
   //      },
   //   "bool": map[string]interface{}{
   //      "filter": queryTimes,
   //   },
   //})
   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{}{
         "range": map[string]interface{}{
            "picDate": map[string]interface{}{
               "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{}{
@@ -419,14 +418,6 @@
      "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)
   }
@@ -453,29 +444,31 @@
   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)
   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)
            //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)
                  //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)
                        //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 {
@@ -484,7 +477,7 @@
                                 }
                              }
                              logger.Debugf("floor--------------------------------------- %s-- %s --", floor, appearCount)
                              logger.Debugf("floor, persons--,appearCount------------------------------------- %s-- %s --", floor, persons, appearCount)
                              record := &LocationRecord{
                                 //PicDate:        timestamp,
                                 DocumentNumbers: persons,
@@ -492,7 +485,7 @@
                                 Building:        building,
                                 Floor:           floor,
                                 OrgId:           orgId,
                                 AppearCount:     appearCount,
                                 AppearCount:     int(appearCount),
                              }
                              records = append(records, record)