<?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.AllocCanteenMapper">
    <resultMap type="com.bonus.canteen.core.alloc.domain.AllocCanteen" id="AllocCanteenResult">
        <result property="canteenId"    column="canteen_id"    />
        <result property="canteenName"    column="canteen_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="canteenType"    column="canteen_type"    />
        <result property="director"    column="director"    />
        <result property="contactTel"    column="contact_tel"    />
        <result property="businessState"    column="business_state"    />
        <result property="startBusinessTime"    column="start_business_time"    />
        <result property="endBusinessTime"    column="end_business_time"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="ifEnablePay"    column="if_enable_pay"    />
        <result property="payTypes"    column="pay_types"    />
        <result property="ifEnableDelivery"    column="if_enable_delivery"    />
        <result property="deliveries"    column="deliveries"    />
        <result property="ifEnableOrder"    column="if_enable_order"    />
        <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"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectAllocCanteenVo">
        select ac.canteen_id, ac.canteen_name, aa.area_name, ac.area_id, ac.canteen_type, ac.director, ac.contact_tel, ac.business_state,
               ac.start_business_time, ac.end_business_time, ac.img_url, ac.if_enable_pay, ac.pay_types, ac.if_enable_delivery,
               ac.deliveries, ac.if_enable_order, ac.if_book, ac.if_reserve, ac.if_enable_pay_code, ac.pay_code_url, ac.del_flag,
               ac.create_by, ac.create_time, ac.update_by, ac.update_time, ac.remark
        from alloc_canteen ac
        left join alloc_area aa on ac.area_id = aa.area_id
    </sql>

    <select id="selectAllocCanteenList" parameterType="com.bonus.canteen.core.alloc.domain.AllocCanteen" resultMap="AllocCanteenResult">
        <include refid="selectAllocCanteenVo"/>
        <where>  
            <if test="canteenName != null  and canteenName != ''"> and ac.canteen_name like concat('%', #{canteenName}, '%')</if>
            <if test="areaId != null "> and ac.area_id = #{areaId}</if>
            <if test="canteenType != null "> and ac.canteen_type = #{canteenType}</if>
            <if test="director != null "> and ac.director = #{director}</if>
            <if test="contactTel != null  and contactTel != ''"> and ac.contact_tel = #{contactTel}</if>
            <if test="businessState != null "> and ac.business_state = #{businessState}</if>
            <if test="startBusinessTime != null "> and ac.start_business_time = #{startBusinessTime}</if>
            <if test="endBusinessTime != null "> and ac.end_business_time = #{endBusinessTime}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and ac.img_url = #{imgUrl}</if>
            <if test="ifEnablePay != null "> and ac.if_enable_pay = #{ifEnablePay}</if>
            <if test="payTypes != null  and payTypes != ''"> and ac.pay_types = #{payTypes}</if>
            <if test="ifEnableDelivery != null "> and ac.if_enable_delivery = #{ifEnableDelivery}</if>
            <if test="deliveries != null  and deliveries != ''"> and ac.deliveries = #{deliveries}</if>
            <if test="ifEnableOrder != null "> and ac.if_enable_order = #{ifEnableOrder}</if>
            <if test="ifBook != null "> and ac.if_book = #{ifBook}</if>
            <if test="ifReserve != null "> and ac.if_reserve = #{ifReserve}</if>
            <if test="ifEnablePayCode != null "> and ac.if_enable_pay_code = #{ifEnablePayCode}</if>
            <if test="payCodeUrl != null  and payCodeUrl != ''"> and ac.pay_code_url = #{payCodeUrl}</if>
        </where>
    </select>
    
    <select id="selectAllocCanteenByCanteenId" parameterType="Long" resultMap="AllocCanteenResult">
        <include refid="selectAllocCanteenVo"/>
        where ac.canteen_id = #{canteenId}
    </select>
        
    <insert id="insertAllocCanteen" parameterType="com.bonus.canteen.core.alloc.domain.AllocCanteen" useGeneratedKeys="true" keyProperty="canteenId">
        insert into alloc_canteen
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="canteenName != null and canteenName != ''">canteen_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="canteenType != null">canteen_type,</if>
            <if test="director != null">director,</if>
            <if test="contactTel != null">contact_tel,</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="imgUrl != null">img_url,</if>
            <if test="ifEnablePay != null">if_enable_pay,</if>
            <if test="payTypes != null">pay_types,</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="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>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="canteenName != null and canteenName != ''">#{canteenName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="canteenType != null">#{canteenType},</if>
            <if test="director != null">#{director},</if>
            <if test="contactTel != null">#{contactTel},</if>
            <if test="businessState != null">#{businessState},</if>
            <if test="startBusinessTime != null">#{startBusinessTime},</if>
            <if test="endBusinessTime != null">#{endBusinessTime},</if>
            <if test="imgUrl != null">#{imgUrl},</if>
            <if test="ifEnablePay != null">#{ifEnablePay},</if>
            <if test="payTypes != null">#{payTypes},</if>
            <if test="ifEnableDelivery != null">#{ifEnableDelivery},</if>
            <if test="deliveries != null">#{deliveries},</if>
            <if test="ifEnableOrder != null">#{ifEnableOrder},</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>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAllocCanteen" parameterType="com.bonus.canteen.core.alloc.domain.AllocCanteen">
        update alloc_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="canteenType != null">canteen_type = #{canteenType},</if>
            <if test="director != null">director = #{director},</if>
            <if test="contactTel != null">contact_tel = #{contactTel},</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="imgUrl != null">img_url = #{imgUrl},</if>
            <if test="ifEnablePay != null">if_enable_pay = #{ifEnablePay},</if>
            <if test="payTypes != null">pay_types = #{payTypes},</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="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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where canteen_id = #{canteenId}
    </update>

    <delete id="deleteAllocCanteenByCanteenId" parameterType="Long">
        delete from alloc_canteen where canteen_id = #{canteenId}
    </delete>

    <delete id="deleteAllocCanteenByCanteenIds" parameterType="String">
        delete from alloc_canteen where canteen_id in 
        <foreach item="canteenId" collection="array" open="(" separator="," close=")">
            #{canteenId}
        </foreach>
    </delete>
</mapper>