package com.qiuguo.iot.user.api.controller.mq;

import com.qiuguo.iot.data.constants.YunxiRabbitConst;
import com.qiuguo.iot.data.entity.user.UserHomeEntity;
import com.qiuguo.iot.data.service.mq.MqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author simon
 * @date 2023/9/25
 * @description
 **/
@RestController
@Slf4j
@RequestMapping("/mq")
public class MqController {
   @Resource
   private MqService mqService;

   @GetMapping("/sendMsg")
   public Mono<Boolean> sendMsg() {
      UserHomeEntity userHomeEntity = new UserHomeEntity();
      userHomeEntity.setUserId(1L);
      userHomeEntity.setHomeName("小米的家");
      userHomeEntity.setUserId(System.currentTimeMillis());

      Mono<Boolean> booleanMono = mqService.sendMessageWithConfirmation(YunxiRabbitConst.EXCHANGE_YUNXI_EVENT, YunxiRabbitConst.ROUTE_KEY_YUNXI, userHomeEntity);
      return booleanMono.flatMap(res -> {
         if (res.equals(Boolean.FALSE)) {
            return Mono.error(new RuntimeException("消息发送失败"));
         } else {
            return Mono.just(Boolean.TRUE);
         }
      });
   }
}
