Examproject.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\admin\controller\dict;
  3. use app\admin\model\Admin;
  4. use app\common\controller\Backend;
  5. use app\common\library\Upload;
  6. use Throwable;
  7. /**
  8. * 字典-影像检查设备类型代码管理
  9. */
  10. class Examproject extends Backend
  11. {
  12. /**
  13. * Examproject模型对象
  14. * @var object
  15. * @phpstan-var \app\admin\model\dict\Examproject
  16. */
  17. protected object $model;
  18. protected string|array $defaultSortField = 'weigh,desc';
  19. protected array|string $preExcludeFields = ['id', 'update_time'];
  20. protected string|array $quickSearchField = ['id'];
  21. public function initialize(): void
  22. {
  23. parent::initialize();
  24. $this->model = new \app\admin\model\dict\Examproject();
  25. }
  26. /**
  27. * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
  28. */
  29. public function import(): void
  30. {
  31. set_time_limit(0);
  32. // 获取文件
  33. // $file = $this->request->request('file');
  34. $file = $this->request->file('file');
  35. if (!$file) {
  36. $this->error('上传文件错误');
  37. }
  38. $attachment = '';
  39. try {
  40. $upload = new Upload($file);
  41. $attachment = $upload->upload(null, $this->auth->id);
  42. unset($attachment['create_time'], $attachment['quote']);
  43. } catch (Throwable $e) {
  44. $this->error($e->getMessage());
  45. }
  46. if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
  47. $this->error(__('No results were found'));
  48. }
  49. $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
  50. if (!(is_file($url))) {
  51. $this->error('文件获取失败');
  52. }
  53. // 读取内容
  54. $excel_data = read_excel($url);
  55. if (empty($excel_data)) {
  56. $this->error('请填写导入数据');
  57. }
  58. $data = [];
  59. foreach ($excel_data as $key=>$val) {
  60. foreach ($val as &$v) {
  61. $v = str_replace(' ', '', trim($v));
  62. $v = str_replace("\xC2\xA0", '', $v);
  63. }
  64. unset($v);
  65. $data[] = [
  66. 'BW'=>$val[0], //检查部位
  67. 'BW_CODE'=>$val[1], //部位编码
  68. 'XM'=>$val[2], //检查项目
  69. 'XM_CODE'=>$val[3], //项目编码
  70. 'modality'=>$val[4], //模态
  71. 'area'=>$val[5], //互认范围
  72. 'ext'=>$val[6], //备注
  73. ];
  74. }
  75. if(!empty($data))
  76. {
  77. $this->model->saveAll($data);
  78. }else{
  79. $this->error('导入失败,未读取到数据');
  80. }
  81. $this->success('导入成功');
  82. }
  83. /**
  84. * 添加
  85. */
  86. public function add(): void
  87. {
  88. if ($this->request->isPost()) {
  89. $data = $this->request->post();
  90. if (!$data) {
  91. $this->error(__('Parameter %s can not be empty', ['']));
  92. }
  93. $data = $this->excludeFields($data);
  94. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  95. $data[$this->dataLimitField] = $this->auth->id;
  96. }
  97. $result = false;
  98. $this->model->startTrans();
  99. try {
  100. // 模型验证
  101. if ($this->modelValidate) {
  102. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  103. if (class_exists($validate)) {
  104. $validate = new $validate();
  105. if ($this->modelSceneValidate) $validate->scene('add');
  106. $validate->check($data);
  107. }
  108. }
  109. if(!empty($data['create_user_id']))
  110. {
  111. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  112. }
  113. $result = $this->model->save($data);
  114. $this->model->commit();
  115. } catch (Throwable $e) {
  116. $this->model->rollback();
  117. $this->error($e->getMessage());
  118. }
  119. if ($result !== false) {
  120. $this->success(__('Added successfully'));
  121. } else {
  122. $this->error(__('No rows were added'));
  123. }
  124. }
  125. $this->error(__('Parameter error'));
  126. }
  127. /**
  128. * 编辑
  129. * @throws Throwable
  130. */
  131. public function edit(): void
  132. {
  133. $pk = $this->model->getPk();
  134. $id = $this->request->param($pk);
  135. $row = $this->model->find($id);
  136. if (!$row) {
  137. $this->error(__('Record not found'));
  138. }
  139. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  140. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  141. $this->error(__('You have no permission'));
  142. }
  143. if ($this->request->isPost()) {
  144. $data = $this->request->post();
  145. if (!$data) {
  146. $this->error(__('Parameter %s can not be empty', ['']));
  147. }
  148. $data = $this->excludeFields($data);
  149. $result = false;
  150. $this->model->startTrans();
  151. try {
  152. // 模型验证
  153. if ($this->modelValidate) {
  154. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  155. if (class_exists($validate)) {
  156. $validate = new $validate();
  157. if ($this->modelSceneValidate) $validate->scene('edit');
  158. $data[$pk] = $row[$pk];
  159. $validate->check($data);
  160. }
  161. }
  162. if(!empty($data['create_user_id']))
  163. {
  164. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  165. }
  166. $result = $row->save($data);
  167. $this->model->commit();
  168. } catch (Throwable $e) {
  169. $this->model->rollback();
  170. $this->error($e->getMessage());
  171. }
  172. if ($result !== false) {
  173. $this->success(__('Update successful'));
  174. } else {
  175. $this->error(__('No rows updated'));
  176. }
  177. }
  178. $this->success('', [
  179. 'row' => $row
  180. ]);
  181. }
  182. }