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.Notice;
import com.ruoyi.web.work.service.INoticeService;
import io.swagger.annotations.Api;
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 高伟森
 * @date 2024-04-18
 */
@RestController
@RequestMapping("/work/notice")
@Api( tags = "通知公告")
public class NoticeController extends BaseController {
    @Autowired
    private INoticeService noticeService;

    @GetMapping("/list")
    public TableDataInfo list(Notice notice){
        notice.setState(0);
        startPage();
        List<Notice> list = noticeService.selectList(notice);
        return getDataTable(list);
    }

    @GetMapping("/queryList")
    public AjaxResult queryList(){
        return AjaxResult.success(noticeService.homeList("企业通知"));
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id){
        return AjaxResult.success(noticeService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Notice notice){
        return noticeService.add(notice);
    }

    @PreAuthorize("@ss.hasPermi('work:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Notice notice){
        notice.setContents(notice.getContents().replace("/dev-api", "").replace("/prod-api", ""));
        return toAjax(noticeService.updateById(notice));
    }

    @PreAuthorize("@ss.hasPermi('work:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(noticeService.removeByIds(Arrays.asList(ids)));
    }
}
