<?php
namespace common\components;

use common\helpers\Tool;
use Yii;
use yii\data\Pagination;

/**
 * Base active record class for models
 * @package common\components
 */
class MongoActiveRecord extends \yii\mongodb\ActiveRecord {
    /**
     * Query对象
     * @var null
     */
    public static $query = null;
    /**
     * @var array
     */
    public static $argv = [];
    /**
     * 构造器
     *
     * @access public
     */
    public function init(){
        parent::init();
    }

    /**
     * 批量写入数据
     * @param $field
     * @param $data
     * @return mixed
     */
    public static function batchInsert($field, $data){
        $db = static::getDb();
        $query = $db->createCommand()->batchInsert(static::collectionName(), $field, $data);
        return $query->execute();
    }

    /**
     * @param string $condition
     * @param array $argv
     * @throws \yii\base\InvalidConfigException
     */
    public static function prepare($condition = [], $argv = []){
        $default = [
            'page' => null,
            'pageSize'=>0, //10
            'orderBy'=>'_id desc',
            'select'=>null,
            'asArray'=>true,
            'validatePage'=>true,
            'filter' => null,
            'indexBy' => null,
            'count' => '*',
        ];
        self::$argv = Tool::deepParse($argv ,$default);
        if (!self::$argv['pageSize']) {
            $pageSize = \Yii::$app->request->all('pageSize', \Yii::$app->params['pageSize']);
            self::$argv['pageSize'] = $pageSize;
        }
        self::$query = static::find();
        if(self::$argv['select']){
            self::$query->select(self::$argv['select']);
        }
        if(self::$argv['indexBy']){
            self::$query->indexBy(self::$argv['indexBy']);
        }
        if(self::$argv['asArray']){
            self::$query->asArray();
        }
        self::$query->orderBy(self::$argv['orderBy'])->where($condition);
        unset($condition, $argv, $default);
    }

    /**
     * 获取列表
     * @param array $condition
     * @param array $argv
     * @return array
     * @throws \yii\base\InvalidConfigException
     */
    public static function  lists($condition = [], $argv = []) {
        self::prepare($condition, $argv);
        $countQuery = clone self::$query;
        $count = $countQuery->count(self::$argv['count']); // 得到总数
        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->setPageSize(self::$argv['pageSize']);
        if(self::$argv['page'] !== null){
            $pagination->setPage(self::$argv['page']);
        }
        self::$query->offset($pagination->offset)->limit($pagination->limit);
        if(self::$argv['filter']){
            $lists = self::$query->filter(self::$argv['filter'])->all();
        }else{
            $lists = self::$query->all();
        }
        self::$query = null;
        unset($pageParams, $countQuery);
        $startNum = $pagination->page * $pagination->pageSize + 1;
        return [
            'list' => $lists ? $lists : [],
            'pagination' => $pagination,
            'currentPage'=>$pagination->page,
            'totalPages'=>$pagination->pageCount,
            'startNum' => $startNum,
            'totalCount' => $pagination->totalCount,
            'pageSize' => $pagination->pageSize,
        ];
    }
}