186 lines
6.6 KiB
PHP
186 lines
6.6 KiB
PHP
<?php
|
|
|
|
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;
|
|
use think\exception\ValidateException;
|
|
use addons\shopro\service\order\OrderCreate;
|
|
|
|
|
|
class Activity extends Base
|
|
{
|
|
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()
|
|
{
|
|
$result = false;
|
|
$params = $this->request->param();
|
|
$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'] = json_encode($params['cost'] ?? []);
|
|
Db::startTrans();
|
|
try {
|
|
$result = $this->model->allowField(true)->save($params);
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($result === false) {
|
|
$this->error('操作失败');
|
|
}
|
|
$this->success('Success');
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$result = false;
|
|
$params = $this->request->param();
|
|
$model = $this->model->get($params['id']);
|
|
if (empty($model)) {
|
|
$this->error(__('No rows were found'));
|
|
}
|
|
$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'] = 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 ($result === false) {
|
|
$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('活动报名时间未开始');
|
|
}
|
|
|
|
$this->svalidate($params, ".create");
|
|
$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['order_sn'],
|
|
'status' => 1,
|
|
'users' => json_encode($params['users'] ?? [])
|
|
]);
|
|
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage(), $e);
|
|
}
|
|
|
|
$this->success('Success', $join);
|
|
}
|
|
}
|