package com.fs.live.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.live.domain.LiveWatchUser;
import com.fs.live.mapper.LiveWatchUserMapper;
import com.fs.live.service.ILiveWatchUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 直播间观看用户Service业务层处理
 * 
 * @author fs
 * @date 2025-01-18
 */
@Service
public class LiveWatchUserServiceImpl extends ServiceImpl<LiveWatchUserMapper, LiveWatchUser> implements ILiveWatchUserService {

    /**
     * 查询直播间观看用户
     * 
     * @param id 直播间观看用户主键
     * @return 直播间观看用户
     */
    @Override
    public LiveWatchUser selectLiveWatchUserById(Long id)
    {
        return baseMapper.selectLiveWatchUserById(id);
    }

    /**
     * 查询直播间观看用户列表
     * 
     * @param liveWatchUser 直播间观看用户
     * @return 直播间观看用户
     */
    @Override
    public List<LiveWatchUser> selectLiveWatchUserList(LiveWatchUser liveWatchUser)
    {
        return baseMapper.selectLiveWatchUserList(liveWatchUser);
    }

    /**
     * 新增直播间观看用户
     * 
     * @param liveWatchUser 直播间观看用户
     * @return 结果
     */
    @Override
    public int insertLiveWatchUser(LiveWatchUser liveWatchUser)
    {
        liveWatchUser.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertLiveWatchUser(liveWatchUser);
    }

    /**
     * 修改直播间观看用户
     * 
     * @param liveWatchUser 直播间观看用户
     * @return 结果
     */
    @Override
    public int updateLiveWatchUser(LiveWatchUser liveWatchUser)
    {
        liveWatchUser.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateLiveWatchUser(liveWatchUser);
    }

    /**
     * 批量删除直播间观看用户
     * 
     * @param ids 需要删除的直播间观看用户主键
     * @return 结果
     */
    @Override
    public int deleteLiveWatchUserByIds(Long[] ids)
    {
        return baseMapper.deleteLiveWatchUserByIds(ids);
    }

    /**
     * 删除直播间观看用户信息
     * 
     * @param id 直播间观看用户主键
     * @return 结果
     */
    @Override
    public int deleteLiveWatchUserById(Long id)
    {
        return baseMapper.deleteLiveWatchUserById(id);
    }

    @Override
    public LiveWatchUser getByLiveIdAndUserId(long liveId, long userId) {
        return getOne(new QueryWrapper<LiveWatchUser>().eq("live_id", liveId).eq("user_id", userId));
    }

    @Override
    public void join(long liveId, long userId) {
        LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
        if(liveWatchUser != null) {
            liveWatchUser.setUpdateTime(DateUtils.getNowDate());
            liveWatchUser.setOnline(0);
        }else{
            liveWatchUser = new LiveWatchUser();
            liveWatchUser.setLiveId(liveId);
            liveWatchUser.setUserId(userId);
        }
        super.saveOrUpdate(liveWatchUser);
    }
    @Override
    public void close(long liveId, long userId) {
        LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
        liveWatchUser.setUpdateTime(DateUtils.getNowDate());
        liveWatchUser.setOnline(1);
        super.updateById(liveWatchUser);
    }
}
