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.extension.service.impl.ServiceImpl;
import com.fs.common.exception.ServiceException;
import com.fs.common.exception.base.BaseException;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.live.domain.LiveQuestionBank;
import com.fs.live.domain.LiveQuestionLive;
import com.fs.live.mapper.LiveQuestionBankMapper;
import com.fs.live.mapper.LiveQuestionLiveMapper;
import com.fs.live.service.ILiveQuestionBankService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class LiveQuestionBankServiceImpl extends ServiceImpl<LiveQuestionBankMapper, LiveQuestionBank> implements ILiveQuestionBankService {

    private final LiveQuestionLiveMapper liveQuestionLiveMapper;

    /**
     * 查询题库列表
     * @param title 题
     * @return 题库
     */
    @Override
    public List<LiveQuestionBank> selectLiveQuestionBankList(String title) {
        LambdaQueryWrapper<LiveQuestionBank> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(title)) {
            wrapper.like(LiveQuestionBank::getTitle, title);
        }

        return baseMapper.selectList(wrapper);
    }

    /**
     * 新增题
     *
     * @param liveQuestionBank 题
     * @param username  用户名称
     * @return 受影响行数
     */
    @Override
    public int insertLiveQuestionBank(LiveQuestionBank liveQuestionBank, String username) {
        // 参数校验
        verifyParams(liveQuestionBank);

        liveQuestionBank.setCreateTime(LocalDateTime.now());
        liveQuestionBank.setCreateBy(username);
        liveQuestionBank.setUpdateTime(LocalDateTime.now());
        liveQuestionBank.setUpdateBy(username);
        return baseMapper.insert(liveQuestionBank);
    }

    /**
     * 参数校验
     * @param liveQuestionBank 题目
     */
    private void verifyParams(LiveQuestionBank liveQuestionBank) {
        if (StringUtils.isBlank(liveQuestionBank.getTitle())) {
            throw new BaseException("题干不能为空");
        }
        if (StringUtils.isBlank(liveQuestionBank.getContent())) {
            throw new BaseException("内容不能为空");
        }
        if (Objects.isNull(liveQuestionBank.getType())) {
            throw new BaseException("类型不能为空");
        }
        if (StringUtils.isBlank(liveQuestionBank.getAnswer())) {
            throw new BaseException("答案不能为空");
        }
    }

    /**
     * 修改题目
     * @param liveQuestionBank 题目
     * @param username 用户名
     * @return  受影响行数
     */
    @Override
    public int updateLiveQuestionBank(LiveQuestionBank liveQuestionBank, String username) {
        // 参数校验
        verifyParams(liveQuestionBank);

        if (Objects.isNull(liveQuestionBank.getId())) {
            throw new BaseException("ID不能为空");
        }

        LiveQuestionBank questionBank = baseMapper.selectById(liveQuestionBank.getId());
        if (Objects.isNull(questionBank)) {
            throw new BaseException("题目不存在");
        }

        BeanUtils.copyProperties(liveQuestionBank, questionBank);
        liveQuestionBank.setUpdateTime(LocalDateTime.now());
        liveQuestionBank.setUpdateBy(username);
        return baseMapper.updateById(liveQuestionBank);
    }

    /**
     * 删除题目
     * @param ids ID集合
     * @return 受影响行数
     */
    @Override
    public int deleteLiveQuestionBankByIds(Long[] ids) {
        // 判断是否被使用
        Wrapper<LiveQuestionLive> wrapper = new LambdaQueryWrapper<LiveQuestionLive>().in(LiveQuestionLive::getQuestionId, Arrays.asList(ids));
        if (liveQuestionLiveMapper.selectCount(wrapper) > 0) {
            throw new ServiceException("题目使用中,无法删除");
        }

        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
