model = new \app\admin\model\dict\Examproject(); } /** * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写 */ public function import(): void { set_time_limit(0); // 获取文件 // $file = $this->request->request('file'); $file = $this->request->file('file'); if (!$file) { $this->error('上传文件错误'); } $attachment = ''; try { $upload = new Upload($file); $attachment = $upload->upload(null, $this->auth->id); unset($attachment['create_time'], $attachment['quote']); } catch (Throwable $e) { $this->error($e->getMessage()); } if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) { $this->error(__('No results were found')); } $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url']; if (!(is_file($url))) { $this->error('文件获取失败'); } // 读取内容 $excel_data = read_excel($url); if (empty($excel_data)) { $this->error('请填写导入数据'); } $data = []; foreach ($excel_data as $key=>$val) { foreach ($val as &$v) { $v = str_replace(' ', '', trim($v)); $v = str_replace("\xC2\xA0", '', $v); } unset($v); $data[] = [ 'BW'=>$val[0], //检查部位 'BW_CODE'=>$val[1], //部位编码 'XM'=>$val[2], //检查项目 'XM_CODE'=>$val[3], //项目编码 'modality'=>$val[4], //模态 'area'=>$val[5], //互认范围 'ext'=>$val[6], //备注 ]; } if(!empty($data)) { $this->model->saveAll($data); }else{ $this->error('导入失败,未读取到数据'); } $this->success('导入成功'); } /** * 添加 */ public function add(): void { if ($this->request->isPost()) { $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } $data = $this->excludeFields($data); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $data[$this->dataLimitField] = $this->auth->id; } $result = false; $this->model->startTrans(); try { // 模型验证 if ($this->modelValidate) { $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)); if (class_exists($validate)) { $validate = new $validate(); if ($this->modelSceneValidate) $validate->scene('add'); $validate->check($data); } } if(!empty($data['create_user_id'])) { $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname'); } $result = $this->model->save($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Added successfully')); } else { $this->error(__('No rows were added')); } } $this->error(__('Parameter error')); } /** * 编辑 * @throws Throwable */ public function edit(): void { $pk = $this->model->getPk(); $id = $this->request->param($pk); $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } $dataLimitAdminIds = $this->getDataLimitAdminIds(); if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) { $this->error(__('You have no permission')); } if ($this->request->isPost()) { $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } $data = $this->excludeFields($data); $result = false; $this->model->startTrans(); try { // 模型验证 if ($this->modelValidate) { $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)); if (class_exists($validate)) { $validate = new $validate(); if ($this->modelSceneValidate) $validate->scene('edit'); $data[$pk] = $row[$pk]; $validate->check($data); } } if(!empty($data['create_user_id'])) { $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname'); } $result = $row->save($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } $this->success('', [ 'row' => $row ]); } }