package com.ruoyi.platform.controller.component;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.platform.domain.Component;
import com.ruoyi.platform.service.ComponentService;
import com.ruoyi.platform.vo.ComponentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Component)表控制层
 *
 * @author Xidaray
 * @since 2023-11-30 11:34:26
 */
@RestController
@RequestMapping("component")
@Api("组件管理")
public class ComponentController {
    /**
     * 服务对象
     */
    @Resource
    private ComponentService componentService;

    /**
     * 分页查询
     *
     * @param component 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation("分页查询")
    public AjaxResult queryByPage( Component component, int page, int size) {
        PageRequest pageRequest = PageRequest.of(page,size);
        return  AjaxResult.success(this.componentService.queryByPage(component, pageRequest));
    }


    /**
     * 查询全部
     *
     *
     *
     * @return 查询结果
     */
     @GetMapping("/components/all")
     @ApiOperation("查询全部")
     public AjaxResult queryAll() throws Exception {
         return  AjaxResult.success(this.componentService.queryAllGroupedByCategory());
     }



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.componentService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param component 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("添加组件")
    public AjaxResult add(@RequestBody ComponentVo component) {
        return  AjaxResult.success(this.componentService.insert(component));
    }

    /**
     * 编辑数据
     *
     * @param component 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑组件")
    public AjaxResult edit(@RequestBody ComponentVo component) {
        return  AjaxResult.success(this.componentService.update(component));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除组件")
    public AjaxResult deleteById(@PathVariable("id") Integer id) throws Exception {
        return  AjaxResult.success(this.componentService.removeById(id));
    }

}

