package com.ruoyi.frequency.news.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.frequency.news.entity.News;
import com.ruoyi.frequency.news.model.result.NewsCount;
import com.ruoyi.frequency.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 新闻Controller
 *
 * @author liwenlong
 * @date 2024-03-19
 */
@RestController
@RequestMapping("/frequency/news")
public class NewsController extends BaseController {
    @Autowired
    private NewsService newsService;

    /**
     * 查询新闻列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:news:list')")
    @PostMapping("/list")
    public TableDataInfo<News> list(@RequestBody News news) {
        return newsService.selectNewsList(news);
    }


    /**
     * 统计
     */
    @PostMapping("/statistics")
    public AjaxResult statistics(@RequestBody News news) {
        NewsCount count = newsService.statistics(news);
        return AjaxResult.success(count);
    }


    /**
     * 获取新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('frequency:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<News> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(newsService.selectNewsById(id));
    }

    /**
     * 新增新闻
     */
    @PreAuthorize("@ss.hasPermi('frequency:news:add')")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news) {
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改新闻
     */
    @PreAuthorize("@ss.hasPermi('frequency:news:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news) {
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('frequency:news:remove')")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}/{delFlag}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids, @PathVariable("delFlag") Integer delFlag) {
        return toAjax(newsService.deleteNewsByIds(ids, delFlag));
    }
}
