package controller import ( "github.com/gin-gonic/gin" "iot_manager_service/app/device/dao" "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 Bridge = new(bridgeCtl) type bridgeCtl struct{} func (c *bridgeCtl) 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.BridgeService.Get(id) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, device)) } func (c *bridgeCtl) List(ctx *gin.Context) { searchValue := ctx.Query("searchValue") 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.BridgeService.List(searchValue, current, size) if err != nil { ctx.JSON(http.StatusOK, err) return } pages := math.Ceil(float64(len(devices)) / float64(size)) rsp := model.RsqBridgeList{ Current: current, Size: size, Total: len(devices), Pages: int(pages), } for _, device := range devices { rsp.Records = append(rsp.Records, device) } ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp)) } func (c *bridgeCtl) CreateOrUpdate(ctx *gin.Context) { value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) var req dao.Bridge if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil)) return } err := service.BridgeService.CreateOrUpdate(claims.UserId, claims.TenantId, req) ctx.JSON(http.StatusOK, err) } func (c *bridgeCtl) Remove(ctx *gin.Context) { value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) var req *model.ReqBridgeRemove if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil)) return } err := service.BridgeService.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 *bridgeCtl) ImportExcel(ctx *gin.Context) { } func (c *bridgeCtl) ExportExcel(ctx *gin.Context) { } func (c *bridgeCtl) ExportTemplate(ctx *gin.Context) { } func (c *bridgeCtl) GetList(ctx *gin.Context) { value, _ := ctx.Get(middleware.Authorization) claims := value.(*middleware.Claims) devices, err := service.BridgeService.GetList(claims.TenantId) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, devices)) }