DeviceRecord.php 11 KB

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