<?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.MenuRecipeMapper">
    <resultMap type="com.bonus.canteen.core.menu.domain.MenuRecipe" id="MenuRecipeResult">
        <result property="id"    column="id"    />
        <result property="recipeId"    column="recipe_id"    />
        <result property="planId"    column="plan_id"    />
        <result property="recipeName"    column="recipe_name"    />
        <result property="isDefault"    column="is_default"    />
        <result property="recipeType"    column="recipe_type"    />
        <result property="effectDate"    column="effect_date"    />
        <result property="expireDate"    column="expire_date"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="stallId"    column="stall_id"    />
        <result property="enableFlag"    column="enable_flag"    />
        <result property="revision"    column="revision"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <!-- 获取指定日期菜谱详情 -->
    <resultMap id="menuRecipeDateVO" type="com.bonus.canteen.core.menu.vo.MenuRecipeDateVO">
        <result property="day" column="apply_date"/>
        <result property="recipeId" column="recipe_id"/>
        <collection property="details" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDateVO">
            <result property="mealtimeType" column="mealtime_type"/>
            <result property="detailId" column="detail_id"/>
            <collection property="dishesList" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDishesVO">
                <result property="dishesId" column="dishes_id"/>
                <result property="dishesName" column="dishes_name"/>
                <result property="dishesId" column="dishesId"/>
                <result property="price" column="price"/>
                <result property="sizeType" column="size_type"/>
                <result property="supplyNum" column="supply_num"/>
                <result property="saleNum" column="sale_num"/>
                <result property="surplusNum" column="surplus_num"/>
                <result property="restrictNum" column="restrict_num"/>
                <result property="salePrice" column="sale_price"/>
                <result property="recommendFlag" column="recommend_flag"/>
                <result property="sortNum" column="sort_num"/>
                <result property="typeId" column="type_id"/>
                <result property="typeName" column="type_name"/>
                <result property="salesMode" column="sales_mode"/>
                <result property="canteenName" column="canteen_name"/>
                <result property="unitPrice" column="unit_price"/>
                <result property="mealType" column="meal_type"/>
                <result property="dishesNum" column="dishes_num"/>
                <result property="materialCost" column="material_cost"/>
            </collection>
        </collection>
    </resultMap>

    <sql id="selectMenuRecipeVo">
        select id, recipe_id, plan_id, recipe_name, is_default, recipe_type, effect_date, expire_date, canteen_id, stall_id, enable_flag, revision, create_by, create_time, update_by, update_time, remark from menu_recipe
    </sql>

    <select id="selectMenuRecipeList" parameterType="com.bonus.canteen.core.menu.domain.MenuRecipe" resultType="com.bonus.canteen.core.menu.vo.MenuRecipeVO">

<!--        select ala.area_id as areaId,ala.area_name as areaName,ac.canteen_id as canteenId,ac.canteen_name as canteenName,-->
<!--        als.stall_id as stallId,als.stall_name as stallName,map.recipe_id as recipeId,mr.recipe_name as recipeName-->
<!--        from menu_recipe mr-->
<!--        LEFT JOIN alloc_canteen ac on ac.canteen_id = mr.canteen_id-->
<!--        LEFT JOIN alloc_stall als on als.stall_id = mr.stall_id-->
<!--        LEFT JOIN alloc_area ala on ala.area_id = ac.area_id-->
<!--        LEFT JOIN menu_app_recipe map on map.recipe_id = mr.recipe_id-->
<!--        WHERE ala.area_id is not null and map.bind_type = #{key}-->

<!--        AND EXISTS (-->
<!--        SELECT NULL-->
<!--        FROM-->
<!--        device_bind t6-->
<!--        LEFT JOIN device_info t7 ON t7.device_id = t6.device_id-->
<!--        WHERE-->
<!--        t6.canteen_id = ac.canteen_id-->
<!--        AND t6.stall_id = als.stall_id-->
<!--        AND t7.device_type IN-->
<!--        <foreach collection="param.deviceTypeList" item="deviceType" separator="," open="(" close=")">-->
<!--            #{deviceType}-->
<!--        </foreach>-->
<!--        )-->


        SELECT
        t3.area_id as areaId, t3.area_name as areaName, t2.canteen_id as canteenId,
        t2.canteen_name as canteenName, t1.stall_id as stallId, t1.stall_name as stallName,
        a.recipe_id as recipeId,a.recipe_name as recipeName
        FROM
        alloc_stall t1
        LEFT JOIN alloc_canteen t2 ON t1.canteen_id = t2.canteen_id
        LEFT JOIN alloc_area t3 ON t2.area_id = t3.area_id
        LEFT JOIN(
        select
        mar.recipe_id,
        mr.stall_id,
        mar.bind_type,
        mr.recipe_name
        from menu_app_recipe mar
        left join menu_recipe mr on mar.recipe_id = mr.recipe_id
        where bind_type = #{bindType}
        ) a on a.stall_id =t1.stall_id
        <if test="deviceTypeList != null and deviceTypeList.size() > 0">
 and
            EXISTS (
            SELECT NULL
            FROM
            device_bind t6
            LEFT JOIN device_info t7 ON t7.device_id = t6.device_id
            WHERE
            t6.canteen_id = t2.canteen_id
            AND t6.stall_id = t1.stall_id
            AND t7.device_type IN
            <foreach collection="deviceTypeList" item="deviceType" separator="," open="(" close=")">
                #{deviceType}
            </foreach>
            )
        </if>

        where 1= 1

                <if test="areaId !=null">
                    and t3.area_id  = #{areaId}
                </if>
                <if test="canteenId !=null">
                    and t2.canteen_id  = #{canteenId}
                </if>
                <if test="stallId !=null ">
                    and t1.stall_id = #{stallId}
                </if>

<!--        <if test="areaIds !=null and areaIds.size()>0">-->
<!--            and t3.area_id in-->
<!--            <foreach collection="areaIds" open="(" close=")" item="item" separator=",">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->
<!--        <if test="canteenIds !=null and canteenIds.size()>0">-->
<!--            and t2.canteen_id in-->
<!--            <foreach collection="canteenIds" open="(" close=")" item="item" separator=",">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->
<!--        <if test="stallIds !=null and stallIds.size()>0">-->
<!--            and t1.stall_id in-->
<!--            <foreach collection="stallIds" open="(" close=")" item="item" separator=",">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->

        <if test="recipeName !=null and recipeName !=''">
            and a.recipe_name like concat('%', #{recipeName}, '%')
        </if>

        <!--        <include refid="selectMenuRecipeVo"/>-->
<!--        <where>  -->
<!--            <if test="recipeId != null "> and recipe_id = #{recipeId}</if>-->
<!--            <if test="planId != null  and planId != ''"> and plan_id = #{planId}</if>-->
<!--            <if test="recipeName != null  and recipeName != ''"> and recipe_name like concat('%', #{recipeName}, '%')</if>-->
<!--            <if test="isDefault != null "> and is_default = #{isDefault}</if>-->
<!--            <if test="recipeType != null "> and recipe_type = #{recipeType}</if>-->
<!--            <if test="effectDate != null "> and effect_date = #{effectDate}</if>-->
<!--            <if test="expireDate != null "> and expire_date = #{expireDate}</if>-->
<!--            <if test="canteenId != null "> and canteen_id = #{canteenId}</if>-->
<!--            <if test="stallId != null "> and stall_id = #{stallId}</if>-->
<!--            <if test="enableFlag != null "> and enable_flag = #{enableFlag}</if>-->
<!--            <if test="revision != null "> and revision = #{revision}</if>-->
<!--        </where>-->
    </select>

    <select id="getDishesListByRecipeId" parameterType="string" resultType="com.bonus.canteen.core.menu.vo.MenuRecipeMealtimeTypeVO">
<!--        餐次类型 1-早餐 2-午餐 3-晚餐 4-下午茶 5-夜宵-->
        select mealtime_type as mealtimeType,
        CASE
        WHEN mealtime_type = 1 THEN '早餐'
        WHEN mealtime_type = 2 THEN '午餐'
        WHEN mealtime_type = 3 THEN '晚餐'
        WHEN mealtime_type = 4 THEN '下午茶'
        WHEN mealtime_type = 5 THEN '夜宵'
        ELSE '无效参数'
        END AS mealtimeName,
        detail_id as detailId
        from menu_recipe_detail  WHERE recipe_id =#{recipeId} and apply_date = #{applyDate}
        GROUP BY mealtime_type ORDER BY mealtime_type
    </select>

    <select id="getDishesListByDetailId" parameterType="string" resultType="com.bonus.canteen.core.menu.vo.MenuRecipeDishesVO">
        select mrd.id,mrd.dishes_id as dishesId,mud.dishes_name as  dishesName,
        mud.dishes_num as dishesNum,mud.meal_type,mud.material_cost as materialCost,
        mrd.price,mrd.size_type as sizeType,mrd.supply_num as supplyNum,mrd.sale_num as saleNum,mrd.surplus_num as surplusNum,
        mrd.restrict_num as restrictNum,mrd.sale_price as salePrice,mrd.recommend_flag as recommendFlag,mrd.sort_num as sortNum,
        mdt.type_name as typeName,alc.canteen_name as alc,mud.sales_mode as salesMode,mud.unit_price as unitPrice
        from (select * from menu_recipe_dishes where detail_id = #{detailId} ) mrd
        LEFT JOIN menu_dishes mud on mud.dishes_id = mrd.dishes_id
        LEFT JOIN menu_dishes_type mdt on mdt.type_id = mud.type_id
        LEFT JOIN alloc_canteen alc on alc.canteen_id = mud.canteen_id

    </select>

    <select id="selectMenuRecipeById" parameterType="Long" resultMap="MenuRecipeResult">
        <include refid="selectMenuRecipeVo"/>
        where id = #{id}
    </select>
    <select id="getDishesList" resultType="com.bonus.canteen.core.menu.vo.MenuRecipeV2VO">
        select
            mr.recipe_id,
            mr.recipe_name,
            mr.recipe_type,
            ac.canteen_id,
            ac.canteen_name,
            aa.area_id,
            aa.area_name,
            a.stall_id,
            a.stall_name,
            mr.create_time,
            mr.create_by
        from
            menu_recipe mr
                left join alloc_canteen ac on mr.canteen_id = ac.canteen_id
                left join alloc_stall a on mr.stall_id = a.stall_id
                left join alloc_area aa on  ac.area_id = aa.area_id
        where  1 = 1
        <if test="recipeId != null ">
            and mr.recipe_id = #{recipeId}
        </if>
        <if test="areaIds != null and areaIds.size >0">
            and  aa.area_id in
            <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
                #{areaId}
            </foreach>
        </if>
        <if test="recipeName != null and recipeName != ''">
            and mr.recipe_name like concat('%', #{recipeName}, '%')
        </if>
        <if test="canteenId != null and canteenId !=-1">
            and mr.canteen_id = #{canteenId}
        </if>
        <if test="canteenIds != null and canteenIds.size >0">
            and  ac.canteen_id in
            <foreach collection="canteenIds" item="canteenId" open="(" separator="," close=")">
                #{canteenId}
            </foreach>
        </if>
        <if test="stallId != null and stallId !=-1">
            and mr.stall_id = #{stallId}
        </if>
        <if test="stallIds != null and stallIds.size >0">
            and  a.stall_id in
            <foreach collection="stallIds" item="stallId" open="(" separator="," close=")">
                #{stallId}
            </foreach>
        </if>
        <if test="areaId != null and areaId !=-1">
            and ac.area_id = #{areaId}
        </if>
        <if test="areaIds != null and areaIds.size >0">
            and  ac.area_id in
            <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
                #{areaId}
            </foreach>
        </if>
        <if test="recipeType != null">
            and mr.recipe_type = #{recipeType}
        </if>
    </select>
    <select id="getDishesCountByRecipeIds" resultType="com.bonus.canteen.core.menu.dto.RecipeDishesCountDTO">
        SELECT
        mrd.recipe_id,count(DISTINCT(md.base_dishes_id)) as num
        FROM
        menu_recipe_detail mrd
        LEFT JOIN menu_recipe_dishes mrdd ON mrd.detail_id = mrdd.detail_id
        LEFT JOIN menu_dishes md ON md.dishes_id = mrdd.dishes_id
        where
        mrd.apply_date >= curdate()
        <if test="recipeIds !=null and recipeIds.size()>0">
            AND mrd.recipe_id in
            <foreach collection="recipeIds" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        GROUP BY mrd.recipe_id
    </select>
    <select id="selectApplyDateListByRecipeId" resultType="com.bonus.canteen.core.menu.vo.MenuRecipeDataVO">
        SELECT recipe_id as recipeId, apply_date as applyData
        FROM menu_recipe_detail
        WHERE apply_date >= #{applyDate}
        <if test="recipeIds !=null and recipeIds.size()>0">
            AND recipe_id IN
            <foreach collection="recipeIds" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>

    </select>
    <resultMap id="menuBaseModel" type="com.bonus.canteen.core.menu.model.MenuBaseModel">
        <result property="baseDishesId" column="base_dishes_id"/>
        <collection property="dishesList" ofType="com.bonus.canteen.core.menu.model.MenuBaseDishesModel">
            <result property="dishesId" column="dishes_id"/>
            <result property="sizeJson" column="size_json"/>
        </collection>
    </resultMap>
    <select id="selectBaseSizeDishesId" resultMap="menuBaseModel">
        select
        base_dishes_id,
        dishes_id,
        size_json
        from
        menu_dishes
        where
        base_dishes_id in
        <foreach collection="baseDishesIdList" item="baseDishesId" separator="," open="(" close=")">
            #{baseDishesId}
        </foreach>
    </select>


    <select id="selectCount" resultType="java.lang.Long">
        select
            count(1)
        from menu_recipe
        where 1 = 1
        <if test="recipeName != null and recipeName != ''">
            and recipe_name = #{recipeName}
        </if>
    </select>
    <select id="getOneById" resultType="com.bonus.canteen.core.menu.domain.MenuRecipe">
        select recipe_id as recipeId,
               plan_id as planId,
               recipe_name as recipeName,
               is_default as isDefault,
               recipe_type as recipeType,
               effect_date as effectDate,
               expire_date as expireDate,
               canteen_id as canteenId,
               stall_id as stallId,
               enable_flag as enableFlag,
               revision,
               create_by as createBy,
               create_time as createTime,
               update_by as updateBy,
               update_time as updateTime,
               remark
        from menu_recipe
        where recipe_id = #{recipeId}
    </select>
    <!-- MenuRecipeMapper.xml -->
    <select id="selectDetailIds" resultType="java.lang.Long">
        SELECT detail_id
        FROM menu_recipe_detail
        <where>
            recipe_id = #{dto.recipeId}
            AND detail_type = 1
            <choose>
                <!-- 当菜谱类型为3（周类型） -->
                <when test="dto.recipeType == 3">
                    <choose>
                        <!-- 完整模式 -->
                        <when test="dto.isComplete">
                            AND apply_week IN
                            <foreach collection="dto.weeks" item="week" open="(" separator="," close=")">
                                #{week}
                            </foreach>
                        </when>
                        <!-- 非完整模式 -->
                        <otherwise>
                            AND (
                            <foreach collection="dto.menuRecipeV2Add.recipeDateList" item="dateDTO" separator=" OR ">
                                <if test="dateDTO != null and dateDTO.detailList != null and !dateDTO.detailList.isEmpty()">
                                    (
                                    <foreach collection="dateDTO.detailList" item="detailDTO" separator=" OR ">
                                        (apply_week = #{dateDTO.applyWeek} AND mealtime_type = #{detailDTO.mealtimeType})
                                    </foreach>
                                    )
                                </if>
                            </foreach>
                            )
                        </otherwise>
                    </choose>
                </when>

                <!-- 其他类型且非完整模式 -->
                <when test="!dto.isComplete">
                    AND mealtime_type = #{dto.mealType}
                </when>
            </choose>
        </where>
    </select>
    <select id="selectDetailsList" resultType="com.bonus.canteen.core.menu.domain.MenuRecipeDetail">
        SELECT *
        FROM menu_recipe_detail
        <where>
            recipe_id = #{dto.recipeId}
            AND apply_date >= #{currentDate}
            AND detail_type = 2
            <!-- 动态条件 -->
            <choose>
                <!-- 当菜谱类型为3（周类型） -->
                <when test="dto.recipeType == 3">
                    <choose>
                        <!-- 完全模式 -->
                        <when test="dto.isComplete">
                            AND apply_week IN
                            <foreach collection="dto.weeks" item="week" open="(" separator="," close=")">
                                #{week}
                            </foreach>
                        </when>
                        <!-- 非完全模式 -->
                        <otherwise>
                            AND (
                            <foreach collection="dto.menuRecipeV2Add.recipeDateList" item="dateDTO" separator=" OR ">
                                <if test="dateDTO != null and dateDTO.detailList != null and !dateDTO.detailList.isEmpty()">
                                    (
                                    <foreach collection="dateDTO.detailList" item="detailDTO" separator=" OR ">
                                        (apply_week = #{dateDTO.applyWeek} AND mealtime_type = #{detailDTO.mealtimeType})
                                    </foreach>
                                    )
                                </if>
                            </foreach>
                            )
                        </otherwise>
                    </choose>
                </when>

                <!-- 其他类型且非完整模式 -->
                <when test="!dto.isComplete">
                    AND mealtime_type = #{mealType}
                </when>
            </choose>
        </where>
    </select>
    <select id="getMenuRecipeDishesList" resultType="com.bonus.canteen.core.menu.domain.MenuRecipeDishes">
        SELECT id,
               detail_id AS detailId,
               dishes_id as  dishesId,
               price,
               size_type as sizeType,
               supply_num as supplyNum,
               sale_num as saleNum,
               surplus_num as surplusNum,
               restrict_num as restrictNum,
               sale_price as salePrice,
               sale_start as saleStart,
               sale_end as saleEnd,
               chef_id as chefId,
               recommend_flag as recommendFlag,
               sort_num as sortNum,
               revision,
               create_by as createBy,
               create_time as createTime,
               update_by as updateBy,
               update_time as updateTime,
               remark
        FROM menu_recipe_dishes
        where
        detail_id IN
        <foreach collection="haveDetailIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="selectMenuRecipeDetail" resultType="com.bonus.canteen.core.menu.domain.MenuRecipeDetail">
        SELECT
        id,
        detail_id AS detailId,
        sale_type_id AS saleTypeId,
        recipe_id AS recipeId,
        apply_date AS applyDate,
        apply_week AS applyWeek,
        apply_month AS applyMonth,
        mealtime_type AS mealtimeType,
        detail_type AS detailType,
        revision
        FROM menu_recipe_detail
        <where>
            <!-- 固定条件 -->
            recipe_id = #{bean.recipeId}
            AND apply_date >= #{bean.applyDate}
            AND apply_date &lt;= #{bean.endDate}
            AND detail_type = #{bean.detailType}

            <!-- 可扩展的动态条件（示例） -->
            <if test="bean.mealtimeType != null">
                AND mealtime_type = #{bean.mealtimeType}
            </if>
            <if test="weeks != null and weeks.size() > 0">
                and apply_week in
                <foreach collection="weeks" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectDetailIdListByWrapper" resultType="java.lang.Long">
        SELECT detail_id FROM menu_recipe_detail
        WHERE del_flag = 2
        AND recipe_id = #{recipeId}
        AND apply_date IN
        <foreach item="date" collection="applyDateList" open="(" separator="," close=")">
            #{date}
        </foreach>
        <if test="isComplete == false">
            AND (
            <foreach item="dateItem" collection="recipeDateList" separator=" OR ">
                <if test="dateItem != null and dateItem.detailList != null and !dateItem.detailList.isEmpty()">
                    (
                    apply_date = #{dateItem.applyDate}
                    AND mealtime_type IN
                    <foreach item="detail" collection="dateItem.detailList" open="(" separator="," close=")">
                        #{detail.mealtimeType}
                    </foreach>
                    )
                </if>
            </foreach>
            )
        </if>
    </select>
    <select id="selectBatchIds" resultType="com.bonus.canteen.core.menu.domain.MenuRecipeDishes">
        select
            id,
            detail_id as detailId,
            dishes_id as dishesId,
            price,
            size_type as sizeType,
            supply_num as supplyNum,
            sale_num as saleNum,
            surplus_num as surplusNum,
            restrict_num as restrictNum,
            sale_price as salePrice,
            sale_start as saleStart,
            sale_end as saleEnd,
            chef_id as chefId,
            recommend_flag as recommendFlag,
            sort_num as sortNum,
            revision,
            create_by as createBy,
            create_time as createTime,
            update_by as updateBy,
            update_time as updateTime,
            remark
        from menu_recipe_dishes
        where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </select>
    <select id="selectAppRecipeList" resultType="com.bonus.canteen.core.menu.domain.MenuAppRecipe">
            select
                DISTINCT
                recipe_id as recipeId,
                bind_type as bindType,
                meal_line_id as mealLineId,
                bind_time as bindTime
            from menu_app_recipe
            where recipe_id = #{recipeId}
    </select>
    <select id="getDetail" resultMap="menuRecipeDetailDateVO">
        select DISTINCT
        mrd.mealtime_type,
        mrd.detail_id,
        mdb.base_dishes_id ,
        md.dishes_id,
        mdb.dishes_name,
        mrdd.id,
        mrdd.price,
        mrdd.size_type,
        mrdd.supply_num,
        mrdd.sale_num,
        mrdd.surplus_num,
        mrdd.restrict_num,
        mrdd.sale_price,
        mrdd.recommend_flag,
        mrdd.sort_num,
        ac.canteen_name,
        md.sales_mode,
        md.unit_price,
        md.meal_type,
        md.dishes_num,
        md.type_id,
        mdt.type_name,
        md.material_cost
        from menu_recipe_detail mrd
        left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
        left join menu_dishes md on mrdd.dishes_id = md.dishes_id
        left join alloc_canteen ac on md.canteen_id = ac.canteen_id
        left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where mrd.recipe_id = #{params.recipeId}
        <if test="params.applyWeek !=null and params.applyWeek != ''">
            and mrd.apply_week = #{params.applyWeek}
        </if>
        and mrd.detail_type = 1
        order by mrdd.sort_num
    </select>

    <select id="getRecipeBindCount" resultType="java.lang.Integer">
        select DISTINCT
            count(device_id)
        from device_recipe
        where recipe_id = #{recipeId}
    </select>
    <select id="getDetailIdList" resultType="java.lang.Long">
        select
            detail_id as detailId
        from menu_recipe_detail
        where recipe_id = #{recipeId}
    </select>
    <select id="selectMenuAppRecipeCount" resultType="java.lang.Integer">
        select
            count(1)
        from menu_app_recipe
        where recipe_id = #{recipeId}
    </select>
    <select id="selectTypeDishesList" resultType="com.bonus.canteen.core.menu.vo.MenuAllTypeDishesVO">
        select
        mdb.base_dishes_id as dishes_id,
        mdb.dishes_name,
        md.price,
        ac.canteen_name,
        ass.stall_name,
        aa.area_name,
        md.sales_mode,
        md.size_type,
        md.public_dishes,
        md.unit_price,
        md.little_price,
        md.large_price,
        md.material_cost,
        md.meal_type,
        md.type_id,
        mdt.type_name,
        md.dishes_num
        from
        menu_dishes_base mdb
        left join menu_dishes md on mdb.base_dishes_id = md.base_dishes_id
        and md.size_json != '{"sizeType":"小份"}'
        left join alloc_area aa on aa.area_id = md.area_id
        left join alloc_canteen ac on md.canteen_id = ac.canteen_id
        left join alloc_stall ass on ass.stall_id = md.shopstall_id
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where
        1 =1
        <if test="typeId != null">
            and md.type_id = #{typeId}
        </if>
        <if test="canteenId != null and stallId != null and areaId != null">
            and(
            (md.canteen_id = #{canteenId} and md.shopstall_id = #{stallId})
            or (md.canteen_id = #{canteenId} and md.shopstall_id = -1)
            or (md.canteen_id = -1 and md.shopstall_id = -1 and md.area_id = #{areaId})
            or (md.canteen_id = -1 and md.shopstall_id = -1 and md.area_id = -1)
            )
        </if>
        <if test="areaIds != null and areaIds.size >0">
            and md.area_id in
            <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
                #{areaId}
            </foreach>
        </if>
        <if test="canteenIds != null and canteenIds.size >0">
            and md.canteen_id in
            <foreach collection="canteenIds" item="canteenId" open="(" separator="," close=")">
                #{canteenId}
            </foreach>
        </if>
        <if test="stallIds != null and stallIds.size >0">
            and md.shopstall_id in
            <foreach collection="stallIds" item="stallId" open="(" separator="," close=")">
                #{stallId}
            </foreach>
        </if>
        <if test="dishesName != null and dishesName != ''">
            and (md.dishes_name like #{dishesName}
            or md.pinyin_full like #{dishesName}
            or md.pinyin_initials like #{dishesName})
        </if>
        <if test="salesMode != null">
            and md.sales_mode = #{salesMode}
        </if>
    </select>
    <resultMap id="menuRecipeDetailDateVO" type="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDateVO">
        <result property="mealtimeType" column="mealtime_type"/>
        <result property="detailId" column="detail_id"/>
        <collection property="dishesList" ofType="com.bonus.canteen.core.menu.vo.MenuRecipeDetailDishesVO">
            <result property="id" column="id"/>
            <result property="dishesId" column="dishes_id"/>
            <result property="baseDishesId" column="base_dishes_id"/>
            <result property="dishesName" column="dishes_name"/>
            <result property="price" column="price"/>
            <result property="sizeType" column="size_type"/>
            <result property="supplyNum" column="supply_num"/>
            <result property="saleNum" column="sale_num"/>
            <result property="surplusNum" column="surplus_num"/>
            <result property="restrictNum" column="restrict_num"/>
            <result property="salePrice" column="sale_price"/>
            <result property="recommendFlag" column="recommend_flag"/>
            <result property="canteenName" column="canteen_name"/>
            <result property="salesMode" column="sales_mode"/>
            <result property="unitPrice" column="unit_price"/>
            <result property="mealType" column="meal_type"/>
            <result property="dishesNum" column="dishes_num"/>
            <result property="materialCost" column="material_cost"/>
            <result property="sortNum" column="sort_num"/>
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
        </collection>
    </resultMap>
    <!-- 获取指定日期菜谱详情 -->
    <select id="selectDateDetailList" resultMap="menuRecipeDetailDateVO">
        select mrd.mealtime_type,
               mrd.detail_id,
                md.dishes_id,
               mdb.base_dishes_id,
               mdb.dishes_name,
               mrdd.id,
               mrdd.price,
               mrdd.size_type,
               mrdd.supply_num,
               mrdd.sale_num,
               mrdd.surplus_num,
               mrdd.restrict_num,
               mrdd.sale_price,
               mrdd.recommend_flag,
               mrdd.sort_num,
               ac.canteen_name,
               md.sales_mode,
               md.unit_price,
               md.meal_type,
               md.dishes_num,
               md.type_id,
               mdt.type_name,
               md.material_cost
        from menu_recipe_detail mrd
                 left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
                 left join menu_dishes md on mrdd.dishes_id = md.dishes_id
                 left join alloc_canteen ac on md.canteen_id = ac.canteen_id
                 left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
                 left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where mrd.recipe_id = #{recipeId}
          and mrd.apply_date = #{applyDate}
        order by mrdd.sort_num
    </select>
    <select id="getRecipeIdBySn" resultType="java.lang.Long">
        select ifnull(recipe_id,0) as recipeId
        from device_recipe
        where device_id = (select device_id from device_info where device_sn = #{machineSn} limit 1) limit 1
    </select>
    <select id="selectMenuRecipeDetailLists" resultType="com.bonus.canteen.core.menu.vo.MenuAndroidRecipeDetailVO">
        select
            detail_id as detailId,
            mealtime_type as mealTimeType,
            apply_date as applyData
        from menu_recipe_detail where recipe_id = #{recipeId} and apply_date >= Date(NOW())
    </select>
    <select id="selectRecipeDishList" resultType="com.bonus.canteen.core.menu.vo.IssueRecipeDetailCompressVO">
        select
            detail_id as detailId,
            dishes_id as dishesId,
            price as salePrice,
            sale_price as prefPrice,
            supply_num as supplyNum,
            surplus_num as surplusNum,
            restrict_num as restrictNum
        from menu_recipe_dishes where detail_id in
        <foreach collection="detailIds" item="detailId" separator="," open="(" close=")">
            #{detailId}
        </foreach>
    </select>
    <select id="getMenuRecipeListByDishesIds"
            resultType="com.bonus.canteen.core.menu.vo.IssueRecipeMealCompressVO">
        select
            md.dishes_id as dishesId,
            md.custom_id as customId,
            md.dishes_name as productName,
            md.price as originalPrice,
            md.sales_mode as salesMode,
            md.weight_deviation as weightDeviation,
            md.weight,
            mdt.type_id,
            mdt.type_name,
            md.pinyin_initials as pinyinInitials,
            md.pinyin_full as pinyinFull,
            md.purine,
            md.iodine,
            md.initial_score as initialScore,
            md.calories,
            md.protein,
            md.fat,
            md.carbohydrate,
            md.dietary_fiber as dietaryFiber,
            md.cholesterol,
            md.calcium,
            md.sodium,
            md.intro
        from menu_dishes md
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where dishes_id in
        <foreach collection="dishesIds" item="dishesId" separator="," open="(" close=")">
            #{dishesId}
        </foreach>
    </select>
    <insert id="insertMenuRecipe" parameterType="com.bonus.canteen.core.menu.domain.MenuRecipe" useGeneratedKeys="true" keyProperty="id">
        insert into menu_recipe
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recipeId != null">recipe_id,</if>
            <if test="planId != null">plan_id,</if>
            <if test="recipeName != null and recipeName != ''">recipe_name,</if>
            <if test="isDefault != null">is_default,</if>
            <if test="recipeType != null">recipe_type,</if>
            <if test="effectDate != null">effect_date,</if>
            <if test="expireDate != null">expire_date,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="stallId != null">stall_id,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="revision != null">revision,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recipeId != null">#{recipeId},</if>
            <if test="planId != null">#{planId},</if>
            <if test="recipeName != null and recipeName != ''">#{recipeName},</if>
            <if test="isDefault != null">#{isDefault},</if>
            <if test="recipeType != null">#{recipeType},</if>
            <if test="effectDate != null">#{effectDate},</if>
            <if test="expireDate != null">#{expireDate},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="stallId != null">#{stallId},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="revision != null">#{revision},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>
    <insert id="insertData">
        insert into menu_recipe
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recipeId != null">recipe_id,</if>
            <if test="planId != null">plan_id,</if>
            <if test="recipeName != null and recipeName != ''">recipe_name,</if>
            <if test="isDefault != null">is_default,</if>
            <if test="recipeType != null">recipe_type,</if>
            <if test="effectDate != null">effect_date,</if>
            <if test="expireDate != null">expire_date,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="stallId != null">stall_id,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="revision != null">revision,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recipeId != null">#{recipeId},</if>
            <if test="planId != null">#{planId},</if>
            <if test="recipeName != null and recipeName != ''">#{recipeName},</if>
            <if test="isDefault != null">#{isDefault},</if>
            <if test="recipeType != null">#{recipeType},</if>
            <if test="effectDate != null">#{effectDate},</if>
            <if test="expireDate != null">#{expireDate},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="stallId != null">#{stallId},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="revision != null">#{revision},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>
    <insert id="insertMenuDetails">
        <foreach collection="menuDetails" item="menuDetail" separator=";">
        insert into menu_recipe_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuDetail.detailId != null">detail_id,</if>
            <if test="menuDetail.saleTypeId != null">sale_type_id,</if>
            <if test="menuDetail.recipeId != null">recipe_id,</if>
            <if test="menuDetail.applyDate != null">apply_date,</if>
            <if test="menuDetail.applyWeek != null">apply_week,</if>
            <if test="menuDetail.mealtimeType != null">mealtime_type,</if>
            <if test="menuDetail.detailType != null">detail_type,</if>
            <if test="menuDetail.revision != null">revision,</if>
            <if test="menuDetail.createBy != null">create_by,</if>
            <if test="menuDetail.createTime != null">create_time,</if>
            <if test="menuDetail.updateBy != null">update_by,</if>
            <if test="menuDetail.updateTime != null">update_time,</if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuDetail.detailId != null">#{menuDetail.detailId},</if>
            <if test="menuDetail.saleTypeId != null">#{menuDetail.saleTypeId},</if>
            <if test="menuDetail.recipeId != null">#{menuDetail.recipeId},</if>
            <if test="menuDetail.applyDate != null">#{menuDetail.applyDate},</if>
            <if test="menuDetail.applyWeek != null">#{menuDetail.applyWeek},</if>
            <if test="menuDetail.mealtimeType != null">#{menuDetail.mealtimeType},</if>
            <if test="menuDetail.detailType != null">#{menuDetail.detailType},</if>
            <if test="menuDetail.revision != null">#{menuDetail.revision},</if>
            <if test="menuDetail.createBy != null">#{menuDetail.createBy},</if>
            <if test="menuDetail.createTime != null">#{menuDetail.createTime},</if>
            <if test="menuDetail.updateBy != null">#{menuDetail.updateBy},</if>
            <if test="menuDetail.updateTime != null">#{menuDetail.updateTime},</if>
        </trim>
        </foreach>
    </insert>
    <insert id="insertMenuDishes">
        <foreach collection="menuDishes" item="menuDish" separator=";">
        insert into menu_recipe_dishes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuDish.detailId != null">detail_id,</if>
            <if test="menuDish.dishesId != null">dishes_id,</if>
            <if test="menuDish.price != null">price,</if>
            <if test="menuDish.sizeType != null">size_type,</if>
            <if test="menuDish.supplyNum != null">supply_num,</if>
            <if test="menuDish.saleNum != null">sale_num,</if>
            <if test="menuDish.surplusNum != null">surplus_num,</if>
            <if test="menuDish.restrictNum != null">restrict_num,</if>
            <if test="menuDish.salePrice != null">sale_price,</if>
            <if test="menuDish.recommendFlag != null">recommend_flag,</if>
            <if test="menuDish.revision != null">revision,</if>
            <if test="menuDish.createBy != null">create_by,</if>
            <if test="menuDish.createTime != null">create_time,</if>
            <if test="menuDish.updateBy != null">update_by,</if>
            <if test="menuDish.updateTime != null">update_time,</if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuDish.detailId != null">#{menuDish.detailId},</if>
            <if test="menuDish.dishesId != null">#{menuDish.dishesId},</if>
            <if test="menuDish.price != null">#{menuDish.price},</if>
            <if test="menuDish.sizeType != null">#{menuDish.sizeType},</if>
            <if test="menuDish.supplyNum != null">#{menuDish.supplyNum},</if>
            <if test="menuDish.saleNum != null">#{menuDish.saleNum},</if>
            <if test="menuDish.surplusNum != null">#{menuDish.surplusNum},</if>
            <if test="menuDish.restrictNum != null">#{menuDish.restrictNum},</if>
            <if test="menuDish.salePrice != null">#{menuDish.salePrice},</if>
            <if test="menuDish.recommendFlag != null">#{menuDish.recommendFlag},</if>
            <if test="menuDish.revision != null">#{menuDish.revision},</if>
            <if test="menuDish.createBy != null">#{menuDish.createBy},</if>
            <if test="menuDish.createTime != null">#{menuDish.createTime},</if>
            <if test="menuDish.updateBy != null">#{menuDish.updateBy},</if>
            <if test="menuDish.updateTime != null">#{menuDish.updateTime},</if>
        </trim>
        </foreach>
    </insert>
    <update id="updateMenuRecipe" parameterType="com.bonus.canteen.core.menu.domain.MenuRecipe">
        update menu_recipe
        <trim prefix="SET" suffixOverrides=",">
            <if test="recipeId != null">recipe_id = #{recipeId},</if>
            <if test="planId != null">plan_id = #{planId},</if>
            <if test="recipeName != null and recipeName != ''">recipe_name = #{recipeName},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="recipeType != null">recipe_type = #{recipeType},</if>
            <if test="effectDate != null">effect_date = #{effectDate},</if>
            <if test="expireDate != null">expire_date = #{expireDate},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="stallId != null">stall_id = #{stallId},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="updateMenuRecipeDetail">
        update menu_recipe_detail set update_time = #{time} where recipe_id = #{recipeId}
    </update>

    <delete id="deleteMenuRecipeById" parameterType="Long">
        delete from menu_recipe where id = #{id}
    </delete>

    <delete id="deleteMenuRecipeByIds" parameterType="String">
        delete from menu_recipe where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteMenuRecipeDetailByIds">
        delete from menu_recipe_detail where detail_id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteMenuRecipeDishesByIds">
        delete from menu_recipe_dishes where detail_id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectMenuRecipe" resultMap="MenuRecipeResult"  parameterType="long">
    select * from menu_recipe where recipe_id = #{recipeId}
    </select>

    <select id="getCanNum" resultType="com.bonus.canteen.core.alloc.domain.AllocCanteen">
        select canteen_id as canteenId,if_reserve as ifReserve from alloc_canteen
        where  canteen_id = #{canteenId}
    </select>

    <select id="getStallNum" resultType="com.bonus.canteen.core.alloc.domain.AllocStall">
        select stall_id as stallId,if_reserve as ifReserve from alloc_stall
        where stall_id = #{areaId}
    </select>

    <select id="exists" resultType="boolean">
        select count(1) > 0 from menu_app_recipe
        where recipe_id = #{recipeId}
        <if test="mealLineId !=null">
            and meal_line_id = #{mealLineId}
        </if>
        and bind_type = #{bindType}
    </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>

    <delete id="delete" parameterType="com.bonus.canteen.core.menu.domain.MenuAppRecipe">
        delete from menu_app_recipe where recipe_id in
        <foreach collection="recipeIds" item="recipeId" separator="," open="(" close=")">
            #{recipeId}
        </foreach>
        <if test="mealLineId != null">
             and meal_line_id = #{mealLineId}
        </if>
        and bind_type = #{bindType}
    </delete>

    <insert id="insert" parameterType="com.bonus.canteen.core.menu.domain.MenuAppRecipe">

    insert into menu_app_recipe (recipe_id,bind_type,bind_time,meal_line_id)
        values (#{recipeId},#{bindType},now(),#{mealLineId})
    </insert>

    <select id="getSnByCanteenId" resultType="string">
        select
            device_id as deviceId
        where canteen_id = #{canteenId}

    </select>
    <select id="getMealTimeList" resultType="com.bonus.canteen.core.menu.vo.MealTimeVO">
        SELECT
        mealtime_type AS mealTimeType,
        mealtime_name AS mealTimeName,
        start_time AS startTime,
        end_time AS endTime
        FROM alloc_stall_mealtime
        WHERE stall_id = #{stallId}
    </select>
    <select id="getBasicsTimeIntervals" resultType="com.bonus.canteen.core.menu.vo.DeviceMealtimeVO">
        select
            mealtime_type as intervalId,
            mealtime_name as intervalName,
            start_time as startTime,
            end_time as endTime,
            '1' as ifUse
        FROM alloc_stall_mealtime
        where stall_id = #{stallId}
    </select>

    <delete id="deleteRecipeByrecipeId">
        delete from menu_recipe where recipe_id = #{recipeId}
    </delete>

    <update id="updateRecipeByCanteeStallMeal">
        update device_recipe  set recipe_id = #{ recipeId}
        where canteen_id = #{canteenId} and stall_id = #{stallId}
        and device_id in
        <foreach collection="deviceIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </update>
     <select id="getDeviceIdByDevivce" parameterType="string" resultType="integer">
         select a.device_id
         from device_info a
         LEFT JOIN device_bind b on a.device_id = b.device_id
         where  b.stall_id = #{stallId}
     </select>



    <select id="MenuRecipeDetailList" resultType="com.bonus.canteen.core.menu.domain.MenuRecipeDetail">
        select id,detail_id as detailId,sale_type_id as saleTypeId,recipe_id as recipeId,apply_date as applyDate,
        apply_week as applyWeek,apply_month as applyMonth,mealtime_type as mealtimeType,detail_type as detailType,revision
        from menu_recipe_detail
        where recipe_id = #{recipeId}


    </select>

    <select id="getMenuRecipeList" resultType="com.bonus.canteen.core.menu.vo.MenuRecipeV2PageVO">
        select
        mr.recipe_id as recipeId,
        mr.recipe_name as recipeName,
        mr.recipe_type as recipeType,
        ac.canteen_id as canteenId,
        ac.canteen_name as canteenName,
        aa.area_id as areaId,
        aa.area_name as areaName,
        a.stall_id as stallId,
        a.stall_name as stallName
        from
        menu_recipe mr
        left join alloc_canteen ac on mr.canteen_id = ac.canteen_id
        left join alloc_stall a on mr.stall_id = a.stall_id
        left join alloc_area aa on  ac.area_id = aa.area_id
        where 1=1
        <if test="recipeId != null ">
            and mr.recipe_id = #{recipeId}
        </if>
        <if test="areaId ">
            and  aa.area_id  = #{areaId}
        </if>
        <if test="recipeName != null and recipeName != ''">
            and mr.recipe_name like concat('%', #{recipeName}, '%')
        </if>
        <if test="canteenId != null and canteenId !=-1">
            and ac.canteen_id = #{canteenId}
        </if>
        <if test="content.stallId != null and content.stallId !=-1">
            and a.stall_id = #{content.stallId}
        </if>

        <if test="content.areaId != null and content.areaId !=-1">
            and aa.area_id = #{content.areaId}
        </if>

        <if test="content.recipeType != null">
            and mr.recipe_type = #{content.recipeType}
        </if>
        order by
        mr.crtime desc

    </select>

    <select id="getMenuDishDeatilList" resultType="com.bonus.canteen.core.menu.domain.MenuRecipeDetail">
        SELECT
        recipe_id as recipeId,apply_date as applyDate
        FROM
        menu_recipe_detail

        where
        apply_date >= curdate()
        <if test="recipeIds !=null and recipeIds.size()>0">
            AND recipe_id in
            <foreach collection="recipeIds" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectMenuRecipeListV2" resultType="com.bonus.canteen.core.menu.vo.AllocRecipeStallVO">
        SELECT
        t3.area_id, t3.area_name, t2.canteen_id, t2.canteen_name, t1.stall_id, t1.stall_name
        FROM
        alloc_stall t1
        LEFT JOIN alloc_canteen t2 ON t1.canteen_id = t2.canteen_id
        LEFT JOIN alloc_area t3 ON t2.area_id = t3.area_id
        where 1=1
        <if test="recipeName !=null and recipeName !=''">
            AND EXISTS (
            select null
            from menu_app_recipe t4 INNER JOIN  menu_recipe t5 on  t4.recipe_id = t5.recipe_id
            where  t5.stall_id = t1.stall_id  AND t4.bind_type = #{bindType}
            and t5.recipe_name like #{recipeName}
            )
        </if>
        <if test="deviceTypeList != null and deviceTypeList.size() > 0">
            AND EXISTS (
            SELECT NULL
            FROM
            device_bind t6
            LEFT JOIN device_info t7 ON t7.device_id = t6.device_id
            WHERE
            t6.canteen_id = t2.canteen_id
            AND t6.stall_id = t1.stall_id
            AND t7.device_type IN
            <foreach collection="deviceTypeList" item="deviceType" separator="," open="(" close=")">
                #{deviceType}
            </foreach>
            )
        </if>
                <if test="areaId !=null">
                    and t3.area_id  = #{areaId}
                </if>
                <if test="canteenId !=null">
                    and t2.canteen_id  = #{canteenId}
                </if>
                <if test="stallId !=null ">
                    and t1.stall_id = #{stallId}
                </if>

<!--        SELECT-->
<!--        t3.area_id as areaId, t3.area_name as areaName, t2.canteen_id as canteenId,-->
<!--        t2.canteen_name as canteenName, t1.stall_id as stallId, t1.stall_name as stallName,-->
<!--        a.recipe_id as recipeId,a.recipe_name as recipeName-->
<!--        FROM-->
<!--        alloc_stall t1-->
<!--        LEFT JOIN alloc_canteen t2 ON t1.canteen_id = t2.canteen_id-->
<!--        LEFT JOIN alloc_area t3 ON t2.area_id = t3.area_id-->
<!--        LEFT JOIN(-->
<!--        select-->
<!--        mar.recipe_id,-->
<!--        mr.stall_id,-->
<!--        mar.bind_type,-->
<!--        mr.recipe_name-->
<!--        from menu_app_recipe mar-->
<!--        left join menu_recipe mr on mar.recipe_id = mr.recipe_id-->
<!--        where bind_type = #{bindType}-->
<!--        ) a on a.stall_id =t1.stall_id-->
<!--        <if test="deviceTypeList != null and deviceTypeList.size() > 0">-->
<!--            and-->
<!--            EXISTS (-->
<!--            SELECT NULL-->
<!--            FROM-->
<!--            device_bind t6-->
<!--            LEFT JOIN device_info t7 ON t7.device_id = t6.device_id-->
<!--            WHERE-->
<!--            t6.canteen_id = t2.canteen_id-->
<!--            AND t6.stall_id = t1.stall_id-->
<!--            AND t7.device_type IN-->
<!--            <foreach collection="deviceTypeList" item="deviceType" separator="," open="(" close=")">-->
<!--                #{deviceType}-->
<!--            </foreach>-->
<!--            )-->
<!--        </if>-->

<!--        where 1= 1-->

<!--        <if test="areaId !=null">-->
<!--            and t3.area_id  = #{areaId}-->
<!--        </if>-->
<!--        <if test="canteenId !=null">-->
<!--            and t2.canteen_id  = #{canteenId}-->
<!--        </if>-->
<!--        <if test="stallId !=null ">-->
<!--            and t1.stall_id = #{stallId}-->
<!--        </if>-->

<!--        <if test="recipeName !=null and recipeName !=''">-->
<!--            and a.recipe_name like concat('%', #{recipeName}, '%')-->
<!--        </if>-->
    </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="selectTmpBetweenDateDetailList" resultMap="menuRecipeDateVO">

        select mrd.mealtime_type,
        mrd.recipe_id,
        mrd.detail_id,
        mdb.base_dishes_id as dishes_id,
        mdb.dishes_name,
        mrdd.id,
        mrdd.price,
        mrdd.size_type,
        mrdd.supply_num,
        mrdd.sale_num,
        mrdd.surplus_num,
        mrdd.restrict_num,
        mrdd.sale_price,
        mrdd.recommend_flag,
        mrdd.sort_num,
        md.sales_mode,
        md.unit_price,
        md.meal_type,
        md.dishes_num,
        ac.canteen_name,
        md.material_cost,
        md.type_id,
        mdt.type_name
        from menu_recipe_detail mrd
        left join menu_recipe_dishes mrdd on mrd.detail_id = mrdd.detail_id
        left join menu_dishes md on mrdd.dishes_id = md.dishes_id
        left join alloc_canteen ac on md.canteen_id = ac.canteen_id
        left join menu_dishes_base mdb on md.base_dishes_id = mdb.base_dishes_id
        left join menu_dishes_type mdt on md.type_id = mdt.type_id
        where 1=1
        <if test="recipeIds !=null and recipeIds.size() > 0">
            and mrd.recipe_id in
            <foreach collection="recipeIds" item="recipeId" separator="," open="(" close=")">
                #{recipeId}
            </foreach>
        </if>

        and mrd.apply_date between #{applyStartDate} and #{applyEndDate}
        order by mrd.apply_date,mrdd.sort_num
    </select>
    <select id="getDeviceSnByIds" resultType="java.lang.String">
        select device_sn
        from device_info
        where device_id in
        <foreach collection="list" item="deviceId" separator="," open="(" close=")">
            #{deviceId}
        </foreach>
    </select>

    <select id="selectDishesNameAndCanteenName" resultType="java.lang.String">
        SELECT
        concat_ws( '-', mdb.dishes_name, IFNULL(aa.area_name,'*'),IFNULL(ac.canteen_name,'*') , IFNULL( ass.stall_name,'*') )
        FROM
        menu_dishes_base mdb
        LEFT JOIN menu_dishes md ON mdb.base_dishes_id = md.base_dishes_id
        LEFT JOIN alloc_area aa ON aa.area_id = md.area_id
        LEFT JOIN alloc_canteen ac ON md.canteen_id = ac.canteen_id
        LEFT JOIN alloc_stall ass ON ass.stall_id = md.shopstall_id
<!--        WHERE-->
<!--        del_flag = 2-->
    </select>



    <insert id="insertMenuDishesType">
        insert into menu_dishes_type(type_id,type_name,alias_name,default_flag)
        values(#{typeId},#{typeName},#{aliasName},#{defaultFlag})
    </insert>


    <insert id="insertMenuMateralDishes" parameterType="List">
        insert into
        menu_material_dishes (dishes_id, material_id, weight, material_type, crby)
        values
        <foreach collection="materialList" item="materialList" separator="," close=";">
            (#{materialList.dishesId}, #{materialList.materialId}, #{materialList.weight},
            #{materialList.materialType}, #{materialList.createBy})
        </foreach>
    </insert>

    <!-- 获取营养信息详情 -->
    <select id="getMenuNutritionDetailById"
            resultType="com.bonus.canteen.core.menu.vo.MenuNutritionDetailVO">
        select color,
        label,
        processing_method,
        processing_time,
        edible,
        water,
        calories,
        protein,
        fat,
        carbohydrate,
        dietary_fiber,
        cholesterol,
        ash,
        vitamin_a,
        carotene,
        retinol,
        thiamine,
        riboflavin,
        niacin,
        vitamin_c,
        vitamin_d,
        vitamin_e,
        calcium,
        phosphorus,
        kalium,
        sodium,
        magnesium,
        iron,
        zinc,
        selenium
        from menu_nutrition
        where nutrition_id = #{nutritionId}
    </select>

    <select id="getMenuMaterialMap" resultType="com.bonus.canteen.core.menu.domain.MapBean">
        select material_name as `key`,material_id as `value` from menu_material
    </select>

    <select id="getMenuMaterialTypeMap" resultType="com.bonus.canteen.core.menu.domain.MapBean">
        select type_name as `key` ,type_id as `value` from menu_dishes_type
    </select>

    <select id="getDishesCookMap" resultType="com.bonus.canteen.core.menu.domain.MapBean">
        select dict_label as `key`,   dict_value as `value` from  sys_dict_data where dict_type = 'dishes_cook'
    </select>
</mapper>

