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:"更新失败"} } }