package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.TeacherFreeTimeDao;
import com.yonge.cooleshow.biz.dal.entity.HolidaysFestivals;
import com.yonge.cooleshow.biz.dal.entity.TeacherAuthEntryRecord;
import com.yonge.cooleshow.biz.dal.entity.TeacherFreeTime;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.service.CourseScheduleService;
import com.yonge.cooleshow.biz.dal.service.HolidaysFestivalsService;
import com.yonge.cooleshow.biz.dal.service.TeacherAuthEntryRecordService;
import com.yonge.cooleshow.biz.dal.service.TeacherFreeTimeService;
import com.yonge.cooleshow.biz.dal.vo.PracticeTeacherVo;
import com.yonge.cooleshow.biz.dal.wrapper.TeacherFreeTimeWrapper;
import com.yonge.toolset.utils.date.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 老师可排课时间表(TeacherFreeTime)表服务实现类
 * @author zx
 * @since 2022-03-25 10:04:27
 */
@Service("teacherFreeTimeService")
public class TeacherFreeTimeServiceImpl extends ServiceImpl<TeacherFreeTimeDao, TeacherFreeTime> implements TeacherFreeTimeService {

    private final static Logger log = LoggerFactory.getLogger(TeacherFreeTimeServiceImpl.class);

    @Resource
    private TeacherAuthEntryRecordService teacherAuthEntryRecordService;
    @Resource
    private HolidaysFestivalsService holidaysFestivalsService;
    @Resource
    private CourseScheduleService courseScheduleService;

    @Override
    public TeacherFreeTimeDao getDao() {
        return this.baseMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upSet(TeacherFreeTime teacherFreeTime) {
        Long teacherId = teacherFreeTime.getTeacherId();
        TeacherFreeTime oldTeacherFreeTime = baseMapper.selectOne(Wrappers.<TeacherFreeTime>lambdaQuery().eq(TeacherFreeTime::getTeacherId, teacherId));
        //删除原有设置
        if(Objects.nonNull(oldTeacherFreeTime)){
            teacherFreeTime.setRecentFreeTime(oldTeacherFreeTime.getRecentFreeTime());
            baseMapper.deleteById(oldTeacherFreeTime.getId());
        }
        baseMapper.insert(teacherFreeTime);
        //更新老师30天内是否有空
        this.updateTeacherFreeTime(teacherId);
    }

    @Override
    public IPage<PracticeTeacherVo> queryTeacherList(IPage<PracticeTeacherVo> page,TeacherFreeTimeWrapper.TeacherSearch query) {
        return page.setRecords(baseMapper.queryTeacherList(page, query));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTeacherFreeTime(Long teacherId) {
        //查询所有过审的老师时间配置
        TeacherAuthEntryRecord authEntryRecord = teacherAuthEntryRecordService.lambdaQuery()
                .eq(TeacherAuthEntryRecord::getUserId, teacherId)
                .eq(TeacherAuthEntryRecord::getTeacherAuthStatus, AuthStatusEnum.PASS).last("limit 1").one();
        if(Objects.isNull(authEntryRecord)){
            this.lambdaUpdate().eq(TeacherFreeTime::getTeacherId, teacherId)
                    .set(TeacherFreeTime::getRecentFreeTime, 0).update();
        }
        TeacherFreeTime freeTime = this.lambdaQuery().eq(TeacherFreeTime::getTeacherId, teacherId).last("limit 1").one();
        if(Objects.isNull(freeTime)){
            this.lambdaUpdate().eq(TeacherFreeTime::getTeacherId, teacherId)
                    .set(TeacherFreeTime::getRecentFreeTime, 0).update();
        }
        //未来30天日期
        List<String> futureDays = DateUtil.getFutureDays(30);
        if(freeTime.getSkipHolidayFlag()){
            //查询节假日
            HolidaysFestivals holidays = holidaysFestivalsService.queryByYear(Integer.parseInt(DateUtil.getYear(new Date())));
            String[] strs = holidays.getHolidaysFestivalsJson().split(",");
            List<String> holiday = Arrays.asList(strs);
            //未来30天日期-跳过节假日
            futureDays = futureDays.stream().filter(item -> !holiday.contains(item)).collect(Collectors.toList());
        }
        List<TeacherFreeTime> freeTimeList = new ArrayList<>();
        freeTimeList.add(freeTime);
        List<Long> teacherIdList = courseScheduleService.getAllTimeConfig(freeTimeList, futureDays, new ArrayList<>());
        if(CollectionUtils.isNotEmpty(teacherIdList)){
            this.lambdaUpdate().in(TeacherFreeTime::getTeacherId, teacherIdList)
                    .set(TeacherFreeTime::getRecentFreeTime, 1).update();
        }else {
            this.lambdaUpdate().eq(TeacherFreeTime::getTeacherId, teacherId)
                    .set(TeacherFreeTime::getRecentFreeTime, 0).update();
        }
    }
}

