<?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.leromro.core.mapper.AddressMapper">
    
    <resultMap type="Address" id="AddressResult">
        <result property="addressId"    column="address_id"    />
        <result property="userId"    column="user_id"    />
        <result property="telephone"    column="telephone"    />
        <result property="name"    column="name"    />
        <result property="sex"    column="sex"    />
        <result property="age"    column="age"    />
        <result property="isContagion"    column="is_contagion"    />
        <result property="haveContagion"    column="have_contagion"    />
        <result property="provinceCode"    column="province_code"    />
        <result property="provinceName"    column="province_name"    />
        <result property="cityCode"    column="city_code"    />
        <result property="cityName"    column="city_name"    />
        <result property="districtCode"    column="district_code"    />
        <result property="districtName"    column="district_name"    />
        <result property="address"    column="address"    />
        <result property="isDefault"    column="is_default"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="label"    column="label"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectAddressVo">
        select address_id, user_id, telephone, name,sex, age, is_contagion, have_contagion, province_code, province_name, city_code, city_name, district_code, district_name, address, is_default, is_delete, label, create_time, create_by, update_time, update_by, remark from l_address
    </sql>

    <select id="selectAddressList" parameterType="Address" resultMap="AddressResult">
        <include refid="selectAddressVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="telephone != null  and telephone != ''"> and telephone = #{telephone}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="isContagion != null "> and is_contagion = #{isContagion}</if>
            <if test="haveContagion != null  and haveContagion != ''"> and have_contagion = #{haveContagion}</if>
            <if test="provinceCode != null  and provinceCode != ''"> and province_code = #{provinceCode}</if>
            <if test="provinceName != null  and provinceName != ''"> and province_name like concat('%', #{provinceName}, '%')</if>
            <if test="cityCode != null  and cityCode != ''"> and city_code = #{cityCode}</if>
            <if test="cityName != null  and cityName != ''"> and city_name like concat('%', #{cityName}, '%')</if>
            <if test="districtCode != null  and districtCode != ''"> and district_code = #{districtCode}</if>
            <if test="districtName != null  and districtName != ''"> and district_name like concat('%', #{districtName}, '%')</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="isDefault != null "> and is_default = #{isDefault}</if>
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="label != null  and label != ''"> and label = #{label}</if>
        </where>
    </select>
    
    <select id="selectAddressByAddressId" parameterType="Long" resultMap="AddressResult">
        <include refid="selectAddressVo"/>
        where address_id = #{addressId}
    </select>
    <select id="selectAddressBySeconderId" resultType="com.leromro.core.domain.Address">
        select * from l_address where address_id = (select address_id from l_second_order where second_order_id = #{orderId});
    </select>

    <insert id="insertAddress" parameterType="Address" useGeneratedKeys="true" keyProperty="addressId">
        insert into l_address
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="telephone != null">telephone,</if>
            <if test="name != null">name,</if>
            <if test="sex != null">sex,</if>
            <if test="age != null">age,</if>
            <if test="isContagion != null">is_contagion,</if>
            <if test="haveContagion != null">have_contagion,</if>
            <if test="provinceCode != null">province_code,</if>
            <if test="provinceName != null">province_name,</if>
            <if test="cityCode != null">city_code,</if>
            <if test="cityName != null">city_name,</if>
            <if test="districtCode != null">district_code,</if>
            <if test="districtName != null">district_name,</if>
            <if test="address != null">address,</if>
            <if test="isDefault != null">is_default,</if>
            <if test="isDelete != null">is_delete,</if>
            <if test="label != null">label,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="telephone != null">#{telephone},</if>
            <if test="name != null">#{name},</if>
            <if test="sex != null">#{sex},</if>
            <if test="age != null">#{age},</if>
            <if test="isContagion != null">#{isContagion},</if>
            <if test="haveContagion != null">#{haveContagion},</if>
            <if test="provinceCode != null">#{provinceCode},</if>
            <if test="provinceName != null">#{provinceName},</if>
            <if test="cityCode != null">#{cityCode},</if>
            <if test="cityName != null">#{cityName},</if>
            <if test="districtCode != null">#{districtCode},</if>
            <if test="districtName != null">#{districtName},</if>
            <if test="address != null">#{address},</if>
            <if test="isDefault != null">#{isDefault},</if>
            <if test="isDelete != null">#{isDelete},</if>
            <if test="label != null">#{label},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAddress" parameterType="Address">
        update l_address
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="telephone != null">telephone = #{telephone},</if>
            <if test="name != null">name = #{name},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="age != null">age = #{age},</if>
            <if test="isContagion != null">is_contagion = #{isContagion},</if>
            <if test="haveContagion != null">have_contagion = #{haveContagion},</if>
            <if test="provinceCode != null">province_code = #{provinceCode},</if>
            <if test="provinceName != null">province_name = #{provinceName},</if>
            <if test="cityCode != null">city_code = #{cityCode},</if>
            <if test="cityName != null">city_name = #{cityName},</if>
            <if test="districtCode != null">district_code = #{districtCode},</if>
            <if test="districtName != null">district_name = #{districtName},</if>
            <if test="address != null">address = #{address},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="label != null">label = #{label},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where address_id = #{addressId}
    </update>
    <update id="updateDefaultAddress">
        update l_address set is_default = #{isDefault} where user_id = #{userId}
    </update>

    <delete id="deleteAddressByAddressId" parameterType="Long">
        delete from l_address where address_id = #{addressId}
    </delete>

    <delete id="deleteAddressByAddressIds" parameterType="String">
        delete from l_address where address_id in 
        <foreach item="addressId" collection="array" open="(" separator="," close=")">
            #{addressId}
        </foreach>
    </delete>
</mapper>