www_fpvone_cn/application/admin/controller/cms/Leaguemaster.php

563 lines
22 KiB
PHP
Raw Normal View History

2024-12-20 12:29:51 +08:00
<?php
/**
* @Created by PhpStorm.
* @Author:Soar
* @Time:2023/11/15 15:13
*/
namespace app\admin\controller\cms;
use app\admin\model\MatchContestant;
use app\admin\model\MatchRanking;
use app\admin\model\MatchScreen;
use app\admin\model\Players;
use app\admin\model\User;
use app\common\controller\Backend;
use app\admin\service\RankingService;
use app\admin\controller\cms\Leagueround;
use app\admin\service\LeagueService;
use app\admin\model\LeagueMaster as LeagueMasterModel;
use think\Db;
class Leaguemaster extends Backend
{
protected $noNeedLogin = [];
public function _initialize()
{
parent::_initialize();
$this->rankingService = new RankingService();
$this->leagueRound = new Leagueround();
$this->leagueService = new LeagueService();
}
public function export()
{
$matchranking = new MatchRanking();
$user = new User();
$players = new Players();
$this->exportmatch_id = $this->request->param("match_id");
$this->exportcourse = $this->request->param("course"); // 赛程
$this->exportround = $this->request->param("other_round");
$this->exportsorting = $this->request->param("sorting");
$this->birthday = $this->request->param("birthday");
if (empty($this->exportmatch_id)) {
$this->error("赛事id不能为空");
} else if (empty($this->exportcourse)) {
$this->error("赛程不可为空!");
}
if($this->exportcourse == 'all'){
$MatchRanking = new MatchRanking();
$master_info = $MatchRanking->where(['match_id'=>$this->exportmatch_id,'course'=>104,'other_round'=>$this->exportround])->select();
// var_dump($master_info[0]['times']);exit;
foreach($master_info as $va){
if(empty($va['times'])){
$this->error('比赛还未结束');
}
}
// $LeagueMasterModel = new LeagueMasterModel();
// $master_info = $LeagueMasterModel->where('final_type',0)->select();
// if(empty($master_info)) $this->error('比赛还未结束');
}
$archives = new \app\admin\model\cms\Archives();
$PDOStatement = $archives->find($this->exportmatch_id);
if (empty($PDOStatement)) {
$this->error('导出数据为空');
}
if($this->exportcourse == 'all'){
$cellTitles = [
'num' => '排名',
'player_id' => '编号',
'player_name' => '姓名',
'gender' => '性别',
'is_young' => '是否满足青少年组',
];
}
// 数据总条数
// var_dump($row);exit;
// $total = 200;
$total = count($master_info) + 4;
if ($total <= 0) {
$this->error('导出数据为空');
}
switch ($this->exportround) {
case 1:
$PDOStatement->title = $PDOStatement->title."公开组最终排名";
break;
case 2:
$PDOStatement->title = $PDOStatement->title."青少年组最终排名";
break;
case 3:
$PDOStatement->title = $PDOStatement->title."女子组最终排名";
break;
}
//计算资格赛每组人数
$export = new \addons\shopro\library\Export();
$params = [
'file_name' => $PDOStatement->title,
'cell_titles' => $cellTitles,
'total' => $total,
'is_sub_cell' => false,
'group_people' =>4,
];
if( $this->exportcourse != 'all'){
$group_people = $this->leagueService->group_people($this->exportmatch_id, $this->exportcourse);
$params['group_people'] = $group_people;
}
$total_commission = 0;
$config = $export->getConfig();
if($this->exportcourse == 'all'){
$result = $export->export_finalRank($params, function ($pages) use (&$total_commission, $total) {
$LeagueMasterModel = new LeagueMasterModel();
// $master_info = $LeagueMasterModel->where('number',1)->where('final_type',$this->exportround)->find();
// $MatchRanking = new MatchRanking();
// $datas = $MatchRanking->where(['match_id'=>$this->exportmatch_id,'course'=>104,'other_round'=>$this->exportround])->select();
// $LeagueIntegral = new LeagueIntegral();
// $datas = $LeagueIntegral->where('match_id',$this->exportmatch_id)->select();
// var_dump($this->exportround);exit;
$res = $this->final_master_rank($this->exportmatch_id,$this->exportround);
if($this->exportround == 1){
// var_dump($this->exportround);exit;
$datas = $LeagueMasterModel->where('final_type',$this->exportround)->select();
var_dump($datas);exit;
$other_data = $LeagueMasterModel->where('final_type',0)->select();
$datas = array_merge($datas,$other_data);
var_dump($datas);exit;
}
if($this->exportround == 2){
// var_dump($this->exportround);exit;
$datas = $LeagueMasterModel->where('final_type',$this->exportround)->select();
$other_data = $LeagueMasterModel->where('final_type',0)->where('birthday','>=',$this->birthday)->select();
foreach ($other_data as $k =>$value){
foreach ($datas as $val){
if($value['player_id'] == $val['player_id']){
unset($other_data[$k]);
}
}
}
$datas = array_merge($datas,$other_data);
// var_dump($datas);exit;
}
if($this->exportround == 3){
$datas = $LeagueMasterModel->where('final_type',$this->exportround)->select();
$other_data = $LeagueMasterModel->where('final_type',0)->where('gender','女')->select();
foreach ($other_data as $k =>$value){
foreach ($datas as $val){
if($value['player_id'] == $val['player_id']){
unset($other_data[$k]);
}
}
}
$datas = array_merge($datas,$other_data);
}
// $datas = array_merge($datas,$res);
// var_dump($datas);exit;
// $datas = $Leagueround->final_rank($this->exportmatch_id);
// var_dump($datas);exit;
$user = new User();
$players = new Players();
// var_dump($this->exportround);exit;
$newDatas = [];
$nums = 0;
foreach ($datas as &$reward) {
// $user_info = $user->where('member_number',$reward['player_id'])->find();
// $player_info = $players->where('member_id',$user_info['id'])->find();
// if(empty($player_info['gender'])){
// var_dump($user_info);exit;
// }
$data = [
'num' => ++$nums,
'player_id' => $reward['player_id'] ? $reward['player_id'] : 0,
'player_name' => $reward['player_name'],
'gender' => $reward['gender'],
'is_young' => $reward['birthday'] >= $this->birthday ? '是' : '否',
];
$newDatas[] = $data;
}
$total_commission += array_sum(array_column($newDatas, 'commission'));
return $newDatas;
});
}
$this->success('导出成功' . (isset($result['file_path']) && $result['file_path'] ? ',请在服务器: “' . $result['file_path'] . '” 查看' : ''), null, $result);
}
//生成大师赛公开组决赛分组
public function final_public_group(){
$match_id = $this->request->param('match_id');
$this->leagueRound->final_groups($match_id,104,false,1);
$array= ['code'=>200,'msg'=>'success'];
return json($array);
}
//生成大师赛青少年/女子决赛分组
public function final_young_group(){
// $array= ['code'=>200,'msg'=>'success'];
// return json($array);
$match_id = $this->request->param('match_id');
$birthday = $this->request->param('birthday');
$women = $this->request->param('women');
$LeagueMasterModel = new LeagueMasterModel();
$MatchRanking = new MatchRanking();
// var_dump($group_info);exit;
if($women != '1'){
$match_rank_info = $MatchRanking->where(['match_id'=>$match_id,'course'=>104,'other_round'=>2])->find();
}else{
$match_rank_info = $MatchRanking->where(['match_id'=>$match_id,'course'=>104,'other_round'=>3])->find();
}
if(!empty($match_rank_info)){
$array= ['code'=>400,'msg'=>'已生成分组'];
return json($array);
}
$master_info = $LeagueMasterModel->where('number',5)->find();
if(true){
$res = $this->integral($match_id);
}else{
$array= ['code'=>400,'msg'=>'fail'];
return json($array);
}
// var_dump($birthday);exit;
if($women != '1'){
$other_round = 2;
$group_info = $LeagueMasterModel->where('birthday','>=',$birthday)->limit(4)->order('number')->select();
// var_dump($group_info);exit;
}else{
$other_round = 3;
$group_info = $LeagueMasterModel->where('gender','女')->limit(4)->order('number')->select();
}
// $LeagueMasterModelModel->where('gender','女')->limit(4)->order('number')->select();
// $young_info = $LeagueMasterModel->where('birthday','>=',$birthday)->limit(4)->order('number')->select();
$u = 0;
for($i = 1;$i <=5 ;$i++){
foreach ($group_info as &$b){
if($u == 0){
$b['grouping'] = '1';
$b['channel'] = 'R1';
$b['led_color'] = '红';
}
if($u == 1){
$b['grouping'] = '1';
$b['channel'] = 'R2';
$b['led_color'] = '黄';
}
if($u == 2){
$b['grouping'] = '1';
$b['channel'] = 'R6';
$b['led_color'] = '蓝';
}
if($u == 3){
$b['grouping'] = '1';
$b['channel'] = 'R8';
$b['led_color'] = '绿';
}
$u++;
}
// var_dump($young_info);exit;
$this->insert_group($group_info,$match_id,104,$other_round);
}
$array= ['code'=>200,'msg'=>'success'];
return json($array);
// $this->success('build success');
}
//生成大师赛公开组/青少年/女子决赛成绩排名
public function final_master_rank($match_id,$other_round){
$user = new User();
$palyer = new Players();
$LeagueMasterModel = new LeagueMasterModel();
$MatchRanking = new MatchRanking();
$LeagueMasterModel->where('match_id',$match_id)->where('final_type',$other_round)->delete();
// var_dump($res);exit;
// $mess_arr = array('code'=>0,'message'=>'该赛事已生成过排名');
// if($inte_exsit) return json($mess_arr);
// var_dump($other_round);exit;
$result = $MatchRanking->where(['match_id'=>$match_id,'course'=>104,'other_round'=>$other_round])->find();
// var_dump($result);exit;
if(empty($result)) $this->error('比赛未结束1');
// var_dump($other_round);exit;
$res1 = $this->leagueRound->group_rank_sort($match_id,104,$other_round);
var_dump($res1);exit;
//前四名
$i = 1;
foreach ($res1 as $v){
$user_res = $user->where('member_number',$v['player_id'])->find();
$player_res = $palyer->where('member_id',$user_res['id'])->find();
// var_dump($club_res);exit;
$LeagueMasterModel->insert([
'match_id' => $match_id,
'player_id' => $v['player_id'],
'player_name' => $v['player_name'],
'qua_rank' =>$v['qua_rank'],
'country' =>$v['country'],
'national_flag' =>$v['national_flag'],
'gender' => $player_res['gender'],
'birthday' => $player_res['birthday'],
'number' => $i,
'createtime' => time(),
'final_type'=> $other_round,
]);
$i++;
}
return true;
}
public function integral($match_id){
// return json($success);
// var_dump($res_sen);exit;
$user = new User();
$palyer = new Players();
$LeagueMasterModel = new LeagueMasterModel();
$MatchRanking = new MatchRanking();
// $match_id = $this->request->param('match_id');
// $inte_exsit = $LeagueIntegral->where('match_id',$match_id)->find();
// $res_sen = $this->leagueService->bestplayerqua($match_id,101);
// $res_sen = array_slice($res_sen,64);
// $res13 = $this->leagueRound->group_rank_sort($match_id,103,13);
// // $res = $this->extra_match($new_arr,$match_id,103);
// var_dump($res13);exit;
$res = $LeagueMasterModel->where('match_id',$match_id)->delete();
// var_dump($res);exit;
// $mess_arr = array('code'=>0,'message'=>'该赛事已生成过排名');
// if($inte_exsit) return json($mess_arr);
// $result = $MatchRanking->where(['match_id'=>$match_id,'course'=>104,'other_round'=>1])->find();
// if(empty($result)) $this->error('已清空积分');
// var_dump($result['is_extra']);exit;
$res13 = $this->leagueRound->group_rank_sort($match_id,103,13);
$res11 = $this->leagueRound->group_rank_sort($match_id,103,11);
$res10 = $this->leagueRound->group_rank_sort($match_id,103,10);
$res8 = $this->leagueRound->group_rank_sort($match_id,103,8);
$res7 = $this->leagueRound->group_rank_sort($match_id,103,7);
$res5 = $this->leagueRound->group_rank_sort($match_id,103,5);
$res4 = $this->leagueRound->group_rank_sort($match_id,103,4);
$res3 = $this->leagueRound->group_rank_sort($match_id,103,3);
// var_dump($res13);exit;
//5-8名
$eve_arr = [$res13,$res11];
$i = 5;
foreach($eve_arr as $val){
$new_arr = [];
foreach ($val as $value) {
if(in_array($value['group_sort'],array(3,4))){
$new_arr[] = $value;
}
}
// $res = $this->extra_match($new_arr,$match_id,103);
// exit;
foreach ($new_arr as $v){
$user_res = $user->where('member_number',$v['player_id'])->find();
$player_res = $palyer->where('member_id',$user_res['id'])->find();
$LeagueMasterModel->insert([
'match_id' => $match_id,
'player_id' => $v['player_id'],
'player_name' => $v['player_name'],
'qua_rank' =>$v['qua_rank'],
'country' =>$v['country'],
'national_flag' =>$v['national_flag'],
'gender' => $player_res['gender'],
'birthday' => $player_res['birthday'],
'number' => $i,
'createtime' => time(),
'final_type' => 0,
]);
$i++;
}
}
//9-64名
$com_arr = [$res10,$res8,$res7,$res5,$res4,$res3];
$i = 9;
foreach($com_arr as $val){
$new_arr = [];
foreach ($val as $value) {
if(in_array($value['group_sort'],array(3,4))){
$new_arr[] = $value;
}
}
$res = $this->extra_match($new_arr,$match_id,103);
// exit;
foreach ($res as $v){
$user_res = $user->where('member_number',$v['player_id'])->find();
$player_res = $palyer->where('member_id',$user_res['id'])->find();
$LeagueMasterModel->insert([
'match_id' => $match_id,
'player_id' => $v['player_id'],
'player_name' => $v['player_name'],
'qua_rank' =>$v['qua_rank'],
'country' =>$v['country'],
'national_flag' =>$v['national_flag'],
'gender' => $player_res['gender'],
'birthday' => $player_res['birthday'],
'number' => $i,
'createtime' => time(),
'final_type' => 0,
]);
$i++;
}
}
$i = 65;
//64名之后排位赛有1轮成绩的积1分
$res_sen = $this->leagueService->bestplayerqua($match_id,101);
$res_sen = array_slice($res_sen,64);
//TODO先循环拿出fly_num=0 再查组合老数组
foreach($res_sen as $vvv){
// if($vvv['fly_num'] >= 3){
// var_dump($vvv);exit;
if($vvv['fly_num'] > 0 || $vvv['fly_num'] == 'DNF'){
$user_res = $user->where('member_number',$vvv['info']['player_id'])->find();
if(empty($user_res['id'])){var_dump($vvv);exit;}
$player_res = $palyer->where('member_id',$user_res['id'])->find();
$LeagueMasterModel->insert([
'match_id' => $match_id,
'player_id' => $vvv['info']['player_id'],
'player_name' => $vvv['info']['player_name'],
'qua_rank' =>$v['qua_rank'],
'country' =>$v['country'],
'national_flag' =>$v['national_flag'],
'gender' => $player_res['gender'],
'birthday' => $player_res['birthday'],
'number' => $i,
'createtime' => time(),
'final_type' => 0,
]);
$i++;
}
}
// var_dump($res);exit;
$success = array('code'=>1,'message'=>'success');
return json($success);
}
public function insert_group($data,$match_id,$course,$other_round){
$matchranking = new MatchRanking();
Db::startTrans();
try{
foreach ($data as $val){
// var_dump($val);exit;
$match_rank_res = $matchranking->where([
'match_id' => $match_id,
'course' => $course,
'other_round' => $other_round,
'player_id' => $val['player_id'],
])->find();
$player['match_id'] = $match_id;
$player['course'] = $course;
$player['other_round'] = $other_round;
$player['player_id'] = $val['player_id'];
$player['player_name'] = $val['player_name'];
$player['channel'] = $val['channel'];
$player['led_color'] = $val['led_color'];
$player['grouping'] = $val['grouping'];
$player['qua_rank'] = $val['qua_rank'];
$player['country'] = $val['country'];
$player['national_flag'] = $val['national_flag'];
if(empty($match_rank_res)){
$matchranking->insert($player);
}
}
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
}
}
//决赛是否有加赛
public function is_extra(){
$match_id = $this->request->param('match_id');
$is_open = $this->request->param('is_open');
$MatchRanking = new MatchRanking();
$result = $MatchRanking->where(['match_id'=>$match_id,'course'=>104,'other_round' => 1,])->select();
foreach ($result as $v){
$MatchRanking->where('id', $v['id'])
->update(['is_extra'=>$is_open]);
}
if(true){
$data = ['code'=>1,'message'=>'success'];
return json($data);
}else{
$data = ['code'=>0,'message'=>'fail'];
return json($data);
}
}
public function custom_rank(){
$MatchRanking = new MatchRanking();
// var_dump($_POST);exit;
if(empty($_POST['data']) || empty($_POST['match_id']))$this->error('lack param');
foreach ($_POST['data'] as $v){
// var_dump($v);exit;
$MatchRanking->where(['match_id'=> $_POST['match_id'],'course'=>104,'other_round'=>1,'is_extra'=>1,'player_id'=>$v['player_id']])
->update(['custom_sorting'=>$v['custom_sorting']]);
}
$data = ['code'=>1,'message'=>'success'];
return json($data);
}
public function find_sort($match_id,$course,$other_round){
$res = $this->leagueRound->group_rank_sort($match_id,$course,$other_round);
foreach ($res as $value) {
if(in_array($value['group_sort'],array(3,4))){
$new_arr[] = $value;
}
}
$res = $this->extra_match($new_arr,$match_id,$course);
//存表 差俱乐部id
}
//加赛
public function extra_match($data,$match_id,$course){
$res1 = $this->find_quarank($match_id,$course);
// var_dump($res1);exit;
foreach ($res1 as $vvvv){
foreach ($data as $va){
if($va['player_id'] == $vvvv){
$obj_arr[] = $va;
}
}
}
// var_dump($obj_arr);exit;
// var_dump($obj_arr);exit;
return $obj_arr;
}
public function find_quarank($match_id,$course){
// $match_id = 606;$course = 101;
$res = $this->leagueService->bestplayerqua($match_id,$course);
foreach ($res as $val){
$new_arr[] = $val['info']['player_id'];
}
// var_dump($new_arr);exit;
return $new_arr;
}
}