package com.ruoyi.web.work.controller;

import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.Notice;
import com.ruoyi.web.work.service.INoticeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通知公告
 * @author 高伟森
 * @date 2024-04-18
 */
@RestController
@RequestMapping("/work/notice")
@Api( tags = "通知公告")
public class NoticeController extends BaseController {
    @Autowired
    private INoticeService noticeService;

    @PreAuthorize("@ss.hasPermi('work:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(Notice notice){
        startPage();
        List<Notice> list = noticeService.selectList(notice);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:notice:query')")
    @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(@RequestBody Notice notice){
        List<Notice> noticeList = noticeService.selectList(new Notice().setTitle(notice.getTitle()));
        if(notice.getTitle().length() > 20){
            return AjaxResult.error("标题长度大于20");
        }
        if (!noticeList.isEmpty()){
            return AjaxResult.error("该标题已存在!");
        }
        return toAjax(noticeService.save(notice));
    }

    @PreAuthorize("@ss.hasPermi('work:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Notice notice){
        if(notice.getTitle().length() > 20){
            return AjaxResult.error("标题长度大于20");
        }
        List<Notice> noticeList = noticeService.selectisEmpty(new Notice().setTitle(notice.getTitle()));
        noticeList.remove(noticeService.getById(notice.getId()));
        if (!noticeList.isEmpty()){
            return AjaxResult.error("该标题已存在!");
        }
        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)));
    }
}
