<?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="cn.pluss.platform.mapper.MerchantRefundOrderMapper">



    <select id="queryRefundOrderJoinPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select  count(1) from (SELECT
	    ro.merchantCode,
	    ro.alias
        FROM
        tb_pluss_merchant_refund_order ro
        LEFT JOIN tb_pluss_merchant_store ms ON ro.merchantCode = ms.merchantCode
        LEFT JOIN tb_pluss_merchant_transfer mt ON ro.merchantCode = mt.merchantCode
        where  ms.isMarket = 1 AND ro.`status` = 1
        GROUP BY ro.merchantCode) a
        <where>
            <if test="alias != null and alias != ''">
                and alias LIKE CONCAT('%',#{alias},'%')
            </if>
        </where>
    </select>

    <select id="queryRefundOrderJoinPage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.MerchantRefundOrder">
        SELECT * FROM (
        SELECT
            ro.merchantCode,
            ro.alias,
            IFNULL( sum( ro.payAmt ), 0.00 ) AS refundAmtCount,
            count( ro.merchantCode ) AS refunCount,
            IFNUll( SUM( mt.amount ), 0.00 ) AS transferAmtCount
        FROM
            tb_pluss_merchant_refund_order ro
            LEFT JOIN tb_pluss_merchant_store ms ON ro.merchantCode = ms.merchantCode
            LEFT JOIN tb_pluss_merchant_transfer mt ON ro.merchantCode = mt.merchantCode
        WHERE
            ms.isMarket = 1
            AND ro.`status` = 1
        GROUP BY
            ro.merchantCode
        ) a
        <where>
            <if test="alias != null and alias != ''">
               and alias LIKE CONCAT('%',#{alias},'%')
            </if>
        </where>

        ORDER BY refundAmtCount desc
        limit #{pageSize}  offset  #{offset}
    </select>

    <select id="pageDate" resultType="cn.pluss.platform.entity.MerchantRefundOrder">
        select *
        from tb_pluss_merchant_refund_order
        <where>
            <if test="refundOrder.orderNumber != null and refundOrder.orderNumber != '' ">
                and orderNumber=#{refundOrder.orderNumber}
            </if>
            <if test="refundOrder.channelNo != null and refundOrder.channelNo != ''">
                and channelNo=#{refundOrder.channelNo}
            </if>
            <if test="refundOrder.refundNo != null and refundOrder.refundNo !=''">
                and refundNo=#{refundOrder.refundNo}
            </if>
            <if test="refundOrder.orderType != null and refundOrder.orderType != ''">
                and orderType=#{refundOrder.orderType}
            </if>
            <if test="refundOrder.payTypeCodeList != null">
                and (
                <foreach collection="refundOrder.payTypeCodeList" item="item" index="index" separator="or">
                    payTypeCode = #{item}
                </foreach>
                )
            </if>
            <if test="refundOrder.alias != null and refundOrder.alias != '' ">
                and alias = #{refundOrder.alias}
            </if>
            <if test="refundOrder.merchantCode != null and refundOrder.merchantCode != ''">
                and merchantCode IN (SELECT merchantCode
                FROM tb_pluss_merchant_base_info
                WHERE bindingCode = (SELECT bindingCode
                FROM tb_pluss_merchant_base_info
                where merchantCode = #{refundOrder.merchantCode}))
            </if>
        </where>
        order by payTime desc
    </select>

    <select id="getPlatformRefundData" resultType="java.util.Map">
        SELECT
            IFNULL(SUM(IFNULL(refundAmt,payAmt)),0) as toDayRefundCount,
            IFNULL(SUM(IF(payTypeCode = 'wechatPay' or payTypeCode = 'sxfPay1',refundAmt,0)),0) as toDayRefundWxCount,
            IFNULL(SUM(IF(payTypeCode = 'aliPay' or payTypeCode = 'sxfPay0',refundAmt,0)),0) as toDayRefundAliCount,
            IFNULL(SUM(IF(payTypeCode = 'bank' or payTypeCode = 'sxfPay2',refundAmt,0)),0) as toDayRefundUnionCount
        FROM tb_pluss_merchant_refund_order
        WHERE `status` = 1 AND date(createTime) = curdate()
    </select>

    <select id="selectOrderByOrderNumber" resultType="java.math.BigDecimal">

        select IFNULL(sum(payAmt),0) from tb_pluss_merchant_refund_order where orderNumber=#{orderNumber}
    </select>
</mapper>
