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.RayIns;
import com.ruoyi.platform.service.RayInsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

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

@RestController
@RequestMapping("rayIns")
@Api("自动超参数寻优实验实例")
public class RayInsController extends BaseController {
    @Resource
    private RayInsService rayInsService;

    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<RayIns>> queryByPage(Long rayId, int page, int size) throws IOException {
        PageRequest pageRequest = PageRequest.of(page, size);
        return genericsSuccess(this.rayInsService.queryByPage(rayId, pageRequest));
    }

    @PostMapping
    @ApiOperation("新增实验实例")
    public GenericsAjaxResult<RayIns> add(@RequestBody RayIns rayIns) {
        return genericsSuccess(this.rayInsService.insert(rayIns));
    }

    @PutMapping
    @ApiOperation("编辑实验实例")
    public GenericsAjaxResult update(@RequestBody RayIns rayIns) {
        return genericsSuccess(this.rayInsService.update(rayIns));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除实验实例")
    public GenericsAjaxResult<String> deleteById(@PathVariable("id") Long id) {
        return genericsSuccess(this.rayInsService.deleteById(id));
    }

    @DeleteMapping("batchDelete")
    @ApiOperation("批量删除实验实例")
    public GenericsAjaxResult<String> batchDelete(@RequestBody List<Long> ids) {
        return genericsSuccess(this.rayInsService.batchDelete(ids));
    }

    @PutMapping("{id}")
    @ApiOperation("终止实验实例")
    public GenericsAjaxResult<Boolean> terminateRayIns(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.rayInsService.terminateRayIns(id));
    }

    @GetMapping("{id}")
    @ApiOperation("查看实验实例详情")
    public GenericsAjaxResult<RayIns> getDetailById(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.rayInsService.getDetailById(id));
    }

    @PostMapping("/getExpMetrics")
    @ApiOperation("获取当前实验的指标对比地址")
    @ApiResponse
    public GenericsAjaxResult<String> getExpMetrics(@RequestBody String trailIds) throws Exception {
        return genericsSuccess(rayInsService.getExpMetrics(trailIds));
    }
}
