package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbExam;
import com.ruoyi.hezhi.domain.TbExamSubject;
import com.ruoyi.hezhi.domain.TbExamSubjectFormal;
import com.ruoyi.hezhi.mapper.TbExamSubjectAnswerMapper;
import com.ruoyi.hezhi.mapper.TbExamSubjectFormalMapper;
import com.ruoyi.hezhi.mapper.TbExamSubjectMapper;
import com.ruoyi.hezhi.service.ITbExamSubjectFormalService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

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

/**
 * 正式考试题目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-13
 */
@Service
public class TbExamSubjectFormalServiceImpl implements ITbExamSubjectFormalService
{
    @Autowired
    private TbExamSubjectFormalMapper tbExamSubjectFormalMapper;
    @Autowired
    private TbExamSubjectMapper tbExamSubjectMapper;
    @Autowired
    private TbExamSubjectAnswerMapper examSubjectAnswerMapper;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 查询正式考试题目
     *
     * @param examSubjectFormalId 正式考试题目主键
     * @return 正式考试题目
     */
    @Override
    public TbExamSubjectFormal selectTbExamSubjectFormalByExamSubjectFormalId(Long examSubjectFormalId)
    {
        return tbExamSubjectFormalMapper.selectTbExamSubjectFormalByExamSubjectFormalId(examSubjectFormalId);
    }

    /**
     * 查询正式考试题目列表
     *
     * @param tbExamSubjectFormal 正式考试题目
     * @return 正式考试题目
     */
    @Override
    public List<TbExamSubjectFormal> selectTbExamSubjectFormalList(TbExamSubjectFormal tbExamSubjectFormal)
    {
        return tbExamSubjectFormalMapper.selectTbExamSubjectFormalList(tbExamSubjectFormal);
    }

    /**
     * 新增正式考试题目
     *
     * @param tbExamSubjectFormal 正式考试题目
     * @return 结果
     */
    @Override
    public int insertTbExamSubjectFormal(TbExamSubjectFormal tbExamSubjectFormal)
    {
        tbExamSubjectFormal.setCreateTime(DateUtils.getNowDate());
        return tbExamSubjectFormalMapper.insertTbExamSubjectFormal(tbExamSubjectFormal);
    }

    /**
     * 修改正式考试题目
     *
     * @param tbExamSubjectFormal 正式考试题目
     * @return 结果
     */
    @Override
    public int updateTbExamSubjectFormal(TbExamSubjectFormal tbExamSubjectFormal)
    {
        tbExamSubjectFormal.setUpdateTime(DateUtils.getNowDate());
        return tbExamSubjectFormalMapper.updateTbExamSubjectFormal(tbExamSubjectFormal);
    }

    /**
     * 批量删除正式考试题目
     *
     * @param examSubjectFormalIds 需要删除的正式考试题目主键
     * @return 结果
     */
    @Override
    public int deleteTbExamSubjectFormalByExamSubjectFormalIds(Long[] examSubjectFormalIds)
    {
        return tbExamSubjectFormalMapper.deleteTbExamSubjectFormalByExamSubjectFormalIds(examSubjectFormalIds);
    }

    /**
     * 删除正式考试题目信息
     *
     * @param examSubjectFormalId 正式考试题目主键
     * @return 结果
     */
    @Override
    public int deleteTbExamSubjectFormalByExamSubjectFormalId(Long examSubjectFormalId)
    {
        return tbExamSubjectFormalMapper.deleteTbExamSubjectFormalByExamSubjectFormalId(examSubjectFormalId);
    }

    /**
     * 批量新增正式考试题目
     *
     * @param tbExam
     * @return 结果
     */
    @Override
    public int batchInsertTbExamSubjectFormalBy(TbExam tbExam) {
        Integer singleSelectNum = tbExam.getSingleSelectNum();
        Integer manySelectNum = tbExam.getManySelectNum();
        Integer judgeNum = tbExam.getJudgeNum();
        Integer discussNum = tbExam.getDiscussNum();
        Integer level = tbExam.getLevel();

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

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

    /**
     * 查询正式考试所有题目
     *
     * @param params 参数
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> getAllTbExamSubjectFormal(Map<String, Object> params) {
        List<Map<String, Object>> list = tbExamSubjectFormalMapper.getAllTbExamSubjectFormal(params);
//        if (!list.isEmpty()){
//            try {
//                CountDownLatch countDownLatch = new CountDownLatch(list.size());
//                for (Map<String, Object> map : list) {
//                    threadPoolTaskExecutor.execute(()->{
//                        Map<String, Object> examSubjectInfo = tbExamSubjectMapper.getExamSubjectInfo(map);
//                        // 题目类型（1-单选，2-多选，3-判断，4-论述）
//                        int subjectType = MapUtils.getInteger(examSubjectInfo, "subjectType");
//                        String examSubjectNo = MapUtils.getString(examSubjectInfo, "examSubjectNo");
//                        if (subjectType != 4) {
//                            // 选项列表
//                            List<Map<String, Object>> subjectSelectList = examSubjectAnswerMapper.selectSelectList(null, examSubjectNo);
//                            examSubjectInfo.put("subjectSelectList", subjectSelectList);
//                        } else {
//                            examSubjectInfo.put("subjectSelectList", null);
//                        }
//
//                        // 查询答案列表
//                        examSubjectInfo.put("subjectAnswerList", examSubjectAnswerMapper.selectAnswerList(null, examSubjectNo));
//                        map.put("examSubjectInfo", examSubjectInfo);
//                        countDownLatch.countDown();
//                    });
//                }
//                countDownLatch.await();
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
        return list;
    }
}
