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

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.menu.mapper.MenuRecipeDetailMapper;
import com.bonus.canteen.core.menu.domain.MenuRecipeDetail;
import com.bonus.canteen.core.menu.service.IMenuRecipeDetailService;

/**
 * 菜品计划详情信息Service业务层处理
 *
 * @author xsheng
 * @date 2025-04-03
 */
@Service
public class MenuRecipeDetailServiceImpl extends ServiceImpl<MenuRecipeDetailMapper, MenuRecipeDetail> implements IMenuRecipeDetailService {
    @Autowired
    private MenuRecipeDetailMapper menuRecipeDetailMapper;

    /**
     * 查询菜品计划详情信息
     *
     * @param id 菜品计划详情信息主键
     * @return 菜品计划详情信息
     */
    @Override
    public MenuRecipeDetail selectMenuRecipeDetailById(Long id) {
        return menuRecipeDetailMapper.selectMenuRecipeDetailById(id);
    }

    /**
     * 查询菜品计划详情信息列表
     *
     * @param menuRecipeDetail 菜品计划详情信息
     * @return 菜品计划详情信息
     */
    @Override
    public List<MenuRecipeDetail> selectMenuRecipeDetailList(MenuRecipeDetail menuRecipeDetail) {
        return menuRecipeDetailMapper.selectMenuRecipeDetailList(menuRecipeDetail);
    }

    /**
     * 新增菜品计划详情信息
     *
     * @param menuRecipeDetail 菜品计划详情信息
     * @return 结果
     */
    @Override
    public int insertMenuRecipeDetail(MenuRecipeDetail menuRecipeDetail) {
        menuRecipeDetail.setCreateTime(LocalDateTime.now());
        try {
            return menuRecipeDetailMapper.insertMenuRecipeDetail(menuRecipeDetail);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 修改菜品计划详情信息
     *
     * @param menuRecipeDetail 菜品计划详情信息
     * @return 结果
     */
    @Override
    public int updateMenuRecipeDetail(MenuRecipeDetail menuRecipeDetail) {
        menuRecipeDetail.setUpdateTime(LocalDateTime.now());
        try {
            return menuRecipeDetailMapper.updateMenuRecipeDetail(menuRecipeDetail);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 批量删除菜品计划详情信息
     *
     * @param ids 需要删除的菜品计划详情信息主键
     * @return 结果
     */
    @Override
    public int deleteMenuRecipeDetailByIds(Long[] ids) {
        return menuRecipeDetailMapper.deleteMenuRecipeDetailByIds(ids);
    }

    /**
     * 删除菜品计划详情信息信息
     *
     * @param id 菜品计划详情信息主键
     * @return 结果
     */
    @Override
    public int deleteMenuRecipeDetailById(Long id) {
        return menuRecipeDetailMapper.deleteMenuRecipeDetailById(id);
    }

    @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))));
    }
}
