StatController.php 8.2 KB

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