package com.ruoyi.post.frequency.post.controller;

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.poi.ExcelUtil;
import com.ruoyi.post.frequency.post.entity.Post;
import com.ruoyi.post.frequency.post.model.result.PostCount;
import com.ruoyi.post.frequency.post.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帖子Controller
 *
 * @author liwenlong
 * @date 2023-10-09
 */
@RestController
@RequestMapping("/frequency/post")
public class PostController extends BaseController
{
    @Autowired
    private PostService postService;

    /**
     * 查询帖子列表
     */
//    @PreAuthorize("@ss.hasPermi('frequency:post:list')")
    @PostMapping("/list")
    public TableDataInfo<Post> list(@RequestBody Post post)
    {
        startPage();
        List<Post> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    /**
     * 统计
     */
    @PostMapping("/statistics")
    public AjaxResult statistics(@RequestBody Post post)
    {
        PostCount count = postService.statistics(post);
        return AjaxResult.success(count);
    }

    /**
     * 导出帖子列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:post:export')")
    @Log(title = "帖子", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Post post)
    {
        List<Post> list = postService.selectPostList(post);
        ExcelUtil<Post> util = new ExcelUtil<Post>(Post.class);
        return util.exportExcel(list, "帖子数据");
    }

    /**
     * 获取帖子详细信息
     */
//    @PreAuthorize("@ss.hasPermi('frequency:post:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<Post> getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(postService.selectPostById(id));
    }

    /**
     * 新增帖子
     */
    @PreAuthorize("@ss.hasPermi('frequency:post:add')")
    @Log(title = "帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Post post)
    {
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改帖子
     */
    @PreAuthorize("@ss.hasPermi('frequency:post:edit')")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Post post)
    {
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除帖子
     */
//    @PreAuthorize("@ss.hasPermi('frequency:post:remove')")
    @Log(title = "帖子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(postService.deletePostByIds(ids));
    }

    /**
     * 帖子置顶/取消置顶
     */
    @PostMapping("/topping/{id}")
    public AjaxResult topping(@PathVariable Long id)
    {
        return postService.topping(id);
    }
}
