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

    <!-- 分页查询食堂列表 -->
    <select id="pageCanteen" resultType="com.bonus.canteen.core.allocation.canteen.vo.AllocCanteenVO">
        SELECT
        t1.canteen_id, t1.canteen_num, t1.canteen_name, t1.cust_id, t4.cust_name, t1.eff_id, t1.contact_tel, t1.business_state, t1.main_project,
        t1.start_business_time, t1.end_business_time, t1.img_url, t1.capacity, t1.if_book, t1.if_reserve, t1.crtime, t1.uptime, t1.pay_code_url,
        t1.special_flag, t2.area_id, t2.areaNameStr, t3.labelNameStr
        FROM
        alloc_canteen t1
        LEFT JOIN (
        SELECT
        area_id, CONCAT_WS('>', first_area_name, second_area_name, third_area_name) AS areaNameStr
        FROM
        alloc_area) t2
        ON t1.area_id = t2.area_id
        LEFT JOIN (
        SELECT
        canteen_id, GROUP_CONCAT(label_name) AS labelNameStr
        FROM
        alloc_label
        GROUP BY canteen_id) t3
        ON t1.canteen_id = t3.canteen_id
        LEFT JOIN
        cust_info t4 ON t1.cust_id = t4.cust_id
        WHERE
        t1.if_del = 2
        AND t1.canteen_type = #{param.canteenType,jdbcType=TINYINT}
        <if test="param.canteenName != null and param.canteenName != ''">
            AND t1.canteen_name LIKE #{param.canteenName}
        </if>
        <if test="param.areaIdList != null and param.areaIdList.size() > 0">
            AND t1.area_id IN
            <foreach collection="param.areaIdList" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        ORDER BY t1.id DESC
    </select>

    <!-- 通过区域id列表，查询食堂id列表 -->
    <select id="getCanteenIdListByAreaIdList" resultType="java.lang.Long">
        SELECT
        canteen_id
        FROM
        alloc_canteen
        WHERE area_id in
        <foreach collection="areaIdList" item="areaId" separator="," open="(" close=")">
            #{areaId}
        </foreach>
    </select>

    <!-- 根据食堂id 更新食堂信息 -->
    <update id="updateByCanteenId">
        UPDATE
            alloc_canteen
        SET
            canteen_num = #{dto.canteenNum},
            canteen_name = #{dto.canteenName},
            area_id = #{dto.areaId},
            cust_id = #{dto.custId},
            third_canteen_id = #{dto.thirdCanteenId},
            eff_id = #{dto.effId},
            contact_tel = #{dto.contactTel},
            main_project = #{dto.mainProject},
            start_business_time = #{dto.startBusinessTime},
            end_business_time = #{dto.endBusinessTime},
            img_url = #{dto.imgUrl},
            capacity = #{dto.capacity},
            if_enable_pay = #{dto.ifEnablePay},
            pay_types = #{dto.payTypes},
            if_enable_delivery = #{dto.ifEnableDelivery},
            deliveries = #{dto.deliveries},
            if_enable_order = #{dto.ifEnableOrder},
            if_book = #{dto.ifBook},
            if_reserve = #{dto.ifReserve},
            if_enable_pay_code = #{dto.ifEnablePayCode},
            pay_code_url = #{dto.payCodeUrl}
        WHERE
            canteen_id = #{canteenId}
    </update>

    <select id="listAuthCanteen" resultType="com.bonus.canteen.core.allocation.canteen.vo.AllocCanteenVO">
        SELECT t1.canteen_id,
               t1.area_id,
               t1.canteen_name,
               t1.canteen_type
        FROM alloc_canteen t1
        WHERE t1.if_del = 2
    </select>
</mapper>
