Files
fast/addons/shopro/controller/zy/Activity.php
xiadc be7ee40690 feat(zy): 实现比赛报名和自动分组功能
- 新增比赛报名逻辑,支持单双打和团队赛
- 实现自动分组算法,根据比赛规则生成对阵表
- 添加下一轮对阵安排功能,支持淘汰赛制
- 优化比赛结果处理,自动计算排名和得分
- 新增参赛人员列表接口,支持多种查询条件
2025-05-30 16:29:57 +08:00

262 lines
10 KiB
PHP

<?php
namespace addons\shopro\controller\zy;
use think\Db;
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\shopro\Category;
use app\admin\model\zy\game\GameJoin;
use think\exception\ValidateException;
use app\admin\model\shopro\goods\Goods;
use app\admin\model\zy\game\Participant;
use addons\shopro\service\order\OrderCreate;
use app\admin\controller\shopro\traits\SkuPrice;
class Activity extends Base
{
use SkuPrice;
protected $noNeedLogin = ['index', 'test'];
public function __construct()
{
$this->model = new \app\admin\model\zy\game\Activity;
parent::__construct();
}
public function index()
{
$params = $this->request->param();
$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'])) {
$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'])) {
$query->where('a.club_id', $params['club_id']);
}
if (isset($params['week'])) {
$query->where('week', $params['week']);
}
if (isset($params['pid'])) {
$query->where('pid', $params['pid']);
} else {
$query->where('pid', 0);
}
$res = $query->paginate($params['pageSize'] ?? 10);
$list = $res->items();
foreach ($list as &$v) {
$v['public_time'] = json_decode($v['public_time'] ?? '[]', true);
$v['join_start_time'] = json_decode($v['join_start_time'] ?? '[]', true);
$v['join_end_time'] = json_decode($v['join_end_time'] ?? '[]', true);
$v['quit_time'] = json_decode($v['quit_time'] ?? '[]', true);
$v['cost'] = json_decode($v['cost'] ?? '[]', true);
$v['referee'] = explode(',', $v['referee'] ?? '');
}
$this->success('Success', ['list' => $list, 'count' => $res->total()]);
}
public function view()
{
$model = $this->model->get($this->request->param('id'));
if (empty($model)) {
$this->error(__('No rows were found'));
}
$model['public_time'] = json_decode($model['public_time'] ?? '[]', true);
$model['join_start_time'] = json_decode($model['join_start_time'] ?? '[]', true);
$model['join_end_time'] = json_decode($model['join_end_time'] ?? '[]', true);
$model['quit_time'] = json_decode($model['quit_time'] ?? '[]', true);
$model['cost'] = json_decode($model['cost'] ?? '[]', true);
$model['referee'] = explode(',', $model['referee'] ?? '');
$this->success('Success', $model);
}
public function add()
{
$params = $this->request->param();
Db::startTrans();
try {
$category = new Category;
$category->name = $params['name'];
$category->parent_id = 157;
$category->description = '报名费商品';
$category->save();
foreach ($this->model->costKey as $key => $val) {
if (isset($params['cost'][$key])) {
$goods = new Goods;
$goods->category_ids = $category->id;
$goods->subtitle = $key;
$goods->title = $val;
$goods->type = 'virtual';
$goods->limit_type = 'none';
$goods->dispatch_type = 'autosend';
$goods->dispatch_id = 2;
$goods->is_sku = 0;
$goods->original_price = $params['price'][$key];
$goods->price = $params['cost'][$key];
$goods->save();
$this->zySku($goods, 'add');
} else {
throw new Exception('请填写' . $val);
}
}
$params['public_time'] = json_encode($params['public_time'] ?? []);
$params['join_start_time'] = json_encode($params['join_start_time'] ?? []);
$params['join_end_time'] = json_encode($params['join_end_time'] ?? []);
$params['quit_time'] = json_encode($params['quit_time'] ?? []);
$params['cost']['goods_category_id'] = $category->id;
$params['cost'] = json_encode($params['cost'] ?? []);
$result = $this->model->allowField(true)->save($params);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (empty($result)) {
$this->error('操作失败');
}
$this->success('Success');
}
public function update()
{
$params = $this->request->param();
$model = $this->model->get($params['id']);
if (empty($model)) {
$this->error(__('No rows were found'));
}
$cost = json_decode($model->cost, true);
$goods = Goods::where('category_ids', $cost['goods_category_id'])->select();
foreach ($this->model->costKey as $key => $val) {
if (isset($params['cost'][$key])) {
foreach ($goods as $g) { //更新报名费价格
if ($g['title'] == $val && $g['price'] != $params['cost'][$key]) {
$g->save(['price' => $params['cost'][$key]]);
$this->zySku($g, 'edit');
}
}
} else {
throw new Exception('请填写' . $val);
}
}
$params['public_time'] = json_encode($params['public_time'] ?? []);
$params['join_start_time'] = json_encode($params['join_start_time'] ?? []);
$params['join_end_time'] = json_encode($params['join_end_time'] ?? []);
$params['quit_time'] = json_encode($params['quit_time'] ?? []);
$params['cost']['goods_category_id'] = $cost['goods_category_id'];
$params['cost'] = json_encode($params['cost'] ?? []);
Db::startTrans();
try {
$result = $model->allowField(true)->save($params);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (empty($result)) {
$this->error('操作失败');
}
$this->success('Success');
}
// 启动周期性活动创建比赛任务
public function test()
{
if (!\think\Cache::has('addons\shopro\job\Test@zy')) {
$res = \think\Queue::push('\addons\shopro\job\Test@zy', time(), 'shopro');
\think\Cache::set('addons\shopro\job\Test@zy', $res);
$this->success('Success', $res);
}
$this->success('Success', '任务已存在');
}
// 比赛报名
public function gameJoin()
{
Db::startTrans();
try {
$params = $this->request->param();
if (isset($params['game_id'])) {
$game = Game::get($params['game_id']);
} else {
$game = Game::where('id', $params['act_id'] ?? NULL)
->where('week', $params['week'] ?? NULL)
->where('date', $params['date'] ?? NULL)
->find();
}
if (empty($game)) {
$this->error('活动不存在');
}
if ($game['join_start_time'] > date('Y-m-d H:i:s')) {
$this->error('活动报名时间未开始');
}
if (empty($params['users']) || !is_array($params['users']) || empty($params['goods_list']) || !is_array($params['goods_list'])) {
$this->error('请核对报名人员');
}
if (GameJoin::get(['game_id' => $game['id'], 'act_id' => $game['act_id'], 'user_id' => $this->auth->id])) {
$this->error('您已报名此活动');
}
$order = GameJoin::where('game_id', $game['id'])->count();
$gender = ['man' => 0, 'woman' => 0];
$participant = [];
foreach ($params['users'] as $u) {
$order++;
$participant[] = [
'user_id' => $u['user_id'],
'gender' => $u['gender'],
'avatar' => $u['avatar'],
'name' => $u['nickname'],
'game_id' => $game['id'],
'signin' => 0,
'order' => $order,
'status' => 1,
];
if ($u['gender'] == 0) {
$gender['woman'] += 1;
} else {
$gender['man'] += 1;
}
}
foreach ($gender as $k => $v) {
if ($params['goods_list'][$k]['goods_num'] != $v) {
$this->error('报名人数错误');
}
}
$orderCreate = new OrderCreate($params);
$result = $orderCreate->calc('create');
$order = $orderCreate->create($result);
$join = new GameJoin;
$join->allowField(true)->save([
'act_id' => $game['act_id'],
'game_id' => $game['id'],
'user_id' => $this->auth->id,
'orer_id' => $order['id'],
'status' => 0,//待支付
'users' => json_encode($params['users'] ?? [])
]);
foreach ($participant as &$p) {
$p['game_join_id'] = $join->id;
}
(new Participant)->insertAll($participant);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage(), $e);
}
$this->success('Success');
}
}