PatrolStatistics.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. $addrIds = array_unique(array_column($list, 'patrol_addr_id'));
  51. $addrIds = is_array($addrIds) ? array_filter($addrIds) : [];
  52. $addressTitles = [];
  53. if (!empty($addrIds)) {
  54. $addressTitles = Db::name('address')
  55. ->whereIn('id', $addrIds)
  56. ->column('title', 'id');
  57. }
  58. $stats = [];
  59. if (!empty($addrIds)) {
  60. $query = Db::name('patrol_record')
  61. ->field([
  62. 'patrol_addr_id',
  63. 'SUM(CASE WHEN is_normal = 0 THEN 1 ELSE 0 END) as abnormal_count',
  64. 'SUM(CASE WHEN is_normal = 1 THEN 1 ELSE 0 END) as normal_count'
  65. ])
  66. ->whereIn('patrol_addr_id', $addrIds) // 确保 $addrIds 是数组
  67. ->where('patrol_mode', $type)
  68. ->where($map1);
  69. $results = $query->group('patrol_addr_id')->select();
  70. foreach ($results as $row) {
  71. $stats[$row['patrol_addr_id']] = [
  72. 'abnormal_count' => (int)$row['abnormal_count'],
  73. 'normal_count' => (int)$row['normal_count']
  74. ];
  75. }
  76. foreach ($addrIds as $id) {
  77. if (!isset($stats[$id])) {
  78. $stats[$id] = ['abnormal_count' => 0, 'normal_count' => 0];
  79. }
  80. }
  81. }
  82. foreach ($list as $k => $v) {
  83. $addrId = $v['patrol_addr_id'];
  84. $list[$k]['title'] = $addressTitles[$addrId] ?? '';
  85. $list[$k]['count'] = $stats[$addrId]['abnormal_count'] ?? 0;
  86. $list[$k]['normal'] = $stats[$addrId]['normal_count'] ?? 0;
  87. }
  88. return $list;
  89. }
  90. public function patrolExport() {
  91. $cur = date('Y-m-d');
  92. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  93. $end = input('end', date('Y-m-d'));
  94. $start1 = $start . ' 00:00:00';
  95. $end1 = $end . ' 23:59:59';
  96. $type = input('type', 1);
  97. $ret = $this->patrolData($start1, $end1, $type);
  98. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  99. //实例化PHPExcel类
  100. $objPHPExcel = new \PHPExcel();
  101. //激活当前的sheet表
  102. $objPHPExcel->setActiveSheetIndex(0);
  103. //设置表格头(即excel表格的第一行)
  104. $objPHPExcel->setActiveSheetIndex(0)
  105. ->setCellValue('A1', '智慧点')
  106. ->setCellValue('B1', '总数')
  107. ->setCellValue('C1', '总数(异常)');
  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]['title']);
  129. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['count']);
  130. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['normal']);
  131. }
  132. $n = Db::name('patrol_mode')
  133. ->where('id', $type)
  134. ->value('name');
  135. //设置保存的Excel表格名称
  136. $filename = $n . '工作量统计分析_' . date('YmdHis', time()) . '.xls';
  137. //设置当前激活的sheet表格名称
  138. $objPHPExcel->getActiveSheet()->setTitle($n . '工作量统计分析');
  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 work() {
  151. $cur = date('Y-m-d');
  152. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  153. $end = input('end', date('Y-m-d'));
  154. $start1 = $start . ' 00:00:00';
  155. $end1 = $end . ' 23:59:59';
  156. $type = input('type', 1);
  157. $list = $this->workData($start1, $end1, $type);
  158. $this->assign('type', $type);
  159. $this->assign('list', $list);
  160. $this->assign('start', $start);
  161. $this->assign('end', $end);
  162. return $this->fetch();
  163. }
  164. public function workData($start1, $end1, $type) {
  165. $map1[] = ['create_time', '>=', $start1];
  166. $map1[] = ['create_time', '<=', $end1];
  167. $map1[] = ['org_id', '=', $this->orgId];
  168. $list = Db::name('patrol_record')
  169. ->where($map1)
  170. ->where('patrol_mode', $type)
  171. ->group('user_id')
  172. ->distinct(true)
  173. ->select();
  174. $userIds = array_unique(array_column($list, 'user_id'));
  175. $userIds = array_filter($userIds);
  176. // 一次性取出姓名(原循环内逐条查询 -> 1 条)
  177. $userNames = [];
  178. if (!empty($userIds)) {
  179. $userNames = Db::name('user')
  180. ->whereIn('id', $userIds)
  181. ->column('real_name', 'id');
  182. }
  183. // 一次性聚合每个用户的记录统计(原 3 次 count -> 1 条)
  184. $recordStats = [];
  185. if (!empty($userIds)) {
  186. $rows = Db::name('patrol_record')
  187. ->field([
  188. 'user_id',
  189. 'COUNT(*) as total',
  190. 'SUM(CASE WHEN is_normal = 0 THEN 1 ELSE 0 END) as cnt0',
  191. 'SUM(CASE WHEN is_normal = 1 THEN 1 ELSE 0 END) as cnt1'
  192. ])
  193. ->where($map1)
  194. ->where('patrol_mode', $type)
  195. ->whereIn('user_id', $userIds)
  196. ->group('user_id')
  197. ->select();
  198. foreach ($rows as $row) {
  199. $recordStats[$row['user_id']] = $row;
  200. }
  201. }
  202. // 一次性聚合每个用户的任务统计(原 2 次 join count -> 1 条)
  203. $taskStats = [];
  204. if (!empty($userIds)) {
  205. $rows = Db::name('patrol_task_user')
  206. ->alias('a')
  207. ->join('patrol_task b', 'a.patrol_task_id=b.id')
  208. ->field([
  209. 'a.user_id',
  210. 'COUNT(*) as task_total',
  211. 'SUM(CASE WHEN b.status = 2 THEN 1 ELSE 0 END) as task_done'
  212. ])
  213. ->where('a.user_id', 'in', $userIds)
  214. ->where('b.org_id', $this->orgId)
  215. ->where('b.status', '<>', 6)
  216. ->where('b.start_time', '>=', $start1)
  217. ->where('b.end_time', '<=', $end1)
  218. ->where('b.patrol_mode', '=', $type)
  219. ->group('a.user_id')
  220. ->select();
  221. foreach ($rows as $row) {
  222. $taskStats[$row['user_id']] = $row;
  223. }
  224. }
  225. foreach ($list as $k => $v) {
  226. $uid = $v['user_id'];
  227. $list[$k]['title'] = $userNames[$uid] ?? '';
  228. $rs = $recordStats[$uid] ?? null;
  229. $list[$k]['count'] = $rs ? (int)$rs['total'] : 0; // 总数
  230. $list[$k]['count1'] = $rs ? (int)$rs['cnt0'] : 0; // 异常(is_normal=0)
  231. $list[$k]['count2'] = $rs ? (int)$rs['cnt1'] : 0; // 正常(is_normal=1)
  232. $ts = $taskStats[$uid] ?? null;
  233. $taskCount = $ts ? (int)$ts['task_total'] : 0; // 需要完成(status<>6)
  234. $ywcCount = $ts ? (int)$ts['task_done'] : 0; // 已完成(status=2)
  235. $wcl = $taskCount > 0 ? round($ywcCount / $taskCount, 2) * 100 : 0;
  236. $wcl = $wcl . '%';
  237. $list[$k]['wcl'] = $wcl;
  238. $list[$k]['count3'] = $ywcCount;
  239. $list[$k]['count4'] = $taskCount;
  240. }
  241. return $list;
  242. }
  243. public function workDataOld($start1, $end1, $type) {
  244. $map1[] = ['create_time', '>=', $start1];
  245. $map1[] = ['create_time', '<=', $end1];
  246. $map1[] = ['org_id', '=', $this->orgId];
  247. $list = Db::name('patrol_record')
  248. ->where($map1)
  249. ->where('patrol_mode', $type)
  250. ->group('user_id')
  251. ->distinct(true)
  252. ->select();
  253. foreach ($list as $k => $v) {
  254. $list[$k]['title'] = Db::name('user')
  255. ->where('id', $v['user_id'])
  256. ->value('real_name');
  257. $c = Db::name('patrol_record')
  258. ->where($map1)
  259. ->where('user_id', $v['user_id'])
  260. ->where('patrol_mode', $type)
  261. ->count();
  262. $list[$k]['count'] = $c ? $c : 0;
  263. $c1 = Db::name('patrol_record')
  264. ->where($map1)
  265. ->where('user_id', $v['user_id'])
  266. ->where('patrol_mode', $type)
  267. ->where('is_normal', 0)
  268. ->count();
  269. $list[$k]['count1'] = $c1 ? $c1 : 0;
  270. $c2 = Db::name('patrol_record')
  271. ->where($map1)
  272. ->where('user_id', $v['user_id'])
  273. ->where('patrol_mode', $type)
  274. ->where('is_normal', 1)
  275. ->count();
  276. $list[$k]['count2'] = $c2 ? $c2 : 0;
  277. $taskCount = Db::name('patrol_task_user')
  278. ->alias('a')
  279. ->join('patrol_task b','a.patrol_task_id=b.id')
  280. ->where('a.user_id',$v['user_id'])
  281. ->where('b.org_id',$this->orgId)
  282. ->where('b.status','<>',6)
  283. ->where('b.start_time','>=',$start1)
  284. ->where('b.end_time','<=',$end1)
  285. ->where('b.patrol_mode','=',$type)
  286. ->count();
  287. $ywcCount = Db::name('patrol_task_user')
  288. ->alias('a')
  289. ->join('patrol_task b','a.patrol_task_id=b.id')
  290. ->where('a.user_id',$v['user_id'])
  291. ->where('b.org_id',$this->orgId)
  292. ->where('b.status','=',2)
  293. ->where('b.start_time','>=',$start1)
  294. ->where('b.end_time','<=',$end1)
  295. ->where('b.patrol_mode','=',$type)
  296. ->count();
  297. $wcl = $taskCount>0?round($ywcCount/$taskCount,2)*100:0;
  298. $wcl = $wcl.'%';
  299. $list[$k]['wcl'] = $wcl;
  300. $list[$k]['count3'] = $ywcCount;
  301. $list[$k]['count4'] = $taskCount;
  302. }
  303. return $list;
  304. }
  305. public function workList(){
  306. $user_id = input('user_id','','trim');
  307. if($user_id!=''){
  308. $map[] = ['user_id','=',$user_id];
  309. }
  310. $is_normal = input('is_normal','','trim');
  311. if($is_normal!==''){
  312. $map[] = ['is_normal','=',$is_normal];
  313. }
  314. $type = input('type','','trim');
  315. if($type != ''){
  316. $map[] = ['patrol_mode','=',$type];
  317. }
  318. $start = input('start',date('Y-m-d', strtotime('' . date('Y-m-d') . ' -1 week')));
  319. if($start != ''){
  320. $map[] = ['create_time','>=',$start.' 00:00:00'];
  321. }
  322. $end = input('end', date('Y-m-d'));
  323. if($end != ''){
  324. $map[] = ['create_time','<=',$end.' 23:59:59'];
  325. }
  326. if(request()->isAjax()){
  327. //分页参数
  328. $length = input('rows',10,'intval'); //每页条数
  329. $page = input('page',1,'intval'); //第几页
  330. $start = ($page - 1) * $length; //分页开始位置
  331. //排序
  332. $sortRow = input('sidx','sort','trim'); //排序列
  333. $sort = input('sord','asc','trim'); //排序方式
  334. $order = $sortRow.' '.$sort.' ,id desc';
  335. $map[] = ['org_id','=',$this->orgId];
  336. $map= empty($map) ? true: $map;
  337. //数据查询
  338. $lists = Db::name('patrol_record')
  339. ->where($map)
  340. ->limit($start,$length)
  341. ->order($order)
  342. ->select();
  343. foreach ($lists as $k=>$v){
  344. $lists[$k]['address_title'] = Db::name('address')
  345. ->where('id', $v['patrol_addr_id'])
  346. ->value('title');
  347. $lists[$k]['task_user'] = Db::name('user')
  348. ->where('id', $v['user_id'])->value('real_name');
  349. $lists[$k]['task_title'] = Db::name('patrol_task')
  350. ->where('id', $v['patrol_task_id'])
  351. ->value('title');
  352. }
  353. //数据返回
  354. $totalCount = Db::name('patrol_record')->where($map)->count();
  355. $totalPage = ceil($totalCount/$length);
  356. $result['page'] = $page;
  357. $result['total'] = $totalPage;
  358. $result['records'] = $totalCount;
  359. $result['rows'] = $lists;
  360. return json($result);
  361. }else{
  362. $type = input('type',1);
  363. $t = Db::name('patrol_mode')
  364. ->where('id',$type)
  365. ->value('name');
  366. $this->assign('is_normal',$is_normal);
  367. $this->assign('type',$type);
  368. $this->assign('start',$start);
  369. $this->assign('end',$end);
  370. $this->assign('user_id',$user_id);
  371. $this->assign('meta_title',$t.'人员工作量统计');
  372. return $this->fetch();
  373. }
  374. }
  375. public function workList1(){
  376. $user_id = input('user_id','','trim');
  377. if($user_id!=''){
  378. $map[] = ['a.user_id','=',$user_id];
  379. }
  380. $is_normal = input('is_normal','','trim');
  381. if($is_normal!==''){
  382. if($is_normal==1){//总数
  383. $map[] = ['b.status','<>',6];
  384. }
  385. if($is_normal==2){//已完成
  386. $map[] = ['b.status','=',2];
  387. }
  388. }
  389. $type = input('type','','trim');
  390. if($type != ''){
  391. $map[] = ['b.patrol_mode','=',$type];
  392. }
  393. $start = input('start',date('Y-m-d', strtotime('' . date('Y-m-d') . ' -1 week')));
  394. if($start != ''){
  395. $map[] = ['b.start_time','>=',$start.' 00:00:00'];
  396. }
  397. $end = input('end', date('Y-m-d'));
  398. if($end != ''){
  399. $map[] = ['b.end_time','<=',$end.' 23:59:59'];
  400. }
  401. if(request()->isAjax()){
  402. //分页参数
  403. $length = input('rows',10,'intval'); //每页条数
  404. $page = input('page',1,'intval'); //第几页
  405. $start = ($page - 1) * $length; //分页开始位置
  406. $map[] = ['b.org_id','=',$this->orgId];
  407. $map= empty($map) ? true: $map;
  408. //数据查询
  409. $lists = Db::name('patrol_task_user')
  410. ->alias('a')
  411. ->join('patrol_task b','a.patrol_task_id=b.id')
  412. ->field('b.*,a.user_id')
  413. ->where($map)
  414. ->limit($start,$length)
  415. ->order('b.id','desc')
  416. ->select();
  417. foreach ($lists as $k=>$v){
  418. $lists[$k]['task_user'] = Db::name('user')
  419. ->where('id', $v['user_id'])->value('real_name');
  420. }
  421. //数据返回
  422. $totalCount = Db::name('patrol_task_user')
  423. ->alias('a')
  424. ->join('patrol_task b','a.patrol_task_id=b.id')->where($map)->count();
  425. $totalPage = ceil($totalCount/$length);
  426. $result['page'] = $page;
  427. $result['total'] = $totalPage;
  428. $result['records'] = $totalCount;
  429. $result['rows'] = $lists;
  430. return json($result);
  431. }else{
  432. $type = input('type',1);
  433. $t = Db::name('patrol_mode')
  434. ->where('id',$type)
  435. ->value('name');
  436. $this->assign('is_normal',$is_normal);
  437. $this->assign('type',$type);
  438. $this->assign('start',$start);
  439. $this->assign('end',$end);
  440. $this->assign('user_id',$user_id);
  441. $this->assign('meta_title',$t.'人员工作量统计');
  442. return $this->fetch();
  443. }
  444. }
  445. public function workExport() {
  446. $cur = date('Y-m-d');
  447. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  448. $end = input('end', date('Y-m-d'));
  449. $start1 = $start . ' 00:00:00';
  450. $end1 = $end . ' 23:59:59';
  451. $type = input('type', 1);
  452. $ret = $this->workData($start1, $end1, $type);
  453. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  454. //实例化PHPExcel类
  455. $objPHPExcel = new \PHPExcel();
  456. //激活当前的sheet表
  457. $objPHPExcel->setActiveSheetIndex(0);
  458. //设置表格头(即excel表格的第一行)
  459. $objPHPExcel->setActiveSheetIndex(0)
  460. ->setCellValue('A1', 'ID')
  461. ->setCellValue('B1', '姓名')
  462. ->setCellValue('C1', '工作记录(总数)')
  463. ->setCellValue('D1', '工作记录(正常)')
  464. ->setCellValue('E1', '工作记录(异常)')
  465. ->setCellValue('F1', '完成率')
  466. ->setCellValue('G1', '需要完成')
  467. ->setCellValue('H1', '已完成');
  468. // 设置表格头水平居中
  469. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  470. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  471. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  472. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  473. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  474. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  475. //设置列水平居中
  476. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  477. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  478. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  479. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  480. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  481. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  482. //设置单元格宽度
  483. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  484. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  485. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  486. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  487. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  488. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
  489. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  490. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  491. //循环刚取出来的数组,将数据逐一添加到excel表格。
  492. for ($i = 0; $i < count($ret); $i++) {
  493. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);
  494. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['title']);
  495. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['count']);
  496. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['count1']);
  497. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['count2']);
  498. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $ret[$i]['wcl']);
  499. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $ret[$i]['count4']);
  500. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $ret[$i]['count3']);
  501. }
  502. $n = Db::name('patrol_mode')
  503. ->where('id', $type)
  504. ->value('name');
  505. //设置保存的Excel表格名称
  506. $filename = $n . '人员工作量统计_' . date('YmdHis', time()) . '.xls';
  507. //设置当前激活的sheet表格名称
  508. $objPHPExcel->getActiveSheet()->setTitle($n . '人员工作量统计');
  509. //设置浏览器窗口下载表格
  510. ob_end_clean();
  511. header("Content-Type: application/force-download");
  512. header("Content-Type: application/octet-stream");
  513. header("Content-Type: application/download");
  514. header('Content-Disposition:inline;filename="' . $filename);
  515. //生成excel文件
  516. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  517. //下载文件在浏览器窗口
  518. return $objWriter->save('php://output');
  519. }
  520. public function patrolList(){
  521. $patrol_addr_id = input('patrol_addr_id','','trim');
  522. if($patrol_addr_id!=''){
  523. $map[] = ['patrol_addr_id','=',$patrol_addr_id];
  524. }
  525. $is_normal = input('is_normal','','trim');
  526. if($is_normal!==''){
  527. $map[] = ['is_normal','=',$is_normal];
  528. }
  529. $type = input('type','','trim');
  530. if($type != ''){
  531. $map[] = ['patrol_mode','=',$type];
  532. }
  533. $start = input('start',date('Y-m-d', strtotime('' . date('Y-m-d') . ' -1 week')));
  534. if($start != ''){
  535. $map[] = ['create_time','>=',$start.' 00:00:00'];
  536. }
  537. $end = input('end', date('Y-m-d'));
  538. if($end != ''){
  539. $map[] = ['create_time','<=',$end.' 23:59:59'];
  540. }
  541. if(request()->isAjax()){
  542. //分页参数
  543. $length = input('rows',10,'intval'); //每页条数
  544. $page = input('page',1,'intval'); //第几页
  545. $start = ($page - 1) * $length; //分页开始位置
  546. //排序
  547. $sortRow = input('sidx','sort','trim'); //排序列
  548. $sort = input('sord','asc','trim'); //排序方式
  549. $order = $sortRow.' '.$sort.' ,id desc';
  550. $map[] = ['org_id','=',$this->orgId];
  551. $map= empty($map) ? true: $map;
  552. //数据查询
  553. $lists = Db::name('patrol_record')
  554. ->where($map)
  555. ->limit($start,$length)
  556. ->order($order)
  557. ->select();
  558. foreach ($lists as $k=>$v){
  559. $lists[$k]['address_title'] = Db::name('address')
  560. ->where('id', $v['patrol_addr_id'])
  561. ->value('title');
  562. $lists[$k]['task_user'] = Db::name('user')
  563. ->where('id', $v['user_id'])->value('real_name');
  564. $lists[$k]['task_title'] = Db::name('patrol_task')
  565. ->where('id', $v['patrol_task_id'])
  566. ->value('title');
  567. }
  568. //数据返回
  569. $totalCount = Db::name('patrol_record')->where($map)->count();
  570. $totalPage = ceil($totalCount/$length);
  571. $result['page'] = $page;
  572. $result['total'] = $totalPage;
  573. $result['records'] = $totalCount;
  574. $result['rows'] = $lists;
  575. return json($result);
  576. }else{
  577. $type = input('type',1);
  578. $t = Db::name('patrol_mode')
  579. ->where('id',$type)
  580. ->value('name');
  581. $this->assign('is_normal',$is_normal);
  582. $this->assign('type',$type);
  583. $this->assign('start',$start);
  584. $this->assign('end',$end);
  585. $this->assign('patrol_addr_id',$patrol_addr_id);
  586. $this->assign('meta_title',$t.'检查记录');
  587. return $this->fetch();
  588. }
  589. }
  590. }