<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


class UserController extends Controller{

    /*
     * 登录后获取用户信息
     */
    public function user_info(){
        try{
            $data=DB::table('season')->get();
            $da=[];
            if ($data){
                foreach ($data as $k=>$v){
                    $da[$k]['start']=(int)($v->round_start_time)*1000;
                    $da[$k]['end']=(int)($v->round_end_time-1)*1000;
                    $da[$k]['season']=$v->season;
                }
            }
            $user=Auth::user();
            $role=DB::table('admin_role')->where('name',$user->role)->first();
            return $this->response()->array([
                'code'=>200,
                'data'=>[
                    'id'=>$user->id,
                    'name'=>$user->name,
                    'avatar'=>$user->avatar,
                    'routes'=>$role->routes,
                    'season_list'=>$da
                ]
            ]);
        }catch(Exception $e) {
        }
    }

    /*
     * 修改密码
     */
    public function update_password(Request $request){
        $user=Auth::user();
        if (!hash_equals($user->password,MD5($request->old))) {
            //返回401
            return $this->response->array([
                'code' => 300,
                'message' => '密码错误'
            ]);
        }

        $re=DB::table('admin_user')->where('id',$user->id)->update(['password'=>md5($request->new)]);
        if ($re){
            return $this->success([]);
        }
        return $this->error(500);
    }

    /*
     * 获取所有用户
     */
    public function user_all(Request $request){
        if ($request->has('level')){
            $level=$request->level;
        }else{
            $level=0;
        }
        if ($level==0){
            $user_all=DB::table('user')->where('level_name','<>','政委')->orderBy('id')->paginate(15);
        }elseif ($level==1){
            $user_all=DB::table('user')->where('level_name','=','销售主管')->orderBy('id')->paginate(15);
        }elseif ($level==2){
            $user_all=DB::table('user')->where('level_name','=','销售经理')->orderBy('id')->paginate(15);
        }elseif ($level==3){
            $user_all=DB::table('user')->where('level_name','=','代理公司')->orderBy('id')->paginate(15);
        }

        if ($user_all->items()){
            $data=[];
            foreach ($user_all as $k=>$v){
                if ($v->level_name=='代理公司'){
                    $data[$k]['war_name']=DB::table('service')->where('phone',$v->phone)->value('war_names');
                }else{
                    $data[$k]['war_name']=DB::table('service')->where('phone',$v->up_phone)->value('war_names');
                }
                $data[$k]['avatar']=$v->avatar;
                $data[$k]['nickname']=$v->nickname;
                $data[$k]['phone']=$v->phone;
                $data[$k]['level_name']=$v->level_name;
                $data[$k]['up_phone']=$v->up_phone;
                $data[$k]['up_name']=$v->up_name;
            }
//            $total=$user->total();
        }else{
            $data=[];
        }
        return $this->response()->array([
            'code'=>200,
            'data'=>[
                'total'=>$user_all->total(),
                'list'=>$data
            ]
        ]);
    }

    /*
     * 用户修改
     */
    public function user_update(Request $request){
        $data['level_name']=$request->level_name;
        $data['up_name']=$request->up_name;
        $re=DB::table('user')->where('id',$request->id)->update([
            'level_name'=>$request->level_name,
            'up_name'=>$request->up_name,
        ]);
        if ($re){
            return $this->success([]);
        }
        return $this->error(400);
    }

    /*
     * 查找用户
     */
    public function user_search(Request $request){
        $string=$request->phone;
        if (is_numeric($string)){
            $s='user.phone';
        }else{
            $s='user.nickname';
        }
        $user=DB::table('user')->where($s,'like','%'.$string.'%')->paginate(15);
        $total=0;
        if ($user->items()){
            $data=[];
            foreach ($user as $k=>$v){
                if ($v->level_name=='代理公司'){
                $data[$k]['war_name']=DB::table('service')->where('phone',$v->phone)->value('war_names');
                }else{
                $data[$k]['war_name']=DB::table('service')->where('phone',$v->up_phone)->value('war_names');
                }
                $data[$k]['avatar']=$v->avatar;
                $data[$k]['nickname']=$v->nickname;
                $data[$k]['phone']=$v->phone;
                $data[$k]['level_name']=$v->level_name;
                $data[$k]['up_phone']=$v->up_phone;
                $data[$k]['up_name']=$v->up_name;
            }
                $total=$user->total();
        }else{
            $data=[];
        }
        return $this->success_list($data,200,$total);
    }

    //获取用户售货信息
    public function user_sold(Request $request){
        $user=Auth::user();
        $id = $request->id;
        //获取最新赛季标识
        $season = DB::table('basic')->where('keys','=','season')->value('value');
        //精装版销售量
        $hard_num = DB::table('inte_review')->where([['uid','=',$id],['season','=',$season]
        ])->select('type_one')->sum('type_one');
        //简约版销售量
        $simple_num = DB::table('inte_review')->where([['uid','=',$id],['season','=',$season]
        ])->select('type_two')->sum('type_two');
        //老人版销售量
        $old_num = DB::table('inte_review')->where([['uid','=',$id],['season','=',$season]
        ])->select('type_three')->sum('type_three');
        //销售总量,销售总额
        $total_order = DB::table('user')->where('id','=',$id)->select('integral','money','total')->first();
        //今日学分，取出今日范围内的inte_log表内容
        $today_score = DB::table('inte_log')
            ->where('uid','=',$id)
            ->whereBetween('created_at',[Carbon::today(),Carbon::now()])->sum('integral');
        //昨日学分数
        $yes_score = DB::table('inte_log')->where('uid','=',$id)->whereBetween('created_at',[Carbon::yesterday(),Carbon::today()])->sum('integral');
        //今日总销量
        $today_num1 = DB::table('inte_review')->where('uid','=',$id)->whereBetween('created_at',[Carbon::today(),Carbon::now()])->sum('type_one');
        $today_num2 = DB::table('inte_review')->where('uid','=',$id)->whereBetween('created_at',[Carbon::today(),Carbon::now()])->sum('type_two');
        $today_num3 = DB::table('inte_review')->where('uid','=',$id)->whereBetween('created_at',[Carbon::today(),Carbon::now()])->sum('type_three');
        $today_num = $today_num1+$today_num2+$today_num3;
        //昨日销量
        $yes_num1 = DB::table('inte_review')->where('uid','=',$id)->whereBetween('created_at',[Carbon::yesterday(),Carbon::today()])->sum('type_one');
        $yes_num2 = DB::table('inte_review')->where('uid','=',$id)->whereBetween('created_at',[Carbon::yesterday(),Carbon::today()])->sum('type_two');
        $yes_num3 = DB::table('inte_review')->where('uid','=',$id)->whereBetween('created_at',[Carbon::yesterday(),Carbon::today()])->sum('type_three');
        $yes_num = $yes_num1+$yes_num2+$yes_num3;
        return $this->response()->array([
            'code'=>200,
            'data'=>[
                "total_order"=>$total_order->total,
                "total_sales"=>$total_order->money,
                "total_score"=>$total_order->integral,
                "today_score"=>$today_score,
                "yes_score"=>$yes_score,
                "hard_num"=>$hard_num,
                "simple_num"=>$simple_num,
                "old_num"=>$old_num,
                "today_num"=>$today_num,
                "yes_num"=>$yes_num,
            ]
        ]);

    }

}