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.TbExamNotice;
import com.ruoyi.hezhi.mapper.TbExamNoticeMapper;
import com.ruoyi.hezhi.service.ITbExamNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 考试通知Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-16
 */
@Service
public class TbExamNoticeServiceImpl implements ITbExamNoticeService
{
    @Autowired
    private TbExamNoticeMapper tbExamNoticeMapper;

    /**
     * 查询考试通知
     *
     * @param examNoticeId 考试通知主键
     * @return 考试通知
     */
    @Override
    public TbExamNotice selectTbExamNoticeByExamNoticeId(Long examNoticeId)
    {
        return tbExamNoticeMapper.selectTbExamNoticeByExamNoticeId(examNoticeId);
    }

    /**
     * 查询考试通知列表
     *
     * @param tbExamNotice 考试通知
     * @return 考试通知
     */
    @Override
    public List<TbExamNotice> selectTbExamNoticeList(TbExamNotice tbExamNotice)
    {
        return tbExamNoticeMapper.selectTbExamNoticeList(tbExamNotice);
    }

    /**
     * 新增考试通知
     *
     * @param tbExamNotice 考试通知
     * @return 结果
     */
    @Override
    public int insertTbExamNotice(TbExamNotice tbExamNotice)
    {
        //先根据examId和batchIndex查询，如果存在则提示该批次的考试通知已存在
        TbExamNotice paramDto = new TbExamNotice();
        paramDto.setExamId(tbExamNotice.getExamId());
        paramDto.setBatchIndex(tbExamNotice.getBatchIndex());
        List<TbExamNotice> list = tbExamNoticeMapper.selectTbExamNoticeList(paramDto);
        if (!list.isEmpty()) {
            throw new ServiceException("该批次的考试通知已存在");
        }
        tbExamNotice.setCreateTime(DateUtils.getNowDate());
        return tbExamNoticeMapper.insertTbExamNotice(tbExamNotice);
    }

    /**
     * 修改考试通知
     *
     * @param tbExamNotice 考试通知
     * @return 结果
     */
    @Override
    public int updateTbExamNotice(TbExamNotice tbExamNotice)
    {
        tbExamNotice.setUpdateTime(DateUtils.getNowDate());
        return tbExamNoticeMapper.updateTbExamNotice(tbExamNotice);
    }

    /**
     * 批量删除考试通知
     *
     * @param examNoticeIds 需要删除的考试通知主键
     * @return 结果
     */
    @Override
    public int deleteTbExamNoticeByExamNoticeIds(Long[] examNoticeIds)
    {
        return tbExamNoticeMapper.deleteTbExamNoticeByExamNoticeIds(examNoticeIds);
    }

    /**
     * 删除考试通知信息
     *
     * @param examNoticeId 考试通知主键
     * @return 结果
     */
    @Override
    public int deleteTbExamNoticeByExamNoticeId(Long examNoticeId)
    {
        return tbExamNoticeMapper.deleteTbExamNoticeByExamNoticeId(examNoticeId);
    }

    /**
     * 考试通知列表
     *
     * @param params 参数
     * @return 考试通知列表
     */
    @Override
    public List<Map<String, Object>> getExamNoticeList(Map<String, Object> params) {
        return tbExamNoticeMapper.getExamNoticeList(params);

    }

    /**
     * 考试通知详情
     *
     * @param params 参数
     * @return 考试通知详情
     */
    @Override
    public Map<String, Object> getExamNoticeInfo(Map<String, Object> params) {
        Map<String, Object> examNoticeInfo = tbExamNoticeMapper.getExamNoticeInfo(params);//todo 现在必须有考试通知才能查出考试时间
        if (CollUtil.isEmpty(examNoticeInfo)){
            throw new ServiceException("暂无对应考试通知，请联系老师");
        }
        return examNoticeInfo;
    }
}
