<?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="MdeAddressMapper" >
  <resultMap id="BaseResultMap" type="com.wechat.model.dto.AddressesDto" >
    <id column="ADD_ID" property="addId" jdbcType="INTEGER" />
    <result column="USER_ID" property="userId" jdbcType="BIGINT" />
    <result column="RECIPIENT_NAME" property="recipientName" jdbcType="VARCHAR" />
    <result column="RECIPIENT_TEL" property="recipientTel" jdbcType="VARCHAR" />
    <result column="RECIPIENT_ZIP" property="recipientZip" jdbcType="VARCHAR" />
    <result column="RECIPIENT_ADD" property="recipientAdd" jdbcType="VARCHAR" />
    <result column="ADDR_PROVINCE" property="addrProvince" jdbcType="VARCHAR" />
    <result column="ADDR_CITY" property="addrCity" jdbcType="VARCHAR" />
    <result column="ADDR_AREA" property="addrArea" jdbcType="VARCHAR" />
    <result column="DEFAULT_FLG" property="defaultFlg" jdbcType="CHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    ADD_ID, USER_ID, RECIPIENT_NAME, RECIPIENT_TEL, RECIPIENT_ZIP, RECIPIENT_ADD, ADDR_PROVINCE, 
    ADDR_CITY, ADDR_AREA, DEFAULT_FLG
  </sql>
  <sql id="All_Column_List" >
    ADD_ID, USER_ID, RECIPIENT_NAME, RECIPIENT_TEL, RECIPIENT_ZIP, RECIPIENT_ADD, ADDR_PROVINCE, 
    ADDR_CITY, ADDR_AREA, DEFAULT_FLG, VERSION_NO, REMARKS, DEL_FLAG, CREATE_BY, CREATE_DATE, 
    UPDATE_BY, UPDATE_DATE
  </sql>
  
  <select id="selectByUser" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from mde_address
    where USER_ID = #{userId}
      and DEL_FLAG = '0'
  </select>
  
  <select id="selectByAddid" resultMap="BaseResultMap" parameterType="java.util.Map" >
    select 
    <include refid="Base_Column_List" />
    from mde_address
    where USER_ID = #{userId}
      and ADD_ID = #{addId}
      and DEL_FLAG = '0'
  </select>
  
  <!-- 插入并返回自增主key -->
  <insert id="insert" useGeneratedKeys="true" keyProperty="addId" parameterType="com.wechat.model.dbEntity.MdeAddress" >
    insert into mde_address ( USER_ID, RECIPIENT_NAME, 
      RECIPIENT_TEL, RECIPIENT_ZIP, RECIPIENT_ADD, 
      ADDR_PROVINCE, ADDR_CITY, ADDR_AREA, 
      DEFAULT_FLG, REMARKS, 
      CREATE_BY, CREATE_DATE, 
      UPDATE_BY, UPDATE_DATE)
    values ( #{userId}, #{recipientName}, 
      #{recipientTel}, #{recipientZip}, #{recipientAdd}, 
      #{addrProvince}, #{addrCity}, #{addrArea}, 
      #{defaultFlg}, #{remarks}, 
      #{userId}, #{createDate}, 
      #{userId}, #{updateDate})
  </insert>
  
  <!-- 修改并返回自增主key -->
  <update id="updateByPrimaryKey" useGeneratedKeys="true" keyProperty="addId" parameterType="com.wechat.model.dbEntity.MdeAddress" >
    update mde_address
    set 
      RECIPIENT_NAME = #{recipientName},
      RECIPIENT_TEL = #{recipientTel},
      RECIPIENT_ZIP = #{recipientZip},
      RECIPIENT_ADD = #{recipientAdd},
      ADDR_PROVINCE = #{addrProvince},
      ADDR_CITY = #{addrCity},
      ADDR_AREA = #{addrArea},
      DEFAULT_FLG = #{defaultFlg},
      version_no = version_no +1,
      UPDATE_BY = #{userId},
      UPDATE_DATE = #{updateDate}
    where ADD_ID = #{addId}
     and USER_ID = #{userId}
  </update>
  
  <update id="undefault" parameterType="com.wechat.model.dbEntity.MdeAddress" >
    update mde_address
    set 
      DEFAULT_FLG = '0',
      version_no = version_no +1,
      UPDATE_BY = #{userId},
      UPDATE_DATE = #{updateDate}
     where DEFAULT_FLG = '1'
     and USER_ID = #{userId}
  </update>
  
  <delete id="deleteByPrimaryKey" parameterType="java.util.Map" >
    delete from mde_address
    where ADD_ID = #{addId}
      and USER_ID = #{userId}
  </delete>

  <insert id="insertSelective" parameterType="com.wechat.model.dbEntity.MdeAddress" >
    insert into mde_address
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="addId != null" >
        ADD_ID,
      </if>
      <if test="userId != null" >
        USER_ID,
      </if>
      <if test="recipientName != null" >
        RECIPIENT_NAME,
      </if>
      <if test="recipientTel != null" >
        RECIPIENT_TEL,
      </if>
      <if test="recipientZip != null" >
        RECIPIENT_ZIP,
      </if>
      <if test="recipientAdd != null" >
        RECIPIENT_ADD,
      </if>
      <if test="addrProvince != null" >
        ADDR_PROVINCE,
      </if>
      <if test="addrCity != null" >
        ADDR_CITY,
      </if>
      <if test="addrArea != null" >
        ADDR_AREA,
      </if>
      <if test="defaultFlg != null" >
        DEFAULT_FLG,
      </if>
      <if test="versionNo != null" >
        VERSION_NO,
      </if>
      <if test="remarks != null" >
        REMARKS,
      </if>
      <if test="delFlag != null" >
        DEL_FLAG,
      </if>
      <if test="createBy != null" >
        CREATE_BY,
      </if>
      <if test="createDate != null" >
        CREATE_DATE,
      </if>
      <if test="updateBy != null" >
        UPDATE_BY,
      </if>
      <if test="updateDate != null" >
        UPDATE_DATE,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="addId != null" >
        #{addId,jdbcType=INTEGER},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=BIGINT},
      </if>
      <if test="recipientName != null" >
        #{recipientName,jdbcType=VARCHAR},
      </if>
      <if test="recipientTel != null" >
        #{recipientTel,jdbcType=VARCHAR},
      </if>
      <if test="recipientZip != null" >
        #{recipientZip,jdbcType=VARCHAR},
      </if>
      <if test="recipientAdd != null" >
        #{recipientAdd,jdbcType=VARCHAR},
      </if>
      <if test="addrProvince != null" >
        #{addrProvince,jdbcType=VARCHAR},
      </if>
      <if test="addrCity != null" >
        #{addrCity,jdbcType=VARCHAR},
      </if>
      <if test="addrArea != null" >
        #{addrArea,jdbcType=VARCHAR},
      </if>
      <if test="defaultFlg != null" >
        #{defaultFlg,jdbcType=CHAR},
      </if>
      <if test="versionNo != null" >
        #{versionNo,jdbcType=INTEGER},
      </if>
      <if test="remarks != null" >
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="delFlag != null" >
        #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="createBy != null" >
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null" >
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.wechat.model.dbEntity.MdeAddress" >
    update mde_address
    <set >
      <if test="userId != null" >
        USER_ID = #{userId,jdbcType=BIGINT},
      </if>
      <if test="recipientName != null" >
        RECIPIENT_NAME = #{recipientName,jdbcType=VARCHAR},
      </if>
      <if test="recipientTel != null" >
        RECIPIENT_TEL = #{recipientTel,jdbcType=VARCHAR},
      </if>
      <if test="recipientZip != null" >
        RECIPIENT_ZIP = #{recipientZip,jdbcType=VARCHAR},
      </if>
      <if test="recipientAdd != null" >
        RECIPIENT_ADD = #{recipientAdd,jdbcType=VARCHAR},
      </if>
      <if test="addrProvince != null" >
        ADDR_PROVINCE = #{addrProvince,jdbcType=VARCHAR},
      </if>
      <if test="addrCity != null" >
        ADDR_CITY = #{addrCity,jdbcType=VARCHAR},
      </if>
      <if test="addrArea != null" >
        ADDR_AREA = #{addrArea,jdbcType=VARCHAR},
      </if>
      <if test="defaultFlg != null" >
        DEFAULT_FLG = #{defaultFlg,jdbcType=CHAR},
      </if>
      <if test="versionNo != null" >
        VERSION_NO = #{versionNo,jdbcType=INTEGER},
      </if>
      <if test="remarks != null" >
        REMARKS = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="delFlag != null" >
        DEL_FLAG = #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="createBy != null" >
        CREATE_BY = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        CREATE_DATE = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        UPDATE_BY = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null" >
        UPDATE_DATE = #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where ADD_ID = #{addId,jdbcType=INTEGER}
  </update>
  
</mapper>