<?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.xingxi.business.UserAddress.mapper.UserAddressMapper">
    <resultMap type="UserAddress" id="UserAddressResult">
        <result property="userAddressId"    column="userAddressId"    />
        <result property="userId"    column="user_id"    />
        <result property="mobile"    column="mobile"    />
        <result property="recipient"    column="recipient"    />
        <result property="province"    column="province"    />
        <result property="city"    column="city"    />
        <result property="district"    column="district"    />
        <result property="address"    column="address"    />
        <result property="defaultFlag"    column="defaultFlag"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectUserAddress">
        select userAddressId, user_id, mobile, recipient, province, city, district, address, defaultFlag, delFlag, createUser, createTime, updateUser, updateTime from t_user_address
    </sql>

    <sql id="selectUserAddressExt">
        select
            ${tableAlias}.userAddressId,
            ${tableAlias}.user_id,
            ${tableAlias}.mobile,
            ${tableAlias}.recipient,
            ${tableAlias}.province,
            ${tableAlias}.city,
            ${tableAlias}.district,
            ${tableAlias}.address,
            ${tableAlias}.defaultFlag,
            ${tableAlias}.delFlag,
            ${tableAlias}.createUser,
            ${tableAlias}.createTime,
            ${tableAlias}.updateUser,
            ${tableAlias}.updateTime
        from t_user_address ${tableAlias}
    </sql>

    <select id="selectUserAddressList" parameterType="UserAddress" resultMap="UserAddressResult">
        <include refid="selectUserAddress"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="recipient != null  and recipient != ''"> and recipient = #{recipient}</if>
            <if test="province != null  and province != ''"> and province = #{province}</if>
            <if test="city != null  and city != ''"> and city = #{city}</if>
            <if test="district != null  and district != ''"> and district = #{district}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="defaultFlag != null  and defaultFlag != ''"> and defaultFlag = #{defaultFlag}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </select>
    
    <select id="selectUserAddressByUserAddressId" parameterType="Long" resultMap="UserAddressResult">
        <include refid="selectUserAddress"/>
        where userAddressId = #{userAddressId}
    </select>
        
    <insert id="insertUserAddress" parameterType="UserAddress" useGeneratedKeys="true" keyProperty="userAddressId">
        insert into t_user_address
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="mobile != null">mobile,</if>
            <if test="recipient != null">recipient,</if>
            <if test="province != null">province,</if>
            <if test="city != null">city,</if>
            <if test="district != null">district,</if>
            <if test="address != null">address,</if>
            <if test="defaultFlag != null">defaultFlag,</if>
            <if test="delFlag != null">delFlag,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
            <if test="updateUser != null">updateUser,</if>
            <if test="updateTime != null">updateTime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="recipient != null">#{recipient},</if>
            <if test="province != null">#{province},</if>
            <if test="city != null">#{city},</if>
            <if test="district != null">#{district},</if>
            <if test="address != null">#{address},</if>
            <if test="defaultFlag != null">#{defaultFlag},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertUserAddress">
        insert into t_user_address( user_id, mobile, recipient, province, city, district, address, defaultFlag, delFlag, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.userId}, #{item.mobile}, #{item.recipient}, #{item.province}, #{item.city}, #{item.district}, #{item.address}, #{item.defaultFlag}, #{item.delFlag}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateUserAddress" parameterType="UserAddress">
        update t_user_address
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="recipient != null">recipient = #{recipient},</if>
            <if test="province != null">province = #{province},</if>
            <if test="city != null">city = #{city},</if>
            <if test="district != null">district = #{district},</if>
            <if test="address != null">address = #{address},</if>
            <if test="defaultFlag != null">defaultFlag = #{defaultFlag},</if>
            <if test="delFlag != null">delFlag = #{delFlag},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
            <if test="updateUser != null">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where userAddressId = #{userAddressId}
    </update>

    <delete id="deleteUserAddressByUserAddressId" parameterType="Long">
        delete from t_user_address where userAddressId = #{userAddressId}
    </delete>

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

    <update id="logicDeleteUserAddressByUserAddressId" parameterType="Long">
        update t_user_address
            set delFlag = '1'
        where userAddressId = #{userAddressId}
    </update>

    <update id="logicDeleteUserAddressByUserAddressIds" parameterType="String">
        update t_user_address
        set delFlag = '1'
        where userAddressId in
        <foreach item="userAddressId" collection="array" open="(" separator="," close=")">
            #{userAddressId}
        </foreach>
    </update>

    <update id="logicDeleteUserAddressByCondition" parameterType="UserAddress">
        update t_user_address
        set delFlag = '1'
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="recipient != null  and recipient != ''"> and recipient = #{recipient}</if>
            <if test="province != null  and province != ''"> and province = #{province}</if>
            <if test="city != null  and city != ''"> and city = #{city}</if>
            <if test="district != null  and district != ''"> and district = #{district}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="defaultFlag != null  and defaultFlag != ''"> and defaultFlag = #{defaultFlag}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </update>
</mapper>