<?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>
</mapper>
