<?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.kmall.api.dao.ApiAddressMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.AddressVo" id="addressMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="telNumber" column="tel_number"/>
        <result property="postalCode" column="postal_Code"/>
        <result property="nationalCode" column="national_Code"/>
        <result property="provinceName" column="province_Name"/>
        <result property="cityName" column="city_Name"/>
        <result property="countyName" column="county_Name"/>
        <result property="detailInfo" column="detail_Info"/>
        <result property="isDefault" column="is_default"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
    </resultMap>

    <select id="queryObject" resultMap="addressMap">
		select * from mall_address where id = #{value}
	</select>

    <select id="queryList" resultMap="addressMap">
        select * from mall_address
        where 1 = 1
        <if test="user_id != null">
            and user_id = #{user_id}
        </if>
        <if test="telNumber and telNumber.trim() != ''">
            AND tel_number = #{telNumber}
        </if>
        <if test="is_default != null">
            AND is_default = #{is_default}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_address
        where 1 = 1
        <if test="userId != null">
            and user_id = #{user_id}
        </if>
        <if test="telNumber and telNumber.trim() != ''">
            AND tel_number = #{telNumber}
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.api.entity.AddressVo" useGeneratedKeys="true" keyProperty="id">
		insert into mall_address(
			`user_id`,
			`user_name`,
			`tel_number`,
			`postal_Code`,
			`national_Code`,
			`province_Name`,
			`city_Name`,
			`county_Name`,
			`detail_Info`,
			`latitude`,
			`longitude`,
			`is_default`)
		values(
			#{userId},
			#{userName},
			#{telNumber},
			#{postalCode},
			#{nationalCode},
			#{provinceName},
			#{cityName},
			#{countyName},
			#{detailInfo},
			#{latitude},
			#{longitude},
			#{isDefault})
	</insert>

    <update id="update" parameterType="com.kmall.api.entity.AddressVo">
        update mall_address
        <set>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="userName != null">`user_name` = #{userName},</if>
            <if test="telNumber != null">`tel_number` = #{telNumber},</if>
            <if test="postalCode != null">`postal_Code` = #{postalCode},</if>
            <if test="nationalCode != null">`national_Code` = #{nationalCode},</if>
            <if test="provinceName != null">`province_Name` = #{provinceName},</if>
            <if test="cityName != null">`city_Name` = #{cityName},</if>
            <if test="countyName != null">`county_Name` = #{countyName},</if>
            <if test="detailInfo != null">`detail_Info` = #{detailInfo},</if>
            <if test="isDefault != null">`is_default` = #{isDefault},</if>
            <if test="latitude != null">`latitude` = #{latitude},</if>
            <if test="longitude != null">`longitude` = #{longitude},</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from mall_address where id = #{value}
	</delete>

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