package com.ruoyi.web.controller.post;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
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.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.post.domain.PostBulletin;
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 com.ruoyi.post.service.IPostBulletinService;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author HuiANan
 * @Date 2023/1/14 15:40
 * @Exegesis:
 */

@RestController
@RequestMapping("/post/bulletin")
public class PostBulletinController extends BaseController
{
    @Autowired
    private IPostBulletinService postBulletinService;

    /**
     * 查询通知公告;
     */
    @PreAuthorize("@ss.hasPermi('system:bulletin:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostBulletin postBulletin)
    {
        startPage();
        List<PostBulletin> list = postBulletinService.selectPostBulletinList(postBulletin);
        return getDataTable(list);
    }

    /**
     * 导出通知公告;
     */
    @PreAuthorize("@ss.hasPermi('system:bulletin:export')")
    @Log(title = "导出通知公告;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostBulletin postBulletin)
    {
        List<PostBulletin> list = postBulletinService.selectPostBulletinList(postBulletin);
        ExcelUtil<PostBulletin> util = new ExcelUtil<PostBulletin>(PostBulletin.class);
        util.exportExcel(response, list, "通知公告; ");
    }

    /**
     * 获取通知公告;
     */
    @PreAuthorize("@ss.hasPermi('system:bulletin:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Integer noticeId)
    {
        return success(postBulletinService.selectPostBulletinByNoticeId(noticeId));
    }

    @PreAuthorize("@ss.hasPermi('system:bulletin:add')")
    @Log(title = "新增通知公告; ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody PostBulletin postBulletin)
    {
        if(UserConstants.NOT_UNIQUE.equals(postBulletinService.checkTitle(postBulletin)))
        {
            return AjaxResult.error("新增公告"+postBulletin.getNoticeTitle()+"失败，公告标题已经存在");
        }
        else if(UserConstants.NOT_UNIQUE.equals(postBulletinService.checkContent(postBulletin)))
        {
            return AjaxResult.error("新增公告"+postBulletin.getNoticeContent()+"失败，公告内容已经存在");
        }
        postBulletin.setCreateBy(getUsername());
        return toAjax(postBulletinService.insertPostBulletin(postBulletin));
    }

    /**
     * 修改通知公告;
     */
    @PreAuthorize("@ss.hasPermi('system:bulletin:edit')")
    @Log(title = "修改通知公告; ", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PostBulletin postBulletin)
    {
        //判断用户数据是否存在

        if(UserConstants.NOT_UNIQUE.equals(postBulletinService.checkTitle(postBulletin)))
        {
            return AjaxResult.error("修改公告"+postBulletin.getNoticeTitle()+"失败，公告标题已经存在");
        }
        else if(StringUtils.isNotEmpty(postBulletin.getNoticeContent())&&UserConstants.NOT_UNIQUE.equals(postBulletinService.checkContent(postBulletin)))
        {
            return AjaxResult.error("修改公告"+postBulletin.getNoticeContent()+"失败，公告内容已经存在");
        }
        postBulletin.setCreateBy(getUsername());
        return toAjax(postBulletinService.updatePostBulletin(postBulletin));
    }

    /**
     * 删除通知公告;
     */
    @PreAuthorize("@ss.hasPermi('system:bulletin:remove')")
    @Log(title = "删除通知公告; ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Integer[] noticeIds)
    {
        return toAjax(postBulletinService.deletePostBulletinByNoticeIds(noticeIds));
    }

    /**
     * 查看详情
     */
    @PreAuthorize("@ss.hasPermi('system:bulletin:query')")
    @GetMapping(value = "/details/{noticeId}")
    public AjaxResult ViewDetails(@PathVariable("noticeId") Integer noticeId)
    {
        return success(postBulletinService.selectViewDetail(noticeId));
    }
}