<?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.cyksj.mapper.UserDistributeRankStatisticsRecordMapper">

    <select id="selectDistributeRanks" resultType="com.cyksj.model.views.DistributeUserRankView">
        select ROW_NUMBER()                             OVER (ORDER BY ${filed} desc) AS rank_id, concat(LEFT(ifnull(u.nickname,udsr.nickname),1),'*', RIGHT(ifnull(u.nickname,udsr.nickname),1)) as nickname,
               ifnull(u.headimgurl, udsr.headimgurl) as headimgurl,
               udsr.statistics_start_time,
               udsr.money,
               udsr.orders,
               udsr.reward_type,
               udsr.reward
        from user_distribute_rank_statistics_record udsr
                 left join user u on u.id = udsr.user_id
        where statistics_start_time = #{statisticsTime}
        order by ${filed} desc
    </select>

    <select id="getSelfRank" resultType="com.cyksj.model.views.DistributeUserRankView">
        select *
        from (select ROW_NUMBER()                             OVER (ORDER BY ${filed} desc) AS rank_id, concat(LEFT(ifnull(u.nickname,udsr.nickname),1),'*', RIGHT(ifnull(u.nickname,udsr.nickname),1)) as nickname,
                     ifnull(u.headimgurl, udsr.headimgurl) as headimgurl,
                     udsr.user_id,
                     udsr.statistics_start_time,
                     udsr.money,
                     udsr.orders,
                     udsr.reward_type,
                     udsr.reward
              from user_distribute_rank_statistics_record udsr
                       left join user u on u.id = udsr.user_id
              where statistics_start_time = #{statisticsTime}) s
        where s.user_id = #{userId}
    </select>

    <select id="getPreviousWinners" resultType="com.cyksj.model.views.DistributeUserRankView">
        select ROW_NUMBER()                             OVER (ORDER BY ${filed} desc) AS rank_id, concat(LEFT(ifnull(u.nickname,udsr.nickname),1),'*', RIGHT(ifnull(u.nickname,udsr.nickname),1)) as nickname,
               ifnull(u.headimgurl, udsr.headimgurl) as headimgurl,
               udsr.user_id,
               udsr.statistics_start_time,
               udsr.money,
               udsr.orders,
               udsr.reward,
               udsr.reward_type
        from user_distribute_rank_statistics_record udsr
                 left join user u on u.id = udsr.user_id
        where statistics_start_time = #{lastTime}
        limit 10
    </select>

    <select id="selectAcOrderDataView" resultType="com.cyksj.model.views.ActivityOrderDataView">
        select sum(orders) as orders, sum(money) as money
        from user_distribute_rank_statistics_record
        where robot_id is null
    </select>

    <select id="rankPage" resultType="com.cyksj.model.views.UserDistributeRankActivityView">
        select * from (select ROW_NUMBER() OVER (ORDER BY ${filed} desc) AS rank_id,
        ifnull(u.nickname,udsr.nickname) as nickname,
        ifnull(u.headimgurl, udsr.headimgurl) as headimgurl,
        udsr.user_id,
        udsr.statistics_start_time,
        udsr.statistics_end_time,
        udsr.money,
        udsr.orders,
        udsr.reward_type,
        udsr.reward,
        (select count(1) from user_distribute_shared us where us.shared_id = udsr.user_id) as userNum
        from user_distribute_rank_statistics_record udsr
        left join user u on u.id = udsr.user_id
        <where>
            <if test="date != null">
                statistics_start_time = #{date}
            </if>
            <if test="userId != null">
                and udsr.user_id = #{userId}
            </if>
            <if test="nickname != null">
                and u.nickname like '%${nickname}%'
            </if>
        </where>
        order by ${filed} desc limit 20)
        s
        <if test="orderBy!=null">
            order by ${orderBy} ${sort}
        </if>
    </select>

    <select id="getRobotRankInfo" resultType="com.cyksj.model.entity.UserActivityRankRobot">
        select *
        from (select ROW_NUMBER()                             OVER (ORDER BY ${filed} desc) AS rank_id, concat(LEFT(ifnull(u.nickname,udsr.nickname),1),'*', RIGHT(ifnull(u.nickname,udsr.nickname),1)) as nickname,
                     ifnull(u.headimgurl, udsr.headimgurl) as headimgurl,
                     udsr.user_id,
                     udsr.statistics_start_time,
                     udsr.money,
                     udsr.orders,
                     robot_id
              from user_distribute_rank_statistics_record udsr
                       left join user u on u.id = udsr.user_id
              where statistics_start_time = #{statisticsTime}) s
        where s.robot_id = #{robotId}
    </select>

    <select id="getMoreSelfRank" resultType="com.cyksj.model.views.DistributeUserRankView">
        select *
        from (select ROW_NUMBER() OVER (ORDER BY ${filed} desc) AS rank_id,
        concat(LEFT(ifnull(u.nickname,udsr.nickname),1),'*', RIGHT(ifnull(u.nickname,udsr.nickname),1)) as nickname,
        ifnull(u.headimgurl, udsr.headimgurl) as headimgurl,
        udsr.user_id,
        udsr.statistics_start_time,
        udsr.money,
        udsr.orders
        from user_distribute_rank_statistics_record udsr
        left join user u on u.id = udsr.user_id
        where statistics_start_time = #{statisticsTime}) s
        <where>
            <if test="rankId != null">
                s.rank_id &lt; #{rankId}
            </if>
            <if test="filed = 'orders'">
                and s.orders > #{orders}
            </if>
            <if test="filed = 'money'">
                and s.money > #{money}
            </if>
        </where>
        order by rank_id desc limit 1
    </select>
</mapper>