package com.zhgd.xmgl.modules.basicdata.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.dto.NoticeDto;
import com.zhgd.xmgl.modules.basicdata.entity.Notice;
import com.zhgd.xmgl.modules.basicdata.service.INoticeService;
import com.zhgd.xmgl.modules.basicdata.vo.NoticeVo;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * @Title: Controller
 * @Description: 通知公告表
 * @author： pengj
 * @date： 2023-03-02
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/notice")
@Slf4j
@Api(tags = "通知公告管理")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "分页查询", operDesc = "分页列表查询《我发布》的通知公告")
    @ApiOperation(value = " 分页列表查询《我发布》的通知公告", notes = "分页列表查询《我发布》的通知公告", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "通知标题", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "通知内容", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/fromMePage")
    public Result<IPage<Notice>> fromMePageList(@RequestBody Map<String, Object> map) {
        LambdaQueryWrapper<Notice> wrapper = Wrappers.<Notice>lambdaQuery();
        String title = MapUtils.getString(map, "title");
        String content = MapUtils.getString(map, "content");
        Page<Notice> page = PageUtil.getPage(map);
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(Notice::getTitle, title);
        }
        if (StringUtils.isNotBlank(content)) {
            wrapper.like(Notice::getContent, content);
        }
        wrapper.eq(Notice::getCreateBy, SecurityUtil.getUser().getSn());
        IPage<Notice> pageList = noticeService.page(page, wrapper);
        return Result.success(pageList);
    }

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "分页查询", operDesc = "分页列表查询《我接收》的通知公告")
    @ApiOperation(value = " 分页列表查询《我接收》的通知公告", notes = "分页列表查询《我接收》的通知公告", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "通知标题", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "通知内容", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/toMePage")
    public Result<IPage<Notice>> toMePageList(@RequestBody Map<String, Object> map) {
        IPage<Notice> pageList = noticeService.toMePageList(map);
        return Result.success(pageList);
    }

    /**
     * 通过id查询
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "查询", operDesc = "通过id查询通知公告表信息")
    @ApiOperation(value = "通过id查询通知公告表信息", notes = "通过id查询通知公告表信息" , httpMethod="POST")
    @ApiImplicitParam(name = "noticeId", value = "通知公告表ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<NoticeDto> queryById(@RequestBody Notice noticeEntity) {
        Result<NoticeDto> result = new Result<NoticeDto>();
        Notice notice = noticeService.getById(noticeEntity.getNoticeId());
        if(notice==null) {
            result.error500("未找到对应实体");
        }else {
            result.setResult(noticeService.getDetail(notice));
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 添加
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "新增", operDesc = "新建通知")
    @ApiOperation(value = " 新建通知", notes = "新建通知", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody NoticeVo noticeVo) {
        return Result.success(noticeService.saveInfo(noticeVo));
    }

    /**
     * 编辑
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "修改", operDesc = "编辑通知公告表信息")
    @ApiOperation(value = "编辑通知公告表信息", notes = "编辑通知公告表信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<Notice> edit(@RequestBody NoticeVo noticeVo) {
        Result<Notice> result = new Result<Notice>();
        Notice noticeEntity = noticeService.getById(noticeVo.getNoticeId());
        if (noticeEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = noticeService.updateInfo(noticeVo);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 通过id删除
     *
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "删除", operDesc = "删除通知公告表信息")
    @ApiOperation(value = "删除通知公告表信息", notes = "删除通知公告表信息", httpMethod = "POST")
    @ApiImplicitParam(name = "noticeId", value = "通知公告表ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/delete")
    public Result<Notice> delete(@RequestBody Notice noticeEntity) {
        Result<Notice> result = new Result<Notice>();
        Notice notice = noticeService.getById(noticeEntity.getNoticeId());
        if (notice == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = noticeService.delInfo(noticeEntity);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * 阅读通知公告
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "修改", operDesc = "阅读通知公告")
    @ApiOperation(value = "阅读通知公告", notes = "阅读通知公告", httpMethod = "POST")
    @ApiImplicitParam(name = "noticeId", value = "通知公告表ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/read")
    public Result<Notice> read(@RequestBody Notice notice) {
        Result<Notice> result = new Result<Notice>();
        Notice noticeEntity = noticeService.getById(notice.getNoticeId());
        if (noticeEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = noticeService.read(notice);
            if (ok) {
                result.success("阅读成功!");
            }
            result.error500("操作失败!");
        }
        return result;
    }
}
