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

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.knowledgedocumentschunksvectorized.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgedocumentschunksvectorized.KnowledgeDocumentsChunksVectorizedDO;
import cn.iocoder.yudao.module.llm.service.knowledgedocumentschunksvectorized.KnowledgeDocumentsChunksVectorizedService;

@Tag(name = "管理后台 - 向量化存储")
@RestController
@RequestMapping("/llm/knowledge-documents-chunks-vectorized")
@Validated
public class KnowledgeDocumentsChunksVectorizedController {

    @Resource
    private KnowledgeDocumentsChunksVectorizedService knowledgeDocumentsChunksVectorizedService;

    @PostMapping("/create")
    @Operation(summary = "创建向量化存储")
    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks-vectorized:create')")
    public CommonResult<Long> createKnowledgeDocumentsChunksVectorized(@Valid @RequestBody KnowledgeDocumentsChunksVectorizedSaveReqVO createReqVO) {
        return success(knowledgeDocumentsChunksVectorizedService.createKnowledgeDocumentsChunksVectorized(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新向量化存储")
    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks-vectorized:update')")
    public CommonResult<Boolean> updateKnowledgeDocumentsChunksVectorized(@Valid @RequestBody KnowledgeDocumentsChunksVectorizedSaveReqVO updateReqVO) {
        knowledgeDocumentsChunksVectorizedService.updateKnowledgeDocumentsChunksVectorized(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除向量化存储")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks-vectorized:delete')")
    public CommonResult<Boolean> deleteKnowledgeDocumentsChunksVectorized(@RequestParam("id") Long id) {
        knowledgeDocumentsChunksVectorizedService.deleteKnowledgeDocumentsChunksVectorized(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得向量化存储")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks-vectorized:query')")
    public CommonResult<KnowledgeDocumentsChunksVectorizedRespVO> getKnowledgeDocumentsChunksVectorized(@RequestParam("id") Long id) {
        KnowledgeDocumentsChunksVectorizedDO knowledgeDocumentsChunksVectorized = knowledgeDocumentsChunksVectorizedService.getKnowledgeDocumentsChunksVectorized(id);
        return success(BeanUtils.toBean(knowledgeDocumentsChunksVectorized, KnowledgeDocumentsChunksVectorizedRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得向量化存储分页")
    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks-vectorized:query')")
    public CommonResult<PageResult<KnowledgeDocumentsChunksVectorizedRespVO>> getKnowledgeDocumentsChunksVectorizedPage(@Valid KnowledgeDocumentsChunksVectorizedPageReqVO pageReqVO) {
        PageResult<KnowledgeDocumentsChunksVectorizedDO> pageResult = knowledgeDocumentsChunksVectorizedService.getKnowledgeDocumentsChunksVectorizedPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KnowledgeDocumentsChunksVectorizedRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出向量化存储 Excel")
    @PreAuthorize("@ss.hasPermission('llm:knowledge-documents-chunks-vectorized:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKnowledgeDocumentsChunksVectorizedExcel(@Valid KnowledgeDocumentsChunksVectorizedPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KnowledgeDocumentsChunksVectorizedDO> list = knowledgeDocumentsChunksVectorizedService.getKnowledgeDocumentsChunksVectorizedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "向量化存储.xls", "数据", KnowledgeDocumentsChunksVectorizedRespVO.class,
                        BeanUtils.toBean(list, KnowledgeDocumentsChunksVectorizedRespVO.class));
    }

}