package com.bicloud.controller;

import com.bicloud.common.result.PageResult;
import com.bicloud.common.result.Result;
import com.bicloud.pojo.dto.LiteratureCreateDto;
import com.bicloud.pojo.dto.LiteratureDto;
import com.bicloud.pojo.dto.LiteraturePageQueryDto;
import com.bicloud.pojo.vo.LiteratureListVo;
import com.bicloud.service.LiteratureService;
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.*;

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

    @Autowired
    private LiteratureService literatureService;

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

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

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