package com.fs.live.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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 com.fs.live.vo.LiveWatchUserVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 直播间观看用户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 id                直播间用户ID
     * @param companyId         企业ID
     * @param companyUserId     企业用户ID
     * @return LiveWatchUser
     */
    @Override
    public LiveWatchUser selectLiveWatchUserByIdAndCompanyIdAndCompanyUserId(Long id, Long companyId, Long companyUserId)
    {
        return baseMapper.selectLiveWatchUserByIdAndCompanyIdAndCompanyUserId(id, companyId, companyUserId);
    }

    /**
     * 查询直播间观看用户列表
     * 
     * @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);
    }

    /**
     * 查询直播间在线用户列表
     * @param params 参数
     * @return list
     */
    @Override
    public List<LiveWatchUserVO> selectWatchUserList(Map<String, Object> params) {
        return baseMapper.selectWatchUserListByLiveId(params);
    }

    /**
     * 修改直播间用户禁言状态
     * @param liveId    直播间ID
     * @param userId    用户ID
     * @return  受影响条数
     */
    @Override
    public int changeUserState(Long liveId, Long userId) {
        LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
        if (Objects.nonNull(liveWatchUser)) {
            liveWatchUser.setMsgStatus(Math.abs(1 - liveWatchUser.getMsgStatus()));
            liveWatchUser.setUpdateTime(DateUtils.getNowDate());
            return baseMapper.updateById(liveWatchUser);
        }
        return 0;
    }

    /**
     * 根据直播间ID和用户ID查询观看用户信息
     * @param liveId    直播间ID
     * @param userId    观看用户ID
     * @return LiveWatchUserVO
     */
    @Override
    public LiveWatchUserVO selectWatchUserByLiveIdAndUserId(Long liveId, Long userId) {
        return baseMapper.selectWatchUserByLiveIdAndUserId(liveId, userId);
    }

}
