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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassCourseContent;
import com.yida.data.school.dto.secondClass.ListCourseContentDTO;
import com.yida.data.school.secondClass.mapper.EduSecondClassCourseContentMapper;
import com.yida.data.school.secondClass.service.EduSecondClassCourseContentService;
import com.yida.data.school.vo.secondClass.CourseContentVO;
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
public class EduSecondClassCourseContentServiceImpl extends ServiceImpl
    <EduSecondClassCourseContentMapper, EduSecondClassCourseContent> implements EduSecondClassCourseContentService {

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public IPage<CourseContentVO> listContentPage(ListCourseContentDTO dto) {
        return baseMapper.listContentPage(dto.toPage(), dto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveContent(EduSecondClassCourseContent content) {
        long maxSort = count(Wrappers.<EduSecondClassCourseContent>lambdaQuery()
            .eq(EduSecondClassCourseContent::getCourseId, content.getCourseId()));
        content.setSort((int) maxSort);
        save(content);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeLecturer(Long id, Long lecturerId) {
        EduSecondClassCourseContent content = new EduSecondClassCourseContent();
        content.setId(id);
        content.setLecturerId(lecturerId);
        updateById(content);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delContent(Long id) {
        EduSecondClassCourseContent one = getOne(
            Wrappers.<EduSecondClassCourseContent>lambdaQuery().eq(EduSecondClassCourseContent::getId, id)
                .select(EduSecondClassCourseContent::getCourseId, EduSecondClassCourseContent::getSort,
                    EduSecondClassCourseContent::getId));
        long maxSort = count(Wrappers.<EduSecondClassCourseContent>lambdaQuery()
            .eq(EduSecondClassCourseContent::getCourseId, one.getCourseId()));
        removeById(id);
        baseMapper.changeSort(one.getCourseId(), id, -1, one.getSort(), (int)maxSort);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeSort(Long id, Integer sortStep) {
        EduSecondClassCourseContent one = getOne(
            Wrappers.<EduSecondClassCourseContent>lambdaQuery().eq(EduSecondClassCourseContent::getId, id)
                .select(EduSecondClassCourseContent::getCourseId, EduSecondClassCourseContent::getSort,
                    EduSecondClassCourseContent::getId));
        Integer startSort = Math.min(one.getSort() + sortStep, one.getSort());
        Integer endSort = Math.max(one.getSort() + sortStep, one.getSort());
        one.setSort(one.getSort() + sortStep);
        updateById(one);
        baseMapper.changeSort(one.getCourseId(), id, sortStep > 0 ? -1 : 1, startSort, endSort);
    }
}
