package com.jd.lightapplication.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jd.lightapplication.common.lang.R;
import com.jd.lightapplication.model.NoticeInfo;
import com.jd.lightapplication.model.dto.NoticeInfoQueryDto;
import com.jd.lightapplication.service.NoticeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;


@Slf4j
@RestController()
@RequestMapping("/api/notice")
@Api(tags = "公告管理")
public class NoticeInfoController {

    @Resource
    private NoticeInfoService noticeInfoService;

    @GetMapping("/list")
    @ApiOperation(value = "1、分页查询")
    public R queryPageBean(Page page, NoticeInfoQueryDto noticeInfoQueryDto) {
        LambdaQueryWrapper<NoticeInfo> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(noticeInfoQueryDto.getTitle())) {
            wrapper.like(NoticeInfo::getTitle, noticeInfoQueryDto.getTitle());
        }
        wrapper.orderByDesc(NoticeInfo::getCreateTime);
        return R.success(noticeInfoService.page(page, wrapper));
    }

    @PostMapping("/addBean")
    @ApiOperation(value = "2、新增")
    public R addBean(@RequestBody NoticeInfo noticeInfo) {
        noticeInfoService.save(noticeInfo.setCreateTime(DateUtil.now()));
        return R.success();
    }

    @PostMapping("/updateBean")
    @ApiOperation(value = "3、修改")
    public R updateBean(@RequestBody NoticeInfo noticeInfo) {
        noticeInfoService.updateById(noticeInfo.setUpdateTime(DateUtil.now()));
        return R.success();
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "4、删除")
    public R deleteById(long id) {
        noticeInfoService.removeById(id);
        return R.success();
    }

    @GetMapping("/getById")
    @ApiOperation(value = "5、根据ID查询详情")
    public R getById(Long id) {
        return R.success(noticeInfoService.getById(id));
    }

}