<?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="com.miaxis.extension.mapper.WxExtensionIncomeMapper">

    <resultMap type="WxExtensionIncome" id="WxExtensionIncomeResult">
        <result property="id"    column="id"    />
        <result property="out_trade_no"    column="outTradeNo"    />
        <result property="beneficiaryOpenid"    column="beneficiary_openid"    />
        <result property="sourceOpenid"    column="source_openid"    />
        <result property="hierarchy"    column="hierarchy"    />
        <result property="percentage"    column="percentage"    />
        <result property="profitPrice"    column="profit_price"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectWxExtensionIncomeVo">
        select * from wx_extension_income
    </sql>

    <select id="selectWxExtensionIncomeList" resultType="com.miaxis.extension.vo.ExtensionIncomeVo">
        SELECT
            ui.nick_name,
            ui.head_image,
            e.beneficiary_openid,
            e.totalPrice,
            IF( ISNULL(we.extractPrice) = 1, 0, we.extractPrice ) as extractPrice,
            ( e.totalPrice - IF( ISNULL(we.extractPrice) = 1, 0, we.extractPrice ) ) AS remainderPrice
        FROM
        ( SELECT ei.beneficiary_openid, SUM( ei.profit_price ) AS totalPrice FROM wx_extension_income ei GROUP BY ei.beneficiary_openid ) e
        LEFT JOIN user_info ui ON ui.openid = e.beneficiary_openid
        LEFT JOIN ( SELECT w.openid, SUM( w.extract_price ) AS extractPrice FROM wx_extract w where w.`status`=1 GROUP BY w.openid ) we ON we.openid = e.beneficiary_openid
        <where>
            <if test="beneficiaryOpenid != null  and beneficiaryOpenid != ''"> and e.beneficiary_openid = #{beneficiaryOpenid}</if>
            <if test="nickName != null  and nickName != ''"> and ui.nick_name = #{nickName}</if>
        </where>
    </select>

    <select id="extensionIncomeList" resultType = "com.miaxis.extension.vo.WxGzhExtensionIncomeVo">
        SELECT
            u.openid,
            u.nick_name,
            u.head_image,
            CASE ei.hierarchy
                WHEN 1 THEN
                  '一级'
                WHEN 2 THEN
                  '二级'
                ELSE '未知'
            END AS hierarchy,
            CONCAT( ei.percentage, '%' ) AS percentage,
            ei.profit_price,
            ei.create_time
        FROM
            user_info u
            LEFT JOIN wx_extension_income ei ON ei.source_openid = u.openid
        <where>
            ei.beneficiary_openid = #{openid}
            <if test="nickName != null  and nickName != ''"> and u.nick_name LIKE CONCAT('%',#{nickName},'%')</if>
        </where>
        ORDER BY
	        ei.create_time DESC
    </select>


    <select id="extensionIncomePrice" resultType="com.miaxis.extension.vo.ExtensionIncomeVo">
        SELECT
            ui.nick_name,
            ui.head_image,
            e.beneficiary_openid,
            e.totalPrice,
            IF( ISNULL(we.extractPrice) = 1, 0, we.extractPrice ) as extractPrice,
            ( e.totalPrice - IF( ISNULL(we.extractPrice) = 1, 0, we.extractPrice ) ) AS remainderPrice
        FROM
            (SELECT ei.beneficiary_openid, SUM( ei.profit_price) AS totalPrice FROM wx_extension_income ei GROUP BY ei.beneficiary_openid) e
            LEFT JOIN user_info ui ON ui.openid = e.beneficiary_openid
            LEFT JOIN (SELECT w.openid, SUM( w.extract_price ) AS extractPrice FROM wx_extract w WHERE w.`status` = 1 GROUP BY w.openid) we ON we.openid = e.beneficiary_openid
        where e.beneficiary_openid = #{openid}
    </select>


</mapper>