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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.ImNetworkRoomMemberDao;
import com.yonge.cooleshow.biz.dal.dto.BasicUserInfo;
import com.yonge.cooleshow.biz.dal.entity.ImNetworkRoomMember;
import com.yonge.cooleshow.biz.dal.enums.UserRoleEnum;
import com.yonge.cooleshow.biz.dal.service.ImNetworkRoomMemberService;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 网络教室成员(ImNetworkRoomMember)表服务实现类
 *
 * @author zx
 * @since 2022-03-30 16:36:38
 */
@Service("imNetworkRoomMemberService")
public class ImNetworkRoomMemberServiceImpl extends ServiceImpl<ImNetworkRoomMemberDao, ImNetworkRoomMember> implements ImNetworkRoomMemberService {

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

    @Override
    public ImNetworkRoomMemberDao getDao() {
        return this.baseMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ImNetworkRoomMember initRoomMember(String roomId, BasicUserInfo sysUser, UserRoleEnum userRole) {
        ImNetworkRoomMember roomMember = baseMapper.findByRidAndUid(roomId, sysUser.getUserId());
        if(Objects.isNull(roomMember)){
            roomMember = new ImNetworkRoomMember();
            roomMember.setRoomId(roomId);
            roomMember.setCameraFlag(true);
            roomMember.setHandFlag(false);
            roomMember.setMicFlag(true);
            roomMember.setMusicModeFlag(false);
            roomMember.setRole(userRole.ordinal());
            roomMember.setUserId(sysUser.getUserId());
//            roomMember.setUsername(userRole == UserRoleEnum.STUDENT?sysUser.getUsername():sysUser.getRealName());
            roomMember.setUsername(sysUser.getUsername());
            roomMember.setAvatar(sysUser.getAvatar());
            roomMember.setCourseScheduleId(Long.parseLong(roomId));
            baseMapper.insert(roomMember);
        }
        return roomMember;
    }

}

