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


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.classschedule.EduClassScheduleTimeModel;
import com.yida.data.common.core.entity.classschedule.EduClassScheduleTimeModelDetail;
import com.yida.data.school.classSchedule.mapper.EduClassScheduleTimeModelMapper;
import com.yida.data.school.classSchedule.service.EduClassScheduleTimeModelDetailService;
import com.yida.data.school.classSchedule.service.EduClassScheduleTimeModelService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

/**
 * 课程表时间安排模板 Service实现
 *
 * @author wjm
 * @date 2021-08-13 16:53:42
 */
@Service
@RequiredArgsConstructor
@Transactional
public class EduClassScheduleTimeModelServiceImpl extends ServiceImpl
        <EduClassScheduleTimeModelMapper, EduClassScheduleTimeModel> implements EduClassScheduleTimeModelService {

    private final EduClassScheduleTimeModelDetailService eduClassScheduleTimeModelDetailService;

    @Override
    public void saveModel(EduClassScheduleTimeModel model) {
        // 删除时间安排
        if (model.getId() != null) {
            eduClassScheduleTimeModelDetailService.remove(Wrappers.lambdaQuery(new EduClassScheduleTimeModelDetail())
                    .eq(EduClassScheduleTimeModelDetail::getModelId, model.getId()));
        }
        saveOrUpdate(model);
        // 添加新的时间安排
        if (CollUtil.isNotEmpty(model.getDetailList())) {
            for (EduClassScheduleTimeModelDetail detail : model.getDetailList()) {
                detail.setId(null);
                detail.setCreateId(null);
                detail.setCreateDate(null);
                detail.setUpdateDate(null);
                detail.setUpdateId(null);
                detail.setModelId(model.getId());
            }
            eduClassScheduleTimeModelDetailService.saveBatch(model.getDetailList());
        }
    }
}
