<?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.supplier.mapper.WxPayMapper">
    <!-- 支付 -->
    <insert id="pay" parameterType="WxPay">
        insert into bus_pay(uuid,release_uuid,fee_type,mch_id,nonce_str,out_trade_no,total_fee,trade_type,prepay_id,open_id,time_end,jdrq,code_url,body)
        values(#{uuid},#{releaseUuid},#{feeType},#{mchId},#{nonceStr},#{outTradeNo},#{totalFee},#{tradeType},#{prepayId},#{openId},#{timeEnd},CONVERT(VARCHAR(100), SYSDATETIME(), 20),#{codeUrl},#{body})
    </insert>
    <!-- 删除支付信息 -->
    <delete id="deletePay" parameterType="WxPay">
        delete from bus_pay where release_uuid = #{releaseUuid}
    </delete>
    <!-- 修改支付信息 -->
    <update id="payUpdate" parameterType="WxPay">
        update bus_pay
        <set>
            <trim suffixOverrides=",">
                <if test="feeType != null and feeType != ''">
                    fee_type = #{feeType},
                </if>
                <if test="mchId != null and mchId != ''">
                    mch_id = #{mchId},
                </if>
                <if test="nonceStr != null and nonceStr != ''">
                    nonce_str = #{nonceStr},
                </if>
                <if test="outTradeNo != null and outTradeNo != ''">
                    out_trade_no = #{outTradeNo},
                </if>
                <if test="totalFee != null and totalFee != ''">
                    total_fee = #{totalFee},
                </if>
                <if test="tradeType != null and tradeType != ''">
                    trade_type = #{tradeType},
                </if>
                <if test="prepayId != null and prepayId != ''">
                    prepay_id = #{prepayId},
                </if>
                <if test="openId != null and openId != ''">
                    open_id = #{openId},
                </if>
                <if test="timeEnd != null and timeEnd != ''">
                    time_end = #{timeEnd},
                </if>
            </trim>
            where release_uuid = #{releaseUuid}
        </set>
    </update>
    <!-- 查询是否支付 -->
    <select id="getPolling" parameterType="String" resultType="int">
        select count(*) from bus_pay where release_uuid = #{releUuid} and prepay_id is not null
    </select>
    <!-- 存入提现记录 -->
    <insert id="wxCash" parameterType="WxWithdrawal">
        insert into bus_withdrawal(uuid,jdrq,amount,out_trade_no,code)
        values(#{uuid},#{jdrq},#{amount},#{outTradeNo},#{code})
    </insert>
    <!-- 查询指定报价单的支付信息 -->
    <select id="getRefundVal" resultType="WxPay" parameterType="String">
        select * from bus_pay where release_uuid = #{releUuid}
    </select>
    <!-- 查询退款信息 -->
    <select id="getUserRefund" resultType="WxRefund">
        SELECT
            TOP ${limit} a.*,b.offer_name,b.order_no
        FROM
            bus_refund a
            JOIN (select uuid,code,offer_name,jdrq,order_no from bus_supplier_release where code = #{code} GROUP BY uuid,code,offer_name,jdrq,order_no) b on a.release_uuid = b.uuid
        WHERE
            a.id NOT IN
                (
                    select
                        TOP ${page} a.id
                    FROM
                        bus_refund a
                        JOIN (select uuid,code,offer_name,jdrq,order_no from bus_supplier_release where code = #{code} GROUP BY uuid,code,offer_name,jdrq,order_no) b on a.release_uuid = b.uuid
                    <where>
                        <if test="startTime != null and startTime != ''">
                            CONVERT (datetime, b.jdrq) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, b.jdrq) <![CDATA[ <= ]]> #{endTime}
                        </if>
                    </where>
                )
            <if test="startTime != null and startTime != ''">
                and CONVERT (datetime, b.jdrq) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, b.jdrq) <![CDATA[ <= ]]> #{endTime}
            </if>
            and a.refund_state = #{refundState}
        order by a.id
    </select>
    <!-- 查询退款信息 -->
    <select id="getAuditRefund" resultType="WxRefund">
        SELECT
            TOP ${limit} a.*,b.offer_name,b.order_no
        FROM
            bus_refund a
            JOIN (select uuid,code,offer_name,jdrq,order_no from bus_supplier_release where code = #{code} GROUP BY uuid,code,offer_name,jdrq,order_no) b on a.release_uuid = b.uuid
        WHERE
            a.id NOT IN
            (
                select
                    TOP ${page} a.id
                FROM
                    bus_refund a
                    JOIN (select uuid,code,offer_name,jdrq,order_no from bus_supplier_release where code = #{code} GROUP BY uuid,code,offer_name,jdrq,order_no) b on a.release_uuid = b.uuid
                <where>
                    <if test="startTime != null and startTime != ''">
                        CONVERT (datetime, b.jdrq) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, b.jdrq) <![CDATA[ <= ]]> #{endTime}
                    </if>
                </where>
            )
            <if test="startTime != null and startTime != ''">
                and CONVERT (datetime, b.jdrq) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, b.jdrq) <![CDATA[ <= ]]> #{endTime}
            </if>
            and (a.refund_state = '1' or a.refund_state = '2')
            order by a.id
    </select>
    <!-- 查询退款信息条数 -->
    <select id="getUserRefundCount" resultType="int">
        SELECT
            count(*)
        FROM
            bus_refund a
            JOIN (select uuid,code,offer_name,jdrq,order_no from bus_supplier_release where code = #{code} GROUP BY uuid,code,offer_name,jdrq,order_no) b on a.release_uuid = b.uuid
        <trim prefix="WHERE" prefixOverrides="and | or">
            <if test="startTime != null and startTime != ''">
                and CONVERT (datetime, b.jdrq) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, b.jdrq) <![CDATA[ <= ]]> #{endTime}
            </if>
            and a.refund_state = #{refundState}
        </trim>
    </select>
    <!-- 查询退款信息条数 -->
    <select id="getAuditRefundCount" resultType="int">
        SELECT
            count(*)
        FROM
            bus_refund a
            JOIN (select uuid,code,offer_name,jdrq,order_no from bus_supplier_release where code = #{code} GROUP BY uuid,code,offer_name,jdrq,order_no) b on a.release_uuid = b.uuid
        <trim prefix="WHERE" prefixOverrides="and | or">
            <if test="startTime != null and startTime != ''">
                and CONVERT (datetime, b.jdrq) <![CDATA[ >= ]]> #{startTime} and CONVERT(datetime, b.jdrq) <![CDATA[ <= ]]> #{endTime}
            </if>
            and (a.refund_state = '1' or a.refund_state = '2')
        </trim>
    </select>
    <!-- 修改退款状态 -->
    <update id="updateRefundType" parameterType="String">
        update bus_refund set refund_state = #{refundState} where release_uuid = #{releUuid}
    </update>
    <!-- 查询指定参数的退款信息 -->
    <select id="getRefund" resultType="WxRefund">
        select * from bus_refund
        <trim prefix="WHERE" prefixOverrides="and | or">
            <if test="code != null and code !=''">
                and code = #{code}
            </if>
            <if test="releaseUuid != null and releaseUuid !=''">
                and release_uuid = #{releaseUuid}
            </if>
        </trim>
    </select>
    <!-- 供应商余额充值记录 -->
    <insert id="buySupplierBalance" parameterType="WxPay">
        insert into bus_top_up_record(uuid,code,jdrq,total_fee) values(#{uuid},#{code},CONVERT(VARCHAR(100), SYSDATETIME(), 20),#{totalFee})
    </insert>
    <!-- 供应商余额充值 -->
    <update id="updateBalance" parameterType="double">
        UPDATE sys_supplier
        SET balance_of = (SELECT balance_of + ${totalFee} FROM sys_supplier WHERE code = #{code})
        WHERE
            code = #{code}
    </update>
    <!-- 完善充值记录 -->
    <update id="updateBuySupplier" parameterType="WxPay">
        update bus_top_up_record set jdrq = CONVERT(VARCHAR(100), SYSDATETIME(), 20),total_fee=#{totalFee} where code = #{code} and uuid = #{uuid}
    </update>
    <!-- 查询用户是否充值成功 -->
    <select id="getTopUp" resultType="int" parameterType="WxPay">
        select count(*) from bus_top_up_record where uuid = #{uuid} and code = #{code} and total_fee is not null
    </select>
    <!-- 查询超时充值记录 -->
    <select id="getTopUpTimeOut" resultType="WxPay">
        SELECT
            *
        FROM
            bus_top_up_record
        where
            total_fee is null and CONVERT(DATE, jdrq, 20) <![CDATA[ < ]]> CONVERT(DATE, DATEADD(DAY, -1, SYSDATETIME()))
    </select>
    <!-- 删除超时充值记录 -->
    <delete id="delTopOut">
        delete from bus_top_up_record where uuid = #{uuid}
    </delete>
    <!-- 购买记录添加金额 -->
    <update id="updateBuyOfferNum" parameterType="BuyOfferNum">
        update bus_buy_offer_num set pay_amount = #{payAmount} where code = #{code} and uuid = #{uuid}
    </update>
    <!-- 查询用户是否购买次数成功 -->
    <select id="getOfferNum" resultType="int" parameterType="BuyOfferNum">
        select count(*) from bus_buy_offer_num where uuid = #{uuid} and code = #{code} and pay_amount is not null
    </select>
    <!-- 查询超时购买次数记录 -->
    <select id="getOfferNumTimeOut" resultType="BuyOfferNum">
        SELECT
            *
        FROM
            bus_buy_offer_num
        where
            pay_amount is null and CONVERT(DATE, jdrq, 20) <![CDATA[ < ]]> CONVERT(DATE, DATEADD(DAY, -1, SYSDATETIME()))
    </select>
    <!-- 删除超时购买次数记录 -->
    <delete id="delOfferNum">
        delete from bus_buy_offer_num where uuid = #{uuid}
    </delete>
</mapper>