ConveyStatistics.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. }