StatController.php 7.8 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. }
  79. //疫苗统计
  80. public static function vaccineToal()
  81. {
  82. $where = [];
  83. if(self::$org_id){
  84. $where['organization_id']=self::$org_id;
  85. }
  86. $data['have_num'] = Vaccine::where($where)->sum('stock'); //疫苗库存总数
  87. $data['less_num'] = Vaccine::where($where)->where(['stock'=>0])->count();//缺苗钟数
  88. $ids = Order::where(['product_type'=>4])->where($where)->with('orderPatient','appoint_start_time',strtotime('today'))->pluck('id')->toArray();//订单id
  89. $data['yesterday']= OrderVaccine::whereIn('order_id',$ids)->count(); //疫苗订单总数
  90. return $data;
  91. }
  92. //实时数据
  93. public static function service()
  94. {
  95. $where = [];
  96. if(self::$org_id){
  97. $where['organization_id']=self::$org_id;
  98. }
  99. $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();
  100. $data['schedule_docter'] = SchedulePeriod::distinct('docter_id')->where($where)->where('schedule_date',date('Y-m-d H:i:s',time()))->count();
  101. // $data['paitent_num'] = Order::where(['order_status'=>1])->distinct()->count()->BgroupBy('patient_id');
  102. $data['paitent_num'] = Order::whereHas('orderPatient',function ($query){
  103. $query->where(['appoint_start_time'=>strtotime('today')]);
  104. })->where(['product_type'=>4])->where($where)->count();//接诊患者(人次)
  105. return $data;
  106. }
  107. //订单概况
  108. public static function orderTotal()
  109. {
  110. //3.门诊预约 4.疫苗接种预约 5.儿保预约
  111. $where = [];
  112. if(self::$org_id){
  113. $where['organization_id']=self::$org_id;
  114. }
  115. //todo 是否需要区别状态
  116. $order_num[] = Order::where('product_type',3)->where($where)->count();
  117. $order_num[] = Order::where('product_type',4)->where($where)->count();
  118. $order_num[] = Order::where('product_type',5)->where($where)->count();
  119. $order_num[] = Order::whereIn('product_type',[3,4,5])->where($where)->count();
  120. return $order_num;
  121. }
  122. //排班统计
  123. public static function schedule()
  124. {
  125. $where = [];
  126. if(self::$org_id){
  127. $where['organization_id']=self::$org_id;
  128. }
  129. $ids = SchedulePeriod::where('schedule_date',date("Y-m-d"))->where($where)->distinct('docter_id')->pluck('docter_id')->toArray();
  130. $data = ['clinic_num'=>0,'vaccine_num'=>0,'nurse_num'=>0];
  131. if(empty($ids)){
  132. return $data;
  133. };
  134. $docters = Docter::whereIn('id',$ids)->get(['id','type'])->GroupBy('type');
  135. foreach ($docters as $key => $val){
  136. if($key === 1){
  137. $data['clinic_num'] = count($val);
  138. } else if($key == 2){
  139. $data['vaccine_num'] = count($val);
  140. } else if($key == 3){
  141. $data['nurse_num'] = count($val);
  142. }
  143. }
  144. return $data;
  145. }
  146. //门诊统计
  147. public static function clinc()
  148. {
  149. $where = [];
  150. if(self::$org_id){
  151. $where['organization_id']=self::$org_id;
  152. }
  153. $order = Order::where(['product_type'=>3])->where($where);
  154. //今日预约数
  155. $today = $order->whereHas('orderPatient',function ($query){
  156. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1 days')]);
  157. })->whereNotIn('order_status',[1,5,6])->count();
  158. //医生排班人数
  159. $docters = SchedulePeriod::where(['schedule_date'=>date('Y-m-d',time())])->where($where)->distinct('docter_id')->count('docter_id');
  160. //昨日预约
  161. $yesterday = $order->whereHas('orderPatient',function ($query){
  162. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
  163. })->whereNotIn('order_status',[1,5,6])->count();
  164. //昨日接诊人数
  165. $people= $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  166. $data['today'] = $today;
  167. $data['docters'] = $docters;
  168. $data['yesterday'] = $yesterday;
  169. $data['people'] = $people;
  170. return $data;
  171. }
  172. }