<?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.CookRecipeH5Mapper">
    <resultMap id="appletReserveCanteenVO" type="com.bonus.canteen.core.basic.domain.AppletReserveCanteenVO">
        <result property="canteenId" column="canteen_id"/>
        <result property="canteenName" column="canteen_name"/>
        <result property="canteenImgUrl" column="canteen_img_url"/>
        <result property="startBusinessTime" column="biz_start_time_ac"/>
        <result property="endBusinessTime" column="biz_end_time_ac"/>
        <result property="businessState" column="biz_state_ac"/>
        <collection property="stallList" ofType="com.bonus.canteen.core.basic.domain.AppletReserveStallVO">
            <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="biz_start_time"/>
            <result property="endBusinessTime" column="biz_end_time"/>
            <result property="businessState" column="biz_state"/>
        </collection>
    </resultMap>

    <resultMap id="appletReserveRecipeVO" type="com.bonus.canteen.core.cook.vo.AppletReserveRecipeVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <collection property="typeList" ofType="com.bonus.canteen.core.cook.vo.AppletReserveRecipeTypeVO">
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.cook.vo.AppletReserveRecipeDishesVO">
                <result property="baseDishesId" column="base_dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="detailType" column="detail_type"/>
                <result property="dishesImgUrl" column="dishes_img_url"/>
                <result property="detailId" column="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.AppletCurrentDishesDetailVO">
                    <result property="dishesId" column="dishes_id"/>
                    <result property="dishesName" column="dishes_name"/>
                    <result property="intro" column="intro"/>
                    <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>
                <!--                <collection property="tasteNameList" ofType="string">-->
                <!--                    <result property="tasteName" column="taste_name"/>-->
                <!--                </collection>-->
            </collection>
        </collection>
    </resultMap>

    <resultMap id="appletWeekRecipeVO" type="com.bonus.canteen.core.cook.vo.AppletWeekRecipeVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <collection property="typeList" ofType="com.bonus.canteen.core.cook.vo.AppletWeekRecipeTypeVO">
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.cook.vo.AppletWeekRecipeDishesVO">
                <result property="baseDishesId" column="base_dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="dishesImgUrl" column="dishes_img_url"/>
                <result property="detailId" column="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.AppletCurrentDishesDetailVO">
                    <result property="dishesId" column="dishes_id"/>
                    <result property="dishesName" column="dishes_name"/>
                    <result property="intro" column="intro"/>
                    <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>

    <resultMap id="appletWeekCanteenVO" type="com.bonus.canteen.core.cook.vo.AppletWeekCanteenVO">
        <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>

    <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 biz_start_time_ac,
            ac.biz_end_time as biz_end_time_ac,
            ac.biz_state as biz_state_ac,
            ass.biz_start_time,
            ass.biz_end_time,
            ass.biz_state
        from
            basic_canteen ac
                left join basic_stall ass on ac.canteen_id = ass.canteen_id
                left join cook_recipe mr on mr.stall_id = ass.stall_id
                left join cook_recipe_bind_app mar on mar.recipe_id = mr.recipe_id
        where
            bind_type = 2
          and ac.del_flag = 0
          and ass.del_flag = 0
    </select>

    <select id="selectMonthSalesStall" resultType="com.bonus.canteen.core.basic.domain.CookStallSaleModel">
        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="selectReserveRecipe" resultMap="appletReserveRecipeVO">
        select
            mrd.mealtime_type,
            md.meal_type as type_id,
            '菜品' as type_name,
            md.dishes_id as base_dishes_id,
            md.dishes_id,
            md.dishes_name,
            md.dishes_name as real_dishes_name,
            md.meal_type as detail_type,
            md.image_url as dishes_img_url,
            mrd.recipe_detail_id as 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
        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}
        order by
            md.dishes_id asc
    </select>

    <select id="selectStallIdByRecipeId" resultType="Long">
        select stall_id from cook_recipe
        where recipe_id = #{recipeId}
    </select>

    <!-- 获取一周菜谱食堂列表 -->
    <select id="selectWeekCanteenList" 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>

    <!-- 获取菜谱id -->
    <select id="selectWeekRecipeId" resultType="java.lang.Long">
        select
        mar.recipe_id
        from
        cook_recipe_bind_app mar
        left join cook_recipe mr on mar.recipe_id = mr.recipe_id
        where
        bind_type = 3
        <if test="recipeId != null">
            and mar.recipe_id = #{recipeId}
        </if>
    </select>

    <select id="getBingTimeByRecipeId" resultType="java.lang.String">
        select bind_time from cook_recipe_bind_app where recipe_id = #{recipeId} and bind_type = 3
    </select>

    <select id="getRecipeIdListByRecipeId" resultType="java.lang.Long">
        select recipe_id from cook_recipe where stall_id = (select stall_id from cook_recipe where recipe_id = #{recipeId})
    </select>

    <select id="selectWeekRecipeIdHistory" resultType="java.lang.Long">
        select
        mar.recipe_id
        from
        cook_recipe_bind_app_history mar
        left join cook_recipe mr on mar.recipe_id = mr.recipe_id
        where
        bind_type = 3
        and mar.recipe_id in
        <foreach collection="recipeIdList" item="repiceId" separator="," open="(" close=")">
            #{repiceId}
        </foreach>
        and  #{applyDate} >= DATE(mar.bind_time)
        ORDER BY bind_time desc limit 1
    </select>

    <!-- 获取一周菜谱 -->
    <select id="selectWeekRecipe" 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 as base_dishes_id,
        md.dishes_id,
        md.dishes_name,
        md.intro,
        md.image_url as dishes_img_url,
        mrd.recipe_detail_id as 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}
        <if test="recipeId != null">
            and mrd.recipe_id = #{recipeId}
        </if>
        <if test="recipeIdList != null and recipeIdList.size() > 0">
            and mrd.recipe_id in
            <foreach collection="recipeIdList" item="recipeId" open="(" separator="," close=")">
                #{recipeId}
            </foreach>
        </if>
        order by
        md.dishes_id asc
    </select>
</mapper>

