123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace App\Community\Controllers;
- use App\Http\Controllers\Controller;
- use App\Models\Docter;
- use App\Models\Order;
- use App\Models\OrderVaccine;
- 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;
- }
- $order = Order::where(['product_type'=>4])->where($where);
- //今日预约
- $today = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1days')]);
- })->count();
- //明日预约
- $tomorrow = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('+1 days')),date('Y-m-d',strtotime('+2 days'))]);
- })->whereNotIn('order_status',[1,5,6])->count();
- //昨日接诊人数
- $people = $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
- //昨日预约
- $yesterday = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
- })->whereNotIn('order_status',[1,5,6])->count();
- $data['today'] = $today;
- $data['yesterday'] = $yesterday;
- $data['people'] = $people;
- $data['tomorrow '] = $tomorrow;
- // $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 nurse()
- {
- $where = [];
- if(self::$org_id){
- $where['organization_id']=self::$org_id;
- }
- $order = Order::where(['product_type'=>5])->where($where);
- //今日预约
- $today = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1days')]);
- })->count();
- //明日预约
- $tomorrow = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('+1 days')),date('Y-m-d',strtotime('+2 days'))]);
- })->whereNotIn('order_status',[1,5,6])->count();
- //昨日服务人数
- $people = $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
- //昨日预约
- $yesterday = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
- })->whereNotIn('order_status',[1,5,6])->count();
- $data['today'] = $today;
- $data['tomorrow'] = $tomorrow;
- $data['people'] = $people;
- $data['yesterday'] = $yesterday;
- return $data;
- }
- //疫苗统计
- public static function vaccineToal()
- {
- $where = [];
- if(self::$org_id){
- $where['organization_id']=self::$org_id;
- }
- $data['have_num'] = Vaccine::where($where)->sum('stock'); //疫苗库存总数
- $data['less_num'] = Vaccine::where($where)->where(['stock'=>0])->count();//缺苗钟数
- $ids = Order::where(['product_type'=>4])->where($where)->with('orderPatient','appoint_start_time',strtotime('today'))->pluck('id')->toArray();//订单id
- $data['yesterday']= OrderVaccine::whereIn('order_id',$ids)->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 orderTotal()
- {
- //3.门诊预约 4.疫苗接种预约 5.儿保预约
- $where = [];
- if(self::$org_id){
- $where['organization_id']=self::$org_id;
- }
- //todo 是否需要区别状态
- $order_num[] = Order::where('product_type',3)->where($where)->count();
- $order_num[] = Order::where('product_type',4)->where($where)->count();
- $order_num[] = Order::where('product_type',5)->where($where)->count();
- $order_num[] = Order::whereIn('product_type',[3,4,5])->where($where)->count();
- return $order_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;
- }
- //门诊统计
- public static function clinc()
- {
- $where = [];
- if(self::$org_id){
- $where['organization_id']=self::$org_id;
- }
- $order = Order::where(['product_type'=>3])->where($where);
- //今日预约数
- $today = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1 days')]);
- })->whereNotIn('order_status',[1,5,6])->count();
- //医生排班人数
- $docters = SchedulePeriod::where(['schedule_date'=>date('Y-m-d',time())])->where($where)->distinct('docter_id')->count('docter_id');
- //昨日预约
- $yesterday = $order->whereHas('orderPatient',function ($query){
- $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
- })->whereNotIn('order_status',[1,5,6])->count();
- //昨日接诊人数
- $people= $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
- $data['today'] = $today;
- $data['docters'] = $docters;
- $data['yesterday'] = $yesterday;
- $data['people'] = $people;
- return $data;
- }
- }
|