table = 'waste_package'; } public function index() { if (request()->isAjax()) { //分页参数 $length = input('rows', 10, 'intval'); //每页条数 $page = input('page', 1, 'intval'); //第几页 $start = ($page - 1) * $length; //分页开始位置 //排序 $sortRow = input('sidx', 'id', 'trim'); //排序列 $sort = input('sord', 'desc', 'trim'); //排序方式 $order = $sortRow . ' ' . $sort; $cateid = input('cateid', '', 'trim'); if ($cateid) { $map[] = ['cateid', 'like', '%' . $cateid . '%']; } $start_time = input('start', '', 'trim'); $end_time = input('end', '', 'trim'); if ($start_time) { $start_time = date('Ymd', strtotime($start_time)).' 00:00:00'; $map[] = ['create_time', '>=', $start_time]; } if ($end_time) { $end_time = date('Ymd', strtotime($end_time)).' 00:00:00'; $map[] = ['create_time', '<=', $end_time]; } $status = input('status', '', 'trim'); if ($status != '') { $map[] = ['status', '=', $status]; } $map[] = ['del', '=', 0]; $map[] = ['org_id', '=', $this->orgId]; $map = empty($map) ? true : $map; //数据查询 $lists = db($this->table)->where($map) ->limit($start, $length) ->order($order)->select(); foreach ($lists as $k => $v) { $lists[$k]['real_name'] = db('user') ->where('id', $v['user_id']) ->value('real_name'); $ids = explode(',',$v['record']); $record= Db::name('waste_record') ->where('id','in',$ids) ->where('del',0) ->where('org_id',$this->orgId) ->count(); $weight= Db::name('waste_record') ->where('id','in',$ids) ->where('del',0) ->where('org_id',$this->orgId) ->sum('weight'); $lists[$k]['weight'] = round($weight / 1000, 2); $lists[$k]['count'] = $record?$record:0; $lists[$k]['qrcode'] = url('Qrcode/qrcode') . '?code=' . $v['sn']; } //数据返回 $totalCount = db($this->table)->where($map)->count(); $totalPage = ceil($totalCount / $length); $result['page'] = $page; $result['total'] = $totalPage; $result['records'] = $totalCount; $result['rows'] = $lists; return json($result); } else { $this->assign('m_name', '打包记录'); $this->assign('status', [ 0=>"待交接", 1=>"待转运", 2=>"已转运", ]); return $this->fetch(); } } /** * 详情 * * @author wst * @date 2021/9/8 17:29 * @return array */ public function info($id = 0) { $info = Db::name('waste_package') ->where('id',$id) ->find(); if (!$info) { $this->error('记录不存在'); } $data = (new \app\common\model\WasteRecord())->packInfo($info['sn'],cur_org_id()); $info = $data['pinfo']; $record = $data['record']; $this->assign('info', $info); $this->assign('record', $record); return $this->fetch(); } /** * 转运 * * @author wst * @date 2021/9/8 17:48 * @param int $id * @return bool */ public function finish($id) { $info = $this->model->getOne($id,cur_org_id()); if (!$info) { $this->error('记录不存在'); } if ($info['status'] != 1) { $this->error('处于已交接状态的才可转运'); } $ret = $this->model->finish($id, is_login()); $ret ? $this->success('转运成功') : $this->error($this->model->getError()); } /** * 批量转运 * * @author wst * @date 2021/9/8 17:48 * @param array $ids * @return bool */ public function batchFinish() { $ids = input('ids', [], 'trim'); if (empty($ids)) { $this->error('未选择记录'); } $success = 0; $error = ''; foreach ($ids as $k => $v) { $info = $this->model->getOne($v,cur_org_id()); if (!$info) { $error .= 'id='.$v . ':记录不存在'; continue; } if ($info['status'] != 1) { $error .= 'id='.$v.':当期状态不可转运'; continue; } $ret = $this->model->finish($v, is_login()); if ($ret) { $success++; } else { $error .= 'id='.$v.':' . $this->model->getError(); } } $success ? $this->success('成功转运' . $success . '条记录') : $this->error($error); } /** * 删除记录 * @param int $id */ public function del($id = 0) { if (!$id) { $this->error('参数错误'); } $res = db($this->table)->where('id', $id)->update([ 'del' => 1, 'del_user_id' => is_login(), 'del_datetime' => getTime(), ]); if ($res) { $this->success('删除成功'); } else { $this->error('删除失败'); } } //excel导出 public function export() { $meta_title = '医废记录'; if (request()->isGet()) { $cateid = input('cateid', '', 'trim'); if ($cateid) { $map[] = ['cateid', 'like', '%' . $cateid . '%']; } $start_time = input('start', '', 'trim'); $end_time = input('end', '', 'trim'); if ($start_time !== '' && $end_time !== '') { if ($start_time <= $end_time) { $start_time = date('Ymd', strtotime($start_time)); $end_time = date('Ymd', strtotime($end_time)); $map[] = ['create_yyyymmdd', 'between', [$start_time, $end_time]]; } } if ($start_time !== '' && $end_time == '') { $start_time = date('Ymd', strtotime($start_time)); $map[] = ['create_yyyymmdd', '>=', $start_time]; } if ($start_time == '' && $end_time !== '') { $end_time = date('Ymd', strtotime($end_time)); $map[] = ['create_yyyymmdd', '<=', $end_time]; } $status = input('status', '', 'trim'); if ($status != '') { if ($status == 4) { $map[] = ['status', 'in', [0, 1]]; } else { if (in_array($status, array(0, 1, 2, 3))) { $map[] = ['status', '=', $status]; } } } $map[] = ['del', '=', 0]; $map[] = ['org_id', '=', $this->orgId]; $map = empty($map) ? true : $map; //数据查询 $lists = db($this->table)->where($map)->select(); foreach ($lists as $k => $v) { $lists[$k]['waste_device'] = db('address') ->where('id', $v['waste_device_id']) ->value('title'); $lists[$k]['real_name'] = db('user') ->where('id', $v['user_id']) ->value('real_name'); $lists[$k]['cate'] = db('waste_type') ->where('id', $v['cateid']) ->value('title'); $lists[$k]['weight'] = round($v['weight'] / 1000, 2); $lists[$k]['serial_number_path'] = url('Qrcode/qrcode') . '?code=' . $v['serial_number']; $lists[$k]['status'] = $this->model->status[$v['status']]; } include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php'; //实例化PHPExcel类 $objPHPExcel = new \PHPExcel(); //激活当前的sheet表 $objPHPExcel->setActiveSheetIndex(0); //设置表格头(即excel表格的第一行) $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '编号') ->setCellValue('B1', '收取科室') ->setCellValue('C1', '收取人') ->setCellValue('D1', '类别') ->setCellValue('E1', '重量(kg)') ->setCellValue('F1', '收取时间') ->setCellValue('G1', '状态') ->setCellValue('H1', '医废异常信息'); // 设置表格头水平居中 $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(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20); //循环刚取出来的数组,将数据逐一添加到excel表格。 for ($i = 0; $i < count($lists); $i++) { $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']); $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['waste_device']); $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['real_name']); $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['cate']); $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['weight']); $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['create_time']); $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['status']); $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['reason']); } //设置保存的Excel表格名称 $filename = $meta_title.'_' . date('YmdHis', time()) . '.xls'; //设置当前激活的sheet表格名称 $objPHPExcel->getActiveSheet()->setTitle($meta_title); //设置浏览器窗口下载表格 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'); } } }