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['cost'][$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('活动不存在'); } $currentTime = date('Y-m-d H:i:s'); if ($game['join_start_time'] > $currentTime || $game['join_end_time'] < $currentTime) { $this->error('不在报名时间'); } if (empty($params['users']) || !is_array($params['users']) || empty($params['goods_list']) || !is_array($params['goods_list'])) { $this->error('请核对报名人员'); } $join = GameJoin::where(['game_id' => $game['id'], 'user_id' => $this->auth->id])->where('status', '>', -1)->find(); if (!empty($join)) { $this->error('您已报名此活动'); } $order = GameJoin::where('game_id', $game['id'])->count(); $gender = ['man' => 0, 'woman' => 0]; $user = auth_user(); $participant = $msgs = []; 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' => ($order > $game['limit_num']) ? 0 : 1, //超过报名人数为候补 ]; if ($u['gender'] == 0) { $gender['woman'] += 1; } else { $gender['man'] += 1; } $msgs[] = [ 'type' => 1, 'name' => '系统消息', 'avatar' => '', 'from_id' => 0, 'user_id' => $u['user_id'], 'content' => json_encode([ 'msgType' => 1, 'content' => $user['nickname'] . ' 帮你报名了组球', 'game_id' => $game['id'], 'act_id' => $game['act_id'], ]) ]; } foreach ($gender as $k => $v) { if ($params['goods_list'][$k]['goods_num'] != $v) { $this->error('报名人数错误'); } } $cost = json_decode($game['cost'], true); if (!empty($cost['type']) && ($cost['type'] == 10 || $cost['type'] == 30)) { $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, 'order_id' => $order['id'], 'status' => 0, //待支付 'users' => json_encode($params['users'] ?? []) ]); } foreach ($participant as &$p) { $p['game_join_id'] = $join->id; } (new Participant)->insertAll($participant); (new Message())->insertAll($msgs); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage(), $e); } $this->success('Success', $order ?? []); } }