package com.ruoyi.platform.controller.autoML;

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.AutoMl;
import com.ruoyi.platform.service.AutoMlService;
import com.ruoyi.platform.vo.AutoMlVo;
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 org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("autoML")
@Api("自动机器学习")
public class AutoMlController extends BaseController {

    @Resource
    private AutoMlService autoMlService;

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

    @PostMapping
    @ApiOperation("新增自动机器学习")
    public GenericsAjaxResult<AutoMl> addAutoMl(@RequestBody AutoMlVo autoMlVo) throws Exception {
        return genericsSuccess(this.autoMlService.save(autoMlVo));
    }

    @PutMapping
    @ApiOperation("编辑自动机器学习")
    public GenericsAjaxResult<String> editAutoMl(@RequestBody AutoMlVo autoMlVo) throws Exception {
        return genericsSuccess(this.autoMlService.edit(autoMlVo));
    }
    @GetMapping("/getAutoMlDetail")
    @ApiOperation("获取自动机器学习详细信息")
    public GenericsAjaxResult<AutoMlVo> getAutoMlDetail(@RequestParam("id") Long id) throws IOException {
        return genericsSuccess(this.autoMlService.getAutoMlDetail(id));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除自动机器学习")
    public GenericsAjaxResult<String> deleteAutoMl(@PathVariable("id") Long id) {
        return genericsSuccess(this.autoMlService.delete(id));
    }

    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping("/upload")
    @ApiOperation(value = "上传数据文件csv", notes = "上传数据文件csv，并将信息存入数据库。")
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("uuid") String uuid) throws Exception {
        return AjaxResult.success(this.autoMlService.upload(file, uuid));
    }

    @PostMapping("/run/{id}")
    @ApiOperation("运行自动机器学习实验")
    public GenericsAjaxResult<String> runAutoML(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.autoMlService.runAutoMlIns(id));
    }
}
