UserStatistics.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class UserStatistics extends Auth {
  6. public function user() {
  7. $roles = Db::name('roles')
  8. ->where('type', 1)
  9. ->where('parent_id', 0)
  10. ->select();
  11. $all = 0;
  12. foreach ($roles as $k => $v) {
  13. $res = Db::name('user')
  14. ->alias('a')
  15. ->join('user_roles b', 'a.id=b.user_id')
  16. ->join('user_org c', 'a.id=c.user_id')
  17. ->join('roles d', 'b.roles_id=d.id')
  18. ->where('a.del', 0)
  19. ->where('a.enable', 1)
  20. ->where('d.parent_id', $v['id'])
  21. ->where('c.org_id', $this->orgId)
  22. ->count();
  23. $roles[$k]['value'] = $res ? $res : 0;
  24. $all += $res;
  25. }
  26. $this->assign('list', $roles);
  27. $this->assign('allCount', $all);
  28. return $this->fetch();
  29. }
  30. public function org_order() {
  31. $cur = date('Y-m-d');
  32. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  33. $end = input('end', date('Y-m-d'));
  34. $start1 = $start . ' 00:00:00';
  35. $end1 = $end . ' 23:59:59';
  36. $list = $this->orgOrderData($start1, $end1);
  37. $this->assign('list', $list);
  38. $this->assign('start', $start);
  39. $this->assign('end', $end);
  40. return $this->fetch();
  41. }
  42. public function orgOrderData($start1, $end1) {
  43. $map1[] = ['create_time', '>=', $start1];
  44. $map1[] = ['create_time', '<=', $end1];
  45. $list = Db::name('org')
  46. ->where('type', 2)
  47. ->where('del', 0)
  48. ->where('enable', 1)
  49. ->select();
  50. foreach ($list as $k => $v) {
  51. $count = Db::name('orders')
  52. ->where('del', 0)
  53. ->where('org_id', $v['id'])
  54. ->where($map1)
  55. ->count();
  56. $list[$k]['count'] = $count ? $count : 0;
  57. $bx = Db::name('orders')
  58. ->where('del', 0)
  59. ->where('org_id', $v['id'])
  60. ->where('work_type_mode', 1)
  61. ->where($map1)
  62. ->count();
  63. $list[$k]['bx'] = $bx ? $bx : 0;
  64. $bj = Db::name('orders')
  65. ->where('del', 0)
  66. ->where('org_id', $v['id'])
  67. ->where('work_type_mode', 2)
  68. ->where($map1)
  69. ->count();
  70. $list[$k]['bj'] = $bj ? $bj : 0;
  71. $ys = Db::name('orders')
  72. ->where('del', 0)
  73. ->where('org_id', $v['id'])
  74. ->where('work_type_mode', 3)
  75. ->where($map1)
  76. ->count();
  77. $list[$k]['ys'] = $ys ? $ys : 0;
  78. $yh = Db::name('orders')
  79. ->where('del', 0)
  80. ->where('org_id', $v['id'])
  81. ->where('work_type_mode', 4)
  82. ->where($map1)
  83. ->count();
  84. $list[$k]['yh'] = $yh ? $yh : 0;
  85. }
  86. return $list;
  87. }
  88. public function orgOrderExport() {
  89. $cur = date('Y-m-d');
  90. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  91. $end = input('end', date('Y-m-d'));
  92. $start1 = $start . ' 00:00:00';
  93. $end1 = $end . ' 23:59:59';
  94. $ret = $list = $this->orgOrderData($start1, $end1);;
  95. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  96. //实例化PHPExcel类
  97. $objPHPExcel = new \PHPExcel();
  98. //激活当前的sheet表
  99. $objPHPExcel->setActiveSheetIndex(0);
  100. //设置表格头(即excel表格的第一行)
  101. $objPHPExcel->setActiveSheetIndex(0)
  102. ->setCellValue('A1', '项目')
  103. ->setCellValue('B1', '总数')
  104. ->setCellValue('C1', '报修')
  105. ->setCellValue('D1', '保洁')
  106. ->setCellValue('E1', '运送')
  107. ->setCellValue('F1', '应急');
  108. // 设置表格头水平居中
  109. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  110. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  111. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  112. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  113. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  114. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  115. //设置列水平居中
  116. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  117. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  118. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  119. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  120. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  121. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  122. //设置单元格宽度
  123. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  124. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  125. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  126. //循环刚取出来的数组,将数据逐一添加到excel表格。
  127. for ($i = 0; $i < count($ret); $i++) {
  128. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['name']);
  129. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['count']);
  130. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['bx']);
  131. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['bj']);
  132. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['ys']);
  133. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $ret[$i]['yh']);
  134. }
  135. //设置保存的Excel表格名称
  136. $filename = '各项目订单统计_' . date('YmdHis', time()) . '.xls';
  137. //设置当前激活的sheet表格名称
  138. $objPHPExcel->getActiveSheet()->setTitle('各项目订单统计');
  139. //设置浏览器窗口下载表格
  140. ob_end_clean();
  141. header("Content-Type: application/force-download");
  142. header("Content-Type: application/octet-stream");
  143. header("Content-Type: application/download");
  144. header('Content-Disposition:inline;filename="' . $filename);
  145. //生成excel文件
  146. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  147. //下载文件在浏览器窗口
  148. return $objWriter->save('php://output');
  149. }
  150. public function org_user() {
  151. $list = $this->orgUserData();
  152. $this->assign('list', $list);
  153. return $this->fetch();
  154. }
  155. public function orgUserData() {
  156. $list = Db::name('org')
  157. ->where('type', 2)
  158. ->where('del', 0)
  159. ->where('enable', 1)
  160. ->select();
  161. foreach ($list as $k1 => $v1) {
  162. $roles = Db::name('roles')
  163. ->where('type', 1)
  164. ->where('parent_id', 0)
  165. ->select();
  166. $all = 0;
  167. foreach ($roles as $k => $v) {
  168. $res = Db::name('user')
  169. ->alias('a')
  170. ->join('user_roles b', 'a.id=b.user_id')
  171. ->join('user_org c', 'a.id=c.user_id')
  172. ->join('roles d', 'b.roles_id=d.id')
  173. ->where('a.del', 0)
  174. ->where('a.enable', 1)
  175. ->where('d.parent_id', $v['id'])
  176. ->where('c.org_id', $v1['id'])
  177. ->count();
  178. $roles[$k]['value'] = $res ? $res : 0;
  179. $all += $res;
  180. }
  181. $list[$k1]['user'] = $roles;
  182. $list[$k1]['count'] = $all;
  183. }
  184. return $list;
  185. }
  186. public function orgUserExport() {
  187. $ret = $list = $this->orgUserData();;
  188. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  189. //实例化PHPExcel类
  190. $objPHPExcel = new \PHPExcel();
  191. //激活当前的sheet表
  192. $objPHPExcel->setActiveSheetIndex(0);
  193. //设置表格头(即excel表格的第一行)
  194. $objPHPExcel->setActiveSheetIndex(0)
  195. ->setCellValue('A1', '项目')
  196. ->setCellValue('B1', '总数')
  197. ->setCellValue('C1', '客户')
  198. ->setCellValue('D1', '综合')
  199. ->setCellValue('E1', '保安')
  200. ->setCellValue('F1', '运送')
  201. ->setCellValue('G1', '维修')
  202. ->setCellValue('H1', '保洁')
  203. ->setCellValue('I1', '调度')
  204. ->setCellValue('J1', '管理层');
  205. // 设置表格头水平居中
  206. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  207. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  208. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  209. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  210. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  211. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  212. //设置列水平居中
  213. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  214. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  215. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  216. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  217. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  218. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  219. //设置单元格宽度
  220. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  221. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  222. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  223. //循环刚取出来的数组,将数据逐一添加到excel表格。
  224. for ($i = 0; $i < count($ret); $i++) {
  225. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['name']);
  226. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['count']);
  227. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), isset($ret[$i]['user'][0]) ? $ret[$i]['user'][0]['value'] : 0);
  228. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), isset($ret[$i]['user'][1]) ? $ret[$i]['user'][1]['value'] : 1);
  229. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), isset($ret[$i]['user'][2]) ? $ret[$i]['user'][2]['value'] : 2);
  230. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), isset($ret[$i]['user'][3]) ? $ret[$i]['user'][3]['value'] : 3);
  231. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), isset($ret[$i]['user'][4]) ? $ret[$i]['user'][4]['value'] : 4);
  232. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), isset($ret[$i]['user'][5]) ? $ret[$i]['user'][5]['value'] : 5);
  233. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), isset($ret[$i]['user'][6]) ? $ret[$i]['user'][6]['value'] : 6);
  234. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), isset($ret[$i]['user'][7]) ? $ret[$i]['user'][7]['value'] : 7);
  235. }
  236. //设置保存的Excel表格名称
  237. $filename = '各项目人员统计_' . date('YmdHis', time()) . '.xls';
  238. //设置当前激活的sheet表格名称
  239. $objPHPExcel->getActiveSheet()->setTitle('各项目人员统计');
  240. //设置浏览器窗口下载表格
  241. ob_end_clean();
  242. header("Content-Type: application/force-download");
  243. header("Content-Type: application/octet-stream");
  244. header("Content-Type: application/download");
  245. header('Content-Disposition:inline;filename="' . $filename);
  246. //生成excel文件
  247. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  248. //下载文件在浏览器窗口
  249. return $objWriter->save('php://output');
  250. }
  251. }