<?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.mall.mapper.MallMemberAddressMapper">

    <resultMap type="MallMemberAddress" id="MallMemberAddressResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="name"    column="name"    />
        <result property="phone"    column="phone"    />
        <result property="province"    column="province"    />
        <result property="city"    column="city"    />
        <result property="area"    column="area"    />
        <result property="address"    column="address"    />
        <result property="isDefault"    column="is_default"    />
        <result property="status"    column="status"    />
        <result property="createAt"    column="create_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="meetId"    column="meet_id"    />
        <result property="meetAddress"    column="meet_address"    />
    </resultMap>

    <resultMap type="com.ruoyi.mall.domain.vo.MemberAddressVO" id="MemberAddressVOResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="name"    column="name"    />
        <result property="phone"    column="phone"    />
        <result property="province"    column="province"    />
        <result property="city"    column="city"    />
        <result property="area"    column="area"    />
        <result property="address"    column="address"    />
        <result property="isDefault"    column="is_default"    />
        <result property="meetId"    column="meet_id"    />
        <result property="meetAddress"    column="meet_address"    />
    </resultMap>

    <sql id="selectMallMemberAddressVo">
        select id, user_id, name, phone, province, city, area, address, is_default, status, create_at, del_flag, meet_id, meet_address from mall_member_address
    </sql>

    <select id="selectMallMemberAddressList" parameterType="MallMemberAddress" resultMap="MallMemberAddressResult">
        <include refid="selectMallMemberAddressVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="meetId != null  and meetId != ''"> and meet_id = #{meetId}</if>
             and del_flag = '0'
        </where>
    </select>

    <select id="queryByUserId" resultMap="MallMemberAddressResult">
        <include refid="selectMallMemberAddressVo"/>
        where del_flag='0'
        and user_id = #{userId}
        <if test="queryName != null and queryName != ''"> and name like CONCAT(CONCAT('%',#{queryName},'%')) </if>
        order by create_at desc
    </select>

    <select id="selectMallMemberAddressById" parameterType="Long" resultMap="MallMemberAddressResult">
        <include refid="selectMallMemberAddressVo"/>
        where id = #{id}
    </select>

    <select id="getDefault" parameterType="Long" resultMap="MallMemberAddressResult">
        <include refid="selectMallMemberAddressVo"/>
        where user_id = #{userId}
        and `is_default` = '1'
        limit 1
    </select>
    <select id="selectMallMemberAddressVOById" parameterType="Long" resultMap="MemberAddressVOResult">
        select id, user_id, name, phone, province, city, area, address, is_default, meet_id, meet_address from mall_member_address
        where id = #{l}
        and del_flag='0'
    </select>
    <select id="queryAddressVOByUserId" resultMap="MemberAddressVOResult">
        select id, user_id, name, phone, province, city, area, address, is_default, meet_id, meet_address from mall_member_address
        where del_flag='0'
        and user_id = #{userId}
        order by create_at desc
    </select>
    <select id="getDefaultVO" parameterType="Long" resultMap="MemberAddressVOResult">
        select id, user_id, name, phone, province, city, area, address, is_default, meet_id, meet_address from mall_member_address
        where user_id = #{userId}
        and is_default = '1'
        and del_flag='0'
        limit 1
    </select>


    <insert id="insertMallMemberAddress" parameterType="MallMemberAddress" useGeneratedKeys="true" keyProperty="id">
        insert into mall_member_address
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="name != null">name,</if>
            <if test="phone != null">phone,</if>
            <if test="province != null">province,</if>
            <if test="city != null">city,</if>
            <if test="area != null">area,</if>
            <if test="address != null">address,</if>
            <if test="isDefault != null">is_default,</if>
            <if test="status != null">status,</if>
            <if test="createAt != null">create_at,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="meetId != null">meet_id,</if>
            <if test="meetAddress != null">meet_address,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="name != null">#{name},</if>
            <if test="phone != null">#{phone},</if>
            <if test="province != null">#{province},</if>
            <if test="city != null">#{city},</if>
            <if test="area != null">#{area},</if>
            <if test="address != null">#{address},</if>
            <if test="isDefault != null">#{isDefault},</if>
            <if test="status != null">#{status},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="meetId != null">#{meetId},</if>
            <if test="meetAddress != null">#{meetAddress},</if>
         </trim>
    </insert>

    <update id="removeDefault" >
        update mall_member_address
        set is_default = '0'
        where user_id = #{value}
    </update>

    <update id="updateMallMemberAddress" parameterType="MallMemberAddress">
        update mall_member_address
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="province != null">province = #{province},</if>
            <if test="city != null">city = #{city},</if>
            <if test="area != null">area = #{area},</if>
            <if test="address != null">address = #{address},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="meetId != null">meet_id = #{meetId},</if>
            <if test="meetAddress != null">meet_address = #{meetAddress},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="removeMallMemberAddressById" parameterType="Long">
        update mall_member_address set del_flag='1'
        where id = #{id}
    </update>
    <delete id="deleteMallMemberAddressById" parameterType="Long">
        delete from mall_member_address where id = #{id}
    </delete>

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