DailyStatistics.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class DailyStatistics extends Auth {
  6. public function work() {
  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 . ' 00:00:00';
  12. $list = $this->workData($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 workData($start1, $end1) {
  19. $map1[] = ['create_time', '>=', $start1];
  20. $map1[] = ['create_time', '<=', $end1];
  21. $map[] = ['org_id', '=', $this->orgId];
  22. $list = Db::name('daily_record')
  23. ->where($map)
  24. ->group('user_id')
  25. ->select();
  26. foreach ($list as $k => $v) {
  27. $list[$k]['real_name'] = Db::name('user')
  28. ->where('id', $v['user_id'])
  29. ->value('real_name');
  30. $count = Db::name('daily_record')
  31. ->where($map)
  32. ->where('user_id', $v['user_id'])
  33. ->where($map1)
  34. ->count();
  35. $list[$k]['count'] = $count ? $count : 0;
  36. }
  37. return $list;
  38. }
  39. public function workExport() {
  40. $cur = date('Y-m-d');
  41. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  42. $end = input('end', date('Y-m-d'));
  43. $start1 = $start . ' 00:00:00';
  44. $end1 = $end . ' 00:00:00';
  45. $ret = $this->workData($start1, $end1);
  46. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  47. //实例化PHPExcel类
  48. $objPHPExcel = new \PHPExcel();
  49. //激活当前的sheet表
  50. $objPHPExcel->setActiveSheetIndex(0);
  51. //设置表格头(即excel表格的第一行)
  52. $objPHPExcel->setActiveSheetIndex(0)
  53. ->setCellValue('A1', 'ID')
  54. ->setCellValue('B1', '姓名')
  55. ->setCellValue('C1', '工作记录');
  56. // 设置表格头水平居中
  57. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  58. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  59. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  60. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  61. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  62. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  63. //设置列水平居中
  64. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  65. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  66. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  67. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  68. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  69. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  70. //设置单元格宽度
  71. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  72. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  73. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  74. //循环刚取出来的数组,将数据逐一添加到excel表格。
  75. for ($i = 0; $i < count($ret); $i++) {
  76. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);
  77. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['real_name']);
  78. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['count']);
  79. }
  80. //设置保存的Excel表格名称
  81. $filename = '日常工人工作量统计_' . date('YmdHis', time()) . '.xls';
  82. //设置当前激活的sheet表格名称
  83. $objPHPExcel->getActiveSheet()->setTitle('日常工人工作量统计');
  84. //设置浏览器窗口下载表格
  85. ob_end_clean();
  86. header("Content-Type: application/force-download");
  87. header("Content-Type: application/octet-stream");
  88. header("Content-Type: application/download");
  89. header('Content-Disposition:inline;filename="' . $filename);
  90. //生成excel文件
  91. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  92. //下载文件在浏览器窗口
  93. return $objWriter->save('php://output');
  94. }
  95. public function daily() {
  96. $cur = date('Y-m-d');
  97. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  98. $end = input('end', date('Y-m-d'));
  99. $start1 = $start . ' 00:00:00';
  100. $end1 = $end . ' 00:00:00';
  101. $map1[] = ['create_time', '>=', $start1];
  102. $map1[] = ['create_time', '<=', $end1];
  103. $map[] = ['org_id', '=', $this->orgId];
  104. $list = Db::name('daily_record')
  105. ->where($map)
  106. ->group('daily_id')
  107. ->select();
  108. foreach ($list as $k => $v) {
  109. $list[$k]['title'] = Db::name('daily')
  110. ->where('id', $v['daily_id'])
  111. ->value('title');
  112. $count = Db::name('daily_record')
  113. ->where($map)
  114. ->where('daily_id', $v['daily_id'])
  115. ->where($map1)
  116. ->count();
  117. $list[$k]['count'] = $count ? $count : 0;
  118. }
  119. $this->assign('list', $list);
  120. $this->assign('start', $start);
  121. $this->assign('end', $end);
  122. return $this->fetch();
  123. }
  124. }