<?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.user.mapper.UserAddrMapper">
    <resultMap type="com.bonus.canteen.core.user.domain.UserAddr" id="UserAddrResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="addrId"    column="addr_id"    />
        <result property="contactName"    column="contact_name"    />
        <result property="mobile"    column="mobile"    />
        <result property="areaCode"    column="area_code"    />
        <result property="detailAddr"    column="detail_addr"    />
        <result property="addrFullName"    column="addr_full_name"    />
        <result property="openid"    column="openid"    />
        <result property="sourceType"    column="source_type"    />
        <result property="ifDefault"    column="if_default"    />
        <result property="revision"    column="revision"    />
        <result property="placeId"    column="place_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectUserAddrVo">
        select id, user_id, addr_id, contact_name, mobile, area_code, detail_addr, addr_full_name, openid, source_type, if_default, revision, place_id, create_by, create_time, update_by, update_time, del_flag from user_addr
    </sql>

    <select id="selectUserAddrList" parameterType="com.bonus.canteen.core.user.domain.UserAddr" resultMap="UserAddrResult">
        <include refid="selectUserAddrVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="addrId != null "> and addr_id = #{addrId}</if>
            <if test="contactName != null  and contactName != ''"> and contact_name like concat('%', #{contactName}, '%')</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="detailAddr != null  and detailAddr != ''"> and detail_addr = #{detailAddr}</if>
            <if test="addrFullName != null  and addrFullName != ''"> and addr_full_name like concat('%', #{addrFullName}, '%')</if>
            <if test="openid != null  and openid != ''"> and openid = #{openid}</if>
            <if test="sourceType != null "> and source_type = #{sourceType}</if>
            <if test="ifDefault != null "> and if_default = #{ifDefault}</if>
            <if test="revision != null "> and revision = #{revision}</if>
            <if test="placeId != null "> and place_id = #{placeId}</if>
        </where>
    </select>
    
    <select id="selectUserAddrById" parameterType="Long" resultMap="UserAddrResult">
        <include refid="selectUserAddrVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertUserAddr" parameterType="com.bonus.canteen.core.user.domain.UserAddr" useGeneratedKeys="true" keyProperty="id">
        insert into user_addr
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="addrId != null">addr_id,</if>
            <if test="contactName != null and contactName != ''">contact_name,</if>
            <if test="mobile != null and mobile != ''">mobile,</if>
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="detailAddr != null and detailAddr != ''">detail_addr,</if>
            <if test="addrFullName != null and addrFullName != ''">addr_full_name,</if>
            <if test="openid != null and openid != ''">openid,</if>
            <if test="sourceType != null">source_type,</if>
            <if test="ifDefault != null">if_default,</if>
            <if test="revision != null">revision,</if>
            <if test="placeId != null">place_id,</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>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="addrId != null">#{addrId},</if>
            <if test="contactName != null and contactName != ''">#{contactName},</if>
            <if test="mobile != null and mobile != ''">#{mobile},</if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="detailAddr != null and detailAddr != ''">#{detailAddr},</if>
            <if test="addrFullName != null and addrFullName != ''">#{addrFullName},</if>
            <if test="openid != null and openid != ''">#{openid},</if>
            <if test="sourceType != null">#{sourceType},</if>
            <if test="ifDefault != null">#{ifDefault},</if>
            <if test="revision != null">#{revision},</if>
            <if test="placeId != null">#{placeId},</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>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateUserAddr" parameterType="com.bonus.canteen.core.user.domain.UserAddr">
        update user_addr
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="addrId != null">addr_id = #{addrId},</if>
            <if test="contactName != null and contactName != ''">contact_name = #{contactName},</if>
            <if test="mobile != null and mobile != ''">mobile = #{mobile},</if>
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="detailAddr != null and detailAddr != ''">detail_addr = #{detailAddr},</if>
            <if test="addrFullName != null and addrFullName != ''">addr_full_name = #{addrFullName},</if>
            <if test="openid != null and openid != ''">openid = #{openid},</if>
            <if test="sourceType != null">source_type = #{sourceType},</if>
            <if test="ifDefault != null">if_default = #{ifDefault},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="placeId != null">place_id = #{placeId},</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>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateUserAddrAsNotDefault">
        update user_addr set if_default = #{ifDefault} where user_id = #{userId}
    </update>

    <update id="updateUserAddrAsDefault">
        update user_addr set if_default = #{ifDefault} where user_id = #{userId} and id = #{id}
    </update>

    <delete id="deleteUserAddrById" parameterType="Long">
        delete from user_addr where id = #{id}
    </delete>

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