StatController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. $oneday = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days')));
  28. $twoday = strtotime(date('Y-m-d 00:00:00',strtotime('+2 days')));
  29. $beforeoneday = strtotime(date('Y-m-d 00:00:00',strtotime('-1 days')));
  30. //今日预约
  31. if ($org_id)
  32. {
  33. $today = Order::where(['product_type'=>4])
  34. ->where('organization_id',$org_id)
  35. ->whereBetween('appoint_start_time',[strtotime('today'),strtotime(date('Y-m-d 00:00:00',strtotime('+1 days')))])
  36. ->count();
  37. $tomorrow = Order::where(['product_type'=>4])
  38. ->where('organization_id',$org_id)
  39. ->whereBetween('appoint_start_time',[$oneday,$twoday])
  40. ->count();
  41. $people = Order::where(['product_type'=>4])
  42. ->where('organization_id',$org_id)
  43. ->whereBetween('end_time',[$beforeoneday,strtotime('today')])
  44. ->where('order_status',4)->count();
  45. $yesterday = Order::where(['product_type'=>4])
  46. ->where('organization_id',$org_id)
  47. ->whereBetween('appoint_start_time',[$beforeoneday,strtotime(date('Y-m-d 00:00:00',strtotime('today')))])
  48. ->count();
  49. }else{
  50. $today = Order::where(['product_type'=>4])
  51. ->whereBetween('appoint_start_time',[strtotime('today'),strtotime(date('Y-m-d 00:00:00',strtotime('+1 days')))])
  52. ->count();
  53. $tomorrow = Order::where(['product_type'=>4])
  54. ->whereBetween('appoint_start_time',[$oneday,$twoday])
  55. ->count();
  56. $people = Order::where(['product_type'=>4])->whereBetween('end_time',[$beforeoneday,strtotime('today')])->where('order_status',4)->count();
  57. $yesterday = Order::where(['product_type'=>4])
  58. ->whereBetween('appoint_start_time',[$beforeoneday,strtotime(date('Y-m-d 00:00:00',strtotime('today')))])
  59. ->count();
  60. }
  61. $data['today'] = $today;
  62. $data['yesterday'] = $yesterday;
  63. $data['people'] = $people;
  64. $data['tomorrow'] = $tomorrow;
  65. // $data ['sum_num'] = Vaccine::where($where)->count();
  66. // $data['have_num'] = Vaccine::where($where)->sum('stock');
  67. // $data['less_num'] = Vaccine::where($where)->where(['stock'=>0])->count();//取苗钟数
  68. // $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();
  69. // $data['today_vaccine'] = Order::where(['product_type'=>4])->where($where)->with('orderPatient','appoint_start_time',strtotime('today'))->count();//接诊患者(人次)
  70. return $data;
  71. }
  72. //儿保
  73. public static function nurse()
  74. {
  75. $where = [];
  76. $org_id = Admin::user()->org_id;
  77. if($org_id){
  78. $where['organization_id']=$org_id;
  79. }
  80. $order = Order::where(['product_type'=>5])->where($where);
  81. $oneday = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days')));
  82. $twoday = strtotime(date('Y-m-d 00:00:00',strtotime('+2 days')));
  83. $beforeoneday = strtotime(date('Y-m-d 00:00:00',strtotime('-1 days')));
  84. if ($org_id)
  85. {
  86. //今日预约
  87. $today = Order::where(['product_type'=>5])
  88. ->where('organization_id',$org_id)
  89. ->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1days')])
  90. ->count();
  91. //明日预约
  92. $tomorrow = Order::where(['product_type'=>5])
  93. ->where('organization_id',$org_id)
  94. ->whereBetween('appoint_start_time',[$oneday,$twoday])
  95. ->count();
  96. //昨日服务人数
  97. $people = Order::where(['product_type'=>5])
  98. ->where('organization_id',$org_id)
  99. ->whereBetween('end_time',[$beforeoneday,strtotime('today')])->where('order_status',4)->count();
  100. //昨日预约
  101. $yesterday = Order::where(['product_type'=>5])
  102. ->where('organization_id',$org_id)
  103. ->whereBetween('appoint_start_time',[$beforeoneday,date('Y-m-d 00:00:00',strtotime('today'))])
  104. ->count();
  105. }else{
  106. //今日预约
  107. $today = Order::where(['product_type'=>5])
  108. ->whereBetween('appoint_start_time',[strtotime('today'),$oneday])
  109. ->count();
  110. //明日预约
  111. $tomorrow = Order::where(['product_type'=>5])
  112. ->whereBetween('appoint_start_time',[$oneday,$twoday])
  113. ->count();
  114. //昨日服务人数
  115. $people = Order::where(['product_type'=>5])
  116. ->whereBetween('end_time',[$beforeoneday,strtotime('today')])->where('order_status',4)->count();
  117. //昨日预约
  118. $yesterday = Order::where(['product_type'=>5])
  119. ->whereBetween('appoint_start_time',[$beforeoneday,strtotime(date('Y-m-d 00:00:00',strtotime('today')))])
  120. ->count();
  121. }
  122. $data['today'] = $today;
  123. $data['tomorrow'] = $tomorrow;
  124. $data['people'] = $people;
  125. $data['yesterday'] = $yesterday;
  126. return $data;
  127. }
  128. //疫苗统计
  129. public static function vaccineToal()
  130. {
  131. $where = [];
  132. $another = [];
  133. $org_id = Admin::user()->org_id;
  134. if($org_id){
  135. $where['organization_id']=$org_id;
  136. $another['org_id'] = $org_id;
  137. }
  138. $oneday = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days')));
  139. $twoday = strtotime(date('Y-m-d 00:00:00',strtotime('+2 days')));
  140. $beforeoneday = strtotime(date('Y-m-d 00:00:00',strtotime('-1 days')));
  141. if ($org_id)
  142. {
  143. $ids = Order::where(['product_type'=>4])
  144. ->where('organization_id',$org_id)
  145. ->whereBetween('appoint_start_time',[$beforeoneday,strtotime('today')])
  146. ->where('order_status',4)
  147. ->pluck('id')->toArray();
  148. $data['have_num'] = Vaccine::where($another)->sum('stock'); //疫苗库存总数
  149. $data['less_num'] = Vaccine::where($another)->where('stock','<=',10)->count();//缺苗钟数
  150. }else
  151. {
  152. $ids = Order::where(['product_type'=>4])
  153. ->whereBetween('appoint_start_time',[$beforeoneday,strtotime('today')])
  154. ->where('order_status',4)
  155. ->pluck('id')->toArray();
  156. $data['have_num'] = Vaccine::sum('stock'); //疫苗库存总数
  157. $data['less_num'] = Vaccine::where('stock','<=',10)->count();//缺苗钟数
  158. }
  159. $data['yesterday']= OrderVaccine::whereIn('order_id',$ids)->count(); //疫苗订单总数
  160. return $data;
  161. }
  162. //实时数据
  163. public static function service()
  164. {
  165. $where = [];
  166. $org_id = Admin::user()->org_id;
  167. if($org_id){
  168. $where['organization_id']=$org_id;
  169. }
  170. $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();
  171. $data['schedule_docter'] = SchedulePeriod::distinct('docter_id')->where($where)->where('schedule_date',date('Y-m-d H:i:s',time()))->count();
  172. // $data['paitent_num'] = Order::where(['order_status'=>1])->distinct()->count()->BgroupBy('patient_id');
  173. $data['paitent_num'] = Order::whereHas('orderPatient',function ($query){
  174. $query->where(['appoint_start_time'=>strtotime('today')]);
  175. })->where(['product_type'=>4])->where($where)->count();//接诊患者(人次)
  176. return $data;
  177. }
  178. //订单概况
  179. public static function orderTotal()
  180. {
  181. //3.门诊预约 4.疫苗接种预约 5.儿保预约
  182. $where = [];
  183. $org_id = Admin::user()->org_id;
  184. if($org_id){
  185. $where['organization_id']=$org_id;
  186. }
  187. //todo 是否需要区别状态
  188. $order_num[] = Order::where('product_type',3)->where($where)->where('payment_status','!=',1)->count();
  189. $order_num[] = Order::where('product_type',4)->where($where)->count();
  190. $order_num[] = Order::where('product_type',5)->where($where)->count();
  191. $order_num[] = Order::whereIn('product_type',[3,4,5])->where($where)->count();
  192. return $order_num;
  193. }
  194. //排班统计
  195. public static function schedule()
  196. {
  197. $where = [];
  198. $org_id = Admin::user()->org_id;
  199. if($org_id){
  200. $where['organization_id']=$org_id;
  201. }
  202. $ids = SchedulePeriod::where('schedule_date',date("Y-m-d"))->where($where)->distinct('docter_id')->pluck('docter_id')->toArray();
  203. $data = ['clinic_num'=>0,'vaccine_num'=>0,'nurse_num'=>0];
  204. if(empty($ids)){
  205. return $data;
  206. };
  207. $docters = Docter::whereIn('id',$ids)->get(['id','type'])->GroupBy('type');
  208. foreach ($docters as $key => $val){
  209. if($key === 1){
  210. $data['clinic_num'] = count($val);
  211. } else if($key == 2){
  212. $data['vaccine_num'] = count($val);
  213. } else if($key == 3){
  214. $data['nurse_num'] = count($val);
  215. }
  216. }
  217. return $data;
  218. }
  219. //门诊统计
  220. public static function clinc()
  221. {
  222. $where = [];
  223. $org_id = Admin::user()->org_id;
  224. if($org_id){
  225. $where['organization_id']=$org_id;
  226. }
  227. $order = Order::where(['product_type'=>3])->where($where);
  228. //今日预约数
  229. $today = $order->whereHas('orderPatient',function ($query){
  230. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1 days')]);
  231. })->count();
  232. //医生排班人数
  233. $docters = SchedulePeriod::where(['schedule_date'=>date('Y-m-d',time()),'schedule_type'=>1])->where($where)->distinct('docter_id')->count();
  234. //昨日预约
  235. $yesterday = $order->whereHas('orderPatient',function ($query){
  236. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
  237. })->whereNotIn('order_status',[1,5,6])->count();
  238. //昨日接诊人数
  239. $people= $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  240. $data['today'] = $today;
  241. $data['docters'] = $docters;
  242. $data['yesterday'] = $yesterday;
  243. $data['people'] = $people;
  244. return $data;
  245. }
  246. }