package com.bonus.canteen.core.menu.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.menu.dto.IssueRecipeV2DTO;
import com.bonus.canteen.core.menu.dto.QueryRecipeTemplateDateDTO;
import com.bonus.canteen.core.menu.entity.MenuRecipeDetail;
import com.bonus.canteen.core.menu.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MenuRecipeDetailMapper extends BaseMapper<MenuRecipeDetail> {
    @Select({"select count(1) from menu_recipe_detail mrd inner join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id where mrd.detail_id = #{detailId} and mrdd.dishes_id = #{productId} and mrd.apply_date = #{applyDate} and mrd.mealtime_type = #{mealtimeType}"})
    Integer selectDishesCount(@Param("detailId") Long detailId, @Param("productId") Long productId, @Param("applyDate") LocalDate applyDate, @Param("mealtimeType") Integer mealtimeType);

    List<MenuRecipeDateVO> selectBetweenDateDetailList(@Param("recipeIds") List<Long> recipeIds, @Param("applyStartDate") LocalDate applyStartDate, @Param("applyEndDate") LocalDate applyEndDate);

    List<MenuRecipeDetailDateVO> selectTemplateDetailList(@Param("params") QueryRecipeTemplateDateDTO content);

    List<MenuRecipeWeekVO> selectWeekDetailList(@Param("recipeId") Long recipeId);

    @Select({"select detail_id from menu_recipe_detail ${ew.customSqlSegment}"})
    List<Long> selectDetailIdListByWrapper(@Param("ew") Wrapper<MenuRecipeDetail> wrapper);

    List<IssueRecipeDateCompressVO> selectIssueRecipeV2Compress(@Param("issueRecipeV2DTO") IssueRecipeV2DTO issueRecipeV2DTO);

    List<MenuRecipeDetailDateVO> selectDateDetailList(@Param("recipeId") Long recipeId, @Param("applyDate") LocalDate applyDate);

    List<MenuRecipeDateVO> selectTmpBetweenDateDetailList(@Param("recipeIds") List<Long> recipeIds, @Param("applyStartDate") LocalDate applyStartDate, @Param("applyEndDate") LocalDate applyEndDate);

    @Select({"select recipe_id, apply_date, crby, crtime from menu_recipe_detail where recipe_id = #{recipeId} and apply_date >= #{applyDate} group by recipe_id, apply_date, crby, crtime order by apply_date"})
    Page<MenuRecipeDetailV2PageVO> selectDetailDateByRecipeId(Page<MenuRecipeDetailV2PageVO> resultPage, @Param("recipeId") Long recipeId, @Param("applyDate") LocalDate applyDate);

    @Select({"select detail_id, mealtime_type from menu_recipe_detail ${ew.customSqlSegment}"})
    List<MenuRecipeDetail> selectSourceDetailByWrapper(@Param("ew") Wrapper<MenuRecipeDetail> wrapper);

    @Select({"select mealtime_type from menu_recipe_detail where recipe_id = #{recipeId} and apply_date = #{applyDate}"})
    List<Integer> selectIntervalNameListByRecipeId(@Param("recipeId") Long recipeId, @Param("applyDate") LocalDate applyDate);

    List<MenuRecipeDetailV2VO> selectRecipeDetailByIdPc(Long recipeId);

    List<IssueRecipeDateVO> selectIssueRecipeV2(@Param("issueRecipeV2DTO") IssueRecipeV2DTO issueRecipeV2DTO);

    @Select({"select max(uptime) from menu_recipe_detail where recipe_id = #{recipeId}"})
    LocalDateTime selectLastUptimeByRecipeId(Long recipeId);

    @Select({"select detail_id from menu_recipe_detail ${ew.customSqlSegment}"})
    Long selectDetailIdByWrapper(@Param("ew") Wrapper<MenuRecipeDetail> wrapper);

    List<AndroidProductionRecipeVO> selectProductionRecipe(@Param("recipeId") Long recipeId, @Param("applyDateList") List<LocalDate> applyDateList);

}
