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

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

import cn.hutool.core.lang.UUID;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.exam.questions.mapper.ExamQuestionsBankMapper;
import com.yanzhu.exam.questions.domain.ExamQuestionsBank;
import com.yanzhu.exam.questions.service.IExamQuestionsBankService;

/**
 * 题库管理Service业务层处理
 * 
 * @author qnsdt
 * @date 2022-12-19
 */
@Service
public class ExamQuestionsBankServiceImpl implements IExamQuestionsBankService 
{
    @Autowired
    private ExamQuestionsBankMapper examQuestionsBankMapper;

    /**
     * 查询题库管理
     * 
     * @param bankCode 题库管理ID
     * @return 题库管理
     */
    @Override
    public ExamQuestionsBank selectExamQuestionsBankById(String bankCode)
    {
        return examQuestionsBankMapper.selectExamQuestionsBankById(bankCode);
    }

    /**
     * 查询题库管理列表
     * 
     * @param examQuestionsBank 题库管理
     * @return 题库管理
     */
    @Override
    public List<ExamQuestionsBank> selectExamQuestionsBankList(ExamQuestionsBank examQuestionsBank)
    {
        return examQuestionsBankMapper.selectExamQuestionsBankList(examQuestionsBank);
    }

    /**
     * 新增题库管理
     * 
     * @param examQuestionsBank 题库管理
     * @return 结果
     */
    @Override
    public int insertExamQuestionsBank(ExamQuestionsBank examQuestionsBank)
    {
        examQuestionsBank.setCreateTime(DateUtils.getNowDate());
        return examQuestionsBankMapper.insertExamQuestionsBank(examQuestionsBank);
    }

    /**
     * 修改题库管理
     * 
     * @param examQuestionsBank 题库管理
     * @return 结果
     */
    @Override
    public int updateExamQuestionsBank(ExamQuestionsBank examQuestionsBank)
    {
        return examQuestionsBankMapper.updateExamQuestionsBank(examQuestionsBank);
    }

    /**
     * 批量删除题库管理
     * 
     * @param bankCodes 需要删除的题库管理ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionsBankByIds(String[] bankCodes)
    {
        return examQuestionsBankMapper.deleteExamQuestionsBankByIds(bankCodes);
    }

    /**
     * 删除题库管理信息
     * 
     * @param bankCode 题库管理ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionsBankById(String bankCode)
    {
        return examQuestionsBankMapper.deleteExamQuestionsBankById(bankCode);
    }

	@Override
	public List<ExamQuestionsBank> selectExamQuestionsBankListByCode(String[] bankCodes) {
		// TODO Auto-generated method stub
		return examQuestionsBankMapper.selectExamQuestionsBankListByCode(bankCodes);
	}

    /**
     * 导入题库数据
     * 
     * @param examQuestionsBankList 题库数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importExamQuestionsBank(List<ExamQuestionsBank> examQuestionsBankList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(examQuestionsBankList) || examQuestionsBankList.size() == 0)
        {
            throw new CustomException("导入题库数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ExamQuestionsBank examQuestionsBank : examQuestionsBankList)
        {
            try
            {
                // 验证是否存在这个题库
                ExamQuestionsBank b = examQuestionsBankMapper.selectExamQuestionsBankByName(examQuestionsBank.getBankName());
                if (StringUtils.isNull(b))
                {
                    examQuestionsBank.setBankCode(UUID.randomUUID().toString());
                    examQuestionsBank.setCreateBy(operName);
                    examQuestionsBank.setCreateTime(DateUtils.getNowDate());
                    this.insertExamQuestionsBank(examQuestionsBank);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、题库 " + examQuestionsBank.getBankName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    examQuestionsBank.setBankCode(b.getBankCode());
                    examQuestionsBank.setUpdateBy(operName);
                    this.updateExamQuestionsBank(examQuestionsBank);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、题库 " + examQuestionsBank.getBankName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、题库 " + examQuestionsBank.getBankName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、题库 " + examQuestionsBank.getBankName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
