<?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.cab.dao.CabAddressMapper" >
  <resultMap id="BaseResultMap" type="com.cab.model.CabAddress" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="province" property="province" jdbcType="VARCHAR" /> 
    <result column="city" property="city" jdbcType="VARCHAR" />
    <result column="area" property="area" jdbcType="VARCHAR" />
    <result column="address" property="address" jdbcType="VARCHAR" />
    <result column="phoneNum" property="phonenum" jdbcType="VARCHAR" />
    <result column="postal" property="postal" jdbcType="VARCHAR" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="defaultAess" property="defaultAess" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, province, city, area, address, phoneNum, postal, user_id, defaultAess
  </sql>
  <select id="getUserAddress" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from cab_address
    where user_id = #{userId,jdbcType=INTEGER}
  </select>
  <!-- 删除地址 zhuoHeng -->
  <delete id="deleteUserAddress" parameterType="java.lang.Integer" >
    delete from cab_address
    where id = ${id} and user_id = #{userId}
  </delete>
  <insert id="insertSelective" parameterType="com.cab.model.CabAddress">
    insert into cab_address
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </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="phonenum != null" >
        phoneNum,
      </if>
      <if test="postal != null" >
        postal,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      create_time,update_time,
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="province != null">
      	#{province,jdbcType=VARCHAR},
      </if>
      <if test="city != null" >
        #{city,jdbcType=VARCHAR},
      </if>
      <if test="area != null" >
        #{area,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="phonenum != null" >
        #{phonenum,jdbcType=VARCHAR},
      </if>
      <if test="postal != null" >
        #{postal,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      sysdate(),sysdate(),
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.cab.model.CabAddress" >
    update cab_address
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="city != null or province != null or area != null" >
        city = #{city,jdbcType=VARCHAR},
        province = #{province,jdbcType=VARCHAR},
        area = #{area,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="phonenum != null" >
        phoneNum = #{phonenum,jdbcType=VARCHAR},
      </if>
      <if test="postal != null" >
        postal = #{postal,jdbcType=VARCHAR},
      </if>
      defaultAess=#{defaultAess,jdbcType=INTEGER},
      update_time = sysdate(),
    </set>
    where id = #{id,jdbcType=INTEGER} and user_id=#{userId}
    </update>
  <!-- 修改默认地址标识 zhuoHeng -->
  <update id="setDefaultAess" parameterType="com.cab.model.CabAddress">
  update cab_address set defaultAess = 1 where id = #{id} and user_id = #{userId}
  </update>
  <!-- 重置默认地址标识为0 zhuoHeng -->
  <update id="reSetDefaultAess" parameterType="com.cab.model.CabAddress">
  update cab_address set defaultAess = 0 where user_id = #{userId}
  </update> 
</mapper>