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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueGuide;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueGuideSchedule;
import com.yida.data.school.dto.secondClass.ListVenueGuideDTO;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueGuideMapper;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueGuideScheduleMapper;
import com.yida.data.school.secondClass.service.EduSecondClassVenueGuideService;
import com.yida.data.school.vo.secondClass.VenueGuideVO;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
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(rollbackFor = Exception.class)
public class EduSecondClassVenueGuideServiceImpl extends ServiceImpl
    <EduSecondClassVenueGuideMapper, EduSecondClassVenueGuide> implements EduSecondClassVenueGuideService {

    private final EduSecondClassVenueGuideScheduleMapper eduSecondClassVenueGuideScheduleMapper;

    @Override
    public IPage<VenueGuideVO> listGuidePage(ListVenueGuideDTO dto) {
        Page page = dto.toPage();
        page.setRecords(baseMapper.listGuide(page, dto));
        return page;
    }

    @Override
    public void saveGuide(EduSecondClassVenueGuide guide) {
        saveOrUpdate(guide);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        EduSecondClassVenueGuide guide = new EduSecondClassVenueGuide();
        guide.setId(id);
        guide.setStatus(status);
        updateById(guide);
    }

    @Override
    public VenueGuideVO getInfo(Long id) {
        VenueGuideVO vo = new VenueGuideVO();
        EduSecondClassVenueGuide guide = getById(id);
        if (guide != null) {
            BeanUtils.copyProperties(guide, vo);
        }
        return vo;
    }

    @Override
    public void delGuide(Long id) {
        removeById(id);
        eduSecondClassVenueGuideScheduleMapper.delete(
            Wrappers.<EduSecondClassVenueGuideSchedule>lambdaQuery().eq(EduSecondClassVenueGuideSchedule::getGuideId, id));
    }

    @Override
    public List<VenueGuideVO> listGuide(String time, Integer date, Long venueId) {
        List<EduSecondClassVenueGuide> guideList = list(Wrappers.<EduSecondClassVenueGuide>lambdaQuery()
            .eq(EduSecondClassVenueGuide::getVenueId, venueId)
            .eq(EduSecondClassVenueGuide::getStatus, 0));
        if (CollUtil.isNotEmpty(guideList)) {
            List<VenueGuideVO> res = new ArrayList<>();
            List<Long> idList = guideList.stream().map(x -> x.getId()).collect(Collectors.toList());
            List<Long> workGuideIdList = eduSecondClassVenueGuideScheduleMapper.selectList(
                Wrappers.<EduSecondClassVenueGuideSchedule>lambdaQuery()
                    .in(EduSecondClassVenueGuideSchedule::getGuideId, idList)
                    .eq(EduSecondClassVenueGuideSchedule::getWorkDate, date)
                    .eq(EduSecondClassVenueGuideSchedule::getWorkTime, time)
                    .select(EduSecondClassVenueGuideSchedule::getGuideId)).stream().map(x -> x.getGuideId())
                .collect(Collectors.toList());
            if (CollUtil.isNotEmpty(workGuideIdList)) {
                // 工作人群先放
                res.addAll(guideList.stream().filter(x -> workGuideIdList.contains(x.getId())).map(x -> {
                    VenueGuideVO vo = new VenueGuideVO();
                    BeanUtils.copyProperties(x, vo);
                    vo.setWorkStatus(0);
                    return vo;
                }).collect(Collectors.toList()));
                // 不工作人群后方
                res.addAll(guideList.stream().filter(x -> !workGuideIdList.contains(x.getId())).map(x -> {
                    VenueGuideVO vo = new VenueGuideVO();
                    BeanUtils.copyProperties(x, vo);
                    vo.setWorkStatus(1);
                    return vo;
                }).collect(Collectors.toList()));
            } else {
                res.addAll(guideList.stream().map(x -> {
                    VenueGuideVO vo = new VenueGuideVO();
                    BeanUtils.copyProperties(x, vo);
                    vo.setWorkStatus(1);
                    return vo;
                }).collect(Collectors.toList()));
            }
            return res;
        }
        return null;
    }
}
