<?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.basic.mapper.BasicStallMapper">
    <resultMap type="com.bonus.canteen.core.basic.domain.BasicStall" id="BasicStallResult">
        <result property="stallId"    column="stall_id"    />
        <result property="stallName"    column="stall_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="areaName"    column="area_name"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="canteenName"    column="canteen_name"    />
        <result property="manager"    column="manager"    />
        <result property="contactTel"    column="contact_tel"    />
        <result property="bizState"    column="biz_state"    />
        <result property="bizStartTime"    column="biz_start_time"    />
        <result property="bizEndTime"    column="biz_end_time"    />
        <result property="ifDelivery"    column="if_delivery"    />
        <result property="deliveries"    column="deliveries"    />
        <result property="ifOrder"    column="if_order"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="ifBook"    column="if_book"    />
        <result property="ifReserve"    column="if_reserve"    />
        <result property="ifPayCode"    column="if_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="selectBasicStallVo">
        select bs.stall_id, bs.stall_name, bs.area_id, bs.canteen_id, bs.manager, bs.contact_tel, bs.biz_state,
               bs.biz_start_time, bs.biz_end_time, bs.if_delivery, bs.deliveries, bs.if_order, bs.img_url,
               bs.if_book, bs.if_reserve, bs.if_pay_code, bs.pay_code_url, bs.del_flag, bs.create_by, bs.create_time,
               bs.update_by, bs.update_time, bc.canteen_name, ba.area_name
        from basic_stall bs
             left join basic_canteen bc on bc.canteen_id = bs.canteen_id
             left join basic_area ba on bc.area_id = ba.area_id
    </sql>

    <select id="selectBasicStallList" parameterType="com.bonus.canteen.core.basic.domain.BasicStall" resultMap="BasicStallResult">
        <include refid="selectBasicStallVo"/>
        <where>  
            <if test="stallName != null  and stallName != ''"> and bs.stall_name like concat('%', #{stallName}, '%')</if>
            <if test="areaId != null "> and bs.area_id = #{areaId}</if>
            <if test="canteenId != null "> and bs.canteen_id = #{canteenId}</if>
            <if test="manager != null  and manager != ''"> and bs.manager = #{manager}</if>
            <if test="contactTel != null  and contactTel != ''"> and bs.contact_tel = #{contactTel}</if>
            <if test="bizState != null "> and bs.biz_state = #{bizState}</if>
            <if test="bizStartTime != null "> and bs.biz_start_time = #{bizStartTime}</if>
            <if test="bizEndTime != null "> and bs.biz_end_time = #{bizEndTime}</if>
            <if test="ifDelivery != null "> and bs.if_delivery = #{ifDelivery}</if>
            <if test="deliveries != null  and deliveries != ''"> and bs.deliveries = #{deliveries}</if>
            <if test="ifOrder != null "> and bs.if_order = #{ifOrder}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and bs.img_url = #{imgUrl}</if>
            <if test="ifBook != null "> and bs.if_book = #{ifBook}</if>
            <if test="ifReserve != null "> and bs.if_reserve = #{ifReserve}</if>
            <if test="ifPayCode != null "> and bs.if_pay_code = #{ifPayCode}</if>
            <if test="payCodeUrl != null  and payCodeUrl != ''"> and bs.pay_code_url = #{payCodeUrl}</if>
            <if test="delFlag != null  and delFlag != ''"> and bs.del_flag = #{delFlag}</if>
        </where>
    </select>
    
    <select id="selectBasicStallByStallId" parameterType="Long" resultMap="BasicStallResult">
        <include refid="selectBasicStallVo"/>
        where bs.stall_id = #{stallId} and bs.del_flag = 0
    </select>

    <select id="selectBasicStallByStallName" parameterType="com.bonus.canteen.core.basic.domain.BasicStall" resultMap="BasicStallResult">
        <include refid="selectBasicStallVo"/>
        where bs.stall_name = #{stallName} and bs.canteen_id = #{canteenId} and bs.del_flag = 0
    </select>

    <select id="selectBasicStallCountByCanteenIds" resultType="Integer">
        select count(1)
        from basic_stall
        where del_flag = 0 and canteen_id in
        <foreach item="canteenId" collection="array" open="(" separator="," close=")">
            #{canteenId}
        </foreach>
    </select>
        
    <insert id="insertBasicStall" parameterType="com.bonus.canteen.core.basic.domain.BasicStall" useGeneratedKeys="true" keyProperty="stallId">
        insert into basic_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="manager != null">manager,</if>
            <if test="contactTel != null">contact_tel,</if>
            <if test="bizState != null">biz_state,</if>
            <if test="bizStartTime != null">biz_start_time,</if>
            <if test="bizEndTime != null">biz_end_time,</if>
            <if test="ifDelivery != null">if_delivery,</if>
            <if test="deliveries != null">deliveries,</if>
            <if test="ifOrder != null">if_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="ifPayCode != null">if_pay_code,</if>
            <if test="payCodeUrl != null">pay_code_url,</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="manager != null">#{manager},</if>
            <if test="contactTel != null">#{contactTel},</if>
            <if test="bizState != null">#{bizState},</if>
            <if test="bizStartTime != null">#{bizStartTime},</if>
            <if test="bizEndTime != null">#{bizEndTime},</if>
            <if test="ifDelivery != null">#{ifDelivery},</if>
            <if test="deliveries != null">#{deliveries},</if>
            <if test="ifOrder != null">#{ifOrder},</if>
            <if test="imgUrl != null">#{imgUrl},</if>
            <if test="ifBook != null">#{ifBook},</if>
            <if test="ifReserve != null">#{ifReserve},</if>
            <if test="ifPayCode != null">#{ifPayCode},</if>
            <if test="payCodeUrl != null">#{payCodeUrl},</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="updateBasicStall" parameterType="com.bonus.canteen.core.basic.domain.BasicStall">
        update basic_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="manager != null">manager = #{manager},</if>
            <if test="contactTel != null">contact_tel = #{contactTel},</if>
            <if test="bizState != null">biz_state = #{bizState},</if>
            <if test="bizStartTime != null">biz_start_time = #{bizStartTime},</if>
            <if test="bizEndTime != null">biz_end_time = #{bizEndTime},</if>
            <if test="ifDelivery != null">if_delivery = #{ifDelivery},</if>
            <if test="deliveries != null">deliveries = #{deliveries},</if>
            <if test="ifOrder != null">if_order = #{ifOrder},</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="ifPayCode != null">if_pay_code = #{ifPayCode},</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="deleteBasicStallByStallId" parameterType="Long">
        update basic_stall set del_flag = '2' where stall_id = #{stallId}
    </delete>

    <delete id="deleteBasicStallByStallIds" parameterType="String">
        update basic_stall set del_flag = '2' where stall_id in
        <foreach item="stallId" collection="array" open="(" separator="," close=")">
            #{stallId}
        </foreach>
    </delete>
</mapper>