<?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.SupplierBankCardMapper">
  <resultMap id="supplierBankCardMap" type="com.tmerclub.cloud.supplier.model.SupplierBankCard">
    <id column="supplier_bank_card_id" property="supplierBankCardId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="supplier_id" property="supplierId"/>
    <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">
    `supplier_bank_card_id`,`create_time`,`update_time`,`supplier_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="getBySupplierBankCardId" resultType="com.tmerclub.cloud.api.supplier.vo.SupplierBankCardVO">
    select <include refid="Vo_Column_List"/> from supplier_bank_card where supplier_bank_card_id = #{supplierBankCardId}
  </select>
  <select id="listBySupplierId" resultType="com.tmerclub.cloud.api.supplier.vo.SupplierBankCardVO">
    select <include refid="Vo_Column_List"/> from supplier_bank_card where supplier_id = #{supplierId} and status = 1
  </select>
  <select id="countBySupplierId" resultType="java.lang.Integer">
    select count(*) from supplier_bank_card where supplier_id = #{supplierId} and status = 1
  </select>

  <insert id="save" useGeneratedKeys="true" keyProperty="supplierBankCardId">
    insert into supplier_bank_card (`supplier_id`,`bank_name`,`opening_bank`,`recipient_name`,`card_no`,`branch_name`,`is_default`,`bank_card_no`,`bank_card_pro`,`union_bank`,`status`)
    values (#{supplierBankCard.supplierId},#{supplierBankCard.bankName},#{supplierBankCard.openingBank},#{supplierBankCard.recipientName},#{supplierBankCard.cardNo},#{supplierBankCard.branchName},#{supplierBankCard.isDefault},#{supplierBankCard.bankCardNo},#{supplierBankCard.bankCardPro},#{supplierBankCard.unionBank},#{supplierBankCard.status});
  </insert>
  <insert id="insertBatch">
    insert into supplier_bank_card (`supplier_id`,`bank_name`,`opening_bank`,`recipient_name`,`card_no`,`branch_name`,`is_default`,`bank_card_no`,`bank_card_pro`,`union_bank`) values
    <foreach collection="supplierBankCardList" item="supplierBankCard" separator=",">
      (#{supplierId},#{supplierBankCard.bankName},#{supplierBankCard.openingBank},#{supplierBankCard.recipientName},#{supplierBankCard.cardNo},#{supplierBankCard.branchName},#{supplierBankCard.isDefault},#{supplierBankCard.bankCardNo},#{supplierBankCard.bankCardPro},#{supplierBankCard.unionBank})
    </foreach>
  </insert>

  <update id="update">
    update supplier_bank_card
    <set>
      <if test="supplierBankCard.supplierId != null">
        `supplier_id` = #{supplierBankCard.supplierId},
      </if>
      <if test="supplierBankCard.bankName != null">
        `bank_name` = #{supplierBankCard.bankName},
      </if>
      <if test="supplierBankCard.openingBank != null">
        `opening_bank` = #{supplierBankCard.openingBank},
      </if>
      <if test="supplierBankCard.recipientName != null">
        `recipient_name` = #{supplierBankCard.recipientName},
      </if>
      <if test="supplierBankCard.cardNo != null">
        `card_no` = #{supplierBankCard.cardNo},
      </if>
      <if test="supplierBankCard.branchName != null">
        `branch_name` = #{supplierBankCard.branchName},
      </if>
      <if test="supplierBankCard.isDefault != null">
        `is_default` = #{supplierBankCard.isDefault},
      </if>
      <if test="supplierBankCard.status != null">
        `status` = #{supplierBankCard.status},
      </if>
      <if test="supplierBankCard.bankCardNo != null">
        `bank_card_no` = #{supplierBankCard.bankCardNo},
      </if>
      <if test="supplierBankCard.bankCardPro != null">
        `bank_card_pro` = #{supplierBankCard.bankCardPro},
      </if>
      <if test="supplierBankCard.unionBank != null">
        `union_bank` = #{supplierBankCard.unionBank},
      </if>
    </set>
    where supplier_bank_card_id = #{supplierBankCard.supplierBankCardId}
  </update>
  <update id="setPrimary">
    UPDATE supplier_bank_card
    SET is_default =
        IF( supplier_bank_card_id = #{supplierBankCardId}, 1, 0 )
    WHERE
      supplier_id = #{supplierId}
  </update>
  <update id="setNotPrimary">
    UPDATE supplier_bank_card
    SET is_default = 0
    WHERE supplier_id = #{supplierId} and supplier_bank_card_id = #{supplierBankCardId}
  </update>
  <update id="deleteById">
    UPDATE supplier_bank_card
    SET status = -1
    WHERE supplier_bank_card_id = #{supplierBankCardId}
  </update>
  <delete id="deleteBySupplierId">
    delete from supplier_bank_card where supplier_id = #{supplierId}
  </delete>

</mapper>
