<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.pluss.platform.mapper.AgentInfoMapper">

    <resultMap id="AgentInfo"  type="cn.pluss.platform.vo.AgentInfo" autoMapping="true">

    </resultMap>

    <select id="selectPage" resultMap="AgentInfo, cn.pluss.platform.mapper.CommonMapper.count">
        SELECT SQL_CALC_FOUND_ROWS
            ua.userId,
            ui.loginName,
            mr.rate,
            ui.phone,
            ui.STATUS,
            ua.userName,
            ui.createTime,
            ui.updateTime,
            ua.isFixedRate, ua.autoAudit, ua.payEcdemicSwitch, ua.isMarket, ua.isUnionPay,
            sumPrice,
            cashAmt,
            t4.beforeMonthPrice,
            t4.nowMonthPrice
        FROM
            tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_rate mr ON ua.userId = mr.userId
        LEFT JOIN (
            SELECT
                SUM( mp.price ) sumPrice, mp.userId
            FROM
                tb_pluss_merchant_profit mp
            GROUP BY mp.userId
        ) t2 ON ua.userId = t2.userId
        LEFT JOIN (
            SELECT SUM(pc.cashAmt) cashAmt, pc.userId
            FROM tb_pluss_cash pc WHERE pc.`status` = '1'
            GROUP BY pc.userId
        ) t3 ON ua.userId = t3.userId
        LEFT JOIN (
            SELECT
                t.userId
                , SUM(IF(recordMonth = DATE_FORMAT(DATE_ADD(CURRENT_DATE, INTERVAL -1 MONTH), "%Y-%m"), monthPrice, 0)) beforeMonthPrice
                , SUM(IF(recordMonth = DATE_FORMAT(CURRENT_DATE, "%Y-%m"), monthPrice, 0)) nowMonthPrice
            FROM (
                SELECT
                    ua.userId,
                    mp.recordMonth,
                    IFNULL( SUM( mp.price ), 0 ) monthPrice
                FROM
                    tb_pluss_user_app ua
                LEFT JOIN tb_pluss_merchant_profit mp ON ua.userId = mp.userId
                AND mp.recordMonth IN ( DATE_FORMAT(DATE_ADD(CURRENT_DATE, INTERVAL -1 MONTH), "%Y-%m"), DATE_FORMAT(CURRENT_DATE, "%Y-%m") )
                WHERE
                    ua.userType = 'agent'
                GROUP BY
                    ua.userId,
                    mp.recordMonth
            ) t GROUP BY t.userId
        ) t4 ON t4.userId = ua.userId
        WHERE
            ua.userType = 'agent'
            AND ua.parentId IS NULL
        <include refid="commonWhere"/>
        ORDER BY
        <foreach collection="page.orders" item="orderItem" separator=",">
            ${orderItem.column}
            <if test="orderItem.asc == false">
                DESC
            </if>
        </foreach>
        LIMIT ${page.getIntValue('size') * (page.current - 1)}, ${page.getIntValue('size')};
        SELECT found_rows() as count;
    </select>

    <select id="selectPermissionPage" resultMap="AgentInfo, cn.pluss.platform.mapper.CommonMapper.count">
        SELECT SQL_CALC_FOUND_ROWS
        ua.userId,
        ui.loginName,
        mr.rate,
        ui.phone,
        ui.STATUS,
        ua.userName,
        ui.createTime,
        ui.updateTime,
        tpcr.remark,
        ua.isFixedRate, ua.autoAudit, ua.payEcdemicSwitch, ua.isMarket, ua.isUnionPay
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_rate mr ON ua.userId = mr.userId
        LEFT JOIN tb_pluss_common_remark tpcr on mr.userId = tpcr.userId AND tpcr.code = 'USER'
        <where>
            ui.loginName NOT LIKE '%@%'
            AND ua.userType IN ('promoter', 'agent')
            <include refid="commonWhere"/>
        </where>
        ORDER BY
        <foreach collection="page.orders" item="orderItem" separator=",">
            ${orderItem.column}
            <if test="orderItem.asc == false">
                DESC
            </if>
        </foreach>
        LIMIT ${page.getIntValue('size') * (page.current - 1)}, ${page.getIntValue('size')};
        SELECT found_rows() as count;
    </select>

    <select id="selectPageCount" resultType="java.lang.Long">
        SELECT
        count(1)
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_rate mr ON ua.userId = mr.userId
        WHERE
        ua.userType = 'agent'
        AND ua.parentId IS NULL
        <include refid="commonWhere"/>
        ORDER BY ui.createTime desc
    </select>

    <select id="selectOne" resultType="cn.pluss.platform.vo.AgentInfo">
        SELECT ua.userId, ui.loginName, mr.rate, ui.phone, ui.status, ua.userName, ui.createTime,
        ui.updateTime,ua.isFixedRate, t3.cashAmt
        FROM tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_rate mr ON ua.userId=mr.userId
        LEFT JOIN tb_pluss_cash pc ON ua.userId = pc.userId
        LEFT JOIN (
            SELECT SUM(pc.cashAmt) cashAmt, pc.userId
            FROM tb_pluss_cash pc
            GROUP BY pc.userId
        ) t3 ON ua.userId = t3.userId
        WHERE ua.userType = 'agent'
        <include refid="commonWhere"/>
        LIMIT 1
    </select>

    <select id="selectRecentMonthPrice" resultType="cn.pluss.platform.vo.AgentInfo">
        SELECT
            t.userId, t.userName
             , SUM(IF(recordMonth = DATE_FORMAT(DATE_ADD(CURRENT_DATE, INTERVAL -1 MONTH), "%Y-%m"), monthPrice, 0)) beforeMonthPrice
             , SUM(IF(recordMonth = DATE_FORMAT(CURRENT_DATE, "%Y-%m"), monthPrice, 0)) nowMonthPrice
        FROM (
                 SELECT
                     ua.userId,
                     ua.userName,
                     mp.recordMonth,
                     IFNULL( SUM( mp.price ), 0 ) monthPrice
                 FROM
                     tb_pluss_user_app ua
                         LEFT JOIN tb_pluss_merchant_profit mp ON ua.userId = mp.userId
                         AND mp.recordMonth IN ( DATE_FORMAT(DATE_ADD(CURRENT_DATE, INTERVAL -1 MONTH), "%Y-%m"), DATE_FORMAT(CURRENT_DATE, "%Y-%m") )
                 WHERE
                     ua.userType = 'agent'
                 GROUP BY
                     ua.userId,
                     ua.userName,
                     mp.recordMonth
             ) t GROUP BY t.monthPrice, t.userId, t.userName;
    </select>

    <select id="selectSubAgentPage" resultMap="AgentInfo, cn.pluss.platform.mapper.CommonMapper.count">
        SELECT SQL_CALC_FOUND_ROWS
        ua.userId,
        ua.parentId,
        ui.loginName,
        mr.rate,
        ui.phone,
        ui.STATUS,
        ua.userName,
        ui.createTime,
        ui.updateTime,
        ua.isFixedRate, ua.autoAudit, ua.payEcdemicSwitch, ua.isMarket, ua.isUnionPay,
        sumPrice,
        cashAmt,
        t4.beforeMonthPrice,
        t4.nowMonthPrice
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_rate mr ON ua.userId = mr.userId
        LEFT JOIN (
        SELECT
        SUM( mp.price ) sumPrice, mp.userId
        FROM
        tb_pluss_merchant_profit mp
        GROUP BY mp.userId
        ) t2 ON ua.userId = t2.userId
        LEFT JOIN (
            SELECT SUM(pc.cashAmt) cashAmt, pc.userId
            FROM tb_pluss_cash pc
            WHERE pc.`status` = '1'
            GROUP BY pc.userId
        ) t3 ON ua.userId = t3.userId
        LEFT JOIN (
        SELECT
        t.userId
        , SUM(IF(recordMonth = DATE_FORMAT(DATE_ADD(CURRENT_DATE, INTERVAL -1 MONTH), "%Y-%m"), monthPrice, 0)) beforeMonthPrice
        , SUM(IF(recordMonth = DATE_FORMAT(CURRENT_DATE, "%Y-%m"), monthPrice, 0)) nowMonthPrice
        FROM (
        SELECT
        ua.userId,
        mp.recordMonth,
        IFNULL( SUM( mp.price ), 0 ) monthPrice
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_merchant_profit mp ON ua.userId = mp.userId
        AND mp.recordMonth IN ( DATE_FORMAT(DATE_ADD(CURRENT_DATE, INTERVAL -1 MONTH), "%Y-%m"), DATE_FORMAT(CURRENT_DATE, "%Y-%m") )
        WHERE
        ua.userType = 'agent'
        GROUP BY
        ua.userId,
        mp.recordMonth
        ) t GROUP BY t.userId
        ) t4 ON t4.userId = ua.userId
        WHERE
        ua.userType = 'agent'
        AND ua.parentId IS NOT NULL
        <include refid="commonWhere"/>
        ORDER BY
        <foreach collection="page.orders" item="orderItem" separator=",">
            ${orderItem.column}
            <if test="orderItem.asc == false">
                DESC
            </if>
        </foreach>
        LIMIT ${page.getIntValue('size') * (page.current - 1)}, ${page.getIntValue('size')};
        SELECT found_rows() as count;
    </select>

<!--    <select id="selectSubAgentPage" resultType="cn.pluss.platform.vo.AgentInfo">-->
<!--        SELECT-->
<!--        ua.userId,-->
<!--        ua.parentId,-->
<!--        ui.loginName,-->
<!--        mr.rate,-->
<!--        ui.phone,-->
<!--        ui.STATUS,-->
<!--        ua.userName,-->
<!--        ui.createTime,-->
<!--        ui.updateTime,-->
<!--        ua.isFixedRate, ua.autoAudit, ua.payEcdemicSwitch, ua.isMarket, ua.isUnionPay,-->
<!--        t2.sumPrice,-->
<!--        t3.cashAmt-->
<!--        FROM-->
<!--        tb_pluss_user_app ua-->
<!--        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id-->
<!--        LEFT JOIN tb_pluss_merchant_rate mr ON ua.userId = mr.userId-->
<!--        LEFT JOIN (-->
<!--            SELECT-->
<!--                SUM( mp.price ) sumPrice, mp.userId-->
<!--            FROM-->
<!--                tb_pluss_merchant_profit mp-->
<!--            GROUP BY mp.userId-->
<!--        ) t2 ON ua.userId = t2.userId-->
<!--        LEFT JOIN (-->
<!--            SELECT SUM(pc.cashAmt) cashAmt, pc.userId-->
<!--            FROM tb_pluss_cash pc-->
<!--            GROUP BY pc.userId-->
<!--        ) t3 ON ua.userId = t3.userId-->
<!--        WHERE-->
<!--        ua.userType = 'agent'-->
<!--        AND ua.parentId IS NOT NULL-->
<!--        <include refid="commonWhere"/>-->
<!--        <if test="agentInfo.parentId != null and agentInfo.parentId != ''">-->
<!--            and ua.parentId = #{agentInfo.parentId}-->
<!--        </if>-->
<!--        ORDER BY-->
<!--        ui.createTime-->
<!--    </select>-->

    <select id="selectSubAgentPageCount" resultType="java.lang.Long">
        SELECT
            ua.userId,
            ua.parentId,
            ui.loginName,
            mr.rate,
            ui.phone,
            ui.STATUS,
            ua.userName,
            ui.createTime,
            ui.updateTime,
            ua.isFixedRate,
            t2.sumPrice,
            t3.cashAmt
        FROM
        tb_pluss_user_app ua
        LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
        LEFT JOIN tb_pluss_merchant_rate mr ON ua.userId = mr.userId
        WHERE
        ua.userType = 'agent'
        AND ua.parentId IS NOT NULL
        <include refid="commonWhere"/>
        <if test="agentInfo.parentId != null and agentInfo.parentId != ''">
            and ua.parentId = #{agentInfo.parentId}
        </if>
        ORDER BY
        ui.createTime
    </select>

    <select id="selectStaffPage" resultType="cn.pluss.platform.vo.AgentStaffVO">
        SELECT loginName, userId, parentId, userName, createTime, phone, agentName, agentPhone FROM (
            SELECT
                ui.loginName, ua.userId, ua.parentId, ua.userName, ua.createDt createTime, ui.phone
                , ua1.userName agentName, ui1.phone agentPhone
            FROM tb_pluss_user_app ua
            LEFT JOIN tb_pluss_user_info ui ON ua.userId = ui.id
            LEFT JOIN tb_pluss_user_app ua1 ON ua.parentId = ua1.userId
            LEFT JOIN tb_pluss_user_info ui1 ON ui1.id = ua1.userId
             WHERE ua.userType = 'agent_staff'
        ) t
        <where>
            <if test="agentStaffVO.userId != null and agentStaffVO.userId != ''">
                t.userId = #{agentStaffVO.userId}
            </if>
            <if test="agentStaffVO.loginName != null and agentStaffVO.loginName != ''">
                t.loginName = #{agentStaffVO.loginName}
            </if>
            <if test="agentStaffVO.userName != null and agentStaffVO.userName != ''">
                t.userName LIKE CONCAT('%', #{agentStaffVO.userName},'%')
            </if>
            <if test="agentStaffVO.phone != null and agentStaffVO.phone != ''">
                t.phone LIKE CONCAT('%',#{agentStaffVO.phone},'%')
            </if>
            <if test="agentStaffVO.parentId != null and agentStaffVO.parentId != ''">
                t.parentId =#{agentStaffVO.parentId}
            </if>
            <if test="agentStaffVO.agentName != null and agentStaffVO.agentName != ''">
                t.agentName LIKE CONCAT('%', #{agentStaffVO.agentName},'%')
            </if>
            <if test="agentStaffVO.agentPhone != null and agentStaffVO.agentPhone != ''">
                t.agentPhone LIKE CONCAT('%',#{agentStaffVO.agentPhone},'%')
            </if>
        </where>
    </select>


    <sql id="commonWhere">
        <if test="agentInfo.userId != null and agentInfo.userId != ''">
            AND ua.userId = #{agentInfo.userId}
        </if>
        <if test="agentInfo.loginName != null and agentInfo.loginName != ''">
            AND ui.loginName LIKE CONCAT("%", #{agentInfo.loginName}, "%")
        </if>
        <if test="agentInfo.phone != null and agentInfo.phone != ''">
            AND ui.phone LIKE CONCAT("%", #{agentInfo.phone}, "%")
        </if>
        <if test="agentInfo.userName != null and agentInfo.userName != ''">
            AND ua.userName LIKE #{agentInfo.userName}
        </if>
        <if test="agentInfo.isFixedRate == '1'.toString() ">
            AND ua.isFixedRate = '1'
        </if>
        <if test="agentInfo.isFixedRate == '0'.toString() ">
            AND (ua.isFixedRate is null or ua.isFixedRate = '0')
        </if>
        <if test="agentInfo.rate != null and agentInfo.rate != ''">
            AND mr.rate LIKE #{agentInfo.rate}
        </if>
        <if test="agentInfo.autoAudit != null and agentInfo.autoAudit != ''">
            <choose>
                <when test="agentInfo.autoAudit == '1'.toString()">
                    AND ua.autoAudit = '1'
                </when>
                <otherwise>
                    AND (ua.autoAudit = '0' OR ua.autoAudit IS NULL)
                </otherwise>
            </choose>
        </if>
        <if test="agentInfo.payEcdemicSwitch != null and agentInfo.payEcdemicSwitch != ''">
            <choose>
                <when test="agentInfo.payEcdemicSwitch == '1'.toString()">
                    AND ua.payEcdemicSwitch = '1'
                </when>
                <otherwise>
                    AND ua.payEcdemicSwitch = '0'
                </otherwise>
            </choose>
        </if>
        <if test="agentInfo.isMarket != null and agentInfo.isMarket != ''">
            <choose>
                <when test="agentInfo.isMarket == '1'.toString()">
                    AND ua.isMarket = '1'
                </when>
                <otherwise>
                    AND (ua.isMarket = '0' OR ua.isMarket IS NULL)
                </otherwise>
            </choose>
        </if>
        <if test="agentInfo.isUnionPay != null and agentInfo.isUnionPay != ''">
            <choose>
                <when test="agentInfo.isUnionPay == '1'.toString()">
                    AND ua.isUnionPay = '1'
                </when>
                <otherwise>
                    AND (ua.isUnionPay = '0' OR ua.isUnionPay IS NULL)
                </otherwise>
            </choose>
        </if>
    </sql>
</mapper>