package com.bonus.core.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.core.menu.entity.MenuRecipeDetail;
import com.bonus.core.menu.mapper.MenuRecipeDetailMapper;
import com.bonus.core.menu.service.MenuRecipeDetailService;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Service
public class MenuRecipeDetailServiceImpl extends ServiceImpl<MenuRecipeDetailMapper, MenuRecipeDetail> implements MenuRecipeDetailService {
    @Override
    public List<MenuRecipeDetail> getByRecipeIdAndDatesAndIntervalId(Long recipeId, Set<LocalDate> values, Integer mealtimeType) {
        return this.list(Wrappers.<MenuRecipeDetail>lambdaQuery()
                .eq(MenuRecipeDetail::getMealtimeType, mealtimeType)
                .eq(MenuRecipeDetail::getRecipeId, recipeId).in(MenuRecipeDetail::getApplyDate, values));
    }

    @Override
    public MenuRecipeDetail getByRecipeIdAndApplyDateAndIntervalId(Long recipeId, LocalDate applyDate, Integer mealtimeType) {
        return (MenuRecipeDetail) this.getOne(Wrappers.<MenuRecipeDetail>lambdaQuery()
                .eq(MenuRecipeDetail::getMealtimeType, mealtimeType)
                .eq(MenuRecipeDetail::getRecipeId, recipeId)
                .eq(MenuRecipeDetail::getApplyDate, applyDate));
    }

    @Override
    public Map<Long, Set<LocalDate>> selectApplyDateListByRecipeId(List<Long> recipeIds) {
        return this.list(Wrappers.<MenuRecipeDetail>lambdaQuery()
                        .select(MenuRecipeDetail::getRecipeId, MenuRecipeDetail::getApplyDate)
                        .ge(MenuRecipeDetail::getApplyDate, LocalDate.now())
                        .in(MenuRecipeDetail::getRecipeId, recipeIds)).stream()
                .collect(Collectors.groupingBy(MenuRecipeDetail::getRecipeId, Collectors.mapping(MenuRecipeDetail::getApplyDate, Collectors.toCollection(TreeSet::new))));
    }

}
