package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbExamCourse;
import com.ruoyi.hezhi.domain.TbExamSubject;
import com.ruoyi.hezhi.domain.TbExamSubjectSimulate;
import com.ruoyi.hezhi.domain.TbSubjectWarehouseClass;
import com.ruoyi.hezhi.mapper.TbExamCourseMapper;
import com.ruoyi.hezhi.mapper.TbExamSubjectMapper;
import com.ruoyi.hezhi.mapper.TbExamSubjectSimulateMapper;
import com.ruoyi.hezhi.service.ITbExamSubjectSimulateService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 模拟考试题目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class TbExamSubjectSimulateServiceImpl implements ITbExamSubjectSimulateService
{
    @Autowired
    private TbExamSubjectSimulateMapper tbExamSubjectSimulateMapper;
    @Autowired
    private TbExamSubjectMapper tbExamSubjectMapper;
    @Autowired
    private TbExamCourseMapper tbExamCourseMapper;

    /**
     * 查询模拟考试题目
     *
     * @param examSubjectSimulateId 模拟考试题目主键
     * @return 模拟考试题目
     */
    @Override
    public TbExamSubjectSimulate selectTbExamSubjectSimulateByExamSubjectSimulateId(Long examSubjectSimulateId)
    {
        return tbExamSubjectSimulateMapper.selectTbExamSubjectSimulateByExamSubjectSimulateId(examSubjectSimulateId);
    }

    /**
     * 查询模拟考试题目列表
     *
     * @param tbExamSubjectSimulate 模拟考试题目
     * @return 模拟考试题目
     */
    @Override
    public List<TbExamSubjectSimulate> selectTbExamSubjectSimulateList(TbExamSubjectSimulate tbExamSubjectSimulate)
    {
        return tbExamSubjectSimulateMapper.selectTbExamSubjectSimulateList(tbExamSubjectSimulate);
    }

    /**
     * 新增模拟考试题目
     *
     * @param tbExamSubjectSimulate 模拟考试题目
     * @return 结果
     */
    @Override
    public int insertTbExamSubjectSimulate(TbExamSubjectSimulate tbExamSubjectSimulate)
    {
        tbExamSubjectSimulate.setCreateTime(DateUtils.getNowDate());
        return tbExamSubjectSimulateMapper.insertTbExamSubjectSimulate(tbExamSubjectSimulate);
    }

    /**
     * 修改模拟考试题目
     *
     * @param tbExamSubjectSimulate 模拟考试题目
     * @return 结果
     */
    @Override
    public int updateTbExamSubjectSimulate(TbExamSubjectSimulate tbExamSubjectSimulate)
    {
        tbExamSubjectSimulate.setUpdateTime(DateUtils.getNowDate());
        return tbExamSubjectSimulateMapper.updateTbExamSubjectSimulate(tbExamSubjectSimulate);
    }

    /**
     * 批量删除模拟考试题目
     *
     * @param examSubjectSimulateIds 需要删除的模拟考试题目主键
     * @return 结果
     */
    @Override
    public int deleteTbExamSubjectSimulateByExamSubjectSimulateIds(Long[] examSubjectSimulateIds)
    {
        return tbExamSubjectSimulateMapper.deleteTbExamSubjectSimulateByExamSubjectSimulateIds(examSubjectSimulateIds);
    }

    /**
     * 删除模拟考试题目信息
     *
     * @param examSubjectSimulateId 模拟考试题目主键
     * @return 结果
     */
    @Override
    public int deleteTbExamSubjectSimulateByExamSubjectSimulateId(Long examSubjectSimulateId)
    {
        return tbExamSubjectSimulateMapper.deleteTbExamSubjectSimulateByExamSubjectSimulateId(examSubjectSimulateId);
    }

    /**
     * 批量新增模拟考试题目
     *
     * @param tbSubjectWarehouseClass
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchInsertTbExamSubjectSimulateBy(TbSubjectWarehouseClass tbSubjectWarehouseClass) {
        Integer singleSelectNum = tbSubjectWarehouseClass.getSingleSelectNum();
        Integer manySelectNum = tbSubjectWarehouseClass.getManySelectNum();
        Integer judgeNum = tbSubjectWarehouseClass.getJudgeNum();
        Integer discussNum = tbSubjectWarehouseClass.getDiscussNum();

        BigDecimal singleSelectScore = tbSubjectWarehouseClass.getSingleSelectScore();
        BigDecimal manySelectScore = tbSubjectWarehouseClass.getManySelectScore();
        BigDecimal judgeScore = tbSubjectWarehouseClass.getJudgeScore();
        BigDecimal discussScore = tbSubjectWarehouseClass.getDiscussScore();

        int sort = 1;
        Date nowDate = DateUtils.getNowDate();
        List<TbExamSubject> insertList = new ArrayList<>();
        if (singleSelectNum > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("subjectWarehouseClassId", tbSubjectWarehouseClass.getSubjectWarehouseClassId());
            map.put("subjectType", 1);
            map.put("examSubjectNum", singleSelectNum);
            map.put("examType", '1');
            List<TbExamSubject> singleSelectList = tbExamSubjectMapper.randomGenerateTbExamSubjectList(map);
            if (!singleSelectList.isEmpty()) {
                for (TbExamSubject singleSelect : singleSelectList) {
                    singleSelect.setSort(sort);
                    singleSelect.setScore(singleSelectScore);
                    singleSelect.setCreateTime(nowDate);
                    sort ++;
                }
                insertList.addAll(singleSelectList);
            }
        }
        if (manySelectNum > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("subjectWarehouseClassId", tbSubjectWarehouseClass.getSubjectWarehouseClassId());
            map.put("subjectType", 2);
            map.put("examSubjectNum", manySelectNum);
            map.put("examType", '1');
            List<TbExamSubject> manySelectList = tbExamSubjectMapper.randomGenerateTbExamSubjectList(map);
            if (!manySelectList.isEmpty()) {
                for (TbExamSubject manySelect : manySelectList) {
                    manySelect.setSort(sort);
                    manySelect.setScore(manySelectScore);
                    manySelect.setCreateTime(nowDate);
                    sort ++;
                }
                insertList.addAll(manySelectList);
            }
        }
        if (judgeNum > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("subjectWarehouseClassId", tbSubjectWarehouseClass.getSubjectWarehouseClassId());
            map.put("subjectType", 3);
            map.put("examSubjectNum", judgeNum);
            map.put("examType", '1');
            List<TbExamSubject> judgeList = tbExamSubjectMapper.randomGenerateTbExamSubjectList(map);
            if (!judgeList.isEmpty()) {
                for (TbExamSubject judge : judgeList) {
                    judge.setSort(sort);
                    judge.setScore(judgeScore);
                    judge.setCreateTime(nowDate);
                    sort ++;
                }
                insertList.addAll(judgeList);
            }
        }
        if (discussNum > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("subjectWarehouseClassId", tbSubjectWarehouseClass.getSubjectWarehouseClassId());
            map.put("subjectType", 4);
            map.put("examSubjectNum", discussNum);
            map.put("examType", '1');
            List<TbExamSubject> discussList = tbExamSubjectMapper.randomGenerateTbExamSubjectList(map);
            if (!discussList.isEmpty()) {
                for (TbExamSubject discuss : discussList) {
                    discuss.setSort(sort);
                    discuss.setScore(discussScore);
                    discuss.setCreateTime(nowDate);
                    sort ++;
                }
                insertList.addAll(discussList);
            }
        }
        // 删除模拟考试题目
        tbExamSubjectSimulateMapper.deleteTbExamSubjectSimulateBySubjectWarehouseClassId(tbSubjectWarehouseClass.getSubjectWarehouseClassId());
        if (!insertList.isEmpty()) {
            // 批量新增模拟考试题目
            tbExamSubjectSimulateMapper.batchInsertTbExamSubjectSimulateBy(insertList);
        }
        return 1;
    }

    /**
     * 查询所有题目
     *
     * @param params 参数
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> getAllTbExamSubjectSimulate(Map<String, Object> params) {

        TbExamCourse examCourseDetail = tbExamCourseMapper.getSubjectWarehouseClassIdByCourseId(MapUtils.getLong(params, "courseId"));
        if (ObjectUtils.isEmpty(examCourseDetail)){
            throw new ServiceException("管理员未上传该题库信息");
        }
        params.put("subjectWarehouseClassId", examCourseDetail.getSubjectWarehouseClassId());
        return tbExamSubjectSimulateMapper.getAllTbExamSubjectSimulate(params);
    }
}
