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.ActiveLearn;
import com.ruoyi.platform.service.ActiveLearnService;
import com.ruoyi.platform.vo.ActiveLearnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

@RestController
@RequestMapping("activeLearn")
@Api("主动学习")
public class ActiveLearnController extends BaseController  {
    @Resource
    private ActiveLearnService activeLearnService;

    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<ActiveLearn>> 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.activeLearnService.queryByPage(name, pageRequest));
    }

    @PostMapping
    @ApiOperation("新增主动学习")
    public GenericsAjaxResult<ActiveLearn> addActiveLearn(@RequestBody ActiveLearnVo activeLearnVo) throws Exception {
        return genericsSuccess(this.activeLearnService.save(activeLearnVo));
    }

    @PutMapping
    @ApiOperation("编辑主动学习")
    public GenericsAjaxResult<String> editActiveLearn(@RequestBody ActiveLearnVo activeLearnVo) throws Exception {
        return genericsSuccess(this.activeLearnService.edit(activeLearnVo));
    }

    @GetMapping("/getActiveLearnDetail")
    @ApiOperation("获取主动学习详细信息")
    public GenericsAjaxResult<ActiveLearnVo> getActiveLearnDetail(@RequestParam("id") Long id) throws IOException {
        return genericsSuccess(this.activeLearnService.getActiveLearnDetail(id));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除主动学习")
    public GenericsAjaxResult<String> deleteActiveLearn(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.activeLearnService.delete(id));
    }

    @PostMapping("/run/{id}")
    @ApiOperation("运行主动学习实验")
    public GenericsAjaxResult<String> runActiveLearn(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.activeLearnService.runActiveLearnIns(id));
    }
}
