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\Db;
|
||||||
use think\Cache;
|
use think\Cache;
|
||||||
use think\Exception;
|
use think\Exception;
|
||||||
|
use app\admin\model\zy\Club;
|
||||||
|
use app\admin\model\zy\Stadium;
|
||||||
use app\admin\model\zy\game\Game;
|
use app\admin\model\zy\game\Game;
|
||||||
use think\exception\PDOException;
|
use think\exception\PDOException;
|
||||||
use app\admin\model\zy\game\GameJoin;
|
use app\admin\model\zy\game\GameJoin;
|
||||||
@@ -24,23 +26,33 @@ class Activity extends Base
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$params = $this->request->param();
|
$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'])) {
|
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'])) {
|
if (isset($params['club_id'])) {
|
||||||
$model->where('club_id', $params['club_id']);
|
$query->where('a.club_id', $params['club_id']);
|
||||||
}
|
}
|
||||||
if (isset($params['week'])) {
|
if (isset($params['week'])) {
|
||||||
$model->where('week', $params['week']);
|
$query->where('week', $params['week']);
|
||||||
}
|
}
|
||||||
if (isset($params['pid'])) {
|
if (isset($params['pid'])) {
|
||||||
$model->where('pid', $params['pid']);
|
$query->where('pid', $params['pid']);
|
||||||
} else {
|
} else {
|
||||||
$model->where('pid', 0);
|
$query->where('pid', 0);
|
||||||
}
|
}
|
||||||
|
if (isset($params['page'])) {
|
||||||
$res = $model->select();
|
$pageSize = intval($params['pageSize'] ?? 10);
|
||||||
|
$offeset = (intval($params['page']) - 1) * $pageSize;
|
||||||
|
$query->limit($offeset, $pageSize);
|
||||||
|
}
|
||||||
|
$res = $query->select();
|
||||||
foreach ($res as &$v) {
|
foreach ($res as &$v) {
|
||||||
$v['public_time'] = json_decode($v['public_time'] ?? '[]', true);
|
$v['public_time'] = json_decode($v['public_time'] ?? '[]', true);
|
||||||
$v['join_start_time'] = json_decode($v['join_start_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']);
|
$v['referee'] = explode(',', $v['referee']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->success('Success', $res);
|
$this->success('Success', ['list' => $res, 'count' => $query->count()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function view()
|
public function view()
|
||||||
|
|||||||
@@ -22,19 +22,6 @@ class Base extends Common
|
|||||||
$this->user = auth_user();
|
$this->user = auth_user();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function index()
|
|
||||||
{
|
|
||||||
$params = $this->request->param();
|
|
||||||
if (isset($params['name'])) {
|
|
||||||
$this->model->where('name', 'like', '%' . $params['name'] . '%');
|
|
||||||
}
|
|
||||||
$res = $this->model->select();
|
|
||||||
|
|
||||||
|
|
||||||
$this->success('Success', $res);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function add()
|
public function add()
|
||||||
{
|
{
|
||||||
$result = false;
|
$result = false;
|
||||||
|
|||||||
@@ -22,17 +22,17 @@ class Circle extends Base
|
|||||||
|
|
||||||
$sub = CircleModel::alias('c')
|
$sub = CircleModel::alias('c')
|
||||||
->join([Likes::$tableName => 'l'], 'c.id = l.circle_id', 'LEFT')
|
->join([Likes::$tableName => 'l'], 'c.id = l.circle_id', 'LEFT')
|
||||||
->field("c.*,CONCAT('[',GROUP_CONCAT(JSON_OBJECT(
|
->field("c.*,JSON_ARRAYAGG(JSON_OBJECT(
|
||||||
'id', l.id,
|
'id', l.id,
|
||||||
'user_id', l.user_id,
|
'user_id', l.user_id,
|
||||||
'nickname', l.nickname,
|
'nickname', l.nickname,
|
||||||
'avatar', l.avatar,
|
'avatar', l.avatar,
|
||||||
'gender', l.gender
|
'gender', l.gender
|
||||||
)), ']') AS likes")->group('c.id')->buildSql();
|
)) AS likes")->group('c.id')->buildSql();
|
||||||
$query = Comment::alias('m')
|
$query = Comment::alias('m')
|
||||||
->join([$sub => 'c'], 'c.id = m.circle_id', 'RIGHT')
|
->join([$sub => 'c'], 'c.id = m.circle_id', 'RIGHT')
|
||||||
->field("c.*,
|
->field("c.*,
|
||||||
CONCAT('[',GROUP_CONCAT(JSON_OBJECT(
|
JSON_ARRAYAGG(JSON_OBJECT(
|
||||||
'id', m.id,
|
'id', m.id,
|
||||||
'pid', m.pid,
|
'pid', m.pid,
|
||||||
'puser_id', m.puser_id,
|
'puser_id', m.puser_id,
|
||||||
@@ -43,7 +43,7 @@ class Circle extends Base
|
|||||||
'gender', m.gender,
|
'gender', m.gender,
|
||||||
'content', m.content,
|
'content', m.content,
|
||||||
'create_time', m.create_time
|
'create_time', m.create_time
|
||||||
)), ']') AS comment")->group('c.id');
|
)) AS comment")->group('c.id');
|
||||||
if (isset($params['club_id'])) {
|
if (isset($params['club_id'])) {
|
||||||
$query->where('c.club_id', $params['club_id']);
|
$query->where('c.club_id', $params['club_id']);
|
||||||
}
|
}
|
||||||
@@ -54,6 +54,11 @@ class Circle extends Base
|
|||||||
$friend = Relation::where('user_id', $this->auth->id)->where('status', 'IN', explode(',', $params['friend']))->column('target_id');
|
$friend = Relation::where('user_id', $this->auth->id)->where('status', 'IN', explode(',', $params['friend']))->column('target_id');
|
||||||
$query->where('c.user_id', 'IN', $friend);
|
$query->where('c.user_id', 'IN', $friend);
|
||||||
}
|
}
|
||||||
|
if (isset($params['page'])) {
|
||||||
|
$pageSize = intval($params['pageSize'] ?? 10);
|
||||||
|
$offeset = (intval($params['page']) - 1) * $pageSize;
|
||||||
|
$query->limit($offeset, $pageSize);
|
||||||
|
}
|
||||||
$res = $query->select();
|
$res = $query->select();
|
||||||
foreach ($res as &$r) {
|
foreach ($res as &$r) {
|
||||||
$r['likes'] = json_decode($r['likes'], true);
|
$r['likes'] = json_decode($r['likes'], true);
|
||||||
@@ -61,7 +66,7 @@ class Circle extends Base
|
|||||||
$r['comment'] = buildTree(json_decode($r['comment'], true));
|
$r['comment'] = buildTree(json_decode($r['comment'], true));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->success('Success', $res);
|
$this->success('Success', ['list' => $res, 'count' => $query->count()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function view()
|
public function view()
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace addons\shopro\controller\zy;
|
|||||||
use think\Db;
|
use think\Db;
|
||||||
use think\Exception;
|
use think\Exception;
|
||||||
use app\admin\model\zy\Menber;
|
use app\admin\model\zy\Menber;
|
||||||
|
use app\admin\model\zy\Stadium;
|
||||||
use think\exception\PDOException;
|
use think\exception\PDOException;
|
||||||
use app\admin\model\zy\link\Apply;
|
use app\admin\model\zy\link\Apply;
|
||||||
use app\admin\model\zy\link\Message;
|
use app\admin\model\zy\link\Message;
|
||||||
@@ -23,16 +24,23 @@ class Club extends Base
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$params = $this->request->param();
|
$params = $this->request->param();
|
||||||
$model = $this->model;
|
$query = $this->model->alias('c')
|
||||||
|
->join([Stadium::$tableName => 's'], 's.id = c.gym_id', 'LEFT')
|
||||||
|
->field('c.*, s.name as gym_name');
|
||||||
if (isset($params['name'])) {
|
if (isset($params['name'])) {
|
||||||
$model->where('name', 'like', '%' . $params['name'] . '%');
|
$query->where('c.name', 'like', '%' . $params['name'] . '%');
|
||||||
}
|
}
|
||||||
if (isset($params['gym_id'])) {
|
if (isset($params['gym_id'])) {
|
||||||
$model->where('gym_id', $params['gym_id']);
|
$query->where('c.gym_id', $params['gym_id']);
|
||||||
}
|
}
|
||||||
$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();
|
||||||
|
|
||||||
$this->success('Success', $res);
|
$this->success('Success', ['list' => $res, 'count' => $query->count()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add()
|
public function add()
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
namespace addons\shopro\controller\zy;
|
namespace addons\shopro\controller\zy;
|
||||||
|
|
||||||
|
use app\admin\model\zy\Club;
|
||||||
|
use app\admin\model\zy\Stadium;
|
||||||
|
|
||||||
|
|
||||||
class Game extends Base
|
class Game extends Base
|
||||||
{
|
{
|
||||||
@@ -14,35 +17,41 @@ class Game extends Base
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$params = $this->request->param();
|
$params = $this->request->param();
|
||||||
$model = $this->model;
|
$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'])) {
|
if (isset($params['name'])) {
|
||||||
$model->where('name', 'like', '%' . $params['name'] . '%');
|
$query->where('g.name', 'like', '%' . $params['name'] . '%');
|
||||||
}
|
}
|
||||||
if (isset($params['club_id'])) {
|
if (isset($params['club_id'])) {
|
||||||
$model->where('club_id', $params['club_id']);
|
$query->where('g.club_id', $params['club_id']);
|
||||||
}
|
}
|
||||||
if (isset($params['week'])) {
|
if (isset($params['week'])) {
|
||||||
$model->where('week', $params['week']);
|
$query->where('g.week', $params['week']);
|
||||||
}
|
}
|
||||||
if (isset($params['pid'])) {
|
if (isset($params['pid'])) {
|
||||||
$model->where('pid', $params['pid']);
|
$query->where('pid', $params['pid']);
|
||||||
} else {
|
} else {
|
||||||
$model->where('pid', 0);
|
$query->where('pid', 0);
|
||||||
}
|
}
|
||||||
if (isset($params['public_time'])) {
|
if (isset($params['public_time'])) {
|
||||||
$model->where('public_time', $params['public_time']);
|
$query->where('public_time', $params['public_time']);
|
||||||
} else {
|
} else {
|
||||||
$model->where('public_time', '<=', date('Y-m-d H:i:s'));
|
$query->where('public_time', '<=', date('Y-m-d H:i:s'));
|
||||||
}
|
}
|
||||||
|
if (isset($params['page'])) {
|
||||||
$res = $model->select();
|
$pageSize = intval($params['pageSize'] ?? 10);
|
||||||
|
$offeset = (intval($params['page']) - 1) * $pageSize;
|
||||||
|
$query->limit($offeset, $pageSize);
|
||||||
|
}
|
||||||
|
$res = $query->select();
|
||||||
foreach ($res as &$v) {
|
foreach ($res as &$v) {
|
||||||
|
|
||||||
$v['cost'] = json_decode($v['cost'] ?? '[]', true);
|
$v['cost'] = json_decode($v['cost'] ?? '[]', true);
|
||||||
$v['referee'] = explode(',', $v['referee']);
|
$v['referee'] = explode(',', $v['referee']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->success('Success', $res);
|
$this->success('Success', ['list' => $res, 'count' => $query->count()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function view()
|
public function view()
|
||||||
|
|||||||
@@ -17,13 +17,18 @@ class Gym extends Base
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$params = $this->request->param();
|
$params = $this->request->param();
|
||||||
$model = $this->model->where('status', 1);
|
$query = $this->model->where('status', 1);
|
||||||
if (isset($params['name'])) {
|
if (isset($params['name'])) {
|
||||||
$model->where('name', 'like', '%' . $params['name'] . '%');
|
$query->where('name', 'like', '%' . $params['name'] . '%');
|
||||||
}
|
}
|
||||||
$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();
|
||||||
|
|
||||||
|
|
||||||
$this->success('Success', $res);
|
$this->success('Success', ['list' => $res, 'count' => $query->count()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class Stadium extends Model
|
|||||||
|
|
||||||
|
|
||||||
// 表名
|
// 表名
|
||||||
|
public static $tableName = 'zy_stadium';
|
||||||
protected $table = 'zy_stadium';
|
protected $table = 'zy_stadium';
|
||||||
|
|
||||||
// 自动写入时间戳字段
|
// 自动写入时间戳字段
|
||||||
|
|||||||
Reference in New Issue
Block a user