package com.ruoyi.web.controller.ss;

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.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ss.article.domain.SmArticle;
import com.ruoyi.ss.articleClassify.domain.SmArticleClassifyQuery;
import com.ruoyi.ss.article.domain.SmArticleQuery;
import com.ruoyi.ss.articleClassify.service.ISmArticleClassifyService;
import com.ruoyi.ss.article.service.ISmArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.util.UriEncoder;

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

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

    @Autowired
    private ISmArticleClassifyService asArticleClassifyService;

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

    /**
     * 查询文章列表ByIds
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/listByIds/{ids}")
    public AjaxResult listByIds(@PathVariable List<Long> ids)
    {
        SmArticleQuery query = new SmArticleQuery();
        query.setArticleIds(ids);
        List<SmArticle> list = asArticleService.selectSmArticleList(query);
        return success(list);
    }

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

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

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmArticle smArticle)
    {
        if (StringUtils.hasText(smArticle.getContent())) {
            smArticle.setContent(UriEncoder.decode(smArticle.getContent()));
        }
        return toAjax(asArticleService.insertSmArticle(smArticle));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmArticle smArticle)
    {
        if (StringUtils.hasText(smArticle.getContent())) {
            smArticle.setContent(UriEncoder.decode(smArticle.getContent()));
        }

        return toAjax(asArticleService.updateSmArticle(smArticle));
    }

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

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