package com.inspect.system.controller;

import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.domain.SysNotice;
import com.inspect.system.service.ISysNoticeService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/notice"})
public class SysNoticeController extends BaseController {
    private final ISysNoticeService noticeService;

    public SysNoticeController(ISysNoticeService noticeService) {
        this.noticeService = noticeService;
    }

    @RequiresPermissions({"system:notice:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(SysNotice notice) {
        this.startPage();
        List<SysNotice> list = this.noticeService.selectNoticeList(notice);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"system:notice:query"})
    @GetMapping({"/{noticeId}"})
    public AjaxResult getInfo(@PathVariable Long noticeId) {
        return AjaxResult.success(this.noticeService.selectNoticeById(noticeId));
    }

    @RequiresPermissions({"system:notice:add"})
    @Log(
            title = "通知公告",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(SecurityUtils.getUsername());
        return this.toAjax(this.noticeService.insertNotice(notice));
    }

    @RequiresPermissions({"system:notice:edit"})
    @Log(
            title = "通知公告",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(SecurityUtils.getUsername());
        return this.toAjax(this.noticeService.updateNotice(notice));
    }

    @RequiresPermissions({"system:notice:remove"})
    @Log(
            title = "通知公告",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{noticeIds}"})
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        return this.toAjax(this.noticeService.deleteNoticeByIds(noticeIds));
    }
}
