PatrolStatistics.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. namespace app\admin\controller;
  3. use function PHPSTORM_META\map;
  4. use think\App;
  5. use think\Db;
  6. class PatrolStatistics extends Auth {
  7. public function patrol() {
  8. $cur = date('Y-m-d');
  9. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  10. $end = input('end', date('Y-m-d'));
  11. $start1 = $start . ' 00:00:00';
  12. $end1 = $end . ' 23:59:59';
  13. $type = input('type', 1);
  14. $list = $this->patrolData($start1, $end1, $type);
  15. $this->assign('type', $type);
  16. $this->assign('list', $list);
  17. $this->assign('start', $start);
  18. $this->assign('end', $end);
  19. return $this->fetch();
  20. }
  21. public function patrolData($start1, $end1, $type) {
  22. $map1[] = ['create_time', '>=', $start1];
  23. $map1[] = ['create_time', '<=', $end1];
  24. $map1[] = ['org_id', '=', $this->orgId];
  25. $list = Db::name('patrol_record')
  26. ->where($map1)
  27. ->where('patrol_mode', $type)
  28. ->group('patrol_addr_id')
  29. ->distinct(true)
  30. ->select();
  31. foreach ($list as $k => $v) {
  32. $list[$k]['title'] = Db::name('address')
  33. ->where('id', $v['patrol_addr_id'])
  34. ->value('title');
  35. $c = Db::name('patrol_record')
  36. ->where($map1)
  37. ->where('is_normal', 0)
  38. ->where('patrol_addr_id', $v['patrol_addr_id'])
  39. ->where('patrol_mode', $type)
  40. ->count();
  41. $list[$k]['count'] = $c ? $c : 0;
  42. $normal = Db::name('patrol_record')
  43. ->where($map1)
  44. ->where('is_normal', 1)
  45. ->where('patrol_addr_id', $v['patrol_addr_id'])
  46. ->where('patrol_mode', $type)
  47. ->count();
  48. $list[$k]['normal'] = $normal ? $normal : 0;
  49. }
  50. return $list;
  51. }
  52. public function patrolExport() {
  53. $cur = date('Y-m-d');
  54. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  55. $end = input('end', date('Y-m-d'));
  56. $start1 = $start . ' 00:00:00';
  57. $end1 = $end . ' 23:59:59';
  58. $type = input('type', 1);
  59. $ret = $this->patrolData($start1, $end1, $type);
  60. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  61. //实例化PHPExcel类
  62. $objPHPExcel = new \PHPExcel();
  63. //激活当前的sheet表
  64. $objPHPExcel->setActiveSheetIndex(0);
  65. //设置表格头(即excel表格的第一行)
  66. $objPHPExcel->setActiveSheetIndex(0)
  67. ->setCellValue('A1', '智慧点')
  68. ->setCellValue('B1', '总数')
  69. ->setCellValue('C1', '总数(异常)');
  70. // 设置表格头水平居中
  71. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  72. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  73. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  74. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  75. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  76. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  77. //设置列水平居中
  78. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  79. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  80. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  81. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  82. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  83. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  84. //设置单元格宽度
  85. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  86. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  87. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  88. //循环刚取出来的数组,将数据逐一添加到excel表格。
  89. for ($i = 0; $i < count($ret); $i++) {
  90. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['title']);
  91. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['count']);
  92. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['normal']);
  93. }
  94. $n = Db::name('patrol_mode')
  95. ->where('id', $type)
  96. ->value('name');
  97. //设置保存的Excel表格名称
  98. $filename = $n . '工作量统计分析_' . date('YmdHis', time()) . '.xls';
  99. //设置当前激活的sheet表格名称
  100. $objPHPExcel->getActiveSheet()->setTitle($n . '工作量统计分析');
  101. //设置浏览器窗口下载表格
  102. ob_end_clean();
  103. header("Content-Type: application/force-download");
  104. header("Content-Type: application/octet-stream");
  105. header("Content-Type: application/download");
  106. header('Content-Disposition:inline;filename="' . $filename);
  107. //生成excel文件
  108. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  109. //下载文件在浏览器窗口
  110. return $objWriter->save('php://output');
  111. }
  112. public function work() {
  113. $cur = date('Y-m-d');
  114. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  115. $end = input('end', date('Y-m-d'));
  116. $start1 = $start . ' 00:00:00';
  117. $end1 = $end . ' 23:59:59';
  118. $type = input('type', 1);
  119. $list = $this->workData($start1, $end1, $type);
  120. $this->assign('type', $type);
  121. $this->assign('list', $list);
  122. $this->assign('start', $start);
  123. $this->assign('end', $end);
  124. return $this->fetch();
  125. }
  126. public function workData($start1, $end1, $type) {
  127. $map1[] = ['create_time', '>=', $start1];
  128. $map1[] = ['create_time', '<=', $end1];
  129. $map1[] = ['org_id', '=', $this->orgId];
  130. $list = Db::name('patrol_record')
  131. ->where($map1)
  132. ->where('patrol_mode', $type)
  133. ->group('user_id')
  134. ->distinct(true)
  135. ->select();
  136. foreach ($list as $k => $v) {
  137. $list[$k]['title'] = Db::name('user')
  138. ->where('id', $v['user_id'])
  139. ->value('real_name');
  140. $c = Db::name('patrol_record')
  141. ->where($map1)
  142. ->where('user_id', $v['user_id'])
  143. ->where('patrol_mode', $type)
  144. ->count();
  145. $list[$k]['count'] = $c ? $c : 0;
  146. $c1 = Db::name('patrol_record')
  147. ->where($map1)
  148. ->where('user_id', $v['user_id'])
  149. ->where('patrol_mode', $type)
  150. ->where('is_normal', 0)
  151. ->count();
  152. $list[$k]['count1'] = $c1 ? $c1 : 0;
  153. $c2 = Db::name('patrol_record')
  154. ->where($map1)
  155. ->where('user_id', $v['user_id'])
  156. ->where('patrol_mode', $type)
  157. ->where('is_normal', 1)
  158. ->count();
  159. $list[$k]['count2'] = $c2 ? $c2 : 0;
  160. $taskCount = Db::name('patrol_task_user')
  161. ->alias('a')
  162. ->join('patrol_task b','a.patrol_task_id=b.id')
  163. ->where('a.user_id',$v['user_id'])
  164. ->where('b.org_id',$this->orgId)
  165. ->where('b.status','<>',6)
  166. ->where('b.start_time','>=',$start1)
  167. ->where('b.end_time','<=',$end1)
  168. ->where('b.patrol_mode','=',$type)
  169. ->count();
  170. $ywcCount = Db::name('patrol_task_user')
  171. ->alias('a')
  172. ->join('patrol_task b','a.patrol_task_id=b.id')
  173. ->where('a.user_id',$v['user_id'])
  174. ->where('b.org_id',$this->orgId)
  175. ->where('b.status','=',2)
  176. ->where('b.start_time','>=',$start1)
  177. ->where('b.end_time','<=',$end1)
  178. ->where('b.patrol_mode','=',$type)
  179. ->count();
  180. $wcl = $taskCount>0?round($ywcCount/$taskCount,2)*100:0;
  181. $wcl = $wcl.'%';
  182. $list[$k]['wcl'] = $wcl;
  183. $list[$k]['count3'] = $ywcCount;
  184. $list[$k]['count4'] = $taskCount;
  185. }
  186. return $list;
  187. }
  188. public function workList(){
  189. $user_id = input('user_id','','trim');
  190. if($user_id!=''){
  191. $map[] = ['user_id','=',$user_id];
  192. }
  193. $is_normal = input('is_normal','','trim');
  194. if($is_normal!==''){
  195. $map[] = ['is_normal','=',$is_normal];
  196. }
  197. $type = input('type','','trim');
  198. if($type != ''){
  199. $map[] = ['patrol_mode','=',$type];
  200. }
  201. $start = input('start',date('Y-m-d', strtotime('' . date('Y-m-d') . ' -1 week')));
  202. if($start != ''){
  203. $map[] = ['create_time','>=',$start.' 00:00:00'];
  204. }
  205. $end = input('end', date('Y-m-d'));
  206. if($end != ''){
  207. $map[] = ['create_time','<=',$end.' 23:59:59'];
  208. }
  209. if(request()->isAjax()){
  210. //分页参数
  211. $length = input('rows',10,'intval'); //每页条数
  212. $page = input('page',1,'intval'); //第几页
  213. $start = ($page - 1) * $length; //分页开始位置
  214. //排序
  215. $sortRow = input('sidx','sort','trim'); //排序列
  216. $sort = input('sord','asc','trim'); //排序方式
  217. $order = $sortRow.' '.$sort.' ,id desc';
  218. $map[] = ['org_id','=',$this->orgId];
  219. $map= empty($map) ? true: $map;
  220. //数据查询
  221. $lists = Db::name('patrol_record')
  222. ->where($map)
  223. ->limit($start,$length)
  224. ->order($order)
  225. ->select();
  226. foreach ($lists as $k=>$v){
  227. $lists[$k]['address_title'] = Db::name('address')
  228. ->where('id', $v['patrol_addr_id'])
  229. ->value('title');
  230. $lists[$k]['task_user'] = Db::name('user')
  231. ->where('id', $v['user_id'])->value('real_name');
  232. $lists[$k]['task_title'] = Db::name('patrol_task')
  233. ->where('id', $v['patrol_task_id'])
  234. ->value('title');
  235. }
  236. //数据返回
  237. $totalCount = Db::name('patrol_record')->where($map)->count();
  238. $totalPage = ceil($totalCount/$length);
  239. $result['page'] = $page;
  240. $result['total'] = $totalPage;
  241. $result['records'] = $totalCount;
  242. $result['rows'] = $lists;
  243. return json($result);
  244. }else{
  245. $type = input('type',1);
  246. $t = Db::name('patrol_mode')
  247. ->where('id',$type)
  248. ->value('name');
  249. $this->assign('is_normal',$is_normal);
  250. $this->assign('type',$type);
  251. $this->assign('start',$start);
  252. $this->assign('end',$end);
  253. $this->assign('user_id',$user_id);
  254. $this->assign('meta_title',$t.'人员工作量统计');
  255. return $this->fetch();
  256. }
  257. }
  258. public function workList1(){
  259. $user_id = input('user_id','','trim');
  260. if($user_id!=''){
  261. $map[] = ['a.user_id','=',$user_id];
  262. }
  263. $is_normal = input('is_normal','','trim');
  264. if($is_normal!==''){
  265. if($is_normal==1){//总数
  266. $map[] = ['b.status','<>',6];
  267. }
  268. if($is_normal==2){//已完成
  269. $map[] = ['b.status','=',2];
  270. }
  271. }
  272. $type = input('type','','trim');
  273. if($type != ''){
  274. $map[] = ['b.patrol_mode','=',$type];
  275. }
  276. $start = input('start',date('Y-m-d', strtotime('' . date('Y-m-d') . ' -1 week')));
  277. if($start != ''){
  278. $map[] = ['b.start_time','>=',$start.' 00:00:00'];
  279. }
  280. $end = input('end', date('Y-m-d'));
  281. if($end != ''){
  282. $map[] = ['b.end_time','<=',$end.' 23:59:59'];
  283. }
  284. if(request()->isAjax()){
  285. //分页参数
  286. $length = input('rows',10,'intval'); //每页条数
  287. $page = input('page',1,'intval'); //第几页
  288. $start = ($page - 1) * $length; //分页开始位置
  289. $map[] = ['b.org_id','=',$this->orgId];
  290. $map= empty($map) ? true: $map;
  291. //数据查询
  292. $lists = Db::name('patrol_task_user')
  293. ->alias('a')
  294. ->join('patrol_task b','a.patrol_task_id=b.id')
  295. ->field('b.*,a.user_id')
  296. ->where($map)
  297. ->limit($start,$length)
  298. ->order('b.id','desc')
  299. ->select();
  300. foreach ($lists as $k=>$v){
  301. $lists[$k]['task_user'] = Db::name('user')
  302. ->where('id', $v['user_id'])->value('real_name');
  303. }
  304. //数据返回
  305. $totalCount = Db::name('patrol_task_user')
  306. ->alias('a')
  307. ->join('patrol_task b','a.patrol_task_id=b.id')->where($map)->count();
  308. $totalPage = ceil($totalCount/$length);
  309. $result['page'] = $page;
  310. $result['total'] = $totalPage;
  311. $result['records'] = $totalCount;
  312. $result['rows'] = $lists;
  313. return json($result);
  314. }else{
  315. $type = input('type',1);
  316. $t = Db::name('patrol_mode')
  317. ->where('id',$type)
  318. ->value('name');
  319. $this->assign('is_normal',$is_normal);
  320. $this->assign('type',$type);
  321. $this->assign('start',$start);
  322. $this->assign('end',$end);
  323. $this->assign('user_id',$user_id);
  324. $this->assign('meta_title',$t.'人员工作量统计');
  325. return $this->fetch();
  326. }
  327. }
  328. public function workExport() {
  329. $cur = date('Y-m-d');
  330. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  331. $end = input('end', date('Y-m-d'));
  332. $start1 = $start . ' 00:00:00';
  333. $end1 = $end . ' 23:59:59';
  334. $type = input('type', 1);
  335. $ret = $this->workData($start1, $end1, $type);
  336. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  337. //实例化PHPExcel类
  338. $objPHPExcel = new \PHPExcel();
  339. //激活当前的sheet表
  340. $objPHPExcel->setActiveSheetIndex(0);
  341. //设置表格头(即excel表格的第一行)
  342. $objPHPExcel->setActiveSheetIndex(0)
  343. ->setCellValue('A1', 'ID')
  344. ->setCellValue('B1', '姓名')
  345. ->setCellValue('C1', '工作记录(总数)')
  346. ->setCellValue('D1', '工作记录(正常)')
  347. ->setCellValue('E1', '工作记录(异常)')
  348. ->setCellValue('F1', '完成率')
  349. ->setCellValue('G1', '需要完成')
  350. ->setCellValue('H1', '已完成');
  351. // 设置表格头水平居中
  352. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  353. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  354. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  355. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  356. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  357. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  358. //设置列水平居中
  359. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  360. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  361. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  362. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  363. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  364. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  365. //设置单元格宽度
  366. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  367. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  368. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  369. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  370. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  371. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
  372. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  373. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  374. //循环刚取出来的数组,将数据逐一添加到excel表格。
  375. for ($i = 0; $i < count($ret); $i++) {
  376. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);
  377. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['title']);
  378. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['count']);
  379. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['count1']);
  380. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['count2']);
  381. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $ret[$i]['wcl']);
  382. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $ret[$i]['count4']);
  383. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $ret[$i]['count3']);
  384. }
  385. $n = Db::name('patrol_mode')
  386. ->where('id', $type)
  387. ->value('name');
  388. //设置保存的Excel表格名称
  389. $filename = $n . '人员工作量统计_' . date('YmdHis', time()) . '.xls';
  390. //设置当前激活的sheet表格名称
  391. $objPHPExcel->getActiveSheet()->setTitle($n . '人员工作量统计');
  392. //设置浏览器窗口下载表格
  393. ob_end_clean();
  394. header("Content-Type: application/force-download");
  395. header("Content-Type: application/octet-stream");
  396. header("Content-Type: application/download");
  397. header('Content-Disposition:inline;filename="' . $filename);
  398. //生成excel文件
  399. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  400. //下载文件在浏览器窗口
  401. return $objWriter->save('php://output');
  402. }
  403. public function patrolList(){
  404. $patrol_addr_id = input('patrol_addr_id','','trim');
  405. if($patrol_addr_id!=''){
  406. $map[] = ['patrol_addr_id','=',$patrol_addr_id];
  407. }
  408. $is_normal = input('is_normal','','trim');
  409. if($is_normal!==''){
  410. $map[] = ['is_normal','=',$is_normal];
  411. }
  412. $type = input('type','','trim');
  413. if($type != ''){
  414. $map[] = ['patrol_mode','=',$type];
  415. }
  416. $start = input('start',date('Y-m-d', strtotime('' . date('Y-m-d') . ' -1 week')));
  417. if($start != ''){
  418. $map[] = ['create_time','>=',$start.' 00:00:00'];
  419. }
  420. $end = input('end', date('Y-m-d'));
  421. if($end != ''){
  422. $map[] = ['create_time','<=',$end.' 23:59:59'];
  423. }
  424. if(request()->isAjax()){
  425. //分页参数
  426. $length = input('rows',10,'intval'); //每页条数
  427. $page = input('page',1,'intval'); //第几页
  428. $start = ($page - 1) * $length; //分页开始位置
  429. //排序
  430. $sortRow = input('sidx','sort','trim'); //排序列
  431. $sort = input('sord','asc','trim'); //排序方式
  432. $order = $sortRow.' '.$sort.' ,id desc';
  433. $map[] = ['org_id','=',$this->orgId];
  434. $map= empty($map) ? true: $map;
  435. //数据查询
  436. $lists = Db::name('patrol_record')
  437. ->where($map)
  438. ->limit($start,$length)
  439. ->order($order)
  440. ->select();
  441. foreach ($lists as $k=>$v){
  442. $lists[$k]['address_title'] = Db::name('address')
  443. ->where('id', $v['patrol_addr_id'])
  444. ->value('title');
  445. $lists[$k]['task_user'] = Db::name('user')
  446. ->where('id', $v['user_id'])->value('real_name');
  447. $lists[$k]['task_title'] = Db::name('patrol_task')
  448. ->where('id', $v['patrol_task_id'])
  449. ->value('title');
  450. }
  451. //数据返回
  452. $totalCount = Db::name('patrol_record')->where($map)->count();
  453. $totalPage = ceil($totalCount/$length);
  454. $result['page'] = $page;
  455. $result['total'] = $totalPage;
  456. $result['records'] = $totalCount;
  457. $result['rows'] = $lists;
  458. return json($result);
  459. }else{
  460. $type = input('type',1);
  461. $t = Db::name('patrol_mode')
  462. ->where('id',$type)
  463. ->value('name');
  464. $this->assign('is_normal',$is_normal);
  465. $this->assign('type',$type);
  466. $this->assign('start',$start);
  467. $this->assign('end',$end);
  468. $this->assign('patrol_addr_id',$patrol_addr_id);
  469. $this->assign('meta_title',$t.'检查记录');
  470. return $this->fetch();
  471. }
  472. }
  473. }