StatController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Community\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Docter;
  5. use App\Models\Order;
  6. use App\Models\Schedule;
  7. use App\Models\SchedulePeriod;
  8. use App\Models\Vaccine;
  9. use Encore\Admin\Facades\Admin;
  10. use Illuminate\Support\Facades\DB;
  11. class StatController extends Controller
  12. {
  13. public static $org_id;
  14. public function __construct()
  15. {
  16. self::$org_id = Admin::user()->org_id;;
  17. }
  18. //疫苗统计
  19. public static function vaccine()
  20. {
  21. $where = [];
  22. if(self::$org_id){
  23. $where['organization_id']=self::$org_id;
  24. }
  25. $order = Order::where(['product_type'=>4])->where($where);
  26. $today = $order->whereHas('orderPatient',function ($query){
  27. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1days')]);
  28. })->count();
  29. $tomorrow = $order->whereHas('orderPatient',function ($query){
  30. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1days')),date('Y-m-d',time())]);
  31. })->whereNotIn('order_status',[1,5,6])->count();
  32. //昨日接诊人数
  33. $today = $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  34. $data ['sum_num'] = Vaccine::where($where)->count();
  35. $data['have_num'] = Vaccine::where($where)->sum('stock');
  36. $data['less_num'] = Vaccine::where($where)->where(['stock'=>0])->count();//取苗钟数
  37. $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();
  38. $data['today_vaccine'] = Order::where(['product_type'=>4])->where($where)->with('orderPatient','appoint_start_time',strtotime('today'))->count();//接诊患者(人次)
  39. return $data;
  40. }
  41. //实时数据
  42. public static function service()
  43. {
  44. $where = [];
  45. if(self::$org_id){
  46. $where['organization_id']=self::$org_id;
  47. }
  48. $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();
  49. $data['schedule_docter'] = SchedulePeriod::distinct('docter_id')->where($where)->where('schedule_date',date('Y-m-d H:i:s',time()))->count();
  50. // $data['paitent_num'] = Order::where(['order_status'=>1])->distinct()->count()->BgroupBy('patient_id');
  51. $data['paitent_num'] = Order::whereHas('orderPatient',function ($query){
  52. $query->where(['appoint_start_time'=>strtotime('today')]);
  53. })->where(['product_type'=>4])->where($where)->count();//接诊患者(人次)
  54. return $data;
  55. }
  56. //订单概况
  57. public static function nurse()
  58. {
  59. //3.门诊预约 4.疫苗接种预约 5.儿保预约
  60. $where = [];
  61. if(self::$org_id){
  62. $where['organization_id']=self::$org_id;
  63. }
  64. $nurse_num[] = Order::where('product_type',3)->where($where)->count();
  65. $nurse_num[] = Order::where('product_type',4)->where($where)->count();
  66. $nurse_num[] = Order::where('product_type',5)->where($where)->count();
  67. return $nurse_num;
  68. }
  69. //排班统计
  70. public static function schedule()
  71. {
  72. $where = [];
  73. if(self::$org_id){
  74. $where['organization_id']=self::$org_id;
  75. }
  76. $ids = SchedulePeriod::where('schedule_date',date("Y-m-d"))->where($where)->distinct('docter_id')->pluck('docter_id')->toArray();
  77. $data = ['clinic_num'=>0,'vaccine_num'=>0,'nurse_num'=>0];
  78. if(empty($ids)){
  79. return $data;
  80. };
  81. $docters = Docter::whereIn('id',$ids)->get(['id','type'])->GroupBy('type');
  82. foreach ($docters as $key => $val){
  83. if($key === 1){
  84. $data['clinic_num'] = count($val);
  85. } else if($key == 2){
  86. $data['vaccine_num'] = count($val);
  87. } else if($key == 3){
  88. $data['nurse_num'] = count($val);
  89. }
  90. }
  91. return $data;
  92. }
  93. //门诊统计
  94. public static function clinc()
  95. {
  96. $where = [];
  97. if(self::$org_id){
  98. $where['organization_id']=self::$org_id;
  99. }
  100. $order = Order::where(['product_type'=>3])->where($where);
  101. //今日预约数
  102. $today = $order->where('created_at','>=',date('Y-m-d'))->whereNotIn('order_status',[1,5,6])->count();
  103. //医生排班人数
  104. $docters = SchedulePeriod::where(['schedule_date'=>date('Y-m-d',time())])->distinct('docter_id')->count('docter_id');
  105. //昨日接诊人数
  106. $yesterday = $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  107. //明日预约数
  108. $tomorrow = $order->whereHas('orderPatient',function ($query){
  109. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1days')]);
  110. })->whereNotIn('order_status',[1,5,6])->count();
  111. $data['today'] = $today;
  112. $data['yesterday'] = $yesterday;
  113. $data['docters'] = $docters;
  114. $data['tomorrow'] = $tomorrow;
  115. return $data;
  116. }
  117. }