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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenue;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueGuide;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueGuideSchedule;
import com.yida.data.common.core.vo.NameValueVO;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueGuideMapper;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueGuideScheduleMapper;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueMapper;
import com.yida.data.school.secondClass.service.EduSecondClassVenueGuideScheduleService;
import com.yida.data.school.vo.secondClass.VunueGuideScheduleVO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
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-21 10:10:35
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduSecondClassVenueGuideScheduleServiceImpl extends ServiceImpl
    <EduSecondClassVenueGuideScheduleMapper, EduSecondClassVenueGuideSchedule> implements
    EduSecondClassVenueGuideScheduleService {

    private final EduSecondClassVenueMapper eduSecondClassVenueMapper;
    private final EduSecondClassVenueGuideMapper eduSecondClassVenueGuideMapper;

    @Override
    public void saveSchedule(List<EduSecondClassVenueGuideSchedule> scheduleList) {
        remove(Wrappers.<EduSecondClassVenueGuideSchedule>lambdaQuery()
            .eq(EduSecondClassVenueGuideSchedule::getGuideId, scheduleList.get(0).getGuideId()));
        saveBatch(scheduleList);
    }

    @Override
    public List<List<String>> getSchedule(Long guideId) {
        VunueGuideScheduleVO vo = new VunueGuideScheduleVO();
        EduSecondClassVenueGuide guide = eduSecondClassVenueGuideMapper.selectById(guideId);
        EduSecondClassVenue venue = eduSecondClassVenueMapper.selectById(guide.getVenueId());
        List<String> workDate = venue.getOpenDate() != null ? Arrays.asList(venue.getOpenDate().split(",")) : null;
        List<String> workTime = venue.getOpenTime() != null ? Arrays.asList(venue.getOpenTime().split(",")) : null;
        vo.setWorkDate(workDate);
        vo.setWorkTime(workTime);
        List<EduSecondClassVenueGuideSchedule> list = list(
            Wrappers.<EduSecondClassVenueGuideSchedule>lambdaQuery().eq(EduSecondClassVenueGuideSchedule::getGuideId, guideId));

        List<List<String>> scheduleList = new ArrayList<>();
        // 组装数据
        if (CollUtil.isNotEmpty(workTime)) {
            Map<String, List<EduSecondClassVenueGuideSchedule>> dateTimeGroup = list.stream()
                .collect(Collectors.groupingBy(EduSecondClassVenueGuideSchedule::getWorkTime));
            // 遍历时间生成每一行数据
            for (String time : workTime) {
                List<String> rowData = new ArrayList<>();
                rowData.add(time);
                List<Integer> dateList =
                    dateTimeGroup != null && dateTimeGroup.containsKey(time) ? dateTimeGroup.get(time).stream()
                        .map(EduSecondClassVenueGuideSchedule::getWorkDate)
                        .collect(Collectors.toList()) : new ArrayList<>();
                for (int i = 1; i < 8; i++) {
                    if (workDate != null && workDate.contains(String.valueOf(i))) {
                        if (dateList.contains(i)) {
                            // 上班
                            rowData.add("0");
                        } else {
                            // 休息
                            rowData.add("1");
                        }
                    } else {
                        // 不开放
                        rowData.add("2");
                    }
                }
                scheduleList.add(rowData);
            }
        }
        return scheduleList;
    }
}
