package com.ruoyi.web.controller.system;


import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.system.domain.PostNotice;
import com.ruoyi.system.service.IPostNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【公告】Controller
 *
 * @author ruoyi
 * @date 2023-01-14
 */
@Api(tags = "PostNoticeController",description = "公告功能页面")
@RestController
@RequestMapping("/system/notice")
public class PostNoticeController extends BaseController
{
    @Autowired
    private IPostNoticeService postNoticeService;

    /**
     * 查询公告列表
     */
    @ApiOperation("查询公告列表")
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostNotice postNotice)
    {
        startPage();
        List<PostNotice> list = postNoticeService.selectPostNoticeList(postNotice);
        return getDataTable(list);
    }

    /**
     * 搜索公告列表.
     * @param title
     * @param noticeTimeLeft
     * @param noticeTimeRight
     * @return
     */
    @ApiOperation("搜索公告列表")
    @PreAuthorize("@ss.hasPermi('system:notice:queryNotice')")
    @GetMapping("/queryNotice")
    public TableDataInfo list(@RequestParam(value="title",required = false)String title,
                              @RequestParam(value="noticeTimeLeft",required = false) Date noticeTimeLeft,
                              @RequestParam(value="noticeTimeRight",required = false) Date noticeTimeRight)
    {
        startPage();
        List<PostNotice> list= postNoticeService.selectList(title,noticeTimeLeft,noticeTimeRight);
        return getDataTable(list);
    }

    /**
     * 导出公告列表
     */
    @ApiOperation("导出公告列表")
    @PreAuthorize("@ss.hasPermi('system:notice:export')")
    @Log(title = "公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostNotice postNotice)
    {
        List<PostNotice> list = postNoticeService.selectPostNoticeList(postNotice);
        ExcelUtil<PostNotice> util = new ExcelUtil<PostNotice>(PostNotice.class);
        util.exportExcel(response, list, "公告数据");
    }

    /**
     * 获取公告详细信息
     */
    @ApiOperation("获取公告详细信息")
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId)
    {
        return success(postNoticeService.selectPostNoticeByNoticeId(noticeId));
    }

    /**
     * 单独获取公告详细信息
     * @param noticeId
     * @return
     */
    @ApiOperation("单独获取公告详细信息")
    @PreAuthorize("@ss.hasPermi('system:notice:queryDetail')")
    @GetMapping(value = "/{/detail/noticeId}")
    public AjaxResult getDetail(@PathVariable("noticeId")Long noticeId){
        return success(postNoticeService.selectDetailByNoticeId(noticeId));
    }
    /**
     * 新增公告
     */
    @ApiOperation("新增公告")
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostNotice postNotice)
    {
        postNotice.setCreateBy(getUsername());
        postNotice.setCreateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
        postNotice.setUpdateBy(getUsername());
        postNotice.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
        return toAjax(postNoticeService.insertPostNotice(postNotice));
    }

    /**
     * 修改公告
     */
    @ApiOperation("修改公告")
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostNotice postNotice)
    {
        postNotice.setUpdateBy(getUsername());
        postNotice.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
        return toAjax(postNoticeService.updatePostNotice(postNotice));
    }

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


}
