table='washing_record'; $this->model = new WashingRecord(); } 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; $title = input('title','','trim'); if($title!=''){ $map[] = ['title','like','%'.$title.'%']; } $start = input('start','','trim'); if($start != ''){ $map[] = ['create_yyyymm','>=',date('Ymd',strtotime($start))]; } $end = input('end','','trim'); if($end != ''){ $map[] = ['create_yyyymm','<=',date('Ymd',strtotime($end))]; } $mode = input('mode','','trim'); if($mode!=''){ $map[] = ['mode','=',$mode]; } $dep_id = input('dep_id','','trim'); if($dep_id!=''){ $map[] = ['dep_id','=',$dep_id]; } $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::name('user') ->where('id',$v['user_id']) ->value('real_name'); $lists[$k]['title'] = Db::name('address') ->where('id',$v['dep_id']) ->value('title'); $items = Db::name('washing_record_item') ->where('washing_record_id',$v['id']) ->select(); $s = ''; if(!empty($items)){ foreach ($items as $k1=>$v1){ $it = Db::name('washing_items') ->where('id',$v1['items_id']) ->value('title'); $s.=$it.':'.$v1['num'].' '; } } $lists[$k]['items'] = $s; } //数据返回 $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{ $address = (new \app\common\model\Address())->getListByType(9,$this->orgId); $this->assign('address',$address); $this->assign('ids',is_login()); return $this->fetch(); } } public function returnWash($id) { if (!$id) { $this->error('参数错误'); } $ret = $this->model->returnWash($id, $this->orgId); if (!$ret) { $this->error($this->model->getError()); } $this->success('归还成功'); } public function confirmWash($id) { if(!request()->isPost()){ if (!$id) { $this->error('参数错误'); } $this->assign('id',$id); return $this->fetch(); } else { $sign = input('sign'); if (!$sign) { $this->error('请上传签名图'); } $ret = $this->model->confirmWash($id, $this->orgId, $sign); if (!$ret) { $this->error($this->model->getError()); } $this->success('确认成功'); } } /** * 新增/编辑 */ public function add($id=0){ if(request()->isPost()){ $data = request()->post(); if (empty($data['dep_id'])) { $this->error('地点不能为空'); } if (empty($data['items'])) { $this->error('物品不能为空'); } $items = json_decode( $data['items'],true); foreach ($items as $k => $val) { if(empty($val['num'])){ $this->error($val['title'] . '数量不能为空'); } } foreach ($items as $k => $v) { $params['items'][$k] = [ 'items_id' => $v['id'], 'num' => $v['num'], ]; } $params['dep_id'] = $data['dep_id']; $params['org_id'] = $this->orgId; $params['user_id'] = $this->userId; $res = $this->model->addSave($params); if($res){ $this->success('操作成功',url('index')); }else{ $this->error($this->model->getError()); } }else{ $address = (new \app\common\model\Address())->getListByType(9,$this->orgId); $this->assign('dep',$address); $items = (new \app\common\model\WashingItems())->getList($this->orgId); $this->assign('items',$items); return $this->fetch(); } } /** * 删除记录 * @param int $id */ public function del($id=0){ if(!$id){ $this->error('参数错误'); } $res = db($this->table)->where('id',$id)->update([ 'del'=>1, 'del_time'=>getTime(), ]); if($res){ $this->success('删除成功'); }else{ $this->error('删除失败'); } } public function washRecordExport(){ $start = input('start','','trim'); if($start != ''){ $map[] = ['create_yyyymm','>=',date('Ymd',strtotime($start))]; } $end = input('end','','trim'); if($end != ''){ $map[] = ['create_yyyymm','<=',date('Ymd',strtotime($end))]; } $mode = input('mode','','trim'); if($mode!=''){ $map[] = ['mode','=',$mode]; } $dep_id = input('dep_id','','trim'); if($dep_id!=''){ $map[] = ['dep_id','=',$dep_id]; } $map[] = ['del','=',0]; $map[] = ['org_id','=',$this->orgId]; $map= empty($map) ? true: $map; //数据查询 $lists = Db::name('washing_record')->where($map)->order('id asc')->select(); foreach ($lists as $k=>$v){ $lists[$k]['real_name'] = Db::name('user') ->where('id',$v['user_id']) ->value('real_name'); $lists[$k]['title'] = Db::name('address') ->where('id',$v['dep_id']) ->value('title'); $items = Db::name('washing_record_item') ->where('washing_record_id',$v['id']) ->select(); $s = ''; if(!empty($items)){ foreach ($items as $k1=>$v1){ $it = Db::name('washing_items') ->where('id',$v1['items_id']) ->value('title'); $s.=$it.':'.$v1['num'].' '; } } $lists[$k]['items'] = $s; } 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', '领取时间') ->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]['real_name']); $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['title']); $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['items']); $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['create_time']); $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['return_time']); $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['confirm_time']); if($lists[$i]['mode'] == 1){ $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2),'已收取'); }elseif($lists[$i]['mode'] == 2){ $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2),'已归还'); }else{ $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2),'已确认'); } } //设置保存的Excel表格名称 $filename = '洗涤记录'.'_' . date('YmdHis', time()) . '.xls'; //设置当前激活的sheet表格名称 $objPHPExcel->getActiveSheet()->setTitle('洗涤记录'); //设置浏览器窗口下载表格 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'); } }