ConveyStatistics.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use think\Exception;
  5. class ConveyStatistics extends Auth
  6. {
  7. public function urgencyOrder(){
  8. $start = input('start',date('Y-m-d 00:00:00',strtotime('-1 month')));
  9. $end = input('end',date('Y-m-d 23:59:59'));
  10. $rolesId = Db::name('roles')
  11. ->where('parent_id',6)
  12. ->where('del',0)
  13. ->where('type',1)
  14. ->where('enable',1)
  15. ->where('org_id',$this->orgId)
  16. ->column('id');
  17. $user = Db::name('user')
  18. ->alias('u')
  19. ->field('u.*')
  20. ->join('user_roles ur','ur.user_id=u.id')
  21. ->whereIn('ur.roles_id',$rolesId)
  22. ->where('u.del',0)
  23. ->where('u.enable',1)
  24. ->select();
  25. $data = [];
  26. foreach ($user as $k=>$v){
  27. $data[$k]['user_name'] = $v['real_name'];
  28. $count = Db::name('todo')
  29. ->alias('t')
  30. ->join('order_convey oc','oc.order_id=t.order_id')
  31. ->where('t.to_user_id',$v['id'])
  32. ->where('t.org_id',$this->orgId)
  33. ->where('t.del',0)
  34. ->where('t.work_type_mode',3)
  35. ->where('t.todo_mode',3)
  36. ->where('t.create_time','>=',$start)
  37. ->where('t.create_time','<=',$end)
  38. ->where('oc.priority',3)
  39. ->count();
  40. $data[$k]['count'] = $count;
  41. $list = Db::name('todo')
  42. ->alias('t')
  43. ->field('t.id,t.order_id,t.done_time,oc.ywc_time')
  44. ->join('order_convey oc','oc.order_id=t.order_id')
  45. ->where('t.to_user_id',$v['id'])
  46. ->where('t.org_id',$this->orgId)
  47. ->where('t.del',0)
  48. ->where('t.work_type_mode',3)
  49. ->where('t.todo_mode',3)
  50. ->where('t.create_time','>=',$start)
  51. ->where('t.create_time','<=',$end)
  52. ->where('oc.priority',3)
  53. ->select();
  54. $hg = 0;
  55. foreach ($list as $kk=>$vv){
  56. if($vv['done_time'] && $vv['ywc_time'] >= $vv['done_time']){
  57. $hg+=1;
  58. }
  59. }
  60. $data[$k]['hg_num'] = $hg;
  61. $data[$k]['no_hg_num'] = $count -$hg;
  62. if($count > 0){
  63. $bl = round($hg/$count*100,2).'%';
  64. }else{
  65. $bl = '0%';
  66. }
  67. $data[$k]['bl'] = $bl;
  68. }
  69. $data = $data?arraySequence($data,'count','SORT_DESC'):[];
  70. $this->assign('data',$data);
  71. $this->assign('start',$start);
  72. $this->assign('end',$end);
  73. return $this->fetch();
  74. }
  75. public function urgencyOrderExport(){
  76. $start = input('start',date('Y-m-d 00:00:00',strtotime('-1 month')));
  77. $end = input('end',date('Y-m-d 23:59:59'));
  78. $rolesId = Db::name('roles')
  79. ->whereIn('parent_id',6)
  80. ->where('del',0)
  81. ->where('type',1)
  82. ->where('enable',1)
  83. ->where('org_id',$this->orgId)
  84. ->column('id');
  85. $user = Db::name('user')
  86. ->alias('u')
  87. ->field('u.*')
  88. ->join('user_roles ur','ur.user_id=u.id')
  89. ->whereIn('ur.roles_id',$rolesId)
  90. ->where('u.del',0)
  91. ->where('u.enable',1)
  92. ->select();
  93. $data = [];
  94. foreach ($user as $k=>$v){
  95. $data[$k]['user_name'] = $v['real_name'];
  96. $count = Db::name('todo')
  97. ->alias('t')
  98. ->join('order_convey oc','oc.order_id=t.order_id')
  99. ->where('t.to_user_id',$v['id'])
  100. ->where('t.org_id',$this->orgId)
  101. ->where('t.del',0)
  102. ->where('t.work_type_mode',3)
  103. ->where('t.todo_mode',3)
  104. ->where('t.create_time','>=',$start)
  105. ->where('t.create_time','<=',$end)
  106. ->where('oc.priority',3)
  107. ->count();
  108. $data[$k]['count'] = $count;
  109. $list = Db::name('todo')
  110. ->alias('t')
  111. ->field('t.id,t.order_id,t.done_time,oc.ywc_time')
  112. ->join('order_convey oc','oc.order_id=t.order_id')
  113. ->where('t.to_user_id',$v['id'])
  114. ->where('t.org_id',$this->orgId)
  115. ->where('t.del',0)
  116. ->where('t.work_type_mode',3)
  117. ->where('t.todo_mode',3)
  118. ->where('t.create_time','>=',$start)
  119. ->where('t.create_time','<=',$end)
  120. ->where('oc.priority',3)
  121. ->select();
  122. $hg = 0;
  123. foreach ($list as $kk=>$vv){
  124. if($vv['done_time'] && $vv['ywc_time'] >= $vv['done_time']){
  125. $hg+=1;
  126. }
  127. }
  128. $data[$k]['hg_num'] = $hg;
  129. $data[$k]['no_hg_num'] = $count -$hg;
  130. if($count > 0){
  131. $bl = round($hg/$count*100,2).'%';
  132. }else{
  133. $bl = '0%';
  134. }
  135. $data[$k]['bl'] = $bl;
  136. }
  137. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  138. //实例化PHPExcel类
  139. $objPHPExcel =new \PHPExcel();
  140. //激活当前的sheet表
  141. $objPHPExcel->setActiveSheetIndex(0);
  142. //设置表格头(即excel表格的第一行)
  143. $objPHPExcel->setActiveSheetIndex(0)
  144. ->setCellValue('A1', '姓名')
  145. ->setCellValue('B1', '总数')
  146. ->setCellValue('C1', '合格数')
  147. ->setCellValue('D1', '不合格数')
  148. ->setCellValue('E1', '合格率');
  149. // 设置表格头水平居中
  150. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  151. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  152. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  153. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  154. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  155. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  156. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  157. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  158. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  159. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  160. //设置列水平居中
  161. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  162. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  163. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  164. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  165. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  166. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  167. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  168. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  169. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  170. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  171. //设置单元格宽度
  172. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(30);
  173. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  174. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
  175. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
  176. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
  177. //循环刚取出来的数组,将数据逐一添加到excel表格。
  178. for ($i = 0; $i < count($data); $i++) {
  179. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $data[$i]['user_name']);
  180. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $data[$i]['count']);
  181. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $data[$i]['hg_num']);
  182. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $data[$i]['no_hg_num']);
  183. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $data[$i]['bl']);
  184. }
  185. //设置保存的Excel表格名称
  186. $filename = '运送急查订单统计报表_' . date('YmdHis', time()) . '.xls';
  187. //设置当前激活的sheet表格名称
  188. $objPHPExcel->getActiveSheet()->setTitle('运送急查订单统计报表');
  189. //设置浏览器窗口下载表格
  190. ob_end_clean();
  191. header("Content-Type: application/force-download");
  192. header("Content-Type: application/octet-stream");
  193. header("Content-Type: application/download");
  194. header('Content-Disposition:inline;filename="' . $filename);
  195. //生成excel文件
  196. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  197. //下载文件在浏览器窗口
  198. return $objWriter->save('php://output');
  199. }
  200. public function conveyOrder(){
  201. $start = input('start',date('Y-m-d',strtotime('-7 day')));
  202. $end = input('end',date('Y-m-d'));
  203. $address = input('address');
  204. if(request()->isAjax()){
  205. $sTime = $start.' 00:00:00';
  206. $eTime = $end.' 23:59:59';
  207. $map = [];
  208. if(!empty($address)){
  209. $ids = explode(',',$address);
  210. $map[] = ['id','in',$ids];
  211. }
  212. $list = Db::name('address')
  213. ->field('id,title')
  214. ->where('enable',1)
  215. ->where('del',0)
  216. ->where($map)
  217. ->where('org_id',$this->orgId)
  218. ->where('find_in_set(2,types)')
  219. ->select();
  220. foreach ($list as $k=>$v){
  221. $list[$k]['nums'] = Db::name('orders')
  222. ->alias('a')
  223. ->join('order_convey b','a.id=b.order_id')
  224. ->where('a.org_id',$this->orgId)
  225. ->where('a.del',0)
  226. ->where('a.work_type_mode',3)
  227. ->where('a.order_mode','in',[5,6])
  228. ->where('b.start',$v['id'])
  229. ->where('a.create_time','>=',$sTime)
  230. ->where('a.create_time','<=',$eTime)
  231. ->count('a.id');
  232. }
  233. $result['rows'] = $list;
  234. return json($result);
  235. }else{
  236. $addressList = Db::name('address')
  237. ->field('id,title')
  238. ->where('enable',1)
  239. ->where('del',0)
  240. ->where('org_id',$this->orgId)
  241. ->where('find_in_set(2,types)')
  242. ->select();
  243. $this->assign('addressList',$addressList);
  244. $this->assign('start',$start);
  245. $this->assign('end',$end);
  246. $this->assign('addressVal',$address?explode(',',$address):[]);
  247. $this->assign('address',$address);
  248. return $this->fetch();
  249. }
  250. }
  251. public function conveyOrderExport(){
  252. $start = input('start',date('Y-m-d',strtotime('-7 day')));
  253. $end = input('end',date('Y-m-d'));
  254. $sTime = $start.' 00:00:00';
  255. $eTime = $end.' 23:59:59';
  256. $address = input('address');
  257. $map = [];
  258. if(!empty($address)){
  259. $ids = explode(',',$address);
  260. $map[] = ['id','in',$ids];
  261. }
  262. $list = Db::name('address')
  263. ->field('id,title')
  264. ->where('enable',1)
  265. ->where('del',0)
  266. ->where($map)
  267. ->where('org_id',$this->orgId)
  268. ->where('find_in_set(2,types)')
  269. ->select();
  270. foreach ($list as $k=>$v){
  271. $list[$k]['nums'] = Db::name('orders')
  272. ->alias('a')
  273. ->join('order_convey b','a.id=b.order_id')
  274. ->where('a.org_id',$this->orgId)
  275. ->where('a.del',0)
  276. ->where('a.work_type_mode',3)
  277. ->where('a.order_mode','in',[5,6])
  278. ->where('b.start',$v['id'])
  279. ->where('a.create_time','>=',$sTime)
  280. ->where('a.create_time','<=',$eTime)
  281. ->count('a.id');
  282. }
  283. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  284. //实例化PHPExcel类
  285. $objPHPExcel =new \PHPExcel();
  286. //激活当前的sheet表
  287. $objPHPExcel->setActiveSheetIndex(0);
  288. //设置表格头(即excel表格的第一行)
  289. $objPHPExcel->setActiveSheetIndex(0)
  290. ->setCellValue('A1', '运送地点')
  291. ->setCellValue('B1', '单量');
  292. // 设置表格头水平居中
  293. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  294. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  295. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  296. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  297. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  298. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  299. //设置列水平居中
  300. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  301. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  302. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  303. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  304. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  305. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  306. //设置单元格宽度
  307. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(30);
  308. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  309. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
  310. //循环刚取出来的数组,将数据逐一添加到excel表格。
  311. for ($i = 0; $i < count($list); $i++) {
  312. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['title']);
  313. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['nums']);
  314. }
  315. //设置保存的Excel表格名称
  316. $filename = '科室运送单量统计_'. date('YmdHis', time()) . '.xls';
  317. //设置当前激活的sheet表格名称
  318. $objPHPExcel->getActiveSheet()->setTitle('科室运送单量统计_');
  319. //设置浏览器窗口下载表格
  320. ob_end_clean();
  321. header("Content-Type: application/force-download");
  322. header("Content-Type: application/octet-stream");
  323. header("Content-Type: application/download");
  324. header('Content-Disposition:inline;filename="' . $filename);
  325. //生成excel文件
  326. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  327. //下载文件在浏览器窗口
  328. return $objWriter->save('php://output');
  329. }
  330. }