123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package controller
- import (
- "github.com/gin-gonic/gin"
- "iot_manager_service/app/device/model"
- "iot_manager_service/app/device/service"
- "iot_manager_service/app/middleware"
- "iot_manager_service/util"
- "math"
- "net/http"
- "strconv"
- )
- // 摄像头管理对象
- var Camera = new(cameraCtl)
- type cameraCtl struct{}
- func (c *cameraCtl) Detail(ctx *gin.Context) {
- id, e := strconv.Atoi(ctx.Query("id"))
- if e != nil {
- ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(e.Error(), nil))
- return
- }
- device, err := service.CameraService.Get(id)
- if err != nil {
- ctx.JSON(http.StatusOK, err)
- return
- }
- ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device))
- }
- func (c *cameraCtl) List(ctx *gin.Context) {
- searchValue := ctx.Query("searchValue")
- cameraType := ctx.Query("cameraType")
- current, _ := strconv.Atoi(ctx.Query("current"))
- size, _ := strconv.Atoi(ctx.Query("size"))
- if current == 0 {
- current = 1
- }
- if size <= 0 || size > 100 {
- size = 10
- }
- devices, err := service.CameraService.List(searchValue, cameraType, current, size)
- if err != nil {
- ctx.JSON(http.StatusOK, err)
- return
- }
- pages := math.Ceil(float64(len(devices)) / float64(size))
- rsp := model.RspCameraList{
- Current: current,
- Size: size,
- Total: len(devices),
- Pages: int(pages),
- Records: devices,
- }
- ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
- }
- func (c *cameraCtl) CreateOrUpdate(ctx *gin.Context) {
- value, _ := ctx.Get(middleware.Authorization)
- claims := value.(*middleware.Claims)
- // 参数验证
- var req *model.CameraDetail
- if err := ctx.ShouldBind(&req); err != nil {
- ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
- return
- }
- err := service.CameraService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
- if err != nil {
- ctx.JSON(http.StatusOK, err)
- return
- }
- ctx.JSON(http.StatusOK, nil)
- }
- func (c *cameraCtl) Remove(ctx *gin.Context) {
- value, _ := ctx.Get(middleware.Authorization)
- claims := value.(*middleware.Claims)
- var req *model.ReqCameraRemove
- if err := ctx.ShouldBindJSON(&req); err != nil {
- ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
- return
- }
- err := service.CameraService.Remove(claims.UserId, req.IDs)
- if err != nil {
- ctx.JSON(http.StatusOK, err)
- return
- }
- ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
- }
- func (c *cameraCtl) Count(ctx *gin.Context) {
- }
- func (c *cameraCtl) ImportExcel(ctx *gin.Context) {
- }
- func (c *cameraCtl) ExportExcel(ctx *gin.Context) {
- }
- func (c *cameraCtl) ExportTemplate(ctx *gin.Context) {
- }
- func (c *cameraCtl) GetList(ctx *gin.Context) {
- value, _ := ctx.Get(middleware.Authorization)
- claims := value.(*middleware.Claims)
- devices, err := service.CameraService.GetList(claims.TenantId)
- if err != nil {
- ctx.JSON(http.StatusOK, err)
- return
- }
- ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices))
- }
- func (c *cameraCtl) Enable(ctx *gin.Context) {
- var req *model.ReqCameraEnable
- if err := ctx.ShouldBindJSON(&req); err != nil {
- ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
- return
- }
- if req.Status != model.Enable_Enable && req.Status != model.Enable_Disable {
- ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.EnableInvalid, nil))
- return
- }
- err := service.CameraService.Enable(req.ID, req.Status)
- if err != nil {
- ctx.JSON(http.StatusOK, err)
- return
- }
- ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
- }
|