qixiaoning
2025-08-22 0f97177f258c67397b206b70e5aea2b24a4868c1
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
package service
 
import (
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/bhomedbapi.git"
    "encoding/json"
    "github.com/satori/go.uuid"
    "strings"
    "vamicro/config"
    "vamicro/stack-service/models"
    "vamicro/stack-service/vo"
)
 
type FileStackService struct {
    Bk bhomeclient.Broker
}
 
func (sv FileStackService) FindAll() []models.FileStack {
    var model models.FileStack
    list, _ := model.FindAll()
    if list == nil {
        list = []models.FileStack{}
    }
    return list
}
 
func (sv FileStackService) FindAllByPage(fType int, fileName string, page int, size int) (result vo.PageResult) {
    var crApi bhomedbapi.CameraRuleApi
    filterCamIds := ""
    if fType == 3 { //未配规则
        flag, allRules := crApi.FindAll()
        if flag && len(allRules) > 0 {
            for _,c := range allRules {
                if c.CameraInfo != nil && c.CameraInfo.Id != "" && len(c.Rules) > 0 {
                    filterCamIds +="'"+c.CameraInfo.Id+"',"
                }
            }
        }
        if filterCamIds != "" {
            filterCamIds = strings.TrimRight(filterCamIds, ",")
        }
    }
 
    var model models.FileStack
    result.Total = model.Total(fileName, fType, &filterCamIds)
 
 
    if result.Total > 0 {
        arr, _ := model.FindAllByPage(fType, fileName, page, size, &filterCamIds)
        var pageList = make([]vo.FileStackWithWH, 0)
        if arr != nil {
            var resolutions []vo.Resolution
            if config.Server.Resolutions != nil && len(config.Server.Resolutions) > 0 {
                for _, rwh := range config.Server.Resolutions {
                    resolutions = append(resolutions, vo.Resolution{
                        Width:  rwh.Width,
                        Height: rwh.Height,
                    })
                }
            } else {
                dRe0 := vo.Resolution{
                    Width: 0,
                    Height: 0,
                }
                dRe1 := vo.Resolution{
                    Width: 1080,
                    Height: 720,
                }
                dRe2 := vo.Resolution{
                    Width: 1920,
                    Height: 1080,
                }
                dRe3 := vo.Resolution{
                    Width: 2688,
                    Height: 1520,
                }
                resolutions = []vo.Resolution{ dRe0, dRe1, dRe2, dRe3}
            }
 
            for _,sck := range arr {
                if !crApi.ExistRunningTask(sck.Id) {
                    sck.Status = bhomeclient.Stack_Status_NoRule
                }
                v := vo.FileStackWithWH{}
                v.FileStack = sck
                v.Resolutions = resolutions
                pageList = append(pageList, v)
            }
        }
        result.DataList = pageList
    } else {
        result.DataList = []vo.FileStackWithWH{}
    }
    return result
}
 
func (sv FileStackService) Save(fsE *models.FileStack) bool {
    if fsE.Id == "" {
        fsE.Id = bhomeclient.Id_Stack_Pre + uuid.NewV4().String()
        if fsE.Add() {
            dbMsg := protomsg.DbChangeMessage{
                Id: fsE.Id,
                Table: protomsg.TableChanged_T_FileStack,
                Action: protomsg.DbAction_Insert,
            }
            pb,_ := json.Marshal(dbMsg)
            sv.Bk.Publish(ProcName, pb)
            return true
        }
    } else {
        if fsE.Update() {
            dbMsg := protomsg.DbChangeMessage{
                Id: fsE.Id,
                Table: protomsg.TableChanged_T_FileStack,
                Action: protomsg.DbAction_Update,
            }
            pb,_ := json.Marshal(dbMsg)
            sv.Bk.Publish(ProcName, pb)
            return true
        }
    }
    return false
}
 
func (sv FileStackService) UpdateStatus(ids []string, status int) bool {
    var sck models.FileStack
    ret := false
    if status == bhomeclient.Stack_Status_Doing {
        ret = sck.UpdateStatus(ids, status)
    } else if status == bhomeclient.Stack_Status_Done {
        for _,sckId := range ids {
            if sck.HavingWorkFiles(sckId) {
                sck.UpdateStatus([]string{ sckId }, bhomeclient.Stack_Status_Doing)
            } else {
                sck.UpdateStatus([]string{ sckId }, bhomeclient.Stack_Status_Done)
            }
        }
        ret = true
    }
    if ret {
        dbMsg := protomsg.DbChangeMessage{
            Id: strings.Join(ids, ","),
            Table: protomsg.TableChanged_T_FileStack,
            Action: protomsg.DbAction_Update,
        }
        pb,_ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
    }
    return ret
}
 
func (sv FileStackService) UpdateEnable(id string, enable bool) bool {
    var fs models.FileStack
    if fs.UpdateEnable(id, enable) {
        dbMsg := protomsg.DbChangeMessage{
            Table: protomsg.TableChanged_T_FileStack,
            Action: protomsg.DbAction_Update,
        }
        pb,_ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}
 
func (sv FileStackService) UpdateVideoEnable(enable bool) bool {
    var fset models.FileAnalysisSetting
    if fset.UpdateVideoEnable(enable) {
        dbMsg := protomsg.DbChangeMessage{
            Table: protomsg.TableChanged_T_FileSetting,
            Action: protomsg.DbAction_Update,
        }
        pb,_ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}
 
func (sv FileStackService) DeleteById(id string) bool {
    var fs models.FileStack
    if fs.DeleteById(id) {
        dbMsg := protomsg.DbChangeMessage{
            Table: protomsg.TableChanged_T_FileStack,
            Action: protomsg.DbAction_Delete,
        }
        pb,_ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}
 
func (sv FileStackService) UpdateChannelCount(count int) bool {
    var fas models.FileAnalysisSetting
    if fas.UpdateChannelCount(count)  {
        dbMsg := protomsg.DbChangeMessage{
            Table: protomsg.TableChanged_T_FileSetting,
            Action: protomsg.DbAction_Update,
        }
        pb,_ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}