package com.lantone.message.web;

import com.lantone.common.api.CommonResult;
import com.lantone.common.dto.SendToTopicDTO;
import com.lantone.common.util.ListUtil;
import com.lantone.message.gateway.MqttGateway;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.List;

/**
 * @Description: 即时通讯后端示例
 * @author: rengb
 * @time: 2021/1/5 18:27
 */
@RestController
@Api(value = "即时通讯后端示例", tags = { "即时通讯后端示例" })
@RequestMapping("/backSample")
public class BackSampleController {

    @Autowired
    private MqttGateway mqttGateway;

    @PostMapping("/sendToDefaultTopic")
    @ApiOperation("向默认主题发送消息")
    public CommonResult sendToDefaultTopic(String payload) {
        mqttGateway.sendToMqtt(payload);
        return CommonResult.success(null);
    }

    @PostMapping("/sendToTopic")
    @ApiOperation("向指定主题发送消息")
    public CommonResult sendToTopic(@RequestBody List<SendToTopicDTO> sendToTopics) {
        if(ListUtil.isNotEmpty(sendToTopics)){
            sendToTopics.stream().forEach(sendToTopicDTO -> {
                mqttGateway.sendToMqtt(sendToTopicDTO.getMessage(), sendToTopicDTO.getTopic());
            });
        }
        return CommonResult.success(null);
    }

}