<?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.multishop.mapper.ShopRefundAddrMapper">
  <resultMap id="shopRefundAddrMap" type="com.tmerclub.cloud.multishop.model.ShopRefundAddr">
    <id column="shop_refund_addr_id" property="shopRefundAddrId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="shop_id" property="shopId"/>
    <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">
    `shop_refund_addr_id`,`create_time`,`update_time`,`shop_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.multishop.vo.ShopRefundAddrVO">
    select <include refid="Vo_Column_List"/>
    from shop_refund_addr
    <where>
      <if test="shopRefundAddr.shopId != null">
        shop_id = #{shopRefundAddr.shopId}
      </if>
      <if test="shopRefundAddr.mobile != null and shopRefundAddr.mobile != ''">
        and mobile like CONCAT('%',#{shopRefundAddr.mobile},'%')
      </if>
      <if test="shopRefundAddr.consignee != null and shopRefundAddr.consignee != ''">
        and consignee like CONCAT('%',#{shopRefundAddr.consignee},'%')
      </if>
      <if test="shopRefundAddr.isDefault != null">
        and is_default = #{shopRefundAddr.isDefault}
      </if>
    </where>
    order by shop_refund_addr_id desc
  </select>
  <select id="getByShopRefundAddrId" resultType="com.tmerclub.cloud.api.multishop.vo.ShopRefundAddrVO">
    select <include refid="Vo_Column_List"/> from shop_refund_addr
    where shop_id = #{shopId}
    <if test="shopRefundAddrId == 0">
      and is_default = 1
    </if>
    <if test="shopRefundAddrId != 0">
      and shop_refund_addr_id = #{shopRefundAddrId}
    </if>
  </select>

  <insert id="save" useGeneratedKeys="true" keyProperty="shopRefundAddrId">
    insert into shop_refund_addr (`shop_id`,`mobile`,`is_default`,`consignee`,`province_id`,`province`,`city_id`,`city`,`area_id`,`area`,`post_code`,`addr`,`lng`,`lat`)
    values (#{shopRefundAddr.shopId},#{shopRefundAddr.mobile},#{shopRefundAddr.isDefault},#{shopRefundAddr.consignee},#{shopRefundAddr.provinceId},#{shopRefundAddr.province},#{shopRefundAddr.cityId},#{shopRefundAddr.city},#{shopRefundAddr.areaId},#{shopRefundAddr.area},#{shopRefundAddr.postCode},#{shopRefundAddr.addr},#{shopRefundAddr.lng},#{shopRefundAddr.lat});
  </insert>
  <update id="update">
    update shop_refund_addr
    <set>
      <if test="shopRefundAddr.mobile != null">
        `mobile` = #{shopRefundAddr.mobile},
      </if>
      <if test="shopRefundAddr.isDefault != null">
        `is_default` = #{shopRefundAddr.isDefault},
      </if>
      <if test="shopRefundAddr.consignee != null">
        `consignee` = #{shopRefundAddr.consignee},
      </if>
      <if test="shopRefundAddr.provinceId != null">
        `province_id` = #{shopRefundAddr.provinceId},
      </if>
      <if test="shopRefundAddr.province != null">
        `province` = #{shopRefundAddr.province},
      </if>
      <if test="shopRefundAddr.cityId != null">
        `city_id` = #{shopRefundAddr.cityId},
      </if>
      <if test="shopRefundAddr.city != null">
        `city` = #{shopRefundAddr.city},
      </if>
      <if test="shopRefundAddr.areaId != null">
        `area_id` = #{shopRefundAddr.areaId},
      </if>
      <if test="shopRefundAddr.area != null">
        `area` = #{shopRefundAddr.area},
      </if>
      <if test="shopRefundAddr.postCode != null">
        `post_code` = #{shopRefundAddr.postCode},
      </if>
      <if test="shopRefundAddr.addr != null">
        `addr` = #{shopRefundAddr.addr},
      </if>
      <if test="shopRefundAddr.lng != null">
        `lng` = #{shopRefundAddr.lng},
      </if>
      <if test="shopRefundAddr.lat != null">
        `lat` = #{shopRefundAddr.lat},
      </if>
    </set>
    where shop_refund_addr_id = #{shopRefundAddr.shopRefundAddrId}
  </update>
  <delete id="deleteById">
    delete from shop_refund_addr where shop_refund_addr_id = #{shopRefundAddrId}
  </delete>

  <select id="listByShopId" resultType="com.tmerclub.cloud.api.multishop.vo.ShopRefundAddrVO">
    select * from shop_refund_addr where shop_id = #{shopId}
    </select>

  <update id="cancelDefaultAddr">
    UPDATE shop_refund_addr SET  `is_default` = 0 WHERE `shop_id` = #{shopId}
  </update>
</mapper>
