package com.ruoyi.web.work.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.web.work.domain.News;
import com.ruoyi.web.work.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 新闻资讯
 *
 * @author lsw
 * @date 2024-05-31
 */
@RestController
@RequestMapping("/work/news")
public class NewsController extends BaseController {
    @Autowired
    private INewsService newsService;

    @PreAuthorize("@ss.hasPermi('work:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news) {
        startPage();
        List<News> list = newsService.selectList(news);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:news:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") String id) {
        return AjaxResult.success(newsService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:news:add')")
    @Log(title = "新闻资讯", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody News news) {
        news.setContents(news.getContents().replace("/dev-api", "").replace("/prod-api", ""));
        return toAjax(newsService.save(news));
    }

    @PreAuthorize("@ss.hasPermi('work:news:edit')")
    @Log(title = "新闻资讯", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody News news) {
        news.setContents(news.getContents().replace("/dev-api", "").replace("/prod-api", ""));
        return toAjax(newsService.updateById(news));
    }

    @PreAuthorize("@ss.hasPermi('work:news:remove')")
    @Log(title = "新闻资讯", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(newsService.removeByIds(Arrays.asList(ids)));
    }
}
