<?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.web.modules.account.mapper.BizMemberAddressMapper">

    <resultMap id="BizMemberAddressResult" type="com.ruoyi.web.modules.account.domain.BizMemberAddress">
        <id property="addressId" column="address_id"/>
        <result property="memberId" column="member_id"/>
        <result property="consigneeName" column="consignee_name"/>
        <result property="mobile" column="mobile"/>
        <result property="regionCode" column="region_code"/>
        <result property="regionName" column="region_name"/>
        <result property="detailAddress" column="detail_address"/>
        <result property="isDefault" column="is_default"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="selectById" resultMap="BizMemberAddressResult">
        select address_id, member_id, consignee_name, mobile, region_code, region_name,
               detail_address, is_default, del_flag, create_time, update_time
        from biz_member_address
        where address_id = #{addressId} and member_id = #{memberId} and del_flag = '0'
    </select>

    <select id="selectByMemberId" resultMap="BizMemberAddressResult">
        select address_id, member_id, consignee_name, mobile, region_code, region_name,
               detail_address, is_default, del_flag, create_time, update_time
        from biz_member_address
        where member_id = #{memberId} and del_flag = '0'
        order by is_default desc, update_time desc
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="addressId">
        insert into biz_member_address (
            member_id, consignee_name, mobile, region_code, region_name, detail_address,
            is_default, del_flag, create_time, update_time
        ) values (
            #{memberId}, #{consigneeName}, #{mobile}, #{regionCode}, #{regionName}, #{detailAddress},
            #{isDefault}, '0', sysdate(), sysdate()
        )
    </insert>

    <update id="update">
        update biz_member_address
        <set>
            <if test="consigneeName != null">consignee_name = #{consigneeName},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="regionCode != null">region_code = #{regionCode},</if>
            <if test="regionName != null">region_name = #{regionName},</if>
            <if test="detailAddress != null">detail_address = #{detailAddress},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            update_time = sysdate()
        </set>
        where address_id = #{addressId} and member_id = #{memberId} and del_flag = '0'
    </update>

    <update id="logicDelete">
        update biz_member_address
        set del_flag = '2', update_time = sysdate()
        where address_id = #{addressId} and member_id = #{memberId} and del_flag = '0'
    </update>

    <update id="clearDefault">
        update biz_member_address
        set is_default = '0', update_time = sysdate()
        where member_id = #{memberId} and del_flag = '0' and is_default = '1'
    </update>

    <update id="setDefault">
        update biz_member_address
        set is_default = '1', update_time = sysdate()
        where address_id = #{addressId} and member_id = #{memberId} and del_flag = '0'
    </update>

</mapper>
