<?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.SupplierWithdrawCashMapper">
  <resultMap id="supplierWithdrawCashMap" type="com.tmerclub.cloud.supplier.model.SupplierWithdrawCash">
    <id column="cash_id" property="cashId" />
    <result column="pay_sys_type" property="paySysType"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="supplier_id" property="supplierId"/>
    <result column="auditor_id" property="auditorId"/>
    <result column="amount" property="amount"/>
    <result column="auditing_time" property="auditingTime"/>
    <result column="status" property="status"/>
    <result column="remarks" property="remarks"/>
    <result column="supplier_remarks" property="supplierRemarks"/>
    <result column="paying_account" property="payingAccount"/>
    <result column="paying_card_no" property="payingCardNo"/>
    <result column="paying_time" property="payingTime"/>
    <result column="supplier_bank_card_id" property="supplierBankCardId"/>
    <result column="allinpay_status" property="allinpayStatus"/>
    <result column="biz_order_no" property="bizOrderNo"/>
    <result column="pay_fail_message" property="payFailMessage"/>
  </resultMap>
  <resultMap id="supplierWithdrawCashVOMap" type="com.tmerclub.cloud.supplier.vo.SupplierWithdrawCashVO">
    <id column="cash_id" property="cashId" />
    <result column="pay_sys_type" property="paySysType"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="supplier_id" property="supplierId"/>
    <result column="auditor_id" property="auditorId"/>
    <result column="amount" property="amount"/>
    <result column="auditing_time" property="auditingTime"/>
    <result column="status" property="status"/>
    <result column="remarks" property="remarks"/>
    <result column="supplier_remarks" property="supplierRemarks"/>
    <result column="paying_account" property="payingAccount"/>
    <result column="paying_card_no" property="payingCardNo"/>
    <result column="paying_time" property="payingTime"/>
    <result column="supplier_bank_card_id" property="supplierBankCardId"/>
    <result column="supplier_name" property="supplierName"/>
    <result column="supplier_logo" property="supplierLogo"/>
    <collection property="supplierBankCardVO" ofType="com.tmerclub.cloud.api.supplier.vo.SupplierBankCardVO">
      <result column="supplier_bank_card_id" property="supplierBankCardId"/>
      <result column="bank_name" property="bankName"/>
      <result column="recipient_name" property="recipientName"/>
      <result column="card_no" property="cardNo"/>
      <result column="branch_name" property="branchName"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `cash_id`,`pay_sys_type`,`create_time`,`update_time`,`supplier_id`,`auditor_id`,`amount`,`auditing_time`,`status`,`remarks`,`supplier_remarks`,`paying_account`,`paying_card_no`,`paying_time`,`supplier_bank_card_id`,`allinpay_status`,`biz_order_no`,`pay_fail_message`
  </sql>
  <select id="getByBizOrderNo" resultMap="supplierWithdrawCashMap">
    select <include refid="Vo_Column_List"/> from supplier_withdraw_cash where biz_order_no = #{bizOrderNo}
  </select>
  <select id="countBySupplierBandCardIdAndStatus" resultType="java.lang.Integer">
    select count(*) from supplier_withdraw_cash where supplier_bank_card_id = #{supplierBankCardId} and status = #{status}
  </select>
  <select id="listByParam" resultType="com.tmerclub.cloud.supplier.vo.SupplierWithdrawCashVO">
    select swc.*, sd.supplier_name, sd.supplier_logo
    from supplier_withdraw_cash swc
    join supplier_detail sd on sd.supplier_id = swc.supplier_id
    <where>
      swc.pay_sys_type = #{supplierWithdrawCash.paySysType}
      <if test="supplierWithdrawCash.supplierName != null and supplierWithdrawCash.supplierName != ''">
        and sd.supplier_name like trim(replace(concat('%', #{supplierWithdrawCash.supplierName}, '%'), ' ', ''))
      </if>
      <if test="supplierWithdrawCash.supplierId != null">
        and swc.`supplier_id` = #{supplierWithdrawCash.supplierId}
      </if>
      <if test="supplierWithdrawCash.status != null">
        and swc.`status` = #{supplierWithdrawCash.status}
      </if>
      <if test="supplierWithdrawCash.allinpayStatus != null">
        and swc.`allinpay_status` = #{supplierWithdrawCash.allinpayStatus}
      </if>
    </where>
    ORDER BY swc.update_time DESC
  </select>
  <select id="getDetailByCashId" resultMap="supplierWithdrawCashVOMap">
    select swc.*, sd.supplier_name, sbc.*
    from supplier_withdraw_cash swc
    left join supplier_detail sd on sd.supplier_id = swc.supplier_id
    left join supplier_bank_card sbc on swc.supplier_bank_card_id = sbc.supplier_bank_card_id
    where swc.cash_id = #{cashId}
  </select>
    <select id="getWithdrawCashCount" resultType="java.lang.Integer">
      select count(*) from supplier_withdraw_cash where supplier_id = #{supplierWithdrawCash.supplierId} and create_time&gt;=#{startTimes} and create_time&lt;=#{endTimes} and pay_sys_type = #{supplierWithdrawCash.paySysType}
    </select>
    <insert id="save">
    insert into supplier_withdraw_cash (`pay_sys_type`,`supplier_id`,`auditor_id`,`amount`,`auditing_time`,`status`,`remarks`,`supplier_remarks`,`paying_account`,`paying_card_no`,`paying_time`,`supplier_bank_card_id`,`allinpay_status`,`biz_order_no`)
    values (#{supplierWithdrawCash.paySysType},#{supplierWithdrawCash.supplierId},#{supplierWithdrawCash.auditorId},#{supplierWithdrawCash.amount},#{supplierWithdrawCash.auditingTime},#{supplierWithdrawCash.status},#{supplierWithdrawCash.remarks},#{supplierWithdrawCash.supplierRemarks},#{supplierWithdrawCash.payingAccount},#{supplierWithdrawCash.payingCardNo},#{supplierWithdrawCash.payingTime},#{supplierWithdrawCash.supplierBankCardId},#{supplierWithdrawCash.allinpayStatus},#{supplierWithdrawCash.bizOrderNo});
  </insert>
  <update id="update">
    update supplier_withdraw_cash
    <set>
      <if test="supplierWithdrawCash.supplierId != null">
        `supplier_id` = #{supplierWithdrawCash.supplierId},
      </if>
      <if test="supplierWithdrawCash.auditorId != null">
        `auditor_id` = #{supplierWithdrawCash.auditorId},
      </if>
      <if test="supplierWithdrawCash.amount != null">
        `amount` = #{supplierWithdrawCash.amount},
      </if>
      <if test="supplierWithdrawCash.auditingTime != null">
        `auditing_time` = #{supplierWithdrawCash.auditingTime},
      </if>
      <if test="supplierWithdrawCash.status != null">
        `status` = #{supplierWithdrawCash.status},
      </if>
      <if test="supplierWithdrawCash.remarks != null">
        `remarks` = #{supplierWithdrawCash.remarks},
      </if>
      <if test="supplierWithdrawCash.supplierRemarks != null">
        `supplier_remarks` = #{supplierWithdrawCash.supplierRemarks},
      </if>
      <if test="supplierWithdrawCash.payingAccount != null">
        `paying_account` = #{supplierWithdrawCash.payingAccount},
      </if>
      <if test="supplierWithdrawCash.payingCardNo != null">
        `paying_card_no` = #{supplierWithdrawCash.payingCardNo},
      </if>
      <if test="supplierWithdrawCash.payingTime != null">
        `paying_time` = #{supplierWithdrawCash.payingTime},
      </if>
      <if test="supplierWithdrawCash.supplierBankCardId != null">
        `supplier_bank_card_id` = #{supplierWithdrawCash.supplierBankCardId},
      </if>
      <if test="supplierWithdrawCash.allinpayStatus != null">
        `allinpay_status` = #{supplierWithdrawCash.allinpayStatus},
      </if>
      <if test="supplierWithdrawCash.payFailMessage != null">
        `pay_fail_message` = #{supplierWithdrawCash.payFailMessage},
      </if>
    </set>
    where cash_id = #{supplierWithdrawCash.cashId}
  </update>
  <update id="updateByIdAndStatus">
    update supplier_withdraw_cash
    <set>
      <if test="supplierWithdrawCash.supplierId != null">
        `supplier_id` = #{supplierWithdrawCash.supplierId},
      </if>
      <if test="supplierWithdrawCash.auditorId != null">
        `auditor_id` = #{supplierWithdrawCash.auditorId},
      </if>
      <if test="supplierWithdrawCash.amount != null">
        `amount` = #{supplierWithdrawCash.amount},
      </if>
      <if test="supplierWithdrawCash.auditingTime != null">
        `auditing_time` = #{supplierWithdrawCash.auditingTime},
      </if>
      <if test="supplierWithdrawCash.status != null">
        `status` = #{supplierWithdrawCash.status},
      </if>
      <if test="supplierWithdrawCash.remarks != null">
        `remarks` = #{supplierWithdrawCash.remarks},
      </if>
      <if test="supplierWithdrawCash.supplierRemarks != null">
        `supplier_remarks` = #{supplierWithdrawCash.supplierRemarks},
      </if>
      <if test="supplierWithdrawCash.payingAccount != null">
        `paying_account` = #{supplierWithdrawCash.payingAccount},
      </if>
      <if test="supplierWithdrawCash.payingCardNo != null">
        `paying_card_no` = #{supplierWithdrawCash.payingCardNo},
      </if>
      <if test="supplierWithdrawCash.payingTime != null">
        `paying_time` = #{supplierWithdrawCash.payingTime},
      </if>
      <if test="supplierWithdrawCash.supplierBankCardId != null">
        `supplier_bank_card_id` = #{supplierWithdrawCash.supplierBankCardId},
      </if>
    </set>
    where cash_id = #{cashId} and status = #{status}
  </update>
</mapper>
