DeviceRecord.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class DeviceRecord extends Auth {
  5. public function index() {
  6. if (request()->isAjax()) {
  7. //分页参数
  8. $length = input('rows', 10, 'intval'); //每页条数
  9. $page = input('page', 1, 'intval'); //第几页
  10. $start = ($page - 1) * $length; //分页开始位置
  11. //排序
  12. $sortRow = input('sidx', 'sort', 'trim'); //排序列
  13. $sort = input('sord', 'asc', 'trim'); //排序方式
  14. $order = $sortRow . ' ' . $sort . ' ,id desc';
  15. $title = input('title', '', 'trim');
  16. if ($title) {
  17. $map[] = ['d.title', 'like', '%' . $title . '%'];
  18. }
  19. $cate_id = input('cate_id','','trim');
  20. if($cate_id != ''){
  21. $map[] = ['cate_id','=',$cate_id];
  22. }
  23. $map[] = ['dr.org_id', '=', $this->orgId];
  24. $map = empty($map) ? true : $map;
  25. //数据查询
  26. $lists = Db::name('device_record')
  27. ->alias('dr')
  28. ->join('device d', 'd.id=dr.device_id')
  29. ->field('dr.*,d.title as device_name,d.content as device_details')
  30. ->where($map)
  31. ->limit($start, $length)
  32. ->order($order)
  33. ->select();
  34. foreach ($lists as $k => $v) {
  35. $lists[$k]['user_name'] = Db::name('user')
  36. ->where('id', $v['user_id'])->value('real_name');
  37. $status = 0;
  38. if($v['order_id'] > 0 ){
  39. $order = Db::name('orders')
  40. ->where('id',$v['order_id'])
  41. ->where('quality_type',1)
  42. ->whereIn('order_mode',[5,6])->where('del',0)->find();
  43. if($order){
  44. $status = 2;
  45. }else{
  46. $status = 1;
  47. }
  48. }
  49. $lists[$k]['zg_type'] = $status;
  50. }
  51. //数据返回
  52. $totalCount = Db::name('device_record')
  53. ->alias('dr')
  54. ->join('device d', 'd.id=dr.device_id')
  55. ->field('dr.*,d.title as device_name,d.content as device_details')
  56. ->where($map)
  57. ->count();
  58. $totalPage = ceil($totalCount / $length);
  59. $result['page'] = $page;
  60. $result['total'] = $totalPage;
  61. $result['records'] = $totalCount;
  62. $result['rows'] = $lists;
  63. return json($result);
  64. }
  65. else {
  66. $this->assign('meta_title', '设备检查记录列表');
  67. $cate = model('DeviceCate')->list();
  68. $this->assign('cate',$cate);
  69. return $this->fetch();
  70. }
  71. }
  72. public function details($id) {
  73. if (!$id) {
  74. $this->error('参数错误');
  75. }
  76. $ret = Db::name('device_record')->where('id', $id)->find();
  77. $device = Db::name('device')->field('title,content')->where('id', $ret['device_id'])->find();
  78. $ret['title'] = $device['title'];
  79. $ret['device_details'] = $device['content'];
  80. $ret['images'] = explode(',',$ret['images']);
  81. $ret['check_json'] = json_decode($ret['check_json'], true);
  82. $ret['user_name'] = Db::name('user')->where('id', $ret['user_id'])
  83. ->value('real_name');
  84. $status = 0;
  85. if($ret['order_id'] > 0 ){
  86. $order = Db::name('orders')
  87. ->where('id',$ret['order_id'])
  88. ->where('quality_type',1)
  89. ->whereIn('order_mode',[5,6])->where('del',0)->find();
  90. if($order){
  91. $status = 2;
  92. }else{
  93. $status = 1;
  94. }
  95. }
  96. $ret['zg_type'] = $status;
  97. $this->assign('info', $ret);
  98. $this->assign('meta_title', '设备检查记录详情');
  99. return $this->fetch();
  100. }
  101. //excel导出
  102. public function export() {
  103. $meta_title = '设备检查记录';
  104. if (request()->isPost()) {
  105. $start = input('start');
  106. $end = input('end');
  107. $where[] = ['create_yyyymmdd', '>=', $start];
  108. $where[] = ['create_yyyymmdd', '<=', $end];
  109. $where[] = ['org_id', '=', $this->orgId];
  110. $ret = Db::name('device_record')->where($where)->select();
  111. foreach ($ret as $k => $v) {
  112. $device = Db::name('device')
  113. ->field('title,content')
  114. ->where('id', $v['device_id'])->find();
  115. $ret[$k]['user_name'] = Db::name('user')
  116. ->where('id', $v['user_id'])->value('real_name');
  117. $ret[$k]['device_name'] = $device['title'];
  118. $ret[$k]['device_details'] = $device['content'];
  119. }
  120. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  121. //实例化PHPExcel类
  122. $objPHPExcel = new \PHPExcel();
  123. //激活当前的sheet表
  124. $objPHPExcel->setActiveSheetIndex(0);
  125. //设置表格头(即excel表格的第一行)
  126. $objPHPExcel->setActiveSheetIndex(0)
  127. ->setCellValue('A1', '编号')
  128. ->setCellValue('B1', '设备名称')
  129. ->setCellValue('C1', '设备详情')
  130. ->setCellValue('D1', '维护人员')
  131. ->setCellValue('E1', '维护时间')
  132. ->setCellValue('F1', '检查项');
  133. // 设置表格头水平居中
  134. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  135. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  136. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  137. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  138. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  139. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  140. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  141. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  142. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  143. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  144. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  145. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  146. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  147. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  148. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  149. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  150. //设置列水平居中
  151. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  152. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  153. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  154. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  155. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  156. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  157. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  158. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  159. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  160. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  161. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  162. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  163. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  164. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  165. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  166. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  167. //设置单元格宽度
  168. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  169. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  170. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  171. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  172. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  173. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  174. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  175. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  176. //循环刚取出来的数组,将数据逐一添加到excel表格。
  177. for ($i = 0; $i < count($ret); $i++) {
  178. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID
  179. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['device_name']);//设备名称
  180. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['device_details']);//设备详情
  181. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['user_name']);//维护人员
  182. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['create_time']);//维护时间
  183. $result = json_decode($ret[$i]['check_json'], true);
  184. $str = array();
  185. foreach ($result as $kk => $vv) {
  186. $ss = $vv['title'] . ":";
  187. if ($vv['result'] == 1) {
  188. $ss .= '☑';
  189. }
  190. else {
  191. $ss .= '□';
  192. }
  193. $str[] = $ss;
  194. }
  195. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), implode(' ; ', $str));//检查项
  196. }
  197. //设置保存的Excel表格名称
  198. $filename = '设备检查记录_' . date('YmdHis', time()) . '.xls';
  199. //设置当前激活的sheet表格名称
  200. $objPHPExcel->getActiveSheet()->setTitle('设备检查记录');
  201. //设置浏览器窗口下载表格
  202. ob_end_clean();
  203. header("Content-Type: application/force-download");
  204. header("Content-Type: application/octet-stream");
  205. header("Content-Type: application/download");
  206. header('Content-Disposition:inline;filename="' . $filename);
  207. //生成excel文件
  208. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  209. //下载文件在浏览器窗口
  210. return $objWriter->save('php://output');
  211. }
  212. $this->assign('meta_title', $meta_title);
  213. return $this->fetch();
  214. }
  215. }