StatController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\OrderVaccine;
  7. use App\Models\Schedule;
  8. use App\Models\SchedulePeriod;
  9. use App\Models\Vaccine;
  10. use Encore\Admin\Facades\Admin;
  11. use Illuminate\Support\Facades\DB;
  12. class StatController extends Controller
  13. {
  14. public static $org_id;
  15. public function __construct()
  16. {
  17. self::$org_id = Admin::user()->org_id;;
  18. }
  19. //疫苗预约
  20. public static function vaccine()
  21. {
  22. $where = [];
  23. if(self::$org_id){
  24. $where['organization_id']=self::$org_id;
  25. }
  26. $order = Order::where(['product_type'=>4])->where($where);
  27. //今日预约
  28. $today = $order->whereHas('orderPatient',function ($query){
  29. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1days')]);
  30. })->count();
  31. //明日预约
  32. $tomorrow = $order->whereHas('orderPatient',function ($query){
  33. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('+1 days')),date('Y-m-d',strtotime('+2 days'))]);
  34. })->whereNotIn('order_status',[1,5,6])->count();
  35. //昨日接诊人数
  36. $people = $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  37. //昨日预约
  38. $yesterday = $order->whereHas('orderPatient',function ($query){
  39. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
  40. })->whereNotIn('order_status',[1,5,6])->count();
  41. $data['today'] = $today;
  42. $data['yesterday'] = $yesterday;
  43. $data['people'] = $people;
  44. $data['tomorrow '] = $tomorrow;
  45. // $data ['sum_num'] = Vaccine::where($where)->count();
  46. // $data['have_num'] = Vaccine::where($where)->sum('stock');
  47. // $data['less_num'] = Vaccine::where($where)->where(['stock'=>0])->count();//取苗钟数
  48. // $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();
  49. // $data['today_vaccine'] = Order::where(['product_type'=>4])->where($where)->with('orderPatient','appoint_start_time',strtotime('today'))->count();//接诊患者(人次)
  50. return $data;
  51. }
  52. //儿保
  53. public static function nurse()
  54. {
  55. $where = [];
  56. if(self::$org_id){
  57. $where['organization_id']=self::$org_id;
  58. }
  59. $order = Order::where(['product_type'=>5])->where($where);
  60. //今日预约
  61. $today = $order->whereHas('orderPatient',function ($query){
  62. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1days')]);
  63. })->count();
  64. //明日预约
  65. $tomorrow = $order->whereHas('orderPatient',function ($query){
  66. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('+1 days')),date('Y-m-d',strtotime('+2 days'))]);
  67. })->whereNotIn('order_status',[1,5,6])->count();
  68. //昨日服务人数
  69. $people = $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  70. //昨日预约
  71. $yesterday = $order->whereHas('orderPatient',function ($query){
  72. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
  73. })->whereNotIn('order_status',[1,5,6])->count();
  74. $data['today'] = $today;
  75. $data['tomorrow'] = $tomorrow;
  76. $data['people'] = $people;
  77. $data['yesterday'] = $yesterday;
  78. return $data;
  79. }
  80. //疫苗统计
  81. public static function vaccineToal()
  82. {
  83. $where = [];
  84. if(self::$org_id){
  85. $where['organization_id']=self::$org_id;
  86. }
  87. $data['have_num'] = Vaccine::where($where)->sum('stock'); //疫苗库存总数
  88. $data['less_num'] = Vaccine::where($where)->where(['stock'=>0])->count();//缺苗钟数
  89. $ids = Order::where(['product_type'=>4])->where($where)->with('orderPatient','appoint_start_time',strtotime('today'))->pluck('id')->toArray();//订单id
  90. $data['yesterday']= OrderVaccine::whereIn('order_id',$ids)->count(); //疫苗订单总数
  91. return $data;
  92. }
  93. //实时数据
  94. public static function service()
  95. {
  96. $where = [];
  97. if(self::$org_id){
  98. $where['organization_id']=self::$org_id;
  99. }
  100. $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();
  101. $data['schedule_docter'] = SchedulePeriod::distinct('docter_id')->where($where)->where('schedule_date',date('Y-m-d H:i:s',time()))->count();
  102. // $data['paitent_num'] = Order::where(['order_status'=>1])->distinct()->count()->BgroupBy('patient_id');
  103. $data['paitent_num'] = Order::whereHas('orderPatient',function ($query){
  104. $query->where(['appoint_start_time'=>strtotime('today')]);
  105. })->where(['product_type'=>4])->where($where)->count();//接诊患者(人次)
  106. return $data;
  107. }
  108. //订单概况
  109. public static function orderTotal()
  110. {
  111. //3.门诊预约 4.疫苗接种预约 5.儿保预约
  112. $where = [];
  113. if(self::$org_id){
  114. $where['organization_id']=self::$org_id;
  115. }
  116. //todo 是否需要区别状态
  117. $order_num[] = Order::where('product_type',3)->where($where)->count();
  118. $order_num[] = Order::where('product_type',4)->where($where)->count();
  119. $order_num[] = Order::where('product_type',5)->where($where)->count();
  120. $order_num[] = Order::whereIn('product_type',[3,4,5])->where($where)->count();
  121. return $order_num;
  122. }
  123. //排班统计
  124. public static function schedule()
  125. {
  126. $where = [];
  127. if(self::$org_id){
  128. $where['organization_id']=self::$org_id;
  129. }
  130. $ids = SchedulePeriod::where('schedule_date',date("Y-m-d"))->where($where)->distinct('docter_id')->pluck('docter_id')->toArray();
  131. $data = ['clinic_num'=>0,'vaccine_num'=>0,'nurse_num'=>0];
  132. if(empty($ids)){
  133. return $data;
  134. };
  135. $docters = Docter::whereIn('id',$ids)->get(['id','type'])->GroupBy('type');
  136. foreach ($docters as $key => $val){
  137. if($key === 1){
  138. $data['clinic_num'] = count($val);
  139. } else if($key == 2){
  140. $data['vaccine_num'] = count($val);
  141. } else if($key == 3){
  142. $data['nurse_num'] = count($val);
  143. }
  144. }
  145. return $data;
  146. }
  147. //门诊统计
  148. public static function clinc()
  149. {
  150. $where = [];
  151. if(self::$org_id){
  152. $where['organization_id']=self::$org_id;
  153. }
  154. $order = Order::where(['product_type'=>3])->where($where);
  155. //今日预约数
  156. $today = $order->whereHas('orderPatient',function ($query){
  157. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1 days')]);
  158. })->whereNotIn('order_status',[1,5,6])->count();
  159. //医生排班人数
  160. $docters = SchedulePeriod::where(['schedule_date'=>date('Y-m-d',time())])->where($where)->distinct('docter_id')->count('docter_id');
  161. //昨日预约
  162. $yesterday = $order->whereHas('orderPatient',function ($query){
  163. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
  164. })->whereNotIn('order_status',[1,5,6])->count();
  165. //昨日接诊人数
  166. $people= $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  167. $data['today'] = $today;
  168. $data['docters'] = $docters;
  169. $data['yesterday'] = $yesterday;
  170. $data['people'] = $people;
  171. return $data;
  172. }
  173. }