package com.yida.data.school.secondClass.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassCourseContent;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassCourseLecturer;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.school.secondClass.mapper.EduSecondClassCourseContentMapper;
import com.yida.data.school.secondClass.mapper.EduSecondClassCourseLecturerMapper;
import com.yida.data.school.secondClass.service.EduSecondClassCourseLecturerService;
import com.yida.data.school.vo.secondClass.CourseLecturerVO;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * Service实现
 *
 * @author zhanghaijun
 * @date 2022-04-11 11:09:15
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS)
public class EduSecondClassCourseLecturerServiceImpl extends ServiceImpl
    <EduSecondClassCourseLecturerMapper, EduSecondClassCourseLecturer> implements EduSecondClassCourseLecturerService {

    private final EduSecondClassCourseContentMapper eduSecondClassCourseContentMapper;

    @Override
    public IPage<CourseLecturerVO> listLecturerPage(Integer pageNum, Integer pageSize, String name, Long deptId) {
        Page<CourseLecturerVO> res = Page.of(pageNum, pageSize);
        List<CourseLecturerVO> list = baseMapper.listLecturer(res, name, deptId);
        res.setRecords(list);
        return res;
    }

    @Override
    public List<CourseLecturerVO> listLecturer(Long deptId) {
        return baseMapper.listLecturer(null, null, deptId);
    }

    @Override
    public CourseLecturerVO getInfo(Long id) {
        return baseMapper.getInfo(id);
    }

    @Override
    public void delLecturer(Long id) {
        // 是否存在课程关联
        Asserts.isTrue(eduSecondClassCourseContentMapper
            .selectCount(Wrappers.<EduSecondClassCourseContent>lambdaQuery().eq(EduSecondClassCourseContent::getLecturerId, id))
            == 0, "改讲师已关联课程，请接触后再进行操作");
        removeById(id);
    }
}
