- 新增报名费商品类别和商品创建逻辑 - 实现报名费价格动态更新 - 添加活动报名功能 - 优化 SkuPrice trait 中的 sku 处理逻辑 - 修复活动列表和详情页面的一些小问题
231 lines
8.8 KiB
PHP
231 lines
8.8 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 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('您已报名此活动');
|
|
}
|
|
$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' => 1,
|
|
'users' => json_encode($params['users'] ?? [])
|
|
]);
|
|
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage(), $e);
|
|
}
|
|
|
|
$this->success('Success');
|
|
}
|
|
}
|