<?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="cn.iocoder.mall.user.biz.dao.UserAddressMapper">

    <sql id="FIELDS">
        id, user_id, area_no, `name`, mobile, address,
        create_time, update_time, has_default, deleted
    </sql>

    <insert id="insert" parameterType="UserAddressDO" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO user_address (
            user_id, area_no, `name`, mobile, address,
            create_time, update_time, has_default, deleted
        ) VALUES (
          #{userId}, #{areaNo}, #{name}, #{mobile}, #{address},
          #{createTime}, #{updateTime}, #{hasDefault}, #{deleted}
        )
    </insert>

    <update id="updateById">
        UPDATE user_address
        <set>
            <if test="userAddressDO.areaNo != null">
                , area_no = #{userAddressDO.areaNo}
            </if>
            <if test="userAddressDO.name != null">
                , `name` = #{userAddressDO.name}
            </if>
            <if test="userAddressDO.mobile != null">
                , mobile = #{userAddressDO.mobile}
            </if>
            <if test="userAddressDO.address != null">
                , address = #{userAddressDO.address}
            </if>
            <if test="userAddressDO.updateTime != null">
                , update_time = #{userAddressDO.updateTime}
            </if>
            <if test="userAddressDO.hasDefault != null">
                , has_default = #{userAddressDO.hasDefault}
            </if>
            <if test="userAddressDO.deleted != null">
                , deleted = #{userAddressDO.deleted}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="selectByUserIdAndId" resultType="cn.iocoder.mall.user.biz.dataobject.UserAddressDO">
        SELECT
        <include refid="FIELDS" />
        FROM user_address
        WHERE user_id = #{userId}
        AND id = #{id}
    </select>

    <select id="selectByUserIdAndDeleted" resultType="cn.iocoder.mall.user.biz.dataobject.UserAddressDO">
        SELECT
        <include refid="FIELDS" />
        FROM user_address
        WHERE deleted = #{deleted}
        AND `user_id` = #{userId}
    </select>

    <select id="selectHasDefault" resultType="cn.iocoder.mall.user.biz.dataobject.UserAddressDO">
        SELECT
        <include refid="FIELDS" />
        FROM user_address
        WHERE deleted = #{deleted}
        AND `user_id` = #{userId}
        AND `has_default` = #{hasDefault}
    </select>
</mapper>