<?php

namespace App\Http\Controllers;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class IndexController extends Controller{
/*
 *
 * $d['护心卡']=$v->gift_id==1?$value->num:'';
  //              $d['手环']=$v->gift_id==2?$value->num:'';
    //            $d['鞋垫']=$v->gift_id==3?$value->num:'';
      //          $d['项链']=$v->gift_id==6?$value->num:'';
        //        $d['丝巾']=$v->gift_id==13?$value->num:'';
          //      $d['护腰']=$v->gift_id==14?$value->num:'';
            //    $d['秋衣秋裤 女M']=$v->gift_id==15?$value->num:'';
              //  $d['秋衣秋裤  女L号']=$v->gift_id==16?$value->num:'';
                //$d['秋衣秋裤女 XL号']=$v->gift_id==17?$value->num:'';
 //               $d['秋衣秋裤女 2XL号']=$v->gift_id==18?$value->num:'';
   //             $d['秋衣秋裤 男 XL号']=$v->gift_id==19?$value->num:'';
     //           $d['秋衣秋裤 男 2XL号']=$v->gift_id==20?$value->num:'';
       //         $d['秋衣秋裤 男 3XL号']=$v->gift_id==21?$value->num:'';
         //       $d['超能量T恤  M号']=$v->gift_id==22?$value->num:'';
           //     $d['超能量T恤  L号']=$v->gift_id==23?$value->num:'';
             //   $d['超能量T恤 XL号']=$v->gift_id==24?$value->num:'';
               // $d['超能量T恤 3XL号']=$v->gift_id==25?$value->num:'';
                //$d['超能量T恤 4XL号']=$v->gift_id==26?$value->num:'';
                //$d['口罩']=$v->gift_id==27?$value->num:'';
 *
 * */

        public function indexs(Request $request){

            $data=DB::table('city')->groupBy('type')->get();
            foreach ($data as $k=>$v){
                $re=DB::table('city')->where('type',$v->type)->get();
                foreach ($re as $ke=>$va){
                    DB::table('city_copy1')->where('city','=',$va->city.'市')->update(['type'=>$v->type]);
                }
            }
            return 's';
            $data='石家庄市、唐山市、秦皇岛市、邯郸市、邢台市、保定市、张家口市、承德市、沧州市、廊坊市、衡水市、太原市、大同市、阳泉市、长治市、晋城市、朔州市、晋中市、运城市、忻州市、临汾市、吕梁市、呼和浩特市、包头市、乌海市、赤峰市、通辽市、鄂尔多斯市、呼伦贝尔市、巴彦淖尔市、乌兰察布市、沈阳市、大连市、鞍山市、抚顺市、本溪市、丹东市、锦州市、营口市、阜新市、辽阳市、盘锦市、铁岭市、朝阳市、葫芦岛市、长春市、吉林市、四平市、辽源市、通化市、白山市、松原市、白城市、哈尔滨市、齐齐哈尔市、鸡西市、鹤岗市、双鸭山市、大庆市、伊春市、佳木斯市、七台河市、牡丹江市、黑河市、绥化市、南京市、无锡市、徐州市、常州市、苏州市、南通市、连云港市、淮安市、盐城市、扬州市、镇江市、泰州市、宿迁市、杭州市、宁波市、温州市、嘉兴市、湖州市、绍兴市、金华市、衢州市、舟山市、台州市、丽水市、合肥市、芜湖市、蚌埠市、淮南市、马鞍山市、淮北市、铜陵市、安庆市、黄山市、阜阳市、宿州市、滁州市、六安市、宣城市、池州市、亳州市、福州市、厦门市、莆田市、三明市、泉州市、漳州市、南平市、龙岩市、宁德市、南昌市、景德镇市、萍乡市、九江市、抚州市、鹰潭市、赣州市、吉安市、宜春市、新余市、上饶市、济南市、青岛市、淄博市、枣庄市、东营市、烟台市、潍坊市、济宁市、泰安市、威海市、日照市、临沂市、德州市、聊城市、滨州市、菏泽市、郑州市、开封市、洛阳市、平顶山市、安阳市、鹤壁市、新乡市、焦作市、濮阳市、许昌市、漯河市、三门峡市、南阳市、商丘市、信阳市、周口市、驻马店市、武汉市、黄石市、十堰市、宜昌市、襄阳市、鄂州市、荆门市、孝感市、荆州市、黄冈市、咸宁市、随州市、长沙市、株洲市、湘潭市、衡阳市、邵阳市、岳阳市、常德市、张家界市、益阳市、郴州市、永州市、怀化市、娄底市、广州市、韶关市、深圳市、珠海市、汕头市、佛山市、江门市、湛江市、茂名市、肇庆市、惠州市、梅州市、汕尾市、河源市、阳江市、清远市、东莞市、中山市、潮州市、揭阳市、云浮市、南宁市、柳州市、桂林市、梧州市、北海市、防城港市、钦州市、贵港市、玉林市、百色市、贺州市、河池市、来宾市、崇左市、海口市、三亚市、三沙市、儋州市、成都市、自贡市、攀枝花市、泸州市、德阳市、绵阳市、广元市、遂宁市、内江市、乐山市、南充市、眉山市、宜宾市、广安市、达州市、雅安市、巴中市、资阳市、贵阳市、六盘水市、遵义市、安顺市、毕节市、铜仁市、昆明市、曲靖市、玉溪市、保山市、昭通市、丽江市、普洱市、临沧市、拉萨市、日喀则市、昌都市、林芝市、山南市、那曲市、西安市、铜川市、宝鸡市、咸阳市、渭南市、延安市、汉中市、榆林市、安康市、商洛市、兰州市、嘉峪关市、金昌市、白银市、天水市、武威市、张掖市、平凉市、酒泉市、庆阳市、定西市、陇南市、西宁市、海东市、银川市、石嘴山市、吴忠市、固原市、中卫市、乌鲁木齐市、克拉玛依市、吐鲁番市、哈密市';
            $arr=explode('、',$data);
            foreach ($arr as $k=>$v){
                DB::table('city_copy1')->insert(['city'=>$v]);
            }
            return 's';
            dd($arr);
            $ds=DB::table('user')->whereIn('level_name',['销售主管','销售经理'])->groupBy('city')->get();
            $i=0;
            $re=[];
            $res=DB::table('city')->get();
            $arr=[];
            foreach ($res as  $ke=>$va){
//                var_dump($va->city);
                if ($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=$id->toArray();
                $arr=array_merge($ids,$arr);
//                if ($num==0){
//                    $re[$i]=$va->city;
//                    $i++;
//                }
            }
            $data=DB::table('user')->whereIn('level_name',['销售主管','销售经理'])
                ->whereIn('id',$arr)->groupBy('city')->get();
            dd($data);
            $d=DB::table('city')->get();

           $data=DB::table('data_his')->where('season',20)->where('type',3)->value('data');
//           $re=json_decode($data);
//           $re=json_encode($re);
           var_dump($data);
           return $data;
//           return $this->success($re);
        }


    public function index(Request $request){
            $data=DB::table('gift_orders')->where('id','>',6985)
                ->where('created_at','>','2020-03-27 10:30:00')
                ->where('created_at','<','2020-03-27 12:00:00')
                ->groupBy('uid')->get();
            $datas=[];
            foreach ($data as $k=>$v){
                $user=DB::table('user')->where('id',$v->uid)->first();
                $datas[$k]['昵称']=$user->nickname;
                $datas[$k]['等级']=$user->level_name;
                $datas[$k]['剩余奖学金']=$user->bonus;
                $datas[$k]['手机号']=$user->phone;
                $datas[$k]['上级昵称']=$user->up_name;
                $datas[$k]['上级手机号']=$user->up_phone;
            }
            return $this->success_list($datas);

        $user=DB::table('user')
            ->join('service','service.phone','=','user.up_phone')
            ->join('enroll','enroll.uid','=','user.id')
            ->where('enroll.season',20)
            ->where('enroll.is_refund',0)
            ->where('enroll.status',0)
            ->where('user.integral','>=','30')
            ->whereIn('user.level_name',['销售主管','销售经理'])
            ->select('user.nickname as 昵称','user.phone as 手机号','user.level_name as 等级','user.up_name as 上级名称','user.up_phone as 上级手机号','service.war_names as 战区')->get();
         return $this->success_list($user);
        $num=0;
        foreach ($data1 as $k=>$v){
            $num=$num+$v->total;
        }
        return $num;
        $data2=DB::table('inte_review')
            ->where('inte_review.season',20)
            ->where('inte_review.time','>=',$time+86400*1)
            ->where('inte_review.time','<',$time+7*86400*(1+1))
            ->sum("type_two");
        $data3=DB::table('inte_review')
            ->where('inte_review.season',20)
            ->where('inte_review.time','>=',$time+86400*1)
            ->where('inte_review.time','<',$time+7*86400*(1+1))
            ->sum("type_three");
            return $data1+$data2+$data3;

        return $data;
//            DB::table('service')->groupBy('')->
        $user=User::where('role','=','客服')->paginate(5);
        return $user;
            Log::error('{--------------------}'.Carbon::now()->toDateTimeString());
        $per_num=DB::table('user')->join('glory','glory.uid','=','user.id')
            ->where('glory.season',$request->season)
            ->where('glory.integral','<>',0)
            ->count();
        return $this->success($per_num);
//            $time=1577116800-1;
//        $time_end=(Carbon::tomorrow()->timestamp)-1;
//        $day=($time_end-$time)/86400;
//        $day=ceil($day);//进一取整的天数
        $data=DB::table('season')->where('season',19)->first();
        return '当前赛季'.$data->season.'==================='.Carbon::now();
//        DB::table('user')->join('service','service.phone','=','user.up_phone')

        $data=DB::table('gift_orders')->groupBy('uid')->get();
//        dd($data);
//        $data=DB::table('gift_orders')->where('uid',3274)->get();
        foreach ($data as $k=>$v){
            $ss=[];
//            $sss=[];
            $s=DB::table('gift_orders')->where('gift_orders.uid',$v->uid)->get();
            $datas=[];
            $ds=['护心卡*99'=>0,'手环*199'=>0,'鞋垫*199'=>0,'项链*399'=>0,'丝巾*399'=>0,'护腰*799'=>0,'秋衣秋裤 女M*1699'=>0,'秋衣秋裤 女L号*1699'=>0,'秋衣秋裤 女XL号*1699'=>0,'秋衣秋裤 女2XL号*1699'=>0,'秋衣秋裤 男XL号*1699'=>0,'秋衣秋裤 男2XL号*1699'=>0,'秋衣秋裤 男3XL号*1699'=>0,'超能量T恤 M号*799'=>0,'超能量T恤 L号*799'=>0,'超能量T恤 XL号*799'=>0,'超能量T恤 3XL号*799'=>0,'超能量T恤 4XL号*799'=>0,'口罩*199'=>0];
            $bonus=0;
            foreach ($s as $key=>$value){
                $datas[]="$value->name*$value->num";
                if ($value->gift_id==1){
                    $ds['护心卡*99']=$value->num;
                    $bonus=$bonus+99*$value->num;
                }
                elseif ($value->gift_id==2){
                    $ds['手环*199']=$value->num;
                    $bonus=$bonus+199*$value->num;
                }
                elseif ($value->gift_id==3){
                    $ds['鞋垫*199']=$value->num;
                    $bonus=$bonus+199*$value->num;
                }
                elseif ($value->gift_id==6){
                    $ds['项链*399']=$value->num;
                    $bonus=$bonus+399*$value->num;
                }
                elseif ($value->gift_id==13){
                    $ds['丝巾*399']=$value->num;
                    $bonus=$bonus+399*$value->num;
                }
                elseif ($value->gift_id==14){
                    $ds['护腰*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==15){
                    $ds['秋衣秋裤 女M*1699']=$value->num;
                    $bonus=$bonus+1699*$value->num;
                }
                elseif ($value->gift_id==16){
                    $ds['秋衣秋裤 女L号*1699']=$value->num;
                    $bonus=$bonus+1699*$value->num;
                }
                elseif ($value->gift_id==17){
                    $ds['秋衣秋裤 女XL号*1699']=$value->num;
                    $bonus=$bonus+1699*$value->num;
                }
                elseif ($value->gift_id==18){
                    $ds['秋衣秋裤 女2XL号*1699']=$value->num;
                    $bonus=$bonus+1699*$value->num;
                }
                elseif ($value->gift_id==19){
                    $ds['秋衣秋裤 男XL号*1699']=$value->num;
                    $bonus=$bonus+1699*$value->num;
                }
                elseif ($value->gift_id==20){
                    $ds['秋衣秋裤 男2XL号*1699']=$value->num;
                    $bonus=$bonus+1699*$value->num;
                }
                elseif ($value->gift_id==21){
                    $ds['秋衣秋裤 男3XL号*1699']=$value->num;
                    $bonus=$bonus+1699*$value->num;
                }
                elseif ($value->gift_id==22){
                    $ds['超能量T恤 M号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==23){
                    $ds['超能量T恤 L号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==24){
                    $ds['超能量T恤 XL号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==25){
                    $ds['超能量T恤 3XL号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==26){
                    $ds['超能量T恤 4XL号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==27){
                    $ds['口罩*199']=$value->num;
                    $bonus=$bonus+199*$value->num;
                }
            }
            $user=DB::table('user')->where('id',$v->uid)->first();
            $glory=DB::table('glory')->where('uid',$v->uid)->first();


            if ($user){
                $ss['昵称']=$user->nickname;
                $ss['手机号']=$user->phone;
                $ss['销售等级']=$user->level_name;
                if ($user->level_name=='代理公司'){
                    $ss['收货人']=$user->nickname;
                    $ss['收货人手机号']=$user->phone;
                    $ss['战区']=DB::table('service')->where('phone',$user->phone)->value('war_names').'战区';
                }else{
                    $ss['收货人']=$user->up_name;
                    $ss['收货人手机号']=$user->up_phone;
                    $ss['战区']=DB::table('service')->where('phone',$user->up_phone)->value('war_names').'战区';
                }
            }else{
                $ss['昵称']='出错了';
                $ss['手机号']='出错了';
                $ss['销售等级']='';

                $ss['收货人']='';
                $ss['收货人手机号']='';

            }

            if ($glory){
                $ss['奖学金']=$glory->bonus;
            }else{
                $ss['奖学金']=$bonus+$user->bonus;
            }

            $ss['兑换详情']=implode(',',$datas);
            $sss=$ds;
            $ssss[$k]=array_merge($ss,$sss);
//            $ss[$k]['pho']=implode(',',$datas);
        }
//        dd($ss);
        return $this->success_list($ssss);

        $data=DB::table('user')->where('level_name','=','代理公司')->select('nickname','phone','bonus')->orderByDesc('bonus')->get();
        return $this->success_list($data);


        $sss=DB::table('user')->join('service','service.phone','=','user.up_phone')
            ->join('enroll','enroll.uid','=','user.id')
            ->whereIn('user.level_name',['销售经理','销售主管'])
            ->where('enroll.status',0)
            ->where('user.integral','>=','30')
            ->select('user.nickname as 昵称','user.phone as 手机号','user.level_name as 销售等级','user.integral as 学分','user.up_name as 皇冠昵称','user.up_phone as 皇冠手机号','service.war_names as 战区')->get();
        return $this->success_list($sss);
        $start=DB::table('basic')->where('keys','=','round_start_time')->value('value');
        $data=DB::table('user')
            ->where('user.enroll_status',0)
            ->leftJoin('inte_review','user.id','=','inte_review.uid')
            ->whereIn('user.level_name',['销售主管','销售经理'])
            ->where(function ($query) use($start) {
                $query->where('inte_review.created_at','>',date('Y-m-d H:i:s',$start))
                    ->orWhereNull('inte_review.created_at');
            })
            ->groupBy('user.id')
            ->select(DB::raw("SUM(type_one) as hard,SUM(type_two) as simple,SUM(type_three) as old"),'user.id','user.integral','user.nickname','user.up_name','user.phone','user.up_phone','user.war_names')
            ->get()->toArray();
        return $this->success_list($data);
        return 's';

//        DB::table('user_test')->where('id',1)->increment('integral',-100);
        return 's';
        $dat=DB::table('user')->join('enroll','enroll.uid','=','user.id')
            ->where('enroll.season','=',$request->season) ->select('user.level_name',DB::raw('count(*) as num'))
            ->groupBy('user.level_name')
            ->get();
        return $dat;
    }


    public function save_test(Request $request){
        DB::table('article')->insert([
            'body'=>$request->body
        ]);
        return $this->success([]);
    }
    public function save_test_1(Request $request){
        $data=DB::table('article')->where('id',1)->first();
        return $this->success($data);
    }

    public function user_info(Request $request){
        $user=DB::table('user')->where('id',$request->id)->first();
        return $this->success($user);
    }

//    public function user_sold(Request $request){
//    }

    public function user_address(Request $request){
        $dat=DB::table('address')->where('uid',$request->id)->get();
        $data=[];
        if ($dat){
            $data=$dat;
        }
        return $this->success_list($data,'ok');
    }

    public function user_order(Request $request){
        $data=DB::table('gift_orders')->join('address','address.id','=','gift_orders.address_id')->join('gift','gift.id','=','gift_orders.gift_id')
            ->where('gift_orders.uid',$request->id)
            ->select('gift.name as gift_name','gift_orders.*','address.address as address','address.phone','address.name as username')
            ->orderByDesc('gift_orders.created_at')->paginate(5);
        $total=0;
        $da=[];
        if ($data->items()){
            foreach ($data->items() as $k=>$v){
                $da[$k]['order_num']=$v->order_number;
                $da[$k]['time']=strtotime($v->created_at)*1000;
                $da[$k]['gift_name']=$v->gift_name;
                $da[$k]['gift_num']=$v->num;
                $da[$k]['username']=$v->username;
                $da[$k]['phone']=$v->phone;
                $da[$k]['address']=$v->address;
                $da[$k]['status']=$v->shipping_status==0?false:true;
                $da[$k]['express_order']=$v->tracking_number;
            }
            $total=$data->total();
        }
        return $this->success_list($da,'ok',$total);
    }
}