<?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.BasicCanteenMapper">
    <resultMap type="com.bonus.canteen.core.basic.domain.BasicCanteen" id="BasicCanteenResult">
        <result property="canteenId"    column="canteen_id"    />
        <result property="canteenName"    column="canteen_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="areaName"    column="area_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="imgUrl"    column="img_url"    />
        <result property="enablePay"    column="enable_pay"    />
        <result property="payTypes"    column="pay_types"    />
        <result property="ifDelivery"    column="if_delivery"    />
        <result property="deliveries"    column="deliveries"    />
        <result property="ifOrder"    column="if_order"    />
        <result property="ifBook"    column="if_book"    />
        <result property="ifReserve"    column="if_reserve"    />
        <result property="ifPayCode"    column="if_pay_code"    />
        <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="selectBasicCanteenVo">
        select bc.canteen_id, bc.canteen_name, bc.area_id, bc.manager, bc.contact_tel, bc.biz_state,
               bc.biz_start_time, bc.biz_end_time, bc.img_url, bc.enable_pay, bc.pay_types,
               bc.if_delivery, bc.deliveries, bc.if_order, bc.if_book, bc.if_reserve, bc.if_pay_code, bc.del_flag,
               bc.create_by, bc.create_time, bc.update_by, bc.update_time, ba.area_name
        from basic_canteen bc
        left join basic_area ba on bc.area_id = ba.area_id
    </sql>

    <select id="selectBasicCanteenList" parameterType="com.bonus.canteen.core.basic.domain.BasicCanteen" resultMap="BasicCanteenResult">
        <include refid="selectBasicCanteenVo"/>
        <where>
            bc.del_flag = '0'
            <if test="canteenName != null  and canteenName != ''"> and bc.canteen_name like concat('%', #{canteenName}, '%')</if>
            <if test="areaId != null "> and bc.area_id = #{areaId}</if>
            <if test="manager != null  and manager != ''"> and bc.manager = #{manager}</if>
            <if test="contactTel != null  and contactTel != ''"> and bc.contact_tel = #{contactTel}</if>
            <if test="bizState != null "> and bc.biz_state = #{bizState}</if>
            <if test="bizStartTime != null "> and bc.biz_start_time = #{bizStartTime}</if>
            <if test="bizEndTime != null "> and bc.biz_end_time = #{bizEndTime}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and bc.img_url = #{imgUrl}</if>
            <if test="enablePay != null "> and bc.enable_pay = #{enablePay}</if>
            <if test="payTypes != null  and payTypes != ''"> and bc.pay_types = #{payTypes}</if>
            <if test="ifDelivery != null "> and bc.if_delivery = #{ifDelivery}</if>
            <if test="deliveries != null  and deliveries != ''"> and bc.deliveries = #{deliveries}</if>
            <if test="ifOrder != null "> and bc.if_order = #{ifOrder}</if>
            <if test="ifBook != null "> and bc.if_book = #{ifBook}</if>
            <if test="ifReserve != null "> and bc.if_reserve = #{ifReserve}</if>
            <if test="ifPayCode != null "> and bc.if_pay_code = #{ifPayCode}</if>
            <if test="delFlag != null  and delFlag != ''"> and bc.del_flag = #{delFlag}</if>
        </where>
    </select>
    
    <select id="selectBasicCanteenByCanteenId" parameterType="Long" resultMap="BasicCanteenResult">
        <include refid="selectBasicCanteenVo"/>
        where bc.canteen_id = #{canteenId} and bc.del_flag = '0'
    </select>

    <select id="selectBasicCanteenByCanteenName" parameterType="com.bonus.canteen.core.basic.domain.BasicCanteen" resultMap="BasicCanteenResult">
        <include refid="selectBasicCanteenVo"/>
        where bc.canteen_name = #{canteenName} and bc.area_id = #{areaId} and bc.del_flag = '0'
    </select>

    <select id="getBasicCanteenCountByAreaIds" resultType="Integer">
        select count(1)
        from basic_canteen
        where del_flag = '0' and area_id in
        <foreach item="areaId" collection="array" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </select>

    <insert id="insertBasicCanteen" parameterType="com.bonus.canteen.core.basic.domain.BasicCanteen" useGeneratedKeys="true" keyProperty="canteenId">
        insert into basic_canteen
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="canteenName != null and canteenName != ''">canteen_name,</if>
            <if test="areaId != null">area_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="imgUrl != null">img_url,</if>
            <if test="enablePay != null">enable_pay,</if>
            <if test="payTypes != null">pay_types,</if>
            <if test="ifDelivery != null">if_delivery,</if>
            <if test="deliveries != null">deliveries,</if>
            <if test="ifOrder != null">if_order,</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="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="canteenName != null and canteenName != ''">#{canteenName},</if>
            <if test="areaId != null">#{areaId},</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="imgUrl != null">#{imgUrl},</if>
            <if test="enablePay != null">#{enablePay},</if>
            <if test="payTypes != null">#{payTypes},</if>
            <if test="ifDelivery != null">#{ifDelivery},</if>
            <if test="deliveries != null">#{deliveries},</if>
            <if test="ifOrder != null">#{ifOrder},</if>
            <if test="ifBook != null">#{ifBook},</if>
            <if test="ifReserve != null">#{ifReserve},</if>
            <if test="ifPayCode != null">#{ifPayCode},</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="updateBasicCanteen" parameterType="com.bonus.canteen.core.basic.domain.BasicCanteen">
        update basic_canteen
        <trim prefix="SET" suffixOverrides=",">
            <if test="canteenName != null and canteenName != ''">canteen_name = #{canteenName},</if>
            <if test="areaId != null">area_id = #{areaId},</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="imgUrl != null">img_url = #{imgUrl},</if>
            <if test="enablePay != null">enable_pay = #{enablePay},</if>
            <if test="payTypes != null">pay_types = #{payTypes},</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="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="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 canteen_id = #{canteenId}
    </update>

    <delete id="deleteBasicCanteenByCanteenId" parameterType="Long">
        update basic_canteen set del_flag = '2' where canteen_id = #{canteenId}
    </delete>

    <delete id="deleteBasicCanteenByCanteenIds" parameterType="String">
        update basic_canteen set del_flag = '2' where canteen_id in
        <foreach item="canteenId" collection="array" open="(" separator="," close=")">
            #{canteenId}
        </foreach>
    </delete>
</mapper>