<?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.BillStatisticsMapper">

    <select id="selectBillStatistics" resultType="cn.pluss.platform.vo.BillStatisticsVO">
        SELECT * FROM (
          SELECT
              IFNULL( COUNT( IF ( `status` = '1', 1, NULL ) ), 0) allOrderCount,
              IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxOrderCount,
              IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliOrderCount,
              IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankOrderCount,
              IFNULL( COUNT( IF ( `status` = '2', 1, NULL ) ), 0) allRefundOrderCount,
              IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxRefundOrderCount,
              IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliRefundOrderCount,
              IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankRefundOrderCount,
              IFNULL( SUM( IF ( `status` = '1', consumeFee, - consumeFee ) ), 0.00 ) allInMoney,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ), 0.00 ) wxInMoney,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ), 0.00 ) aliInMoney,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ), 0.00 ) bankInMoney,
              IFNULL( SUM( IF ( `status` = 1, marketAmt, - marketAmt ) ), 0.00 ) allSubsidy,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ), 0.00 ) wxSubsidy,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', marketAmt, - marketAmt ), 0.00 ) ), 0.00 ) aliSubsidy,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ), 0.00 ) bankSubsidy,
              IFNULL( SUM( IF ( `status` = 1, enterFee, - enterFee ) ), 0.00 ) allInAccount,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ), 0.00 ) wxInAccount,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1',enterFee, - enterFee  ), 0.00 ) ), 0.00 ) aliInAccount,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ), 0.00 ) bankInAccount,
              #{startDate} startDate,
              #{endDate} endDate,
              "汇总" billName
          FROM
              tb_pluss_merchant_order
          WHERE
              `status` IN ( '1', '2' )
            AND merchantCode = #{merchantCode}
            AND createDt BETWEEN #{startDate} AND #{endDate}
          UNION
          SELECT
              IFNULL( COUNT( IF ( `status` = '1', 1, NULL ) ), 0) allOrderCount,
              IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxOrderCount,
              IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliOrderCount,
              IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankOrderCount,
              IFNULL( COUNT( IF ( `status` = '2', 1, NULL ) ), 0) allRefundOrderCount,
              IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxRefundOrderCount,
              IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliRefundOrderCount,
              IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankRefundOrderCount,
              IFNULL( SUM( IF ( `status` = '1', consumeFee, - consumeFee ) ), 0.00 ) allInMoney,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ), 0.00 ) wxInMoney,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ), 0.00 ) aliInMoney,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ), 0.00 ) bankInMoney,
              IFNULL( SUM( IF ( `status` = 1, marketAmt, - marketAmt ) ), 0.00 ) allSubsidy,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ), 0.00 ) wxSubsidy,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', marketAmt, - marketAmt ), 0.00 ) ), 0.00 ) aliSubsidy,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ), 0.00 ) bankSubsidy,
              IFNULL( SUM( IF ( `status` = 1, enterFee, - enterFee ) ), 0.00 ) allInAccount,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ), 0.00 ) wxInAccount,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1',enterFee, - enterFee  ), 0.00 ) ), 0.00 ) aliInAccount,
              IFNULL( SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ), 0.00 ) bankInAccount,
              DATE_FORMAT(mo.createDt, '%Y-%m-%d 00:00:00') startDate,
              DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59') endDate,
              DATE_FORMAT(mo.createDt, '%Y-%m-%d') billName
          FROM tb_pluss_merchant_order mo
          WHERE mo.`status` IN ('1', '2')
            AND merchantCode = #{merchantCode}
            AND createDt BETWEEN #{startDate} AND #{endDate}
          GROUP BY DATE_FORMAT(mo.createDt, '%Y-%m-%d'), DATE_FORMAT(mo.createDt, '%Y-%m-%d 00:00:00'), DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59')
        ) t, (
            SELECT CONCAT(mc.channelName, '备付金') merchantChannelType
            FROM tb_pluss_merchant_channel mc
            LEFT JOIN tb_pluss_merchant_channel_status mcs ON mc.id = mcs.channel
            WHERE mcs.merchantCode = #{merchantCode}
        ) t2
        ORDER BY t.billName DESC;
    </select>

    <select id="selectBillList" resultType="cn.pluss.platform.vo.MerchantOrderExportVO">
        SELECT mo.orderNumber,
               mo.thirdTransNo thirdOrderNo,
               mro.refundNo,
               CASE mo.payTypeCode
                   WHEN "sxfPay1" THEN "微信"
                   WHEN "wechatPay" THEN "微信"
                   WHEN "sxfPay0" THEN "支付宝"
                   WHEN "aliPay" THEN "支付宝"
                   WHEN "sxfPay2" THEN "银联"
                   WHEN "bank" THEN "银联"
                END payType,
               CASE mo.orderType
                   WHEN 1 THEN '反扫收款'
                   WHEN 2 THEN '收款码'
                   WHEN 3 THEN '会员充值'
                   WHEN 4 THEN 'POS刷卡'
                   WHEN 5 THEN '反扫会员充值'
                   WHEN 6 THEN '扫码王'
                   WHEN 9 THEN '收款码'
                   WHEN 10 THEN '线上店' END 'orderTypeDesc',
               consumeFee,
               IF(mro.refundNo IS NULL, 0, consumeFee) refundFee,
               0 subsidy,
               mo.marketAmt,
               IF(mro.refundNo IS NOT NULL, 0, consumeFee - enterFee) serviceCharge,
               IF(mro.refundNo IS NOT NULL, 0, enterFee) enterFee,
               mo.staffName,
               mo.remark,
               mo.createDt,
               mo.transDt
        FROM tb_pluss_merchant_order mo
                 LEFT JOIN tb_pluss_merchant_refund_order mro ON mo.orderNumber = mro.orderNumber
        WHERE mo.merchantCode = #{merchantCode}
          AND mo.`status` IN ( '1' )
          AND mo.createDt BETWEEN #{startDate}
            AND #{endDate};
    </select>

    <select id="selectMemberBillList" resultType="cn.pluss.platform.vo.MemberOrderVO">
        SELECT mo.orderNumber,
               mo2.thirdTransNo thirdOrderNo,
               mm.nickName,
               mm.phone,
               IF (mo.orderType = 0, NULL,
                   IF (mo.rechargeType = 1, "现金",
                       CASE mo2.payTypeCode
                           WHEN "sxfPay1" THEN "微信"
                           WHEN "wechatPay" THEN "微信"
                           WHEN "sxfPay0" THEN "支付宝"
                           WHEN "aliPay" THEN "支付宝"
                           WHEN "sxfPay2" THEN "银联"
                           WHEN "bank" THEN "银联"
                           END
                   ))
                     payType,
               IF (mo.orderType = 0, "会员消费", "会员充值") orderType,
               mo.orderFee,
               IF (mo.orderType = 0, 0.00,
                   IF (mo.rechargeType = 1, 0.00, mo2.consumeFee - mo2.enterFee)
                   ) serviceRecharge,
               IF (mo.orderType = 0, 0.00,
                   IF (mo.rechargeType = 1, mo.orderFee, 0.00)
                   ) inCashMoney,
               IF (mo.orderType = 0, 0.00,
                   IF (mo.rechargeType != 1, mo2.enterFee, 0.00)
                   ) cardMoney,
               IF(mo.orderType = 0, mo.orderFee, 0.00) consumeFee,
               IFNULL(mmr.giveMoney, IFNULL(mmc.giveMoney, 0.00)) giveMoney,
               IFNULL(mo.staffName, mbi.alias) staffName,
               mo.remark,
               mo.createDt,
               IFNULL(mo2.transDt, mo.createDt) transDt,
               t.memberBalance
        FROM tb_pluss_member_order mo
            LEFT JOIN tb_pluss_merchant_order mo2 ON mo.orderNumber = mo2.orderNumber
            LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.merchantCode = mo.merchantCode
            LEFT JOIN tb_pluss_merchant_menber mm ON mm.memberCode = mo.memberCode
            LEFT JOIN tb_pluss_merchant_menber_recharge mmr ON mmr.orderNumber = mo.orderNumber
            LEFT JOIN tb_pluss_merchant_menber_comsume mmc ON mmc.orderNumber = mo.orderNumber
            LEFT JOIN (
                SELECT
                    t.*,
                    ROUND(IF (@cgroup = t.memberCode, @csum := @csum + t.cardMoney, @csum := 0 + t.cardMoney), 2) memberBalance,
                    (@cgroup:= t.memberCode) `newGroup`
                FROM (
                     SELECT
                         SUM( IF (
                                 mo.orderType = 0,
                                 ( IFNULL( mmc.giveMoney, 0.00 ) + IFNULL( mmr.giveMoney, 0.00 ) - IFNULL( mo.orderFee, 0.00 ) ),
                                 ( IFNULL( mmc.giveMoney, 0.00 ) + IFNULL( mmr.giveMoney, 0.00 ) + IFNULL( mo.orderFee, 0.00 ) )
                             )
                             ) cardMoney, mo.memberCode, mo.createDt
                     FROM tb_pluss_member_order mo
                              LEFT JOIN tb_pluss_merchant_menber_comsume mmc ON mo.orderNumber = mmc.orderNumber
                              LEFT JOIN tb_pluss_merchant_menber_recharge mmr ON mo.orderNumber = mmr.orderNumber
                     WHERE mo.merchantCode = #{merchantCode} AND mo.createDt &lt;= #{endDate}
                     GROUP BY mo.memberCode, mo.createDt
                     ORDER BY mo.memberCode, mo.createDt
                 ) t,
                 (select @csum:=0, @cgroup:='') it
                ORDER BY t.memberCode, t.createDt
            ) t ON t.createDt = mo.createDt
        WHERE mo.orderStatus = '1' AND mo.merchantCode = #{merchantCode}
          AND mo.createDt BETWEEN #{startDate} AND #{endDate}
        ORDER BY mo.createDt;
    </select>

    <select id="selectMemberBillStatistics" resultType="cn.pluss.platform.vo.MemberOrderStatisticsVO">
        SELECT t.*, t2.sumMemberBalance memberBalance, t3.merchantChannelType FROM (
              SELECT
                  COUNT( IF ( mo.orderType = 0, 1, NULL ) ) consumeOrderCount,
                  COUNT( IF ( mo.orderType = 1, 1, NULL ) ) rechargeOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; mo.rechargeType = 1, 1, NULL ) ) inCashRechargeOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay1' || mo2.payTypeCode = 'wechatPay' || mo2.payTypeCode = 'leshuaPay1' ), 1, NULL ) ) wxOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay0' || mo2.payTypeCode = 'aliPay' || mo2.payTypeCode = 'leshuaPay0' ), 1, NULL ) ) aliOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay2' || mo2.payTypeCode = 'bank' || mo2.payTypeCode = 'leshuaPay2' ), 1, NULL ) ) bankOrderCount,
                  ROUND(SUM( IF ( mo.orderType = 0, mo.orderFee, 0.00)), 2) consumeSum,
                  ROUND(SUM( IF ( mo.orderType = 1, mo.orderFee, 0.00)), 2) rechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; mo.rechargeType = 1, mo.orderFee, 0.00) ), 2) inCashRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay1' || mo2.payTypeCode = 'wechatPay' || mo2.payTypeCode = 'leshuaPay1' ), mo.orderFee, 0.00 ) ), 2) wxRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay0' || mo2.payTypeCode = 'aliPay' || mo2.payTypeCode = 'leshuaPay0' ), mo.orderFee, 0.00 ) ), 2) aliRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay2' || mo2.payTypeCode = 'bank' || mo2.payTypeCode = 'leshuaPay2' ), mo.orderFee, 0.00 ) ), 2) bankRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 0, IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) consumeGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1, IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) rechargeGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; mo.rechargeType = 1, IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) inCashRechargeGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay1' || mo2.payTypeCode = 'wechatPay' || mo2.payTypeCode = 'leshuaPay1' ), IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) wxGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay0' || mo2.payTypeCode = 'aliPay' || mo2.payTypeCode = 'leshuaPay0') , IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) aliGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay2' || mo2.payTypeCode = 'bank' || mo2.payTypeCode = 'leshuaPay2' ), IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) bankGiveMoney,
                  #{startDate} startDate,
                  #{endDate} endDate,
                  '汇总' billName
              FROM
                  tb_pluss_member_order mo
                      LEFT JOIN tb_pluss_merchant_order mo2 ON mo.orderNumber = mo2.orderNumber
                      LEFT JOIN tb_pluss_merchant_menber_recharge mmr ON mmr.orderNumber = mo.orderNumber
                      LEFT JOIN tb_pluss_merchant_menber_comsume mmc ON mmc.orderNumber = mo.orderNumber
              WHERE
                  mo.orderStatus = '1'
                AND mo.merchantCode = #{merchantCode}
                AND mo.createDt BETWEEN #{startDate} AND #{endDate}
              UNION
              SELECT
                  COUNT( IF ( mo.orderType = 0, 1, NULL ) ) consumeOrderCount,
                  COUNT( IF ( mo.orderType = 1, 1, NULL ) ) rechargeOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; mo.rechargeType = 1, 1, NULL ) ) inCashRechargeOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay1' || mo2.payTypeCode = 'wechatPay' || mo2.payTypeCode = 'leshuaPay1' ), 1, NULL ) ) wxOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay0' || mo2.payTypeCode = 'aliPay' || mo2.payTypeCode = 'leshuaPay0' ), 1, NULL ) ) aliOrderCount,
                  COUNT( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay2' || mo2.payTypeCode = 'bank' || mo2.payTypeCode = 'leshuaPay2' ), 1, NULL ) ) bankOrderCount,
                  ROUND(SUM( IF ( mo.orderType = 0, mo.orderFee, 0.00)), 2) consumeSum,
                  ROUND(SUM( IF ( mo.orderType = 1, mo.orderFee, 0.00)), 2) rechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; mo.rechargeType = 1, mo.orderFee, 0.00) ), 2) inCashRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay1' || mo2.payTypeCode = 'wechatPay' || mo2.payTypeCode = 'leshuaPay1' ), mo.orderFee, 0.00 ) ), 2) wxRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay0' || mo2.payTypeCode = 'aliPay' || mo2.payTypeCode = 'leshuaPay0' ), mo.orderFee, 0.00 ) ), 2) aliRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay2' || mo2.payTypeCode = 'bank' || mo2.payTypeCode = 'leshuaPay2' ), mo.orderFee, 0.00 ) ), 2) bankRechargeSum,
                  ROUND(SUM( IF ( mo.orderType = 0, IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) consumeGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1, IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) rechargeGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; mo.rechargeType = 1, IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) inCashRechargeGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay1' || mo2.payTypeCode = 'wechatPay' || mo2.payTypeCode = 'leshuaPay1' ), IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) wxGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay0' || mo2.payTypeCode = 'aliPay' || mo2.payTypeCode = 'leshuaPay0') , IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) aliGiveMoney,
                  ROUND(SUM( IF ( mo.orderType = 1 &amp;&amp; ( mo2.payTypeCode = 'sxfPay2' || mo2.payTypeCode = 'bank' || mo2.payTypeCode = 'leshuaPay2' ), IFNULL(mmr.giveMoney, 0.00) + IFNULL(mmc.giveMoney, 0.00), 0.00)), 2) bankGiveMoney,
                  DATE_FORMAT(mo.createDt, '%Y-%m-%d 00:00:00') startDate,
                  DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59') endDate,
                  DATE_FORMAT(mo.createDt, '%Y-%m-%d') billName
              FROM tb_pluss_member_order mo
                       LEFT JOIN tb_pluss_merchant_order mo2 ON mo.orderNumber = mo2.orderNumber
                       LEFT JOIN tb_pluss_merchant_menber_recharge mmr ON mmr.orderNumber = mo.orderNumber
                       LEFT JOIN tb_pluss_merchant_menber_comsume mmc ON mmc.orderNumber = mo.orderNumber
              WHERE mo.orderStatus = '1'
                AND mo.merchantCode = #{merchantCode}
                AND mo.createDt BETWEEN #{startDate} AND #{endDate}
              GROUP BY DATE_FORMAT(mo.createDt, '%Y-%m-%d'), DATE_FORMAT(mo.createDt, '%Y-%m-%d 00:00:00'), DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59')
          ) t LEFT JOIN (
            SELECT date, ROUND((@csum := @csum + memberBalance), 2) sumMemberBalance
            FROM (SELECT
                      SUM(
                              IF (mo.orderType = 0,
                                  (IFNULL(mmc.giveMoney, 0.00) + IFNULL(mmr.giveMoney, 0.00) - IFNULL(mo.orderFee, 0.00)),
                                  (IFNULL(mmc.giveMoney, 0.00) + IFNULL(mmr.giveMoney, 0.00) + IFNULL(mo.orderFee, 0.00))
                                  )
                          ) memberBalance, DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59') date
                  FROM tb_pluss_member_order mo
                           LEFT JOIN tb_pluss_merchant_menber_comsume mmc ON mo.orderNumber = mmc.orderNumber
                           LEFT JOIN tb_pluss_merchant_menber_recharge mmr ON mo.orderNumber = mmr.orderNumber
                  WHERE mo.merchantCode = 'M800202104111235020' AND mo.orderStatus = 1
                  GROUP BY DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59')) t ,(select @csum:=0) as it order by date
          ) t2 ON DATE_FORMAT(t.endDate, '%Y%m%d%H%i%s') = DATE_FORMAT(t2.date, '%Y%m%d%H%i%s'), (
                SELECT CONCAT(mc.channelName, '备付金') merchantChannelType
                FROM tb_pluss_merchant_channel mc
                         LEFT JOIN tb_pluss_merchant_channel_status mcs ON mc.id = mcs.channel
                WHERE mcs.merchantCode = #{merchantCode}
          ) t3
        ORDER BY t.billName DESC;
    </select>

    <select id="selectStaffBillStatistics" resultType="cn.pluss.platform.vo.BillStatisticsVO">
        SELECT ta.staffName,
               IFNULL(t.billName, tn.billName) billName,
               IFNULL(t.allOrderCount, 0) allOrderCount,
               IFNULL(t.wxOrderCount, 0) wxOrderCount,
               IFNULL(t.aliOrderCount, 0) aliOrderCount,
               IFNULL(t.bankOrderCount, 0) bankOrderCount,
               IFNULL(t.allRefundOrderCount, 0) allRefundOrderCount,
               IFNULL(t.wxRefundOrderCount, 0) wxRefundOrderCount,
               IFNULL(t.aliRefundOrderCount, 0) aliRefundOrderCount,
               IFNULL(t.bankRefundOrderCount, 0) bankRefundOrderCount,
               IFNULL(t.allInMoney, 0.00) allInMoney,
               IFNULL(t.wxInMoney, 0.00) wxInMoney,
               IFNULL(t.aliInMoney, 0.00) aliInMoney,
               IFNULL(t.bankInMoney, 0.00) bankInMoney,
               IFNULL(t.allSubsidy, 0.00) allSubsidy,
               IFNULL(t.wxSubsidy, 0.00) wxSubsidy,
               IFNULL(t.aliSubsidy, 0.00) aliSubsidy,
               IFNULL(t.bankSubsidy, 0.00) bankSubsidy,
               IFNULL(t.allInAccount, 0.00) allInAccount,
               IFNULL(t.wxInAccount, 0.00) wxInAccount,
               IFNULL(t.aliInAccount, 0.00) aliInAccount,
               IFNULL(t.bankInAccount, 0.00) bankInAccount,
               #{startDate} startDate,
               #{endDate} endDate,
               IFNULL(tn.inCashOrderCount, 0) inCashOrderCount,
               IFNULL(tn.inCashInMoney, 0.00) inCashInMoney,
               IFNULL(tn.inCashInMoney, 0.00) inCashInAccount,
               t2.merchantChannelType
        FROM
        ( SELECT userId, ua.userName staffName
          FROM tb_pluss_user_app ua
          WHERE userType = 'staff'
            AND merchantCode = #{merchantCode}
            ) ta LEFT JOIN
        (
              SELECT
                  IFNULL( COUNT( IF ( `status` = '1', 1, NULL ) ), 0) allOrderCount,
                  IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxOrderCount,
                  IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliOrderCount,
                  IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2', 1, NULL ) ), 0) allRefundOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxRefundOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliRefundOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankRefundOrderCount,
                  SUM( IF ( `status` = '1', consumeFee, - consumeFee ) ) allInMoney,
                  SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ) wxInMoney,
                  SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ) aliInMoney,
                  SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ) bankInMoney,
                  SUM( IF ( `status` = 1, marketAmt, - marketAmt ) ) allSubsidy,
                  SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ) wxSubsidy,
                  SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', marketAmt, - marketAmt ), 0.00 ) ) aliSubsidy,
                  SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ) bankSubsidy,
                  SUM( IF ( `status` = 1, enterFee, - enterFee ) ) allInAccount,
                  SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ) wxInAccount,
                  SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1',enterFee, - enterFee  ), 0.00 ) ) aliInAccount,
                  SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ) bankInAccount,
                  #{startDate} startDate,
                  #{endDate} endDate,
                  mo.staffCode,
                  CONCAT(staffName, '——汇总') billName
              FROM
                  tb_pluss_merchant_order mo
              WHERE `status` IN ( '1', '2' )
                AND staffName IS NOT NULL
                AND merchantCode = #{merchantCode}
                AND createDt BETWEEN #{startDate} AND #{endDate}
              GROUP BY staffName, staffCode
              UNION
              SELECT
                  IFNULL( COUNT( IF ( `status` = '1', 1, NULL ) ), 0) allOrderCount,
                  IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxOrderCount,
                  IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliOrderCount,
                  IFNULL( COUNT( IF ( `status` = '1' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2', 1, NULL ) ), 0) allRefundOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), 1, NULL ) ), 0) wxRefundOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), 1, NULL ) ), 0) aliRefundOrderCount,
                  IFNULL( COUNT( IF ( `status` = '2' &amp;&amp; ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), 1, NULL ) ), 0) bankRefundOrderCount,
                  SUM( IF ( `status` = '1', consumeFee, - consumeFee ) ) allInMoney,
                  SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ) wxInMoney,
                  SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ) aliInMoney,
                  SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', consumeFee, - consumeFee ), 0.00 ) ) bankInMoney,
                  SUM( IF ( `status` = 1, marketAmt, - marketAmt ) ) allSubsidy,
                  SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ) wxSubsidy,
                  SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1', marketAmt, - marketAmt ), 0.00 ) ) aliSubsidy,
                  SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1',  marketAmt, - marketAmt ), 0.00 ) ) bankSubsidy,
                  SUM( IF ( `status` = 1, enterFee, - enterFee ) ) allInAccount,
                  SUM( IF ( ( payTypeCode = 'sxfPay1' || payTypeCode = 'wechatPay' || payTypeCode = 'leshuaPay1' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ) wxInAccount,
                  SUM( IF ( ( payTypeCode = 'sxfPay0' || payTypeCode = 'aliPay' || payTypeCode = 'leshuaPay0' ), IF ( `status` = '1',enterFee, - enterFee  ), 0.00 ) ) aliInAccount,
                  SUM( IF ( ( payTypeCode = 'sxfPay2' || payTypeCode = 'bank' || payTypeCode = 'leshuaPay2' ), IF ( `status` = '1', enterFee, - enterFee  ), 0.00 ) ) bankInAccount,
                  DATE_FORMAT(mo.createDt, '%Y-%m-%d 00:00:00') startDate,
                  DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59') endDate,
                  mo.staffCode,
                  CONCAT(mo.staffName, '——', DATE_FORMAT(mo.createDt, '%Y-%m-%d')) billName
              FROM tb_pluss_merchant_order mo
              WHERE mo.`status` IN ('1', '2')
                AND staffName IS NOT NULL
                AND merchantCode = #{merchantCode}
                AND createDt BETWEEN #{startDate} AND #{endDate}
              GROUP BY DATE_FORMAT(mo.createDt, '%Y-%m-%d'), DATE_FORMAT(mo.createDt, '%Y-%m-%d 00:00:00'), DATE_FORMAT(mo.createDt, '%Y-%m-%d 23:59:59'), CONCAT(mo.staffName, '——', DATE_FORMAT(mo.createDt, '%Y-%m-%d')), mo.staffCode
          ) t ON ta.userId = t.staffCode
        LEFT JOIN (
            SELECT
                CONCAT(staffName, '——汇总' ) billName,
                staffName,
                SUM( orderFee ) inCashInMoney,
                COUNT(1) inCashOrderCount
            FROM
                tb_pluss_member_order
            WHERE
                orderType = 1
              AND rechargeType = 1
              AND orderStatus = 1
              AND merchantCode = #{merchantCode}
              AND createDt BETWEEN #{startDate} AND #{endDate}
            GROUP BY
                CONCAT(staffName, '——汇总' ),
                staffName
            UNION
            SELECT
                CONCAT(staffName, DATE_FORMAT( createDt, '——%Y-%m-%d' )) billName,
                staffName,
                SUM( orderFee ) inCashMoney,
                COUNT(1) inCashOrderCount
            FROM
                tb_pluss_member_order
            WHERE
                orderType = 1
              AND rechargeType = 1
              AND orderStatus = 1
              AND merchantCode = #{merchantCode}
              AND createDt BETWEEN #{startDate} AND #{endDate}
            GROUP BY
                CONCAT(staffName, DATE_FORMAT( createDt, '——%Y-%m-%d' )),
                staffName
        ) tn ON tn.staffName = ta.staffName
                    , (
              SELECT CONCAT(mc.channelName, '备付金') merchantChannelType
              FROM tb_pluss_merchant_channel mc
                       LEFT JOIN tb_pluss_merchant_channel_status mcs ON mc.id = mcs.channel
              WHERE mcs.merchantCode = #{merchantCode}
          ) t2
        ORDER BY t.billName DESC;
    </select>

</mapper>
