123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- namespace app\admin\controller\institution;
- use app\admin\model\Admin;
- use app\common\library\Upload;
- use think\facade\Cache;
- use think\facade\Db;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\DbException;
- use Throwable;
- use app\common\controller\Backend;
- /**
- * 机构管理
- */
- class Institution extends Backend
- {
- /**
- * Institution模型对象
- * @var object
- * @phpstan-var \app\admin\model\institution\Institution
- */
- protected object $model;
- protected array|string $preExcludeFields = ['id', 'update_time'];
- protected array $withJoinTable = ['parentInstitution'];
- protected string|array $quickSearchField = ['name'];
- protected array $noNeedPermission = ['getInstitutionTree','getInsList','getTypeList','makeEmpower'];
- protected array $noNeedLogin = [];
- public function initialize(): void
- {
- parent::initialize();
- $this->model = new \app\admin\model\institution\Institution();
- }
- /**
- * 查看
- * @throws Throwable
- */
- public function index(): void
- {
- // 如果是 select 则转发到 select 方法,若未重写该方法,其实还是继续执行 index
- if ($this->request->param('select')) {
- $this->select();
- }
- /**
- * 1. withJoin 不可使用 alias 方法设置表别名,别名将自动使用关联模型名称(小写下划线命名规则)
- * 2. 以下的别名设置了主表别名,同时便于拼接查询参数等
- * 3. paginate 数据集可使用链式操作 each(function($item, $key) {}) 遍历处理
- */
- list($where, $alias, $limit, $order) = $this->queryBuilder();
- $res = $this->model
- ->withJoin($this->withJoinTable, $this->withJoinType)
- ->alias($alias)
- ->where($where)
- ->order($order)
- ->paginate($limit);
- $res->visible(['parentInstitution' => ['name']]);
- $this->success('', [
- 'list' => $res->items(),
- 'total' => $res->total(),
- 'remark' => get_route_remark(),
- ]);
- }
- /**
- * 若需重写查看、编辑、删除等方法,请复制 @throws \Exception
- * @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
- */
- public function import()
- {
- 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'];
- $allInstitution = $this->model->cache('allIns',300)->column('id','institution_code');
- 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);
- }
- $parent = '';
- if(!empty($val[3]))
- {
- $parent = $allInstitution[$val[3]] ?? '';
- }
- if(empty($val[0]))
- {
- continue;
- }
- unset($v);
- $data[] = [
- 'name'=>$val[0], //机构名称
- 'institution_code'=>$val[1], //机构编码
- 'institution_type'=>$val[2], //机构类型
- 'parent_institution_id'=>$parent, //上级医院
- 'port_empower'=>md5($val[1].rand(100000,999999)), //接口授权ID
- 'area'=>$val[5], //互认范围
- ];
- }
- Cache::delete('institution_dashboard');
- Cache::delete('allIns');
- if(!empty($data))
- {
- $this->model->saveAll($data);
- }else{
- $this->error('导入失败,未读取到数据');
- }
- $this->success('导入成功');
- }
- /**
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws \think\db\exception\DbException
- */
- public function getInstitutionTree(): bool|string
- {
- $id= $this->request->post('institution_id') ?? '';
- $name= $this->request->post('institution_name') ?? '';
- $ins = $this->model->select()->toArray();
- $institution = [];
- foreach ($ins as $k=>$v)
- {
- $institution[$v['parent_institution_id']][] = $v;
- }
- if(!empty($id))
- {
- $organization = $this->model->where('id',$id)->select()->toArray();
- }elseif(!empty($name)){
- $organization = $this->model->where('name',$name)->select()->toArray();
- }else{
- $organization = $this->model->where('parent_institution_id',0)->select()->toArray();
- }
- $data = $this->makeInsData($institution,$organization);
- $this->success('success',$data);
- }
- public function makeInsData($institution,$organization): array
- {
- $data = [];
- if($organization['name'] ?? '')
- {
- $data[] = ['label'=>$organization['name']];
- return $data;
- }
- foreach ($organization as $k=>$v)
- {
- if(!empty($institution[$v['id']]))
- {
- $child = $this->makeInsData($institution,$institution[$v['id']]);
- $data[] = [
- 'label'=>$v['name'],
- 'id'=>$v['id'],
- 'children'=>$child
- ];
- }else{
- $data[] = [
- 'label'=>$v['name'],
- 'id'=>$v['id'],
- 'children'=>[]
- ];
- }
- }
- return $data;
- }
- public function getInsList(): void
- {
- $res = $this->model->field(['id','name'])->select()->toArray();
- $this->success('', ['list' => $res]);
- }
- public function getTypeList(): void
- {
- $type= $this->request->post('type');
- if(empty($type))
- {
- $this->error('参数错误');
- }
- $res = Db::name('dict_common_data')->where('type',$type)->field(['id','name'])->select()->toArray();
- $this->success('', ['list' => $res]);
- }
- /**
- * 添加
- */
- 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');
- }
- $data['port_empower'] = md5($data['institution_code'].rand(100000,999999));
- $result = $this->model->save($data);
- $this->model->commit();
- Cache::delete('institution_dashboard');
- Cache::delete('allIns');
- } 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);
- }
- }
- $this->checkChild($data['parent_institution_id'],$id);
- if(!empty($data['create_user_id']))
- {
- $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
- }
- if($row['port_empower'] != $data['port_empower'])
- {
- $info = Cache::get($row['port_empower']);
- if(!empty($info))
- {
- foreach ($info as $k=>$v)
- {
- Cache::delete($v[$row['port_empower']]);
- }
- }
- Cache::delete($row['port_empower']);
- }
- $result = $row->save($data);
- $this->model->commit();
- Cache::delete('institution_dashboard');
- Cache::delete('allIns');
- } catch (Throwable $e) {
- $this->model->rollback();
- if(empty($e->getMessage()))
- {
- $this->error($this->auth->getError());
- }else{
- $this->error($e->getMessage());
- }
- }
- if ($result !== false) {
- $this->success(__('Update successful'));
- } else {
- $this->error(__('No rows updated'));
- }
- }
- $this->success('', [
- 'row' => $row
- ]);
- }
- public function checkChild($parent,$id)
- {
- if($parent == $id)
- {
- $this->auth->setError('当前医院的上级医院不能为自己');
- $this->error('当前医院的上级医院不能为自己');
- }
- $child = $this->model->where('parent_institution_id',$id)->find();
- if(!empty($child))
- {
- if($child['id'] == $parent)
- {
- $this->auth->setError('当前医院的上级医院不能为自己');
- $this->error('当前医院的上级医院不能为自己的下级');
- }else{
- $this->checkChild($parent,$child['id']);
- }
- }
- }
- public function makeEmpower(): string
- {
- $code = $this->request->post('code');
- $this->success('success',md5($code.rand(100000,999999)));
- }
- }
|