package com.ruoyi.web.controller.system;

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.system.domain.PoNotice;
import com.ruoyi.system.service.IPoNoticeService;
import io.swagger.annotations.ApiOperation;
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.List;

/**
 * 公告 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/poNotice")
public class PoNoticeController extends BaseController {
    @Autowired
    private IPoNoticeService poNoticeService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    @ApiOperation("获取通知公告")
    public TableDataInfo list(PoNotice poNotice) {
        startPage();
        List<PoNotice> list = poNoticeService.selectPoNoticeList(poNotice);
        return getDataTable(list);
    }

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/export")
    @ApiOperation("导出通知公告")
    public AjaxResult export(PoNotice poNotice) {
        List<PoNotice> list = poNoticeService.selectPoNoticeList(poNotice);
        ExcelUtil<PoNotice> util = new ExcelUtil<>(PoNotice.class);
        return util.exportExcel(list, "通知公告列表");
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{poNoticeId}")
    @ApiOperation("获取通知公告内容")
    public AjaxResult getInfo(@PathVariable("poNoticeId") Long poNoticeId) {
        return success(poNoticeService.selectPoNoticeById(poNoticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @ApiOperation("添加通知公告")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PoNotice poNotice) {
        List<PoNotice> poNoticeList = poNoticeService.selectPoNoticeList(poNotice);
        if (poNoticeList == null || poNoticeList.isEmpty())
            return toAjax(poNoticeService.insertPoNotice(poNotice));
        return error("重复的公告");
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @ApiOperation("编辑通知公告")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PoNotice poNotice) {
        List<PoNotice> poNotice1 = poNoticeService.selectPoNoticeList(poNotice);
        if (poNotice1 == null || poNotice1.isEmpty())
            return error("要修改的公告不存在");
        poNotice.setUpdateBy(getUsername());
        poNotice.setPublisherId(getUserId());
        return toAjax(poNoticeService.updatePoNotice(poNotice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @ApiOperation("删除通知公告")
    @DeleteMapping("/{poNoticeIds}")
    public AjaxResult remove(@PathVariable Long[] poNoticeIds) {
        return toAjax(poNoticeService.deletePoNoticeByIds(poNoticeIds));
    }
}
