From 2e2e065ffcf79b90e2e42ec999886ef7e8f29fb7 Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期一, 10 二月 2025 16:36:24 +0800 Subject: [PATCH] 昼伏夜出完善 --- models/accessRegularity.go | 22 ++++- service/esSearch.go | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+), 4 deletions(-) diff --git a/models/accessRegularity.go b/models/accessRegularity.go index 78274b8..54d5850 100644 --- a/models/accessRegularity.go +++ b/models/accessRegularity.go @@ -105,11 +105,12 @@ // 鏌ユ壘鎸囧畾鏃堕棿鑼冨洿鍐呭嚭琛岃繃鐨勬。妗堢紪鍙� now := time.Now() - startDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -m.Duration).Unix() + 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 @@ -117,16 +118,29 @@ AND (p.community_id IN ? OR p.org_id IN ?) AND p.status IN ? - `, startDate, m.AreaIds, m.OrgIds, m.IdentityType).Scan(&baseFilter).Error + `, 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) + //} // 鍐欐暟鎹簱 - var hitCount int result := &db.ModelTaskResults{ Title: m.Task.Name, Event: fmt.Sprintf("%s %d娆�", m.Task.Name, hitCount), @@ -136,7 +150,7 @@ OrgID: p.OrgId, ObjectIds: p.DocumentNumber, Location: p.FrequentAddress, - PicDate: time.Unix(p.LastAppearanceTime, 0).Format("2006-01-02 15:04:05"), + PicDate: time.Unix(p.LastAppearanceTime, 0).Format(time.DateTime), FirstPersonID: p.DocumentNumber, } diff --git a/service/esSearch.go b/service/esSearch.go new file mode 100644 index 0000000..c5a14e9 --- /dev/null +++ b/service/esSearch.go @@ -0,0 +1,200 @@ +package service + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "github.com/elastic/go-elasticsearch/v6" + "model-engine/config" +) + +type CameraLocation struct { + Building string `json:"building"` + Unit string `json:"unit"` + Pos string `json:"pos"` + Floor string `json:"floor"` + Direction string `json:"direction"` + Latitude string `json:"latitude"` + Longitude string `json:"longitude"` +} + +func (cl *CameraLocation) Join() string { + return cl.Building + cl.Unit + cl.Pos + cl.Floor +} + +type ESRecordInfo struct { + Id string `json:"id"` + ClusterId string `json:"clusterId"` + CameraId string `json:"cameraId"` + CameraAddr string `json:"cameraAddr"` // 鎽勫儚鏈哄湴鍧� + CameraName string `json:"cameraName"` // 鎽勫儚鏈哄悕绉� + CommunityId string `json:"communityId"` // 灏忓尯ID + CommunityName string `json:"communityName"` // 灏忓尯鍚嶇О + PersonName string `json:"personName"` // 濮撳悕 + IdCard string `json:"idCard"` // 璇佷欢鍙风爜 + CameraLocation CameraLocation `json:"cameraLocation"` // 鎶撴媿浣嶇疆 + DocumentNumber string `json:"documentNumber"` // 妗f缂栧彿 + OrgId string `json:"orgId"` // 鏈烘瀯ID + OrgName string `json:"orgName"` // 鏈烘瀯鍚嶇О + PicDate string `json:"picDate"` + PicId string `json:"picId"` + PicMaxUrl string `json:"picMaxUrl"` + TaskId string `json:"taskId"` + TaskName string `json:"taskName"` + SdkName string `json:"sdkName"` + Content string `json:"content"` + ShowLabels string `json:"showLabels"` + OtherLabels string `json:"otherLabels"` + VideoUrl string `json:"videoUrl"` + IsAlarm bool `json:"isAlarm"` + IsAckAlarm bool `json:"isAckAlarm"` + IsDelete bool `json:"isDelete"` + IsKeyPerson bool `json:"isKeyPerson"` + KeyPersonType []string `json:"keyPersonType"` + DataSource string `json:"dataSource"` // 鏁版嵁鏉ユ簮:鎽勫儚鏈�, 鏁版嵁鏍� +} + +func QueryEsRecord(esClient *elasticsearch.Client, startTime, endTime string, orgIds, areaIds []interface{}, documentNumbers []string) ([]*ESRecordInfo, error) { + var buf bytes.Buffer + + // 鏋勫缓杩囨护鏉′欢 + var filters []map[string]interface{} + documentNumberFilter := map[string]interface{}{ + "terms": map[string]interface{}{ + "documentNumber": documentNumbers, + }, + } + filters = append(filters, documentNumberFilter) + + if len(orgIds) > 0 || len(areaIds) > 0 { + // 鑾峰彇鏁版嵁鏉冮檺杩囨护鏉′欢 + authFilters := getDomainFilters(orgIds, areaIds) + filters = append(filters, authFilters...) + } + + // 鏃堕棿鑼冨洿 + filters = append(filters, map[string]interface{}{ + "range": map[string]interface{}{ + "picDate": map[string]interface{}{ + "gte": startTime, + "lt": endTime, + }, + }, + }) + + query := map[string]interface{}{ + "query": map[string]interface{}{ + "bool": map[string]interface{}{ + "filter": filters, + }, + }, + "size": 0, + "sort": []interface{}{map[string]interface{}{"picDate": map[string]interface{}{"order": "asc"}}}, + "_source": map[string]interface{}{"includes": []interface{}{}, "excludes": []interface{}{"*.feature"}}, + } + + if err := json.NewEncoder(&buf).Encode(query); err != nil { + return nil, fmt.Errorf("error encoding query: %s", err) + } + + res, err := esClient.Search( + esClient.Search.WithContext(context.Background()), + esClient.Search.WithIndex(config.EsInfo.EsIndex.AiOcean.IndexName), + esClient.Search.WithDocumentType(config.EsInfo.EsIndex.AiOcean.IndexType), + esClient.Search.WithBody(&buf), + esClient.Search.WithTrackTotalHits(true), + esClient.Search.WithPretty(), + ) + if err != nil { + return nil, fmt.Errorf("error getting response: %s", err) + } + defer res.Body.Close() + + // Check for a successful status code (2xx range) + if res.IsError() { + return nil, fmt.Errorf("error getting response: %s", res.String()) + } + + var result map[string]interface{} + if err := json.NewDecoder(res.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("error parsing response body: %s", err) + } + + if _, ok := result["hits"]; !ok || result["hits"] == nil { + return nil, nil + } + + // 瑙f瀽鏌ヨ缁撴灉 + dat := result["hits"].(map[string]interface{}) + var records = make([]*ESRecordInfo, len(dat["hits"].([]interface{}))) + + for idx, value := range dat["hits"].([]interface{}) { + sourceData := value.(map[string]interface{}) + source := sourceData["_source"].(map[string]interface{}) + + var record = ESRecordInfo{ + Id: source["id"].(string), + CameraId: source["cameraId"].(string), + CameraAddr: source["cameraAddr"].(string), + CameraName: source["cameraName"].(string), + CommunityId: source["communityId"].(string), + CommunityName: source["communityName"].(string), + DocumentNumber: source["documentNumber"].(string), + OrgId: source["orgId"].(string), + OrgName: source["orgName"].(string), + PicDate: source["picDate"].(string), + PicId: source["picId"].(string), + PicMaxUrl: source["picMaxUrl"].(string), + } + + cameraLocation := source["cameraId"].(map[string]interface{}) + record.CameraLocation = CameraLocation{ + Building: cameraLocation["building"].(string), + Unit: cameraLocation["unit"].(string), + Pos: cameraLocation["pos"].(string), + Floor: cameraLocation["floor"].(string), + Direction: cameraLocation["direction"].(string), + } + + records[idx] = &record + } + + return records, nil +} + +func getDomainFilters(orgIds, areaIds []interface{}) (filters []map[string]interface{}) { + filters = make([]map[string]interface{}, 0) + // 鏁版嵁鏉冮檺杩囨护 + if len(orgIds) > 0 && len(areaIds) > 0 { + var authParams = map[string]interface{}{ + "bool": map[string]interface{}{ + "should": []interface{}{ + map[string]interface{}{ + "terms": map[string]interface{}{ + "orgId": orgIds, + }}, + map[string]interface{}{ + "terms": map[string]interface{}{ + "communityId": areaIds, + }}, + }, + }, + } + filters = append(filters, authParams) + + } else if len(orgIds) > 0 { + filters = append(filters, map[string]interface{}{ + "terms": map[string]interface{}{ + "orgId": orgIds, + }, + }) + } else if len(areaIds) > 0 { + filters = append(filters, map[string]interface{}{ + "terms": map[string]interface{}{ + "communityId": areaIds, + }, + }) + } + return filters +} -- Gitblit v1.8.0