<?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.ShopWithdrawCashMapper">
  <resultMap id="shopWithdrawCashMap" type="com.tmerclub.cloud.multishop.model.ShopWithdrawCash">
    <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="shop_id" property="shopId"/>
    <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="shop_remarks" property="shopRemarks"/>
    <result column="paying_account" property="payingAccount"/>
    <result column="paying_card_no" property="payingCardNo"/>
    <result column="paying_time" property="payingTime"/>
    <result column="shop_bank_card_id" property="shopBankCardId"/>
    <result column="allinpay_status" property="allinpayStatus"/>
    <result column="biz_order_no" property="bizOrderNo"/>
    <result column="pay_fail_message" property="payFailMessage"/>
  </resultMap>
  <resultMap id="shopWithdrawCashVOMap" type="com.tmerclub.cloud.multishop.vo.ShopWithdrawCashVO">
    <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="shop_id" property="shopId"/>
    <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="shop_remarks" property="shopRemarks"/>
    <result column="paying_account" property="payingAccount"/>
    <result column="paying_card_no" property="payingCardNo"/>
    <result column="paying_time" property="payingTime"/>
    <result column="shop_bank_card_id" property="shopBankCardId"/>
    <result column="shop_name" property="shopName"/>
    <result column="shop_logo" property="shopLogo"/>
    <result column="allinpay_status" property="allinpayStatus"/>
    <result column="biz_order_no" property="bizOrderNo"/>
    <result column="pay_fail_message" property="payFailMessage"/>
    <collection property="shopBankCardVO" ofType="com.tmerclub.cloud.api.multishop.vo.ShopBankCardVO">
      <result column="shop_bank_card_id" property="shopBankCardId"/>
      <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`,`shop_id`,`auditor_id`,`amount`,`auditing_time`,`status`,`remarks`,`shop_remarks`,`paying_account`,`paying_card_no`,`paying_time`,`shop_bank_card_id`,`allinpay_status`,`biz_order_no`,`pay_fail_message`
  </sql>
  <select id="getByBizOrderNo" resultMap="shopWithdrawCashMap">
    select <include refid="Vo_Column_List"/> from shop_withdraw_cash where biz_order_no = #{bizOrderNo}
  </select>
  <select id="countByShopBandCardIdAndStatus" resultType="java.lang.Integer">
    select count(*) from shop_withdraw_cash where shop_bank_card_id = #{shopBankCardId} and status = #{status}
  </select>
  <select id="listByParam" resultType="com.tmerclub.cloud.multishop.vo.ShopWithdrawCashVO">
    select swc.*, sd.shop_name, sd.shop_logo
    from shop_withdraw_cash swc
    join shop_detail sd on sd.shop_id = swc.shop_id
    <where>
      swc.pay_sys_type = #{shopWithdrawCash.paySysType}
      <if test="shopWithdrawCash.shopName != null and shopWithdrawCash.shopName != ''">
        and trim(replace(sd.shop_name, ' ', '')) like trim(replace(concat('%', #{shopWithdrawCash.shopName}, '%'), ' ', ''))
      </if>
      <if test="shopWithdrawCash.shopId != null">
        and swc.`shop_id` = #{shopWithdrawCash.shopId}
      </if>
      <if test="shopWithdrawCash.status != null">
        and swc.`status` = #{shopWithdrawCash.status}
      </if>
      <if test="shopWithdrawCash.allinpayStatus != null">
        and swc.`allinpay_status` = #{shopWithdrawCash.allinpayStatus}
      </if>
    </where>
    ORDER BY swc.update_time DESC
  </select>
  <select id="getDetailByCashId" resultMap="shopWithdrawCashVOMap">
    select swc.*, sd.shop_name, sbc.*
    from shop_withdraw_cash swc
    left join shop_detail sd on sd.shop_id = swc.shop_id
    left join shop_bank_card sbc on swc.shop_bank_card_id = sbc.shop_bank_card_id
    where swc.cash_id = #{cashId}
  </select>
    <select id="getWithdrawCashCount" resultType="java.lang.Integer">
      select count(*) from shop_withdraw_cash where shop_id = #{shopWithdrawCash.shopId} and create_time&gt;=#{startTimes} and create_time&lt;=#{endTimes} and pay_sys_type = #{shopWithdrawCash.paySysType}
    </select>
    <insert id="save">
    insert into shop_withdraw_cash (`pay_sys_type`,`shop_id`,`auditor_id`,`amount`,`auditing_time`,`status`,`remarks`,`shop_remarks`,`paying_account`,`paying_card_no`,`paying_time`,`shop_bank_card_id`,`allinpay_status`,`biz_order_no`)
    values (#{shopWithdrawCash.paySysType},#{shopWithdrawCash.shopId},#{shopWithdrawCash.auditorId},#{shopWithdrawCash.amount},#{shopWithdrawCash.auditingTime},#{shopWithdrawCash.status},#{shopWithdrawCash.remarks},#{shopWithdrawCash.shopRemarks},#{shopWithdrawCash.payingAccount},#{shopWithdrawCash.payingCardNo},#{shopWithdrawCash.payingTime},#{shopWithdrawCash.shopBankCardId},#{shopWithdrawCash.allinpayStatus},#{shopWithdrawCash.bizOrderNo});
  </insert>
  <update id="update">
    update shop_withdraw_cash
    <set>
      <if test="shopWithdrawCash.shopId != null">
        `shop_id` = #{shopWithdrawCash.shopId},
      </if>
      <if test="shopWithdrawCash.auditorId != null">
        `auditor_id` = #{shopWithdrawCash.auditorId},
      </if>
      <if test="shopWithdrawCash.amount != null">
        `amount` = #{shopWithdrawCash.amount},
      </if>
      <if test="shopWithdrawCash.auditingTime != null">
        `auditing_time` = #{shopWithdrawCash.auditingTime},
      </if>
      <if test="shopWithdrawCash.status != null">
        `status` = #{shopWithdrawCash.status},
      </if>
      <if test="shopWithdrawCash.remarks != null">
        `remarks` = #{shopWithdrawCash.remarks},
      </if>
      <if test="shopWithdrawCash.shopRemarks != null">
        `shop_remarks` = #{shopWithdrawCash.shopRemarks},
      </if>
      <if test="shopWithdrawCash.payingAccount != null">
        `paying_account` = #{shopWithdrawCash.payingAccount},
      </if>
      <if test="shopWithdrawCash.payingCardNo != null">
        `paying_card_no` = #{shopWithdrawCash.payingCardNo},
      </if>
      <if test="shopWithdrawCash.payingTime != null">
        `paying_time` = #{shopWithdrawCash.payingTime},
      </if>
      <if test="shopWithdrawCash.shopBankCardId != null">
        `shop_bank_card_id` = #{shopWithdrawCash.shopBankCardId},
      </if>
      <if test="shopWithdrawCash.allinpayStatus != null">
        `allinpay_status` = #{shopWithdrawCash.allinpayStatus},
      </if>
      <if test="shopWithdrawCash.payFailMessage != null">
        `pay_fail_message` = #{shopWithdrawCash.payFailMessage},
      </if>
    </set>
    where cash_id = #{shopWithdrawCash.cashId}
  </update>
  <update id="updateByIdAndStatus">
    update shop_withdraw_cash
    <set>
      <if test="shopWithdrawCash.shopId != null">
        `shop_id` = #{shopWithdrawCash.shopId},
      </if>
      <if test="shopWithdrawCash.auditorId != null">
        `auditor_id` = #{shopWithdrawCash.auditorId},
      </if>
      <if test="shopWithdrawCash.amount != null">
        `amount` = #{shopWithdrawCash.amount},
      </if>
      <if test="shopWithdrawCash.auditingTime != null">
        `auditing_time` = #{shopWithdrawCash.auditingTime},
      </if>
      <if test="shopWithdrawCash.status != null">
        `status` = #{shopWithdrawCash.status},
      </if>
      <if test="shopWithdrawCash.remarks != null">
        `remarks` = #{shopWithdrawCash.remarks},
      </if>
      <if test="shopWithdrawCash.shopRemarks != null">
        `shop_remarks` = #{shopWithdrawCash.shopRemarks},
      </if>
      <if test="shopWithdrawCash.payingAccount != null">
        `paying_account` = #{shopWithdrawCash.payingAccount},
      </if>
      <if test="shopWithdrawCash.payingCardNo != null">
        `paying_card_no` = #{shopWithdrawCash.payingCardNo},
      </if>
      <if test="shopWithdrawCash.payingTime != null">
        `paying_time` = #{shopWithdrawCash.payingTime},
      </if>
      <if test="shopWithdrawCash.shopBankCardId != null">
        `shop_bank_card_id` = #{shopWithdrawCash.shopBankCardId},
      </if>
    </set>
    where cash_id = #{cashId} and status = #{status}
  </update>
</mapper>
