遇到查多張分表還需分頁的操作思路

1、使用緩存分別獲取兩張表的數據,然後採用數組分頁的方式

$list = Cache::getOrSet("wx_mini_user_reward_exchange_log_" . $preTime . '_' . $map['region'] . '_' . $type, function () use ($preTime, $filter) {
            return $list = $this->UserRewardLog()->setTableName($preTime, false)->setDb($this->db)->where($filter)
                ->field('*')->order('use_time desc')->select();

        }, $this->duration, true);

        if($time != $preTime){
            $list1 = Cache::getOrSet("wx_mini_user_reward_exchange_log_" . $time . '_' . $map['region'] . '_' . $type, function () use ($time, $filter) {
                return $list1 = $this->UserRewardLog()->setTableName($time, false)->setDb($this->db)->where($filter)
                    ->field('*')->order('use_time desc')->select();

            }, $this->duration, true);

            $list = array_merge($list, $list1);
        }


        //翻頁
        $page['totalCount'] = count($list);//總條數
        $pageSize           = $export == 1 ? count($list) : $pageSize;
        $page['totalPage']  = ceil($page['totalCount'] / $pageSize);//總頁數
        $offset             = ($currentPage - 1) * $pageSize;
        $page['list']       = array_slice($list, $offset, $pageSize) ?: [];
        foreach ($page['list'] as $key => &$item) {
            if (!is_array($item))
                $item = $item->toArray();
            $number                           = $item['prop_type'] == 101 ? $item['number'] / 100 : (int)$item['number'];

            $page['list'][$key]['give_time']  = $item['get_time'] ? date('Y-m-d H:i:s', $item['get_time']) : '';
            $page['list'][$key]['use_time']   = $item['use_time'] ? date('Y-m-d H:i:s', $item['use_time']) : '';
         
            $page['list'][$key]['price']      = $number . $item['unit'];
            $page['list'][$key]['prop_name']  = $item['prop_name'] . "(" . $item['prop_id'] . ")";
            $page['list'][$key]['state_name'] = $item['prop_type'] == 102 ? $this->master[$item['reward_state']] : $this->state[$item['reward_state']];

        }

        return $page;

2、使用子查詢

if($time != $preTime){

            $preSql = $this->UserRewardLog()->setTableName($time, false)->setDb($this->db)->where($filter)->fetchSql(true)
                ->field('*')->order('use_time desc')->select();
            $sql = $this->UserRewardLog()->setTableName($time, false)->setDb($this->db)->where($filter)->field('COUNT(1) AS totalCount')->fetchSql(true)->select();
            $totalCount = $this->UserRewardLog()->setTableName($preTime, false)->setDb($this->db)->where($filter)->field('COUNT(1) AS totalCount')
                ->union($sql,true)->select();
            $count = 0;
            foreach ($totalCount as $c){
                $count += $c->totalCount;
            }
            $list['totalCount'] = $count;

            $pageSize           = $export == 1 ? $list['totalCount'] : $pageSize;
            $list['list'] = $this->UserRewardLog()->setTableName($preTime, false)->setDb($this->db)->where($filter)->page($currentPage,$pageSize)->union($preSql,true)
                ->field('*')->order('use_time desc')->select();

        }else{
            $list['totalCount'] = $this->UserRewardLog()->setTableName($preTime, false)->setDb($this->db)->where($filter)->count();
            $pageSize           = $export == 1 ? $list['totalCount'] : $pageSize;
            $list['list'] = $this->UserRewardLog()->setTableName($preTime, false)->setDb($this->db)
                ->where($filter)->page($currentPage,$pageSize)
                ->field('*')->order('use_time desc')->select();
        }

注:本人使用tp5框架操作,設置表名方法已封裝過

獲取表名的封裝方法:

    /**
     * 獲取表名
     * @param $name
     * @param bool $needHash
     * @return $this
     */
    public function setTableName($name, $needHash = true)
    {
        $this->table = $this->preTable . '_' . $this->getTableSub($name, $needHash);
        $this->setTable($this->table);
        return $this;
    }

    public function getTableSub($name, $needHash = true)
    {
        if ($this->partDebug) return 0;
        if (!$needHash) return $name;
        return hexdec(substr(md5($name), 4, 1)) & ($this->partMaxNum - 1);
    }
    /**
     * 指定默認數據表名(含前綴)
     * @access public
     * @param string $table 表名
     * @return $this
     */
    public function setTable($table)
    {
        $this->table = $table;
        return $this;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章