package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.MessageEntity;
import org.springblade.modules.system.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

import static org.springblade.core.secure.utils.AuthUtil.getUser;

@RestController
@RequestMapping("/blade_message")
@AllArgsConstructor
public class MessageController {

    @Autowired
    private MessageService messageService;


    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "消息提醒列表")
    @ApiOperationSupport(order = 1)
    public R<IPage<MessageEntity>> list(MessageEntity messageEntity, Query query){
        QueryWrapper queryWrapper = Condition.getQueryWrapper(messageEntity);
        queryWrapper.eq("status",0);
        queryWrapper.eq("user_id",getUser().getUserId());
        queryWrapper.orderByDesc("id");
        return  R.data(messageService.page(Condition.getPage(query), queryWrapper));


    }

    /**
     * 更新
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "更新 update", notes = "传入MessageEntity")
    public R update(@Valid @RequestBody MessageEntity messageEntity) {
        messageEntity.setUpdateTime(new Date());
        messageEntity.setStatus(1);
        messageService.updateById(messageEntity);
        return R.success("更新成功");
    }
}
