<?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.cook.mapper.CookH5Mapper">

    <resultMap id="appletWeekCanteenVO" type="com.bonus.canteen.core.cook.vo.CookH5WeekCanteenListVO">
        <result property="recipeId" column="recipe_id"/>
        <result property="stallName" column="stall_name"/>
        <result property="canteenName" column="canteen_name"/>
        <result property="canteenImgUrl" column="canteen_img_url"/>
        <result property="stallImgUrl" column="stall_img_url"/>
    </resultMap>

    <resultMap id="appletWeekRecipeVO" type="com.bonus.canteen.core.cook.vo.CookH5WeekDetailsVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <collection property="typeList" ofType="com.bonus.canteen.core.cook.vo.CookH5WeekTypeVO">
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.cook.vo.CookH5WeekDishesVO">
                <result property="dishesId" column="dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="dishesImgUrl" column="dishes_img_url"/>
                <result property="recipeDetailId" column="recipe_detail_id"/>
                <result property="canteenId" column="canteen_id"/>
                <result property="canteenName" column="canteen_name"/>
                <result property="stallId" column="stall_id"/>
                <result property="stallName" column="stall_name"/>
                <collection property="dishesDetailList" ofType="com.bonus.canteen.core.cook.vo.CookH5WeekDishesDetailsVO">
                    <result property="dishesId" column="dishes_id"/>
                    <result property="dishesName" column="dishes_name"/>
                    <result property="remanentNum" column="remanent_num"/>
                    <result property="limitNum" column="limit_num"/>
                    <result property="dishesPrice" column="dishes_price"/>
                    <result property="prefPrice" column="pref_price"/>
                    <result property="sizeType" column="size_type"/>
                    <result property="sizeJson" column="size_json"/>
                </collection>
            </collection>
        </collection>
    </resultMap>

    <select id="getWeekCanteenList" resultMap="appletWeekCanteenVO">
        select
            mr.recipe_id,
            ass.stall_name,
            ac.canteen_name,
            ac.img_url as canteen_img_url,
            ass.img_url as stall_img_url
        from
            cook_recipe_bind_app mar
                left join cook_recipe mr on mar.recipe_id = mr.recipe_id
                left join basic_canteen ac on mr.canteen_id = ac.canteen_id
                left join basic_stall ass on mr.stall_id = ass.stall_id
        where
            bind_type = 3
    </select>

    <select id="getWeekRecipeDetail" resultMap="appletWeekRecipeVO">
        select
            mrd.mealtime_type,
            if(md.meal_type = 2, 2, mdt.dishes_type_id) as type_id,
            if(md.meal_type = 2, '套餐', mdt.dishes_type_name) as type_name,
            md.dishes_id,
            md.dishes_name,
            md.image_url as dishes_img_url,
            mrd.recipe_detail_id,
            m.remanent_num,
            m.limit_num,
            m.price as dishes_price,
            m.sale_price as pref_price,
            ac.canteen_id,
            ac.canteen_name,
            ass.stall_id,
            ass.stall_name,
            m.size_type,
            md.size_json
        from
            cook_recipe_detail mrd
                inner join cook_recipe_dishes m on mrd.recipe_detail_id = m.recipe_detail_id
                inner join cook_dishes md on m.dishes_id = md.dishes_id
                inner join cook_dishes_type mdt on md.type_id = mdt.dishes_type_id
                inner join cook_recipe mr on mrd.recipe_id = mr.recipe_id
                left join cook_dishes_sale_record mdsr on md.dishes_id = mdsr.dishes_id
                and mr.stall_id = mdsr.stall_id
                and mdsr.sale_month = month(curdate())
            left join basic_canteen ac on mr.canteen_id = ac.canteen_id
            left join basic_stall ass on mr.stall_id = ass.stall_id
        where
            mrd.apply_date = #{applyDate}

          and mrd.recipe_id = #{recipeId}

        order by
            m.remanent_num asc
    </select>


    <resultMap id="appletReserveCanteenVO" type="com.bonus.canteen.core.cook.vo.CookH5ReserveCanteenListVO">
        <result property="canteenId" column="canteen_id"/>
        <result property="canteenName" column="canteen_name"/>
        <result property="canteenImgUrl" column="canteen_img_url"/>
        <result property="startBusinessTime" column="start_business_time_ac"/>
        <result property="endBusinessTime" column="end_business_time_ac"/>
        <result property="businessState" column="business_state_ac"/>
        <collection property="stallList" ofType="com.bonus.canteen.core.cook.vo.CookH5ReserveStallVO">
            <result property="stallId" column="stall_id"/>
            <result property="stallName" column="stall_name"/>
            <result property="stallImgUrl" column="stall_img_url"/>
            <result property="recipeId" column="recipe_id"/>
            <result property="startBusinessTime" column="start_business_time"/>
            <result property="endBusinessTime" column="end_business_time"/>
            <result property="businessState" column="business_state"/>
        </collection>
    </resultMap>

    <select id="selectReserveMealCanteenList" resultMap="appletReserveCanteenVO">
        select
            ac.canteen_id,
            ac.canteen_name,
            ac.img_url as canteen_img_url,
            ass.stall_id,
            ass.stall_name,
            ass.img_url as stall_img_url,
            mr.recipe_id,
            ac.biz_start_time as start_business_time_ac,
            ac.biz_end_time as end_business_time_ac,
            ac.biz_state as business_state_ac,
            ass.biz_start_time as startBusinessTime,
            ass.biz_end_time as endBusinessTime,
            ass.biz_state as businessState
        from
            cook_recipe_bind_app mar
                left join cook_recipe mr on mar.recipe_id = mr.recipe_id
                left join basic_canteen ac on mr.canteen_id = ac.canteen_id
                left join basic_stall ass on mr.stall_id = ass.stall_id
        where
            bind_type = 2 and mr.del_flag = '0'
    </select>

    <select id="selectMonthSalesStall" resultType="com.bonus.canteen.core.cook.vo.StallMonthSalesVO">
        select
        stall_id,
        sum(monthly_sales) as monthly_sales
        from
        cook_dishes_sale_record
        where
        sale_month = month(curdate())
        and stall_id in
        <foreach collection="stallIdList" item="stallId" open="(" separator="," close=")">
            #{stallId}
        </foreach>
        group by
        stall_id
    </select>

    <select id="selectTopDishesIdList" resultType="java.lang.Long">

        select
        dishes_id
        from
        cook_dishes_sale_record
        <where>
            stall_id in
            <foreach collection="stallIdList" item="stallId" open="(" separator="," close=")">
                #{stallId}
            </foreach>
        </where>
        order by
        sale_num desc

    </select>

    <select id="selectAllRecipeDishesList" resultType="com.bonus.canteen.core.cook.vo.CookH5ReserveDishesVO">
    select
    md.dishes_id,
    md.dishes_name,
    m.price as dishes_price,
    md.image_url as dishes_img_url
    from
    cook_recipe_detail mrd
    inner join cook_recipe_dishes m on mrd.recipe_detail_id = m.recipe_detail_id
    inner join cook_dishes md on m.dishes_id = md.dishes_id
    where
    mrd.recipe_id = 1
    and mrd.apply_date = curdate()
    and mrd.mealtime_type = 1
    </select>

    <resultMap id="appletReserveRecipeVO" type="com.bonus.canteen.core.cook.vo.CookH5ReserveRecipeDetailsVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <collection property="typeList" ofType="com.bonus.canteen.core.cook.vo.CookH5ReserveRecipeTypeVO">
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.cook.vo.CookH5ReserveRecipeDishesVO">
                <result property="dishesId" column="dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="detailType" column="detail_type"/>
                <result property="dishesImgUrl" column="dishes_img_url"/>
                <result property="recipeDetailId" column="recipe_detail_id"/>
                <result property="canteenId" column="canteen_id"/>
                <result property="canteenName" column="canteen_name"/>
                <result property="stallId" column="stall_id"/>
                <result property="stallName" column="stall_name"/>
                <result property="recommendFlag" column="recommend_flag"/>
                <collection property="dishesDetailList" ofType="com.bonus.canteen.core.cook.vo.CookH5CurrentDishesDetailVO">
                    <result property="dishesId" column="dishes_id"/>
                    <result property="dishesName" column="dishes_name"/>
                    <result property="remanentNum" column="remanent_num"/>
                    <result property="limitNum" column="limit_num"/>
                    <result property="dishesPrice" column="dishes_price"/>
                    <result property="prefPrice" column="pref_price"/>
                    <result property="sizeType" column="size_type"/>
                    <result property="sizeJson" column="size_json"/>
                    <result property="monthlySales" column="monthly_sales"/>
                    <result property="goodProbability" column="good_probability"/>
                    <collection property="nutritionInfo" resultMap="CookH5ReserveDishesNutritionMap">

                    </collection>
                </collection>
            </collection>
        </collection>
    </resultMap>

    <select id="selectReserveRecipe" resultMap="appletReserveRecipeVO">

        select
            mrd.mealtime_type,
            if(md.meal_type = 2, 2, mdt.dishes_type_id) as type_id,
            if(md.meal_type = 2, '套餐', mdt.dishes_type_name) as type_name,
            md.dishes_id,
            md.dishes_name,
            md.meal_type as detail_type,
            md.image_url as dishes_img_url,
            mrd.recipe_detail_id,
            m.remanent_num,
            m.limit_num,
            m.price as dishes_price,
            m.sale_price as pref_price,
            ac.canteen_id,
            ac.canteen_name,
            ass.stall_id,
            ass.stall_name,
            m.size_type,
            m.recommend_flag,
            md.size_json,
            mdsr.monthly_sales,
            mdsr.good_probability,
            md.dishes_id,
            md.initial_score,
            md.calories,
            md.protein,
            md.fat,
            md.carbohydrate,
            md.dietary_fiber,
            md.cholesterol,
            md.calcium,
            md.sodium,
            md.purine,
            md.iron,
            md.iodine,
            md.kalium,
            md.vitamin_a,
            md.vitamin_c,
            md.vitamin_e,
            md.magnesium,
            md.zinc,
            md.glycemic_index
        from
            cook_recipe_detail mrd
                inner join cook_recipe_dishes m on mrd.recipe_detail_id = m.recipe_detail_id
                inner join cook_dishes md on m.dishes_id = md.dishes_id
                inner join cook_dishes_type mdt on md.type_id = mdt.dishes_type_id
                inner join cook_recipe mr on mrd.recipe_id = mr.recipe_id
                inner join cook_recipe_bind_app mar on mr.recipe_id = mar.recipe_id
                and mar.bind_type = 2
                left join cook_dishes_sale_record mdsr on md.dishes_id = mdsr.dishes_id
                and mr.stall_id = mdsr.stall_id
                and mdsr.sale_month = month(curdate())
            left join basic_canteen ac on mr.canteen_id = ac.canteen_id
            left join basic_stall ass on mr.stall_id = ass.stall_id
        where
           mrd.apply_date = #{applyDate}
          and mr.recipe_id = #{recipeId}
    </select>

    <select id="getCookMealtimeListByStallId" resultType="com.bonus.canteen.core.cook.vo.CookH5ReserveMealtimeStateVO">

        SELECT
        mealtime_type AS mealtimeType,
        if_enable AS ifUse,
        start_time AS startTime,
        end_time AS endTime
        FROM
        basic_stall_mealtime
        WHERE
        stall_id in
        <foreach item="stallId" collection="stallIdSet" open="(" separator="," close=")">
            #{stallId}
        </foreach>

    </select>

    <resultMap id="CookH5ReserveDishesNutritionMap" type="com.bonus.canteen.core.cook.vo.CookH5ReserveDishesNutritionVO">
        <result property="dishesId" column="dishes_id"/>
        <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="purine" column="purine"/>
        <result property="iron" column="iron"/>
        <result property="iodine" column="iodine"/>
        <result property="kalium" column="kalium"/>
        <result property="vitaminA" column="vitamin_a"/>
        <result property="vitaminC" column="vitamin_c"/>
        <result property="vitaminE" column="vitamin_e"/>
        <result property="magnesium" column="magnesium"/>
        <result property="zinc" column="zinc"/>
        <result property="glycemicIndex" column="glycemic_index"/>
    </resultMap>

    <select id="selectNutrition" resultMap="CookH5ReserveDishesNutritionMap">
        select
        md.dishes_id,
        md.initial_score,
        md.calories,
        md.protein,
        md.fat,
        md.carbohydrate,
        md.dietary_fiber,
        md.cholesterol,
        md.calcium,
        md.sodium,
        md.purine,
        md.iron,
        md.iodine,
        md.kalium,
        md.vitamin_a,
        md.vitamin_c,
        md.vitamin_e,
        md.magnesium,
        md.zinc,
        md.glycemic_index
        from
        cook_dishes md
        left join cook_dishes_type mdt on md.type_id = mdt.dishes_type_id
        where
        md.dishes_id in
        <foreach collection="dishesIdSet" item="dishesId" open="(" separator="," close=")">
            #{dishesId}
        </foreach>
    </select>

    <select id="selectRecipeIdList" resultType="java.lang.Long">
        select recipe_id from cook_recipe
        where recipe_type in (2,3)
    </select>


    <select id="recipeIdMap" resultType="long">
        select recipe_id as recipeId from cook_recipe_detail
        WHERE apply_date = #{applyDate} and detail_type = 2
        and recipe_id in
        <foreach collection="recipeIdList" item="recipeId" open="(" separator="," close=")">
            #{recipeId}
        </foreach>
        GROUP BY recipe_id
    </select>

    <select id="selectDishesDetailByDishesId" resultType="com.bonus.canteen.core.cook.vo.AppletDishesDetailVO">
        select
        md.dishes_id,
        md.meal_type,
        md.custom_id,
        md.dishes_name,
        md.alias_name,
        md.image_url,
        md.intro,
        md.particulars,
        mrd.size_type,
        md.size_json,
        mrd.price as dishes_price,
        mrd.sale_price as pref_price,
        mrd.supply_num - mrd.remanent_num as sales_num,
        mrd.remanent_num as surplusNum,
        mrd.limit_num as restrictNum,
        md.calories,
        md.protein,
        md.fat,
        md.carbohydrate,
        md.dietary_fiber,
        md.cholesterol,
        md.calcium,
        md.sodium
        from
        cook_dishes md
        left join cook_recipe_dishes mrd on md.dishes_id = mrd.dishes_id
        where
        md.dishes_id = #{dishesId}
        and mrd.recipe_detail_id = #{detailId}
        <!--        and md.del_flag = #{delFlag}-->
        limit 1
    </select>

    <select id="getMenuMaterialById" resultType="java.util.List">
    </select>
</mapper>
