<?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.MenuAppRecipeMapper">

    <resultMap id="appletCurrentCanteenVO" type="com.bonus.canteen.core.menu.vo.AppletCurrentCanteenVO">
        <result property="canteenId" column="canteen_id"/>
        <result property="canteenName" column="canteen_name"/>
        <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.menu.vo.AppletCurrentStallVO">
            <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="selectCurrentShopstallList" resultMap="appletCurrentCanteenVO">
        select distinct
        ac.canteen_id,
        ac.canteen_name,
        ass.stall_id,
        ass.stall_name,
        ass.img_url as stall_img_url,
        mr.recipe_id,
        ac.start_business_time as start_business_time_ac,
        ac.end_business_time as end_business_time_ac,
        ac.business_state as business_state_ac,
        ass.start_business_time,
        ass.end_business_time,
        ass.business_state
        from
        menu_app_recipe mar
        left join menu_recipe mr on mar.recipe_id = mr.recipe_id
        left join alloc_canteen ac on mr.canteen_id = ac.canteen_id
        left join alloc_stall ass on mr.stall_id = ass.stall_id
        left join menu_recipe_detail mrd on mr.recipe_id = mrd.recipe_id
        and mrd.apply_date = curdate()
        left join menu_recipe_dishes m on mrd.detail_id = m.detail_id
        left join menu_dishes md on m.dishes_id = md.dishes_id
        left join alloc_kind ak on mr.stall_id = ak.stall_id
        where
        bind_type = 1
        <if test="kindName != null and kindName != ''">
            and ak.kind_name = #{kindName}
        </if>
        <if test="keyword != null and keyword != ''">
            and md.dishes_name like #{keyword}
        </if>
        and mr.eff_id in
        <foreach collection="effIdSet" item="effId" separator="," open="(" close=")">
            #{effId}
        </foreach>
    </select>

    <resultMap id="appletWeekCanteenVO" type="com.bonus.canteen.core.menu.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"/>
        <collection property="labelList" ofType="string">
            <result property="labelName" column="label_name"/>
        </collection>
    </resultMap>

    <!-- 获取一周菜谱食堂列表 -->
    <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,
        al.label_name
        from
        menu_app_recipe mar
        left join menu_recipe mr on mar.recipe_id = mr.recipe_id
        left join alloc_canteen ac on mr.canteen_id = ac.canteen_id
        left join alloc_stall ass on mr.stall_id = ass.stall_id
        left join alloc_label al on ass.stall_id = al.stall_id
        where
        bind_type = 3
        and mr.eff_id in
        <foreach collection="effIdSet" item="effId" separator="," open="(" close=")">
            #{effId}
        </foreach>
    </select>

    <!-- 获取菜谱id -->
    <select id="selectWeekRecipeId" resultType="java.lang.Long">
        select
        mar.recipe_id
        from
        menu_app_recipe mar
        left join menu_recipe mr on mar.recipe_id = mr.recipe_id
        where
        bind_type = 3
        and mr.eff_id in
        <foreach collection="effIdSet" item="effId" separator="," open="(" close=")">
            #{effId}
        </foreach>
        <if test="recipeId != null">
            and mar.recipe_id = #{recipeId}
        </if>
    </select>

    <resultMap id="appletReserveCanteenVO" type="com.bonus.canteen.core.menu.vo.AppletReserveCanteenVO">
        <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.menu.vo.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="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.start_business_time as start_business_time_ac,
        ac.end_business_time as end_business_time_ac,
        ac.business_state as business_state_ac,
        ass.start_business_time,
        ass.end_business_time,
        ass.business_state
        from
        menu_app_recipe mar
        left join menu_recipe mr on mar.recipe_id = mr.recipe_id
        left join alloc_canteen ac on mr.canteen_id = ac.canteen_id
        left join alloc_stall ass on mr.stall_id = ass.stall_id
        where
        bind_type = 2
        and mr.eff_id in
        <foreach collection="effIdSet" item="effId" separator="," open="(" close=")">
            #{effId}
        </foreach>
    </select>

<!--    <resultMap id="appletReserveCanteenVO" type="com.bonus.canteen.core.menu.vo.AppletReserveCanteenVO">-->
<!--        <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.menu.vo.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="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.start_business_time as start_business_time_ac,-->
<!--            ac.end_business_time as end_business_time_ac,-->
<!--            ac.business_state as business_state_ac,-->
<!--            ass.start_business_time,-->
<!--            ass.end_business_time,-->
<!--            ass.business_state-->
<!--        from-->
<!--        menu_app_recipe mar-->
<!--        left join menu_recipe mr on mar.recipe_id = mr.recipe_id-->
<!--        left join alloc_canteen ac on mr.canteen_id = ac.canteen_id-->
<!--        left join alloc_stall ass on mr.stall_id = ass.stall_id-->
<!--        where-->
<!--        bind_type = 2-->
<!--        and mr.eff_id in-->
<!--        <foreach collection="effIdSet" item="effId" separator="," open="(" close=")">-->
<!--            #{effId}-->
<!--        </foreach>-->
<!--    </select>-->

    <select id="selectCurrentShopstallListByPlaceId" resultMap="appletCurrentCanteenVO">
        select distinct
        ac.canteen_id,
        ac.canteen_name,
        ass.stall_id,
        ass.stall_name,
        ass.img_url as stall_img_url,
        mr.recipe_id,
        ac.start_business_time as start_business_time_ac,
        ac.end_business_time as end_business_time_ac,
        ac.business_state as business_state_ac,
        ass.start_business_time,
        ass.end_business_time,
        ass.business_state
        from
        alloc_table_recipe atr
        left join menu_app_recipe mar on mar.recipe_id = atr.recipe_id
        left join menu_recipe mr on atr.recipe_id = mr.recipe_id
        left join alloc_canteen ac on mr.canteen_id = ac.canteen_id
        left join alloc_stall ass on mr.stall_id = ass.stall_id
        left join menu_recipe_detail mrd on mr.recipe_id = mrd.recipe_id
        and mrd.apply_date = curdate()
        left join menu_recipe_dishes m on mrd.detail_id = m.detail_id
        left join menu_dishes md on m.dishes_id = md.dishes_id
        left join alloc_kind ak on mr.stall_id = ak.stall_id
        where
        mar.bind_type = 1 and atr.table_id = #{placeId}
        <if test="kindName != null and kindName != ''">
            and ak.kind_name = #{kindName}
        </if>
        <if test="keyword != null and keyword != ''">
            and md.dishes_name like concat('%', #{keyword}, '%')
        </if>
        and mr.eff_id in
        <foreach collection="effIdSet" item="effId" separator="," open="(" close=")">
            #{effId}
        </foreach>
    </select>
    <select id="selectRecipeByStallIdsAndBindType" resultType="com.bonus.canteen.core.menu.dto.StallAndRecipeBindDto">
        select
        mar.recipe_id,
        mr.stall_id,
        mar.bind_type
        from menu_app_recipe mar
        left join menu_recipe mr on mar.recipe_id = mr.recipe_id
        where bind_type = #{bindType}
        <if test="stallIds !=null and stallIds.size() > 0 ">
            and mr.stall_id IN
            <foreach collection="stallIds" item="stallId" separator="," open="(" close=")">
                #{stallId}
            </foreach>
        </if>
    </select>
    <select id="selectRecipeInSameShop" resultType="java.lang.Long">
        select mar.recipe_id
        from menu_app_recipe mar
                 inner join menu_recipe mr on mar.recipe_id = mr.recipe_id
        where mar.bind_type = #{bindType}
          and mr.stall_id = #{stallId}
        <if test="effId !=null">
            and mr.eff_id = #{effId}
        </if>
        <if test="mealLineId !=null">
            and mar.meal_line_id= #{mealLineId}
        </if>
    </select>


    <select id="getBindByTypeAndStalls" resultType="com.bonus.canteen.core.device.manage.dto.DeviceRecipeDTO">
        SELECT
        t2.stall_id,
        t2.canteen_id,
        t1.recipe_id,
        t1.meal_line_id
        FROM
        menu_app_recipe t1
        LEFT JOIN menu_recipe t2 ON t1.recipe_id = t2.recipe_id
        WHERE t1.bind_type = #{bindType} AND
        <foreach collection="deviceBinds" separator="OR" open="(" close=")" item="item">
            ( t2.stall_id = #{item.stallId}
            AND t2.canteen_id = #{item.canteenId}
                <if test="item.mealLineId !=null ">
                    AND t1.meal_line_id = #{item.mealLineId}
                </if>
            )
        </foreach>
    </select>

<!--    <resultMap id="AppletWeekCanteenStallVO" type="com.bonus.canteen.core.menu.vo.AppletWeekCanteenStallVO">-->
<!--        <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.menu.vo.AppletWeekStallVO">-->
<!--            <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="getWeekMealCanteenList" resultMap="AppletWeekCanteenStallVO">-->
<!--        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.start_business_time as start_business_time_ac,-->
<!--        ac.end_business_time as end_business_time_ac,-->
<!--        ac.business_state as business_state_ac,-->
<!--        ass.start_business_time,-->
<!--        ass.end_business_time,-->
<!--        ass.business_state-->
<!--        from-->
<!--        menu_app_recipe mar-->
<!--        left join menu_recipe mr on mar.recipe_id = mr.recipe_id-->
<!--        left join alloc_canteen ac on mr.canteen_id = ac.canteen_id-->
<!--        left join alloc_stall ass on mr.stall_id = ass.stall_id-->
<!--        where-->
<!--        bind_type = 3-->
<!--        and mr.eff_id in-->
<!--        <foreach collection="effIdSet" item="effId" separator="," open="(" close=")">-->
<!--            #{effId}-->
<!--        </foreach>-->
<!--    </select>-->
</mapper>
