<?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.core.menu.mapper.MenuRecipeMapper">

    <resultMap type="com.bonus.core.marketing.vo.MktEffectiveUserVO" id="mktEffectiveUserVO">
        <result property="effId" column="eff_id"/>
        <result property="userType" column="user_type"/>
<!--        <result property="orgId" column="org_id"/>-->
<!--        <result property="psnType" column="psn_type"/>-->
        <collection property="orgIdList" ofType="Long">
            <result property="orgId" column="org_id"/>
        </collection>
        <collection property="psnTypeList" ofType="Integer">
            <result property="psnType" column="psn_type"/>
        </collection>
    </resultMap>

    <resultMap id="appletWeekRecipeVO" type="com.bonus.core.menu.vo.AppletWeekRecipeVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <collection property="typeList" ofType="com.bonus.core.menu.vo.AppletWeekRecipeTypeVO">
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <collection property="dishesList" ofType="com.bonus.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.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="selectOrgAndPsnByCustLimitId" parameterType="com.bonus.domain.CustInfo" resultType="com.bonus.domain.CustInfo">
        select org_id, psn_type
        from cust_info
        where cust_id = #{custId} and cust_state = 1
    </select>

    <!-- 获取指定类型的生效范围详情 -->
    <select id="selectAllUserEff" resultMap="mktEffectiveUserVO">
        select me.eff_id,
               me.user_type,
               med.org_id,
               med.psn_type
        from mkt_effective me
                 left join mkt_effective_detail med on me.eff_id = med.eff_id
        where me.eff_type = #{effType}
          and me.del_flag = #{delFlag}
    </select>

    <!-- 获取一周菜谱 -->
    <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="selectAllRecipeDishesList" resultType="com.bonus.core.menu.vo.AppletRecommendDishesVO">
        select
        md.dishes_id,
        md.dishes_name,
        m.price as dishes_price,
        md.image_url as dishes_img_url
        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
        where
        mrd.recipe_id = #{recipeId}
        and mrd.apply_date = curdate()
        <if test="mealtimeType != null">
            and mrd.mealtime_type = #{mealtimeType}
        </if>
        order by m.sort_num asc
    </select>

    <resultMap id="appletReserveRecipeVO" type="com.bonus.core.menu.vo.AppletReserveRecipeVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <collection property="typeList" ofType="com.bonus.core.menu.vo.AppletReserveRecipeTypeVO">
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <collection property="dishesList" ofType="com.bonus.core.menu.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.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"/>
                    <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>
    <!-- 获取预定点餐菜谱 -->
    <select id="selectReserveRecipe" resultMap="appletReserveRecipeVO">
        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.meal_type as detail_type,
            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,
            m.recommend_flag,
            md.size_json,
            mdtt.taste_name,
            mdsr.monthly_sales,
            mdsr.good_probability
        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
                inner join menu_app_recipe mar on mr.recipe_id = mar.recipe_id
                and mar.bind_type = 2
                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
            left join menu_taste_dishes mtd on md.dishes_id = mtd.dishes_id
            left join menu_dishes_taste mdtt on mtd.taste_id = mdtt.taste_id
        where
            mrd.apply_date = #{applyDate}
          and mr.recipe_id = #{recipeId}
        order by
            m.sort_num asc
    </select>
</mapper>
