StatController.php 7.9 KB

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