当前位置:网站首页>Tp6 realize Commission ranking

Tp6 realize Commission ranking

2022-07-07 19:39:00 Results of persistence and efforts

Reference data ( surface )

CREATE TABLE `user_money` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gid` int(10) DEFAULT NULL COMMENT ' goods id',
  `tid` int(10) DEFAULT NULL COMMENT ' user id',
  `money` int(11) DEFAULT NULL COMMENT ' commission ',
  `add_time` datetime DEFAULT NULL COMMENT ' Add the time ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

  Leaderboard logic code

        public function rankingList($time)
    {
        // Connect redis
        $redis = new Redis();
        $redis->connect('127.0.0.1', '6379');
        // Query data 
        $data =  UserMoney::with('UserCommander')->select()->toArray();
        // Array processing    Cumulative Commission 
        $newArr = array();
        foreach ($data as $v)
        {
            if (array_key_exists($v['tid'], $newArr))
                {
                    $newArr[$v['tid']]['money'] += $v['money'];
                }
                else {
                    $newArr[$v['tid']] = $v;
                }
        }
        
        // Add to redis Ordered set 
        foreach ($data as $k =>$v)
        {
            $redis->zincrby('tag1',$v['money'],$v['tid']);
        }
        $data = $redis->zrevrange('tag1', 0, -1);
        $ids = implode(',', $data);
        $score = $redis->zRevRange('tag1', 0, -1, true);
        // Judge data time ranking 
        if(!empty($time))
        {
            $data = UserMoney::with('UserCommander')->whereTime('add_time',$time)->whereIn('tid', $data)->order(Db::raw("FIELD(tid,$ids)"))->select()->toArray();
            $newArr = array();
            foreach ($data as $v)
            {
                if (array_key_exists($v['tid'], $newArr))
                {
                    $newArr[$v['tid']]['money'] += $v['money'];
                }
                else {
                    $newArr[$v['tid']] = $v;
                }
            }
        }else{
            $data = UserMoney::whereIn('tid', $data)->order(Db::raw("FIELD(tid,$ids)"))->select()->toArray();
            $newArr = array();
            foreach ($data as $v)
            {
                if (array_key_exists($v['tid'], $newArr))
                {
                    $newArr[$v['tid']]['money'] += $v['money'];
                }
                else {
                    $newArr[$v['tid']] = $v;
                }
            }
        }
        $res = [];
        foreach ($newArr as $key => $val) {
            $res[] = [
                'tid' => $val['tid'],
                'money' => $val['money'],
                'id' => $val['id'],
                'name' => $val['UserCommander']['name'],
            ];
        }
        return $res;
    }

 

原网站

版权声明
本文为[Results of persistence and efforts]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071729306211.html