123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace app\admin\controller\dict;
- use app\admin\model\Admin;
- use app\common\controller\Backend;
- use app\common\library\Upload;
- use Throwable;
- /**
- * 字典-影像检查设备类型代码管理
- */
- class Examproject extends Backend
- {
- /**
- * Examproject模型对象
- * @var object
- * @phpstan-var \app\admin\model\dict\Examproject
- */
- protected object $model;
- protected string|array $defaultSortField = 'weigh,desc';
- protected array|string $preExcludeFields = ['id', 'update_time'];
- protected string|array $quickSearchField = ['id'];
- public function initialize(): void
- {
- parent::initialize();
- $this->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
- ]);
- }
- }
|