qixiaoning
2025-07-08 fe724b50b3f1b3dfe2219eb9af4bcca96c89a158
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package models
 
import (
    "basic.com/valib/bhomeclient.git"
    "strconv"
    "strings"
)
 
//本地文件处理
type FileAnalysis struct {
    Id                     string         `gorm:"primary_key;column:id" json:"id"`
    Name                 string         `gorm:"column:name" json:"name"`
    Path                 string         `gorm:"column:path" json:"path"`
    Type                 int         `gorm:"column:type;default:1" json:"type"`//类型:0:本地图片,1:本地视频,2:本地语音
    Sort                 int            `gorm:"column:sort;default:1" json:"sort"`//排序
    Alias                 string            `gorm:"column:alias" json:"alias"` //摄像机的别名
    SnapshotUrl         string         `gorm:"column:snapshot_url" json:"snapshot_url"`//快照地址
    Duration             string         `gorm:"column:duration" json:"duration"`//时长
    IsRunning             bool         `gorm:"column:is_running" json:"is_running"`//是否正在解码
    Progress              int         `gorm:"column:progress;default:0" json:"progress"`//进度
    RuleType             int         `gorm:"column:ruleType;default:0" json:"ruleType"`//规则类型(0:独有规则,1:公共规则)
    Status              int         `gorm:"column:status;default:1" json:"status"`//状态(-1:已删除,0:暂停,1:等待 2:处理中  9:已完成)
    CreateTime             string         `gorm:"column:createTime" json:"createTime"`
    UpdateTime             string         `gorm:"column:updateTime" json:"updateTime"`
    Identifier            string         `gorm:"column:identifier" json:"identifier"`     //整个文件的唯一标识,md5
    StackId               string       `gorm:"column:stack_id" json:"stack_id"` //所属数据栈的id
    Size                int64       `gorm:"column:size" json:"size"`
}
 
func (FileAnalysis) TableName() string {
    return "file_analysis"
}
 
func IsStack(id string) bool {
    if strings.HasPrefix(id, bhomeclient.Id_Stack_Pre) {
        return true
    }
    return false
}
 
func (fa *FileAnalysis) ExistByPath(path string) (flag bool,file FileAnalysis) {
    foundResult := db.Table(fa.TableName()).Where("path=?", path).First(&file)
    if foundResult.Error !=nil {
        return false,file
    }
    if foundResult.RecordNotFound() {
        return false,file
    } else {
        return true,file
    }
}
 
func (fa *FileAnalysis) SelectById(id string) (rows int64, err error) {
    result := db.Table(fa.TableName()).Where("id = ?", id).First(&fa)
    if result.Error != nil || result.RowsAffected == 0 {
        return 0, err
    }
    return result.RowsAffected, nil
}
 
func (fa *FileAnalysis) FindByName(name string) (list []FileAnalysis, err error) {
 
    return list,err
}
 
func (fa *FileAnalysis) FindBySort(sort int) (rows int64, err error) {
    result := db.Table(fa.TableName()).Where("sort = ?", sort).First(&fa)
    if result.Error != nil || result.RowsAffected == 0 {
        return 0, err
    }
    return result.RowsAffected, nil
}
 
func (fa *FileAnalysis) GetMaxSortFile(stackId string) (rows int64, err error) {
    result := db.Table(fa.TableName()).Where("stack_id=?", stackId).Order("sort desc").First(&fa)
    if result.Error != nil || result.RowsAffected == 0 {
        return 0, err
    }
    return result.RowsAffected, nil
}
 
func (fa *FileAnalysis) Add() bool {
 
    result := db.Table(fa.TableName()).Create(&fa)
    if result.Error ==nil && result.RowsAffected >0 {
        return true
    }
    return false
}
 
func (fa *FileAnalysis) Total(fileName string, fType int, stackId string) int {
    var total int
    sql := "select count(1) from file_analysis f where f.status !=-1"
    if stackId != "" {
        sql += " and f.stack_id='"+stackId+"'"
    }
    if fileName !="" {
        sql += " and f.name like '%"+fileName+"%'"
    }
    if fType == 0 { //全部视频
 
    } else if fType == 1 { //等待处理
        sql += " and f.status=" + strconv.Itoa(bhomeclient.File_Status_Wait)
    } else if fType == 2 { //处理中
        sql += " and f.status=" + strconv.Itoa(bhomeclient.File_Status_Doing)
    } else if fType == 3 {
        sql += " and f.status=" + strconv.Itoa(bhomeclient.File_Status_Complete)
    } else if fType == 4 {
        sql += " and f.status=-2"
    }
 
    err := db.Raw(sql).Count(&total).Error
    if err !=nil {
        total = 0
    }
    return total
}
 
func (fa *FileAnalysis) FindAllByPage(fileName string, fType int, page int, size int, stackId string) (arr []FileAnalysis, err error) {
    sql := "select * from file_analysis f where f.status !=-1"
    if stackId != "" {
        sql += " and f.stack_id='"+stackId+"'"
    }
    if fileName !="" {
        sql += " and f.name like '%"+fileName+"%'"
    }
    if fType == 0 { //全部视频
 
    } else if fType == 1 { //处理完成
        sql += " and f.status=9"
    } else if fType == 2 { //处理中
        sql += " and f.status=3"
    } else if fType == 3 {
        sql += " and f.status=-2"
    }
    offset := (page-1)*size
    err = db.Raw(sql).Offset(offset).Limit(size).Order("sort asc").Find(&arr).Error
    return arr,err
}
 
func (fa *FileAnalysis) FindAll(stackId string, fileName string) (arr []FileAnalysis, err error) {
    sql := "select * from file_analysis where status !=-1"
    if fileName !="" {
        sql += " and name like '%"+fileName+"%'"
    }
    if stackId != "" {
        sql += " and stack_id='"+stackId+"'"
    }
 
    err = db.Raw(sql).Order("sort asc").Find(&arr).Error
    return arr,err
}
 
func (fa *FileAnalysis) FindByStackId(stackId string, fType int, name string, page int, size int) (arr []FileAnalysis, err error) {
    sql := "select * from file_analysis f where f.stack_id='"+stackId+"' and f.status !=-1"
    if name != "" {
        sql += " and f.name like '%"+name+"%'"
    }
    if fType == 0 { //全部视频
 
    } else if fType == 1 { //待处理
        sql += " and f.status="+strconv.Itoa(bhomeclient.File_Status_Wait)
    } else if fType == 2 { //处理中
        sql += " and f.status="+strconv.Itoa(bhomeclient.File_Status_Doing)
    } else if fType == 3 { //处理完成
        sql += " and f.status="+strconv.Itoa(bhomeclient.File_Status_Complete)
    } else if fType == 4 {//处理失败
        sql += " and f.status=-2"
    }
    offset := (page-1)*size
    err = db.Raw(sql).Offset(offset).Limit(size).Order("sort asc").Find(&arr).Error
 
    return arr,err
}
 
func (fa *FileAnalysis) FindAnalysisFiles() (arr []FileAnalysis, err error) {
    sql := "select * from file_analysis where status =1"
    err = db.Raw(sql).Order("sort asc").Find(&arr).Error
    return arr,err
}
 
func (fa *FileAnalysis) UpdateStatus(id string,status int) bool {
    result := db.Exec("update file_analysis set status=? where id=?",status,id)
    if result.Error ==nil && result.RowsAffected >0 {
        return true
    }
    return false
}
 
func (fa *FileAnalysis) UpdateIsRunning(id string, isRunning bool) bool {
    isRunningStr := "0"
    if isRunning {
        isRunningStr = "1"
    }
    result := db.Exec("update file_analysis set is_running="+isRunningStr+" where id='"+id+"'")
    if result.Error !=nil {
        return false
    }
    return result.RowsAffected>0
}
 
func (fa *FileAnalysis) UpdateProgress(ids []string,progress int) bool {
    idStr := ""
    for _,id := range ids {
        idStr += "'"+id+"',"
    }
    idStr = strings.Trim(idStr, ",")
    if idStr != "" {
        result := db.Exec("update file_analysis set status=? where id in ("+idStr+")",progress)
        if result.Error ==nil && result.RowsAffected >0 {
            return true
        }
    }
 
    return false
}
 
func (fa *FileAnalysis) Rename(id string, name string) bool {
    result := db.Exec("update file_analysis set name=? where id=?",name, id)
    if result.Error ==nil && result.RowsAffected >0 {
        return true
    }
    return false
}
 
 
func (fa *FileAnalysis) Move(id string, stackId string) bool {
    result := db.Exec("update file_analysis set stack_id=? where id=?",stackId, id)
    if result.Error ==nil && result.RowsAffected >0 {
        return true
    }
    return false
}