zhaoqingang
2025-02-10 9f1b86b7f2732432aed2c13165c95812e57d4f49
models/locationAnalysis.go
@@ -65,9 +65,9 @@
      m.KeyTypes = strings.Split(task.PersonType, ",")
   }
   if len(m.AreaIds) == 0 {
      m.AreaIds = m.OrgIds
   }
   //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 {
@@ -136,6 +136,10 @@
   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,
@@ -148,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())
   }
@@ -278,8 +282,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{}
@@ -339,20 +343,20 @@
   //      },
   //   },
   //})
   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"),
            },
         },
      })
   }
   //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{}{
         "bool": map[string]interface{}{
@@ -415,6 +419,14 @@
      "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)
   }
@@ -441,9 +453,35 @@
   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)
   // 解析聚合结果
   var records []*LocationRecord
   //dat, ok := result["hits"].(map[string]interface{})
   //dat := result["hits"].(map[string]interface{})
   //
   //var data = make(map[string]interface{}, 2)
   //
   //tmp, b := dat["total"].(map[string]interface{})
   //if b != true {
   //   data["total"] = dat["total"].(float64)
   //} else {
   //   data["total"] = tmp["value"].(float64)
   //}
   //sources := []interface{}{}
   //for _, value := range dat["hits"].([]interface{}) {
   //   sourceData := value.(map[string]interface{})
   //   source := sourceData["_source"].(map[string]interface{})
   //   source["_id"] = sourceData["_id"]
   //   /*sdkType := source["sdkType"]
   //   if sdkType != nil {
   //      sdk, _ := strconv.Atoi(sdkType.(string))
   //      source["sdkType"] = sdkTypeToValue(sdk)
   //   }*/
   //   sources = append(sources, source)
   //}
   //data["datalist"] = sources
   //logger.Debugf("ressss--------------data------------------------- %s", data)
   if aggs, ok := result["aggregations"].(map[string]interface{}); ok {
      if orgBuckets, ok := aggs["orgs"].(map[string]interface{})["buckets"].([]interface{}); ok {
         for _, orgBucket := range orgBuckets {
@@ -463,7 +501,9 @@
                        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)
                              logger.Debugf("floor--------------------------------------- %s -----%s ", floor, floorBucket)
                              appearCount := floorBucket.(map[string]interface{})["filter_floor"].(int)
                              logger.Debugf("appearCount--------------------------------------- %s- --", appearCount)
                              // 构建 LocationRecord 结构体
                              var persons []string
                              if docNumBuckets, ok := floorBucket.(map[string]interface{})["document_numbers"].(map[string]interface{})["buckets"].([]interface{}); ok {
@@ -472,7 +512,7 @@
                                 }
                              }
                              logger.Debugf("floor--------------------------------------- %s-- %s --", floor, appearCount)
                              //logger.Debugf("floor--------------------------------------- %s-- %s --", floor, appearCount)
                              record := &LocationRecord{
                                 //PicDate:        timestamp,
                                 DocumentNumbers: persons,