package com.qmth.teachcloud.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.common.entity.BasicTeachCourse;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.mapper.BasicTeachCourseMapper;
import com.qmth.teachcloud.common.service.BasicTeachCourseService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 老师和课程关联表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-04-24
 */
@Service
public class BasicTeachCourseServiceImpl extends ServiceImpl<BasicTeachCourseMapper, BasicTeachCourse> implements BasicTeachCourseService {

    /**
     * 插入老师和课程关联表
     *
     * @param courseId
     * @param teachIdSet
     */
    @Override
    @Transactional
    public void updateByCourseIdAndTeacherList(Long courseId, Set<Long> teachIdSet) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        List<BasicTeachCourse> basicTeachCourseList = null;
        // 删除原有的全部
        this.remove(new QueryWrapper<BasicTeachCourse>().lambda().eq(BasicTeachCourse::getCourseId, courseId).gt(BasicTeachCourse::getId, 0));
        // 新增
        if (!CollectionUtils.isEmpty(teachIdSet)) {
            basicTeachCourseList = new ArrayList<>();
            for (Long teachId : teachIdSet) {
                basicTeachCourseList.add(new BasicTeachCourse(sysUser, courseId, teachId));
            }
            this.saveBatch(basicTeachCourseList);
        }
    }
}
