qixiaoning
2025-07-18 24f44f6ecefb5e83295bab670533529c6bc81810
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
234
235
236
237
238
package service
 
import (
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/bhomedbapi.git"
    "basic.com/valib/logger.git"
    "encoding/json"
    "errors"
    "vamicro/stack-service/models"
    "vamicro/stack-service/vo"
)
 
type FileAnalysisService struct {
    Bk bhomeclient.Broker
}
 
const (
    Sort_Up   = 1
    Sort_Down = 2
)
 
func (fas *FileAnalysisService) FindAllByPage(fileName string, fType int, page int, size int, stackId string) (result vo.PageResult) {
 
    var model models.FileAnalysis
    result.Total = model.Total(fileName, fType, stackId)
    var fileList = make([]vo.FileIfRule, 0)
 
    if result.Total > 0 {
        arr, _ := model.FindAllByPage(fileName, fType, page, size, stackId)
        if arr != nil {
            var crApi bhomedbapi.CameraRuleApi
            for _, f := range arr {
                file := vo.FileIfRule{}
                file.FileAnalysis = f
                if crApi.ExistRunningTask(f.Id) {
                    file.HasRule = true
                } else {
                    file.HasRule = false
                }
                fileList = append(fileList, file)
            }
        }
    }
    result.DataList = fileList
    return result
}
 
func (fas *FileAnalysisService) GetAnalysisFiles() (fileList []models.FileAnalysis, err error) {
    var model models.FileAnalysis
    var crApi bhomedbapi.CameraRuleApi
    arr, e := model.FindAnalysisFiles()
    if e == nil && arr != nil {
        for _, f := range arr {
            if crApi.ExistRunningTask(f.Id) {
                fileList = append(fileList, f)
            }
        }
        return fileList, nil
    }
    return nil, e
}
 
func (fas *FileAnalysisService) Add(fa models.FileAnalysis) bool {
    if fa.Add() {
        dbMsg := protomsg.DbChangeMessage{
            Table:  protomsg.TableChanged_T_File,
            Id:     fa.Id,
            Action: protomsg.DbAction_Insert,
        }
        pb, _ := json.Marshal(dbMsg)
        fas.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}
 
func (fas *FileAnalysisService) Rename(id, name string) bool {
    var fa models.FileAnalysis
    if fa.Rename(id, name) {
        dbMsg := protomsg.DbChangeMessage{
            Table:  protomsg.TableChanged_T_File,
            Id:     id,
            Action: protomsg.DbAction_Update,
        }
        pb, _ := json.Marshal(dbMsg)
        fas.Bk.Publish(ProcName, pb)
 
        return true
    }
    return false
}
 
func (fas *FileAnalysisService) Move(id, stackId string) bool {
    var fa models.FileAnalysis
    if fa.Move(id, stackId) {
        dbMsg := protomsg.DbChangeMessage{
            Table:  protomsg.TableChanged_T_File,
            Id:     id,
            Action: protomsg.DbAction_Update,
        }
        pb, _ := json.Marshal(dbMsg)
        fas.Bk.Publish(ProcName, pb)
 
        return true
    }
    return false
}
 
func (fas *FileAnalysisService) UpdateProgress(ids []string, progress int) bool {
    var file models.FileAnalysis
    return file.UpdateProgress(ids, progress)
}
 
func (fas *FileAnalysisService) UpdateStatus(ids []string, status int) bool {
    for _, id := range ids {
        var tmpFile models.FileAnalysis
        _, err := tmpFile.SelectById(id)
        if err != nil {
            logger.Error("fail to tmpFile.SelectById(id)", id)
            continue
        }
        logger.Debug("updateStatus", id)
        flag := false
        if status == 0 && tmpFile.Status == bhomeclient.File_Status_Wait {
            if tmpFile.UpdateStatus(id, bhomeclient.File_Status_Pause) {
                flag = true
            }
        } else if status == 1 && tmpFile.Status != bhomeclient.File_Status_Doing {
            if tmpFile.UpdateStatus(id, bhomeclient.File_Status_Wait) {
                flag = true
            }
        }
        if flag {
            dbMsg := protomsg.DbChangeMessage{
                Table:  protomsg.TableChanged_T_File,
                Id:     id,
                Action: protomsg.DbAction_Update,
            }
            pb, _ := json.Marshal(dbMsg)
            fas.Bk.Publish(ProcName, pb)
        }
    }
    return true
}
 
func (fas *FileAnalysisService) Delete(arg *vo.IdArrVo) bool {
    var file models.FileAnalysis
    logger.Debug("argIds", arg.Ids, "argIdsLength", len(arg.Ids))
    for _, id := range arg.Ids {
        //正在处理的文件不允许删除
        //rows, err := file.SelectById(id)
        //if err == nil && rows > 0 {
        //    //if file.Status == bhomeclient.File_Status_Doing {
        //    //    continue
        //    //} else {
        //    //}
        //}
        file.UpdateStatus(id, -1)
    }
    return true
}
 
func (fas *FileAnalysisService) SortFile(sortArg *vo.SortVo) bool {
    var err error
    tx := models.GetDB().Begin()
    defer func() {
        if err != nil && tx != nil {
            tx.Rollback()
        }
    }()
 
    var file models.FileAnalysis
    rows, _ := file.SelectById(sortArg.Id)
    if rows > 0 {
        if sortArg.Direct == Sort_Up {
            if file.Sort > 1 {
                preSort := file.Sort - 1
                var preFile models.FileAnalysis
                pr, _ := preFile.FindBySort(preSort)
                if pr > 0 {
                    if preFile.Status == bhomeclient.File_Status_Doing {
                        err = errors.New("cannot be pre by doing file")
                        return false
                    }
                    err = tx.Exec("update file_analysis set sort=? where id=?", preSort, file.Id).Error
                    if err != nil {
                        return false
                    }
                    err = tx.Exec("update file_analysis set sort=? where id=?", file.Sort, preFile.Id).Error
                    if err != nil {
                        return false
                    }
                }
            }
        } else if sortArg.Direct == Sort_Down {
            if file.Status == bhomeclient.File_Status_Doing {
                err = errors.New("doing file cannot be sort down")
                return false
            }
            var maxTmp models.FileAnalysis
            mr, _ := maxTmp.GetMaxSortFile(file.StackId)
            if mr > 0 {
                if maxTmp.Id != sortArg.Id {
                    nextSort := file.Sort + 1
                    var nextFile models.FileAnalysis
                    nr, _ := nextFile.FindBySort(nextSort)
                    if nr > 0 {
                        err = tx.Exec("update file_analysis set sort=? where id=?", nextSort, file.Id).Error
                        if err != nil {
                            return false
                        }
                        err = tx.Exec("update file_analysis set sort=? where id=?", file.Sort, nextFile.Id).Error
                        if err != nil {
                            return false
                        }
                    }
                }
            }
        }
    }
    tx.Commit()
 
    return true
}
 
func (fas *FileAnalysisService) FindByStackId(stackId string, typ int, name string, page int, size int) (result vo.PageResult) {
    var faE models.FileAnalysis
    result.Total = faE.Total(name, typ, stackId)
    if result.Total > 0 {
        arr, _ := faE.FindByStackId(stackId, typ, name, page, size)
        result.DataList = arr
    } else {
        result.DataList = []models.FileAnalysis{}
    }
 
    return result
}