package com.bs.web.controller.system;

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

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.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.system.domain.SysNotice;
import com.bs.system.service.ISysNoticeService;
import javax.annotation.Resource;

/**
 * 通知公告Controller
 *
 * @author bs
 * @date 2023-08-06
 */
@Api(tags = "通知公告")
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {
    @Resource
    private ISysNoticeService sysNoticeService;

    /**
     * 分页查询通知公告列表
     */
    @ApiOperation("分页查询通知公告列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(SysNotice sysNotice) {
        startPage();
        LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,sysNotice);
        List<SysNotice> list = sysNoticeService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询通知公告列表
     */
    @ApiOperation("查询通知公告列表")
    @GetMapping("/list")
    public AjaxResult list(SysNotice sysNotice) {
        LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,sysNotice);
        List<SysNotice> list = sysNoticeService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出通知公告列表
     */
    @ApiOperation("导出通知公告列表")
    @Log(title = "通知公告导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNotice sysNotice) {
        LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,sysNotice);
        List<SysNotice> list = sysNoticeService.list(queryWrapper);
        ExcelUtil<SysNotice> util = new ExcelUtil<SysNotice>(SysNotice. class);
        util.exportExcel(response, list, "通知公告数据");
    }

    /**
     * 获取通知公告详细信息
     */
    @ApiOperation("获取通知公告详细信息")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId) {
        return success(sysNoticeService.getById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @ApiOperation("新增通知公告")
    @Log(title = "通知公告新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNotice sysNotice) {
        return toAjax(sysNoticeService.save(sysNotice));
    }

    /**
     * 修改通知公告
     */
    @ApiOperation("修改通知公告")
    @Log(title = "通知公告修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNotice sysNotice) {
        return toAjax(sysNoticeService.updateById(sysNotice));
    }

    /**
     * 删除通知公告
     */
    @ApiOperation("删除通知公告")
    @Log(title = "通知公告删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable List<Long> noticeIds) {
        return toAjax(sysNoticeService.removeBatchByIds(noticeIds));
    }

    /**
     * 条件设置
     */
    private void condition (LambdaQueryWrapper<SysNotice> queryWrapper,SysNotice sysNotice){


        //公告标题
        if(Validator.isNotEmpty(sysNotice.getNoticeTitle())){
            queryWrapper.eq(SysNotice::getNoticeTitle,sysNotice.getNoticeTitle());
        }

        //公告类型sys_notice_type字典（1通知 2公告）
        if(Validator.isNotEmpty(sysNotice.getNoticeType())){
            queryWrapper.eq(SysNotice::getNoticeType,sysNotice.getNoticeType());
        }

        //公告内容
        if(Validator.isNotEmpty(sysNotice.getNoticeContent())){
            queryWrapper.eq(SysNotice::getNoticeContent,sysNotice.getNoticeContent());
        }

        //公告状态（0正常 1关闭）
        if(Validator.isNotEmpty(sysNotice.getStatus())){
            queryWrapper.eq(SysNotice::getStatus,sysNotice.getStatus());
        }

        //阅读状态（0未阅读 1已阅读）
        if(Validator.isNotEmpty(sysNotice.getReadStatus())){
            queryWrapper.eq(SysNotice::getReadStatus,sysNotice.getReadStatus());
        }

        //查看人
        if(sysNotice.getReadUser()!=null){
            queryWrapper.eq(SysNotice::getReadUser,sysNotice.getReadUser());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(sysNotice.getDelFlag())){
            queryWrapper.eq(SysNotice::getDelFlag,sysNotice.getDelFlag());
        }


        //创建人
        if(sysNotice.getCreateBy()!=null){
            queryWrapper.eq(SysNotice::getCreateBy,sysNotice.getCreateBy());
        }

    }

}
