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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.MemberTransStatistics">
        <id column="id" property="id" />
        <result column="userId" property="userId" />
        <result column="date" property="date" />
        <result column="rechargeAmount" property="rechargeAmount" />
        <result column="consumeAmount" property="consumeAmount" />
        <result column="giveAmount" property="giveAmount" />
        <result column="remark" property="remark" />
        <result column="createTime" property="createTime" />
        <result column="updateTime" property="updateTime" />
    </resultMap>

    <insert id="insertDuringBill">
        INSERT IGNORE INTO tb_pluss_member_trans_statistics(userId, startDate, endDate, type, rechargeAmount, consumeAmount, giveAmount)
        SELECT ua.userId, #{startDate}, #{endDate}, "1", IFNULL(t.rechargeAmount, 0), IFNULL(t.consumeAmount, 0), IFNULL(t.giveAmount, 0)
        FROM
            tb_pluss_user_app ua LEFT JOIN
            (
                SELECT
                    merchantCode,
                    SUM(IF(type = 1, transMoney, 0)) rechargeAmount,
                    SUM(IF(type = 2, transMoney, 0)) consumeAmount,
                    SUM(giveMoney) giveAmount
                FROM (
                         SELECT merchantCode, rechargeMoney transMoney, giveMoney, 1 type, createDt
                         FROM tb_pluss_merchant_menber_recharge
                         WHERE createDt BETWEEN CONCAT(#{startDate}, " 00:00:00")
                            AND CONCAT(#{endDate}, " 23:59:59")
                         UNION
                         SELECT merchantCode, comsumeMoney transMoney, giveMoney, 2 type, createDt
                         FROM tb_pluss_merchant_menber_comsume
                         WHERE createDt BETWEEN CONCAT(#{startDate}, " 00:00:00")
                            AND CONCAT(#{endDate}, " 23:59:59")
                     ) t  GROUP BY merchantCode
            ) t ON t.merchantCode = ua.merchantCode
                                 LEFT JOIN tb_pluss_merchant_channel_status mcs ON ua.merchantCode = mcs.merchantCode
        WHERE ua.userType = 'promoter' AND mcs.`status` = '3' AND mcs.updateTime &lt;= CONCAT(#{endDate}, " 23:59:59" )
    </insert>

    <select id="selectOneDuringBill" resultType="cn.pluss.platform.entity.MemberTransStatistics">
        SELECT ua.userId, #{startDate} startDate, #{endDate} endDate, "1" type,
               IFNULL(t.rechargeAmount, 0) rechargeAmount, IFNULL(t.consumeAmount, 0) consumeAmount, IFNULL(t.giveAmount, 0) giveAmount
        FROM
            tb_pluss_user_app ua LEFT JOIN
            (
                SELECT
                    merchantCode,
                    SUM(IF(type = 1, transMoney, 0)) rechargeAmount,
                    SUM(IF(type = 2, transMoney, 0)) consumeAmount,
                    SUM(giveMoney) giveAmount
                FROM (
                         SELECT merchantCode, rechargeMoney transMoney, giveMoney, 1 type, createDt
                         FROM tb_pluss_merchant_menber_recharge
                         WHERE createDt BETWEEN CONCAT(#{startDate}, " 00:00:00")
                                   AND CONCAT(#{endDate}, " 23:59:59")
                         UNION
                         SELECT merchantCode, comsumeMoney transMoney, giveMoney, 2 type, createDt
                         FROM tb_pluss_merchant_menber_comsume
                         WHERE createDt BETWEEN CONCAT(#{startDate}, " 00:00:00")
                                   AND CONCAT(#{endDate}, " 23:59:59")
                     ) t  GROUP BY merchantCode
            ) t ON t.merchantCode = ua.merchantCode
                                 LEFT JOIN tb_pluss_merchant_channel_status mcs ON ua.merchantCode = mcs.merchantCode
        WHERE mcs.`status` = '3' AND mcs.updateTime &lt;= CONCAT(#{endDate}, " 23:59:59" )
        AND ua.userId = #{userId}
        ORDER BY mcs.valid DESC, mcs.createTime LIMIT 1
    </select>
</mapper>
