package com.sqx.modules.message.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.message.entity.Notice;
import com.sqx.modules.message.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@Api(value = "公告管理", tags = {"公告管理"})
@RequestMapping(value = "/app/message")
public class NoticeController {
    private final NoticeService noticeService;

    public NoticeController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    @GetMapping("/selectNotice")
    @ApiOperation("获取公告列表")
    public Result selectNotice(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("banner",noticeService.getBaseMapper().selectList(new QueryWrapper<Notice>().eq("is_use",1).eq("type",0)));
        map.put("rule",noticeService.getBaseMapper().selectList(new QueryWrapper<Notice>().eq("is_use",1).eq("type",1)));
        map.put("notice",noticeService.getBaseMapper().selectList(new QueryWrapper<Notice>().eq("is_use",1).eq("type",2)));
        return  Result.success().put("data",map);
    }

    @PostMapping("/insertNotice")
    @ApiOperation("增加或修改公告")
    public  Result insertNotice(Notice notice){
        noticeService.saveOrUpdate(notice);
        return Result.success();
    }
    @PostMapping("/deleteNotice")
    @ApiOperation("增加或修改公告")
    public  Result deleteNotice(Long noticeId){
        Notice notice = noticeService.getById(noticeId);
        notice.setIsUse(0);
        noticeService.saveOrUpdate(notice);
        return Result.success();
    }
}
