package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.microsvc.toolkit.middleware.rtc.RTCRoomPluginContext;
import com.microsvc.toolkit.middleware.rtc.RTCRoomPluginService;
import com.microsvc.toolkit.middleware.rtc.enums.EMemberAction;
import com.microsvc.toolkit.middleware.rtc.impl.TencentCloudRTCPlugin;
import com.microsvc.toolkit.middleware.rtc.message.RTCRoomMessage;
import com.yonge.cooleshow.biz.dal.dto.*;
import com.yonge.cooleshow.biz.dal.entity.CourseSchedule;
import com.yonge.cooleshow.biz.dal.entity.ImNetworkRoom;
import com.yonge.cooleshow.biz.dal.entity.ImNetworkRoomMember;
import com.yonge.cooleshow.biz.dal.entity.ImNetworkRoomMemberStatus;
import com.yonge.cooleshow.biz.dal.enums.*;
import com.yonge.cooleshow.biz.dal.service.*;
import com.yonge.cooleshow.biz.dal.support.IMHelper;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * 网络教室成员(ImNetworkRoomMember)表服务实现类
 *
 * @author zx
 * @since 2022-03-30 16:36:38
 */
@Service("imService")
public class ImServiceImpl implements ImService {

    private final static Logger log = LoggerFactory.getLogger(ImServiceImpl.class);

    @Autowired
    private RTCRoomPluginContext rtcRoomPluginContext;

    @Autowired
    private ImNetworkRoomMemberStatusService imNetworkRoomMemberStatusService;

    @Autowired
    private IMHelper imHelper;

    @Autowired
    private ImNetworkRoomMemberService imNetworkRoomMemberService;

    @Autowired
    private ImGroupService imGroupService;

    @Autowired
    private ImNetworkRoomService imNetworkRoomService;


    @Override
    public void publishMemberChangedMessage(ImNetworkRoomMember roomMember,String serviceProvider) throws Exception {
        //发送人员变动消息
        // 获取RTC服务提供方
        if (serviceProvider.equals(TencentCloudRTCPlugin.PLUGIN_NAME) || StringUtils.isEmpty(serviceProvider)) {
            RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(serviceProvider);
            // 腾讯云RTC服务
            RTCRoomMessage.MessageContent.MessageContentBuilder action = RTCRoomMessage.MessageContent.builder()
                    .action(EMemberAction.JOIN.getValue());

            action.handUpOn(roomMember.isHandFlag())
                    .microphone(roomMember.isMicFlag())
                    .timestamp(new Date().getTime())
                    .camera(roomMember.isCameraFlag())
                    .role(roomMember.getRole())
                    .sendUserInfo(this.getSendUser(roomMember));

            RTCRoomMessage roomMessage = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.MEMBER_CHANGE_MESSAGE)
                    .fromUserId(roomMember.getUserId())
                    .content(action.build())
                    .toChatRoomId(roomMember.getRoomId())
                    .isPersisted(1)
                    .isIncludeSender(1)
                    .build();

            pluginService.sendChatRoomMessage(roomMessage);
        } else {
            ImNetworkRoomMemberChangedMessage msg = new ImNetworkRoomMemberChangedMessage(roomMember, ImNetworkRoomMemberChangedEnum.JOIN);
            //获取节拍器信息
            ImNetworkMetronomeMessage imNetworkCustomMessage = imNetworkRoomMemberStatusService.getMemberMidi(roomMember.getRoomId());
            msg.setMetronomeSwitch(imNetworkCustomMessage.getEnable());
            //获取伴奏信息
            ImNetworkRoomMusicSheetDownloadData musicSheetDownloadData = imNetworkRoomMemberStatusService.getMemberExamSong(roomMember.getRoomId());
            msg.setExamSongSwitch(musicSheetDownloadData.getEnable());
            imHelper.publishMessage(roomMember.getUserId(), roomMember.getRoomId(), msg);
        }
    }

    @Override
    public void sendDisplay(ImNetworkRoomMember roomMember, ImNetworkRoom room,String serviceProvider) throws Exception {
        if(roomMember == null){
            log.error("sendDisplay: roomMember is null");
            return;
        }
        // IM发送用户消息
        if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(serviceProvider) || StringUtils.isEmpty(serviceProvider)) {
            // 获取RTC服务提供方
            RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(TencentCloudRTCPlugin.PLUGIN_NAME);
            // 腾讯云RTC服务
            RTCRoomMessage.MessageUser sendUser = getSendUser(roomMember);
            RTCRoomMessage.MessageContent.MessageContentBuilder action = RTCRoomMessage.MessageContent.builder()
                    .display(room.getDisplay())
                    .sendUserInfo(sendUser);


            RTCRoomMessage roomMessage = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.DISPLAY_MESSAGE)
                    .content(action.build())
                    .toChatRoomId(roomMember.getRoomId())
                    .fromUserId(sendUser.getSendUserId())
                    .isIncludeSender(1)
                    .isPersisted(1)
                    .build();

            pluginService.sendChatRoomMessage(roomMessage);
        } else {
            //发送display改动通知
            ImNetworkDisplayMessage displayMessage = new ImNetworkDisplayMessage(room.getDisplay());
            imHelper.publishMessage(roomMember.getUserId(), room.getRoomId(), displayMessage, 0);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendImPlayMidiMessage(ImNetworkCustomMessage customMessage) throws Exception {
        log.info("sendImPlayMidiMessage: customMessage={}", customMessage);
        ImNetworkMetronomeMessage playMidiMessage = JSONObject.parseObject(customMessage.getContent(), ImNetworkMetronomeMessage.class);

        ImNetworkRoomMember roomMember = imNetworkRoomMemberService.lambdaQuery().eq(ImNetworkRoomMember::getUserId, customMessage.getCurrentUserId())
                .eq(ImNetworkRoomMember::getRoomId, customMessage.getRoomId()).last("LIMIT 1").one();
        // 获取RTC服务提供方
        if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(customMessage.getServiceProvider()) || StringUtils.isEmpty(customMessage.getServiceProvider())) {
            RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(customMessage.getServiceProvider());
            // 腾讯云RTC服务
            RTCRoomMessage.MessageContent messageContent = RTCRoomMessage.MessageContent
                    .builder()
                    .enable(playMidiMessage.getEnable())
                    .rate(playMidiMessage.getRate())
                    .customType(playMidiMessage.getCustomType())
                    .userId(customMessage.getUserId())
                    .playVolume(playMidiMessage.getPlayVolume())
                    .sendUserInfo(getSendUser(roomMember))
                    .build();

            RTCRoomMessage roomMessage = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.PLAY_MIDI_MESSAGE)
                    .content(messageContent)
                    .toChatRoomId(roomMember.getRoomId())
                    .fromUserId(roomMember.getUserId())
                    .isIncludeSender(1)
                    .isPersisted(1)
                    .build();

            pluginService.sendChatRoomMessage(roomMessage);
        } else {
            imHelper.publishMessage(roomMember.getUserId(), roomMember.getRoomId(), playMidiMessage, 1);
        }
    }

    @Override
    public void pushDownloadMusicSheetMsg(ImNetworkMusicSheetDownloadMessageContent content,BasicUserInfo userInfo) throws Exception {
        // 发送消息
        if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(content.getServiceProvider()) || StringUtils.isEmpty(content.getServiceProvider())) {

            RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(content.getServiceProvider());
            // 腾讯消息推送
            RTCRoomMessage.MessageContent messageContent = RTCRoomMessage.MessageContent
                    .builder()
                    .url(content.getUrl())
                    .mp3Url(content.getMp3Url())
                    .songId(String.valueOf(content.getId()))
                    .speed(content.getSpeed())
                    .sendUserInfo(RTCRoomMessage.MessageUser.builder()
                            .sendUserId(userInfo.getImUserId())
                            .sendUserName(userInfo.getRealName())
                            .avatarUrl(userInfo.getAvatar())
                            .build())
                    .build();

            RTCRoomMessage roomMessage = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.MUSIC_SCORE_MESSAGE)
                    .content(messageContent)
                    .toChatRoomId(content.getRoomId())
                    .fromUserId(userInfo.getImUserId())
                    .isIncludeSender(1)
                    .isPersisted(1)
                    .build();

            // 发送消息
            pluginService.sendChatRoomMessage(roomMessage);
        } else {
            ImNetworkMusicSheetDownloadMessage msg = new ImNetworkMusicSheetDownloadMessage(content);
            // 融云消息推送
            imHelper.publishMessage(userInfo.getImUserId(), content.getRoomId(), msg, 0);
        }
    }

    @Override
    public void pushDeviceResourceMessage(ImNetworkDeviceControlDto deviceControl,BasicUserInfo teacher) throws Exception {
        RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(deviceControl.getServiceProvider());
        String roomId = deviceControl.getRoomId();
        // 通知消息
        RTCRoomMessage.MessageContent notifyContent = RTCRoomMessage.MessageContent
                .builder()
                .type(deviceControl.getDeviceType().ordinal())
                .enable(deviceControl.getEnable())
                .targetId(deviceControl.getUserId())
                .targetName(teacher.getRealName())
                .songId(Optional.ofNullable(deviceControl.getMusicScoreAccompanimentId()).map(String::valueOf).orElse(null))
                .songVolume(deviceControl.getSoundVolume())
                .sendUserInfo(RTCRoomMessage.MessageUser.builder()
                        .sendUserId(teacher.getImUserId())
                        .sendUserName(teacher.getRealName())
                        .avatarUrl(teacher.getAvatar())
                        .build())
                .build();

        // 腾讯云消息推送
        RTCRoomMessage message = RTCRoomMessage.builder()
                .objectName(RTCRoomMessage.CONTROL_DEVICE_NOTIFY_MESSAGE)
                .fromUserId(deviceControl.getUserId())
                .toChatRoomId(roomId)
                .content(notifyContent)
                .isPersisted(1)
                .isIncludeSender(0)
                .build();

        if(deviceControl.getEnable()){
            ImNetworkDeviceStateChangedMessage deviceResourceMessage = new ImNetworkDeviceStateChangedMessage(deviceControl.getDeviceType().ordinal(),true);
            deviceResourceMessage.setUserId(deviceControl.getUserId());
            switch (deviceControl.getDeviceType()) {
                case EXAM_SONG:
                    ImNetworkRoomMusicSheetDownloadData msg = imNetworkRoomMemberStatusService.getMemberExamSong(roomId);
                    msg.setEnable(true);
                    imNetworkRoomMemberStatusService.lambdaUpdate()
                                    .eq(ImNetworkRoomMemberStatus::getRoomId,roomId)
                                                    .set(ImNetworkRoomMemberStatus::getMusicSheetDownloadJson,JSON.toJSONString(msg)).update();

                    // 消息发送
                    if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(deviceControl.getServiceProvider()) || StringUtils.isEmpty(deviceControl.getServiceProvider())) {
                        // 腾讯云推送
                        pluginService.sendChatRoomMessage(message.objectName(RTCRoomMessage.CONTROL_DEVICE_NOTIFY_MESSAGE).content(notifyContent));
                    } else {
                        // 融云推送
                        imHelper.publishMessage(deviceControl.getUserId(), roomId, deviceResourceMessage, 1);
                    }
                    break;
                case MUSIC_SHEET:
                case ACCOMPANIMENT:
                    deviceResourceMessage.setMusicSheetAccompanimentId(deviceControl.getMusicScoreAccompanimentId());
                    deviceResourceMessage.setUserId(deviceControl.getUserId());
                    deviceResourceMessage.setSoundVolume(deviceControl.getSoundVolume());
                    if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(pluginService.pluginName())) {
                        // 腾讯云推送
                        pluginService.sendChatRoomMessage(message.objectName(RTCRoomMessage.CONTROL_DEVICE_NOTIFY_MESSAGE).content(notifyContent));
                    } else {
                        // 融云推送
                        imHelper.publishMessage(teacher.getImUserId(), roomId, deviceResourceMessage, 1);
                    }
                    break;
                default:
                    // 发送消息
                    if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(pluginService.pluginName())) {
                        // 腾讯消息
                        pluginService.sendChatRoomMessage(message.objectName(RTCRoomMessage.CONTROL_DEVICE_NOTIFY_MESSAGE).content(notifyContent));
                    } else {
                        // 融云消息
                        //邀请打开指定设备权限
                        ImNetworkControlDeviceNotifyMessage message1 = new ImNetworkControlDeviceNotifyMessage(ImNetworkActionEnum.INVITE.ordinal());
                        message1.setType(deviceControl.getDeviceType().ordinal());
                        message1.setOpUserId(teacher.getImUserId());
                        message1.setOpUserName(teacher.getRealName());
                        imHelper.publishMessage(teacher.getImUserId(), deviceControl.getUserId(), roomId, message1);
                    }
                    break;
            }
        }else {
            if(deviceControl.getDeviceType() == ImNetworkDeviceTypeEnum.MUSIC_SHEET ||
                    deviceControl.getDeviceType() == ImNetworkDeviceTypeEnum.ACCOMPANIMENT){
                ImNetworkRoomMusicSheetDownloadData msg = imNetworkRoomMemberStatusService.getMemberExamSong(roomId);
                msg.setEnable(false);
                imNetworkRoomMemberStatusService.lambdaUpdate()
                        .eq(ImNetworkRoomMemberStatus::getRoomId,roomId)
                        .set(ImNetworkRoomMemberStatus::getMusicSheetDownloadJson,JSON.toJSONString(msg)).update();
            }else {
                ImNetworkRoomMember roomMember = Optional.ofNullable(imNetworkRoomMemberService.lambdaQuery()
                                .eq(ImNetworkRoomMember::getRoomId,roomId)
                                .eq(ImNetworkRoomMember::getUserId,deviceControl.getUserId())
                                .eq(ImNetworkRoomMember::getRole,RoleEnum.RoleStudent.getValue()).last("LIMIT 1").one())
                        .orElseThrow(()-> new BizException("用户不在房间内"));
                switch (deviceControl.getDeviceType()) {
                    case CAMERA:
                        roomMember.setCameraFlag(deviceControl.getEnable());
                        break;
                    case MICROPHONE:
                        roomMember.setMicFlag(deviceControl.getEnable());
                        break;
                    case HAND_UP:
                        roomMember.setHandFlag(deviceControl.getEnable());
                        break;
                    case MUSIC_MODE:
                        roomMember.setMusicModeFlag(deviceControl.getEnable());
                        break;
                }
                if(deviceControl.getDeviceType() != ImNetworkDeviceTypeEnum.MUSIC_SHEET &&
                        deviceControl.getDeviceType() != ImNetworkDeviceTypeEnum.EXAM_SONG &&
                        deviceControl.getDeviceType() != ImNetworkDeviceTypeEnum.ACCOMPANIMENT){
                    imNetworkRoomMemberService.getDao().updateById(roomMember);
                }
            }
            if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(pluginService.pluginName())) {
                // 腾讯消息
                pluginService.sendChatRoomMessage(message.objectName(RTCRoomMessage.CONTROL_DEVICE_NOTIFY_MESSAGE).content(notifyContent));
            } else {
                // 融云消息
                ImNetworkDeviceStateChangedMessage deviceResourceMessage = new ImNetworkDeviceStateChangedMessage(deviceControl.getDeviceType().ordinal(),deviceControl.getEnable());
                deviceResourceMessage.setUserId(deviceControl.getUserId());
                deviceResourceMessage.setUserName(teacher.getRealName());
                imHelper.publishMessage(teacher.getImUserId(), roomId, deviceResourceMessage, 1);
            }
        }
    }

    @Override
    public void pushApproveControlDevice(ImNetworkDeviceControlDto deviceControl, ImNetworkRoomMember roomMember,BasicUserInfo student,BasicUserInfo teacher) throws Exception {
        switch (deviceControl.getDeviceType()) {
            case CAMERA:
                roomMember.setCameraFlag(deviceControl.getEnable());
                break;
            case EXAM_SONG:
                ImNetworkRoomMusicSheetDownloadData msg = imNetworkRoomMemberStatusService.getMemberExamSong(roomMember.getRoomId());
                msg.setEnable(deviceControl.getEnable());
                imNetworkRoomMemberStatusService.lambdaUpdate()
                        .eq(ImNetworkRoomMemberStatus::getRoomId,roomMember.getRoomId())
                        .set(ImNetworkRoomMemberStatus::getMusicSheetDownloadJson,JSON.toJSONString(msg)).update();
                break;
            case MICROPHONE:
                roomMember.setMicFlag(deviceControl.getEnable());
                break;
        }
        if(deviceControl.getDeviceType() == ImNetworkDeviceTypeEnum.CAMERA ||
                deviceControl.getDeviceType() == ImNetworkDeviceTypeEnum.MICROPHONE){
            imNetworkRoomMemberService.updateById(roomMember);
        }

        if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(deviceControl.getServiceProvider())) {

            // 获取RTC服务提供方
            RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(TencentCloudRTCPlugin.PLUGIN_NAME);
            RTCRoomMessage.MessageUser build = RTCRoomMessage.MessageUser.builder()
                    .sendUserId(teacher.getImUserId())
                    .sendUserName(teacher.getRealName())
                    .avatarUrl(teacher.getAvatar())
                    .build();
            // 腾讯云RTC服务
            RTCRoomMessage.MessageContent.MessageContentBuilder action = RTCRoomMessage.MessageContent.builder()
                    .type(ImNetworkActionEnum.APPROVE.ordinal())
                    .enable(deviceControl.getEnable())
                    .targetId(roomMember.getUserId())
                    .targetName(student.getUsername())
                    .sendUserInfo(build);


            RTCRoomMessage roomMessage = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.CONTROL_DEVICE_NOTIFY_MESSAGE)
                    .content(action.build())
                    .toChatRoomId(roomMember.getRoomId())
                    .fromUserId(roomMember.getUserId())
                    .isIncludeSender(1)
                    .isPersisted(1)
                    .build();

            pluginService.sendChatRoomMessage(roomMessage);

            // 腾讯云RTC服务
            action = RTCRoomMessage.MessageContent.builder()
                    .type(ImNetworkActionEnum.APPROVE.ordinal())
                    .enable(deviceControl.getEnable())
                    .sendUserInfo(getSendUser(roomMember));

            // 腾讯云消息推送
            RTCRoomMessage message = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.DEVICE_MESSAGE)
                    .fromUserId(roomMember.getUserId())
                    .toChatRoomId(roomMember.getRoomId())
                    .content(action.build())
                    .isPersisted(1)
                    .isIncludeSender(0)
                    .build();

            pluginService.sendChatRoomMessage(message);
        }else {
            ImNetworkControlDeviceNotifyMessage msg = new ImNetworkControlDeviceNotifyMessage(ImNetworkActionEnum.APPROVE.ordinal());
            msg.setType(deviceControl.getDeviceType().ordinal());
            msg.setOpUserId(roomMember.getUserId());
            msg.setOpUserName(student.getUsername());
            //获取老师编号
            String imUserId1 = imGroupService.getImUserId(teacher.getUserId(), RoleEnum.RoleTeacher);
            imHelper.publishMessage(roomMember.getUserId(), imUserId1, deviceControl.getRoomId(), msg);
            //发送设备状态变更消息
            this.sendDeviceStateChangedMessage(deviceControl, roomMember.getUserId());
        }
    }

    @Override
    public void sendDeviceStatusSync(ImNetworkDeviceControlDto deviceStatusSync, ImNetworkRoomMember roomMember) throws Exception {
        ImNetworkDeviceTypeEnum deviceType = deviceStatusSync.getDeviceType();
        switch (deviceType) {
            case CAMERA:
                roomMember.setCameraFlag(deviceStatusSync.getEnable());
                break;
            case MICROPHONE:
                roomMember.setMicFlag(deviceStatusSync.getEnable());
                break;
            case HAND_UP:
                roomMember.setHandFlag(deviceStatusSync.getEnable());
                break;
            case MUSIC_MODE:
                roomMember.setMusicModeFlag(deviceStatusSync.getEnable());
                break;
            case EXAM_SONG:
                ImNetworkRoomMusicSheetDownloadData msg = imNetworkRoomMemberStatusService.getMemberExamSong(roomMember.getRoomId());
                msg.setEnable(deviceStatusSync.getEnable());
                imNetworkRoomMemberStatusService.lambdaUpdate()
                        .eq(ImNetworkRoomMemberStatus::getRoomId,roomMember.getRoomId())
                        .set(ImNetworkRoomMemberStatus::getMusicSheetDownloadJson,JSON.toJSONString(msg)).update();
                break;
        }
        if(deviceType == ImNetworkDeviceTypeEnum.CAMERA ||
                deviceType == ImNetworkDeviceTypeEnum.MICROPHONE ||
                deviceType == ImNetworkDeviceTypeEnum.HAND_UP ||
                deviceType == ImNetworkDeviceTypeEnum.MUSIC_MODE){
            imNetworkRoomMemberService.getDao().updateById(roomMember);
        }
        //发送设备状态同步消息
        if (TencentCloudRTCPlugin.PLUGIN_NAME.matches(deviceStatusSync.getServiceProvider())) {
            // RTC服务对象
            RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(deviceStatusSync.getServiceProvider());
            // 消息内容
            RTCRoomMessage.MessageContent messageContent = RTCRoomMessage.MessageContent
                    .builder()
                    .type(deviceType.ordinal())
                    .enable(deviceStatusSync.getEnable())
                    .sendUserInfo(getSendUser(roomMember))
                    .build();
            // 腾讯云消息推送
            RTCRoomMessage message = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.DEVICE_MESSAGE)
                    .fromUserId(roomMember.getUserId())
                    .toChatRoomId(deviceStatusSync.getRoomId())
                    .content(messageContent)
                    .isPersisted(1)
                    .isIncludeSender(0)
                    .build();
            // 发送消息
            pluginService.sendChatRoomMessage(message);
        } else {
            // 融云消息推送
            this.sendDeviceStateChangedMessage(deviceStatusSync, roomMember.getUserId());
        }
    }

    @Override
    public void sendRejectControlDevice(ImNetworkDeviceControlDto deviceControl, ImNetworkRoomMember roomMember, BasicUserInfo teacher) throws Exception {
        if (TencentCloudRTCPlugin.PLUGIN_NAME.equals(deviceControl.getServiceProvider())) {
            // 获取RTC服务提供方
            RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(TencentCloudRTCPlugin.PLUGIN_NAME);

            RTCRoomMessage.MessageUser build = RTCRoomMessage.MessageUser.builder()
                    .sendUserId(imGroupService.getImUserId(teacher.getUserId(),ClientEnum.TEACHER))
                    .sendUserName(teacher.getRealName())
                    .avatarUrl(teacher.getAvatar())
                    .build();
            RTCRoomMessage.MessageContent.MessageContentBuilder action = RTCRoomMessage.MessageContent.builder()
                    .type(ImNetworkActionEnum.REJECT.ordinal())
                    .enable(deviceControl.getEnable())
                    .targetId(roomMember.getUserId())
                    .targetName(roomMember.getUserName())
                    .sendUserInfo(build);

            RTCRoomMessage roomMessage = RTCRoomMessage.builder()
                    .objectName(RTCRoomMessage.CONTROL_DEVICE_NOTIFY_MESSAGE)
                    .content(action.build())
                    .toChatRoomId(roomMember.getRoomId())
                    .fromUserId(roomMember.getUserId())
                    .isIncludeSender(1)
                    .isPersisted(1)
                    .build();

            pluginService.sendChatRoomMessage(roomMessage);
        } else {
            ImNetworkControlDeviceNotifyMessage msg = new ImNetworkControlDeviceNotifyMessage(ImNetworkActionEnum.REJECT.ordinal());
            msg.setType(deviceControl.getDeviceType().ordinal());
            msg.setOpUserId(roomMember.getUserId());
            msg.setOpUserName(roomMember.getUserName());
            imHelper.publishMessage(roomMember.getUserId(),
                    imGroupService.getImUserId(teacher.getUserId(),ClientEnum.TEACHER), roomMember.getRoomId(), msg);
        }
    }

    @Override
    public void sendQuitRoomMsg(ImNetworkRoomMember roomMember,String serviceProvider) throws Exception {
        RTCRoomPluginService pluginService = rtcRoomPluginContext.getPluginService(serviceProvider);
        // 腾讯云RTC服务
        RTCRoomMessage.MessageContent messageContent = RTCRoomMessage.MessageContent
                .builder()
                .action(EMemberAction.LEAVE.getValue())
                .role(roomMember.getRole())
                .handUpOn(false)
                .timestamp(System.currentTimeMillis())
                .microphone(false)
                .camera(false)
                .sendUserInfo(getSendUser(roomMember))
                .build();

        RTCRoomMessage roomMessage = RTCRoomMessage.builder()
                .objectName(RTCRoomMessage.MEMBER_CHANGE_MESSAGE)
                .toChatRoomId(roomMember.getRoomId())
                .content(messageContent)
                .fromUserId(roomMember.getUserId())
                .isPersisted(1)
                .isIncludeSender(1)
                .build();

        pluginService.sendChatRoomMessage(roomMessage);
    }

    private RTCRoomMessage.MessageUser getSendUser(ImNetworkRoomMember roomMember) {
        RTCRoomMessage.MessageUser build = RTCRoomMessage.MessageUser.builder()
                .sendUserId(roomMember.getUserId())
                .sendUserName(roomMember.getUserName())
                .avatarUrl(roomMember.getAvatar())
                .build();
        return build;
    }

    //发送设备状态同步消息
    private void sendDeviceStateChangedMessage(ImNetworkDeviceControlDto deviceStatusSync,String imUserId) throws Exception {
        ImNetworkRoom room = imNetworkRoomService.getById(deviceStatusSync.getRoomId());
        ImNetworkDeviceStateChangedMessage deviceResourceMessage = new ImNetworkDeviceStateChangedMessage(deviceStatusSync.getDeviceType().ordinal(),deviceStatusSync.getEnable());
        deviceResourceMessage.setUserId(imUserId);
        deviceResourceMessage.setSoundVolume(room.getSoundVolume());
        imHelper.publishMessage(imUserId, deviceStatusSync.getRoomId(), deviceResourceMessage, 1);
    }
}

