当前位置:网站首页>tp6 实现佣金排行榜

tp6 实现佣金排行榜

2022-07-07 17:29:00 坚持与努力的成果

参考数据(表)

CREATE TABLE `user_money` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gid` int(10) DEFAULT NULL COMMENT '商品id',
  `tid` int(10) DEFAULT NULL COMMENT '用户id',
  `money` int(11) DEFAULT NULL COMMENT '佣金',
  `add_time` datetime DEFAULT NULL COMMENT '添加时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

 排行榜逻辑代码

        public function rankingList($time)
    {
        //连接redis
        $redis = new Redis();
        $redis->connect('127.0.0.1', '6379');
        //查询数据
        $data =  UserMoney::with('UserCommander')->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;
                }
        }
        
        //添加到redis有序集合
        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);
        //判断数据时间排行
        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;
    }

 

原网站

版权声明
本文为[坚持与努力的成果]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_58856770/article/details/125610045