package controller import ( "github.com/gin-gonic/gin" "iot_manager_service/app/dao" "iot_manager_service/app/model" "iot_manager_service/app/service" "iot_manager_service/app/utils" "math" "net/http" "strconv" ) // 灯控基本信息管理对象 var Light = new(lightCtl) type lightCtl struct{} func (c *lightCtl) Detail(ctx *gin.Context) { id, e := strconv.Atoi(ctx.Query("id")) if e != nil { ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(e.Error(), nil)) return } device, err := service.LightControlService.Get(id) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device)) } func (c *lightCtl) List(ctx *gin.Context) { searchValue := ctx.Query("searchValue") controlType := ctx.Query("controlType") zigbeeId := ctx.Query("zigbeeId") 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.LightControlService.List(searchValue, controlType, zigbeeId, current, size) if err != nil { ctx.JSON(http.StatusOK, err) return } pages := math.Ceil(float64(len(devices)) / float64(size)) rsp := model.RspLightControlList{ Current: current, Size: size, Total: len(devices), Pages: int(pages), } for _, d := range devices { rsp.Records = append(rsp.Records, lightControlDaoToModel(d)) } ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, rsp)) } func (c *lightCtl) CreateOrUpdate(ctx *gin.Context) { var req *dao.LightControl if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil)) return } err := service.LightControlService.CreateOrUpdate(req) ctx.JSON(http.StatusOK, err) } func (c *lightCtl) Remove(ctx *gin.Context) { var req *model.ReqLightControlRemove if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil)) return } err := service.LightControlService.Remove(req.IDs) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil)) } func (c *lightCtl) ImportExcel(ctx *gin.Context) { } func (c *lightCtl) ExportExcel(ctx *gin.Context) { } func (c *lightCtl) ExportTemplate(ctx *gin.Context) { } func (c *lightCtl) GetList(ctx *gin.Context) { devices, err := service.LightControlService.GetList() if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices)) } func (c *lightCtl) Enable(ctx *gin.Context) { var req *model.ReqLightControlEnable if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil)) return } if req.Status != model.Enable_Enable && req.Status != model.Enable_Disable { ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(model.EnableInvalid, nil)) return } err := service.LightControlService.Enable(req.ID, req.Status) if err != nil { ctx.JSON(http.StatusOK, err) return } ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil)) } func (c *lightCtl) Switch(ctx *gin.Context) { } func lightControlDaoToModel(device dao.LightControl) model.LightControlDetail { return model.LightControlDetail{LightControl: device} }