<?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.supermarket.mapper.SupermarketInfoMapper">
    <resultMap type="com.bonus.canteen.core.supermarket.domain.SupermarketInfo" id="SupermarketInfoResult">
        <result property="supermarketId"    column="supermarket_id"    />
        <result property="supermarketName"    column="supermarket_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="warehouseId"    column="warehouse_id"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="address"    column="address"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="ifPrintNow"    column="if_print_now"    />
        <result property="appSaleMode"    column="app_sale_mode"    />
        <result property="minDeliveryTime"    column="min_delivery_time"    />
        <result property="selectTimeInterval"    column="select_time_interval"    />
        <result property="deliveryCost"    column="delivery_cost"    />
        <result property="autoVerifyDay"    column="auto_verify_day"    />
        <result property="deliveryWay"    column="delivery_way"    />
        <result property="refundLimitTime"    column="refund_limit_time"    />
        <result property="ifRelateDrp"    column="if_relate_drp"    />
        <result property="mealCode"    column="meal_code"    />
        <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="selectSupermarketInfoVo">
        select si.supermarket_id, si.supermarket_name, si.area_id, si.manager, si.contact_tel,
               si.warehouse_id, si.address, si.img_url, si.if_print_now, si.app_sale_mode,
               si.min_delivery_time, si.select_time_interval, si.delivery_cost, si.auto_verify_day,
               si.delivery_way, si.refund_limit_time, si.if_relate_drp, si.meal_code, si.if_enable_pay_code,
               si.pay_code_url, si.del_flag, si.create_by, si.create_time, si.update_by, si.update_time,
               ba.area_name, swi.warehouse_name
        from supermarket_info si
        left join basic_area ba on si.area_id = ba.area_id
        left join supply_warehouse_info swi on swi.warehouse_id = si.warehouse_id
    </sql>

    <select id="selectSupermarketInfoList" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketInfo" resultMap="SupermarketInfoResult">
        <include refid="selectSupermarketInfoVo"/>
        <where>
            si.del_flag = '0'
            <if test="supermarketName != null  and supermarketName != ''"> and si.supermarket_name like concat('%', #{supermarketName}, '%')</if>
            <if test="areaId != null "> and si.area_id = #{areaId}</if>
            <if test="manager != null "> and si.manager = #{manager}</if>
            <if test="contactTel != null  and contactTel != ''"> and si.contact_tel = #{contactTel}</if>
            <if test="warehouseId != null "> and si.warehouse_id = #{warehouseId}</if>
            <if test="address != null  and address != ''"> and si.address = #{address}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and si.img_url = #{imgUrl}</if>
            <if test="ifPrintNow != null "> and si.if_print_now = #{ifPrintNow}</if>
            <if test="appSaleMode != null "> and si.app_sale_mode = #{appSaleMode}</if>
            <if test="minDeliveryTime != null "> and si.min_delivery_time = #{minDeliveryTime}</if>
            <if test="selectTimeInterval != null "> and si.select_time_interval = #{selectTimeInterval}</if>
            <if test="deliveryCost != null "> and si.delivery_cost = #{deliveryCost}</if>
            <if test="autoVerifyDay != null "> and si.auto_verify_day = #{autoVerifyDay}</if>
            <if test="deliveryWay != null  and deliveryWay != ''"> and si.delivery_way = #{deliveryWay}</if>
            <if test="refundLimitTime != null "> and si.refund_limit_time = #{refundLimitTime}</if>
            <if test="ifRelateDrp != null "> and si.if_relate_drp = #{ifRelateDrp}</if>
            <if test="mealCode != null  and mealCode != ''"> and si.meal_code = #{mealCode}</if>
            <if test="ifEnablePayCode != null "> and si.if_enable_pay_code = #{ifEnablePayCode}</if>
            <if test="payCodeUrl != null  and payCodeUrl != ''"> and si.pay_code_url = #{payCodeUrl}</if>
        </where>
    </select>
    
    <select id="selectSupermarketInfoBySupermarketId" parameterType="Long" resultMap="SupermarketInfoResult">
        <include refid="selectSupermarketInfoVo"/>
        where si.supermarket_id = #{supermarketId} and si.del_flag = '0'
    </select>

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

    <select id="selectSupermarketInfoBySupermarketName" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketInfo" resultMap="SupermarketInfoResult">
        <include refid="selectSupermarketInfoVo"/>
        where si.supermarket_name = #{supermarketName} and si.area_id = #{areaId} and si.del_flag = '0'
    </select>
        
    <insert id="insertSupermarketInfo" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketInfo" useGeneratedKeys="true" keyProperty="supermarketId">
        insert into supermarket_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="supermarketName != null and supermarketName != ''">supermarket_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="manager != null">manager,</if>
            <if test="contactTel != null">contact_tel,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="address != null">address,</if>
            <if test="imgUrl != null">img_url,</if>
            <if test="ifPrintNow != null">if_print_now,</if>
            <if test="appSaleMode != null">app_sale_mode,</if>
            <if test="minDeliveryTime != null">min_delivery_time,</if>
            <if test="selectTimeInterval != null">select_time_interval,</if>
            <if test="deliveryCost != null">delivery_cost,</if>
            <if test="autoVerifyDay != null">auto_verify_day,</if>
            <if test="deliveryWay != null and deliveryWay != ''">delivery_way,</if>
            <if test="refundLimitTime != null">refund_limit_time,</if>
            <if test="ifRelateDrp != null">if_relate_drp,</if>
            <if test="mealCode != null">meal_code,</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="supermarketName != null and supermarketName != ''">#{supermarketName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="manager != null">#{manager},</if>
            <if test="contactTel != null">#{contactTel},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="address != null">#{address},</if>
            <if test="imgUrl != null">#{imgUrl},</if>
            <if test="ifPrintNow != null">#{ifPrintNow},</if>
            <if test="appSaleMode != null">#{appSaleMode},</if>
            <if test="minDeliveryTime != null">#{minDeliveryTime},</if>
            <if test="selectTimeInterval != null">#{selectTimeInterval},</if>
            <if test="deliveryCost != null">#{deliveryCost},</if>
            <if test="autoVerifyDay != null">#{autoVerifyDay},</if>
            <if test="deliveryWay != null and deliveryWay != ''">#{deliveryWay},</if>
            <if test="refundLimitTime != null">#{refundLimitTime},</if>
            <if test="ifRelateDrp != null">#{ifRelateDrp},</if>
            <if test="mealCode != null">#{mealCode},</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="updateSupermarketInfo" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketInfo">
        update supermarket_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="supermarketName != null and supermarketName != ''">supermarket_name = #{supermarketName},</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="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="address != null">address = #{address},</if>
            <if test="imgUrl != null">img_url = #{imgUrl},</if>
            <if test="ifPrintNow != null">if_print_now = #{ifPrintNow},</if>
            <if test="appSaleMode != null">app_sale_mode = #{appSaleMode},</if>
            <if test="minDeliveryTime != null">min_delivery_time = #{minDeliveryTime},</if>
            <if test="selectTimeInterval != null">select_time_interval = #{selectTimeInterval},</if>
            <if test="deliveryCost != null">delivery_cost = #{deliveryCost},</if>
            <if test="autoVerifyDay != null">auto_verify_day = #{autoVerifyDay},</if>
            <if test="deliveryWay != null and deliveryWay != ''">delivery_way = #{deliveryWay},</if>
            <if test="refundLimitTime != null">refund_limit_time = #{refundLimitTime},</if>
            <if test="ifRelateDrp != null">if_relate_drp = #{ifRelateDrp},</if>
            <if test="mealCode != null">meal_code = #{mealCode},</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 supermarket_id = #{supermarketId}
    </update>

    <delete id="deleteSupermarketInfoBySupermarketId" parameterType="Long">
        update supermarket_info set del_flag = '2' where supermarket_id = #{supermarketId}
    </delete>

    <delete id="deleteSupermarketInfoBySupermarketIds" parameterType="String">
        update supermarket_info set del_flag = '2' where supermarket_id in
        <foreach item="supermarketId" collection="array" open="(" separator="," close=")">
            #{supermarketId}
        </foreach>
    </delete>
</mapper>