package controllers import ( "basic.com/valib/bhomeclient.git" "basic.com/valib/logger.git" "encoding/json" "github.com/satori/go.uuid" "strconv" "time" "vamicro/stack-service/models" "vamicro/stack-service/service" "vamicro/stack-service/vo" ) type FileAnalysisController struct { } // @Summary 获取本地文件列表 // @Description 获取本地文件列表 // @Produce json // @Tags 本地文件 // @Param stackId query string true "数据栈id" // @Param fileName 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/fileAnalysis/findAllFile [get] func (fac FileAnalysisController) FindAllFile(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ fileName := c.Query("fileName") stackId := c.Query("stackId") page,_ := strconv.Atoi(c.Query("page")) size,_ := strconv.Atoi(c.Query("size")) fType,_ := strconv.Atoi(c.Query("type")) logger.Debug("FindAllFile fileName:",fileName,"page:",page,"size:",size,"fType:",fType,"stackId:",stackId) if page < 0 { page = 1 } if size <= 0 { size = 20 } sv := service.FileAnalysisService{Bk: h.Bk} result := sv.FindAllByPage(fileName, fType, page, size, stackId) return &bhomeclient.Reply{Success:true, Data:result} } // @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/fileAnalysis/show [get] func (fac FileAnalysisController) Show(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ id := c.Query("id") if id == "" { return &bhomeclient.Reply{Msg:"参数有误"} } var file models.FileAnalysis rows, _ := file.SelectById(id) if rows >0 { return &bhomeclient.Reply{Success:true, Data:file} } else { return &bhomeclient.Reply{Msg:"记录不存在"} } } // @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/fileAnalysis/getAnalysisFiles [get] func (fac FileAnalysisController) GetAnalysisFiles(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ sv := service.FileAnalysisService{Bk: h.Bk} arr, _ := sv.GetAnalysisFiles() if arr !=nil && len(arr) >0 { return &bhomeclient.Reply{Success:true, Data:arr} } else { return &bhomeclient.Reply{Success:true, Data:[]models.FileAnalysis{}} } } type FileProgressVo struct { Ids []string `json:"ids" binding:"required"` Progress int `json:"progress" binding:"required"` } // @Summary 更新文件分析进度 // @Description 更新文件分析进度 // @Accept json // @Produce json // @Tags 本地文件 // @Param reqBody body controllers.FileProgressVo true "更新进度参数,progress为1-100" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/fileAnalysis/updateProgress [post] func (fac FileAnalysisController) UpdateProgress(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var reqBody FileProgressVo err := c.BindJSON(&reqBody) logger.Debug("updateProgress reqBody:",reqBody,"bindJson err:",err) if err !=nil { return &bhomeclient.Reply{Msg:"参数有误"} } sv := service.FileAnalysisService{Bk: h.Bk} if sv.UpdateProgress(reqBody.Ids, reqBody.Progress) { if reqBody.Progress == bhomeclient.File_Status_Complete { type donePub struct { Topic string Ids []string } param := donePub{ Topic: "FileDecodeDone", Ids: reqBody.Ids, } data,_ := json.Marshal(param) h.Bk.Publish(service.ProcName, data) } return &bhomeclient.Reply{Success:true, Msg:"更新成功"} } else { return &bhomeclient.Reply{Msg:"更新失败"} } } type FileStatusVo struct { Ids []string `json:"ids" binding:"required"` Status int `json:"status"` } // @Summary 开启或暂停文件分析 // @Description 开启或暂停文件分析 // @Accept json // @Produce json // @Tags 本地文件 // @Param reqBody body controllers.FileStatusVo true "开启暂停参数,暂停status=0,开启status=1" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/fileAnalysis/updateStatus [post] func (fac FileAnalysisController) 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.FileAnalysisService{Bk: h.Bk} if sv.UpdateStatus(reqBody.Ids, reqBody.Status) { return &bhomeclient.Reply{Success:true, Msg:"更新成功"} } else { return &bhomeclient.Reply{Msg:"更新失败"} } } // @Summary 删除本地文件 // @Description 删除本地文件 // @Accept json // @Produce json // @Tags 本地文件 // @Param reqBody body vo.IdArrVo 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/fileAnalysis/delete [post] func (fac FileAnalysisController) Delete(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var reqBody vo.IdArrVo err := c.BindJSON(&reqBody) if err !=nil { return &bhomeclient.Reply{Msg:"参数有误"} } sv := service.FileAnalysisService{Bk: h.Bk} if sv.Delete(&reqBody) { return &bhomeclient.Reply{Success:true, Msg:"删除成功"} } else { return &bhomeclient.Reply{Msg:"删除失败"} } } // @Summary 本地文件排序 // @Description 本地文件排序 // @Accept json // @Produce json // @Tags 本地文件 // @Param reqBody body vo.SortVo true "排序参数,向上direct=1,向下direct=2" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/fileAnalysis/sortFile [post] func (fac FileAnalysisController) SortFile(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var reqBody vo.SortVo err := c.BindJSON(&reqBody) if err !=nil || (reqBody.Direct !=service.Sort_Up && reqBody.Direct !=service.Sort_Down) { return &bhomeclient.Reply{Msg:"参数有误"} } sv := service.FileAnalysisService{Bk: h.Bk} if sv.SortFile(&reqBody) { return &bhomeclient.Reply{Success:true, Msg:"更新成功"} } else { return &bhomeclient.Reply{Msg:"更新失败"} } } // @Summary 新增文件 // @Description 新增文件 // @Accept json // @Produce json // @Tags 本地文件 // @Param reqBody body models.FileAnalysis true "文件新增json" // @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}" // @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}" // @Router /data/api-v/fileAnalysis/save [post] func (fac FileAnalysisController) Save(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var reqBody models.FileAnalysis err := c.BindJSON(&reqBody) if err !=nil || reqBody.StackId == "" { return &bhomeclient.Reply{Msg:"参数有误"} } logger.Debug("file add reqBody:", reqBody) if reqBody.Id == "" { reqBody.Id = uuid.NewV4().String() } reqBody.CreateTime = time.Now().Format("2006-01-02 15:04:05") var tmpMax models.FileAnalysis rows, err := tmpMax.GetMaxSortFile(reqBody.StackId) if err == nil && rows >0 { reqBody.Sort = tmpMax.Sort +1 } sv := service.FileAnalysisService{Bk: h.Bk} if sv.Add(reqBody) { return &bhomeclient.Reply{Success:true, Msg:"保存成功"} } else { return &bhomeclient.Reply{Msg:"保存失败"} } } // @Param stackId query string true "stackId" // @Param type query int true "type" // @Param name query string false "搜索条件" // @Param page query int true "当前页" // @Param size query int true "每页数量" func (fac FileAnalysisController) FindByStackId(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ stackId := c.Query("stackId") if stackId == "" { return &bhomeclient.Reply{Msg:"参数有误"} } typ,_ := strconv.Atoi(c.Query("type")) page,_ := strconv.Atoi(c.Query("page")) size,_ := strconv.Atoi(c.Query("size")) name := c.Query("name") if page <= 0 { page = 1 } if size <= 0 { size = 20 } sv := service.FileAnalysisService{Bk: h.Bk} result := sv.FindByStackId(stackId, typ, name, page, size) return &bhomeclient.Reply{Success:true, Data: result} } // @Summary 重命名 // @Description 重命名 // @Accept json // @Produce json // @Tags 本地文件 // @Param id formData string true "文件id" // @Param name formData string 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/fileAnalysis/rename [post] func (fac FileAnalysisController) Rename(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ id := c.PostForm("id") name := c.PostForm("name") if id == "" || name == "" { return &bhomeclient.Reply{Msg:"参数有误"} } sv := service.FileAnalysisService{Bk: h.Bk} if sv.Rename(id, name) { return &bhomeclient.Reply{Success: true, Msg:"修改成功"} } else { return &bhomeclient.Reply{Msg:"修改失败"} } } // @Summary 移动 // @Description 移动 // @Accept json // @Produce json // @Tags 本地文件 // @Param id formData string true "文件id" // @Param stackId formData 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/fileAnalysis/move [post] func (fac FileAnalysisController) Move(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ id := c.PostForm("id") stackId := c.PostForm("stackId") if id == "" || stackId == "" { return &bhomeclient.Reply{Msg:"参数有误"} } var tmp models.FileAnalysis rows, err := tmp.SelectById(id) if rows > 0 && err == nil { sv := service.FileAnalysisService{Bk: h.Bk} if sv.Move(id, stackId) { return &bhomeclient.Reply{Success:true, Msg:"移动成功"} } } return &bhomeclient.Reply{Msg:"移动失败"} } // @Summary 复制 // @Description 复制 // @Accept json // @Produce json // @Tags 本地文件 // @Param reqBody body vo.FileMoveVo 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/fileAnalysis/copy [post] func (fac FileAnalysisController) Copy(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var reqBody vo.FileMoveVo err := c.BindJSON(&reqBody) if err != nil { return &bhomeclient.Reply{Msg:"参数有误"} } var tmp models.FileAnalysis rows, err := tmp.SelectById(reqBody.Id) if rows > 0 && err == nil { sv := service.FileAnalysisService{Bk: h.Bk} for _,sckId := range reqBody.StackIds { if sckId != tmp.StackId { tmp.Id = uuid.NewV4().String() tmp.StackId = sckId sv.Add(tmp) } } return &bhomeclient.Reply{Success: true, Msg:"拷贝成功"} } return &bhomeclient.Reply{Msg:"拷贝失败"} }