<?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.tmerclub.cloud.supplier.mapper.SupplierRefundAddrMapper">
  <resultMap id="supplierRefundAddrMap" type="com.tmerclub.cloud.supplier.model.SupplierRefundAddr">
    <id column="supplier_refund_addr_id" property="supplierRefundAddrId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="supplier_id" property="supplierId"/>
    <result column="mobile" property="mobile"/>
    <result column="is_default" property="isDefault"/>
    <result column="consignee" property="consignee"/>
    <result column="province_id" property="provinceId"/>
    <result column="province" property="province"/>
    <result column="city_id" property="cityId"/>
    <result column="city" property="city"/>
    <result column="area_id" property="areaId"/>
    <result column="area" property="area"/>
    <result column="post_code" property="postCode"/>
    <result column="addr" property="addr"/>
    <result column="lng" property="lng"/>
    <result column="lat" property="lat"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `supplier_refund_addr_id`,`create_time`,`update_time`,`supplier_id`,`mobile`,`is_default`,`consignee`,`province_id`,`province`,`city_id`,`city`,`area_id`,`area`,`post_code`,`addr`,`lng`,`lat`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.api.supplier.vo.SupplierApiRefundAddrVO">
    select <include refid="Vo_Column_List"/>
    from supplier_refund_addr
    <where>
      <if test="supplierRefundAddr.supplierId != null">
        and supplier_id = #{supplierRefundAddr.supplierId}
      </if>
      <if test="supplierRefundAddr.mobile != null">
        and mobile like CONCAT('%',#{supplierRefundAddr.mobile},'%')
      </if>
      <if test="supplierRefundAddr.consignee != null">
        and consignee like CONCAT('%',#{supplierRefundAddr.consignee},'%')
      </if>
      <if test="supplierRefundAddr.isDefault != null">
        and is_default = #{supplierRefundAddr.isDefault}
      </if>
    </where>
    order by supplier_refund_addr_id desc
  </select>
  <select id="getBySupplierRefundAddrId" resultType="com.tmerclub.cloud.api.supplier.vo.SupplierApiRefundAddrVO">
    select <include refid="Vo_Column_List"/> from supplier_refund_addr where supplier_refund_addr_id = #{supplierRefundAddrId} and supplier_id = #{supplierId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="supplierRefundAddrId">
    insert into supplier_refund_addr (`supplier_id`,`mobile`,`is_default`,`consignee`,`province_id`,`province`,`city_id`,`city`,`area_id`,`area`,`post_code`,`addr`,`lng`,`lat`)
    values (#{supplierRefundAddr.supplierId},#{supplierRefundAddr.mobile},#{supplierRefundAddr.isDefault},#{supplierRefundAddr.consignee},#{supplierRefundAddr.provinceId},#{supplierRefundAddr.province},#{supplierRefundAddr.cityId},#{supplierRefundAddr.city},#{supplierRefundAddr.areaId},#{supplierRefundAddr.area},#{supplierRefundAddr.postCode},#{supplierRefundAddr.addr},#{supplierRefundAddr.lng},#{supplierRefundAddr.lat});
  </insert>
  <update id="update">
    update supplier_refund_addr
    <set>
      <if test="supplierRefundAddr.mobile != null">
        `mobile` = #{supplierRefundAddr.mobile},
      </if>
      <if test="supplierRefundAddr.isDefault != null">
        `is_default` = #{supplierRefundAddr.isDefault},
      </if>
      <if test="supplierRefundAddr.consignee != null">
        `consignee` = #{supplierRefundAddr.consignee},
      </if>
      <if test="supplierRefundAddr.provinceId != null">
        `province_id` = #{supplierRefundAddr.provinceId},
      </if>
      <if test="supplierRefundAddr.province != null">
        `province` = #{supplierRefundAddr.province},
      </if>
      <if test="supplierRefundAddr.cityId != null">
        `city_id` = #{supplierRefundAddr.cityId},
      </if>
      <if test="supplierRefundAddr.city != null">
        `city` = #{supplierRefundAddr.city},
      </if>
      <if test="supplierRefundAddr.areaId != null">
        `area_id` = #{supplierRefundAddr.areaId},
      </if>
      <if test="supplierRefundAddr.area != null">
        `area` = #{supplierRefundAddr.area},
      </if>
      <if test="supplierRefundAddr.postCode != null">
        `post_code` = #{supplierRefundAddr.postCode},
      </if>
      <if test="supplierRefundAddr.addr != null">
        `addr` = #{supplierRefundAddr.addr},
      </if>
      <if test="supplierRefundAddr.lng != null">
        `lng` = #{supplierRefundAddr.lng},
      </if>
      <if test="supplierRefundAddr.lat != null">
        `lat` = #{supplierRefundAddr.lat},
      </if>
    </set>
    where supplier_refund_addr_id = #{supplierRefundAddr.supplierRefundAddrId}
  </update>
  <delete id="deleteById">
    delete from supplier_refund_addr where supplier_refund_addr_id = #{supplierRefundAddrId}
  </delete>

  <select id="listBySupplierId" resultType="com.tmerclub.cloud.api.supplier.vo.SupplierApiRefundAddrVO">
    select * from supplier_refund_addr where supplier_id = #{supplierId}
    </select>

  <update id="cancelDefaultAddr">
    UPDATE supplier_refund_addr SET  `is_default` = 0 WHERE `supplier_id` = #{supplierId}
  </update>
</mapper>
