package com.ruoyi.hezhi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbCaseType;
import com.ruoyi.hezhi.domain.TbTeacher;
import com.ruoyi.hezhi.domain.vo.CourseCardVO;
import com.ruoyi.hezhi.domain.vo.TeacherDetailVO;
import com.ruoyi.hezhi.domain.vo.TeacherVO;
import com.ruoyi.hezhi.mapper.TbTeacherMapper;
import com.ruoyi.hezhi.service.ITbCaseTypeService;
import com.ruoyi.hezhi.service.ITbTeacherCourseService;
import com.ruoyi.hezhi.service.ITbTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 教师Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@Service
public class TbTeacherServiceImpl implements ITbTeacherService
{
    @Autowired
    private TbTeacherMapper tbTeacherMapper;

    @Resource
    private ITbCaseTypeService caseTypeService;

    @Resource
    private ITbTeacherCourseService teacherCourseService;


    /**
     * 查询教师
     *
     * @param teacherId 教师主键
     * @return 教师
     */
    @Override
    public TbTeacher selectTbTeacherByTeacherId(Long teacherId)
    {
        return tbTeacherMapper.selectTbTeacherByTeacherId(teacherId);
    }

    /**
     * 查询教师列表
     *
     * @param tbTeacher 教师
     * @return 教师
     */
    @Override
    public List<TbTeacher> selectTbTeacherList(TbTeacher tbTeacher)
    {
        return tbTeacherMapper.selectTbTeacherList(tbTeacher);
    }

    /**
     * 查询师资课程列表
     *
     * @param params 教师
     * @return 教师
     */
    @Override
    public List<Map> selectTbTeacherCourseList(Map<String, Object> params)
    {
        return tbTeacherMapper.selectTbTeacherCourseList(params);
    }

    /**
     * 新增教师
     *
     * @param tbTeacher 教师
     * @return 结果
     */
    @Override
    public int insertTbTeacher(TbTeacher tbTeacher)
    {
        tbTeacher.setCreateTime(DateUtils.getNowDate());
        return tbTeacherMapper.insertTbTeacher(tbTeacher);
    }

    /**
     * 修改教师
     *
     * @param tbTeacher 教师
     * @return 结果
     */
    @Override
    public int updateTbTeacher(TbTeacher tbTeacher)
    {
        tbTeacher.setUpdateTime(DateUtils.getNowDate());
        return tbTeacherMapper.updateTbTeacher(tbTeacher);
    }

    /**
     * 批量删除教师
     *
     * @param teacherIds 需要删除的教师主键
     * @return 结果
     */
    @Override
    public int deleteTbTeacherByTeacherIds(Long[] teacherIds)
    {
        return tbTeacherMapper.deleteTbTeacherByTeacherIds(teacherIds);
    }

    /**
     * 删除教师信息
     *
     * @param teacherId 教师主键
     * @return 结果
     */
    @Override
    public int deleteTbTeacherByTeacherId(Long teacherId)
    {
        return tbTeacherMapper.deleteTbTeacherByTeacherId(teacherId);
    }

    /**
     * 教师list
     * @param typeId 类型id 0全部
     * @return
     */
    @Override
    public List<TeacherVO> selectTeacherListByType(Long typeId) {
        if(null != typeId && 0 != typeId ){
            TbCaseType tbCaseType = caseTypeService.selectTbCaseTypeByCaseTypeId(typeId);
            if(ObjectUtils.isEmpty(tbCaseType)){
                throw new ServiceException("未查询到分类",500);
            }
            if(tbCaseType.getShowStatus() == 1){
                throw new ServiceException("未查询到分类",500);
            }
        }
        return tbTeacherMapper.selectTeacherList(typeId);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public TeacherDetailVO getDetail(Long id) {
        TeacherDetailVO result = new TeacherDetailVO();
        TbTeacher detail = tbTeacherMapper.selectTbTeacherByTeacherId(id);
        TeacherVO vo = BeanUtil.toBean(detail,TeacherVO.class);
        if(ObjectUtils.isEmpty(vo)){
            throw new ServiceException("查询教师详情异常",500);
        }
        result.setTeacher(vo);

        //主讲课程
        List<CourseCardVO> mainCourseList = teacherCourseService.selectTeacherCourseList(id, 1);
        if(!CollectionUtils.isEmpty(mainCourseList)){
            result.setMainCourse(mainCourseList);
        }
        //辅讲课程
        List<CourseCardVO> subCourseList = teacherCourseService.selectTeacherCourseList(id, 0);
        if(!CollectionUtils.isEmpty(subCourseList)){
            result.setSubCourse(subCourseList);
        }
        return result;
    }

    /**
     * 师资团队列表
     *
     * @param params 参数
     * @return 师资团队列表
     */
    @Override
    public List<Map<String, Object>> getTeacherList(Map<String, Object> params) {
        return tbTeacherMapper.getTeacherList(params);
    }

    /**
     * 老师详情
     *
     * @param params 参数
     * @return 老师详情
     */
    @Override
    public Map<String, Object> getTeacherInfo(Map<String, Object> params) {
        return tbTeacherMapper.getTeacherInfo(params);
    }
}
