232 lines
6.0 KiB
PHP
232 lines
6.0 KiB
PHP
<?php
|
|
/**
|
|
* @Created by PhpStorm.
|
|
* @Author:Soar
|
|
* @Time:2023/10/8 13:58
|
|
*/
|
|
|
|
namespace app\service;
|
|
|
|
use app\admin\model\cms\Archives;
|
|
use app\common\model\MatchContestant;
|
|
use think\Cache;
|
|
|
|
class MatchService
|
|
{
|
|
protected int $match_id;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->matchModel = new Archives();
|
|
$this->matchContestantModel = new MatchContestant();
|
|
$this->addonproductModel = db("cms_addonproduct");
|
|
}
|
|
|
|
/**
|
|
* 获取赛事信息开启缓存
|
|
* @Author:Soar
|
|
* @Time:2023/10/8 15:33
|
|
* @return array|\think\response\Json
|
|
*/
|
|
public function get_match()
|
|
{
|
|
$match = $this->matchModel::get($this->match_id, [], true);
|
|
$addon = $this->addonproductModel->where('id', $this->match_id)->find();
|
|
if ($addon) {
|
|
$match->setData($addon);
|
|
}
|
|
|
|
try {
|
|
$match = $match->toArray();
|
|
} catch (\Exception $exception) {
|
|
$message = $exception->getMessage();
|
|
|
|
return $this->result([], $message, 102);
|
|
|
|
}
|
|
|
|
return $this->result($match);
|
|
}
|
|
|
|
/**
|
|
* 获取赛事信息禁用缓存
|
|
* @Author:Soar
|
|
* @Time:2023/10/8 15:33
|
|
* @return array|\think\response\Json
|
|
*/
|
|
public function get_line_match()
|
|
{
|
|
$match = $this->matchModel::get($this->match_id, [], false);
|
|
$addon = $this->addonproductModel->where('id', $this->match_id)->find();
|
|
if ($addon) {
|
|
$match->setData($addon);
|
|
}
|
|
|
|
try {
|
|
$match = $match->toArray();
|
|
} catch (\Exception $exception) {
|
|
$message = $exception->getMessage();
|
|
|
|
return $this->result([], $message, 102);
|
|
|
|
}
|
|
|
|
return $this->result($match, "请求成功");
|
|
}
|
|
|
|
public function get_match_players(int $players_id = null, array $where = [])
|
|
{
|
|
$collection = $this->matchContestantModel->all($where, [], true);
|
|
|
|
return $collection;
|
|
}
|
|
|
|
/**
|
|
* 飞手报名参赛
|
|
* @Author:Soar
|
|
* @Time:2023/10/8 16:48
|
|
* @param int $players_id
|
|
* @param int $match_id
|
|
*/
|
|
public function add_players_to_match(int $players_id = null, int $match_id = null)
|
|
{
|
|
//验证
|
|
$json = $this->check_match($match_id);
|
|
|
|
if (is_array($json) && $json['code'] != 100) {
|
|
return $json;
|
|
}
|
|
|
|
$match_info_players = $this->get_match_user($players_id, $match_id);
|
|
|
|
if (!empty($match_info_players)) {
|
|
return json_encode($this->result([], "报名信息已存在!请勿重复报名!", 102));
|
|
}
|
|
|
|
if (!$this->add_match_ontestant($players_id, $match_id)) {
|
|
return json_encode($this->result([], "报名参赛出错!", 102));
|
|
}
|
|
|
|
return json_encode($this->result([], "报名成功!"));
|
|
}
|
|
|
|
/**
|
|
* 查询飞手报名列表信息
|
|
* @Author:Soar
|
|
* @Time:2023/10/9 14:55
|
|
* @param int|null $players_id 飞手id
|
|
* @param int|null $match_id 赛事id
|
|
* @param bool $cache 是否缓存数据
|
|
*/
|
|
public function get_match_user(int $players_id = null, int $match_id = null, bool $cache = false)
|
|
{
|
|
// 查询是否已经报名参赛
|
|
$match_info_players = $this->matchContestantModel;
|
|
$data = ['player_id' => $players_id, 'match_id' => $match_id];
|
|
// 读取缓存
|
|
if ($cache) {
|
|
$cacheKey = $this->get_cache_key($data);
|
|
|
|
if (Cache::has($cacheKey)) {
|
|
return Cache::get($cacheKey);
|
|
}
|
|
}
|
|
|
|
if ($players_id) {
|
|
$match_info_players->where('player_id', $players_id);
|
|
}
|
|
|
|
$match_info_players = $match_info_players->where('match_id', $match_id)
|
|
->where('status', 'in', [1, 2])
|
|
->select();
|
|
// 设置缓存
|
|
if ($cache) {
|
|
Cache::set($this->get_cache_key($data), $match_info_players, 3600);
|
|
}
|
|
|
|
if (empty($match_info_players)) {
|
|
return null;
|
|
}
|
|
|
|
return $match_info_players;
|
|
}
|
|
|
|
/**
|
|
* 验证信息
|
|
* @Author:Soar
|
|
* @Time:2023/10/9 15:28
|
|
* @param int $match_id
|
|
*/
|
|
public function check_match(int $match_id)
|
|
{
|
|
$info = $this->get_match();
|
|
//验证是否是飞手
|
|
|
|
// 验证赛事是否已经报名结束
|
|
if (@strtotime($info['data']['endtime']) <= time()) {
|
|
return $this->result([], "报名时间已经截至!", 102);
|
|
}
|
|
// 验证赛事是否已经开始报名
|
|
if (@strtotime($info['data']['starttime']) > time()) {
|
|
return $this->result([], "报名暂未开始!", 102);
|
|
}
|
|
// 验证赛事参赛人员是否已经满员
|
|
if (@$info['data']['csrs'] <= count($this->get_match_user(null, 181, false))) {
|
|
return $this->result([], "报名人数已满!", 102);
|
|
}
|
|
|
|
return $this->result([], 'success', 100);
|
|
}
|
|
|
|
/**
|
|
* 参加赛事
|
|
* @Author:Soar
|
|
* @Time:2023/10/9 15:56
|
|
* @param int $player_id
|
|
* @param int $match_id
|
|
*/
|
|
public function add_match_ontestant(int $player_id, int $match_id)
|
|
{
|
|
return $this->matchContestantModel->insert([
|
|
'match_id' => $match_id,
|
|
'player_id' => $player_id,
|
|
'status' => 1,
|
|
'created_at' => date("Y-m-d H:i:s")
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取缓存主键
|
|
* @Created by PhpStorm.
|
|
* @Author:Soar
|
|
* @Time:2023/10/9 15:27
|
|
* @param array $data 条件信息
|
|
* @return string
|
|
*/
|
|
public function get_cache_key(array $data)
|
|
{
|
|
return $cacheKey = md5('match:' . serialize($data));
|
|
}
|
|
|
|
public function result($data = [], $msg = " ", $code = 100)
|
|
{
|
|
$res['msg'] = $msg;
|
|
$res['data'] = $data;
|
|
$res['code'] = $code;
|
|
|
|
return $res;
|
|
}
|
|
|
|
public function getMatchId(): int
|
|
{
|
|
return $this->match_id;
|
|
}
|
|
|
|
public function setMatchId(int $match_id): void
|
|
{
|
|
$this->match_id = $match_id;
|
|
}
|
|
|
|
|
|
|
|
} |