package cn.iocoder.yudao.module.llm.controller.admin.dataset;

import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetFilesPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetFilesRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetFilesSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetFilesDO;
import cn.iocoder.yudao.module.llm.service.dataset.DatasetFilesService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


/*
@Tag(name = "管理后台 - 数据集数据文件")
@RestController
@RequestMapping("/llm/dataset-files")
@Validated*/
public class DatasetFilesController {

    @Resource
    private DatasetFilesService datasetFilesService;

    @PostMapping("/create")
    @Operation(summary = "创建数据集数据文件")
    @PreAuthorize("@ss.hasPermission('llm:dataset-files:create')")
    public CommonResult<Long> createDatasetFiles(@Valid @RequestBody DatasetFilesSaveReqVO createReqVO) {
        return success(datasetFilesService.createDatasetFiles(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据集数据文件")
    @PreAuthorize("@ss.hasPermission('llm:dataset-files:update')")
    public CommonResult<Boolean> updateDatasetFiles(@Valid @RequestBody DatasetFilesSaveReqVO updateReqVO) {
        datasetFilesService.updateDatasetFiles(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据集数据文件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:dataset-files:delete')")
    public CommonResult<Boolean> deleteDatasetFiles(@RequestParam("id") Long id) {
        datasetFilesService.deleteDatasetFiles(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据集数据文件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:dataset-files:query')")
    public CommonResult<DatasetFilesRespVO> getDatasetFiles(@RequestParam("id") Long id) {
        DatasetFilesDO datasetFiles = datasetFilesService.getDatasetFiles(id);
        return success(BeanUtils.toBean(datasetFiles, DatasetFilesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据集数据文件分页")
    @PreAuthorize("@ss.hasPermission('llm:dataset-files:query')")
    public CommonResult<PageResult<DatasetFilesRespVO>> getDatasetFilesPage(@Valid DatasetFilesPageReqVO pageReqVO) {
        PageResult<DatasetFilesDO> pageResult = datasetFilesService.getDatasetFilesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DatasetFilesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据集数据文件 Excel")
    @PreAuthorize("@ss.hasPermission('llm:dataset-files:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDatasetFilesExcel(@Valid DatasetFilesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DatasetFilesDO> list = datasetFilesService.getDatasetFilesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据集数据文件.xls", "数据", DatasetFilesRespVO.class,
                        BeanUtils.toBean(list, DatasetFilesRespVO.class));
    }

}