package com.bonus.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.InnerAuth;
import com.bonus.common.security.annotation.RequiresPermissionsOrInnerAuth;
import com.bonus.system.api.domain.SysNotice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.service.ISysNoticeService;

/**
 * 公告信息操作处理
 * 
 * @author bonus
 */
@RestController
@RequestMapping("/notice")
@Slf4j
public class SysNoticeController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:notice:list"))
    @GetMapping("/list")
    @SysLog(title = "通知公告", businessType = OperaType.QUERY,logType = 0,module = "系统管理->通知公告")
    public TableDataInfo list(SysNotice notice) {
        try{
            startPage();
            List<SysNotice> list = noticeService.selectNoticeList(notice);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:notice:query"))
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId) {
        try{
            return success(noticeService.selectNoticeById(noticeId));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");

    }

    /**
     * 新增通知公告
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:notice:add"))
    @PostMapping
    @SysLog(title = "通知公告", businessType = OperaType.INSERT,logType = 0,module = "系统管理->通知公告")
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        try{
            notice.setCreateBy(SecurityUtils.getUsername());
            return toAjax(noticeService.insertNotice(notice));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");


    }

    /**
     * 修改通知公告
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:notice:edit"))
   @PostMapping("/edit")
    @SysLog(title = "通知公告", businessType = OperaType.UPDATE,logType = 0,module = "系统管理->通知公告")
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        try{
            notice.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(noticeService.updateNotice(notice));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");

    }

    /**
     * 删除通知公告
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:notice:remove"))
    @PostMapping("/delete/{noticeIds}")
    @SysLog(title = "通知公告", businessType = OperaType.DELETE,logType = 0,module = "系统管理->通知公告")
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        try{
            return toAjax(noticeService.deleteNoticeByIds(noticeIds));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");

    }
}
