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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package controllers
 
import (
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/logger.git"
    "os"
    "path"
    "strconv"
    "strings"
    "vamicro/config"
    "vamicro/extend/util"
    "vamicro/stack-service/models"
    "vamicro/stack-service/service"
    "vamicro/stack-service/vo"
)
 
type FileStackController struct {
 
}
 
 
func (fsc FileStackController) FindAllDoingStacks(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    //1.需要开启分析开关
    //2.需要配置有规则
    var model models.FileStack
    list,err := model.FindAll()
    if err ==nil {
        var resultList = make([]models.FileStack, 0)
        if list != nil {
            //var ctE models.CameraTask
            for _,fsE := range list {
                //if fsE.Enable && ctE.ExistRunningTask(fsE.Id) {
                if fsE.Enable {
                    resultList = append(resultList, fsE)
                }
            }
        }
        return &bhomeclient.Reply{Success:true, Data:resultList}
    } else {
        return &bhomeclient.Reply{Msg:"查询失败"}
    }
}
 
// @Summary 获取数据栈列表
// @Description 获取数据栈列表
// @Produce json
// @Tags 数据栈
// @Param name query string false "根据数据栈名称查询"
// @Param type query int true "0:全部视频,1:处理完成,2:处理中,3:未配规则,4:未开启"
// @Param page query int true "当前页"
// @Param size query int true "每页数量"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/fileStack/findAllByPage [get]
func (fsc FileStackController) FindAllByPage(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    name := c.Query("name")
    page,_ := strconv.Atoi(c.Query("page"))
    size,_ := strconv.Atoi(c.Query("size"))
    fType,_ := strconv.Atoi(c.Query("type"))
    logger.Debug("FindAllFile fileName:",name,"page:",page,"size:",size,"fType:",fType)
    if page <= 0 {
        page = 1
    }
    if size <= 0 {
        size = 20
    }
    sv := service.FileStackService{Bk: h.Bk}
    result := sv.FindAllByPage(fType, name, page, size)
 
    return &bhomeclient.Reply{Success:true, Data:result}
}
 
// @Summary 获取所有数据栈列表
// @Description 获取所有数据栈列表
// @Produce json
// @Tags 数据栈
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/fileStack/findAll [get]
func (fsc FileStackController) FindAll(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
 
    sv := service.FileStackService{Bk: h.Bk}
    result := sv.FindAll()
 
    return &bhomeclient.Reply{Success:true, Data:result}
}
 
// @Summary 新增或者更新
// @Description 新增或者更新,更新时id不能为空
// @Accept json
// @Produce json
// @Tags 数据栈
// @Param reqBody body models.FileStack true "数据栈结构体"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/fileStack/save [post]
func (fsc FileStackController) Save(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    var reqBody models.FileStack
    if err := c.BindJSON(&reqBody);err !=nil {
        return &bhomeclient.Reply{Msg:"参数有误"}
    }
    sv := service.FileStackService{Bk: h.Bk}
    if sv.Save(&reqBody) {
        return &bhomeclient.Reply{Success:true, Msg:"保存成功"}
    } else {
        return &bhomeclient.Reply{Msg:"保存失败"}
    }
}
 
// @Summary 查看数据栈详情
// @Description 查看数据栈详情
// @Produce json
// @Tags 数据栈
// @Param id path string true "数据栈id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/fileStack/show [get]
func (fsc FileStackController) Show(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    id := c.Query("id")
    if id == "" {
        return &bhomeclient.Reply{Msg:"参数有误"}
    }
    var fsE models.FileStack
    rows, _ := fsE.SelectById(id)
    if rows > 0 {
        m := util.Struct2Map(fsE)
 
        if config.Server.Resolutions != nil && len(config.Server.Resolutions) > 0 {
            var arr []vo.Resolution
            for _,rwh := range config.Server.Resolutions {
                arr = append(arr, vo.Resolution{
                    Width: rwh.Width,
                    Height: rwh.Height,
                })
            }
            m["resolutions"] = arr
        } 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,
            }
            m["resolutions"] = []vo.Resolution{ dRe0, dRe1, dRe2, dRe3 }
        }
        return &bhomeclient.Reply{ Success:true, Data:m }
    } else {
        return &bhomeclient.Reply{Msg:"查询失败"}
    }
}
 
// @Summary 删除
// @Description 删除
// @Produce json
// @Tags 数据栈
// @Param id path string true "删除数据栈id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/fileStack/delete [delete]
func (fsc FileStackController) Delete(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    id := c.Query("id")
    if id == "" {
        return &bhomeclient.Reply{Msg:"参数有误"}
    }
    sv := service.FileStackService{Bk: h.Bk}
    var fa models.FileAnalysis
    allFiles, _ := fa.FindAll(id, "")
    if sv.DeleteById(id) {
        go deleteDiskFiles(allFiles)
        return &bhomeclient.Reply{Success:true, Msg:"删除成功"}
    } else {
        return &bhomeclient.Reply{Msg:"删除失败"}
    }
}
 
func deleteDiskFiles(files []models.FileAnalysis) {
    for _,f := range files {
        //如果是文件,则删除。是文件夹则跳过
        if util.DirExists(f.Path) {
            if util.Exists(f.Path) {
                os.Remove(f.Path)
            }
            ext := path.Ext(f.Path)
            md5Path := strings.Replace(f.Path, ext, "",-1)
            if util.Exists(md5Path) {
                os.RemoveAll(md5Path)
            }
        }
    }
}
 
// @Summary 切换enable
// @Description 切换enable
// @Accept json
// @Produce json
// @Tags 数据栈
// @Param reqBody body vo.EnableVo true "切换enable"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/fileStack/changeEnable [post]
func (fsc FileStackController) ChangeEnable(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    var reqBody vo.EnableVo
    c.BindJSON(&reqBody)
    if reqBody.Id == "" {
        return &bhomeclient.Reply{Msg:"参数有误"}
    }
    sv := service.FileStackService{Bk: h.Bk}
    if sv.UpdateEnable(reqBody.Id, reqBody.Enable) {
        return &bhomeclient.Reply{Success:true, Msg:"更新成功"}
    } else {
        return &bhomeclient.Reply{Msg:"更新失败"}
    }
}
 
// @Summary 更新数据栈状态
// @Description 更新数据栈状态
// @Accept json
// @Produce json
// @Tags 数据栈
// @Param reqBody body controllers.FileStatusVo true "数据栈状态参数"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/fileStack/updateStatus [post]
func (fsc FileStackController) UpdateStatus(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    var reqBody FileStatusVo
    err := c.BindJSON(&reqBody)
    if err !=nil {
        return &bhomeclient.Reply{Msg:"参数有误"}
    }
 
    sv := service.FileStackService{Bk: h.Bk}
    if sv.UpdateStatus(reqBody.Ids, reqBody.Status) {
        return &bhomeclient.Reply{Success:true, Msg:"更新成功"}
    } else {
        return &bhomeclient.Reply{Msg:"更新失败"}
    }
}