package jnpf.cultivate.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.FtbCultivateExamHistoryPaperMapper;
import jnpf.cultivate.service.FtbCultivateExamHistoryPaperService;
import jnpf.model.cultivate.po.exam.FtbCultivateExam;
import jnpf.model.cultivate.po.exam.FtbCultivateExamHistoryPaper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FtbCultivateExamHistoryPaperServiceImpl extends ServiceImpl<FtbCultivateExamHistoryPaperMapper, FtbCultivateExamHistoryPaper> implements FtbCultivateExamHistoryPaperService {

    /**
     * 根据关键字搜索考试名称
     * @param keyword 考试名称
     * @return
     */
    @Override
    public List<String> searchFrzzExamName(String keyword) {
        List<String> examIds = new ArrayList<>();
        if (StringUtils.isEmpty(keyword)) {
            return examIds;
        }
        LambdaQueryWrapper<FtbCultivateExamHistoryPaper> wrapper = new LambdaQueryWrapper<FtbCultivateExamHistoryPaper>()
                .select(FtbCultivateExamHistoryPaper::getId, FtbCultivateExamHistoryPaper::getExamId, FtbCultivateExamHistoryPaper::getPrimaryExamId)
                .like(FtbCultivateExamHistoryPaper::getExamName, keyword);
        List<FtbCultivateExamHistoryPaper> list = baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            for (FtbCultivateExamHistoryPaper ftbCultivateExam : list) {
                examIds.add(ftbCultivateExam.getPrimaryExamId());
            }
        }
        return examIds;
    }

    /**
     * 根据考试ID批量查询历史考试试卷信息
     * @param examIds 考试ID集合
     * @return
     */
    @Override
    public Map<String, FtbCultivateExamHistoryPaper> queryHistoryExamListByIds(List<String> examIds) {
        Map<String, FtbCultivateExamHistoryPaper> ret = new HashMap<>();
        if (CollectionUtil.isEmpty(examIds)) {
            return ret;
        }
        LambdaQueryWrapper<FtbCultivateExamHistoryPaper> wrapper = new LambdaQueryWrapper<FtbCultivateExamHistoryPaper>()
                .in(FtbCultivateExamHistoryPaper::getPrimaryExamId, examIds);
        List<FtbCultivateExamHistoryPaper> list = baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            for (FtbCultivateExamHistoryPaper entity : list) {
                ret.put(entity.getPrimaryExamId() + entity.getVersionBatch(), entity);
            }
        }
        return ret;
    }

    /**
     * 根据考试id和批次查询历史试卷
     * @param id 考试ID
     * @param versionBatch 批次
     * @return
     */
    @Override
    public FtbCultivateExamHistoryPaper queryByEamIdAndVersionBatch(String id, String versionBatch) {
        LambdaQueryWrapper<FtbCultivateExamHistoryPaper> wrapper = new LambdaQueryWrapper<FtbCultivateExamHistoryPaper>()
                .eq(FtbCultivateExamHistoryPaper::getVersionBatch, versionBatch)
                .eq(FtbCultivateExamHistoryPaper::getPrimaryExamId, id);
        List<FtbCultivateExamHistoryPaper> list = baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 记录考试的历史试卷
     * @param oldExam
     */
    @Override
    public void recordHistory(FtbCultivateExam oldExam) {
        FtbCultivateExamHistoryPaper entity = BeanUtil.copyProperties(oldExam, FtbCultivateExamHistoryPaper.class);
        entity.setPrimaryExamId(entity.getId());
        entity.setId(null);
        baseMapper.insert(entity);
    }

    /**
     * 根据考试id查询历史试卷
     * @param examId 考试id
     * @return
     */
    @Override
    public List<FtbCultivateExamHistoryPaper> queryByExamId(String examId) {
        LambdaQueryWrapper<FtbCultivateExamHistoryPaper> wrapper = new LambdaQueryWrapper<FtbCultivateExamHistoryPaper>()
                .eq(FtbCultivateExamHistoryPaper::getPrimaryExamId, examId)
                .orderByDesc(FtbCultivateExamHistoryPaper::getCreatorTime);
        List<FtbCultivateExamHistoryPaper> list = baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return list;
        }
        return new ArrayList<>();
    }
}
