<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bonus.canteen.core.menu.mapper.MenuRecipeDetailMapper">
    <!-- 获取指定日期菜谱详情 -->
    <resultMap id="menuRecipeDateVO" type="com.bonus.canteen.core.menu.vo.MenuRecipeDateVO">
        <result property="day" column="apply_date"/>
        <result property="recipeId" column="recipe_id"/>
        <collection property="details" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDateVO">
            <result property="mealtimeType" column="mealtime_type"/>
            <result property="detailId" column="detail_id"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDishesVO">
                <result property="dishesId" column="dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="dishesId" column="dishesId"/>
                <result property="price" column="price"/>
                <result property="sizeType" column="size_type"/>
                <result property="supplyNum" column="supply_num"/>
                <result property="saleNum" column="sale_num"/>
                <result property="surplusNum" column="surplus_num"/>
                <result property="restrictNum" column="restrict_num"/>
                <result property="salePrice" column="sale_price"/>
                <result property="recommendFlag" column="recommend_flag"/>
                <result property="sortNum" column="sort_num"/>
                <result property="typeId" column="type_id"/>
                <result property="typeName" column="type_name"/>
                <result property="salesMode" column="sales_mode"/>
                <result property="canteenName" column="canteen_name"/>
                <result property="unitPrice" column="unit_price"/>
                <result property="mealType" column="meal_type"/>
                <result property="dishesNum" column="dishes_num"/>
                <result property="materialCost" column="material_cost"/>
            </collection>
        </collection>
    </resultMap>

    <!-- 获取指定日期菜谱详情 -->
    <resultMap id="menuRecipeDetailDateVO" type="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDateVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <result property="detailId" column="detail_id"/>
        <collection property="dishesList" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDishesVO">
            <result property="id" column="id"/>
            <result property="dishesId" column="dishes_id"/>
            <result property="dishesName" column="dishes_name"/>
            <result property="price" column="price"/>
            <result property="sizeType" column="size_type"/>
            <result property="supplyNum" column="supply_num"/>
            <result property="saleNum" column="sale_num"/>
            <result property="surplusNum" column="surplus_num"/>
            <result property="restrictNum" column="restrict_num"/>
            <result property="salePrice" column="sale_price"/>
            <result property="recommendFlag" column="recommend_flag"/>
            <result property="canteenName" column="canteen_name"/>
            <result property="salesMode" column="sales_mode"/>
            <result property="unitPrice" column="unit_price"/>
            <result property="mealType" column="meal_type"/>
            <result property="dishesNum" column="dishes_num"/>
            <result property="materialCost" column="material_cost"/>
            <result property="sortNum" column="sort_num"/>
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
        </collection>
    </resultMap>
    <!-- 获取指定日期菜谱详情 -->
    <resultMap id="menuRecipeWeekVO" type="com.bonus.canteen.core.menu.vo.MenuRecipeWeekVO">
        <result property="day" column="apply_week"/>
        <collection property="details" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDateVO">
            <result property="mealtimeType" column="mealtime_type"/>
            <result property="detailId" column="detail_id"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDishesVO">
                <result property="dishesId" column="dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="dishesId" column="dishesId"/>
                <result property="price" column="price"/>
                <result property="supplyNum" column="supply_num"/>
                <result property="saleNum" column="sale_num"/>
                <result property="surplusNum" column="surplus_num"/>
                <result property="restrictNum" column="restrict_num"/>
                <result property="sortNum" column="sort_num"/>
                <result property="typeId" column="type_id"/>
                <result property="typeName" column="type_name"/>
                <result property="salePrice" column="sale_price"/>
                <result property="recommendFlag" column="recommend_flag"/>
                <result property="mealType" column="meal_type"/>
                <result property="dishesNum" column="dishes_num"/>
                <result property="materialCost" column="material_cost"/>
            </collection>
        </collection>
    </resultMap>
    <resultMap id="issueRecipeDateCompressVO" type="com.bonus.canteen.core.menu.vo.IssueRecipeDateCompressVO">
        <result property="applyDate" column="apply_date"/>
        <collection property="intervalList" ofType="com.bonus.canteen.core.menu.vo.IssueRecipeIntervalCompressVO">
            <result property="intervalId" column="interval_id"/>
            <collection property="productList" ofType="com.bonus.canteen.core.menu.vo.IssueRecipeDetailCompressVO">
                <result property="prefPrice" column="pref_price"/>
                <result property="salePrice" column="sale_price"/>
                <result property="detailId" column="detail_id"/>
                <result property="dishesId" column="dishes_id"/>
            </collection>
        </collection>
    </resultMap>

    <resultMap id="menuRecipeDetailV2VO" type="com.bonus.canteen.core.menu.vo.MenuRecipeDetailV2VO">
        <result property="applyDate" column="apply_date"/>
        <collection property="intervalList" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailV2IntervalVO">
            <result property="mealtimeType" column="mealtime_type"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailV2DishesVO">
                <result property="dishesId" column="dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="price" column="price"/>
                <result property="supplyNum" column="supply_num"/>
                <result property="saleNum" column="sale_num"/>
                <result property="surplusNum" column="supply_num"/>
                <result property="restrictNum" column="restrict_num"/>
                <result property="salePrice" column="sale_price"/>
            </collection>
        </collection>
    </resultMap>

    <resultMap id="issueRecipeDateVO" type="com.bonus.canteen.core.menu.vo.IssueRecipeDateVO">
        <result property="applyDate" column="apply_date"/>
        <collection property="intervalList" ofType="com.bonus.canteen.core.menu.vo.IssueRecipeIntervalVO">
            <result property="intervalId" column="interval_id"/>
            <collection property="productList" ofType="com.bonus.canteen.core.menu.vo.IssueRecipeDetailVO">
                <result property="customId" column="custom_id"/>
                <result property="productName" column="product_name"/>
                <result property="surplusNum" column="surplus_num"/>
                <result property="restrictNum" column="restrict_num"/>
                <result property="prefPrice" column="pref_price"/>
                <result property="salePrice" column="sale_price"/>
                <result property="originalPrice" column="original_price"/>
                <result property="salseMode" column="sales_mode"/>
                <result property="weightDeviation" column="weight_deviation"/>
                <result property="weight" column="weight"/>
                <result property="typeId" column="type_id"/>
                <result property="typeName" column="type_name"/>
                <result property="purine" column="purine"/>
                <result property="iodine" column="iodine"/>
                <result property="initialScore" column="initial_score"/>
                <result property="calories" column="calories"/>
                <result property="protein" column="protein"/>
                <result property="fat" column="fat"/>
                <result property="carbohydrate" column="carbohydrate"/>
                <result property="dietaryFiber" column="dietary_fiber"/>
                <result property="cholesterol" column="cholesterol"/>
                <result property="calcium" column="calcium"/>
                <result property="sodium" column="sodium"/>
                <result property="foodImage" column="foodImage"/>
            </collection>
        </collection>
    </resultMap>

    <resultMap id="androidProductionRecipeVO" type="com.bonus.canteen.core.menu.vo.AndroidProductionRecipeVO">
        <result property="applyDate" column="apply_date"/>
        <collection property="intervalList" ofType="com.bonus.canteen.core.menu.vo.AndroidProductionMealtimeVO">
            <result property="intervalId" column="interval_id"/>
            <collection property="productList" ofType="com.bonus.canteen.core.menu.vo.AndroidProductionDishesVO">
                <result property="customId" column="custom_id"/>
                <result property="productName" column="product_name"/>
                <result property="surplusNum" column="surplus_num"/>
                <result property="restrictNum" column="restrict_num"/>
                <result property="prefPrice" column="pref_price"/>
                <result property="salePrice" column="sale_price"/>
                <result property="originalPrice" column="original_price"/>
                <result property="intro" column="intro"/>
                <result property="salesMode" column="sales_mode"/>
                <result property="weightDeviation" column="weight_deviation"/>
                <result property="weight" column="weight"/>
                <result property="typeId" column="type_id"/>
                <result property="typeName" column="type_name"/>
                <result property="purine" column="purine"/>
                <result property="iodine" column="iodine"/>
                <result property="initialScore" column="initial_score"/>
                <result property="calories" column="calories"/>
                <result property="protein" column="protein"/>
                <result property="fat" column="fat"/>
                <result property="carbohydrate" column="carbohydrate"/>
                <result property="dietaryFiber" column="dietary_fiber"/>
                <result property="cholesterol" column="cholesterol"/>
                <result property="calcium" column="calcium"/>
                <result property="sodium" column="sodium"/>
                <result property="foodImage" column="foodImage"/>
            </collection>
        </collection>
    </resultMap>

    <select id="selectBetweenDateDetailList" resultMap="menuRecipeDateVO">
        select mrd.mealtime_type,
        mrd.detail_id,
        mrd.recipe_id,
        mrd.apply_date,
        md.dishes_id,
        mdb.dishes_name,
        mrdd.price,
        mrdd.size_type,
        mrdd.supply_num,
        mrdd.sale_num,
        mrdd.surplus_num,
        mrdd.restrict_num,
        mrdd.sale_price,
        mrdd.recommend_flag,
        mrdd.sort_num,
        md.sales_mode,
        md.unit_price,
        md.meal_type,
        md.dishes_num,
        md.material_cost,
        md.type_id,
        mdt.type_name
        from menu_recipe_detail mrd
        left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
        left join menu_dishes md on mrdd.dishes_id = md.dishes_id
        left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where mrd.recipe_id in
        <foreach collection="recipeIds" item="recipeId" separator="," open="(" close=")">
            #{recipeId}
        </foreach>
        and mrd.apply_date between #{applyStartDate} and #{applyEndDate}
        order by mrd.apply_date
    </select>

    <select id="selectTemplateDetailList" resultMap="menuRecipeDetailDateVO">
        select mrd.mealtime_type,
        mrd.detail_id,
        mdb.base_dishes_id as dishes_id,
        mdb.dishes_name,
        mrdd.id,
        mrdd.price,
        mrdd.size_type,
        mrdd.supply_num,
        mrdd.sale_num,
        mrdd.surplus_num,
        mrdd.restrict_num,
        mrdd.sale_price,
        mrdd.recommend_flag,
        mrdd.sort_num,
        ac.canteen_name,
        md.sales_mode,
        md.unit_price,
        md.meal_type,
        md.dishes_num,
        md.type_id,
        mdt.type_name,
        md.material_cost
        from menu_recipe_detail mrd
        left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
        left join menu_dishes md on mrdd.dishes_id = md.dishes_id
        left join alloc_canteen ac on md.canteen_id = ac.canteen_id
        left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where mrd.recipe_id = #{params.recipeId}
        <if test="params.applyWeek !=null ">
            and mrd.apply_week = #{params.applyWeek}
        </if>
        and mrd.detail_type = 1
        order by mrdd.sort_num
    </select>

    <select id="selectWeekDetailList" resultMap="menuRecipeWeekVO">
        select mrd.mealtime_type,
               mrd.detail_id,
               mrd.apply_week,
               md.dishes_id,
               mdb.dishes_name,
               mrdd.price,
               mrdd.supply_num,
               mrdd.sale_num,
               mrdd.surplus_num,
               mrdd.restrict_num,
               mrdd.sale_price,
               mrdd.recommend_flag,
               mrdd.sort_num,
               md.meal_type,
               md.dishes_num,
               md.material_cost,
               md.type_id,
               mdt.type_name
          from menu_recipe_detail mrd
                   left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
                   left join menu_dishes md on mrdd.dishes_id = md.dishes_id
                   left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
                   left join menu_dishes_type mdt on md.type_id = mdt.type_id
         where mrd.recipe_id = #{recipeId}

           and mrd.detail_type = 1
         order by mrd.apply_week
    </select>

    <select id="selectIssueRecipeV2Compress" resultMap="issueRecipeDateCompressVO">
        select mrd.detail_id,
               mrd.apply_date,
               mrd.mealtime_type as interval_id,
               md.dishes_id,
               mrdd.sale_price   as pref_price,
               mrdd.price        as sale_price
          from menu_recipe_detail mrd
                   left join menu_recipe mr on mrd.recipe_id = mr.recipe_id
                   left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
                  and mrdd.size_type != 3
                   left join menu_dishes md on mrdd.dishes_id = md.dishes_id
         where mrd.recipe_id = #{issueRecipeV2DTO.recipeId}
           and mrd.apply_date between #{issueRecipeV2DTO.beginDate} and #{issueRecipeV2DTO.endDate}
           and md.del_flag = 2
         order by mrdd.sort_num asc
    </select>

    <!-- 获取指定日期菜谱详情 -->
    <select id="selectDateDetailList" resultMap="menuRecipeDetailDateVO">
        select mrd.mealtime_type,
               mrd.detail_id,
               mdb.base_dishes_id as dishes_id,
               mdb.dishes_name,
               mrdd.id,
               mrdd.price,
               mrdd.size_type,
               mrdd.supply_num,
               mrdd.sale_num,
               mrdd.surplus_num,
               mrdd.restrict_num,
               mrdd.sale_price,
               mrdd.recommend_flag,
               mrdd.sort_num,
               ac.canteen_name,
               md.sales_mode,
               md.unit_price,
               md.meal_type,
               md.dishes_num,
               md.type_id,
               mdt.type_name,
               md.material_cost
        from menu_recipe_detail mrd
                 left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
                 left join menu_dishes md on mrdd.dishes_id = md.dishes_id
                 left join alloc_canteen ac on md.canteen_id = ac.canteen_id
                 left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
                 left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where mrd.recipe_id = #{recipeId}
          and mrd.apply_date = #{applyDate}
        order by mrdd.sort_num
    </select>

    <select id="selectTmpBetweenDateDetailList" resultMap="menuRecipeDateVO">
        select mrd.mealtime_type,
        mrd.recipe_id,
        mrd.detail_id,
        mdb.base_dishes_id as dishes_id,
        mdb.dishes_name,
        mrdd.id,
        mrdd.price,
        mrdd.size_type,
        mrdd.supply_num,
        mrdd.sale_num,
        mrdd.surplus_num,
        mrdd.restrict_num,
        mrdd.sale_price,
        mrdd.recommend_flag,
        mrdd.sort_num,
        md.sales_mode,
        md.unit_price,
        md.meal_type,
        md.dishes_num,
        ac.canteen_name,
        md.material_cost,
        md.type_id,
        mdt.type_name
        from menu_recipe_detail mrd
        left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
        left join menu_dishes md on mrdd.dishes_id = md.dishes_id
        left join alloc_canteen ac on md.canteen_id = ac.canteen_id
        left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where mrd.recipe_id in
        <foreach collection="recipeIds" item="recipeId" separator="," open="(" close=")">
            #{recipeId}
        </foreach>
        and mrd.apply_date between #{applyStartDate} and #{applyEndDate}
        order by mrd.apply_date,mrdd.sort_num
    </select>

    <!-- pc端获取指定菜谱详情 -->
    <select id="selectRecipeDetailByIdPc" resultMap="menuRecipeDetailV2VO">
        select mrd.apply_date,
               mrd.mealtime_type,
               mdb.base_dishes_id as dishes_id,
               mdb.dishes_name,
               mrdd.price,
               mrdd.supply_num,
               mrdd.sale_num,
               mrdd.supply_num,
               mrdd.restrict_num,
               mrdd.sale_price
        from menu_recipe_detail mrd
                 left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
                 left join menu_dishes md on mrdd.dishes_id = md.dishes_id
                 left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
        where mrd.recipe_id = #{recipeId}
          and mrd.apply_date >= curdate()
    </select>

    <!-- 获取出品机菜谱详情 -->
    <select id="selectIssueRecipeV2" resultMap="issueRecipeDateVO">
        select mrd.apply_date,
               mrd.mealtime_type                               as interval_id,
               md.custom_id,
               md.dishes_name                                  as product_name,
               mrdd.surplus_num,
               mrdd.restrict_num,
               mrdd.sale_price                                 as pref_price,
               mrdd.price                                      as sale_price,
               md.price                                        as original_price,
               md.sales_mode,
               md.weight_deviation,
               if(md.sales_mode = 1, md.weight, md.unit_price) as weight,
               mdt.type_id,
               mdt.type_name,
               md.purine,
               md.iodine,
               md.initial_score,
               md.calories,
               md.protein,
               md.fat,
               md.carbohydrate,
               md.dietary_fiber,
               md.cholesterol,
               md.calcium,
               md.sodium,
               md.image_url                                       foodImage
        from menu_recipe_detail mrd
                 left join menu_recipe mr on mrd.recipe_id = mr.recipe_id
                 left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
            and mrdd.size_type != 3
                 left join menu_dishes md on mrdd.dishes_id = md.dishes_id
                 left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where mrd.recipe_id = #{issueRecipeV2DTO.recipeId}
          and mrd.apply_date between #{issueRecipeV2DTO.beginDate} and #{issueRecipeV2DTO.endDate}
          and md.del_flag = 2
        order by mrdd.sort_num asc
    </select>

    <select id="selectProductionRecipe" resultMap="androidProductionRecipeVO">
        select
        mrd.apply_date,
        mrd.mealtime_type as interval_id,
        md.custom_id,
        md.dishes_name as product_name,
        mrdd.surplus_num,
        mrdd.restrict_num,
        mrdd.sale_price as pref_price,
        mrdd.price as sale_price,
        md.price as original_price,
        md.sales_mode,
        md.intro,
        md.weight_deviation,
        if(md.sales_mode = 1, md.weight, md.unit_price) as weight,
        mdt.type_id,
        mdt.type_name,
        md.purine,
        md.iodine,
        md.initial_score,
        md.calories,
        md.protein,
        md.fat,
        md.carbohydrate,
        md.dietary_fiber,
        md.cholesterol,
        md.calcium,
        md.sodium,
        md.image_url foodImage
        from
        menu_recipe_detail mrd
        left join menu_recipe mr on mrd.recipe_id = mr.recipe_id
        left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
        and mrdd.size_type != 3
        left join menu_dishes md on mrdd.dishes_id = md.dishes_id
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where
        mrd.recipe_id = #{recipeId}
        and mrd.apply_date in
        <foreach collection="applyDateList" item="applyDate" separator="," open="(" close=")">
            #{applyDate}
        </foreach>
    </select>

</mapper>
