Institution.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace app\admin\controller\institution;
  3. use app\admin\model\Admin;
  4. use app\common\library\Upload;
  5. use think\facade\Cache;
  6. use think\facade\Db;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\exception\DbException;
  10. use Throwable;
  11. use app\common\controller\Backend;
  12. /**
  13. * 机构管理
  14. */
  15. class Institution extends Backend
  16. {
  17. /**
  18. * Institution模型对象
  19. * @var object
  20. * @phpstan-var \app\admin\model\institution\Institution
  21. */
  22. protected object $model;
  23. protected array|string $preExcludeFields = ['id', 'update_time'];
  24. protected array $withJoinTable = ['parentInstitution'];
  25. protected string|array $quickSearchField = ['name'];
  26. protected array $noNeedPermission = ['getInstitutionTree','getInsList','getTypeList','makeEmpower'];
  27. protected array $noNeedLogin = [];
  28. public function initialize(): void
  29. {
  30. parent::initialize();
  31. $this->model = new \app\admin\model\institution\Institution();
  32. }
  33. /**
  34. * 查看
  35. * @throws Throwable
  36. */
  37. public function index(): void
  38. {
  39. // 如果是 select 则转发到 select 方法,若未重写该方法,其实还是继续执行 index
  40. if ($this->request->param('select')) {
  41. $this->select();
  42. }
  43. /**
  44. * 1. withJoin 不可使用 alias 方法设置表别名,别名将自动使用关联模型名称(小写下划线命名规则)
  45. * 2. 以下的别名设置了主表别名,同时便于拼接查询参数等
  46. * 3. paginate 数据集可使用链式操作 each(function($item, $key) {}) 遍历处理
  47. */
  48. list($where, $alias, $limit, $order) = $this->queryBuilder();
  49. $res = $this->model
  50. ->withJoin($this->withJoinTable, $this->withJoinType)
  51. ->alias($alias)
  52. ->where($where)
  53. ->order($order)
  54. ->paginate($limit);
  55. $res->visible(['parentInstitution' => ['name']]);
  56. $this->success('', [
  57. 'list' => $res->items(),
  58. 'total' => $res->total(),
  59. 'remark' => get_route_remark(),
  60. ]);
  61. }
  62. /**
  63. * 若需重写查看、编辑、删除等方法,请复制 @throws \Exception
  64. * @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
  65. */
  66. public function import()
  67. {
  68. set_time_limit(0);
  69. // 获取文件
  70. // $file = $this->request->request('file');
  71. $file = $this->request->file('file');
  72. if (!$file) {
  73. $this->error('上传文件错误');
  74. }
  75. $attachment = '';
  76. try {
  77. $upload = new Upload($file);
  78. $attachment = $upload->upload(null, $this->auth->id);
  79. unset($attachment['create_time'], $attachment['quote']);
  80. } catch (Throwable $e) {
  81. $this->error($e->getMessage());
  82. }
  83. if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
  84. $this->error(__('No results were found'));
  85. }
  86. $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
  87. $allInstitution = $this->model->cache('allIns',300)->column('id','institution_code');
  88. if (!(is_file($url))) {
  89. $this->error('文件获取失败');
  90. }
  91. // 读取内容
  92. $excel_data = read_excel($url);
  93. if (empty($excel_data)) {
  94. $this->error('请填写导入数据');
  95. }
  96. $data = [];
  97. foreach ($excel_data as $key=>$val) {
  98. foreach ($val as &$v) {
  99. $v = str_replace(' ', '', trim($v));
  100. $v = str_replace("\xC2\xA0", '', $v);
  101. }
  102. $parent = '';
  103. if(!empty($val[3]))
  104. {
  105. $parent = $allInstitution[$val[3]] ?? '';
  106. }
  107. if(empty($val[0]))
  108. {
  109. continue;
  110. }
  111. unset($v);
  112. $data[] = [
  113. 'name'=>$val[0], //机构名称
  114. 'institution_code'=>$val[1], //机构编码
  115. 'institution_type'=>$val[2], //机构类型
  116. 'parent_institution_id'=>$parent, //上级医院
  117. 'port_empower'=>md5($val[1].rand(100000,999999)), //接口授权ID
  118. 'area'=>$val[5], //互认范围
  119. ];
  120. }
  121. Cache::delete('institution_dashboard');
  122. Cache::delete('allIns');
  123. if(!empty($data))
  124. {
  125. $this->model->saveAll($data);
  126. }else{
  127. $this->error('导入失败,未读取到数据');
  128. }
  129. $this->success('导入成功');
  130. }
  131. /**
  132. * @throws DataNotFoundException
  133. * @throws DbException
  134. * @throws ModelNotFoundException
  135. * @throws \think\db\exception\DbException
  136. */
  137. public function getInstitutionTree(): bool|string
  138. {
  139. $id= $this->request->post('institution_id') ?? '';
  140. $name= $this->request->post('institution_name') ?? '';
  141. $ins = $this->model->select()->toArray();
  142. $institution = [];
  143. foreach ($ins as $k=>$v)
  144. {
  145. $institution[$v['parent_institution_id']][] = $v;
  146. }
  147. if(!empty($id))
  148. {
  149. $organization = $this->model->where('id',$id)->select()->toArray();
  150. }elseif(!empty($name)){
  151. $organization = $this->model->where('name',$name)->select()->toArray();
  152. }else{
  153. $organization = $this->model->where('parent_institution_id',0)->select()->toArray();
  154. }
  155. $data = $this->makeInsData($institution,$organization);
  156. $this->success('success',$data);
  157. }
  158. public function makeInsData($institution,$organization): array
  159. {
  160. $data = [];
  161. if($organization['name'] ?? '')
  162. {
  163. $data[] = ['label'=>$organization['name']];
  164. return $data;
  165. }
  166. foreach ($organization as $k=>$v)
  167. {
  168. if(!empty($institution[$v['id']]))
  169. {
  170. $child = $this->makeInsData($institution,$institution[$v['id']]);
  171. $data[] = [
  172. 'label'=>$v['name'],
  173. 'id'=>$v['id'],
  174. 'children'=>$child
  175. ];
  176. }else{
  177. $data[] = [
  178. 'label'=>$v['name'],
  179. 'id'=>$v['id'],
  180. 'children'=>[]
  181. ];
  182. }
  183. }
  184. return $data;
  185. }
  186. public function getInsList(): void
  187. {
  188. $res = $this->model->field(['id','name'])->select()->toArray();
  189. $this->success('', ['list' => $res]);
  190. }
  191. public function getTypeList(): void
  192. {
  193. $type= $this->request->post('type');
  194. if(empty($type))
  195. {
  196. $this->error('参数错误');
  197. }
  198. $res = Db::name('dict_common_data')->where('type',$type)->field(['id','name'])->select()->toArray();
  199. $this->success('', ['list' => $res]);
  200. }
  201. /**
  202. * 添加
  203. */
  204. public function add(): void
  205. {
  206. if ($this->request->isPost()) {
  207. $data = $this->request->post();
  208. if (!$data) {
  209. $this->error(__('Parameter %s can not be empty', ['']));
  210. }
  211. $data = $this->excludeFields($data);
  212. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  213. $data[$this->dataLimitField] = $this->auth->id;
  214. }
  215. $result = false;
  216. $this->model->startTrans();
  217. try {
  218. // 模型验证
  219. if ($this->modelValidate) {
  220. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  221. if (class_exists($validate)) {
  222. $validate = new $validate();
  223. if ($this->modelSceneValidate) $validate->scene('add');
  224. $validate->check($data);
  225. }
  226. }
  227. if(!empty($data['create_user_id']))
  228. {
  229. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  230. }
  231. $data['port_empower'] = md5($data['institution_code'].rand(100000,999999));
  232. $result = $this->model->save($data);
  233. $this->model->commit();
  234. Cache::delete('institution_dashboard');
  235. Cache::delete('allIns');
  236. } catch (Throwable $e) {
  237. $this->model->rollback();
  238. $this->error($e->getMessage());
  239. }
  240. if ($result !== false) {
  241. $this->success(__('Added successfully'));
  242. } else {
  243. $this->error(__('No rows were added'));
  244. }
  245. }
  246. $this->error(__('Parameter error'));
  247. }
  248. /**
  249. * 编辑
  250. * @throws Throwable
  251. */
  252. public function edit(): void
  253. {
  254. $pk = $this->model->getPk();
  255. $id = $this->request->param($pk);
  256. $row = $this->model->find($id);
  257. if (!$row) {
  258. $this->error(__('Record not found'));
  259. }
  260. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  261. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  262. $this->error(__('You have no permission'));
  263. }
  264. if ($this->request->isPost()) {
  265. $data = $this->request->post();
  266. if (!$data) {
  267. $this->error(__('Parameter %s can not be empty', ['']));
  268. }
  269. $data = $this->excludeFields($data);
  270. $result = false;
  271. $this->model->startTrans();
  272. try {
  273. // 模型验证
  274. if ($this->modelValidate) {
  275. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  276. if (class_exists($validate)) {
  277. $validate = new $validate();
  278. if ($this->modelSceneValidate) $validate->scene('edit');
  279. $data[$pk] = $row[$pk];
  280. $validate->check($data);
  281. }
  282. }
  283. $this->checkChild($data['parent_institution_id'],$id);
  284. if(!empty($data['create_user_id']))
  285. {
  286. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  287. }
  288. if($row['port_empower'] != $data['port_empower'])
  289. {
  290. $info = Cache::get($row['port_empower']);
  291. if(!empty($info))
  292. {
  293. foreach ($info as $k=>$v)
  294. {
  295. Cache::delete($v[$row['port_empower']]);
  296. }
  297. }
  298. Cache::delete($row['port_empower']);
  299. }
  300. $result = $row->save($data);
  301. $this->model->commit();
  302. Cache::delete('institution_dashboard');
  303. Cache::delete('allIns');
  304. } catch (Throwable $e) {
  305. $this->model->rollback();
  306. if(empty($e->getMessage()))
  307. {
  308. $this->error($this->auth->getError());
  309. }else{
  310. $this->error($e->getMessage());
  311. }
  312. }
  313. if ($result !== false) {
  314. $this->success(__('Update successful'));
  315. } else {
  316. $this->error(__('No rows updated'));
  317. }
  318. }
  319. $this->success('', [
  320. 'row' => $row
  321. ]);
  322. }
  323. public function checkChild($parent,$id)
  324. {
  325. if($parent == $id)
  326. {
  327. $this->auth->setError('当前医院的上级医院不能为自己');
  328. $this->error('当前医院的上级医院不能为自己');
  329. }
  330. $child = $this->model->where('parent_institution_id',$id)->find();
  331. if(!empty($child))
  332. {
  333. if($child['id'] == $parent)
  334. {
  335. $this->auth->setError('当前医院的上级医院不能为自己');
  336. $this->error('当前医院的上级医院不能为自己的下级');
  337. }else{
  338. $this->checkChild($parent,$child['id']);
  339. }
  340. }
  341. }
  342. public function makeEmpower(): string
  343. {
  344. $code = $this->request->post('code');
  345. $this->success('success',md5($code.rand(100000,999999)));
  346. }
  347. }