<?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.ruoyi.system.mapper.UserAddressMapper">
    
    <resultMap type="UserAddress" id="UserAddressResult">
        <result property="id"    column="id"    />
        <result property="uid"    column="uid"    />
        <result property="name"    column="name"    />
        <result property="phone"    column="phone"    />
        <result property="latitude"    column="latitude"    />
        <result property="longitude"    column="longitude"    />
        <result property="addressName"    column="address_name"    />
        <result property="addressInfo"    column="address_info"    />
        <result property="info"    column="info"    />
        <result property="isDefault"    column="is_default"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
        <result property="deletedAt"    column="deleted_at"    />
    </resultMap>

    <sql id="selectUserAddressVo">
        select id, uid, name, phone, latitude, longitude, address_name, address_info, info, is_default, created_at, updated_at, deleted_at from user_address
    </sql>

    <select id="selectUserAddressList" parameterType="UserAddress" resultMap="UserAddressResult">
        <include refid="selectUserAddressVo"/>
        <where>  
            <if test="uid != null "> and uid = #{uid}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="info != null  and info != ''"> and info = #{info}</if>
            <if test="isDefault != null "> and is_default = #{isDefault}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="updatedAt != null "> and updated_at = #{updatedAt}</if>
        </where>
    </select>
    
    <select id="selectUserAddressById" parameterType="Long" resultMap="UserAddressResult">
        <include refid="selectUserAddressVo"/>
        where id = #{id}
    </select>

    <insert id="insertUserAddress" parameterType="UserAddress" useGeneratedKeys="true" keyProperty="id">
        insert into user_address
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="uid != null">uid,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="latitude != null and latitude != ''">latitude,</if>
            <if test="longitude != null and longitude != ''">longitude,</if>
            <if test="addressName != null">address_name,</if>
            <if test="addressInfo != null">address_info,</if>
            <if test="info != null and info != ''">info,</if>
            <if test="isDefault != null">is_default,</if>
            <if test="deletedAt != null">deleted_at,</if>
            created_at,
            updated_at

         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="uid != null">#{uid},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="latitude != null and latitude != ''">#{latitude},</if>
            <if test="longitude != null and longitude != ''">#{longitude},</if>
            <if test="addressName != null">#{addressName},</if>
            <if test="addressInfo != null">#{addressInfo},</if>
            <if test="info != null and info != ''">#{info},</if>
            <if test="isDefault != null">#{isDefault},</if>
            <if test="deletedAt != null">#{deletedAt},</if>
            NOW(),
            NOW()

         </trim>
    </insert>

    <update id="updateUserAddress" parameterType="UserAddress">
        update user_address
        <trim prefix="SET" suffixOverrides=",">
            <if test="uid != null">uid = #{uid},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="latitude != null and latitude != ''">latitude = #{latitude},</if>
            <if test="longitude != null and longitude != ''">longitude = #{longitude},</if>
            <if test="addressName != null">address_name = #{addressName},</if>
            <if test="addressInfo != null">address_info = #{addressInfo},</if>
            <if test="info != null and info != ''">info = #{info},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="deletedAt != null">deleted_at = #{deletedAt},</if>
             updated_at=NOW()

        </trim>
        where id = #{id}
    </update>

    <delete id="deleteUserAddressById" parameterType="Long">
        delete from user_address where id = #{id}
    </delete>

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