- 重构了 Activity、Circle、Club、Game 和 Gym 控制器中的查询方法 - 添加了分页功能,支持指定页码和每页数量 - 优化了查询结果,返回包含总数的格式化数据 - 使用 alias 和 join 方法改进了查询效率 - 删除了 Base 控制器中的通用查询方法
70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\controller\zy;
|
|
|
|
use app\admin\model\zy\Club;
|
|
use app\admin\model\zy\Stadium;
|
|
|
|
|
|
class Game extends Base
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->model = new \app\admin\model\zy\game\Game;
|
|
parent::__construct();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$params = $this->request->param();
|
|
$query = $this->model->alias('g')
|
|
->join([Stadium::$tableName => 's'], 's.id = g.gym_id', 'LEFT')
|
|
->join([Club::$tableName => 'c'], 'c.id = g.club_id', 'LEFT')
|
|
->field('g.*, s.name as gym_name, c.name as club_name');
|
|
if (isset($params['name'])) {
|
|
$query->where('g.name', 'like', '%' . $params['name'] . '%');
|
|
}
|
|
if (isset($params['club_id'])) {
|
|
$query->where('g.club_id', $params['club_id']);
|
|
}
|
|
if (isset($params['week'])) {
|
|
$query->where('g.week', $params['week']);
|
|
}
|
|
if (isset($params['pid'])) {
|
|
$query->where('pid', $params['pid']);
|
|
} else {
|
|
$query->where('pid', 0);
|
|
}
|
|
if (isset($params['public_time'])) {
|
|
$query->where('public_time', $params['public_time']);
|
|
} else {
|
|
$query->where('public_time', '<=', date('Y-m-d H:i:s'));
|
|
}
|
|
if (isset($params['page'])) {
|
|
$pageSize = intval($params['pageSize'] ?? 10);
|
|
$offeset = (intval($params['page']) - 1) * $pageSize;
|
|
$query->limit($offeset, $pageSize);
|
|
}
|
|
$res = $query->select();
|
|
foreach ($res as &$v) {
|
|
$v['cost'] = json_decode($v['cost'] ?? '[]', true);
|
|
$v['referee'] = explode(',', $v['referee']);
|
|
}
|
|
|
|
$this->success('Success', ['list' => $res, 'count' => $query->count()]);
|
|
}
|
|
|
|
public function view()
|
|
{
|
|
$model = $this->model->get($this->request->param('id'));
|
|
if (empty($model)) {
|
|
$this->error(__('No rows were found'));
|
|
}
|
|
|
|
$model['cost'] = json_decode($model['cost'] ?? '[]', true);
|
|
$model['referee'] = explode(',', $model['referee']);
|
|
|
|
$this->success('Success', $model);
|
|
}
|
|
}
|