isAjax()) { //分页参数 $length = input('rows', 10, 'intval'); //每页条数 $page = input('page', 1, 'intval'); //第几页 $start = ($page - 1) * $length; //分页开始位置 //排序 $sortRow = input('sidx', 'sort', 'trim'); //排序列 $sort = input('sord', 'asc', 'trim'); //排序方式 $order = $sortRow . ' ' . $sort . ' ,id desc'; $title = input('title', '', 'trim'); if ($title) { $map[] = ['pa.title', 'like', '%' . $title . '%']; } $plan_id = input('plan_id','','trim'); if($plan_id != ''){ $map[] = ['pt.plan_id','=',$plan_id]; } $b = input('start', '', 'trim'); $e = input('end', '', 'trim'); if ($b) { $b = date('Ymd', strtotime($b)); $map[] = ['pr.create_yyyymmdd', '>=', $b]; } if ($e) { $e = date('Ymd', strtotime($e)); $map[] = ['pr.create_yyyymmdd', '<=', $e]; } $map[] = ['pr.org_id', '=', $this->orgId]; $map[] = ['pr.patrol_mode', '=', $mode]; $map = empty($map) ? true : $map; //数据查询 $lists = Db::name('patrol_record') ->alias('pr') ->field('pr.*,pa.title,pt.title as task_title') ->join('patrol_task pt','pt.id = pr.patrol_task_id') ->Leftjoin('address pa', 'pa.id=pr.patrol_addr_id') ->where($map) ->limit($start, $length) ->order($order) ->select(); foreach ($lists as $k => $v) { $lists[$k]['task_user'] = Db::name('user') ->where('id', $v['user_id'])->value('real_name'); $status = 0; if($v['order_id'] > 0 ){ $order = Db::name('orders') ->where('id',$v['order_id']) ->where('quality_type',$v['patrol_mode']+1) ->whereIn('order_mode',[5,6])->where('del',0)->find(); if($order){ $status = 2; }else{ $status = 1; } } $lists[$k]['zg_type'] = $status; $lists[$k]['exception'] = 1; $checkJson = json_decode($v['check_json'],true); foreach ($checkJson as $kk=>$vv){ if(isset($vv['forms'])){ foreach ($vv['forms'] as $kkk=>$vvv){ if(isset($vvv['status']) && $vvv['status'] == 0 && $lists[$k]['exception'] == 1){ $lists[$k]['exception'] = 0; break; } } } } } //数据返回 $where[] = ['pr.org_id', '=', $this->orgId]; $where[] = ['pr.patrol_mode', '=', $mode]; $totalCount = Db::name('patrol_record') ->alias('pr') ->field('pr.*,pa.title,pt.title as task_title') ->join('patrol_task pt','pt.id = pr.patrol_task_id') ->Leftjoin('address pa', 'pa.id=pr.patrol_addr_id') ->where($map)->count(); $totalPage = ceil($totalCount / $length); $result['page'] = $page; $result['total'] = $totalPage; $result['records'] = $totalCount; $result['rows'] = $lists; return json($result); } else { $plans = model('PatrolPlan')->getListByMode($this->orgId,$mode); $this->assign('plans',$plans); $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode); $this->assign('meta_title', $modeName . '记录列表'); $this->assign('mode', $mode); return $this->fetch(); } } // 详情 public function details($id, $mode) { $meta_title = '记录详情'; if (!$id) { $this->error('参数错误'); } $info = Db::name('patrol_record')->where('id', $id)->find(); $info['addr'] = Db::name('address') ->where('id', $info['patrol_addr_id'])->value('title'); $info['task_name'] = Db::name('patrol_task') ->where('id', $info['patrol_task_id'])->value('title'); $info['in_order'] = Db::name('patrol_task') ->where('id', $info['patrol_task_id'])->value('in_order'); $info['task_addr_form'] = Db::name('patrol_addr_form') ->alias('a') ->join('patrol_task_addr b','a.id = b.patrol_form_id') ->where('b.id', $info['patrol_addr_form_id'])->value('a.title'); $info['task_user'] = Db::name('user')->where('id', $info['user_id'])->value('real_name'); $info['check_json'] = json_decode($info['check_json'], true); $info['images'] = explode(',',$info['images']); $status = 0; if($info['order_id'] > 0 ){ $order = Db::name('orders') ->where('id',$info['order_id']) ->where('quality_type',$info['patrol_mode']+1) ->whereIn('order_mode',[5,6])->where('del',0)->find(); if($order){ $status = 2; }else{ $status = 1; } } $info['zg_type'] = $status; $this->assign('meta_title', $meta_title); $this->assign('info', $info); $this->assign('mode', $mode); return $this->fetch(); } //excel导出 public function export($mode) { //获取excel表名 $tableName = Db::name('patrol_mode')->where('id', $mode)->value('name'); $meta_title = $tableName . ' 记录导出'; include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php'; if (request()->isGet()) { $b = input('start', '', 'trim'); $e = input('end', '', 'trim'); if ($b) { $b = date('Ymd', strtotime($b)); $where[] = ['pr.create_yyyymmdd', '>=', $b]; } if ($e) { $e = date('Ymd', strtotime($e)); $where[] = ['pr.create_yyyymmdd', '<=', $e]; } $title = input('title', '', 'trim'); if ($title) { $where[] = ['pa.title', 'like', '%' . $title . '%']; } $plan_id = input('plan_id','','trim'); if($plan_id != ''){ $where[] = ['pt.plan_id','=',$plan_id]; } $where[] = ['pr.patrol_mode', '=', $mode]; $where[] = ['pr.org_id', '=', $this->orgId]; $ret = Db::name('patrol_record') ->alias('pr') ->field('pr.*,pa.title,pt.title as task_title') ->join('patrol_task pt','pt.id = pr.patrol_task_id') ->Leftjoin('address pa', 'pa.id=pr.patrol_addr_id') ->where($where)->select(); foreach ($ret as $k => $v) { $ret[$k]['addr'] = Db::name('address')->where('id', $v['patrol_addr_id'])->value('title'); $ret[$k]['task_title'] = Db::name('patrol_task')->where('id', $v['patrol_task_id'])->value('title'); $ret[$k]['task_user'] = Db::name('user')->where('id', $v['user_id'])->value('real_name'); } //实例化PHPExcel类 $objPHPExcel =new \PHPExcel(); //激活当前的sheet表 $objPHPExcel->setActiveSheetIndex(0); //设置表格头(即excel表格的第一行) $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '编号') ->setCellValue('B1', '执行人') ->setCellValue('C1', '计划') ->setCellValue('D1', '地点') ->setCellValue('E1', '汇报内容') ->setCellValue('F1', '提交日期') ->setCellValue('G1', '检查项'); // 设置表格头水平居中 $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //设置列水平居中 $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //设置单元格宽度 $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(90); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30); //循环刚取出来的数组,将数据逐一添加到excel表格。 for ($i = 0; $i < count($ret); $i++) { $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['task_user']);//执行人 $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['task_title']);//名称 $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['addr']);//地点 $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['content']);//汇报内容 $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $ret[$i]['create_time']);//提交时间 $result = json_decode($ret[$i]['check_json'], true); /*foreach ($result as $kk => $vv) { $ss = $vv['title'] . ":"; if ($vv['type'] == 1) { $ss .= $vv['result']; } else { if ($vv['result'] == 1) { $ss .= '☑'; } else { $ss .= '□'; } } $str[] = $ss; }*/ $str = []; foreach ($result as $rk => $rv) { if(isset($rv['forms'])){ foreach ($rv['forms'] as $kk => $vv) { $ss = $vv['title'] . ":"; if ($vv['type'] == 1) { $ss .= $vv['result']; } else { if ($vv['result'] == 1) { $ss .= '☑'; } else { $ss .= '□'; } } $str[] = $ss; } } } $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), implode(' ; ', $str));//检查项 } //设置保存的Excel表格名称 $filename = $tableName . '记录_' . date('YmdHis', time()) . '.xls'; //设置当前激活的sheet表格名称 $objPHPExcel->getActiveSheet()->setTitle($tableName); //设置浏览器窗口下载表格 ob_end_clean(); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="' . $filename); //生成excel文件 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //下载文件在浏览器窗口 return $objWriter->save('php://output'); } } }