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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.dto.DataJsonTemplate;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetSaveReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetTreeNode;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetDO;
import cn.iocoder.yudao.module.llm.service.dataset.DatasetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

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

    @Resource
    private DatasetService datasetService;

    @PostMapping("/create")
    @Operation(summary = "创建数据集")
    @PreAuthorize("@ss.hasPermission('llm:dataset:create')")
    public CommonResult<Long> createDataset(@Valid @RequestBody DatasetSaveReqVO createReqVO) {
        return success(datasetService.createDataset(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据集")
    @PreAuthorize("@ss.hasPermission('llm:dataset:update')")
    public CommonResult<Boolean> updateDataset(@Valid @RequestBody DatasetSaveReqVO updateReqVO) {
        datasetService.updateDataset(updateReqVO);
        return success(true);
    }

    @GetMapping("/all")
    @Operation(summary = "查询所有数据集接口")
    @PreAuthorize("@ss.hasPermission('llm:dataset:query')")
    public CommonResult<List<DatasetTreeNode>> all() {
        List<DatasetTreeNode> list = datasetService.queryAll();
        return success(list);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得数据集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:dataset:query')")
    public CommonResult<DatasetRespVO> getDataset(@RequestParam("id") Long id) {
        return success(datasetService.getDataset(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据集分页")
    @PreAuthorize("@ss.hasPermission('llm:dataset:query')")
    public CommonResult<PageResult<DatasetRespVO>> getDatasetPage(@Valid DatasetPageReqVO pageReqVO) {
        PageResult<DatasetDO> pageResult = datasetService.getDatasetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DatasetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据集 Excel")
    @PreAuthorize("@ss.hasPermission('llm:dataset:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDatasetExcel(@Valid DatasetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DatasetDO> list = datasetService.getDatasetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据集.xls", "数据", DatasetRespVO.class,
                        BeanUtils.toBean(list, DatasetRespVO.class));
    }

}
