From ef51da5404827e826e979ad614950a9e0192f4c6 Mon Sep 17 00:00:00 2001 From: qixiaoning <jony.kee@outlook.com> Date: 星期五, 08 八月 2025 09:15:26 +0800 Subject: [PATCH] 调整数据推送相关接口 --- camera-common/models/Record.go | 1024 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 512 insertions(+), 512 deletions(-) diff --git a/camera-common/models/Record.go b/camera-common/models/Record.go index 6901768..6c8012c 100644 --- a/camera-common/models/Record.go +++ b/camera-common/models/Record.go @@ -1,512 +1,512 @@ -package models - -import ( - "context" - "fmt" - "math" - "sort" - "strconv" - "strings" - "time" - - "github.com/milvus-io/milvus-sdk-go/v2/entity" -) - -// DataOperator 鏁版嵁鎿嶄綔鐩稿叧鏂规硶 -type DataOperator struct { - client *MilvusClient -} - -// NewDataOperator 鍒涘缓鏁版嵁鎿嶄綔鍣� -func NewDataOperator(client *MilvusClient) *DataOperator { - return &DataOperator{client: client} -} - -type PicWidHei struct { - PicW int `json:"picW"` - PicH int `json:"picH"` -} - -// 缁熻鏌ヨ -type StaticRecord struct { - Id string `json:"id"` - TaskId []int64 `json:"task_id"` - RuleId []int64 `json:"rule_id"` - EventLevelId []int64 `json:"event_level_id"` - VideoPointId int64 `json:"video_point_id"` - DetectId []int64 `json:"detect_id"` - VideoName string `json:"video_name"` - TaskNames []TaskOption `json:"task_names"` - CheckNames []CheckOption `json:"check_names"` - RuleNames []RuleOption `json:"rule_names"` - EventLevels []DictOption `json:"event_levels"` - KnowledgeDocuments []KnowledgeDocumentOption `json:"knowledge_documents"` - ImagePath string `json:"image_path"` - VideoPath string `json:"video_path"` - DetectTime string `json:"detect_time"` - IsWarning int64 `json:"is_warning"` - ZhDescClass string `json:"zh_desc_class"` - TaskName string `json:"task_name"` - EventLevelName string `json:"event_level_name"` - DetectNum int64 `json:"detect_num"` - Suggestion string `json:"suggestion"` - RiskDescription string `json:"risk_description"` - KnowledgeId []int64 `json:"knowledge_id"` - IsDesc int64 `json:"is_desc"` - CameraId string `json:"cameraId"` - CameraName string `json:"cameraName"` - CameraAddr string `json:"cameraAddr"` - PicDate string `json:"picDate"` - PicId int64 `json:"picId"` - PicMaxUrl []string `json:"picMaxUrl"` - PicSrcUrl []string `json:"picSrcUrl"` - PicWH PicWidHei `json:"picWH"` - SdkName string `json:"sdkName"` - Content string `json:"content"` - AlarmRules []AlarmRule `json:"alarmRules"` - LikeDate string `json:"likeDate"` - ShowLabels string `json:"showLabels"` - OtherLabels string `json:"otherLabels"` - VideoUrl string `json:"videoUrl"` - AnalyServerId string `json:"analyServerId"` - AnalyServerName string `json:"analyServerName"` - AnalyServerIp string `json:"analyServerIp"` - ClusterId string `json:"clusterId"` - IsAlarm bool `json:"isAlarm"` - IsAckAlarm bool `json:"isAckAlarm"` - IsCollect bool `json:"isCollect"` - IsDelete bool `json:"isDelete"` - BaseInfo []*BaseCompareInfo `json:"baseInfo"` - TargetInfo []TargetInfo `json:"targetInfo"` - FileId string `json:"fileId"` // 鏁版嵁鏍堟枃浠秈d - DataSource string `json:"dataSource"` // 鏁版嵁鏉ユ簮:鎽勫儚鏈�, 鏁版嵁鏍� -} - -type Point struct { - X float64 `json:"x"` - Y float64 `json:"y"` -} - -type Points struct { - TopLeft Point `json:"topLeft"` - BottomRight Point `json:"bottomRight"` -} - -type BaseCompareInfo struct { - TableId string `json:"tableId"` - TableName string `json:"tableName"` - BwType string `json:"bwType"` - CompareScore float64 `json:"compareScore"` - TargetId string `json:"targetId"` - TargetName string `json:"targetName"` - TargetPicUrl string `json:"targetPicUrl"` - MonitorLevel string `json:"monitorLevel"` - Content string `json:"content"` - DbLabel string `json:"labels"` -} - -type TargetInfo struct { - TargetId string `json:"targetId"` - AreaId string `json:"areaId"` - AreaName string `json:"areaName"` - TargetScore float64 `json:"targetScore"` - TargetType string `json:"targetType"` - Feature string `json:"feature"` - PicSmUrl string `json:"picSmUrl"` - TargetLocation Points `json:"targetLocation"` - BelongsTargetID string `json:"belongsTargetId"` - Attribute string `json:"attribute"` -} - -type AlarmRule struct { - AlarmLevel string `db:"alarmLevel" json:"alarmLevel"` - DefenceState bool `db:"defenceState" json:"defenceState"` - GroupId string `db:"groupId" json:"groupId"` - LinkInfo string `db:"linkInfo" json:"linkInfo"` - RuleText string `db:"ruleText" json:"ruleText"` -} - -type TaskOption struct { - TaskId int64 `db:"task_id" json:"taskId"` - TaskName string `db:"task_name" json:"taskName"` -} - -type CheckOption struct { - TaskId int64 `db:"task_id" json:"taskId,omitempty"` - CheckId int64 `db:"check_id" json:"checkId"` - FileName string `db:"file_name" json:"fileName"` -} - -type RuleOption struct { - TaskId int64 `db:"task_id" json:"taskId,omitempty"` - RuleId int64 `db:"rule_id" json:"ruleId"` - FileName string `db:"file_name" json:"fileName"` -} - -// 瀛楀吀 -type DictOption struct { - DictId int64 `db:"dict_id" json:"dictId"` - DictName string `db:"dict_name" json:"dictName"` - DictValue string `db:"dict_value" json:"dictValue"` - DictType string `db:"dict_type" json:"dictType"` -} - -// 鐭ヨ瘑搴� -type KnowledgeDocumentOption struct { - Id string `db:"id" json:"id"` - Title string `db:"title" json:"title"` - KnowId int64 `db:"know_id" json:"know_id"` - FileUrl string `db:"title" json:"file_url"` -} - -// Pagination 鍒嗛〉淇℃伅 -type Pagination struct { - Page int `json:"page"` // 褰撳墠椤电爜 - PageSize int `json:"pageSize"` // 姣忛〉澶у皬 - Total int `json:"total"` // 鎬昏褰曟暟 - TotalPage int `json:"totalPage"` // 鎬婚〉鏁� -} - -// PaginatedResult 鍒嗛〉缁撴灉 -type PaginatedResult struct { - Items []StaticRecord `json:"list"` - Pagination Pagination `json:"pagination"` -} - -type PaginatedResult2 struct { - Items map[string]interface{} `json:"list"` - Pagination Pagination `json:"pagination"` -} - -// 璁板綍 -type RecordReq struct { - Ids []string `json:"ids"` - VideoIds []string `json:"treeNodes"` - TaskIds []int64 `json:"taskIds` - TaskName []string `json:"taskName"` - Warning int64 `json:"warning` - Page int64 `json:"page"` - PageSize int64 `json:"pageSize"` -} - -// 鏍规嵁鍒嗛〉鍙栨暟鎹� -func GetWithPage(collectionName string, pageNum int64, pageSize int64, filter string) (*PaginatedResult, error) { - - if pageNum < 1 { - pageNum = 1 - } - if pageSize < 1 { - pageSize = 10 - } - - if dbClient == nil { - return nil, nil - } - - dataOp := &DataOperator{client: dbClient} - - fmt.Println(filter) - records, err := dataOp.queryWithPagination(collectionName, filter, pageNum, pageSize) - if err != nil { - return nil, err - } - - return records, nil -} - -// 杞崲鏁版嵁 -func convertResultToMap(result []entity.Column) []StaticRecord { - if len(result) == 0 { - return nil - } - count := result[0].Len() - var records []StaticRecord - for i := 0; i < count; i++ { - record := StaticRecord{} - for _, field := range result { - switch field.Name() { - case "id": - IdColumn := field.(*entity.ColumnInt64).Data() - if len(IdColumn) > 0 { - record.Id = strconv.FormatInt(IdColumn[i], 10) - } - case "task_id": - TaskIdColumn := field.(*entity.ColumnInt64Array).Data() - if len(TaskIdColumn) > 0 { - record.TaskId = TaskIdColumn[i] - } - case "rule_id": - RuleIdColumn := field.(*entity.ColumnInt64Array).Data() - if len(RuleIdColumn) > 0 { - record.RuleId = RuleIdColumn[i] - } - case "event_level_id": - EventLevelIdColumn := field.(*entity.ColumnInt64Array).Data() - if len(EventLevelIdColumn) > 0 { - record.EventLevelId = EventLevelIdColumn[i] - } - case "video_point_id": - VideoPointIdColumn := field.(*entity.ColumnInt64).Data() - if len(VideoPointIdColumn) > 0 { - record.VideoPointId = VideoPointIdColumn[i] - } - case "detect_id": - DetectIdColumn := field.(*entity.ColumnInt64Array).Data() - if len(DetectIdColumn) > 0 { - record.DetectId = DetectIdColumn[i] - } - case "image_path": - ImagePathColumn := field.(*entity.ColumnVarChar).Data() - if len(ImagePathColumn) > 0 { - record.ImagePath = ImagePathColumn[i] - } - case "video_path": - VideoPathColumn := field.(*entity.ColumnVarChar).Data() - if len(VideoPathColumn) > 0 { - record.VideoPath = VideoPathColumn[i] - } - case "zh_desc_class": - zhColumn := field.(*entity.ColumnVarChar).Data() - if len(zhColumn) > 0 { - record.ZhDescClass = zhColumn[i] - } - case "task_name": - TNColumn := field.(*entity.ColumnVarChar).Data() - if len(TNColumn) > 0 { - record.TaskName = TNColumn[i] - } - case "event_level_name": - EVColumn := field.(*entity.ColumnVarChar).Data() - if len(EVColumn) > 0 { - record.EventLevelName = EVColumn[i] - } - case "is_desc": - descColumn := field.(*entity.ColumnInt64).Data() - if len(descColumn) > 0 { - record.IsDesc = descColumn[i] - } - case "detect_num": - DEColumn := field.(*entity.ColumnInt64).Data() - if len(DEColumn) > 0 { - record.DetectNum = DEColumn[i] - } - case "is_waning": - warnColumn := field.(*entity.ColumnInt64).Data() - if len(warnColumn) > 0 { - record.IsWarning = warnColumn[i] - } - case "detect_time": - DetectTimeColumn := field.(*entity.ColumnVarChar).Data() - if len(DetectTimeColumn) > 0 { - record.DetectTime = DetectTimeColumn[i] - } - case "knowledge_id": - KnowledgeIdColumn := field.(*entity.ColumnInt64Array).Data() - if len(KnowledgeIdColumn) > 0 { - record.KnowledgeId = KnowledgeIdColumn[i] - } - case "suggestion": - SuggestionColumn := field.(*entity.ColumnVarChar).Data() - if len(SuggestionColumn) > 0 { - record.Suggestion = SuggestionColumn[i] - } - case "risk_description": - RiskDescriptionColumn := field.(*entity.ColumnVarChar).Data() - if len(RiskDescriptionColumn) > 0 { - record.RiskDescription = RiskDescriptionColumn[i] - } - } - } - records = append(records, record) - } - return records -} - -// 鍒嗛〉鏌ヨ -func (do *DataOperator) queryWithPagination(collectionName string, filterExpr string, pageNum int64, pageSize int64) (*PaginatedResult, error) { - ctx := context.Background() - - // 璁$畻鍋忕Щ閲� - //offset := (pageNum - 1) * pageSize - - // 鏋勫缓鏌ヨ琛ㄨ揪寮忥紙Milvus 2.1+ 鏀寔 limit/offset锛� - //queryExpr := fmt.Sprintf("%s limit %d offset %d", filterExpr, pageSize, offset) - - total, err := do.getTotalCount(collectionName, filterExpr) - if err != nil { - return nil, err - } - - // 璁$畻鎬婚〉鏁� - totalPages := int(math.Ceil(float64(total) / float64(pageSize))) - - // 鎵ц鏌ヨ - result, err := do.client.client.Query( - ctx, - collectionName, - []string{}, // 鎵�鏈夊垎鍖� - filterExpr, - []string{"rule_id", "task_id", "is_waning", "zh_desc_class", "task_name", "event_level_name", "detect_num", - "event_level_id", "video_point_id", "detect_id", "image_path", - "video_path", "detect_time", "knowledge_id", "risk_description", "suggestion", "id", "is_desc"}, // 杩斿洖鎵�鏈夊瓧娈� - // client.WithLimit(pageSize), - // client.WithOffset(offset), - ) - if err != nil { - return nil, fmt.Errorf("鍒嗛〉鏌ヨ澶辫触: %v", err) - } - lists := convertResultToMap(result) - layout := "2006-01-02 15:04:05.999999" - - var temp1 time.Time - var temp2 time.Time - - //鎺掑簭 - sort.Slice(lists, func(i, j int) bool { - //return lists[i].DetectTime > lists[j].:DetectTime - temp1, _ = time.Parse(layout, lists[i].DetectTime) - temp2, _ = time.Parse(layout, lists[j].DetectTime) - return temp1.After(temp2) - }) - - items := Paginate(lists, int(pageNum), int(pageSize)) - return &PaginatedResult{ - Items: items, - Pagination: Pagination{ - Page: int(pageNum), - PageSize: int(pageSize), - Total: int(total), - TotalPage: totalPages, - }, - }, nil -} - -// 鍒嗛〉鏁版嵁 -func Paginate(data []StaticRecord, page, pageSize int) []StaticRecord { - start := (page - 1) * pageSize - if start >= len(data) { - return nil - } - end := start + pageSize - if end > len(data) { - end = len(data) - } - return data[start:end] -} - -// 缁熻鏁伴噺 -func (do *DataOperator) getTotalCount(collectionName string, filterExpr string) (int64, error) { - result, err := do.client.client.Query( - context.Background(), - collectionName, - []string{}, - filterExpr, - []string{"count(*)"}, - ) - if err != nil { - return 0, err - } - return result[0].(*entity.ColumnInt64).Data()[0], nil -} - -// 鏍规嵁id鑾峰彇浠诲姟 -func GetTaskByIds(ids []int64) (items []TaskOption, err error) { - placeholders := make([]string, len(ids)) - args := make([]interface{}, len(ids)) - for i, id := range ids { - placeholders[i] = "?" - args[i] = id - } - sqlStr := `select task_id,task_name from mal_smart_task where task_id in (` + strings.Join(placeholders, ",") + `)` - if err := db.Raw(sqlStr).Scan(&items).Error; err != nil { - return nil, err - } - return -} - -// 鏍规嵁id鑾峰彇瑙嗛 -func GetVideoById(vid int64) (video Camera, err error) { - // 濡傛灉杩斿洖鐨勬槸鎸囬拡,闇�瑕佸垵濮嬪寲 - sqlStr := "select id,name,alias,type,addr,rtsp,is_running,run_type,run_enable FROM cameras where video_id=?" - if err := db.Raw(sqlStr, vid).Scan(&video).Error; err != nil { - return Camera{}, err - } - return -} - -// 鏍规嵁id鑾峰彇妫�娴嬪唴瀹� -func GetCheckByIds(ids []int64) (items []CheckOption, err error) { - placeholders := make([]string, len(ids)) - args := make([]interface{}, len(ids)) - for i, id := range ids { - placeholders[i] = "?" - args[i] = id - } - sqlStr := `select check_id,file_name from mal_check_content where check_id in (` + strings.Join(placeholders, ",") + `)` - if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { - return nil, err - } - return -} - -// 鏍规嵁id鑾峰彇瑙勫垯 -func GetRuleByIds(ids []int64) (items []RuleOption, err error) { - placeholders := make([]string, len(ids)) - args := make([]interface{}, len(ids)) - for i, id := range ids { - placeholders[i] = "?" - args[i] = id - } - sqlStr := `select rule_id,file_name from mal_warning_rule where rule_id in (` + strings.Join(placeholders, ",") + `)` - if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { - return nil, err - } - return -} - -// 鑾峰彇浜嬩欢 -func GetEventByIds(ids []int64) (items []DictOption, err error) { - placeholders := make([]string, len(ids)) - args := make([]interface{}, len(ids)) - for i, id := range ids { - placeholders[i] = "?" - args[i] = id - } - sqlStr := `select dict_id,dict_name,dict_value from mal_dict_type where dict_id in (` + strings.Join(placeholders, ",") + `)` - if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { - return nil, err - } - - return - -} - -// 鏍规嵁鐭ヨ瘑搴搃d鏌ユ暟鎹� -func GetKnowledgeDocumentByIds(ids []int64) (items []KnowledgeDocumentOption, err error) { - placeholders := make([]string, len(ids)) - args := make([]interface{}, len(ids)) - for i, id := range ids { - placeholders[i] = "?" - args[i] = id - } - sqlStr := `SELECT id, know_id, file_name as title FROM mal_knowledge_document where id in (` + strings.Join(placeholders, ",") + `)` - if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { - return nil, err - } - return -} - -// 鏍规嵁鐭ヨ瘑搴搃d鏌ユ暟鎹� -func GetCameraIds(ids []string) (items []CameraDto, err error) { - placeholders := make([]string, len(ids)) - args := make([]interface{}, len(ids)) - for i, id := range ids { - placeholders[i] = "?" - args[i] = id - } - sqlStr := `select id,name,alias,type,addr,rtsp,is_running,run_type,run_enable,video_id FROM cameras where id in (` + strings.Join(placeholders, ",") + `)` - if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { - return nil, err - } - return -} +package models + +import ( + "context" + "fmt" + "math" + "sort" + "strconv" + "strings" + "time" + + "github.com/milvus-io/milvus-sdk-go/v2/entity" +) + +// DataOperator 鏁版嵁鎿嶄綔鐩稿叧鏂规硶 +type DataOperator struct { + client *MilvusClient +} + +// NewDataOperator 鍒涘缓鏁版嵁鎿嶄綔鍣� +func NewDataOperator(client *MilvusClient) *DataOperator { + return &DataOperator{client: client} +} + +type PicWidHei struct { + PicW int `json:"picW"` + PicH int `json:"picH"` +} + +// 缁熻鏌ヨ +type StaticRecord struct { + Id string `json:"id"` + TaskId []int64 `json:"task_id"` + RuleId []int64 `json:"rule_id"` + EventLevelId []int64 `json:"event_level_id"` + VideoPointId int64 `json:"video_point_id"` + DetectId []int64 `json:"detect_id"` + VideoName string `json:"video_name"` + TaskNames []TaskOption `json:"task_names"` + CheckNames []CheckOption `json:"check_names"` + RuleNames []RuleOption `json:"rule_names"` + EventLevels []DictOption `json:"event_levels"` + KnowledgeDocuments []KnowledgeDocumentOption `json:"knowledge_documents"` + ImagePath string `json:"image_path"` + VideoPath string `json:"video_path"` + DetectTime string `json:"detect_time"` + IsWarning int64 `json:"is_warning"` + ZhDescClass string `json:"zh_desc_class"` + TaskName string `json:"task_name"` + EventLevelName string `json:"event_level_name"` + DetectNum int64 `json:"detect_num"` + Suggestion string `json:"suggestion"` + RiskDescription string `json:"risk_description"` + KnowledgeId []int64 `json:"knowledge_id"` + IsDesc int64 `json:"is_desc"` + CameraId string `json:"cameraId"` + CameraName string `json:"cameraName"` + CameraAddr string `json:"cameraAddr"` + PicDate string `json:"picDate"` + PicId int64 `json:"picId"` + PicMaxUrl []string `json:"picMaxUrl"` + PicSrcUrl []string `json:"picSrcUrl"` + PicWH PicWidHei `json:"picWH"` + SdkName string `json:"sdkName"` + Content string `json:"content"` + AlarmRules []AlarmRule `json:"alarmRules"` + LikeDate string `json:"likeDate"` + ShowLabels string `json:"showLabels"` + OtherLabels string `json:"otherLabels"` + VideoUrl string `json:"videoUrl"` + AnalyServerId string `json:"analyServerId"` + AnalyServerName string `json:"analyServerName"` + AnalyServerIp string `json:"analyServerIp"` + ClusterId string `json:"clusterId"` + IsAlarm bool `json:"isAlarm"` + IsAckAlarm bool `json:"isAckAlarm"` + IsCollect bool `json:"isCollect"` + IsDelete bool `json:"isDelete"` + BaseInfo []*BaseCompareInfo `json:"baseInfo"` + TargetInfo []TargetInfo `json:"targetInfo"` + FileId string `json:"fileId"` // 鏁版嵁鏍堟枃浠秈d + DataSource string `json:"dataSource"` // 鏁版嵁鏉ユ簮:鎽勫儚鏈�, 鏁版嵁鏍� +} + +type Point struct { + X float64 `json:"x"` + Y float64 `json:"y"` +} + +type Points struct { + TopLeft Point `json:"topLeft"` + BottomRight Point `json:"bottomRight"` +} + +type BaseCompareInfo struct { + TableId string `json:"tableId"` + TableName string `json:"tableName"` + BwType string `json:"bwType"` + CompareScore float64 `json:"compareScore"` + TargetId string `json:"targetId"` + TargetName string `json:"targetName"` + TargetPicUrl string `json:"targetPicUrl"` + MonitorLevel string `json:"monitorLevel"` + Content string `json:"content"` + DbLabel string `json:"labels"` +} + +type TargetInfo struct { + TargetId string `json:"targetId"` + AreaId string `json:"areaId"` + AreaName string `json:"areaName"` + TargetScore float64 `json:"targetScore"` + TargetType string `json:"targetType"` + Feature string `json:"feature"` + PicSmUrl string `json:"picSmUrl"` + TargetLocation Points `json:"targetLocation"` + BelongsTargetID string `json:"belongsTargetId"` + Attribute string `json:"attribute"` +} + +type AlarmRule struct { + AlarmLevel string `db:"alarmLevel" json:"alarmLevel"` + DefenceState bool `db:"defenceState" json:"defenceState"` + GroupId string `db:"groupId" json:"groupId"` + LinkInfo string `db:"linkInfo" json:"linkInfo"` + RuleText string `db:"ruleText" json:"ruleText"` +} + +type TaskOption struct { + TaskId int64 `db:"task_id" json:"taskId"` + TaskName string `db:"task_name" json:"taskName"` +} + +type CheckOption struct { + TaskId int64 `db:"task_id" json:"taskId,omitempty"` + CheckId int64 `db:"check_id" json:"checkId"` + FileName string `db:"file_name" json:"fileName"` +} + +type RuleOption struct { + TaskId int64 `db:"task_id" json:"taskId,omitempty"` + RuleId int64 `db:"rule_id" json:"ruleId"` + FileName string `db:"file_name" json:"fileName"` +} + +// 瀛楀吀 +type DictOption struct { + DictId int64 `db:"dict_id" json:"dictId"` + DictName string `db:"dict_name" json:"dictName"` + DictValue string `db:"dict_value" json:"dictValue"` + DictType string `db:"dict_type" json:"dictType"` +} + +// 鐭ヨ瘑搴� +type KnowledgeDocumentOption struct { + Id string `db:"id" json:"id"` + Title string `db:"title" json:"title"` + KnowId int64 `db:"know_id" json:"know_id"` + FileUrl string `db:"title" json:"file_url"` +} + +// Pagination 鍒嗛〉淇℃伅 +type Pagination struct { + Page int `json:"page"` // 褰撳墠椤电爜 + PageSize int `json:"pageSize"` // 姣忛〉澶у皬 + Total int `json:"total"` // 鎬昏褰曟暟 + TotalPage int `json:"totalPage"` // 鎬婚〉鏁� +} + +// PaginatedResult 鍒嗛〉缁撴灉 +type PaginatedResult struct { + Items []StaticRecord `json:"list"` + Pagination Pagination `json:"pagination"` +} + +type PaginatedResult2 struct { + Items map[string]interface{} `json:"list"` + Pagination Pagination `json:"pagination"` +} + +// 璁板綍 +type RecordReq struct { + Ids []string `json:"ids"` + VideoIds []string `json:"treeNodes"` + TaskIds []int64 `json:"taskIds` + TaskName []string `json:"taskName"` + Warning int64 `json:"warning` + Page int64 `json:"page"` + PageSize int64 `json:"pageSize"` +} + +// 鏍规嵁鍒嗛〉鍙栨暟鎹� +func GetWithPage(collectionName string, pageNum int64, pageSize int64, filter string) (*PaginatedResult, error) { + + if pageNum < 1 { + pageNum = 1 + } + if pageSize < 1 { + pageSize = 10 + } + + if dbClient == nil { + return nil, nil + } + + dataOp := &DataOperator{client: dbClient} + + fmt.Println(filter) + records, err := dataOp.queryWithPagination(collectionName, filter, pageNum, pageSize) + if err != nil { + return nil, err + } + + return records, nil +} + +// 杞崲鏁版嵁 +func convertResultToMap(result []entity.Column) []StaticRecord { + if len(result) == 0 { + return nil + } + count := result[0].Len() + var records []StaticRecord + for i := 0; i < count; i++ { + record := StaticRecord{} + for _, field := range result { + switch field.Name() { + case "id": + IdColumn := field.(*entity.ColumnInt64).Data() + if len(IdColumn) > 0 { + record.Id = strconv.FormatInt(IdColumn[i], 10) + } + case "task_id": + TaskIdColumn := field.(*entity.ColumnInt64Array).Data() + if len(TaskIdColumn) > 0 { + record.TaskId = TaskIdColumn[i] + } + case "rule_id": + RuleIdColumn := field.(*entity.ColumnInt64Array).Data() + if len(RuleIdColumn) > 0 { + record.RuleId = RuleIdColumn[i] + } + case "event_level_id": + EventLevelIdColumn := field.(*entity.ColumnInt64Array).Data() + if len(EventLevelIdColumn) > 0 { + record.EventLevelId = EventLevelIdColumn[i] + } + case "video_point_id": + VideoPointIdColumn := field.(*entity.ColumnInt64).Data() + if len(VideoPointIdColumn) > 0 { + record.VideoPointId = VideoPointIdColumn[i] + } + case "detect_id": + DetectIdColumn := field.(*entity.ColumnInt64Array).Data() + if len(DetectIdColumn) > 0 { + record.DetectId = DetectIdColumn[i] + } + case "image_path": + ImagePathColumn := field.(*entity.ColumnVarChar).Data() + if len(ImagePathColumn) > 0 { + record.ImagePath = ImagePathColumn[i] + } + case "video_path": + VideoPathColumn := field.(*entity.ColumnVarChar).Data() + if len(VideoPathColumn) > 0 { + record.VideoPath = VideoPathColumn[i] + } + case "zh_desc_class": + zhColumn := field.(*entity.ColumnVarChar).Data() + if len(zhColumn) > 0 { + record.ZhDescClass = zhColumn[i] + } + case "task_name": + TNColumn := field.(*entity.ColumnVarChar).Data() + if len(TNColumn) > 0 { + record.TaskName = TNColumn[i] + } + case "event_level_name": + EVColumn := field.(*entity.ColumnVarChar).Data() + if len(EVColumn) > 0 { + record.EventLevelName = EVColumn[i] + } + case "is_desc": + descColumn := field.(*entity.ColumnInt64).Data() + if len(descColumn) > 0 { + record.IsDesc = descColumn[i] + } + case "detect_num": + DEColumn := field.(*entity.ColumnInt64).Data() + if len(DEColumn) > 0 { + record.DetectNum = DEColumn[i] + } + case "is_waning": + warnColumn := field.(*entity.ColumnInt64).Data() + if len(warnColumn) > 0 { + record.IsWarning = warnColumn[i] + } + case "detect_time": + DetectTimeColumn := field.(*entity.ColumnVarChar).Data() + if len(DetectTimeColumn) > 0 { + record.DetectTime = DetectTimeColumn[i] + } + case "knowledge_id": + KnowledgeIdColumn := field.(*entity.ColumnInt64Array).Data() + if len(KnowledgeIdColumn) > 0 { + record.KnowledgeId = KnowledgeIdColumn[i] + } + case "suggestion": + SuggestionColumn := field.(*entity.ColumnVarChar).Data() + if len(SuggestionColumn) > 0 { + record.Suggestion = SuggestionColumn[i] + } + case "risk_description": + RiskDescriptionColumn := field.(*entity.ColumnVarChar).Data() + if len(RiskDescriptionColumn) > 0 { + record.RiskDescription = RiskDescriptionColumn[i] + } + } + } + records = append(records, record) + } + return records +} + +// 鍒嗛〉鏌ヨ +func (do *DataOperator) queryWithPagination(collectionName string, filterExpr string, pageNum int64, pageSize int64) (*PaginatedResult, error) { + ctx := context.Background() + + // 璁$畻鍋忕Щ閲� + //offset := (pageNum - 1) * pageSize + + // 鏋勫缓鏌ヨ琛ㄨ揪寮忥紙Milvus 2.1+ 鏀寔 limit/offset锛� + //queryExpr := fmt.Sprintf("%s limit %d offset %d", filterExpr, pageSize, offset) + + total, err := do.getTotalCount(collectionName, filterExpr) + if err != nil { + return nil, err + } + + // 璁$畻鎬婚〉鏁� + totalPages := int(math.Ceil(float64(total) / float64(pageSize))) + + // 鎵ц鏌ヨ + result, err := do.client.client.Query( + ctx, + collectionName, + []string{}, // 鎵�鏈夊垎鍖� + filterExpr, + []string{"rule_id", "task_id", "is_waning", "zh_desc_class", "task_name", "event_level_name", "detect_num", + "event_level_id", "video_point_id", "detect_id", "image_path", + "video_path", "detect_time", "knowledge_id", "risk_description", "suggestion", "id", "is_desc"}, // 杩斿洖鎵�鏈夊瓧娈� + // client.WithLimit(pageSize), + // client.WithOffset(offset), + ) + if err != nil { + return nil, fmt.Errorf("鍒嗛〉鏌ヨ澶辫触: %v", err) + } + lists := convertResultToMap(result) + layout := "2006-01-02 15:04:05.999999" + + var temp1 time.Time + var temp2 time.Time + + //鎺掑簭 + sort.Slice(lists, func(i, j int) bool { + //return lists[i].DetectTime > lists[j].:DetectTime + temp1, _ = time.Parse(layout, lists[i].DetectTime) + temp2, _ = time.Parse(layout, lists[j].DetectTime) + return temp1.After(temp2) + }) + + items := Paginate(lists, int(pageNum), int(pageSize)) + return &PaginatedResult{ + Items: items, + Pagination: Pagination{ + Page: int(pageNum), + PageSize: int(pageSize), + Total: int(total), + TotalPage: totalPages, + }, + }, nil +} + +// 鍒嗛〉鏁版嵁 +func Paginate(data []StaticRecord, page, pageSize int) []StaticRecord { + start := (page - 1) * pageSize + if start >= len(data) { + return nil + } + end := start + pageSize + if end > len(data) { + end = len(data) + } + return data[start:end] +} + +// 缁熻鏁伴噺 +func (do *DataOperator) getTotalCount(collectionName string, filterExpr string) (int64, error) { + result, err := do.client.client.Query( + context.Background(), + collectionName, + []string{}, + filterExpr, + []string{"count(*)"}, + ) + if err != nil { + return 0, err + } + return result[0].(*entity.ColumnInt64).Data()[0], nil +} + +// 鏍规嵁id鑾峰彇浠诲姟 +func GetTaskByIds(ids []int64) (items []TaskOption, err error) { + placeholders := make([]string, len(ids)) + args := make([]interface{}, len(ids)) + for i, id := range ids { + placeholders[i] = "?" + args[i] = id + } + sqlStr := `select task_id,task_name from mal_smart_task where task_id in (` + strings.Join(placeholders, ",") + `)` + if err := db.Raw(sqlStr).Scan(&items).Error; err != nil { + return nil, err + } + return +} + +// 鏍规嵁id鑾峰彇瑙嗛 +func GetVideoById(vid int64) (video Camera, err error) { + // 濡傛灉杩斿洖鐨勬槸鎸囬拡,闇�瑕佸垵濮嬪寲 + sqlStr := "select id,name,alias,type,addr,rtsp,is_running,run_type,run_enable FROM cameras where video_id=?" + if err := db.Raw(sqlStr, vid).Scan(&video).Error; err != nil { + return Camera{}, err + } + return +} + +// 鏍规嵁id鑾峰彇妫�娴嬪唴瀹� +func GetCheckByIds(ids []int64) (items []CheckOption, err error) { + placeholders := make([]string, len(ids)) + args := make([]interface{}, len(ids)) + for i, id := range ids { + placeholders[i] = "?" + args[i] = id + } + sqlStr := `select check_id,file_name from mal_check_content where check_id in (` + strings.Join(placeholders, ",") + `)` + if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { + return nil, err + } + return +} + +// 鏍规嵁id鑾峰彇瑙勫垯 +func GetRuleByIds(ids []int64) (items []RuleOption, err error) { + placeholders := make([]string, len(ids)) + args := make([]interface{}, len(ids)) + for i, id := range ids { + placeholders[i] = "?" + args[i] = id + } + sqlStr := `select rule_id,file_name from mal_warning_rule where rule_id in (` + strings.Join(placeholders, ",") + `)` + if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { + return nil, err + } + return +} + +// 鑾峰彇浜嬩欢 +func GetEventByIds(ids []int64) (items []DictOption, err error) { + placeholders := make([]string, len(ids)) + args := make([]interface{}, len(ids)) + for i, id := range ids { + placeholders[i] = "?" + args[i] = id + } + sqlStr := `select dict_id,dict_name,dict_value from mal_dict_type where dict_id in (` + strings.Join(placeholders, ",") + `)` + if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { + return nil, err + } + + return + +} + +// 鏍规嵁鐭ヨ瘑搴搃d鏌ユ暟鎹� +func GetKnowledgeDocumentByIds(ids []int64) (items []KnowledgeDocumentOption, err error) { + placeholders := make([]string, len(ids)) + args := make([]interface{}, len(ids)) + for i, id := range ids { + placeholders[i] = "?" + args[i] = id + } + sqlStr := `SELECT id, know_id, file_name as title FROM mal_knowledge_document where id in (` + strings.Join(placeholders, ",") + `)` + if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { + return nil, err + } + return +} + +// 鏍规嵁鐭ヨ瘑搴搃d鏌ユ暟鎹� +func GetCameraIds(ids []string) (items []CameraDto, err error) { + placeholders := make([]string, len(ids)) + args := make([]interface{}, len(ids)) + for i, id := range ids { + placeholders[i] = "?" + args[i] = id + } + sqlStr := `select id,name,alias,type,addr,rtsp,is_running,run_type,run_enable,video_id FROM cameras where id in (` + strings.Join(placeholders, ",") + `)` + if err := db.Raw(sqlStr, args...).Scan(&items).Error; err != nil { + return nil, err + } + return +} -- Gitblit v1.8.0