0
0

GreenRecord.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class GreenRecord extends Auth {
  6. public function index() {
  7. if (request()->isAjax()) {
  8. //分页参数
  9. $length = input('rows', 10, 'intval'); //每页条数
  10. $page = input('page', 1, 'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. $order = 'id desc';
  13. $title = input('title','','trim');
  14. if($title !=''){
  15. $map[] = ['ga.title','like','%'.$title.'%'];
  16. }
  17. $map[] = ['gr.org_id', '=', $this->orgId];
  18. $map = empty($map) ? true : $map;
  19. //数据查询
  20. $lists = Db::name('green_record')
  21. ->alias('gr')
  22. ->join('green_addr ga', 'ga.id=gr.green_addr_id')
  23. ->field('gr.*,ga.title')
  24. ->where($map)
  25. ->limit($start, $length)
  26. ->order($order)
  27. ->select();
  28. foreach ($lists as $k => $v) {
  29. $lists[$k]['user_name'] = Db::name('user')
  30. ->where('id', $v['user_id'])
  31. ->value('real_name');
  32. $lists[$k]['cate_name'] = Db::name('green_cate')->where('id',$v['green_cate_id'])->value('title');
  33. }
  34. //数据返回
  35. $totalCount = Db::name('green_record')
  36. ->alias('gr')
  37. ->field('gr.*,ga.title')
  38. ->join('green_addr ga', 'ga.id=gr.green_addr_id')
  39. ->where($map)
  40. ->count();
  41. $totalPage = ceil($totalCount / $length);
  42. $result['page'] = $page;
  43. $result['total'] = $totalPage;
  44. $result['records'] = $totalCount;
  45. $result['rows'] = $lists;
  46. return json($result);
  47. } else {
  48. $this->assign('meta_title', '养护记录列表');
  49. return $this->fetch();
  50. }
  51. }
  52. public function details($id) {
  53. if (!$id) {
  54. $this->error('参数错误');
  55. }
  56. $info = Db::name('green_record')->where('id', $id)->find();
  57. $info['addr'] = Db::name('green_addr')->where('id', $info['green_addr_id'])->value('title');
  58. $info['cate_name'] = Db::name('green_cate')->where('id',$info['green_cate_id'])->value('title');
  59. $info['user_name'] = Db::name('user')->where('id', $info['user_id'])
  60. ->value('real_name');
  61. $info['images'] = isset($info['images'])?explode(',',$info['images']):'';
  62. $this->assign('info', $info);
  63. $this->assign('meta_title', '养护记录详情');
  64. return $this->fetch();
  65. }
  66. //excel导出
  67. public function export() {
  68. $meta_title = '养护记录';
  69. if (request()->isPost()) {
  70. $start = input('start');
  71. $end = input('end');
  72. $where[] = ['create_yyyymmdd', '>=', $start];
  73. $where[] = ['create_yyyymmdd', '<=', $end];
  74. $where[] = ['org_id', '=', $this->orgId];
  75. $ret = Db::name('green_record')->where($where)->select();
  76. foreach ($ret as $k => $v) {
  77. $ret[$k]['user_name'] = Db::name('user')
  78. ->where('id', $v['user_id'])->value('real_name');
  79. $ret[$k]['addr'] = Db::name('green_addr')
  80. ->where('id', $v['green_addr_id'])
  81. ->value('title');
  82. $ret[$k]['cate_name'] = Db::name('green_cate')->where('id',$v['green_cate_id'])->value('title');
  83. }
  84. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  85. //实例化PHPExcel类
  86. $objPHPExcel = new \PHPExcel();
  87. //激活当前的sheet表
  88. $objPHPExcel->setActiveSheetIndex(0);
  89. //设置表格头(即excel表格的第一行)
  90. $objPHPExcel->setActiveSheetIndex(0)
  91. ->setCellValue('A1', '编号')
  92. ->setCellValue('B1', '地点名称')
  93. ->setCellValue('C1', '设备内容')
  94. ->setCellValue('D1', '执行人员')
  95. ->setCellValue('E1', '执行时间')
  96. ->setCellValue('F1', '分类');
  97. // 设置表格头水平居中
  98. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  99. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  100. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  101. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  102. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  103. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  104. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  105. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  106. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  107. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  108. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  109. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  110. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  111. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  112. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  113. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  114. //设置列水平居中
  115. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  116. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  117. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  118. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  119. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  120. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  121. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  122. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  123. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  124. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  125. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  126. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  127. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  128. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  129. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  130. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  131. //设置单元格宽度
  132. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  133. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  134. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  135. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  136. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  137. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  138. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  139. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  140. //循环刚取出来的数组,将数据逐一添加到excel表格。
  141. for ($i = 0; $i < count($ret); $i++) {
  142. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID
  143. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['addr']);
  144. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['content']);
  145. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['user_name']);//维护人员
  146. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['create_time']);//维护时间
  147. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $ret[$i]['cate_name']);
  148. }
  149. //设置保存的Excel表格名称
  150. $filename = '养护记录_' . date('YmdHis', time()) . '.xls';
  151. //设置当前激活的sheet表格名称
  152. $objPHPExcel->getActiveSheet()->setTitle('养护记录');
  153. //设置浏览器窗口下载表格
  154. ob_end_clean();
  155. header("Content-Type: application/force-download");
  156. header("Content-Type: application/octet-stream");
  157. header("Content-Type: application/download");
  158. header('Content-Disposition:inline;filename="' . $filename);
  159. //生成excel文件
  160. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  161. //下载文件在浏览器窗口
  162. return $objWriter->save('php://output');
  163. }
  164. $this->assign('s',date('Ymd'));
  165. $this->assign('e',date('Ymd',time()+(3*86400)));
  166. $this->assign('meta_title', $meta_title);
  167. return $this->fetch();
  168. }
  169. }