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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dto.ImNetworkMetronomeMessage;
import com.yonge.cooleshow.biz.dal.dto.ImNetworkRoomMusicSheetDownloadData;
import com.yonge.cooleshow.biz.dal.entity.ImNetworkRoomMemberStatus;
import com.yonge.cooleshow.biz.dal.mapper.ImNetworkRoomMemberStatusMapper;
import com.yonge.cooleshow.biz.dal.service.ImNetworkRoomMemberStatusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 网络教室成员状态表
 * 2023-08-14 15:47:02
 */
@Slf4j
@Service
public class ImNetworkRoomMemberStatusServiceImpl extends ServiceImpl<ImNetworkRoomMemberStatusMapper, ImNetworkRoomMemberStatus> implements ImNetworkRoomMemberStatusService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(String roomId) {
        ImNetworkRoomMemberStatus memberStatus = this.lambdaQuery()
                .eq(ImNetworkRoomMemberStatus::getRoomId,roomId).last("LIMIT 1").one();
        if(Objects.nonNull(memberStatus)){
            return;
        }
        memberStatus = new ImNetworkRoomMemberStatus();
        memberStatus.setRoomId(roomId);
        baseMapper.insert(memberStatus);
    }

    @Override
    public ImNetworkMetronomeMessage getMemberMidi(String roomId) {
        String midi = this.lambdaQuery()
                .eq(ImNetworkRoomMemberStatus::getRoomId,roomId).last("LIMIT 1").one().getPlayMidi();
        if(StringUtils.isNotEmpty(midi)){
            return JSONObject.parseObject(midi, ImNetworkMetronomeMessage.class);
        }
        return new ImNetworkMetronomeMessage();
    }

    @Override
    public ImNetworkRoomMusicSheetDownloadData getMemberExamSong(String roomId) {
        String musicSheet = this.lambdaQuery()
                .eq(ImNetworkRoomMemberStatus::getRoomId,roomId).last("LIMIT 1").one().getMusicSheetDownloadJson();
        if(StringUtils.isNotEmpty(musicSheet)){
            return JSONObject.parseObject(musicSheet, ImNetworkRoomMusicSheetDownloadData.class);
        }
        return new ImNetworkRoomMusicSheetDownloadData();
    }
}