package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.article.domain.Article;
import com.ruoyi.bst.article.domain.ArticleQuery;
import com.ruoyi.bst.article.domain.ArticleVO;
import com.ruoyi.bst.article.service.ArticleService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 文章Controller
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/bst/article")
public class ArticleController extends BaseController
{
    @Autowired
    private ArticleService articleService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('bst:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArticleQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<ArticleVO> list = articleService.selectArticleList(query);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('bst:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArticleQuery query)
    {
        List<ArticleVO> list = articleService.selectArticleList(query);
        ExcelUtil<ArticleVO> util = new ExcelUtil<ArticleVO>(ArticleVO.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(articleService.selectArticleById(id));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('bst:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.ARTICLE)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('bst:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.ARTICLE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article)
    {
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('bst:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE, bizIdName = "arg0", bizType = LogBizType.ARTICLE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(articleService.deleteArticleByIds(ids));
    }
}
