cxgj/models/QueryListModel.php
2023-11-27 09:45:13 +08:00

129 lines
2.7 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年11月1日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models;
use yii\data\Pagination;
class QueryListModel extends Model
{
private $limit;
private $page;
private $query;
private $field;
private $sort;
private $count;
private $pagination;
private $list;
protected function setLimit($limit)
{
$this->limit = $limit;
}
protected function getLimit()
{
return $this->limit;
}
protected function setPage($page)
{
$this->page = $page;
}
protected function getPage()
{
return $this->page;
}
protected function setQuery($query)
{
$this->query = $query;
}
protected function getQuery()
{
return $this->query;
}
protected function setFiled($filed)
{
$this->field = $filed;
}
protected function getField()
{
return $this->field;
}
protected function setSort($sort)
{
$this->sort = $sort;
}
protected function getSort()
{
return $this->sort;
}
private function setCount()
{
$count_query = clone $this->query;
$this->count = $count_query->count();
}
protected function getCount()
{
return $this->count;
}
private function setPagination()
{
$this->pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $this->count, 'page' => $this->page - 1]);
}
protected function getList()
{
return $this->list;
}
protected function buildQuery($asArray = true, $usePagination = true)
{
$query = $this->query;
$query = $query->select($this->field);
$this->setCount();
if($usePagination){
$this->setPagination();
$query = $query->offset($this->pagination->offset)
->limit($this->pagination->limit);
}
$list = $query->orderBy($this->sort);
$this->list = $asArray ? $list->asArray()->all() : $list->all();
}
protected function formatData($data)
{
return [
'code' => 0,
'msg' => 'ok',
'data' => $data,
'count' => $this->count,
'page_size' => $this->limit,
'page_count' => $this->pagination->pageCount,
'page_no' => $this->page,
'end_flag' => $this->page > $this->pagination->pageCount ? true : false
];
}
}