package com.ruoyi.hezhi.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbExamCourse;
import com.ruoyi.hezhi.domain.TbSubjectWarehouseClass;
import com.ruoyi.hezhi.mapper.TbExamCourseMapper;
import com.ruoyi.hezhi.mapper.TbSubjectWarehouseClassMapper;
import com.ruoyi.hezhi.service.ITbSubjectWarehouseClassService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 题库分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-06
 */
@Service
public class TbSubjectWarehouseClassServiceImpl implements ITbSubjectWarehouseClassService
{
    @Autowired
    private TbSubjectWarehouseClassMapper tbSubjectWarehouseClassMapper;
    @Autowired
    private TbExamCourseMapper tbExamCourseMapper;

    /**
     * 查询题库分类
     *
     * @param subjectWarehouseClassId 题库分类主键
     * @return 题库分类
     */
    @Override
    public TbSubjectWarehouseClass selectTbSubjectWarehouseClassBySubjectWarehouseClassId(Long subjectWarehouseClassId)
    {
        TbSubjectWarehouseClass tbSubjectWarehouseClass = tbSubjectWarehouseClassMapper.selectTbSubjectWarehouseClassBySubjectWarehouseClassId(subjectWarehouseClassId);
        if (ObjectUtils.isNotEmpty(tbSubjectWarehouseClass)){
            List<TbExamCourse> examCourseList = tbExamCourseMapper.getExamCourseList(null, null, tbSubjectWarehouseClass.getSubjectWarehouseClassId(), null);
            tbSubjectWarehouseClass.setExamCourseList(examCourseList);
        }
        return tbSubjectWarehouseClass;
    }

    /**
     * 查询题库分类列表
     *
     * @param tbSubjectWarehouseClass 题库分类
     * @return 题库分类
     */
    @Override
    public List<TbSubjectWarehouseClass> selectTbSubjectWarehouseClassList(TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        return tbSubjectWarehouseClassMapper.selectTbSubjectWarehouseClassList(tbSubjectWarehouseClass);
    }

    /**
     * 查询题库分类列表
     *
     * @return 题库分类
     */
    @Override
    public List<TbSubjectWarehouseClass> getAllSubjectWarehouseCalssList()
    {
        return tbSubjectWarehouseClassMapper.getAllSubjectWarehouseCalssList();
    }

    /**
     * 新增题库分类
     *
     * @param tbSubjectWarehouseClass 题库分类
     * @return 结果
     */
    @Override
    public int insertTbSubjectWarehouseClass(TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        // 新增考试课程
        List<TbExamCourse> examCourseList = tbSubjectWarehouseClass.getExamCourseList();
        tbSubjectWarehouseClass.setCreateTime(DateUtils.getNowDate());
        tbSubjectWarehouseClass.setDelFlag(0);
        //先插入得到ID再插入关联表
        int i = tbSubjectWarehouseClassMapper.insertTbSubjectWarehouseClass(tbSubjectWarehouseClass);
        if (!examCourseList.isEmpty()){
            for (TbExamCourse tbExamCourse : examCourseList) {
                tbExamCourse.setSubjectWarehouseClassId(tbSubjectWarehouseClass.getSubjectWarehouseClassId());
                tbExamCourseMapper.insertTbExamCourse(tbExamCourse);
            }
        }
        return i;
    }

    /**
     * 修改题库分类
     *
     * @param tbSubjectWarehouseClass 题库分类
     * @return 结果
     */
    @Override
    public int updateTbSubjectWarehouseClass(TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        // 新增考试课程
        List<TbExamCourse> examCourseList = tbSubjectWarehouseClass.getExamCourseList();
        tbExamCourseMapper.deleteTbExamCourse(null, null, tbSubjectWarehouseClass.getSubjectWarehouseClassId(), null);
        if (!CollUtil.isEmpty(examCourseList)){
            for (TbExamCourse tbExamCourse : examCourseList) {
                tbExamCourse.setSubjectWarehouseClassId(tbSubjectWarehouseClass.getSubjectWarehouseClassId());
                tbExamCourseMapper.insertTbExamCourse(tbExamCourse);
            }
        }
        tbSubjectWarehouseClass.setUpdateTime(DateUtils.getNowDate());
        return tbSubjectWarehouseClassMapper.updateTbSubjectWarehouseClass(tbSubjectWarehouseClass);
    }

    /**
     * 批量删除题库分类
     *
     * @param subjectWarehouseClassIds 需要删除的题库分类主键
     * @return 结果
     */
    @Override
    public int deleteTbSubjectWarehouseClassBySubjectWarehouseClassIds(Long[] subjectWarehouseClassIds)
    {
        return tbSubjectWarehouseClassMapper.deleteTbSubjectWarehouseClassBySubjectWarehouseClassIds(subjectWarehouseClassIds);
    }

    /**
     * 删除题库分类信息
     *
     * @param subjectWarehouseClassId 题库分类主键
     * @return 结果
     */
    @Override
    public int deleteTbSubjectWarehouseClassBySubjectWarehouseClassId(Long subjectWarehouseClassId)
    {
        return tbSubjectWarehouseClassMapper.deleteTbSubjectWarehouseClassBySubjectWarehouseClassId(subjectWarehouseClassId);
    }

    /**
     * 题库列表
     *
     * @param params 参数
     * @return 题库列表
     */
    @Override
    public List<Map<String, Object>> getSubjectWarehouseClassList(Map<String, Object> params) {
        return tbSubjectWarehouseClassMapper.getSubjectWarehouseClassList(params);
    }

    @Override
    public TbSubjectWarehouseClass getSubjectWarehouseClass(Long courseId){
        TbExamCourse examCourseDetail = tbExamCourseMapper.getSubjectWarehouseClassIdByCourseId(courseId);
        if (ObjectUtils.isEmpty(examCourseDetail)){
            throw new ServiceException("管理员未上传该题库信息");
        }

        return tbSubjectWarehouseClassMapper.selectTbSubjectWarehouseClassBySubjectWarehouseClassId(examCourseDetail.getSubjectWarehouseClassId());
    }
}
