feat(俱乐部和比赛模块): 增加俱乐部和比赛的相关功能和优化

- 在 Club 控制器中添加了对俱乐部列表的查询,包括俱乐部名称、场馆名称、俱乐部标签等信息
- 在 Game 控制器中增加了对比赛列表的查询,包括比赛名称、场馆名称、俱乐部名称、参赛选手头像等信息
- 优化了 Game 控制器中的比赛详情查询,增加了参赛选手头像的解析
- 在 Game、GameJoin 和 Participant 模型中添加了 tableName 静态属性,便于统一管理表名
This commit is contained in:
2025-05-16 11:16:29 +08:00
parent 8a68fb84af
commit 751c742726
5 changed files with 44 additions and 4 deletions

View File

@@ -6,12 +6,14 @@ 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 app\admin\model\zy\Stadium;
use app\admin\model\zy\game\Game;
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;
use app\admin\model\shopro\user\User; use app\admin\model\shopro\user\User;
use app\admin\model\zy\game\Activity; use app\admin\model\zy\game\Activity;
use think\exception\ValidateException; use think\exception\ValidateException;
use app\admin\model\zy\game\Participant;
class Club extends Base class Club extends Base
{ {
@@ -24,17 +26,49 @@ class Club extends Base
public function index() public function index()
{ {
$params = $this->request->param(); $params = $this->request->param();
$sub = Game::alias('g')
->join([Participant::$tableName => 'p'], 'p.game_id=g.id', 'LEFT')
->field('g.id,g.pid,g.club_id,g.act_id,g.date,g.name,g.start_time,g.end_time,g.cost,JSON_ARRAYAGG(p.avatar) as avatar,count(p.id) as join_num')
->where('g.date', '>=', date('Y-m-d'))
->order('g.date', 'asc')->group('g.id')->buildSql();
$query = $this->model->alias('c') $query = $this->model->alias('c')
->join([Stadium::$tableName => 's'], 's.id = c.gym_id', 'LEFT') ->join([Stadium::$tableName => 's'], 's.id = c.gym_id', 'LEFT')
->field('c.*, s.name as gym_name'); ->join([$sub => 'g'], 'g.club_id = c.id', 'LEFT')
->field("c.*, s.position,s.name as gym_name,JSON_ARRAYAGG(JSON_OBJECT(
'id', g.id,
'pid', g.pid,
'act_id', g.act_id,
'date', g.date,
'name', g.name,
'start_time', g.start_time,
'end_time', g.end_time,
'join_num', g.join_num,
'avatar', g.avatar,
'cost', g.cost
)) AS games")->group('c.id');
if (isset($params['name'])) { if (isset($params['name'])) {
$query->where('c.name', 'like', '%' . $params['name'] . '%'); $query->where('c.name', 'like', '%' . $params['name'] . '%');
} }
if (isset($params['tag'])) {
$query->where('c.tags', 'like', '%' . $params['tag'] . '%');
}
if (isset($params['gym_id'])) { if (isset($params['gym_id'])) {
$query->where('c.gym_id', $params['gym_id']); $query->where('c.gym_id', $params['gym_id']);
} }
if (isset($params['order'])) {
$query->order($params['order'], $params['sort'] ?? NULL);
}
$res = $query->paginate($params['pageSize'] ?? 10); $res = $query->paginate($params['pageSize'] ?? 10);
$this->success('Success', ['list' => $res->items(), 'count' => $res->total()]); $list = $res->items();
foreach ($list as &$l) {
$games = json_decode($l['games'], true);
if (count($games) == 1 && empty($games[0]['id'])) $games = [];
foreach ($games as &$g) {
$g['cost'] = json_decode($g['cost'], true);
}
$l['games'] = $games;
}
$this->success('Success', ['list' => $list, 'count' => $res->total()]);
} }
public function add() public function add()

View File

@@ -4,6 +4,7 @@ namespace addons\shopro\controller\zy;
use app\admin\model\zy\Club; use app\admin\model\zy\Club;
use app\admin\model\zy\Stadium; use app\admin\model\zy\Stadium;
use app\admin\model\zy\game\Participant;
class Game extends Base class Game extends Base
@@ -18,9 +19,10 @@ class Game extends Base
{ {
$params = $this->request->param(); $params = $this->request->param();
$query = $this->model->alias('g') $query = $this->model->alias('g')
->join([Participant::$tableName => 'p'], 'p.game_id=g.id', 'LEFT')
->join([Stadium::$tableName => 's'], 's.id = g.gym_id', 'LEFT') ->join([Stadium::$tableName => 's'], 's.id = g.gym_id', 'LEFT')
->join([Club::$tableName => 'c'], 'c.id = g.club_id', 'LEFT') ->join([Club::$tableName => 'c'], 'c.id = g.club_id', 'LEFT')
->field('g.*, s.name as gym_name, c.name as club_name'); ->field('g.*, s.name as gym_name, c.name as club_name,JSON_ARRAYAGG(p.avatar) as avatar,count(p.id) as join_num');
if (isset($params['name'])) { if (isset($params['name'])) {
$query->where('g.name', 'like', '%' . $params['name'] . '%'); $query->where('g.name', 'like', '%' . $params['name'] . '%');
} }
@@ -49,6 +51,7 @@ class Game extends Base
$list = $res->items(); $list = $res->items();
foreach ($list as &$v) { foreach ($list as &$v) {
$v['cost'] = json_decode($v['cost'] ?? '[]', true); $v['cost'] = json_decode($v['cost'] ?? '[]', true);
$v['avatar'] = json_decode($v['avatar'] ?? '[]', true);
} }
$this->success('Success', ['list' => $list, 'count' => $res->total()]); $this->success('Success', ['list' => $list, 'count' => $res->total()]);
} }

View File

@@ -13,6 +13,7 @@ class Game extends Model
// 表名 // 表名
public static $tableName = 'zy_game';
protected $table = 'zy_game'; protected $table = 'zy_game';
// 自动写入时间戳字段 // 自动写入时间戳字段

View File

@@ -13,6 +13,7 @@ class GameJoin extends Model
// 表名 // 表名
public static $tableName = 'zy_game_join';
protected $table = 'zy_game_join'; protected $table = 'zy_game_join';
// 自动写入时间戳字段 // 自动写入时间戳字段

View File

@@ -13,6 +13,7 @@ class Participant extends Model
// 表名 // 表名
public static $tableName = 'zy_participant';
protected $table = 'zy_participant'; protected $table = 'zy_participant';
// 自动写入时间戳字段 // 自动写入时间戳字段