<?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.ghy.customer.mapper.CustomerAddressMapper">

    <resultMap id="CustomerAddressResult" type="com.ghy.customer.domain.CustomerAddress">
        <result property="customerAddressId"          column="customer_address_id"     />
        <result property="customerId"          column="customer_id"     />
        <result property="name"                column="name"     />
        <result property="phone"               column="phone"     />
        <result property="provinceId"          column="province_id"/>
        <result property="cityId"          column="city_id"/>
        <result property="countryId"          column="country_id"/>
        <result property="address"          column="address"/>
        <result property="status"              column="status"     />
        <result property="isDefault"              column="is_default"     />
        <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="remark"              column="remark"        />
    </resultMap>

    <sql id="selectCustomerAddress">
        SELECT customer_address_id, customer_id, name, phone, province_id, city_id, country_id, status,
               address, create_by, create_time, remark, is_default
        FROM customer_address
    </sql>

    <select id="getCustomerAddressList" resultMap="CustomerAddressResult">
        <include refid="selectCustomerAddress" />
        <where>
            deleted = 0
            <if test="customerId != null">
                AND customer_id = #{customerId}
            </if>
        </where>
    </select>

    <delete id="deleteByIds">
        UPDATE customer_address SET deleted = 1 WHERE customer_address_id IN
        <foreach collection="array" item="customerIds" open="(" separator="," close=")">
            #{customerAddressIds}
        </foreach>
    </delete>

    <delete id="deleteByCustomerAddressId" parameterType="Long">
        UPDATE customer_address SET deleted = 1 WHERE customer_address_id = #{customerAddressId}
    </delete>

    <select id="selectByCustomerId" resultMap="CustomerAddressResult">
        <include refid="selectCustomerAddress"/>
        <where>
            deleted = 0
            <if test="customerId != null and customerId != 0">
                AND customer_id = #{customerId}
            </if>
        </where>
    </select>

    <select id="selectByCustomerAddressId" resultMap="CustomerAddressResult">
        <include refid="selectCustomerAddress"/>
        WHERE customer_address_id = #{customerAddressId}
    </select>

    <insert id="insertCustomerAddress" parameterType="com.ghy.customer.domain.CustomerAddress" useGeneratedKeys="true" keyProperty="customerAddressId">
        insert into customer_address(
        <if test="customerId != null and customerId != 0">customer_id,</if>
        <if test="name != null and name != ''">name,</if>
        <if test="phone != null and phone != ''">phone,</if>
        <if test="provinceId != null and provinceId != 0">province_id,</if>
        <if test="cityId != null and cityId != 0">city_id,</if>
        <if test="countryId != null and countryId != 0">country_id,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="address != null and address != ''">address,</if>
        <if test="isDefault != null">is_default,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="remark != null and remark != ''">remark,</if>
        deleted,
        create_time
        )values(
        <if test="customerId != null and customerId != 0">#{customerId},</if>
        <if test="name != null and name != ''">#{name},</if>
        <if test="phone != null and phone != ''">#{phone},</if>
        <if test="provinceId != null and provinceId != 0">#{provinceId},</if>
        <if test="cityId != null and cityId != 0">#{cityId},</if>
        <if test="countryId != null and countryId != 0">#{countryId},</if>
        <if test="status != null">#{status},</if>
        <if test="address != null and address != ''">#{address},</if>
        <if test="isDefault != null">#{isDefault},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        0,
        sysdate()
        )
    </insert>

    <update id="updateCustomerAddress" parameterType="com.ghy.customer.domain.CustomerAddress">
        update customer_address
        <set>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="provinceId != null and provinceId != 0">province_id = #{provinceId},</if>
            <if test="cityId != null and cityId != 0">city_id = #{cityId},</if>
            <if test="countryId != null and countryId != 0">country_id = #{countryId},</if>
            <if test="address != null and address != ''">address = #{address},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where customer_address_id = #{customerAddressId}
    </update>

</mapper>
