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.system.domain.PoNotice;
import com.ruoyi.system.service.IPoNoticeService;
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")
    public TableDataInfo list(PoNotice poNotice) {
        startPage();
        List<PoNotice> list = poNoticeService.selectPoNoticeList(poNotice);
        return getDataTable(list);
    }

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

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PoNotice poNotice) {
        System.out.println(poNotice);
        return toAjax(poNoticeService.insertPoNotice(poNotice));
    }

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

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