package com.ruoyi.platform.controller.experiment;


import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.Experiment;
import com.ruoyi.platform.service.ExperimentService;
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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * (Experiment)表控制层
 *
 * @author makejava
 * @since 2023-11-01 16:50:37
 */
@RestController
@RequestMapping("experiment")
@Api("实验管理")
public class ExperimentController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ExperimentService experimentService;

    /**
     * 分页查询
     *
     * @param experiment 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<Experiment>> queryByPage(Experiment experiment, int page, int size) {
        PageRequest pageRequest = PageRequest.of(page,size);
        return genericsSuccess(this.experimentService.queryByPage(experiment, pageRequest));
    }

    @GetMapping("/status")
    @ApiOperation("查询实验状态")
    public GenericsAjaxResult<Page<Experiment>> selectStatus(@RequestBody Experiment experiment, PageRequest pageRequest) throws IOException {
        return genericsSuccess(this.experimentService.selectStatus(experiment, pageRequest));
    }



    @GetMapping("/configuration")
    @ApiOperation("查询实验配置参数")
    public GenericsAjaxResult<ResponseEntity<Experiment>> showExperimentConfig(@RequestBody Experiment experiment){
        return genericsSuccess(this.experimentService.showExperimentConfig(experiment));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过id查询实验")
    public GenericsAjaxResult<Experiment> queryById(@PathVariable("id") Integer id) throws IOException {
        return  genericsSuccess(this.experimentService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param experiment 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增实验")
    public GenericsAjaxResult<Experiment> add(@RequestBody Experiment experiment) throws Exception {
        return  genericsSuccess(this.experimentService.insert(experiment));
    }

    /**
     * 编辑数据
     *
     * @param experiment 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑实验")
    public GenericsAjaxResult<Experiment> edit(@RequestBody Experiment experiment) throws Exception {
        return  genericsSuccess(this.experimentService.update(experiment));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除实验")
    public GenericsAjaxResult<String> deleteById(@PathVariable("id") Integer id) throws Exception {
        return  genericsSuccess(this.experimentService.removeById(id));
    }



    /**
     * 实验管理页面运行实验
     *
     *
     *
     * @param id 主键
     * @return 运行结果
     */
    @PutMapping("/experiments/{id}")
    @ApiOperation("运行实验")
    public GenericsAjaxResult<Experiment> runExperiment(@PathVariable("id") Integer id) throws Exception {
        return  genericsSuccess(this.experimentService.runExperiment(id));
    }

    /**
     * 实验创建页面确定并运行
     *
     * @param experiment 实体
     * @return 运行结果
     */
    @PostMapping("/addAndRunExperiment")
    @ApiOperation("实验创建页面确定并运行")
    public GenericsAjaxResult<Experiment> addAndRunExperiment(@RequestBody Experiment experiment) throws Exception {
        return  genericsSuccess(this.experimentService.addAndRunExperiment(experiment));
    }
}

