package com.bicloud.controller;

import com.bicloud.common.result.PageResult;
import com.bicloud.common.result.Result;
import com.bicloud.pojo.dto.DocumentCreateDto;
import com.bicloud.pojo.dto.DocumentPageQueryDto;
import com.bicloud.pojo.vo.DocumentTypeVo;
import com.bicloud.pojo.vo.DocumentListVo;
import com.bicloud.service.DocumentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/document")
@Tag(name = "文献管理", description = "文献相关接口")
@Slf4j
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    // 新增文献数据
    @PostMapping("/add")
    @Operation(summary = "新增文献")
    public Result<String> addDocument(@RequestBody DocumentCreateDto dto) {
        documentService.addDocument(dto);
        return Result.success("新增文献成功");
    }

    // 删除文献
    @DeleteMapping("/delete/{id}") // 通过路径变量接收文献ID
    @Operation(summary = "删除文献")
    public Result<String> deleteDocument(@PathVariable Long id) { // @PathVariable绑定路径中的id参数
        try {
            documentService.deleteDocument(id);
            return Result.success("删除文献成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除文献失败：" + e.getMessage());
        }
    }

    //分页查询
    @Operation(summary = "分页查询文献")
    @GetMapping("/page")
    public Result<PageResult<DocumentListVo>> page(DocumentPageQueryDto queryDTO) {
        PageResult<DocumentListVo> result = documentService.pageQuery(queryDTO);
        return Result.success(result);
    }

    // 文献分类下拉框（document_type）
    @GetMapping("/types")
    @Operation(summary = "获取文献分类下拉列表")
    public Result<List<DocumentTypeVo>> types() {
        return Result.success(documentService.listTypes());
    }

}
