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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.PlatformData">
        <id column="id" property="id" />
        <result column="transAmt" property="transAmt" />
        <result column="aliAmt" property="aliAmt" />
        <result column="wxAmt" property="wxAmt" />
        <result column="ysfAmt" property="ysfAmt" />
        <result column="transNum" property="transNum" />
        <result column="aliNum" property="aliNum" />
        <result column="wxNum" property="wxNum" />
        <result column="ysfNum" property="ysfNum" />
        <result column="refundAmt" property="refundAmt" />
        <result column="aliRefundAmt" property="aliRefundAmt" />
        <result column="wxRefundAmt" property="wxRefundAmt" />
        <result column="ysfRefundAmt" property="ysfRefundAmt" />
        <result column="storeNum" property="storeNum" />
        <result column="memberNum" property="memberNum" />
        <result column="promoterNum" property="promoterNum" />
        <result column="profitAmt" property="profitAmt" />
        <result column="shareAmt" property="shareAmt" />
        <result column="fansAmt" property="fansAmt" />
        <result column="actualAmt" property="actualAmt" />
        <result column="operatDate" property="operatDate" />
        <result column="createTime" property="createTime" />
    </resultMap>

    <select id="getYestDayPlatformData" resultType="cn.pluss.platform.entity.PlatformData">
        SELECT
        SUM(consumeFee) AS transAmt,
        SUM(CASE WHEN (payTypeCode = 'wechatPay' or payTypeCode = 'sxfPay1') THEN
        consumeFee
        ELSE
        0
        END) AS wxAmt,
        SUM(CASE WHEN (payTypeCode = 'aliPay' or payTypeCode = 'sxfPay0') THEN
        consumeFee
        ELSE
        0
        END) AS aliAmt,
        SUM(CASE WHEN (payTypeCode = 'bank' or payTypeCode = 'sxfPay2') THEN
        consumeFee
        ELSE
        0
        END) AS ysfAmt,
        count(1) AS transNum,
        SUM(CASE WHEN (payTypeCode = 'wechatPay' or payTypeCode = 'sxfPay1') THEN
        1
        ELSE
        0
        END) AS wxNum,
        SUM(CASE WHEN (payTypeCode = 'aliPay' or payTypeCode = 'sxfPay0') THEN
        1
        ELSE
        0
        END) AS aliNum,
        SUM(CASE WHEN (payTypeCode = 'bank' or payTypeCode = 'sxfPay2') THEN
        1
        ELSE
        0
        END) AS ysfNum,
        SUM(
        IF(orderType = 8,enterFee, consumeFee - enterFee) - channelFee - (IF(cashFee = 0,consumeFee - enterFee - mercFee, cashFee) * 0.2)
        ) AS profitAmt,
        IFNULL(SUM(profitShareMoney), 0) AS shareAmt,
        IFNULL(SUM(fansShareMoney), 0) AS fansAmt
        FROM tb_pluss_merchant_order WHERE  `status` = 1 and createDate = date_sub(curdate(),interval 1 day)
    </select>

    <select id="getYestDayPlatformRefundData" resultType="cn.pluss.platform.entity.PlatformData">
        SELECT
            IFNULL(SUM(payAmt), 0) AS refundAmt,
            IFNULL(SUM(CASE WHEN (payTypeCode = 'wechatPay' or payTypeCode = 'sxfPay1') THEN
                payAmt
                ELSE
                0
                END), 0) AS wxRefundAmt,
            IFNULL(SUM(CASE WHEN (payTypeCode = 'aliPay' or payTypeCode = 'sxfPay0') THEN
                payAmt
                ELSE
                0
                END), 0) AS aliRefundAmt,
            IFNULL(SUM(CASE WHEN (payTypeCode = 'bank' or payTypeCode = 'sxfPay2') THEN
                payAmt
                ELSE
                0
                END), 0) AS ysfRefundAmt
        FROM tb_pluss_merchant_refund_order WHERE `status` = 1 and date(createTime) = date_sub(curdate(),interval 1 day)
    </select>

    <select id="getYestDayPlatformAddData" resultType="cn.pluss.platform.entity.PlatformData">
        SELECT
            SUM(IF(type = 'yesterdayPromoter',num,0)) AS promoterNum,
            SUM(IF(type = 'yesterdayMember',num,0))AS memberNum,
            SUM(IF(type = 'yesterdayStore',num,0)) AS storeNum
        FROM (
        SELECT count(1) AS num, 'yesterdayPromoter' as type, 'add' as addType FROM tb_pluss_user_app WHERE date(createDt) = date_sub(curdate(),interval 1 day)
        union
        SELECT count(1) AS num, 'yesterdayMember' AS type,'add' as addType  FROM tb_pluss_merchant_menber WHERE date(createDt) = date_sub(curdate(),interval 1 day)
        union
        SELECT count(1) AS num, 'yesterdayStore' AS type,'add' as addType FROM tb_pluss_merchant_store WHERE date(createDt) = date_sub(curdate(),interval 1 day)
        )as
        a GROUP BY addType
    </select>

    <select id="getChartTransCountData" resultType="java.util.Map">
        SELECT
            DATE_FORMAT(createDate, '%Y-%m-%d') days,
            SUM( consumeFee ) AS transCountAmt,
            count(1) AS transNum
        FROM
            ( SELECT consumeFee, createDate FROM tb_pluss_merchant_order WHERE DATE_SUB( CURDATE(), INTERVAL 7 DAY ) &lt;= createDate AND STATUS = '1' ) AS d
        GROUP BY
            d.createDate
    </select>


    <select id="getChartChannelCountData" resultType="java.util.Map">
        SELECT * from (
          SELECT
              (CASE aisleSwitch
                   WHEN 1 THEN
                       '乐刷'
                   WHEN 2 THEN
                       '随行付'
                   WHEN 3 THEN
                       '瑞银信'
                   WHEN 4 THEN
                       '银盛'
                   ELSE
                       '其他'
                  END) as channelName,
              SUM(consumeFee) AS transCountAmt,
              count(1) AS transNum
          FROM tb_pluss_merchant_order WHERE `status` = 1 AND date(createDt) = curdate() GROUP BY aisleSwitch
        ) a ORDER BY a.transCountAmt desc
    </select>

    <select id="selectMonthData" resultType="cn.pluss.platform.entity.PlatformData">
        SELECT
            SUM(transAmt) transAmt,
            SUM(aliAmt) aliAmt,
            SUM(wxAmt) wxAmt,
            SUM(ysfAmt) ysfAmt,
            SUM(transNum) transNum,
            SUM(aliNum) aliNum,
            SUM(wxNum) wxNum,
            SUM(ysfNum) ysfNum,
            SUM(storeNum) storeNum,
            SUM(memberNum) memberNum,
            SUM(promoterNum) promoterNum,
            SUM(actualAmt) actualAmt
        FROM tb_pluss_platform_data WHERE operateMonth = #{month};
    </select>

</mapper>
