package com.ruoyi.web.controller.system;

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.domain.entity.ArticleClassify;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ss.article.domain.Article;
import com.ruoyi.ss.articleClassify.service.IArticleClassifyService;
import com.ruoyi.ss.article.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章Controller
 *
 * @author qiuzhenzhao
 * @date 2023-12-06
 */
@RestController
@RequestMapping("/admin/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService asArticleService;

    @Autowired
    private IArticleClassifyService asArticleClassifyService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(Article etArticle)
    {
        startPage();
        List<Article> list = asArticleService.selectAsArticleListByIsolate(etArticle);
        return getDataTable(list);
    }

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

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

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article etArticle)
    {
//        etArticle.setAgentId(getAgentId());
        return toAjax(asArticleService.insertAsArticle(etArticle));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article etArticle)
    {
        return toAjax(asArticleService.updateAsArticle(etArticle));
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(asArticleService.deleteAsArticleByArticleIds(articleIds));
    }

    /**
     * 获取分类树列表
     */
    @PreAuthorize("@ss.hasPermi('system:articleClassify:list')")
    @GetMapping("/classifyTree")
    public AjaxResult deptTree(ArticleClassify classify)
    {
        return success(asArticleClassifyService.selectClassifyTreeList(classify));
    }
}
