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

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.knowledgedocuments.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgedocuments.KnowledgeDocumentsDO;
import cn.iocoder.yudao.module.llm.service.knowledgedocuments.KnowledgeDocumentsService;

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

    @Resource
    private KnowledgeDocumentsService knowledgeDocumentsService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得知识库文档分页")
    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents:query')")
    public CommonResult<PageResult<KnowledgeDocumentsRespVO>> getKnowledgeDocumentsPage(@Valid KnowledgeDocumentsPageReqVO pageReqVO) {
        PageResult<KnowledgeDocumentsDO> pageResult = knowledgeDocumentsService.getKnowledgeDocumentsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KnowledgeDocumentsRespVO.class));
    }

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

}