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

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.*;

import cn.iocoder.yudao.module.llm.controller.admin.knowledgedocumentschunks.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgedocumentschunks.KnowledgeDocumentsChunksDO;
import cn.iocoder.yudao.module.llm.service.knowledgedocumentschunks.KnowledgeDocumentsChunksService;

@Tag(name = "管理后台 - 知识库文档块")
@RestController
@RequestMapping("/llm/knowledge-documents-chunks")
@Validated
public class KnowledgeDocumentsChunksController {

    @Resource
    private KnowledgeDocumentsChunksService knowledgeDocumentsChunksService;

    @PostMapping("/create")
    @Operation(summary = "创建知识库文档块")
//    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks:create')")
    public CommonResult<Long> createKnowledgeDocumentsChunks(@Valid @RequestBody KnowledgeDocumentsChunksSaveReqVO createReqVO) {
        return success(knowledgeDocumentsChunksService.createKnowledgeDocumentsChunks(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新知识库文档块")
//    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks:update')")
    public CommonResult<Boolean> updateKnowledgeDocumentsChunks(@Valid @RequestBody KnowledgeDocumentsChunksSaveReqVO updateReqVO) {
        knowledgeDocumentsChunksService.updateKnowledgeDocumentsChunks(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除知识库文档块")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks:delete')")
    public CommonResult<Boolean> deleteKnowledgeDocumentsChunks(@RequestParam("id") Long id) {
        knowledgeDocumentsChunksService.deleteKnowledgeDocumentsChunks(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得知识库文档块")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks:query')")
    public CommonResult<KnowledgeDocumentsChunksRespVO> getKnowledgeDocumentsChunks(@RequestParam("id") Long id) {
        KnowledgeDocumentsChunksDO knowledgeDocumentsChunks = knowledgeDocumentsChunksService.getKnowledgeDocumentsChunks(id);
        return success(BeanUtils.toBean(knowledgeDocumentsChunks, KnowledgeDocumentsChunksRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得知识库文档块分页")
//    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks:query')")
    public CommonResult<PageResult<KnowledgeDocumentsChunksRespVO>> getKnowledgeDocumentsChunksPage(@Valid KnowledgeDocumentsChunksPageReqVO pageReqVO) {
        PageResult<KnowledgeDocumentsChunksDO> pageResult = knowledgeDocumentsChunksService.getKnowledgeDocumentsChunksPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KnowledgeDocumentsChunksRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出知识库文档块 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKnowledgeDocumentsChunksExcel(@Valid KnowledgeDocumentsChunksPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KnowledgeDocumentsChunksDO> list = knowledgeDocumentsChunksService.getKnowledgeDocumentsChunksPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "知识库文档块.xls", "数据", KnowledgeDocumentsChunksRespVO.class,
                        BeanUtils.toBean(list, KnowledgeDocumentsChunksRespVO.class));
    }

}