package cn.iocoder.yudao.module.base.service.impl;

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.base.entity.DlDriveSchoolCoach;
import cn.iocoder.yudao.module.base.entity.DlDriveSchoolCoachCourse;
import cn.iocoder.yudao.module.base.mapper.DlDriveSchoolCoachCourseMapper;
import cn.iocoder.yudao.module.base.service.DlDriveSchoolCoachCourseService;
import cn.iocoder.yudao.module.base.vo.DlDriveSchoolCoachVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

/**
 * 驾校教练 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class DlDriveSchoolCoachCourseServiceImpl extends ServiceImpl<DlDriveSchoolCoachCourseMapper, DlDriveSchoolCoachCourse> implements DlDriveSchoolCoachCourseService {

    /**
     * 保存课程与教练关联关系
     *
     * @param courseId   课程id
     * @param courseName 课程名称
     * @param courseType 教练类型
     * @param coachList  教练集合
     * @author PQZ
     * @date 17:14 2025/2/7
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCoachCourse(String courseId, String courseName, String courseType, List<DlDriveSchoolCoachVO> coachList) {
        List<DlDriveSchoolCoachCourse> saveList = new ArrayList<>();
        //删除与课程关联的教练
        removeByCourseId(courseId);
        //初始化教练与课程关联数据
        if (!coachList.isEmpty()){
            coachList.forEach(item -> {
                DlDriveSchoolCoachCourse coachCourse = new DlDriveSchoolCoachCourse();
                coachCourse.setCoachId(item.getCoachId());
                coachCourse.setCoachName(item.getCoachName());
                coachCourse.setCoachPhone(item.getCoachPhone());
                //教练在本课程中的任教科目
                coachCourse.setSubject(item.getSubject());
                coachCourse.setCourseId(courseId);
                coachCourse.setCourseName(courseName);
                coachCourse.setCourseType(courseType);
                saveList.add(coachCourse);
            });
        }
        //批量保存
        saveBatch(saveList);
    }

    /**
     * 通过课程id删除课程与教练关联关系
     *
     * @param courseId 课程id
     * @author PQZ
     * @date 17:35 2025/2/7
     **/
    @Override
    public void removeByCourseId(String courseId) {
        LambdaQueryWrapper<DlDriveSchoolCoachCourse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlDriveSchoolCoachCourse::getCourseId,courseId);
        remove(lambdaQueryWrapper);
    }

    /**
     * 通过课程id查询课程与教练关联关系
     *
     * @param courseId 课程id
     * @return java.util.List<cn.iocoder.yudao.module.base.entity.DlDriveSchoolCoachCourse>
     * @author PQZ
     * @date 14:17 2025/2/8
     **/
    @Override
    public List<DlDriveSchoolCoachCourse> queryByCourseId(String courseId) {
        LambdaQueryWrapper<DlDriveSchoolCoachCourse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlDriveSchoolCoachCourse::getCourseId,courseId).eq(BaseDO::getDeleted,0).orderByDesc(BaseDO::getCreateTime);
        return list(lambdaQueryWrapper);
    }
}