package app import ( "context" "fmt" "github.com/gin-gonic/gin" "github.com/redis/go-redis/v9" "github.com/sirupsen/logrus" "go.uber.org/zap" "lcfns/global" "lcfns/model/app" "lcfns/model/common/request" "lcfns/model/common/response" "lcfns/service" "lcfns/utils" "net/http" "strings" ) type CameraApi struct { } var cs = service.ServiceGroupApp.AppServiceGroup.CameraService // CreateCamera 创建Camera // @Tags Camera // @Summary 创建Camera // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data body app.Camera true "创建Camera" // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Router /camera/createCamera [post] func (cameraApi *CameraApi) CreateCamera(c *gin.Context) { var camera app.Camera err := c.ShouldBindJSON(&camera) if err != nil { response.FailWithMessage(err.Error(), c) return } if err := cs.CreateCamera(&camera); err != nil { logrus.Error("创建失败!", zap.Error(err)) response.FailWithMessage("创建失败", c) } else { response.OkWithMessage("创建成功", c) } } // DeleteCamera 删除Camera // @Tags Camera // @Summary 删除Camera // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data body app.Camera true "删除Camera" // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" // @Router /camera/deleteCamera [delete] func (cameraApi *CameraApi) DeleteCamera(c *gin.Context) { var camera app.Camera err := c.ShouldBindJSON(&camera) if err != nil { response.FailWithMessage(err.Error(), c) return } if err := cs.DeleteCamera(camera); err != nil { logrus.Error("删除失败!", zap.Error(err)) response.FailWithMessage("删除失败", c) } else { response.OkWithMessage("删除成功", c) } } // DeleteCameraByIds 批量删除Camera // @Tags Camera // @Summary 批量删除Camera // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data body request.IdsReq true "批量删除Camera" // @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}" // @Router /camera/deleteCameraByIds [delete] func (cameraApi *CameraApi) DeleteCameraByIds(c *gin.Context) { var IDS request.IdsReq err := c.ShouldBindJSON(&IDS) if err != nil { response.FailWithMessage(err.Error(), c) return } deletedBy := utils.GetUserID(c) if err := cs.DeleteCameraByIds(IDS, deletedBy); err != nil { logrus.Error("批量删除失败!", zap.Error(err)) response.FailWithMessage("批量删除失败", c) } else { response.OkWithMessage("批量删除成功", c) } } // UpdateCamera 更新Camera // @Tags Camera // @Summary 更新Camera // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data body app.Camera true "更新Camera" // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}" // @Router /camera/updateCamera [put] func (cameraApi *CameraApi) UpdateCamera(c *gin.Context) { var camera app.Camera err := c.ShouldBindJSON(&camera) if err != nil { response.FailWithMessage(err.Error(), c) return } if err := cs.UpdateCamera(camera); err != nil { logrus.Error("更新失败!", zap.Error(err)) response.FailWithMessage("更新失败", c) } else { response.OkWithMessage("更新成功", c) } } // FindCamera 用id查询Camera // @Tags Camera // @Summary 用id查询Camera // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data query app.Camera true "用id查询Camera" // @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}" // @Router /camera/findCamera [get] func (cameraApi *CameraApi) FindCamera(c *gin.Context) { var camera app.Camera err := c.ShouldBindQuery(&camera) if err != nil { response.FailWithMessage(err.Error(), c) return } if recamera, err := cs.GetCamera(camera.ID); err != nil { logrus.Error("查询失败!", zap.Error(err)) response.FailWithMessage("查询失败", c) } else { response.OkWithData(gin.H{"recamera": recamera}, c) } } func (cameraApi *CameraApi) Status(c *gin.Context) { topic := c.GetHeader("topic") topics := strings.Split(topic, "/") if len(topics) != 2 { logrus.Error("前端http header : topic数据不对", zap.String("topic", topic)) response.FailWithMessage("topic头数据不对", c) } get, err := global.GVA_REDIS.Get(context.Background(), fmt.Sprintf("lcfns:status:%s:%s", topics[0], topics[1])).Result() if err == redis.Nil { //-1边缘端不在线 response.OkWithData(-1, c) } if err != nil { response.FailWithMessage("redis 查询失败", c) } //摄像头:1在线,0不在线 response.OkWithData(get, c) } func (cameraApi *CameraApi) RewindText(ctx *gin.Context) { streamId := ctx.Query("streamId") date := ctx.Query("date") //edgeService := edgeService.ForRewindService{} operate, err := cs.GetRewindText(streamId, date) if err != nil { ctx.JSON(http.StatusOK, err) } if operate == "" { ctx.JSON(http.StatusOK, nil) return } response.OkWithData(operate, ctx) }