package apis import ( "fmt" "IotAdmin/core/sdk/api" "IotAdmin/core/sdk/pkg/jwt-auth/user" _ "IotAdmin/core/sdk/pkg/response" "github.com/gin-gonic/gin" "IotAdmin/app/iot/models" "IotAdmin/app/iot/service" "IotAdmin/app/iot/service/dto" "IotAdmin/common/permission" ) // IotGroupApi 分组接口 type IotGroupApi struct { api.Api } // GetPage 获取分组列表 // @Summary 获取分组列表 // @Description 获取分组列表 // @Tags 分组管理 // @Param path query string false "分组路径" // @Param name query string false "分组名称" // @Param description query string false "分组描述" // @Param pageSize query int false "页条数" // @Param pageIndex query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]models.IotGroup}} "{"code": 200, "data": [...]}" // @Router /api/iot-group [get] // @Security Bearer func (e IotGroupApi) GetPage(c *gin.Context) { req := dto.IotGroupGetPageReq{} s := service.IotGroupService{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := permission.GetPermissionFromContext(c) list := make([]models.IotGroup, 0) var count int64 err = s.GetPage(&req, p, &list) if err != nil { e.Error(500, err, fmt.Sprintf("获取分组失败,\r\n失败信息 %s", err.Error())) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // Get 获取分组 // @Summary 获取分组 // @Description 获取分组 // @Tags 分组管理 // @Param id path int false "id" // @Success 200 {object} response.Response{data=models.IotGroup} "{"code": 200, "data": [...]}" // @Router /api/iot-group/{id} [get] // @Security Bearer func (e IotGroupApi) Get(c *gin.Context) { req := dto.IotGroupGetReq{} s := service.IotGroupService{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var object models.IotGroup p := permission.GetPermissionFromContext(c) err = s.Get(&req, p, &object) if err != nil { e.Error(500, err, fmt.Sprintf("获取分组失败,\r\n失败信息 %s", err.Error())) return } e.OK(object, "查询成功") } // Insert 添加分组 // @Summary 添加分组 // @Description 添加分组 // @Tags 分组管理 // @Accept application/json // @Product application/json // @Param data body dto.IotGroupInsertReq true "data" // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}" // @Router /api/iot-group [post] // @Security Bearer func (e IotGroupApi) Insert(c *gin.Context) { req := dto.IotGroupInsertReq{} s := service.IotGroupService{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // 设置创建人 req.SetCreateBy(user.GetUserId(c)) err = s.Insert(&req) if err != nil { e.Error(500, err, fmt.Sprintf("添加分组失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "添加成功") } // Update 修改分组 // @Summary 修改分组 // @Description 修改分组 // @Tags 分组管理 // @Accept application/json // @Product application/json // @Param id path int true "id" // @Param data body dto.IotGroupUpdateReq true "body" // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}" // @Router /api/iot-group/{id} [put] // @Security Bearer func (e IotGroupApi) Update(c *gin.Context) { req := dto.IotGroupUpdateReq{} s := service.IotGroupService{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } req.SetUpdateBy(user.GetUserId(c)) p := permission.GetPermissionFromContext(c) err = s.Update(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("修改分组失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "修改成功") } // Delete 删除分组 // @Summary 删除分组 // @Description 删除分组 // @Tags 分组管理 // @Param data body dto.IotGroupDeleteReq true "body" // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}" // @Router /api/iot-group [delete] // @Security Bearer func (e IotGroupApi) Delete(c *gin.Context) { s := service.IotGroupService{} req := dto.IotGroupDeleteReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // req.SetUpdateBy(user.GetUserId(c)) p := permission.GetPermissionFromContext(c) err = s.Remove(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("删除分组失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "删除成功") }