<?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.diaoyun.zion.chinafrica.dao.TbCfAddressDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfAddressEntity" id="tbCfAddressMap">
        <result property="addressId" column="address_id"/>
        <result property="userId" column="user_id"/>
        <result property="deliveryName" column="delivery_name"/>
        <result property="phone" column="phone"/>
        <result property="defaultFlag" column="default_flag"/>
        <result property="addressDetail" column="address_detail"/>
        <result property="addressCountryCode" column="address_country_code"/>
        <result property="addressCountryName" column="address_country_name"/>
        <result property="addressStateCode" column="address_state_code"/>
        <result property="addressStateName" column="address_state_name"/>
        <result property="addressAreaCode" column="address_area_code"/>
        <result property="addressAreaName" column="address_area_name"/>
        <result property="labelCode" column="label_code"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

	<select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfAddressEntity">
		select
			`address_id`,
			`user_id`,
			`delivery_name`,
			`phone`,
			`default_flag`,
			`address_detail`,
			`address_country_code`,
			`address_country_name`,
			`address_state_code`,
			`address_state_name`,
			`address_area_code`,
			`address_area_name`,
			`label_code`,
			`create_time`,
			`update_time`
		from tb_cf_address
		where address_id = #{id}
	</select>

	<select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfAddressEntity">
		select
    		`address_id`,
    		`user_id`,
    		`delivery_name`,
    		`phone`,
    		`default_flag`,
    		`address_detail`,
    		`address_country_code`,
    		`address_country_name`,
    		`address_state_code`,
    		`address_state_name`,
    		`address_area_code`,
    		`address_area_name`,
    		`label_code`,
    		`create_time`,
    		`update_time`
		from tb_cf_address
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by address_id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from tb_cf_address
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>
	 
	<insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfAddressEntity">
		insert into tb_cf_address(
			`address_id`,
			`user_id`,
			`delivery_name`,
			`phone`,
			`default_flag`,
			`address_detail`,
			`address_country_code`,
			`address_country_name`,
			`address_state_code`,
			`address_state_name`,
			`address_area_code`,
			`address_area_name`,
			`label_code`,
			`create_time`,
			`update_time`)
		values(
			#{addressId},
			#{userId},
			#{deliveryName},
			#{phone},
			#{defaultFlag},
			#{addressDetail},
			#{addressCountryCode},
			#{addressCountryName},
			#{addressStateCode},
			#{addressStateName},
			#{addressAreaCode},
			#{addressAreaName},
			#{labelCode},
			#{createTime},
			#{updateTime})
	</insert>
	 
	<update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfAddressEntity">
		update tb_cf_address 
		<set>
			<if test="userId != null">`user_id` = #{userId}, </if>
			<if test="deliveryName != null">`delivery_name` = #{deliveryName}, </if>
			<if test="phone != null">`phone` = #{phone}, </if>
			<if test="defaultFlag != null">`default_flag` = #{defaultFlag}, </if>
			<if test="addressDetail != null">`address_detail` = #{addressDetail}, </if>
			<if test="addressCountryCode != null">`address_country_code` = #{addressCountryCode}, </if>
			<if test="addressCountryName != null">`address_country_name` = #{addressCountryName}, </if>
			<if test="addressStateCode != null">`address_state_code` = #{addressStateCode}, </if>
			<if test="addressStateName != null">`address_state_name` = #{addressStateName}, </if>
			<if test="addressAreaCode != null">`address_area_code` = #{addressAreaCode}, </if>
			<if test="addressAreaName != null">`address_area_name` = #{addressAreaName}, </if>
			<if test="labelCode != null">`label_code` = #{labelCode}, </if>
			<if test="createTime != null">`create_time` = #{createTime}, </if>
			<if test="updateTime != null">`update_time` = #{updateTime}</if>
		</set>
		where address_id = #{addressId}
	</update>
	
	<delete id="delete">
		delete from tb_cf_address where address_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from tb_cf_address where address_id in 
		<foreach item="addressId" collection="array" open="(" separator="," close=")">
			#{addressId}
		</foreach>
	</delete>

	<!--根据用户id获取地址列表-->
	<select id="queryListByUserId" resultType="com.diaoyun.zion.chinafrica.entity.TbCfAddressEntity">
		select
		`address_id`,
		`user_id`,
		`delivery_name`,
		`phone`,
		`default_flag`,
		`address_detail`,
		`address_country_code`,
		`address_country_name`,
		`address_state_code`,
		`address_state_name`,
		`address_area_code`,
		`address_area_name`,
		`label_code`,
		`create_time`,
		`update_time`
		from tb_cf_address
		WHERE 1=1 and user_id=#{userId}
		order by update_time desc
	</select>

    <!--重置用户全部地址的默认地址Flag-->
    <update id="resetDefaulFlag">
        update tb_cf_address set default_flag=0 where user_id=#{userId}
    </update>

	<!--获取用户默认地址-->
	<select id="getDefaultAddress" resultType="com.diaoyun.zion.chinafrica.entity.TbCfAddressEntity">
		select t1.* from tb_cf_address t1,tb_cf_user_info t2 where t2.user_id=#{userId} and t2.default_address_id=t1.address_id
	</select>

</mapper>