DeviceStatistics.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class DeviceStatistics extends Auth {
  6. public function device(){
  7. $cur = date('Y-m-d');
  8. $start = input('start',date('Y-m-d', strtotime(''.$cur.' -1 week')));
  9. $end = input('end',date('Y-m-d'));
  10. $start1 = $start.' 00:00:00';
  11. $end1 = $end.' 23:59:59';
  12. $list = $this->deviceData($start1,$end1);
  13. $this->assign('list',$list);
  14. $this->assign('start',$start);
  15. $this->assign('end',$end);
  16. return $this->fetch();
  17. }
  18. public function deviceData($start1,$end1){
  19. $map1[] = ['create_time','>=',$start1];
  20. $map1[] = ['create_time','<=',$end1];
  21. $map1[] = ['org_id','<=',$this->orgId];
  22. $list =Db::name('device_record')
  23. ->where('org_id',$this->orgId)
  24. ->group('device_id')
  25. ->select();
  26. foreach ($list as $k=>$v){
  27. $list[$k]['title'] = Db::name('device')
  28. ->where('id',$v['device_id'])
  29. ->value('title');
  30. $c = Db::name('device_record')
  31. ->where($map1)
  32. ->where('device_id',$v['device_id'])
  33. ->count();
  34. $list[$k]['count'] = $c?$c:0;
  35. }
  36. return $list;
  37. }
  38. public function deviceExport(){
  39. $cur = date('Y-m-d');
  40. $start = input('start',date('Y-m-d', strtotime(''.$cur.' -1 week')));
  41. $end = input('end',date('Y-m-d'));
  42. $start1 = $start.' 00:00:00';
  43. $end1 = $end.' 23:59:59';
  44. $ret = $this->deviceData($start1,$end1);
  45. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  46. //实例化PHPExcel类
  47. $objPHPExcel = new \PHPExcel();
  48. //激活当前的sheet表
  49. $objPHPExcel->setActiveSheetIndex(0);
  50. //设置表格头(即excel表格的第一行)
  51. $objPHPExcel->setActiveSheetIndex(0)
  52. ->setCellValue('A1', '设备')
  53. ->setCellValue('B1', '总数');
  54. // 设置表格头水平居中
  55. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  56. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  57. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  58. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  59. //设置列水平居中
  60. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  61. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  62. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  63. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  64. //设置单元格宽度
  65. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  66. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  67. //循环刚取出来的数组,将数据逐一添加到excel表格。
  68. for ($i = 0; $i < count($ret); $i++) {
  69. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['title']);
  70. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['count']);
  71. }
  72. //设置保存的Excel表格名称
  73. $filename = '设备维保工作量统计分析_' . date('YmdHis', time()) . '.xls';
  74. //设置当前激活的sheet表格名称
  75. $objPHPExcel->getActiveSheet()->setTitle('设备维保工作量统计分析');
  76. //设置浏览器窗口下载表格
  77. ob_end_clean();
  78. header("Content-Type: application/force-download");
  79. header("Content-Type: application/octet-stream");
  80. header("Content-Type: application/download");
  81. header('Content-Disposition:inline;filename="' . $filename);
  82. //生成excel文件
  83. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  84. //下载文件在浏览器窗口
  85. return $objWriter->save('php://output');
  86. }
  87. }