package com.orangesale.cn.controller;

import com.orangesale.cn.entity.OrangeMessage;
import com.orangesale.cn.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author zhijun
 * @Date 2019/12/13
 */
@RestController
@RequestMapping("/orange/message")
@CrossOrigin
public class OrangeMessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 推送消息
     *
     * @param message
     * @return
     */
    @RequestMapping(value = "/pushMessage", method = RequestMethod.POST)
    public Map<String, Object> insertMessage(@RequestBody OrangeMessage message) {
        Integer insertFlag = messageService.insertMessage(message);
        Map<String, Object> map = new HashMap<>();
        if (insertFlag > 0) {
            map.put("flag", true);
            map.put("msg", "success");
            return map;
        }
        map.put("flag", false);
        map.put("msg", "error");
        return map;
    }

    /**
     * 消费消息
     *
     * @return
     */
    @RequestMapping(value = "/consumeMessage", method = RequestMethod.GET)
    public Map<String, Object> consumeMessage() {
        OrangeMessage orangeMessage = messageService.consumeMessage();
        Map<String, Object> map = new HashMap<>();
        if (Objects.isNull(orangeMessage)) {
            map.put("flag", false);
            map.put("msg", "consume error");
            return map;
        }
        map.put("flag", true);
        map.put("data", orangeMessage);
        return map;
    }

    /**
     * 设置消息为已使用
     *
     * @return
     */
    @RequestMapping(value = "/updateMessage", method = RequestMethod.GET)
    public Map<String, Object> updateMessage() {
        Integer updateFlag = messageService.updateMessage();
        Map<String, Object> map = new HashMap<>();
        if (updateFlag > 0) {
            map.put("flag", true);
            map.put("msg", "success");
            return map;
        }
        map.put("flag", false);
        map.put("msg", "error");
        return map;
    }

}
