package com.fs.live.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.exception.ServiceException;
import com.fs.live.domain.Live;
import com.fs.live.domain.LiveQuestionBank;
import com.fs.live.domain.LiveQuestionLive;
import com.fs.live.mapper.LiveMapper;
import com.fs.live.mapper.LiveQuestionBankMapper;
import com.fs.live.mapper.LiveQuestionLiveMapper;
import com.fs.live.service.ILiveQuestionLiveService;
import com.fs.live.vo.LiveQuestionLiveVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class LiveQuestionLiveServiceImpl extends ServiceImpl<LiveQuestionLiveMapper, LiveQuestionLive> implements ILiveQuestionLiveService {

    private final LiveQuestionBankMapper liveQuestionBankMapper;
    private final LiveMapper liveMapper;

    /**
     * 查询直播间题目列表
     * @param liveId    直播间ID
     * @return  list
     */
    @Override
    public List<LiveQuestionLiveVO> selectLiveQuestionLiveList(Long liveId) {
        return baseMapper.selectLiveQuestionLiveList(liveId);
    }

    /**
     * 添加直播间题目
     * @param liveId    直播间ID
     * @param questionIds   题目ID集合
     * @param username  用户名称
     * @return  是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertLiveQuestion(Long liveId, List<Long> questionIds, String username) {
        Live live = liveMapper.selectById(liveId);
        if (Objects.isNull(live)) {
            throw new ServiceException("直播间不存在");
        }

        Collection<LiveQuestionBank> liveQuestionBanks = liveQuestionBankMapper.selectBatchIds(questionIds);
        if (liveQuestionBanks.isEmpty()) {
            throw new ServiceException("请选择有效的题目");
        }

        Set<Long> ids = liveQuestionBanks.stream().map(LiveQuestionBank::getId).collect(Collectors.toSet());

        // 过滤已经存在的题目
        Wrapper<LiveQuestionLive> wrapper = Wrappers.<LiveQuestionLive>lambdaQuery()
                .eq(LiveQuestionLive::getLiveId, liveId)
                .in(LiveQuestionLive::getQuestionId, ids);
        Set<Long> hasQuestions = baseMapper.selectList(wrapper).stream().map(LiveQuestionLive::getQuestionId).collect(Collectors.toSet());

        List<LiveQuestionLive> liveQuestionLives = ids.stream().filter(id -> !hasQuestions.contains(id))
        .map(id -> {
            LiveQuestionLive liveQuestionLive = new LiveQuestionLive();
            liveQuestionLive.setLiveId(liveId);
            liveQuestionLive.setQuestionId(id);
            liveQuestionLive.setCreateTime(LocalDateTime.now());
            liveQuestionLive.setCreateBy(username);
            return liveQuestionLive;
        }).collect(Collectors.toList());

        if (!liveQuestionLives.isEmpty()) {
            return saveBatch(liveQuestionLives);
        }

        return false;
    }

    /**
     * 删除直播间题目
     * @param liveId 直播间ID
     * @param ids   题目集合
     * @return 受影响行数
     */
    @Override
    public int deleteLiveQuestionByIds(Long liveId, List<Long> ids) {
        Live live = liveMapper.selectById(liveId);
        if (Objects.isNull(live)) {
            throw new ServiceException("直播间不存在");
        }

        // TODO:判断是否被使用

        Wrapper<LiveQuestionLive> wrapper = Wrappers.<LiveQuestionLive>lambdaQuery().eq(LiveQuestionLive::getLiveId, liveId).in(LiveQuestionLive::getId, ids);
        return baseMapper.delete(wrapper);
    }

    /**
     * 查询未被直播间选择的题目列表
     * @param liveId    直播间ID
     * @param title     标题
     * @return  list
     */
    @Override
    public List<LiveQuestionLiveVO> selectLiveQuestionOptionList(Long liveId, String title) {
        return baseMapper.selectLiveQuestionOptionList(liveId, title);
    }
}
