package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.UserOnline;
import com.ruoyi.cai.dto.app.vo.user.OnlineStatusVo;
import com.ruoyi.cai.mapper.UserOnlineMapper;
import com.ruoyi.cai.service.UserOnlineService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户在线状态Service业务层处理
 *
 * @author 77
 * @date 2023-12-23
 */
@RequiredArgsConstructor
@Service
public class UserOnlineServiceImpl extends ServiceImpl<UserOnlineMapper, UserOnline> implements UserOnlineService {

    @Override
    public UserOnline getByUserId(Long userId){
        return this.getOne(Wrappers.lambdaQuery(UserOnline.class).eq(UserOnline::getUserId,userId).last("limit 1"));
    }


    @Override
    public OnlineStatusVo onlineStatus(Long userId,Integer openVideoStatus,Integer videoStatus,Integer isAnchor){
        UserOnline userOnline = this.getByUserId(userId);
        Integer onlineStatus = userOnline.getStatus();
        Integer status = 0;
        String statusMsg = "离线";
        if(isAnchor == 1){
            if(openVideoStatus == 1 && videoStatus == 0 && onlineStatus == 1){
                status = 1;
                statusMsg = "空闲";
            }else if(videoStatus == 1){
                status = 2;
                statusMsg = "忙碌中";
            }else if(openVideoStatus == 0){
                status = 3;
                statusMsg = "勿扰";
            }
        }else{
            if(onlineStatus == 1){
                status = 1;
                statusMsg = "在线";
            }
        }
        OnlineStatusVo vo = new OnlineStatusVo();
        vo.setStatus(status);
        vo.setOnlineStatus(onlineStatus);
        vo.setStatusMsg(statusMsg);
        return vo;
    }

}
