StatController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. $data['have_num'] = Vaccine::where($another)->sum('stock'); //疫苗库存总数
  142. $data['less_num'] = Vaccine::where($another)->where(['stock'=>10])->count();//缺苗钟数
  143. if ($org_id)
  144. {
  145. $ids = Order::where(['product_type'=>4])
  146. ->where('organization_id',$org_id)
  147. ->whereBetween('appoint_start_time',[$beforeoneday,strtotime('today')])
  148. ->where('order_status',4)
  149. ->pluck('id')->toArray();
  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. }
  157. $data['yesterday']= OrderVaccine::whereIn('order_id',$ids)->count(); //疫苗订单总数
  158. return $data;
  159. }
  160. //实时数据
  161. public static function service()
  162. {
  163. $where = [];
  164. $org_id = Admin::user()->org_id;
  165. if($org_id){
  166. $where['organization_id']=$org_id;
  167. }
  168. $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();
  169. $data['schedule_docter'] = SchedulePeriod::distinct('docter_id')->where($where)->where('schedule_date',date('Y-m-d H:i:s',time()))->count();
  170. // $data['paitent_num'] = Order::where(['order_status'=>1])->distinct()->count()->BgroupBy('patient_id');
  171. $data['paitent_num'] = Order::whereHas('orderPatient',function ($query){
  172. $query->where(['appoint_start_time'=>strtotime('today')]);
  173. })->where(['product_type'=>4])->where($where)->count();//接诊患者(人次)
  174. return $data;
  175. }
  176. //订单概况
  177. public static function orderTotal()
  178. {
  179. //3.门诊预约 4.疫苗接种预约 5.儿保预约
  180. $where = [];
  181. $org_id = Admin::user()->org_id;
  182. if($org_id){
  183. $where['organization_id']=$org_id;
  184. }
  185. //todo 是否需要区别状态
  186. $order_num[] = Order::where('product_type',3)->where($where)->where('payment_status','!=',1)->count();
  187. $order_num[] = Order::where('product_type',4)->where($where)->count();
  188. $order_num[] = Order::where('product_type',5)->where($where)->count();
  189. $order_num[] = Order::whereIn('product_type',[3,4,5])->where($where)->count();
  190. return $order_num;
  191. }
  192. //排班统计
  193. public static function schedule()
  194. {
  195. $where = [];
  196. $org_id = Admin::user()->org_id;
  197. if($org_id){
  198. $where['organization_id']=$org_id;
  199. }
  200. $ids = SchedulePeriod::where('schedule_date',date("Y-m-d"))->where($where)->distinct('docter_id')->pluck('docter_id')->toArray();
  201. $data = ['clinic_num'=>0,'vaccine_num'=>0,'nurse_num'=>0];
  202. if(empty($ids)){
  203. return $data;
  204. };
  205. $docters = Docter::whereIn('id',$ids)->get(['id','type'])->GroupBy('type');
  206. foreach ($docters as $key => $val){
  207. if($key === 1){
  208. $data['clinic_num'] = count($val);
  209. } else if($key == 2){
  210. $data['vaccine_num'] = count($val);
  211. } else if($key == 3){
  212. $data['nurse_num'] = count($val);
  213. }
  214. }
  215. return $data;
  216. }
  217. //门诊统计
  218. public static function clinc()
  219. {
  220. $where = [];
  221. $org_id = Admin::user()->org_id;
  222. if($org_id){
  223. $where['organization_id']=$org_id;
  224. }
  225. $order = Order::where(['product_type'=>3])->where($where);
  226. //今日预约数
  227. $today = $order->whereHas('orderPatient',function ($query){
  228. $query->whereBetween('appoint_start_time',[strtotime('today'),strtotime('+1 days')]);
  229. })->count();
  230. //医生排班人数
  231. $docters = SchedulePeriod::where(['schedule_date'=>date('Y-m-d',time()),'schedule_type'=>1])->where($where)->distinct('docter_id')->count();
  232. //昨日预约
  233. $yesterday = $order->whereHas('orderPatient',function ($query){
  234. $query->whereBetween('appoint_start_time',[date('Y-m-d',strtotime('-1 days')),date('Y-m-d',strtotime('today'))]);
  235. })->whereNotIn('order_status',[1,5,6])->count();
  236. //昨日接诊人数
  237. $people= $order->whereBetween('end_time',[strtotime('-1days'),strtotime('today')])->where('order_status',4)->count();
  238. $data['today'] = $today;
  239. $data['docters'] = $docters;
  240. $data['yesterday'] = $yesterday;
  241. $data['people'] = $people;
  242. return $data;
  243. }
  244. }