package com.ynfy.app.api.v1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynfy.app.api.v1.annoation.IgnoreAuth;
import com.ynfy.app.api.v1.entity.dto.ArticleDTO;
import com.ynfy.buss.article.entity.Article;
import com.ynfy.buss.article.service.IArticleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/v1/article")
@Slf4j
public class ApiAircleController extends ApiBaseController {
    @Autowired
    private IArticleService articleService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @IgnoreAuth
    @PostMapping(value = "/list")
    public Result<IPage<Article>> queryPageList(@RequestBody ArticleDTO dto) {
        Page<Article> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        LambdaQueryWrapper<Article> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getTitle())) {
            query.like(Article::getTitle, dto.getTitle());
        }
        query.eq(Article::getStatus, 1); //已发布
        IPage<Article> pageList = articleService.page(page, query);
        return Result.OK(pageList);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<Article> queryById(@RequestParam String id) {
        Article article = articleService.getById(id);
        if (article == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(article);
    }


}
