<?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.allocation.canteen.mapper.AllocStallMapper">
    <!-- 分页查询档口列表 -->
    <select id="pageStall" resultType="com.bonus.canteen.core.allocation.canteen.vo.AllocStallVO">
        SELECT
        t1.stall_id, t1.stall_num, t1.stall_name, t5.cust_name, t1.contact_tel,
        t1.business_state, t1.start_business_time, t1.end_business_time, t1.if_use_call_num,
        t1.on_line_meal_code_prefix, t1.off_line_meal_code_prefix,
        t1.img_url, t1.if_book, t1.if_reserve, t1.crtime, t1.uptime, t1.pay_code_url,
        t1.canteen_id, t2.canteen_name, t3.labelNameStr, t4.kindNameStr, t6.area_name
        FROM
        alloc_stall t1
        INNER JOIN alloc_canteen t2
        ON t1.canteen_id = t2.canteen_id
        LEFT JOIN (
        SELECT
        stall_id, GROUP_CONCAT(label_name) AS labelNameStr
        FROM
        alloc_label
        GROUP BY stall_id) t3
        ON t1.stall_id = t3.stall_id
        LEFT JOIN (
        SELECT
        stall_id, GROUP_CONCAT(kind_name) AS kindNameStr
        FROM
        alloc_kind
        GROUP BY stall_id) t4
        ON t1.stall_id = t4.stall_id
        LEFT JOIN
        cust_info t5 ON t1.cust_id = t5.cust_id
        INNER JOIN
        alloc_area t6 ON t2.area_id = t6.area_id
        WHERE
        t1.if_del = #{param.ifDel,jdbcType=NUMERIC}
        AND t1.stall_type = #{param.stallType}
        <if test="param.canteenId != null">
            AND t1.canteen_id = #{param.canteenId}
        </if>
        <if test="param.stallId != null">
            AND t1.stall_id = #{param.stallId}
        </if>
        <if test="param.stallName != null and param.stallName != ''">
            AND t1.stall_name LIKE #{param.stallName}
        </if>
        <if test="param.areaIdList != null and param.areaIdList.size() > 0">
            AND t2.area_id IN
            <foreach collection="param.areaIdList" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        ORDER BY t1.id DESC
    </select>

    <!-- 根据档口id 更新档口信息 -->
    <update id="updateByStallId">
        UPDATE
            alloc_stall
        SET
            stall_name = #{dto.stallName},
            canteen_id = #{dto.canteenId},
            area_id = #{dto.areaId},
            cust_id = #{dto.custId},
            contact_tel = #{dto.contactTel},
            main_project = #{dto.mainProject},
            start_business_time = #{dto.startBusinessTime},
            end_business_time = #{dto.endBusinessTime},
            if_use_call_num = #{dto.ifUseCallNum},
            on_line_meal_code_prefix = #{dto.onLineMealCodePrefix},
            off_line_meal_code_prefix = #{dto.offLineMealCodePrefix},
            if_enable_delivery = #{dto.ifEnableDelivery},
            deliveries = #{dto.deliveries},
            if_enable_order = #{dto.ifEnableOrder},
            img_url = #{dto.imgUrl},
            if_book = #{dto.ifBook},
            if_reserve = #{dto.ifReserve},
            if_enable_pay_code = #{dto.ifEnablePayCode},
            pay_code_url = #{dto.payCodeUrl}
        WHERE
            stall_id = #{stallId}
    </update>

    <select id="listAuthStallByUserRole" resultType="com.bonus.canteen.core.allocation.canteen.vo.AllocStallVO">
        SELECT t1.stall_id,
               t1.canteen_id,
               t1.stall_name,
               t1.stall_type
        FROM alloc_stall t1
        WHERE t1.if_del = 2
    </select>

    <!-- 查询带权限的档口列表 -->
    <select id="listAuthStall" resultType="com.bonus.canteen.core.allocation.canteen.vo.AllocStallAuthVO">
        SELECT
        t1.stall_id, t1.stall_name
        FROM
        alloc_stall t1
        WHERE
        t1.if_del = 2
        AND t1.stall_type = 1
        <if test="canteenIdList != null and canteenIdList.size() > 0">
            AND t1.canteen_id IN
            <foreach collection="canteenIdList" item="canteenId" separator="," open="(" close=")">
                #{canteenId}
            </foreach>
        </if>
    </select>


    <select id="pageRecipeMealLineInfo" resultType="com.bonus.canteen.core.allocation.canteen.vo.AllocRecipeStallVO">
        SELECT
        t3.area_id, t3.area_name, t2.canteen_id, t2.canteen_name, t1.stall_id, t1.stall_name,
        t8.meal_line_id, t8.meal_line_name, t8.meal_line_type
        FROM
        alloc_meal_line t8
        LEFT JOIN alloc_stall t1 ON t8.stall_id = t1.stall_id
        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 t8.if_del = 2 and t1.if_del = 2 and t2.if_del = 2 and t3.if_del = 2
        <if test="param.recipeName !=null and param.recipeName !=''">
            AND EXISTS (
            select null
            from menu_app_recipe t4 INNER JOIN  menu_recipe t5 on  t4.recipe_id = t5.recipe_id
            where  t4.meal_line_id = t8.meal_line_id AND t4.bind_type = #{param.bindType}
            and t5.recipe_name like #{param.recipeName}
            )
        </if>
        <if test="param.deviceTypeList != null and param.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 t6.meal_line_id = t8.meal_line_id
            AND t7.device_type IN
            <foreach collection="param.deviceTypeList" item="deviceType" separator="," open="(" close=")">
                #{deviceType}
            </foreach>
            )
        </if>
        <if test="param.areaId != null">
            AND t3.area_id = #{param.areaId}
        </if>
        <if test="param.areaIds != null and param.areaIds.size() > 0">
            AND t3.area_id IN
            <foreach collection="param.areaIds" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        <if test="param.canteenId != null">
            AND t2.canteen_id = #{param.canteenId}
        </if>
        <if test="param.canteenIds != null and param.canteenIds.size() > 0">
            AND t2.canteen_id IN
            <foreach collection="param.canteenIds" item="canteenId" separator="," open="(" close=")">
                #{canteenId}
            </foreach>
        </if>
        <if test="param.stallId != null">
            AND t1.stall_id = #{param.stallId}
        </if>
        <if test="param.stallIds != null and param.stallIds.size() > 0">
            AND t1.stall_id IN
            <foreach collection="param.stallIds" item="stallId" separator="," open="(" close=")">
                #{stallId}
            </foreach>
        </if>
        <if test="param.mealLineId != null">
            AND t8.meal_line_id = #{param.mealLineId}
        </if>
        <if test="param.mealLineType != null">
            AND t8.meal_line_type = #{param.mealLineType}
        </if>
    </select>

    <!-- 查询菜谱档口关联信息 -->
    <select id="pageRecipeStallInfo" resultType="com.bonus.canteen.core.allocation.canteen.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 t1.if_del = 2 and t2.if_del = 2 and t3.if_del = 2
        <if test="param.recipeName !=null and param.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 = #{param.bindType}
            and t5.recipe_name like #{param.recipeName}
            )
        </if>
        <if test="param.deviceTypeList != null and param.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="param.deviceTypeList" item="deviceType" separator="," open="(" close=")">
                #{deviceType}
            </foreach>
            )
        </if>
        <if test="param.areaId != null">
            AND t3.area_id = #{param.areaId}
        </if>
        <if test="param.areaIds != null and param.areaIds.size() > 0">
            AND t3.area_id IN
            <foreach collection="param.areaIds" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        <if test="param.canteenId != null">
            AND t2.canteen_id = #{param.canteenId}
        </if>
        <if test="param.canteenIds != null and param.canteenIds.size() > 0">
            AND t2.canteen_id IN
            <foreach collection="param.canteenIds" item="canteenId" separator="," open="(" close=")">
                #{canteenId}
            </foreach>
        </if>
        <if test="param.stallId != null">
            AND t1.stall_id = #{param.stallId}
        </if>
        <if test="param.stallIds != null and param.stallIds.size() > 0">
            AND t1.stall_id IN
            <foreach collection="param.stallIds" item="stallId" separator="," open="(" close=")">
                #{stallId}
            </foreach>
        </if>
    </select>

    <!-- 根据食堂id获取档口列表 -->
    <select id="listStallByCanteenId" resultType="com.bonus.canteen.core.allocation.canteen.vo.CanteenStallVO">
        SELECT stall_id,
               canteen_id
        FROM alloc_stall
        WHERE canteen_id = #{canteenId}
    </select>

    <!-- 通过区域id列表，查询未删除的全部档口id列表 -->
    <select id="listStallIdByAreaIdList" resultType="java.lang.Long">
        SELECT
        t1.stall_id
        FROM
        alloc_stall t1
        WHERE
        t1.if_del = 2
        <if test="areaIdList != null and areaIdList.size() > 0">
            AND t1.area_id IN
            <foreach collection="areaIdList" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
    </select>
</mapper>
