refactor(zy): 重构查询接口并添加分页功能
- 重构了 Activity、Circle、Club、Game 和 Gym 控制器中的查询方法 - 添加了分页功能,支持指定页码和每页数量 - 优化了查询结果,返回包含总数的格式化数据 - 使用 alias 和 join 方法改进了查询效率 - 删除了 Base 控制器中的通用查询方法
This commit is contained in:
@@ -5,6 +5,8 @@ namespace addons\shopro\controller\zy;
|
||||
use think\Db;
|
||||
use think\Cache;
|
||||
use think\Exception;
|
||||
use app\admin\model\zy\Club;
|
||||
use app\admin\model\zy\Stadium;
|
||||
use app\admin\model\zy\game\Game;
|
||||
use think\exception\PDOException;
|
||||
use app\admin\model\zy\game\GameJoin;
|
||||
@@ -24,23 +26,33 @@ class Activity extends Base
|
||||
public function index()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
$model = $this->model;
|
||||
$query = $this->model->alias('a')
|
||||
->join([Stadium::$tableName => 's'], 's.id = a.gym_id', 'LEFT')
|
||||
->join([Club::$tableName => 'c'], 'c.id = a.club_id', 'LEFT')
|
||||
->field('a.*, s.name as gym_name, c.name as club_name');
|
||||
if (isset($params['name'])) {
|
||||
$model->where('name', 'like', '%' . $params['name'] . '%');
|
||||
$query->where('a.name', 'like', '%' . $params['name'] . '%');
|
||||
}
|
||||
if (isset($params['gym_id'])) {
|
||||
$query->where('a.gym_id', $params['gym_id']);
|
||||
}
|
||||
if (isset($params['club_id'])) {
|
||||
$model->where('club_id', $params['club_id']);
|
||||
$query->where('a.club_id', $params['club_id']);
|
||||
}
|
||||
if (isset($params['week'])) {
|
||||
$model->where('week', $params['week']);
|
||||
$query->where('week', $params['week']);
|
||||
}
|
||||
if (isset($params['pid'])) {
|
||||
$model->where('pid', $params['pid']);
|
||||
$query->where('pid', $params['pid']);
|
||||
} else {
|
||||
$model->where('pid', 0);
|
||||
$query->where('pid', 0);
|
||||
}
|
||||
|
||||
$res = $model->select();
|
||||
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['public_time'] = json_decode($v['public_time'] ?? '[]', true);
|
||||
$v['join_start_time'] = json_decode($v['join_start_time'] ?? '[]', true);
|
||||
@@ -50,7 +62,7 @@ class Activity extends Base
|
||||
$v['referee'] = explode(',', $v['referee']);
|
||||
}
|
||||
|
||||
$this->success('Success', $res);
|
||||
$this->success('Success', ['list' => $res, 'count' => $query->count()]);
|
||||
}
|
||||
|
||||
public function view()
|
||||
|
||||
Reference in New Issue
Block a user