<?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.alloc.mapper.AllocStallMapper">
    <resultMap type="com.bonus.canteen.core.alloc.domain.AllocStall" id="AllocStallResult">
        <result property="stallId"    column="stall_id"    />
        <result property="stallName"    column="stall_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="stallType"    column="stall_type"    />
        <result property="thirdStallId"    column="third_stall_id"    />
        <result property="director"    column="director"    />
        <result property="contactTel"    column="contact_tel"    />
        <result property="mainProject"    column="main_project"    />
        <result property="businessState"    column="business_state"    />
        <result property="startBusinessTime"    column="start_business_time"    />
        <result property="endBusinessTime"    column="end_business_time"    />
        <result property="ifUseCallNum"    column="if_use_call_num"    />
        <result property="onLineMealCodePrefix"    column="on_line_meal_code_prefix"    />
        <result property="offLineMealCodePrefix"    column="off_line_meal_code_prefix"    />
        <result property="ifEnableDelivery"    column="if_enable_delivery"    />
        <result property="deliveries"    column="deliveries"    />
        <result property="ifEnableOrder"    column="if_enable_order"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="ifBook"    column="if_book"    />
        <result property="ifReserve"    column="if_reserve"    />
        <result property="ifEnablePayCode"    column="if_enable_pay_code"    />
        <result property="payCodeUrl"    column="pay_code_url"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectAllocStallVo">
        select ast.stall_id, ast.stall_name, ast.area_id, ast.canteen_id, ac.canteen_name, aa.area_name,
               ast.stall_type, ast.third_stall_id, ast.director, ast.contact_tel,
               ast.main_project, ast.business_state, ast.start_business_time, ast.end_business_time, ast.if_use_call_num,
               ast.on_line_meal_code_prefix, ast.off_line_meal_code_prefix, ast.if_enable_delivery, ast.deliveries,
               ast.if_enable_order, ast.img_url, ast.if_book, ast.if_reserve, ast.if_enable_pay_code, ast.pay_code_url,
               ast.del_flag, ast.create_by, ast.create_time, ast.update_by, ast.update_time
        from alloc_stall ast
             left join alloc_canteen ac on ast.canteen_id = ac.canteen_id
             left join alloc_area aa on ac.area_id = aa.area_id
    </sql>

    <select id="selectAllocStallList" parameterType="com.bonus.canteen.core.alloc.domain.AllocStall" resultMap="AllocStallResult">
        <include refid="selectAllocStallVo"/>
        <where>  
            <if test="stallName != null  and stallName != ''"> and ast.stall_name like concat('%', #{stallName}, '%')</if>
            <if test="areaId != null "> and ast.area_id = #{areaId}</if>
            <if test="canteenId != null "> and ast.canteen_id = #{canteenId}</if>
            <if test="stallType != null "> and ast.stall_type = #{stallType}</if>
            <if test="thirdStallId != null  and thirdStallId != ''"> and ast.third_stall_id = #{thirdStallId}</if>
            <if test="director != null "> and ast.director = #{director}</if>
            <if test="contactTel != null  and contactTel != ''"> and ast.contact_tel = #{contactTel}</if>
            <if test="mainProject != null  and mainProject != ''"> and ast.main_project = #{mainProject}</if>
            <if test="businessState != null "> and ast.business_state = #{businessState}</if>
            <if test="startBusinessTime != null "> and ast.start_business_time = #{startBusinessTime}</if>
            <if test="endBusinessTime != null "> and ast.end_business_time = #{endBusinessTime}</if>
            <if test="ifUseCallNum != null "> and ast.if_use_call_num = #{ifUseCallNum}</if>
            <if test="onLineMealCodePrefix != null  and onLineMealCodePrefix != ''"> and ast.on_line_meal_code_prefix = #{onLineMealCodePrefix}</if>
            <if test="offLineMealCodePrefix != null  and offLineMealCodePrefix != ''"> and ast.off_line_meal_code_prefix = #{offLineMealCodePrefix}</if>
            <if test="ifEnableDelivery != null "> and ast.if_enable_delivery = #{ifEnableDelivery}</if>
            <if test="deliveries != null  and deliveries != ''"> and ast.deliveries = #{deliveries}</if>
            <if test="ifEnableOrder != null "> and ast.if_enable_order = #{ifEnableOrder}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and ast.img_url = #{imgUrl}</if>
            <if test="ifBook != null "> and ast.if_book = #{ifBook}</if>
            <if test="ifReserve != null "> and ast.if_reserve = #{ifReserve}</if>
            <if test="ifEnablePayCode != null "> and ast.if_enable_pay_code = #{ifEnablePayCode}</if>
            <if test="payCodeUrl != null  and payCodeUrl != ''"> and ast.pay_code_url = #{payCodeUrl}</if>
        </where>
    </select>
    
    <select id="selectAllocStallByStallId" parameterType="Long" resultMap="AllocStallResult">
        <include refid="selectAllocStallVo"/>
        where ast.stall_id = #{stallId}
    </select>

    <select id="selectAllocStallByStallName" parameterType="com.bonus.canteen.core.alloc.domain.AllocStall" resultMap="AllocStallResult">
        <include refid="selectAllocStallVo"/>
        where ast.stall_name = #{stallName} and ac.canteen_id = #{canteenId}
    </select>
        
    <insert id="insertAllocStall" parameterType="com.bonus.canteen.core.alloc.domain.AllocStall" useGeneratedKeys="true" keyProperty="stallId">
        insert into alloc_stall
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stallName != null and stallName != ''">stall_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="stallType != null">stall_type,</if>
            <if test="thirdStallId != null">third_stall_id,</if>
            <if test="director != null">director,</if>
            <if test="contactTel != null">contact_tel,</if>
            <if test="mainProject != null">main_project,</if>
            <if test="businessState != null">business_state,</if>
            <if test="startBusinessTime != null">start_business_time,</if>
            <if test="endBusinessTime != null">end_business_time,</if>
            <if test="ifUseCallNum != null">if_use_call_num,</if>
            <if test="onLineMealCodePrefix != null">on_line_meal_code_prefix,</if>
            <if test="offLineMealCodePrefix != null">off_line_meal_code_prefix,</if>
            <if test="ifEnableDelivery != null">if_enable_delivery,</if>
            <if test="deliveries != null">deliveries,</if>
            <if test="ifEnableOrder != null">if_enable_order,</if>
            <if test="imgUrl != null">img_url,</if>
            <if test="ifBook != null">if_book,</if>
            <if test="ifReserve != null">if_reserve,</if>
            <if test="ifEnablePayCode != null">if_enable_pay_code,</if>
            <if test="payCodeUrl != null">pay_code_url,</if>
            <if test="delFlag != null">del_flag,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stallName != null and stallName != ''">#{stallName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="stallType != null">#{stallType},</if>
            <if test="thirdStallId != null">#{thirdStallId},</if>
            <if test="director != null">#{director},</if>
            <if test="contactTel != null">#{contactTel},</if>
            <if test="mainProject != null">#{mainProject},</if>
            <if test="businessState != null">#{businessState},</if>
            <if test="startBusinessTime != null">#{startBusinessTime},</if>
            <if test="endBusinessTime != null">#{endBusinessTime},</if>
            <if test="ifUseCallNum != null">#{ifUseCallNum},</if>
            <if test="onLineMealCodePrefix != null">#{onLineMealCodePrefix},</if>
            <if test="offLineMealCodePrefix != null">#{offLineMealCodePrefix},</if>
            <if test="ifEnableDelivery != null">#{ifEnableDelivery},</if>
            <if test="deliveries != null">#{deliveries},</if>
            <if test="ifEnableOrder != null">#{ifEnableOrder},</if>
            <if test="imgUrl != null">#{imgUrl},</if>
            <if test="ifBook != null">#{ifBook},</if>
            <if test="ifReserve != null">#{ifReserve},</if>
            <if test="ifEnablePayCode != null">#{ifEnablePayCode},</if>
            <if test="payCodeUrl != null">#{payCodeUrl},</if>
            <if test="delFlag != null">#{delFlag},</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>
         </trim>
    </insert>

    <update id="updateAllocStall" parameterType="com.bonus.canteen.core.alloc.domain.AllocStall">
        update alloc_stall
        <trim prefix="SET" suffixOverrides=",">
            <if test="stallName != null and stallName != ''">stall_name = #{stallName},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="stallType != null">stall_type = #{stallType},</if>
            <if test="thirdStallId != null">third_stall_id = #{thirdStallId},</if>
            <if test="director != null">director = #{director},</if>
            <if test="contactTel != null">contact_tel = #{contactTel},</if>
            <if test="mainProject != null">main_project = #{mainProject},</if>
            <if test="businessState != null">business_state = #{businessState},</if>
            <if test="startBusinessTime != null">start_business_time = #{startBusinessTime},</if>
            <if test="endBusinessTime != null">end_business_time = #{endBusinessTime},</if>
            <if test="ifUseCallNum != null">if_use_call_num = #{ifUseCallNum},</if>
            <if test="onLineMealCodePrefix != null">on_line_meal_code_prefix = #{onLineMealCodePrefix},</if>
            <if test="offLineMealCodePrefix != null">off_line_meal_code_prefix = #{offLineMealCodePrefix},</if>
            <if test="ifEnableDelivery != null">if_enable_delivery = #{ifEnableDelivery},</if>
            <if test="deliveries != null">deliveries = #{deliveries},</if>
            <if test="ifEnableOrder != null">if_enable_order = #{ifEnableOrder},</if>
            <if test="imgUrl != null">img_url = #{imgUrl},</if>
            <if test="ifBook != null">if_book = #{ifBook},</if>
            <if test="ifReserve != null">if_reserve = #{ifReserve},</if>
            <if test="ifEnablePayCode != null">if_enable_pay_code = #{ifEnablePayCode},</if>
            <if test="payCodeUrl != null">pay_code_url = #{payCodeUrl},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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>
        </trim>
        where stall_id = #{stallId}
    </update>

    <delete id="deleteAllocStallByStallId" parameterType="Long">
        delete from alloc_stall where stall_id = #{stallId}
    </delete>

    <delete id="deleteAllocStallByStallIds" parameterType="String">
        delete from alloc_stall where stall_id in 
        <foreach item="stallId" collection="array" open="(" separator="," close=")">
            #{stallId}
        </foreach>
    </delete>

    <select id="selectAllocStallCountByCanteenIds" resultType="Integer">
        select count(1)
        from alloc_stall
        where del_flag = 0 and canteen_id in
        <foreach item="canteenId" collection="array" open="(" separator="," close=")">
            #{canteenId}
        </foreach>
    </select>

    <delete id="deleteMealtimeByStallId" parameterType="Long">
        delete from alloc_stall_mealtime where stall_id = #{stallId}
    </delete>

    <select id="selectMealtimeByStallId" parameterType="Long" resultType="com.bonus.canteen.core.alloc.domain.AllocStallMealtime">
        select * from alloc_stall_mealtime where stall_id = #{stallId}
    </select>

    <insert id="insertAllocStallMealtime" parameterType="com.bonus.canteen.core.alloc.domain.AllocStall" useGeneratedKeys="true" keyProperty="id">
        insert into alloc_stall_mealtime(stall_id, mealtime_type, mealtime_name, start_time, end_time)
        values
        <foreach collection="list" item="item" separator=",">
        (
            #{item.stallId},
            #{item.mealtimeType},
            #{item.mealtimeName},
            #{item.startTime},
            #{item.endTime}
        )
        </foreach>
    </insert>

    <!-- 通过档口id列表获取档口名称 -->
    <select id="getStallNameByIdSet" resultType="java.lang.String">
        SELECT
        GROUP_CONCAT(stall_name SEPARATOR ',') AS stallName
        FROM
        alloc_stall
        WHERE stall_id in
        <foreach collection="stallIdSet" item="stallId" separator="," open="(" close=")">
            #{stallId}
        </foreach>
    </select>
</mapper>