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



    <select id="selectMessageByUserId" resultType="com.bonus.canteen.core.android.vo.UserInfoVo" parameterType="com.bonus.canteen.core.android.vo.UserInfoVo">
        select dept_id as org_id, is_built_in
        from sys_user
        where user_id =#{userId}
    </select>

    <resultMap id="appletWeekCanteenVO" type="com.bonus.canteen.core.menu.vo.AppletWeekCanteenVO">
        <result property="recipeId" column="recipe_id"/>
        <result property="stallId" column="stall_id"/>
        <result property="stallName" column="stall_name"/>
        <result property="canteenId" column="canteen_id"/>
        <result property="canteenName" column="canteen_name"/>
        <result property="canteenImgUrl" column="canteen_img_url"/>
        <result property="stallImgUrl" column="stall_img_url"/>
    </resultMap>


    <select id="selectWeekCanteenList" resultMap="appletWeekCanteenVO">

        select
        mr.recipe_id,
        ass.stall_id,
        ass.stall_name,
        ac.canteen_id,
        ac.canteen_name,
        ac.img_url as canteen_img_url,
        ass.img_url as stall_img_url
        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
    </select>

    <resultMap id="appletDishesDetailVO" type="com.bonus.canteen.core.menu.vo.AppletDishesDetailVO">
        <result property="baseDishesId" column="base_dishes_id"/>
        <result property="mealType" column="meal_type"/>
        <result property="customId" column="custom_d"/>
        <result property="dishesName" column="dishes_name"/>
        <result property="aliasName" column="alias_name"/>
        <result property="imageUrl" column="image_url"/>
        <result property="intro" column="intro"/>
        <result property="particulars" column="particulars"/>
        <collection property="dishesDetailList" ofType="com.bonus.canteen.core.menu.vo.AppletDishesSizeV2VO">
            <result property="dishesId" column="dishes_id"/>
            <result property="dishesName" column="real_dishes_name"/>
            <result property="surplusNum" column="surplus_num"/>
            <result property="restrictNum" column="restrict_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="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="tasteIds" column="taste_id"/>
        </collection>
    </resultMap>

    <select id="selectDishesDetailByDishesId" resultMap="appletDishesDetailVO" >
        select
        mdb.base_dishes_id,
        md.dishes_id,
        md.meal_type,
        md.custom_id,
        mdb.dishes_name,
        md.dishes_name as real_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.surplus_num as sales_num,
        mrd.surplus_num,
        mrd.restrict_num,
        md.calories,
        md.protein,
        md.fat,
        md.carbohydrate,
        md.dietary_fiber,
        md.cholesterol,
        md.calcium,
        md.sodium,
        md.taste_id
        from
        menu_dishes_base mdb
        left join menu_dishes md on mdb.base_dishes_id = md.base_dishes_id
        left join menu_recipe_dishes mrd on md.dishes_id = mrd.dishes_id
        where
        mdb.base_dishes_id = #{baseDishesId}
        and mrd.detail_id = #{detailId}
<!--        and md.del_flag = #{delFlag}-->
        limit 1
    </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="appletWeekRecipeVO" type="com.bonus.canteen.core.menu.vo.AppletWeekRecipeVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <collection property="typeList" ofType="com.bonus.canteen.core.menu.vo.AppletWeekRecipeTypeVO">
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.menu.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.menu.vo.AppletCurrentDishesDetailVO">
                    <result property="dishesId" column="dishes_id"/>
                    <result property="dishesName" column="real_dishes_name"/>
                    <result property="surplusNum" column="surplus_num"/>
                    <result property="restrictNum" column="restrict_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="selectWeekRecipe" resultMap="appletWeekRecipeVO">
        select
        mrd.mealtime_type,
        if(md.meal_type = 2, 2, mdt.type_id) as type_id,
        if(md.meal_type = 2, '套餐', mdt.type_name) as type_name,
        mdb.base_dishes_id,
        md.dishes_id,
        mdb.dishes_name,
        md.dishes_name as real_dishes_name,
        md.image_url as dishes_img_url,
        mrd.detail_id,
        m.surplus_num,
        m.restrict_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
        menu_recipe_detail mrd
        inner join menu_recipe_dishes m on mrd.detail_id = m.detail_id
        inner join menu_dishes md on m.dishes_id = md.dishes_id
        inner join menu_dishes_type mdt on md.type_id = mdt.type_id
        inner join menu_recipe mr on mrd.recipe_id = mr.recipe_id
        left join menu_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 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_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_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
        m.sort_num asc
    </select>

    <select id="getMenuMaterialById" resultType="com.bonus.canteen.core.menu.vo.MenuMaterialBasVO">
        SELECT mm.material_id,
        mm.material_name,
        mm.material_code,
        mm.image_url,
        mmd.weight,
        mmd.material_type,
        mn.big_type,
        mn.little_type
        FROM menu_material mm
        LEFT JOIN menu_material_dishes mmd ON mm.material_id = mmd.material_id
        LEFT JOIN menu_nutrition mn ON mn.nutrition_id = mm.nutrition_id
        WHERE mmd.dishes_id = #{dishesId}
        and mmd.del_flag = 2
    </select>

    <select id = "getCollectionCountByUserIdAndDishesId" resultType="Integer">
        select count(1)
        from menu_collection_dishes
        where user_id = #{userId} and dishes_id = #{dishesId}
    </select>

    <select id="selectWeekRecipeIdHistory" resultType="java.lang.Long">
        select
        mar.recipe_id
        from
        menu_app_recipe_history 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>-->
        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="getRecipeIdListByRecipeId" resultType="java.lang.Long">
        select recipe_id from menu_recipe where stall_id = (select stall_id from menu_recipe where recipe_id = #{recipeId})
    </select>

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

</mapper>
