
package com.iformall.controller;

import com.alibaba.fastjson.JSON;
import com.iformall.service.UserService;
import com.iformall.util.Constance;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;


/**
 * @author Stormeye
 */
@RestController
@RequestMapping("/websocket")
public class SendController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Autowired
    private UserService userService;


    /**
     * ADMIN通知消息
     */
    @GetMapping("/noticeA/{tenantId}")
    public void noticea(@PathVariable("tenantId") String tenantId, @RequestParam String message) {
        messagingTemplate.convertAndSend("/topic/"+tenantId+"A", JSON.toJSONString(message));
    }

    /**
     * B端通知消息
     */
    @GetMapping("/noticeB/{tenantId}")
    public void noticeb(@PathVariable("tenantId") String tenantId, @RequestParam String message) {
        messagingTemplate.convertAndSend("/topic/"+tenantId+"B", JSON.toJSONString(message));
    }

    /**
     * 通知消息
     */
    @GetMapping("/noticeC/{tenantId}")
    public void noticec(@PathVariable("tenantId") String tenantId, @RequestParam String message) {
        messagingTemplate.convertAndSend("/topic/"+tenantId+"C", JSON.toJSONString(message));
    }

    /**
     * 具体用户消息
     */
    @GetMapping("/user/{name}")
    public void user(@PathVariable("name") String name, @RequestParam String message) {
        // 方式1
        messagingTemplate.convertAndSendToUser(name, "/message", JSON.toJSONString(message));
        // 方式2
        String userId = userService.getSessionId(name);
        amqpTemplate.convertAndSend("", Constance.queue_pre+userId, message);
    }


}
