<?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.admin.dao.AddressDao">

    <resultMap type="com.kmall.admin.entity.AddressEntity" 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"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.AddressEntity">
        select
        mall_address.id,
        mall_address.user_id,
        mall_address.user_name,
        mall_address.tel_number,
        mall_address.postal_Code,
        mall_address.national_Code,
        mall_address.province_Name,
        mall_address.city_Name,
        mall_address.county_Name,
        mall_address.detail_Info,
        mall_address.is_default,
        mall_user.nickname shop_user_name
        from mall_address LEFT JOIN mall_user ON mall_address.user_id = mall_user.id
        where mall_address.id = #{id}
    </select>

    <select id="queryList" resultType="com.kmall.admin.entity.AddressEntity">
        select
        mall_address.id,
        mall_address.user_id,
        mall_address.user_name,
        mall_address.tel_number,
        mall_address.postal_Code,
        mall_address.national_Code,
        mall_address.province_Name,
        mall_address.city_Name,
        mall_address.county_Name,
        mall_address.detail_Info,
        mall_address.is_default,
        mall_user.nickname shop_user_name
        from mall_address LEFT JOIN mall_user ON mall_address.user_id = mall_user.id
        WHERE 1=1
        <if test="userId != null">
            AND mall_address.user_id = #{userId}
        </if>
        <if test="userName != null and userName != ''">
            AND mall_address.user_name LIKE concat('%', #{userName},'%')
        </if>
        <if test="telNumber != null and telNumber != ''">
            AND mall_address.tel_number LIKE concat('%', #{telNumber},'%')
        </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 mall_address.user_id = #{userId}
        </if>
        <if test="userName != null and userName != ''">
            AND mall_address.user_name LIKE concat('%', #{userName},'%')
        </if>
        <if test="telNumber != null and telNumber != ''">
            AND mall_address.tel_number LIKE concat('%', #{telNumber},'%')
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.AddressEntity" 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`,
        `is_default`)
        values(
        #{userId},
        #{userName},
        #{telNumber},
        #{postalCode},
        #{nationalCode},
        #{provinceName},
        #{cityName},
        #{countyName},
        #{detailInfo},
        #{isDefault})
    </insert>

    <update id="update" parameterType="com.kmall.admin.entity.AddressEntity">
        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>
        </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>