package com.ruoyi.platform.controller.ray;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.Ray;
import com.ruoyi.platform.service.RayService;
import com.ruoyi.platform.vo.RayVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("ray")
@Api("自动超参数寻优")
public class RayController extends BaseController {
    @Resource
    private RayService rayService;

    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<Ray>> queryByPage(@RequestParam("page") int page,
                                                     @RequestParam("size") int size,
                                                     @RequestParam(value = "name", required = false) String name) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return genericsSuccess(this.rayService.queryByPage(name, pageRequest));
    }

    @PostMapping
    @ApiOperation("新增自动超参数寻优")
    public GenericsAjaxResult<Ray> addRay(@RequestBody RayVo rayVo) throws Exception {
        return genericsSuccess(this.rayService.save(rayVo));
    }

    @PutMapping
    @ApiOperation("编辑自动超参数寻优")
    public GenericsAjaxResult<String> editRay(@RequestBody RayVo rayVo) throws Exception{
        return genericsSuccess(this.rayService.edit(rayVo));
    }

    @GetMapping("/getRayDetail")
    @ApiOperation("获取自动超参数寻优详细信息")
    public GenericsAjaxResult<RayVo> getRayDetail(@RequestParam("id") Long id) throws IOException {
        return genericsSuccess(this.rayService.getRayDetail(id));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除自动超参数寻优")
        public GenericsAjaxResult<String> deleteRay(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.rayService.delete(id));
    }

    @PostMapping("/run/{id}")
    @ApiOperation("运行自动超参数寻优实验")
    public GenericsAjaxResult<String> runRay(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.rayService.runRayIns(id));
    }
}
