package com.ruoyi.platform.controller.dataset;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.platform.domain.Dataset;
import com.ruoyi.platform.service.DatasetService;
import com.ruoyi.platform.vo.DatasetVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
@RestController
@RequestMapping("newdataset")
@Api(value = "新数据集管理")
public class NewDatasetFromGitController {
    /**
     * 服务对象
     */
    @Resource
    private DatasetService datasetService;



    /**
     * 新增数据集与版本新
     *
     * @param datasetVo 实体
     * @return 新增结果
     */
    @PostMapping("/addDatasetAndVersion")
    @ApiOperation("添加数据集和版本")
    public AjaxResult addDatasetAndVersion(@RequestBody DatasetVo datasetVo) throws Exception {
        return AjaxResult.success(this.datasetService.newCreateDataset(datasetVo));

    }


    /**
     * 新增数据集与版本新
     *
     * @param datasetVo 实体
     * @return 新增结果
     */
    @PostMapping("/addVersion")
    @ApiOperation("添加版本")
    public AjaxResult addVersion(@RequestBody DatasetVo datasetVo) throws Exception {
        return AjaxResult.success(this.datasetService.newCreateVersion(datasetVo));

    }


    /**
     * 上传数据集
     *
     * @param files 上传的数据集文件
     * @param uuid 上传唯一标识，构建url
     * @return 上传结果
     */
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping("/upload")
    @ApiOperation(value = "上传数据集")
    public AjaxResult uploadDataset(@RequestParam("file") MultipartFile[] files, @RequestParam("uuid") String uuid) throws Exception {
        return AjaxResult.success(this.datasetService.uploadDatasetlocal(files,uuid));
    }

    /**
     * 数据集打包下载
     *
     * @param version  数据集版本
     * @return 单条数据
     */
    @GetMapping("/downloadAllFiles")
    @ApiOperation(value = "下载同一版本下所有数据集，并打包")
    public ResponseEntity<InputStreamResource> downloadAllDatasetFiles(@RequestParam("repository_name") String repositoryName, @RequestParam("version") String version) throws Exception {
        return datasetService.downloadAllDatasetFilesNew(repositoryName, version);
    }

    /**
     * 下载数据集
     *
     * @param dataset_version_id  ps:这里的id是dataset_version表的主键
     * @return 单条数据
     */

    @GetMapping("/download/{dataset_version_id}")
    @ApiOperation(value = "下载单个数据集文件", notes = "根据数据集版本表id下载单个数据集文件")
    public ResponseEntity<InputStreamResource> downloadDataset(@PathVariable("dataset_version_id") Integer dataset_version_id) throws Exception {
        return datasetService.downloadDataset(dataset_version_id);
    }

    @GetMapping("/queryDatasets")
    @ApiOperation("数据集广场公开数据集分页查询,根据data_type,data_tag筛选，true公开false私有")
    public AjaxResult queryDatasets(Dataset dataset, @RequestParam("page") int page,
                                     @RequestParam("size") int size,
                                    @RequestParam(value = "is_public") Boolean isPublic,
                                     @RequestParam(value = "data_type", required = false) String dataType,
                                     @RequestParam(value = "data_tag", required = false) String dataTag) throws Exception {
        PageRequest pageRequest = PageRequest.of(page, size);
        if(isPublic){
            return AjaxResult.success(this.datasetService.newPubilcQueryByPage(dataset, pageRequest));
        }else {
            return AjaxResult.success(this.datasetService.newPersonalQueryByPage(dataset, pageRequest));
        }
    }

    @GetMapping("/getdatasetDetail")
    @ApiOperation(value = "获取数据集详情")
    public AjaxResult getDatasetVersions(@PathVariable("datasetId") Integer datasetId) throws Exception {
        return AjaxResult.success(this.datasetService.getDatasetVersions(datasetId));
    }
}
