<?php


namespace App\Community\Controllers;


use App\Http\Controllers\Controller;
use App\Models\Docter;
use App\Models\Order;
use App\Models\Schedule;
use App\Models\SchedulePeriod;
use App\Models\Vaccine;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;

class StatController extends  Controller
{

    public static $org_id;
    public function __construct()
    {
        self::$org_id = Admin::user()->org_id;;
    }

    //疫苗统计
    public static function vaccine()
    {
        $where = [];
        if(self::$org_id){
            $where['organization_id']=self::$org_id;
        }
        $data ['sum_num'] = Vaccine::where($where)->count();
        $data['have_num'] = Vaccine::where($where)->sum('stock');
        $data['less_num'] = Vaccine::where($where)->where(['stock'=>0])->count();//取苗钟数
        $data['today_schedule'] = Order::where(['order_status'=>1])->where($where)->where('product_type',4)->where('created_at','>',date('Y-m-d H:i:s',time()))->count();
        $data['today_vaccine'] = Order::where(['product_type'=>4])->where($where)->with('orderPatient','appoint_start_time',strtotime('today'))->count();//接诊患者（人次)
        return $data;

    }



    //实时数据
    public static function  service()
    {
        $where = [];
        if(self::$org_id){
            $where['organization_id']=self::$org_id;
        }
        $data['schedule_paitent'] = Order::where(['order_status'=>1])->where($where)->whereIn('product_type',[3,4,5])->where('created_at','>',date('Y-m-d H:i:s',time()))->count();
        $data['schedule_docter'] = SchedulePeriod::distinct('docter_id')->where($where)->where('schedule_date',date('Y-m-d H:i:s',time()))->count();
//        $data['paitent_num'] = Order::where(['order_status'=>1])->distinct()->count()->BgroupBy('patient_id');
        $data['paitent_num'] = Order::whereHas('orderPatient',function ($query){
            $query->where(['appoint_start_time'=>strtotime('today')]);
        })->where(['product_type'=>4])->where($where)->count();//接诊患者（人次)

return $data;


    }

    //订单概况
    public static function nurse()
    {
        //3.门诊预约 4.疫苗接种预约 5.儿保预约
         $where = [];
        if(self::$org_id){
             $where['organization_id']=self::$org_id;
         }
         $nurse_num[] = Order::where('product_type',3)->where($where)->count();
         $nurse_num[] = Order::where('product_type',4)->where($where)->count();
         $nurse_num[] = Order::where('product_type',5)->where($where)->count();
         return $nurse_num;
    }

    //排班统计
    public static function schedule()
    {
        $where = [];
        if(self::$org_id){
            $where['organization_id']=self::$org_id;
        }

        $ids = SchedulePeriod::where('schedule_date',date("Y-m-d"))->where($where)->distinct('docter_id')->pluck('docter_id')->toArray();
        $data = ['clinic_num'=>0,'vaccine_num'=>0,'nurse_num'=>0];
        if(empty($ids)){
            return  $data;
        };
        $docters = Docter::whereIn('id',$ids)->get(['id','type'])->GroupBy('type');
        foreach ($docters as $key => $val){
            if($key === 1){
                $data['clinic_num'] = count($val);
            } else if($key == 2){
                $data['vaccine_num'] = count($val);
            } else if($key == 3){
                $data['nurse_num'] = count($val);
            }
        }
        return $data;
    }
}
