<?php


namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Jenssegers\Agent\Facades\Agent;

class DataController extends  Controller{

    /*
     * 接口一，计算参赛情况
     */
    public function type_num(Request $request){

        $season=DB::table('basic')->where('keys','=','season')->value('value');
        if ($request->season==$season){
            $per_num=DB::table('user')->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('enroll.status','=',0)
                ->where('user.integral','<>',0)
                ->count();
        }else{
            $per_num=DB::table('user')->join('glory','glory.uid','=','user.id')
                ->where('glory.season',$request->season)
                ->where('glory.integral','<>',0)
                ->count();
        }


        $dat=DB::table('user')->join('enroll','enroll.uid','=','user.id')->where('enroll.status',0)
            ->where('enroll.season','=',$request->season) ->select('user.level_name',DB::raw('count(*) as num'))
            ->groupBy('user.level_name')
            ->get();
        $data=DB::table('user')->select('level_name',DB::raw('count(*) as num'))->groupBy('level_name')->get();
        $super_join=0;
        $top_join=0;
        if ($dat){
            foreach ($dat as $k=>$v){
                if ($v->level_name=='销售主管'){
                    $super_join=$v->num;
                }
                if ($v->level_name=='销售经理'){
                    $top_join=$v->num;
                }
            }
        }
        $season_data=DB::table('season')->where('season',$request->season)->first();
        $time=$season_data->round_start_time;
        $end=$season_data->round_end_time;
        $super_total=0;
        $top_total=0;
        $crown_total=0;
        $crown_join=0;
        if ($data){
            foreach ($data as $k=>$v){
                if ($v->level_name=='销售主管'){
                    $super_total=$v->num;
                }
                if ($v->level_name=='销售经理'){
                    $top_total=$v->num;
                }
                if ($v->level_name=='代理公司'){
                    $crown_total=$v->num;
                    $crown_join=$v->num;
                }
            }
        }

        $num1=(int)DB::table('inte_review')->where('season','=',$request->season)->whereBetween('time',[$time,$end])
            ->sum('type_one');
        $num2=(int)DB::table('inte_review')->where('season','=',$request->season)->whereBetween('time',[$time,$end])
            ->sum('type_two');
        $num3=(int)DB::table('inte_review')->where('season','=',$request->season)->whereBetween('time',[$time,$end])
            ->sum('type_three');

        $list=[
            'crown_join'=>$crown_join,
            'super_join'=>$super_join,
            'top_join'=>$top_join,
            'crown_total'=>$crown_total,
            'super_total'=>$super_total,
            'top_total'=>$top_total,
            'hard_sale_total'=>$num1*399,
            'simple_sale_total'=>$num2*169,
            'old_sale_total'=>$num3*199,
            'hard_order_total'=>$num1,
            'simple_order_total'=>$num2,
            'old_order_total'=>$num3,
            'per_num'=>$per_num
        ];
        return $this->success($list);
    }

    public function day_num(Request $request){

        if ($request->has('day')){
            $start=date('Y-m-d H:i:s',$request->day);
            $day=$request->day+86400;
            $end=date('Y-m-d H:i:s',$day);
        }else{
            $now=Carbon::today()->timestamp;
            $s=DB::table('season')->where('season',$request->season)->first();
            $time=$s->round_start_time;
            $ends=$s->round_end_time;
//            $starts=DB::table('basic')->where('keys','round_start_time')->value('value');

            if ($now>$ends){
                $end=date('Y-m-d H:i:s',$ends-1);
                $start=Carbon::parse($end)->startOfDay();
            }else{
                $start=Carbon::today();
                $end=date('Y-m-d H:i:s',$now+86399);
            }
        }
        $data['hard']=(int)DB::table('inte_review')->where('season',$request->season)->where('created_at','>',$start)->where('created_at','<',$end)->sum('type_one');
        $data['simple']=(int)DB::table('inte_review')->where('season',$request->season)->whereBetween('created_at',[$start,$end])->sum('type_two');
        $data['old']=(int)DB::table('inte_review')->where('season',$request->season)->whereBetween('created_at',[$start,$end])->sum('type_three');
        return $this->success($data);
    }

    /*
     * 销售统计
     */
    public function order_count(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $res=DB::table('data_his')->where('season',$request->season)->where('type',2)->first();
        if ($res){
            $data=json_decode($res->data);
        }else{
            $s=DB::table('season')->where('season',$request->season)->first();
            $time=$s->round_start_time;
            $time_end=$s->round_end_time;
            $start=$time;
            $end=Carbon::now()->timestamp;
            if ($request->has('start')){
                $start=$request->start;
                $end=$request->end;
            }
            if ($start>$end){
                $data=[
                    'date'=>[],
                    'hard'=>[],
                    'old'=>[],
                    'simple'=>[],
                ];
                return $this->success($data);
            }
            if ($end>$time_end){
                $end=$time_end;
            }
            $day=ceil(($end-$start)/86400);
            $sold_total=0;
            $date=[date('n/j',Carbon::today()->timestamp)];
            for ($i=0;$i<$day;$i++){
                $date[$i] = date('n/j', $start+(86400*$i));
                $dat[$i]=date('Y-m-d', $start+(86400*$i));
                $data1[$i]=(int)DB::table('inte_review')->where([['season','=',$request->season],['status','=',0]])
                    ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                    ->sum('type_one');
                $data2[$i]=(int)DB::table('inte_review')->where([['season','=',$request->season],['status','=',0]])
                    ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                    ->sum('type_two');
                $data3[$i]=(int)DB::table('inte_review')->where([['season','=',$request->season],['status','=',0]])
                    ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                    ->sum('type_three');
            }
            $data=[
                'date'=>$date,
                'hard'=>$data1,
                'old'=>$data3,
                'simple'=>$data2,
            ];
            if ($request->season!=$season){
                DB::table('data_his')->insert(['data'=>json_encode($data),'type'=>2,'season'=>$request->season]);
            }
        }
        return $this->success($data);
    }

    public function sales(Request $request){

        if ($request->has('day')){
            $start=date('Y-m-d H:i:s',$request->day);
            $day=$request->day+86399;
            $end=date('Y-m-d H:i:s',$day);
        }else{
            $now=Carbon::today()->timestamp;
            $s=DB::table('season')->where('season',$request->season)->first();
            $time=$s->round_start_time;
            $ends=$s->round_end_time;
//            $starts=DB::table('basic')->where('keys','round_start_time')->value('value');

            if ($now>$ends){
                $end=date('Y-m-d H:i:s',$ends-1);
                $start=Carbon::parse($end)->startOfDay();
            }else{
                $start=Carbon::today();
                $end=date('Y-m-d H:i:s',$now+86399);
            }

        }
        $data['hard']=(int)DB::table('inte_review')->where('season',$request->season)->whereBetween('created_at',[$start,$end])->sum('type_one');
        $data['simple']=(int)DB::table('inte_review')->where('season',$request->season)->whereBetween('created_at',[$start,$end])->sum('type_two');
        $data['old']=(int)DB::table('inte_review')->where('season',$request->season)->whereBetween('created_at',[$start,$end])->sum('type_three');
        $data['hard']=$data['hard']*399;
        $data['old']=$data['old']*199;
        $data['simple']=$data['simple']*169;
        return $this->success($data);

    }

    public function sales_all(Request $request){

//        return Carbon::now()->endOfDay()->timestamp;
        $s=DB::table('season')->where('season',$request->season)->first();
        $time=$s->round_start_time;
        $time_end=$s->round_end_time;
        $start=$time;
        $end=Carbon::now()->timestamp;
        if ($end>$time_end){
            $end=$time_end;
        }
        if ($request->has('start')){
            $start=$request->start;
            $end=$request->end;
        }
        if ($start>$end){
            $data=[
                'date'=>[],
                'hard'=>[],
                'old'=>[],
                'simple'=>[],
            ];
            return $this->success($data);
        }
        $day=ceil(($end-$start)/86400);
        $sold_total=0;
        $date=[date('n/j',Carbon::today()->timestamp)];
        for ($i=0;$i<$day;$i++){
            $date[$i] = date('n/j', $start+(86400*$i));
            $dat[$i]=date('Y-m-d', $start+(86400*$i));
            $data1[$i]=399*(int)DB::table('inte_review')->where([['season','=',$request->season],['status','=',0]])
                    ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                    ->sum('type_one');
            $data2[$i]=169*(int)DB::table('inte_review')->where([['season','=',$request->season],['status','=',0]])
                    ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                    ->sum('type_two');
            $data3[$i]=199*(int)DB::table('inte_review')->where([['season','=',$request->season],['status','=',0]])
                    ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                    ->sum('type_three');
        }
        $data=[
            'date'=>$date,
            'hard'=>$data1,
            'old'=>$data3,
            'simple'=>$data2,
        ];
        return $this->success($data);
    }


    public function war_data(Request $request){

        $res=DB::table('data_his')->where('season',$request->season)->where('type',3)->first();
        if ($res){
            $data=$res->data;
        }else {
            $seasons=DB::table('basic')->where('keys','=','season')->value('value');
            $datas = DB::table('service')->groupBy('war_names')->select('war_names')->get();
            $season = DB::table('season')->where('season', $request->season)->first();
            $data = $this->war_zone($datas, $season->season, $season->round_start_time, $season->round_end_time);
            if ($request->season!=$seasons){
                DB::table('data_his')->insert(['type'=>3,'season'=>$request->season,'data'=>json_encode($data)]);
            }
        }
        return $data;
    }

    public function war_zone($datas,$season,$start,$end){
        $season_now=DB::table('basic')->where('keys','=','season')->value('value');

        foreach ($datas as $k=>$v){
            $num=DB::table('enroll')
                ->join('user','user.id','=','enroll.uid')
                ->join('service','service.phone','=','user.up_phone')
                ->where('enroll.season',$season)
                ->where('enroll.status',0)
                ->where('service.war_names',$v->war_names)->count();
            if ($season==$season_now){
                $join_num[$k]=DB::table('user')->join('service','service.phone','=','user.up_phone')
                    ->whereIn('user.level_name',['销售主管','销售经理'])
                    ->where('user.integral','<>',0)->where('service.war_names',$v->war_names)->count();
            }else{
                $join_num[$k]=DB::table('user')
                    ->join('service','service.phone','=','user.up_phone')
                    ->join('glory','glory.uid','=','user.id')
                    ->where('glory.season',$season)
                    ->whereIn('user.level_name',['销售主管','销售经理'])
                    ->where('glory.integral','<>',0)->where('service.war_names',$v->war_names)->count();
            }

            $nums[$k]=$num;
            $hard[$k]=$this->war_type($v->war_names,$season,$start,$end,'type_one');
            $simple[$k]=$this->war_type($v->war_names,$season,$start,$end,'type_two');
            $old[$k]=$this->war_type($v->war_names,$season,$start,$end,'type_three');
//            $count[$k]=round((($hard[$k]+$simple[$k]+$old[$k])/$num),2);
            $data[$k]=$v->war_names;

        }
        return ['war_name'=>$data,'num'=>$nums,'hard'=>$hard,'simple'=>$simple,'old'=>$old,'join_num'=>$join_num];
    }

    public function war_type($war_names,$season,$start,$end,$type){
        $data=(int)DB::table('inte_review')
            ->join('user','user.id','=','inte_review.uid')
            ->join('service','service.phone','=','user.up_phone')
            ->where('service.war_names',$war_names)
            ->where('inte_review.season',$season)
            ->whereBetween('inte_review.time',[$start,$end])
            ->sum("inte_review.$type");
        return $data;
    }

    public function data_count(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $re=[];
        $season_data=DB::table('season')->orderByDesc('season')->get();
        foreach ($season_data as $key=>$val){
            $time=$val->round_start_time;
            $end=$val->round_end_time;
            $enroll=DB::table('enroll')
                ->join('user','user.id','=','enroll.uid')
                ->where('enroll.season',$val->season)->where('enroll.status','=',0)->groupBy('enroll.uid')->get();
            foreach ($enroll as $k=>$v){
                $re[$k]=$v->uid;
            }
            for ($i=0;$i<3;$i++){
//                $week[$i]='第'.($i+1).'周';
                $join[$i]=count($enroll);
                $sale[$i]=count(DB::table('inte_review')
                    ->whereIn('uid',$re)
                    ->where('inte_review.season',$val->season)
                    ->where('inte_review.time','>=',$time+7*86400*($i))
                    ->where('inte_review.time','<=',$time+7*86400*($i+1)-1)
                    ->groupBy('inte_review.uid')->get());
                $num_one=$this->sql($val->season,$time,$end,$i,"type_one");
                $num_two=$this->sql($val->season,$time,$end,$i,"type_two");
                $num_three=$this->sql($val->season,$time,$end,$i,"type_three");
                $num_all=$num_one+$num_two+$num_three;
                $money_all=$num_one*399+$num_two*179+$num_three*199;
                $num[$i]=$num_all;
                $money[$i]=$money_all;
                if ($join[$i]==0||$sale[$i]==0){
                    $enroll_avg[$i]=0;
                    $sale_avg[$i]=0;
                }else{
                    $enroll_avg[$i]=round($num_all / $join[$i],2);
                    $sale_avg[$i]=round($num_all / $sale[$i],2);
                }

//                $money_join_avg=round($money_all / $join[$i] ,2);
//                $money_sale_avg=round($money_all / $sale[$i] ,2);
            }
//            $num[3]=$num[2];
//            $money[3]=$money[2];
//            $s['week']=$week;
            $s['num']=$num;//每周卖货量
            $s['money']=$money;//每周卖货金额
            $s['enroll_num']=$join;//参赛人数
            $s['sale_num']=$sale;//卖货人数
            $s['enroll_avg']=$enroll_avg;
            $s['join_avg']=$sale_avg;
//            $s['money_join_avg']=$money_join_avg;
//            $s['money_sale_avg']=$money_sale_avg;
            $data[$key]=$s;
            $data[$key]['season']=$val->season;
//            if ($season!=$val->season){
//                $r=json_encode($s);
//                DB::table('data_his')->insert(['season'=>$val->season,'data'=>$r]);
//            }
        }

        return $this->success($data);
    }

    public function sql($season,$time,$end,$i,$type){
//        if ($i==2){
//            $data=DB::table('inte_review')
//                ->where('inte_review.season',$season)
//                ->where('inte_review.time','>=',$time)
//                ->where('inte_review.time','<=',$end)
//                ->sum("$type");
//        }else{
            $data=DB::table('inte_review')
                ->where('inte_review.season',$season)
                ->where('inte_review.time','>=',$time+7*86400*$i)
                ->where('inte_review.time','<=',$time+7*86400*($i+1)-1)
                ->sum("$type");
//        }
        return $data;
    }

    public function season_sale(){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $re=DB::table('season')->orderByDesc('season')->get();
        foreach ($re as $k=>$v){
            $res=DB::table('data_his')->where('season',$v->season)->where('type',1)->first();
            if ($res){
                $data[$k]=[
                    'season'=>$v->season,
                    'all'=>json_decode($res->data)
                ];
            }else{
                $time=$v->round_start_time;
                $time_end=$v->round_end_time;
                $start=$time;
                $day=ceil(($time_end-$start)/86400);
//            $sold_total=0;
                $date=[date('n/j',Carbon::today()->timestamp)];
                for ($i=0;$i<$day;$i++){
//                $date[$i] = date('n/j', $start+(86400*$i));
                    $dat[$i]=date('Y-m-d', $start+(86400*$i));
                    $data1[$i]=399*(int)DB::table('inte_review')->where([['season','=',$v->season],['status','=',0]])
                            ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                            ->sum('type_one');
                    $data2[$i]=169*(int)DB::table('inte_review')->where([['season','=',$v->season],['status','=',0]])
                            ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                            ->sum('type_two');
                    $data3[$i]=199*(int)DB::table('inte_review')->where([['season','=',$v->season],['status','=',0]])
                            ->whereBetween('created_at',[Carbon::parse($dat[$i])->startOfDay(),Carbon::parse($dat[$i])->endOfDay()])
                            ->sum('type_three');
                    $all[$i]=$data1[$i]+$data2[$i]+$data3[$i];
                }
                $data[$k]=[
                    'season'=>$v->season,
                    'all'=>$all
                ];
                if ($v->season!=$season){
                    DB::table('data_his')->insert(['type'=>1,'season'=>$v->season,'data'=>json_encode($all)]);
                }
            }
        }

        return $this->success($data);
    }

    public function location(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $data=['北京', '天津', '上海', '重庆', '河北', '河南', '云南', '辽宁', '黑龙江', '湖南', '安徽', '山东', '新疆', '江苏','浙江', '江西', '湖北', '广西', '甘肃', '山西', '内蒙古', '陕西', '吉林', '福建', '贵州', '广东', '青海', '西藏', '四川', '宁夏', '海南', '台湾','香港', '澳门'];
        foreach ($data as $k=>$v){
            $top=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售经理')
                ->where('user.province','like',$v.'%')
                ->where('enroll.status',0)->count();
            $tj=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售主管')
                ->where('user.province','like',$v.'%')
                ->where('enroll.status',0)->count();
            $re_1[$k]['name']=$v;
            $re_1[$k]['top']=$top;
            $re_1[$k]['tj']=$tj;
            $re_1[$k]['value']=$tj+$top;
        }
        $top1=DB::table('user')
            ->join('enroll','enroll.uid','=','user.id')
            ->where('enroll.season',$season)
            ->where('user.level_name','=','销售经理')
            ->where('enroll.status',0)->count();
        $tj1=DB::table('user')
            ->join('enroll','enroll.uid','=','user.id')
            ->where('enroll.season',$season)
            ->where('user.level_name','=','销售主管')
            ->where('enroll.status',0)->count();

        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $time_start=DB::table('basic')->where('keys','=','sign_start_time')->value('value');
        $time_end=DB::table('basic')->where('keys','=','sign_end_time')->value('value');
        $time=$time_end-$time_start+1;
        if (Agent::isMobile()){
            $l=6;
            $num=ceil($time/($l*3600));
        }elseif (Agent::isTablet()){
            $l=2;
            $num=ceil($time/($l*3600));
        }elseif (Agent::isDesktop()){
            $l=2;
            $num=ceil($time/($l*3600));
        }

        $k=0;
        $re=[];
        $res=[];
        $ress=[];
        for($i=0;$i<=$num;$i++){
            if ($num==$i){
                $time_s=date('Y-m-d H:i:s',$i*3600*$l+$time_start);
                $time_s_end=date('Y-m-d H:i:s',$time_end);
            }else{
                $time_s=date('Y-m-d H:i:s',$i*3600*$l+$time_start);
                $time_s_end=date('Y-m-d H:i:s',($i+1)*3600*$l+$time_start-1);
            }

            $top_1=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售经理')
                ->whereBetween('enroll.created_at',[$time_s,$time_s_end])
                ->where('enroll.status',0)->count();
            $tj_1=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售主管')
                ->whereBetween('enroll.created_at',[$time_s,$time_s_end])
                ->where('enroll.status',0)->count();
            $re[].=$top_1;
            $res[].=$tj_1;
//            $re[]=$tj+$top;
            if ($num==$i){
                $ress[].=date('m/d H:i',$time_end);
            }else{
                $ress[].=date('m/d H:i',$i*3600*$l+$time_start);
            }

            $k++;
        }
        $city=$this->city($season);
        $per=$this->per_season($season);
        $now=Carbon::now()->timestamp;
        if ($now>=$time_start&&$now<$time_end){
            $status=true;
        }else{
            $status=false;
        }
        return $this->success_list_one($re_1,'',$top1+$tj1,$top1,$tj1,$re,$res,$ress,$status,$city,$per);
    }

    /*-报名人数等级与上届对比-*/
    public function per_season($season){
        $top_ids=$this->per_season_all($season,'销售经理');
        $top_ids_yes=$this->per_season_all($season-1,'销售经理');
        $tj_ids=$this->per_season_all($season,'销售主管');
        $tj_ids_yes=$this->per_season_all($season-1,'销售主管');
        $num_top_season=count($top_ids);
        $num_top_season_yes=count($top_ids_yes);
        $num_tj_season=count($tj_ids);
        $num_tj_season_yes=count($tj_ids_yes);
        $includ_top=count(array_intersect($top_ids,$top_ids_yes));
        $includ_tj=count(array_intersect($tj_ids,$tj_ids_yes));
        $add_top=$num_top_season-$num_top_season_yes;
        $add_tj=$num_tj_season-$num_tj_season_yes;
        return [
            ['level'=>'销售主管','now'=>$num_tj_season,'pre'=>$num_tj_season_yes,'include'=>$includ_tj,'add'=>$add_tj],
            ['level'=>'销售经理','now'=>$num_top_season,'pre'=>$num_top_season_yes,'include'=>$includ_top,'add'=>$add_top],
        ];
    }

    public function per_season_all($season,$level_name){
        $re=DB::table('user')
            ->join('enroll','enroll.uid','=','user.id')
            ->where('enroll.season','=',$season)
            ->where('enroll.status','=',0)
            ->where('user.level_name','=',$level_name)
            ->pluck('enroll.uid')->toArray();
        return $re;
    }

    /*--报名地区对比--*/
    public function city($season){
        $city=DB::table('city_copy1')->groupBy('type')->select('type')->get();
        $ids=[];
        foreach ($city as $k=>$v){
            $num_all=0;
            $data=DB::table('city_copy1')->where('type',$v->type)->get();
            $res=[];
            foreach ($data as $ke=>$va){
                if ($va->city=='北京市'||$va->city=='上海市'||$va->city=='重庆市'){
                    $id=DB::table('user')->where('province','like','%'.$va->city.'%')->pluck('id');
                }else{
                    $id=DB::table('user')->where('city','like','%'.$va->city.'%')->pluck('id');
                }
//                $ids=array_merge($id,$ids);
//           $a=DB::table('inte_review')->whereIn('uid',$id)->where('season',$season)->sum('type_one');
//           $b=DB::table('inte_review')->whereIn('uid',$id)->where('season',$season)->sum('type_two');
//           $c=DB::table('inte_review')->whereIn('uid',$id)->where('season',$season)->sum('type_three');
                $num=DB::table('enroll')
                    ->where('season',$season)->where('status',0)
                    ->whereIn('uid',$id)->count();
                $res[$ke]['name']=$va->city;
                $res[$ke]['num']=$num;
                $num_all=$num_all+$num;
            }
            if ($v->type=='一线城市'){
                $re[$k]['xh']=0;
            }elseif ($v->type=='新一线城市'){
                $re[$k]['xh']=1;
            }elseif ($v->type=='二线城市'){
                $re[$k]['xh']=2;
            }elseif ($v->type=='三线城市'){
                $re[$k]['xh']=3;
            }elseif ($v->type=='四线城市'){
                $re[$k]['xh']=4;
            }elseif ($v->type=='五线城市'){
                $re[$k]['xh']=5;
            }elseif ($v->type=='其他'){
                $re[$k]['xh']=6;
            }
           $re[$k]['name']=$v->type;
           $re[$k]['num']=$num_all;
           $re[$k]['city']=$res;
//           $re[$k]['money']=$a*399+$b*169+$c*199;
        }
        $a=[];
        foreach($re as $key=>$val){
            $a[] = $val['xh'];//这里要注意$val['nums']不能为空，不然后面会出问题
        }
//$a先排序
        sort($a);
        $a = array_flip($a);
        $result = array();
        foreach($re as $k=>$v){
            $temp1 = $v['xh'];
            $temp2 = $a[$temp1];
            $result[$temp2] = $v;
        }
//这里还要把$result进行排序，健的位置不对
        sort($result);
//然后就是你想看到的结果了
//        var_dump($result);

        return $result;
    }

    public function province(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $datas=DB::table('dt_area')->where('area_name','like',"%$request->province%")->where('area_parent_id','=',0)->value('id');
        $data=DB::table('dt_area')->where('area_parent_id',$datas)->get();
        $re=[];
        foreach ($data as $k=>$v){
            $top=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售经理')
                ->where('user.province','like','%'.$request->province.'%')
                ->where('city','like',"%$v->area_name%")
                ->where('enroll.status',0)->count();
            $tj=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售主管')
                ->where('user.province','like','%'.$request->province.'%')
                ->where('city','like',"%$v->area_name%")
                ->where('enroll.status',0)->count();
            $re[$k]['name']=$v->area_name;
            $re[$k]['top']=$top;
            $re[$k]['tj']=$tj;
            $re[$k]['value']=$tj+$top;
        }
        return $this->success_list($re);
    }

    public function province_time(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $time_start=DB::table('basic')->where('keys','=','sign_start_time')->value('value');
        $time_end=DB::table('basic')->where('keys','=','sign_end_time')->value('value');
        $time=$time_end-$time_start+1;
        if (Agent::isMobile()){
            $l=6;
            $num=ceil($time/($l*3600));
        }elseif (Agent::isTablet()){
            $l=2;
            $num=ceil($time/($l*3600));
        }elseif (Agent::isDesktop()){
            $l=2;
            $num=ceil($time/($l*3600));
        }

//        dump($num);
        $k=0;
        $re=[];
        $res=[];
        $ress=[];
        for($i=0;$i<=$num;$i++){
            if ($num==$i){
                $time_s=date('Y-m-d H:i:s',$i*3600*$l+$time_start);
                $time_s_end=date('Y-m-d H:i:s',$time_end);
            }else{
                $time_s=date('Y-m-d H:i:s',$i*3600*$l+$time_start);
                $time_s_end=date('Y-m-d H:i:s',($i+1)*3600*$l+$time_start-1);
            }

            $top=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售经理')
                ->where('user.province','like','%'.$request->province.'%')
                ->whereBetween('enroll.created_at',[$time_s,$time_s_end])
                ->where('enroll.status',0)->count();
            $tj=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$season)
                ->where('user.level_name','=','销售主管')
                ->where('user.province','like','%'.$request->province.'%')
                ->whereBetween('enroll.created_at',[$time_s,$time_s_end])
                ->where('enroll.status',0)->count();
            $re[].=$top;
            $res[].=$tj;
//            $re[]=$tj+$top;
            if ($num==$i){
                $ress[].=date('m/d H:i',$time_end);
            }else{
                $ress[].=date('m/d H:i',$i*3600*$l+$time_start);
            }

            $k++;
        }
//        dump($re);
        $arr=[
            'top'=>$re,
            'tj'=>$res,
            'time'=>$ress
        ];
        return $this->success($arr);
    }

    public function juge_head(){
        if (Agent::isMobile()){
            return '手机1';
        }elseif (Agent::isTablet()){
            return '未知';
        }elseif (Agent::isDesktop()){
            return '桌面';
        }
    }

    public function sale_location(){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
//        $season=21;
        $data=['北京', '天津', '上海', '重庆', '河北', '河南', '云南', '辽宁', '黑龙江', '湖南', '安徽', '山东', '新疆', '江苏','浙江', '江西', '湖北', '广西', '甘肃', '山西', '内蒙古', '陕西', '吉林', '福建', '贵州', '广东', '青海', '西藏', '四川', '宁夏', '海南', '台湾','香港', '澳门'];
        $hard_all=0;
        $old_all=0;
        $simple_all=0;
        foreach ($data as $k=>$v){
            $datas=DB::table('inte_review')
                ->join('user','user.id','=','inte_review.uid')
                ->where('user.province','like',$v.'%')
                ->where('inte_review.season','=',$season)
                ->first(
                    array(
                        \DB::raw('SUM(type_one) as hard'),
                        \DB::raw('SUM(type_two) as simple'),
                        \DB::raw('SUM(type_three) as old'),
                    )
                );
            $a=$datas->hard==null?0:$datas->hard;
            $b=$datas->simple==null?0:$datas->simple;
            $c=$datas->old==null?0:$datas->old;


            $re[$k]['name']=$v;
            $re[$k]['hard']=$datas->hard==null?0:$datas->hard;
            $re[$k]['simple']=$datas->simple==null?0:$datas->simple;
            $re[$k]['old']=$datas->old==null?0:$datas->old;
            $re[$k]['money']=$re[$k]['hard']*399+$re[$k]['simple']*169+$re[$k]['old']*199;
            $re[$k]['value']=$re[$k]['hard']+$re[$k]['simple']+$re[$k]['old'];
        }

        $f=$this->all_sale($season);
        $hard_all=$f['hard'];
        $simple_all=$f['simple'];
        $old_all=$f['old'];

        $all=$old_all+$hard_all+$simple_all;
        $money=$old_all*199+$hard_all*399+$simple_all*169;
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $time_start=DB::table('basic')->where('keys','=','round_start_time')->value('value');
        $time_end=DB::table('basic')->where('keys','=','round_end_time')->value('value');
//        $season=21;
//        $d=DB::table('season')->where('season',21)->first();
//        $time_start=$d->round_start_time;
//        $time_end=$d->round_end_time;
        $hard=[];
        $simple=[];
        $old=[];
        $time=[];
        for($i=0;$i<21;$i++){
            $time_s=$i*3600*24+$time_start;
            $time_s_end=($i+1)*3600*24+$time_start-1;
            $data=DB::table('inte_review')
//                ->join('user','user.id','=','inte_review.uid')
//                ->where('user.province','like','%'.$request->province.'%')
//                ->where('inte_review.season','=',$season)
                ->whereBetween('inte_review.time',[$time_s,$time_s_end])
                ->where('inte_review.season','=',$season)
                ->first(
                    array(
                        \DB::raw('SUM(type_one) as hard'),
                        \DB::raw('SUM(type_two) as simple'),
                        \DB::raw('SUM(type_three) as old'),
                    )
                );
            $hard[].=$data->hard==null?0:$data->hard;
            $simple[].=$data->simple==null?0:$data->simple;
            $old[].=$data->old==null?0:$data->old;
            $time[].=date('m/d',$time_s);
        }

        $yes=$this->yes_sale($season,$time_start);
        $yes_hard=(int)$yes['hard'];
        $yes_simple=(int)$yes['simple'];
        $yes_old=(int)$yes['old'];
        $crown=$this->crown_sale($season);
        $service=$this->service_sale($season);
//        $city=$this->sale_type($season);
        $now=Carbon::now()->timestamp;
        if ($now>=$time_start&&$now<$time_end){
            $status=true;
        }else{
            $status=false;
        }
        return $this->success_list_two($re,'',$all,$hard_all,$simple_all,$old_all,$hard,$simple,$old,$time,$status,$money,$yes_hard,$yes_simple,$yes_old,$crown,$service);
    }


    public function sale_type(){
        // dd(DB::table('season')->get());
         $season=DB::table('basic')->where('keys','=','season')->value('value');
        $city=DB::table('city_copy1')->groupBy('type')->select('type')->get();
        $ids=[];
        foreach ($city as $k=>$v){
            $num_all=0;
            $money_all=0;
            $data=DB::table('city_copy1')->where('type',$v->type)->get();
            $res=[];
            foreach ($data as $ke=>$va){
                if ($va->city=='北京市'||$va->city=='上海市'||$va->city=='重庆市'){
                    $id=DB::table('user')->where('province','like','%'.$va->city.'%')->pluck('id');
                }else{
                    $id=DB::table('user')->where('city','like','%'.$va->city.'%')->pluck('id');
                }
//                $ids=array_merge($id,$ids);
                $datas=DB::table('inte_review')
                    ->whereIn('uid',$id)
                    ->where('inte_review.season','=',$season)
                    ->first(
                        array(
                            \DB::raw('SUM(type_one) as hard'),
                            \DB::raw('SUM(type_two) as simple'),
                            \DB::raw('SUM(type_three) as old'),
                        )
                    );
                    $a=$datas->hard==null?0:$datas->hard;
                    $b=$datas->simple==null?0:$datas->simple;
                    $c=$datas->old==null?0:$datas->old;
    //                $num=DB::table('enroll')
    //                    ->where('season',$season)->where('status',0)
    //                    ->whereIn('uid',$id)->count();
                    $res[$ke]['name']=$va->city;
                    $res[$ke]['num']=$a+$b+$c;
                    $res[$ke]['money']=$a*399+$b*179+$c*199;
                    $num_all=$num_all+$res[$ke]['num'];
                    $money_all=$money_all+$res[$ke]['money'];
            }
            if ($v->type=='一线城市'){
                $re[$k]['xh']=0;
            }elseif ($v->type=='新一线城市'){
                $re[$k]['xh']=1;
            }elseif ($v->type=='二线城市'){
                $re[$k]['xh']=2;
            }elseif ($v->type=='三线城市'){
                $re[$k]['xh']=3;
            }elseif ($v->type=='四线城市'){
                $re[$k]['xh']=4;
            }elseif ($v->type=='五线城市'){
                $re[$k]['xh']=5;
            }elseif ($v->type=='其他'){
                $re[$k]['xh']=6;
            }
            $re[$k]['name']=$v->type;
            $re[$k]['num']=$num_all;
            $re[$k]['city']=$res;
           $re[$k]['money']=$money_all;
        }
        $a=[];
        foreach($re as $key=>$val){
            $a[] = $val['xh'];//这里要注意$val['nums']不能为空，不然后面会出问题
        }
//$a先排序
        sort($a);
        $a = array_flip($a);
        $result = array();
        foreach($re as $k=>$v){
            $temp1 = $v['xh'];
            $temp2 = $a[$temp1];
            $result[$temp2] = $v;
        }
//这里还要把$result进行排序，健的位置不对
        sort($result);
//然后就是你想看到的结果了
//        var_dump($result);

        return $result;
    }

    public function crown_sale($season){
        $crown=DB::table('crown_data')->where('type','=',1)->get();
        foreach ($crown as $k=>$v){
            $re[$k]['hard']=$v->hard;
            $re[$k]['nickname']=$v->nickname;
            $re[$k]['simple']=$v->simple;
            $re[$k]['old']=$v->old;
            $re[$k]['value']=$v->total;
            $re[$k]['money']=$v->money;
        }
        return $re;
    }

    public function all_sale($season){
        $datas=DB::table('inte_review')
//            ->join('user','user.id','=','inte_review.uid')
//            ->where('user.province','like',$v.'%')
            ->where('inte_review.season','=',$season)
            ->first(
                array(
                    \DB::raw('SUM(type_one) as hard'),
                    \DB::raw('SUM(type_two) as simple'),
                    \DB::raw('SUM(type_three) as old'),
                )
            );
        $a=$datas->hard==null?0:$datas->hard;
        $b=$datas->simple==null?0:$datas->simple;
        $c=$datas->old==null?0:$datas->old;

//        $hard_all=$hard_all+$a;
//        $simple_all=$simple_all+$b;
//        $old_all=$old_all+$c;
        return ['hard'=>$a,'simple'=>$b,'old'=>$c];
    }

    public function service_sale($season){
        $crown=DB::table('crown_data')->where('type','=',0)->get();
        foreach ($crown as $k=>$v){
            $re[$k]['hard']=$v->hard;
            $re[$k]['nickname']=$v->nickname;
            $re[$k]['war_name']=$v->war_name;
            $re[$k]['simple']=$v->simple;
            $re[$k]['old']=$v->old;
            $re[$k]['value']=$v->total;
            $re[$k]['money']=$v->money;
        }
        return $re;
    }

    public function yes_sale($season,$time_start){
        $today=Carbon::today()->timestamp;
        $data=DB::table('inte_review')
//            ->join('user','user.id','=','inte_review.uid')
//                ->where('user.province','like','%'.$request->province.'%')
            ->where('inte_review.season','=',$season)
            ->whereBetween('inte_review.time',[$time_start,$today-1])
//            ->where('inte_review.season','=',$season)
            ->first(
                array(
                    \DB::raw('SUM(type_one) as hard'),
                    \DB::raw('SUM(type_two) as simple'),
                    \DB::raw('SUM(type_three) as old'),
                )
            );
        $hard=$data->hard==null?0:$data->hard;
        $simple=$data->simple==null?0:$data->simple;
        $old=$data->old==null?0:$data->old;
        return ['hard'=>$hard,'simple'=>$simple,'old'=>$old];
    }

    public function sale_province(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
//        $season=21;
//        $season=21;
        $datas=DB::table('dt_area')->where('area_name','like',"%$request->province%")->where('area_parent_id','=',0)->value('id');
        $data=DB::table('dt_area')->where('area_parent_id',$datas)->get();
        $re=[];
        foreach ($data as $k=>$v){
            $datas=DB::table('inte_review')
                ->join('user','user.id','=','inte_review.uid')
                ->where('user.province','like','%'.$request->province.'%')
                ->where('city','like',"%$v->area_name%")
                ->where('inte_review.season','=',$season)
                ->first(
                    array(
                        \DB::raw('SUM(type_one) as hard'),
                        \DB::raw('SUM(type_two) as simple'),
                        \DB::raw('SUM(type_three) as old'),
                    )
                );
            $re[$k]['name']=$v->area_name;
            $re[$k]['hard']=$datas->hard==null?0:$datas->hard;
            $re[$k]['simple']=$datas->simple==null?0:$datas->simple;
            $re[$k]['old']=$datas->old==null?0:$datas->old;
            $re[$k]['value']=$re[$k]['hard']+$re[$k]['simple']+$re[$k]['old'];
        }
        return $this->success_list($re);
    }

    public function sale_province_time(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
//        $season=21;
        $time_start=DB::table('basic')->where('keys','=','round_start_time')->value('value');
//        $d=DB::table('season')->where('season',21)->first();
//        $time_start=$d->round_start_time;
        $hard=[];
        $simple=[];
        $old=[];
        $time=[];
        for($i=0;$i<21;$i++){
            $time_s=$i*3600*24+$time_start;
            $time_s_end=($i+1)*3600*24+$time_start-1;
            $data=DB::table('inte_review')
                ->join('user','user.id','=','inte_review.uid')
                ->where('user.province','like','%'.$request->province.'%')
                ->where('inte_review.season','=',$season)
                ->whereBetween('inte_review.time',[$time_s,$time_s_end])
                ->where('inte_review.season','=',$season)
                ->first(
                    array(
                        \DB::raw('SUM(type_one) as hard'),
                        \DB::raw('SUM(type_two) as simple'),
                        \DB::raw('SUM(type_three) as old'),
                    )
                );
            $hard[].=$data->hard==null?0:$data->hard;
            $simple[].=$data->simple==null?0:$data->simple;
            $old[].=$data->old==null?0:$data->old;
            $time[].=date('m/d',$time_s);
        }
        $arr=[
            'hard'=>$hard,
            'simple'=>$simple,
            'old'=>$old,
            'time'=>$time
        ];
        return $this->success($arr);
    }
}