package controllers import ( "sort" "strconv" "strings" "vamicro/camera-common/models" "vamicro/camera-service/cache" "vamicro/extend/util" "basic.com/pubsub/protomsg.git" "basic.com/valib/bhomeclient.git" "basic.com/valib/bhomedbapi.git" "basic.com/valib/logger.git" uuid "github.com/satori/go.uuid" ) type AreaController struct { bk bhomeclient.Broker } // @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()} } logger.Debug("len(areas):", len(areas)) lkMap := make(map[string]string) linkWhere := "" if searchType == 3 { //查联动 var crApi bhomedbapi.CameraRuleApi lb, lkList := crApi.FindAllCameraLink() if !lb { return &bhomeclient.Reply{Msg: "查询联动配置失败"} } if len(lkList) == 0 { return &bhomeclient.Reply{Success: true, Data: []struct{}{}} } for _, lc := range lkList { cArr := strings.Split(lc.CameraIds, ",") for _, lcId := range cArr { if _, ok := lkMap[lcId]; !ok { lkMap[lcId] = lcId } } } for k := range lkMap { linkWhere += "'" + k + "'," } linkWhere = strings.TrimRight(linkWhere, ",") } var cam models.Camera cams, err := cam.Find(searchType, cameraName, models.TYPE_LOCAL_CAMERA, isPlatform, linkWhere) 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) allRules, e2 := cache.GetCameraRules() ruleM := make(map[string]protomsg.CameraAndRules) if e2 == nil && allRules != nil { for _, r := range allRules { if r.CameraInfo != nil { ruleM[r.CameraInfo.Id] = r } } } //获取督查任务 tasks := models.GetTasks() var totalAreas models.AreaList totalAreas = rAreas sort.Sort(totalAreas) logger.Debug("len(rAreas):", len(rAreas)) arr := menuFormat(totalAreas, parentId, totalCams, treeType, tasks) 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 if taskInfo, ok := tasks[camera.Id]; ok && taskInfo != nil && len(taskInfo) > 0 && (camera.RunType == models.TYPE_RUNTYPE_POLL || camera.RunType == models.TYPE_RUNTYPE_REALTIME) { isAI = true } } 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.Area, parentId string, dbCams []models.CameraTreeNode, treeType int, tasks map[string][]interface{}) []models.TreeMenu { var arr []models.TreeMenu 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, tasks) 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 if taskInfo, ok := tasks[camera.Id]; ok && taskInfo != nil && len(taskInfo) > 0 && (camera.RunType == models.TYPE_RUNTYPE_POLL || camera.RunType == models.TYPE_RUNTYPE_REALTIME) { isAI = true } //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" { 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 cArea.IsUnique("", cArea.Name) { return &bhomeclient.Reply{Msg: "名称不允许重复"} } 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") if aram.IsUnique(id, name) { return &bhomeclient.Reply{Msg: "名称不允许重复"} } 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: "删除成功"} } }