<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RankController extends Controller
{
    public function user_rank(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        if ($request->type==1){
            if ($request->season==$season){
                $data=DB::table('user')
                    ->join('enroll','enroll.uid','=','user.id')
                    ->join('service','service.phone','=','user.up_phone')
                    ->join('war_zone','war_zone.uid','=','service.uid')
                    ->where('enroll.status',0)
                    ->where('enroll.season',$request->season)
                    ->orderByDesc('user.integral')
                    ->select('user.nickname','user.id','user.phone','user.integral as score','user.up_name','user.avatar','user.level_name','service.uid as sid','war_zone.war_name','user.up_phone')
                    ->paginate(15);
                if($data->items()){
                    return $this->success_list($data->items(),'ok',$data->total());
                }
            }else{
                $data=DB::table('user')
                    ->join('glory','glory.uid','=','user.id')
                    ->join('service','service.phone','=','user.up_phone')
                    ->join('war_zone','war_zone.uid','=','service.uid')
                    ->where('glory.season',$request->season)
                    ->orderByDesc('glory.integral')
                    ->select('user.nickname','user.id','user.phone','glory.integral as score','user.avatar','user.up_name','user.level_name','service.uid as sid','war_zone.war_name','user.up_phone')
                    ->paginate(15);
                if($data->items()){
                    return $this->success_list($data->items(),'ok',$data->total());
                }
            }

            return $this->success_list([]);
        }

        if ($request->type==2){
            if ($request->season==$season){
                $data=DB::table('user')
                    ->join('service','service.phone','=','user.phone')
                    ->join('war_zone','war_zone.uid','=','service.uid')
                    ->where('user.level_name','=','代理公司')
                    ->orderByDesc('user.integral')
                    ->select('user.id','user.nickname','user.phone','user.integral as score','user.avatar','user.up_name','user.up_phone','service.uid as sid','war_zone.war_name')->paginate(15);
                if($data->items()){
                    return $this->success_list($data->items(),'ok',$data->total());
                }
            }else{
                $data=DB::table('user')
                    ->join('service','service.phone','=','user.phone')
                    ->join('war_zone','war_zone.uid','=','service.uid')
                    ->join('glory','glory.uid','=','user.id')
                    ->where('glory.season','=',$request->season)
                    ->where('user.level_name','=','代理公司')
                    ->orderByDesc('glory.integral')
                    ->select('user.id','user.nickname','user.phone','glory.integral as score','user.up_name','user.avatar','user.up_phone','service.uid as sid','war_zone.war_name')->paginate(15);
                if($data->items()){
                    return $this->success_list($data->items(),'ok',$data->total());
                }
            }

            return $this->success_list([]);
        }

        else{

                $phone=DB::table('user')->where('level_name','政委')->select('phone','id')->get();
                foreach ($phone as $k=>$v){
                    $inte=DB::table('user')->where('up_phone',$v->phone)->sum('integral');
                    DB::table('user')->where('id',$v->id)->update(['integral'=>$inte]);
                }
                $datas=DB::select("SELECT FLOOR(f.total) as total,f.war_names FROM (SELECT SUM(s.integral/10) as total,s.war_names FROM (SELECT t.* ,w.war_names FROM service w JOIN (SELECT a.phone,a.up_phone,p.integral FROM `user` a JOIN  (SELECT SUM(integral) as integral,uid FROM inte_log WHERE type=1 AND season=? GROUP BY uid) as p ON a.id=p.uid) as t ON w.phone =t.up_phone) as s GROUP BY s.war_names) as f ",[$request->season]);
                foreach ($datas as $k=>$v){
                    $s[$v->war_names]=$v->total;
                }
                if ($request->season==$season){
                    $data = DB::table('user')->join('war_zone', 'war_zone.uid', '=', 'user.id')
                        ->select('user.*', 'war_zone.war_name','war_zone.war_avatar','user.nickname','user.phone','user.total')->orderByDesc('user.integral')->get();
                    foreach ($data as $k => $v) {
                        $float = $v->rank_end - $v->rank;
                        $da[$k]['id'] = $v->id;
                        $da[$k]['total'] = $v->total;
                        $da[$k]['war_name'] = $v->war_name;
                        $da[$k]['score'] = $v->integral;
                        $da[$k]['avatar'] = $v->war_avatar;
                        $da[$k]['nickname'] = $v->nickname;
                        $da[$k]['phone'] = $v->phone;
                        $da[$k]['level_name'] = "政委";
                        $da[$k]['up_name']=$s[$v->war_name];
                        $da[$k]['float'] = $float > 0 ? $float : 0;
                    }

                    return $this->response()->array([
                        'code' => 200,
                        'data' => [
                            'list' => $da
                        ]
                    ]);
                }else{
                    $data = DB::table('user')->join('war_zone', 'war_zone.uid', '=', 'user.id')
                        ->join('glory','glory.uid','=','user.id')
                        ->where('glory.season','=',$request->season)
                        ->select('user.id','user.total','glory.integral as integral','user.nickname','user.phone', 'war_zone.war_name','war_zone.war_avatar','user.nickname','user.phone','user.total')->orderByDesc('glory.integral')->get();
                    foreach ($data as $k => $v) {
//                        $float = $v->rank_end - $v->rank;
                        $da[$k]['id'] = $v->id;
                        $da[$k]['total'] = $v->total;
                        $da[$k]['war_name'] = $v->war_name;
                        $da[$k]['score'] = $v->integral;
                        $da[$k]['avatar'] = $v->war_avatar;
                        $da[$k]['nickname'] = $v->nickname;
                        $da[$k]['phone'] = $v->phone;
                        $da[$k]['level_name'] = "政委";
                        $da[$k]['up_name']=$s[$v->war_name];
//                        $da[$k]['float'] = $float > 0 ? $float : 0;
                    }

                    return $this->response()->array([
                        'code' => 200,
                        'data' => [
                            'list' => $da
                        ]
                    ]);
                }

            }

    }


    public function rank_search(Request $request){
        $string=$request->phone;
        if (is_numeric($string)){
            $s='user.phone';
        }else{
            $s='user.nickname';
        }
        if ($request->type==1){
            $data=DB::table('user')
                ->join('service','service.phone','=','user.up_phone')
                ->join('war_zone','war_zone.uid','=','service.uid')
                ->where($s,'like','%'.$string.'%')
                ->select('user.nickname','user.id','user.phone','user.integral as score','user.up_name','user.level_name','user.avatar','service.uid as sid','war_zone.war_name','user.rank','user.up_phone')
                ->paginate(15);
        }if ($request->type==2){
            $data=DB::table('user')
                ->join('service','service.phone','=','user.phone')
                ->join('war_zone','war_zone.uid','=','service.uid')
                ->where($s,'like','%'.$string.'%')
                ->select('user.nickname','user.id','user.phone','user.integral as score','user.avatar','user.up_name','user.level_name','service.uid as sid','war_zone.war_name','user.rank','user.up_phone')
                ->paginate(15);
        }

        if ($data->items()){
            return $this->success_list($data->items(),'ok',$data->total());
        }
        return $this->success([]);

    }

}
