package com.ruoyi.platform.controller.activeLearn;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.ActiveLearnIns;
import com.ruoyi.platform.service.ActiveLearnInsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Page;

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

@RestController
@RequestMapping("activeLearnIns")
@Api("主动学习实验实例")
public class ActiveLearnInsController extends BaseController {
    @Resource
    private ActiveLearnInsService activeLearnInsService;

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

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

    @PutMapping
    @ApiOperation("编辑实验实例")
    public GenericsAjaxResult<ActiveLearnIns> update(@RequestBody ActiveLearnIns activeLearnIns) {
        return genericsSuccess(this.activeLearnInsService.update(activeLearnIns));
    }

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

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

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

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

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