• 0
  • 0

用户订单数排名

2019-08-21 150 0 admin 所属分类:PHP 记录
/*
    用户排行
    kind  string  
        today 今日
        yesterday 昨日 
        month  本月
 */
public function rider_rank($kind,$limit=20) {
    if ($kind=='today') {
        $begin = strtotime(date('Y-m-d',TIMESTAMP));
        $end = TIMESTAMP;
    } else if ($kind=='yesterday'){
        $end = strtotime(date('Y-m-d',TIMESTAMP));
        $begin = $end-86400;
    } else if ($kind=='month') {
        $begin = mktime(0,0,0,date('n',TIMESTAMP),1,date('Y',TIMESTAMP));
        $end = TIMESTAMP;
    }

    $sql = '';
    if ($begin && $end) {
        $sql = " and addtime between {$begin} and {$end}";
    } 

    $sql = "SELECT uid,count(*) as num FROM pre_orders WHERE status in (20,25,30,40) {$sql} GROUP BY uid ORDER BY num DESC LIMIT {$limit}";

    $rows = fetch_all($sql);
    $users = groupbyid(C::t(PT_USER)->getMany(joinbyid($rows, 'uid')), 'uid');
    $rank = 1;
    $pre_num = 0;
    foreach ($rows as $k => &$v) {
        if ($v['num']==$pre_num) {
            $v['rank'] = $rank;
        } else {
            $pre_num = $v['num'];
            $v['rank'] = $rank++;
        }
        $v['realname'] = $users[$v['rider_id']]['realname'];
        $v['avatar_url'] = DISCUZ_SITE .get_user_avatar($v['rider_id']);

        unset($v);
    }

    if ($rows) {
        succ($rows);
    } else {
        fail('暂无排名信息');
    }
}


返回顶部