package com.yanzhu.exam.exam.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.bean.BeanUtil;
import com.yanzhu.exam.onlineexam.domain.dto.ExamTitleData;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.exam.exam.mapper.ExamTaskManagerMapper;
import com.yanzhu.exam.exam.domain.ExamTaskManager;
import com.yanzhu.exam.exam.service.IExamTaskGroupService;
import com.yanzhu.exam.exam.service.IExamTaskManagerService;
import com.yanzhu.exam.exam.service.IExamTaskPersonService;
import com.yanzhu.exam.exam.service.IExamTaskQuestionsService;

/**
 * 创建考试Service业务层处理
 * 
 * @author qnsdt
 * @date 2023-01-06
 */
@Service
public class ExamTaskManagerServiceImpl implements IExamTaskManagerService 
{
    @Autowired
    private ExamTaskManagerMapper examTaskManagerMapper;

    @Autowired
    private IExamTaskGroupService examTaskGroupService;

    @Autowired
    private IExamTaskQuestionsService examTaskQuestionsService;

    @Autowired
    private IExamTaskPersonService examTaskPersonService;

    /**
     * 查询创建考试
     * 
     * @param examCode 创建考试ID
     * @return 创建考试
     */
    @Override
    public ExamTaskManager selectExamTaskManagerById(String examCode)
    {
        return examTaskManagerMapper.selectExamTaskManagerById(examCode);
    }

    /**
     * 查询创建考试列表
     * 
     * @param examTaskManager 创建考试
     * @return 创建考试
     */
    @Override
    public List<ExamTaskManager> selectExamTaskManagerList(ExamTaskManager examTaskManager)
    {
        return examTaskManagerMapper.selectExamTaskManagerList(examTaskManager);
    }

    /**
     * 新增创建考试
     * 
     * @param examTaskManager 创建考试
     * @return 结果
     */
    @Override
    public int insertExamTaskManager(ExamTaskManager examTaskManager)
    {
        examTaskManager.setCreateTime(DateUtils.getNowDate());
        return examTaskManagerMapper.insertExamTaskManager(examTaskManager);
    }

    /**
     * 修改创建考试
     * 
     * @param examTaskManager 创建考试
     * @return 结果
     */
    @Override
    public int updateExamTaskManager(ExamTaskManager examTaskManager)
    {
        return examTaskManagerMapper.updateExamTaskManager(examTaskManager);
    }

    /**
     * 批量删除创建考试
     * 
     * @param examCodes 需要删除的创建考试ID
     * @return 结果
     */
    @Override
    public int deleteExamTaskManagerByIds(String[] examCodes)
    {
    	// 删除题目
    	examTaskQuestionsService.deleteExamTaskQuestionsByIds(examCodes);
    	// 删除人员分组
    	examTaskGroupService.deleteExamTaskGroupByIds(examCodes);
    	// 删除人员
    	examTaskPersonService.deleteExamTaskPersonByIds(examCodes);
    	
        return examTaskManagerMapper.deleteExamTaskManagerByIds(examCodes);
    }

    /**
     * 删除创建考试信息
     * 
     * @param examCode 创建考试ID
     * @return 结果
     */
    @Override
    public int deleteExamTaskManagerById(String examCode)
    {
        return examTaskManagerMapper.deleteExamTaskManagerById(examCode);
    }

    /**
     * 获取当前用户可以参加的考试
     * @param userCode
     * @return
     */
    @Override
    public List<ExamTitleData> selectCurrentExam(String userCode) {
        //先根据当前时间和startDate和endDate过滤一下
        List<ExamTaskManager> list=examTaskManagerMapper.selectCurrentExam(userCode);
        List<ExamTitleData> retList=new ArrayList<>();
        List<String> examCodeList=new ArrayList<>();
        for(ExamTaskManager examTaskManager:list){
            if(examTaskManager.getExamType()==1){
                if(!checkCanRun(examTaskManager)){
                    continue;
                }
            }else{

            }
            ExamTitleData ed=new ExamTitleData();
            examCodeList.add(examTaskManager.getExamCode());
            // 使用 Hutool 的 BeanUtil.copyProperties 方法复制属性
            BeanUtil.copyProperties(examTaskManager, ed);
            retList.add(ed);
        }
        if(examCodeList.isEmpty()){
            return retList;
        }
        List<ExamTaskManager> listScore=examTaskManagerMapper.countExamScore(examCodeList);
        List<ExamTaskManager> listTypeCount=examTaskManagerMapper.groupExamTypeCount(examCodeList);
        for(ExamTitleData item :retList){
            List<ExamTaskManager> tmp=  listScore.stream().filter(d->d.getExamCode().equals(item.getExamCode())).collect(Collectors.toList());
            if(tmp.size()>0){
                item.setQuestionsScore(tmp.get(0).getExamType());
            }
            tmp=listTypeCount.stream().filter(d->d.getExamCode().equals(item.getExamCode()) && d.getExamId()==1).collect(Collectors.toList());
            if(tmp.size()>0){
                item.setJudgeNumber(tmp.get(0).getExamType());
            }
            tmp=listTypeCount.stream().filter(d->d.getExamCode().equals(item.getExamCode()) && d.getExamId()==2).collect(Collectors.toList());
            if(tmp.size()>0){
                item.setRadioNumber(tmp.get(0).getExamType());
            }
            tmp=listTypeCount.stream().filter(d->d.getExamCode().equals(item.getExamCode()) && d.getExamId()==3).collect(Collectors.toList());
            if(tmp.size()>0){
                item.setChoiceNumber(tmp.get(0).getExamType());
            }
        }
        return retList;
    }

    private boolean checkCanRun(ExamTaskManager examTaskManager) {
        //开始时间+考试时长 小于当前时间表示考试已发结束,大于表示还可能继续考试
        DateTime dtStart=new DateTime( examTaskManager.getStartTime());
        String sp=examTaskManager.getExamDuration();
        String[] timeParts = sp.split(":");
        int hours = Integer.parseInt(timeParts[0]);
        int minutes = Integer.parseInt(timeParts[1]);
        // 计算结束时间
        DateTime endDateTime = dtStart.offset(DateField.HOUR_OF_DAY, hours)
                .offset(DateField.MINUTE, minutes);
        return endDateTime.isAfter(DateUtils.getNowDate());

    }
}
