package api import ( "fmt" "github.com/gin-gonic/gin" "go_backend/internal/dbs" "go_backend/internal/model" "go_backend/internal/utils" ) type CameraVO struct { ID int32 `json:"id"` CameraID string `json:"camera_id"` DisplayName string `json:"display_name"` ParentCategoryID []int `json:"parent_category_id"` ViewPriority int32 `json:"view_priority"` Status string `json:"status"` Type string `json:"type"` IPAddress string `json:"ip_address"` IsDeleted bool `json:"is_deleted"` CreateTime string `json:"create_time"` DeletedTime string `json:"deleted_time"` UpdateTime string `json:"update_time"` ChildVO []*CameraVO `json:"child"` } func GetAllCameras(router *gin.RouterGroup) { router.GET("/camerasList", func(c *gin.Context) { cameras := make([]model.Camera, 0) db := dbs.GetGormDB() db.Find(&cameras) cameraMap := make(map[int][]model.Camera) for _, camera := range cameras { cameraMap[int(camera.ViewPriority)] = append(cameraMap[(int(camera.ViewPriority))], camera) } cameraVOList := make([]*CameraVO, 0) level := 0 cameraRecord := make(map[int]*CameraVO) for priority, cameraArray := range cameraMap { if priority == 0 { for _, camera := range cameraArray { vo := transVO(camera) cameraVOList = append(cameraVOList, &vo) cameraRecord[int(camera.ID)] = &vo } level += 1 continue } if priority == level { tempRecord := make(map[int]*CameraVO) for _, camera := range cameraArray { vo := transVO(camera) parentId := vo.ParentCategoryID[len(vo.ParentCategoryID)-1] if pointer, exists := cameraRecord[parentId]; exists { pointer.ChildVO = append(pointer.ChildVO, &vo) utils.PrintSlice(vo.ParentCategoryID) newParent := make([]int, 0) utils.PrintSlice(pointer.ParentCategoryID) for _, id := range pointer.ParentCategoryID { newParent = append(newParent, id) } newParent = append(newParent, parentId) vo.ParentCategoryID = newParent tempRecord[int(vo.ID)] = &vo } } cameraRecord = tempRecord level += 1 } } Success(c, cameraVOList) }) } type Node struct { ParentID int `json:"parent_id"` DisplayName string `json:"display_name"` Ip string `json:"ip"` } func CreateNode(router *gin.RouterGroup) { router.POST("/camera/addNode", func(c *gin.Context) { var req Node err := c.ShouldBindJSON(&req) if err != nil { fmt.Println("ctx.ShouldBindJSON err: ", err) c.JSON(400, gin.H{"error": err.Error()}) return } newNode := model.Camera{ ParentCategoryID: int32(req.ParentID), DisplayName: req.DisplayName, IPAddress: req.Ip, } db := dbs.GetGormDB() db.Create(&newNode) Success(c, "success") }) } type ModifyNodeData struct { Id int `json:"id"` DisplayName string `json:"display_name"` Ip string `json:"ip"` } func ModifyNode(router *gin.RouterGroup) { router.POST("/camera/modify", func(c *gin.Context) { var req ModifyNodeData err := c.ShouldBindJSON(&req) if err != nil { fmt.Println("ctx.ShouldBindJSON err: ", err) c.JSON(400, gin.H{"error": err.Error()}) return } node := model.Camera{ ID: int32(req.Id), IPAddress: req.Ip, } if req.DisplayName != "" { node.DisplayName = req.DisplayName } db := dbs.GetGormDB() result := db.Updates(&node) Success(c, result) }) } func transVO(camera model.Camera) CameraVO { vo := CameraVO{ ID: camera.ID, CameraID: camera.CameraID, DisplayName: camera.DisplayName, ParentCategoryID: make([]int, 1), ViewPriority: camera.ViewPriority, Status: camera.Status, Type: camera.Type, IPAddress: camera.IPAddress, IsDeleted: camera.IsDeleted, CreateTime: camera.CreateTime, DeletedTime: camera.DeletedTime, UpdateTime: camera.UpdateTime, ChildVO: make([]*CameraVO, 0), } vo.ParentCategoryID[0] = int(camera.ParentCategoryID) return vo }