package com.ssj.service.sys.lesson.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.lesson.domain.LessonGroup;
import com.ssj.dao.sys.lesson.dao.LessonGroupDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.lesson.service.LessonGroupService;
import com.ssj.service.sys.lesson.service.LessonServiceService;
import com.ssj.bean.sys.lesson.domain.LessonService;
import java.util.List;

@Service(version = "1.0")
@Transactional
public class LessonGroupServiceImpl extends BaseServiceImpl<LessonGroup, String> implements LessonGroupService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LessonGroupDao lessonGroupDao;
    @Autowired
    private LessonServiceService lessonServiceService;

    @Override
    public PagingAndSortingRepository<LessonGroup, String> getDao() {
        return lessonGroupDao;
    }

    @Override
    public LessonGroup findByIdAndTeacherIdAndPlanStatus(String lessonGroupId, String teacherId, Integer planStatus) {
        return lessonGroupDao.findByIdAndTeacherIdAndPlanStatus(lessonGroupId, teacherId, planStatus);
    }

	@Override
	public LessonGroup findByIdAndMasterId(String lessonGroupId, String masterId) {
		return lessonGroupDao.findByIdAndMasterId(lessonGroupId,masterId);
	}

    @Override
    public void checkAndAdjustLessonGroup(String teacherId, String masterId) {
        //查询老师缺席的小课
        List<LessonGroup> lessonGroupList;
        if (teacherId != null && masterId == null) {//老师端小课列表触发
            lessonGroupList = lessonGroupDao.findLessonGroupTeacherAbsentByTeacherId(teacherId);
        } else {//校长端小课列表触发
            lessonGroupList = lessonGroupDao.findLessonGroupTeacherAbsentByMasterId(masterId);
        }

        if (lessonGroupList!=null && !lessonGroupList.isEmpty()) {//老师存在缺席不上课现象，处理小课小组当前课次
            lessonGroupList.forEach(lessonGroup -> {
                logger.warn(String.format("小组（id=%s）第%s次课程，老师缺席未上课", lessonGroup.getId(), lessonGroup.getCurrentCount()));

                List<LessonService> lessonServiceList = lessonServiceService.findByLessonGroupId(lessonGroup.getId());

                if (lessonGroup.getCurrentCount() < lessonGroup.getCourseCount()) {//不是最后一节课，调整当前课次为下一节课程
                    lessonServiceList.forEach(lessonService -> {
                        lessonService.setCurrentCount(lessonGroup.getCurrentCount() + 1);
                        lessonServiceService.save(lessonService);
                    });

                    lessonGroup.setCurrentCount(lessonGroup.getCurrentCount() + 1);
                    lessonGroup.setHandoutTime(null);

                    if (lessonGroup.getConfirmTime() == null) {//未确认上课意向
                        lessonGroup.setPlanStatus(1);
                    } else {
                        lessonGroup.setPlanStatus(2);
                    }
                } else {//更新小组内所有学生的订单为已完结
                    lessonServiceList.forEach(lessonService -> {
                        lessonService.setStatus(2);
                        lessonServiceService.save(lessonService);
                    });
                    lessonGroup.setIsFinish(1);
                }

                lessonGroupDao.save(lessonGroup);
            });
        }
    }

    @Override
    public List<LessonGroup> findByVipId(String vipId) {
        return lessonGroupDao.findByVipId(vipId);
    }

}
