package controller import ( "github.com/gin-gonic/gin" "iot_manager_service/app/middleware" "iot_manager_service/app/multimedia/dao" "iot_manager_service/app/multimedia/model" "iot_manager_service/app/multimedia/service" "iot_manager_service/util/common" "math" "net/http" "strconv" ) var Library = new(libraryCtl) type libraryCtl struct{} func (c *libraryCtl) Detail(ctx *gin.Context) { id, e := strconv.Atoi(ctx.Query("id")) if e != nil { ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil)) return } library, err := service.LibraryService.Get(id) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, library)) } func (c *libraryCtl) List(ctx *gin.Context) { searchValue := ctx.Query("searchValue") current, _ := strconv.Atoi(ctx.Query("current")) size, _ := strconv.Atoi(ctx.Query("size")) s := ctx.Query("sysType") if current == 0 { current = 1 } if size <= 0 || size > 100 { size = 10 } sysType := -1 if s != "" { sysType, _ = strconv.Atoi(s) } value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) libraries, err := service.LibraryService.List(claims.TenantId, searchValue, current, size, sysType) if err != nil { ctx.JSON(http.StatusOK, err) return } pages := math.Ceil(float64(len(libraries)) / float64(size)) rsp := model.RsqLibraryList{ Current: current, Size: size, Total: len(libraries), Pages: int(pages), Records: libraries, } ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp)) } func (c *libraryCtl) Remove(ctx *gin.Context) { ids := ctx.Query("ids") id, e := strconv.Atoi(ids) if e != nil { ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil)) return } value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) err := service.LibraryService.Remove(claims.UserId, claims.TenantId, id) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil)) } func (c *libraryCtl) GetList(ctx *gin.Context) { libName := ctx.Query("libName") t := ctx.Query("libType") s := ctx.Query("sysType") value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) libType := -1 if t != "" { libType, _ = strconv.Atoi(t) } sysType := -1 if s != "" { sysType, _ = strconv.Atoi(s) } library, err := service.LibraryService.GetList(claims.TenantId, libName, libType, sysType) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, library)) } func (c *libraryCtl) UploadFile(ctx *gin.Context) { file, err := ctx.FormFile("file") if err != nil { ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil)) return } value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) rsp, e := service.LibraryService.UploadFile(claims.TenantId, file) if e != nil { ctx.JSON(http.StatusOK, e) return } ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp)) } func (c *libraryCtl) Submit(ctx *gin.Context) { var req dao.Library if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil)) return } value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) e := service.LibraryService.Submit(claims.TenantId, claims.UserId, req) if e != nil { ctx.JSON(http.StatusOK, e) return } ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil)) }