package controllers import ( "basic.com/valib/bhomeclient.git" "basic.com/valib/bhomedbapi.git" "basic.com/valib/logger.git" "vamicro/sync-service/models" "github.com/satori/go.uuid" "sort" "strconv" "vamicro/extend/util" ) type AreaController struct { } // @Summary 显示树形结构 // @Description 显示左侧所有区域和摄像机 // @Produce json // @Tags menu // @Param parentId query string true "区域的id" // @Param searchType query int true "查询类型(0:全部,1:分析摄像机,2:监控摄像机, 3:联动摄像机)" // @Param cameraName query string false "摄像机名称" // @Success 200 {string} json "{"code":200, msg:"目录结构数据"}" // @Failure 500 {string} json "{"code":500, msg:"返回错误信息"}" // @Router /data/api-v/area/localmenu [get] func (ac AreaController) CameraTree(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply { var aram models.Area var searchType = 0 var err error parentId := c.Query("parentId") if parentId == "" { parentId = "0" } searchTypeStr := c.Query("searchType") cameraName := c.Query("cameraName") isPlatformStr := c.Query("isPlatform") isPlatform, _ := strconv.Atoi(isPlatformStr)//单机获取本机摄像机,平台获取所有摄像机 treeType := models.TYPE_LOCAL_TREE //默认是本地树 if searchTypeStr != "" { searchType, err = strconv.Atoi(searchTypeStr) if err != nil { return &bhomeclient.Reply{ Success:false, Msg: "参数有误", Data:""} } } areas, err := aram.Find(parentId,cameraName, util.FiledbyStuct(models.TreeNode{}),treeType) if err != nil { logger.Debug("area.Find err:",err) return &bhomeclient.Reply{ Success: false, Msg: err.Error()} } var cam models.Camera cams, err := cam.Find(searchType, cameraName, models.TYPE_LOCAL_CAMERA, isPlatform) if err != nil { logger.Debug("cam.Find err:",err) return &bhomeclient.Reply{ Success: false, Msg: err.Error()} } var totalCams models.CamTreeNodeList totalCams = cams sort.Sort(totalCams) rAreas := models.RecursiveArea(areas, cams, searchType) var totalAreas models.AreaList totalAreas = rAreas sort.Sort(totalAreas) logger.Debug("rAreas:",rAreas) arr := menuFormat(totalAreas, parentId, totalCams, treeType) var crApi bhomedbapi.CameraRuleApi for _,camE := range totalCams { if camE.Areaid == "0" { isRunnig := false isAI := false camera := models.Camera{} rows, e := camera.SelectById(camE.Id) if rows>0 && e==nil{ isRunnig = camera.IsRunning isAI = crApi.ExistRunningTask(camE.Id) && (camera.RunType == models.TYPE_RUNTYPE_POLL || camera.RunType == models.TYPE_RUNTYPE_REALTIME) } arr = append(arr,models.TreeMenu{ Id:camE.Id, Type:"4", Name:camE.Name, IsAI: isAI, //是否AI摄像机 IsRunning: isRunnig, Rtsp:camE.Rtsp, CameraType: treeType, }) } } return &bhomeclient.Reply{ Success: true, Data: arr } } // 将区域和摄像机数组转换目录树形结构 func menuFormat(dbArr models.AreaList, parentId string, dbCams []models.CameraTreeNode, treeType int) []models.TreeMenu { var arr []models.TreeMenu var crApi bhomedbapi.CameraRuleApi for _, area := range dbArr { if parentId == area.Parentid { tmp := models.TreeMenu{} tmp.Id = area.Id tmp.Type = "MENU" tmp.Name = area.Name sontmp := menuFormat(dbArr, area.Id, dbCams, treeType) tmp.Areanodes = sontmp for _, val := range dbCams { if val.Areaid == area.Id { camtmp := models.TreeMenu{} camtmp.Id = val.Id camtmp.Name = val.Name camtmp.Type = "4" isRunnig := false isAI := false camera := models.Camera{} rows, e := camera.SelectById(val.Id) if rows>0 && e==nil{ isRunnig = camera.IsRunning isAI = crApi.ExistRunningTask(val.Id) && (camera.RunType == models.TYPE_RUNTYPE_POLL || camera.RunType == models.TYPE_RUNTYPE_REALTIME) //if camera.RunType == models.TYPE_RUNTYPE_POLL || camera.RunType == models.TYPE_RUNTYPE_REALTIME { // isAI = true //} } camtmp.IsAI = isAI camtmp.IsRunning = isRunnig camtmp.Rtsp = val.Rtsp camtmp.CameraType = treeType tmp.Areanodes = append(tmp.Areanodes, camtmp) } } arr = append(arr, tmp) } } return arr } // @Summary 添加menu的区域 // @Description 添加目录上区域 // @Produce json // @Tags menu // @Param name formData string true "区域名字" // @Param parentid formData string true "上一级父id" // @Success 200 {string} json "{"code":200, data:"添加的区域信息",msg:"请求成功", success:true}" // @Failure 200 {string} json "{"code":"错误码", data:"出错信息",msg:"请求失败", success:false}" // @Router /data/api-v/area/add [post] func (ac AreaController) AreaAdd(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var pArea models.Area var cArea models.Area name := c.PostForm("name") parentId := c.PostForm("parentId") if parentId == "" || parentId == "0" { cArea.Parentids = ",0," } else { if _, err := pArea.SelectbyId(parentId); err != nil { return &bhomeclient.Reply{ Success:false, Msg: "参数有误"} } cArea.Parentids = pArea.Parentids + parentId+"," } cArea.Id = uuid.NewV4().String() cArea.Name = name cArea.Parentid = parentId cArea.Type = models.TYPE_LOCAL_TREE if err := cArea.Insert(); err != nil { return &bhomeclient.Reply{ Success:false, Msg:err.Error() } } return &bhomeclient.Reply{ Success: true, Data: cArea} } // @Summary 修改名字 // @Description 修改区域名字 // @Accept json // @Produce json // @Tags menu // @Param id formData string true "区域id" // @Param name formData string true "区域名字" // @Param parentId formData string true "上一级父id" // @Param alias formData string false "备注名称" // @Success 200 {string} json "{"code":200, data:"删除的区域信息",msg:"请求成功", success:true}" // @Failure 200 {string} json "{"code":"错误码", data:"出错信息",msg:"请求失败", success:false}" // @Router /data/api-v/area/update [post] func (ac AreaController) AreaUpdate(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var aram models.Area id := c.PostForm("id") if id == ""{ return &bhomeclient.Reply{ Success: false, Msg: "参数有误"} } name := c.PostForm("name") parentId := c.PostForm("parentId") if parentId == "" { return &bhomeclient.Reply{ Success: false, Msg: "参数有误"} } alias := c.PostForm("alias") rows, err := aram.SelectbyId(id) if err != nil { return &bhomeclient.Reply{ Success: false, Msg: err.Error()} } if rows == 0 { return &bhomeclient.Reply{ Success: false, Msg: "没有记录"} } err = aram.Update(id, name, parentId, alias) if err != nil { return &bhomeclient.Reply{ Success: false, Msg: err.Error()} } return &bhomeclient.Reply{ Success: true, Data: aram} } // @Summary 删除一个区域 // @Description 点击删除按钮时删除一个区域 // @Produce json // @Tags menu // @Param id formData string true "当前id" // @Success 200 {string} json "{"code":200, data:"删除的区域信息",msg:"请求成功", success:true}" // @Failure 200 {string} json "{"code":"500", data:"出错信息",msg:"请求失败", success:false}" // @Router /data/api-v/area/del [post] func (ac AreaController) AreaDelete(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{ var aram models.Area id := c.PostForm("id") logger.Debug("AreaDelete id:",id) if id == "" { return &bhomeclient.Reply{ Success: false, Msg: "参数有误"} } rows, err := aram.SelectbyId(id) if err != nil { return &bhomeclient.Reply{ Success: false, Msg: "参数有误"} } if rows == 0 { return &bhomeclient.Reply{ Success: false, Msg: "没有记录"} } //如果存在子节点或者摄像机 msg, ishas := aram.IsHasCnode(id) if ishas { return &bhomeclient.Reply{ Success: false, Msg: msg} } if err := aram.Delete(id); err != nil { return &bhomeclient.Reply{ Success: false, Msg: err.Error()} } else { return &bhomeclient.Reply{ Success: true, Msg: "删除成功"} } }