<?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.ShopBankCardMapper">
  <resultMap id="shopBankCardMap" type="com.tmerclub.cloud.multishop.model.ShopBankCard">
    <id column="shop_bank_card_id" property="shopBankCardId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="bank_name" property="bankName"/>
    <result column="opening_bank" property="openingBank"/>
    <result column="recipient_name" property="recipientName"/>
    <result column="card_no" property="cardNo"/>
    <result column="branch_name" property="branchName"/>
    <result column="is_default" property="isDefault"/>
    <result column="status" property="status"/>
    <result column="bank_card_no" property="bankCardNo"/>
    <result column="bank_card_pro" property="bankCardPro"/>
    <result column="union_bank" property="unionBank"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `shop_bank_card_id`,`create_time`,`update_time`,`shop_id`,`bank_name`,`opening_bank`,`recipient_name`,`card_no`,
    `branch_name`,`is_default`,`bank_card_no`,`bank_card_pro`,`union_bank`,`status`
  </sql>
  <select id="getByShopBankCardId" resultType="com.tmerclub.cloud.api.multishop.vo.ShopBankCardVO">
    select <include refid="Vo_Column_List"/> from shop_bank_card where shop_bank_card_id = #{shopBankCardId}
  </select>
  <select id="listByShopId" resultType="com.tmerclub.cloud.api.multishop.vo.ShopBankCardVO">
    select <include refid="Vo_Column_List"/> from shop_bank_card where shop_id = #{shopId} and status = 1
  </select>
  <select id="countByShopId" resultType="java.lang.Integer">
    select count(*) from shop_bank_card where shop_id = #{shopId} and status = 1
  </select>

  <insert id="save" useGeneratedKeys="true" keyProperty="shopBankCardId">
    insert into shop_bank_card (`shop_id`,`bank_name`,`opening_bank`,`recipient_name`,`card_no`,`branch_name`,`is_default`,`bank_card_no`,`bank_card_pro`,`union_bank`,`status`)
    values (#{shopBankCard.shopId},#{shopBankCard.bankName},#{shopBankCard.openingBank},#{shopBankCard.recipientName},#{shopBankCard.cardNo},#{shopBankCard.branchName},#{shopBankCard.isDefault},#{shopBankCard.bankCardNo},#{shopBankCard.bankCardPro},#{shopBankCard.unionBank},#{shopBankCard.status});
  </insert>
  <insert id="insertBatch">
    insert into shop_bank_card (`shop_id`,`bank_name`,`opening_bank`,`recipient_name`,`card_no`,`branch_name`,`is_default`,`bank_card_no`,`bank_card_pro`,`union_bank`) values
    <foreach collection="shopBankCardList" item="shopBankCard" separator=",">
      (#{shopId},#{shopBankCard.bankName},#{shopBankCard.openingBank},#{shopBankCard.recipientName},#{shopBankCard.cardNo},#{shopBankCard.branchName},#{shopBankCard.isDefault},#{shopBankCard.bankCardNo},#{shopBankCard.bankCardPro},#{shopBankCard.unionBank})
    </foreach>
  </insert>

  <update id="update">
    update shop_bank_card
    <set>
      <if test="shopBankCard.shopId != null">
        `shop_id` = #{shopBankCard.shopId},
      </if>
      <if test="shopBankCard.bankName != null">
        `bank_name` = #{shopBankCard.bankName},
      </if>
      <if test="shopBankCard.openingBank != null">
        `opening_bank` = #{shopBankCard.openingBank},
      </if>
      <if test="shopBankCard.recipientName != null">
        `recipient_name` = #{shopBankCard.recipientName},
      </if>
      <if test="shopBankCard.cardNo != null">
        `card_no` = #{shopBankCard.cardNo},
      </if>
      <if test="shopBankCard.branchName != null">
        `branch_name` = #{shopBankCard.branchName},
      </if>
      <if test="shopBankCard.isDefault != null">
        `is_default` = #{shopBankCard.isDefault},
      </if>
      <if test="shopBankCard.status != null">
        `status` = #{shopBankCard.status},
      </if>
      <if test="shopBankCard.bankCardNo != null">
        `bank_card_no` = #{shopBankCard.bankCardNo},
      </if>
      <if test="shopBankCard.bankCardPro != null">
        `bank_card_pro` = #{shopBankCard.bankCardPro},
      </if>
      <if test="shopBankCard.unionBank != null">
        `union_bank` = #{shopBankCard.unionBank},
      </if>
    </set>
    where shop_bank_card_id = #{shopBankCard.shopBankCardId}
  </update>
  <update id="setPrimary">
    UPDATE shop_bank_card
    SET is_default =
        IF( shop_bank_card_id = #{shopBankCardId}, 1, 0 )
    WHERE
      shop_id = #{shopId}
  </update>
  <update id="setNotPrimary">
    UPDATE shop_bank_card
    SET is_default = 0
    WHERE shop_id = #{shopId} and shop_bank_card_id = #{shopBankCardId}
  </update>
  <update id="deleteById">
    UPDATE shop_bank_card
    SET status = -1
    WHERE shop_bank_card_id = #{shopBankCardId}
  </update>
  <delete id="deleteByShopId">
    delete from shop_bank_card where shop_id = #{shopId}
  </delete>

</mapper>
