package com.tourism.webadmin.app.website.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.tourism.common.core.annotation.DisableDataFilter;
import com.tourism.common.core.object.ResponseResult;
import com.tourism.common.core.object.TokenData;
import com.tourism.webadmin.app.website.dto.InteractionMessageDto;
import com.tourism.webadmin.app.website.dto.SystemMessageDTO;
import com.tourism.webadmin.back.model.TourImMessage;
import com.tourism.webadmin.back.model.constant.NoticeType;
import com.tourism.webadmin.back.service.TourImMessageService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "系统消息")
@RestController
@DisableDataFilter
@Slf4j
@RequestMapping("/website/systemMessageController")
public class SystemMessageController {

    @Autowired
    private  RabbitTemplate rabbitTemplate;
    @Autowired
    private TourImMessageService tourImMessageService;

    /**
     * 系统消息
     * @param params 消息体
     * @return {@link ResponseResult }<{@link Void }>
     */
    @PostMapping("/sendSysMessage")
    public ResponseResult<Void> sendMessage(@RequestBody SystemMessageDTO params) {
        TokenData tokenData = TokenData.takeFromRequest();
        if (tokenData!=null) params.setSendUserId(tokenData.getUserId().toString());
        rabbitTemplate.convertSendAndReceive("systemExchange", "systemRouter", JSONUtil.toJsonStr(params));
        return ResponseResult.success();
    }

    /**
     * 互动消息
     * @param params 消息体
     * @return {@link ResponseResult }<{@link Void }>
     */
    @PostMapping("/sendInteractionMessage")
    public ResponseResult<Void> sendInteractionMessage(@RequestBody InteractionMessageDto params) {
        TokenData tokenData = TokenData.takeFromRequest();
        if (tokenData!=null) params.setSendUserId(tokenData.getUserId().toString());
        rabbitTemplate.convertSendAndReceive("interactExchange", "interactRouter", JSONUtil.toJsonStr(params));
        return ResponseResult.success();
    }


    /**
     * 关注消息
     * @param params 消息体
     * @return {@link ResponseResult }<{@link Void }>
     */
    @PostMapping("/sendFollowMessage")
    public ResponseResult<Void> sendFollowMessage(@RequestBody SystemMessageDTO params) {
        rabbitTemplate.convertSendAndReceive("focusExchange", "focusRouter", JSONUtil.toJsonStr(params));
        return ResponseResult.success();
    }

    /**
     * 系统消息详情
     * @param messageId 消息体
     * @return {@link ResponseResult }<{@link Void }>
     */
    @GetMapping("/getSysMessageDetail")
    public ResponseResult<SystemMessageDTO> getSysMessageDetail(Long messageId) {
        TourImMessage tourImMessage = tourImMessageService.getById(messageId);
        if (tourImMessage!=null&&tourImMessage.getMessageContent()!=null){
            SystemMessageDTO bean = JSONUtil.toBean(tourImMessage.getMessageContent(), SystemMessageDTO.class);
            return ResponseResult.success(bean);
        }
       return null;
    }
}
