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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.UserRewardFlow">
        <id column="id" property="id" />
        <result column="userId" property="userId" />
        <result column="parentId" property="parentId" />
        <result column="t2Flow" property="t2Flow" />
        <result column="t3Flow" property="t3Flow" />
        <result column="dividFlow" property="dividFlow" />
        <result column="areaFlow" property="areaFlow" />
        <result column="createTime" property="createTime" />
        <result column="createDate" property="createDate" />
        <result column="orderNumber" property="orderNumber" />
    </resultMap>

    <select id="listManageProfit" resultType="cn.pluss.platform.entity.UserManageProfit">
        SELECT
            a.userId,
            a.parentId,
            a.roleCode,
            a.levelCode,
            a.t2Flow,
            a.t3Flow,
            ROUND((a.t2Flow * (a.directRate / 10000) * a.t2Scale),2) AS t2Profit,
            ROUND(( a.t3Flow * (a.indirectRate / 10000) * a.t3Scale ),2)AS t3Profit
        FROM
            (
                SELECT
                    rf.userId,
                    rf.parentId,
                    ua.roleCode,
                    ua.levelCode,
                    SUM( rf.t2Flow ) AS t2Flow,
                    SUM( rf.t3Flow ) AS t3Flow,
                    rf.createTime,
                    ul.directRate,
                    ul.indirectRate,
                    ul.t2Scale,
                    ul.t3Scale
                FROM
                    tb_pluss_user_reward_flow rf
                        LEFT JOIN tb_pluss_user_app ua ON rf.userId = ua.userId
                        LEFT JOIN tb_pluss_user_level ul ON ua.levelCode = ul.levelCode
                WHERE createDate = #{date} AND rf.`status` = 1
                GROUP BY
                    userId,
                    createDate
            ) a
    </select>
</mapper>
