123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- namespace app\admin\controller\institution;
- use app\common\controller\Backend;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 远程诊断关系管理
- *
- * @icon fa fa-circle-o
- */
- class Contact extends Backend
- {
-
- /**
- * Contact模型对象
- * @var \app\admin\model\institution\Contact
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\institution\Contact;
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- // 判断关系是否已存在
- $exist = $this->model
- ->where('hospital_id', $params['hospital_id'])
- ->where('super_hospital_id', $params['super_hospital_id'])
- ->value('id');
- if($exist){
- $this->error('此关系已维护,请直接修改');
- }
- $this->institution_status($params['hospital_id'],$params['super_hospital_id']);
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- if($params['remote_doctor']){
- $res = $this->saveRemoteDoctor($params['hospital_id'], $params['super_hospital_id'], $params['remote_doctor']);
- if(!$res){
- Db::rollback();
- $this->error('提交远程医生数据失败');
- }
- }
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- /**
- * 添加上下级医院关系
- */
- public function institution_status($local,$remote)
- {
- $ins = DB::table('institution')->where('id',$local)->value('parent_institution');
- if(empty($ins))
- {
- DB::table('institution')->where('id',$local)->update(['parent_institution'=>$remote]);
- return;
- }
- $search = stripos($ins,$remote);
- if($search === false)
- {
- //不存在父级id
- $str = $ins.','.$remote;
- DB::table('institution')->where('id',$local)->update(['parent_institution'=>$str]);
- }
- return;
- }
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- $this->institution_status($row['hospital_id'],$row['super_hospital_id']);
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- if($params['remote_doctor']){
- $res = $this->saveRemoteDoctor($row['hospital_id'], $row['super_hospital_id'], $params['remote_doctor']);
- if(!$res){
- Db::rollback();
- $this->error('提交远程医生数据失败');
- }
- }
- // $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $remote_doctor = Db::table('remote_cost')
- ->where('hospital_id', $row['hospital_id'])
- ->where('super_hospital_id', $row['super_hospital_id'])
- ->column('super_doctor_id');
- $remote_doctor = implode(',', array_unique($remote_doctor));
- $this->view->assign("remote_doctor", $remote_doctor);
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if ($ids) {
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- Db::startTrans();
- try {
- $row = $this->model->get($ids);
- $res = Db::table('remote_cost')
- ->where('hospital_id', $row['hospital_id'])
- ->where('super_hospital_id', $row['super_hospital_id'])
- ->delete();
- if($res === false){
- Db::rollback();
- $this->error('删除远程医生关系数据失败');
- }
- foreach ($list as $k => $v) {
- $count += $v->delete();
- }
- Db::commit();
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * 提交远程医生关系表数据
- * @param $hospital_id
- * @param $super_hospital_id
- * @param $remote_doctor
- * @return bool
- * @throws Exception
- * @throws PDOException
- */
- public function saveRemoteDoctor($hospital_id, $super_hospital_id, $remote_doctor)
- {
- // 删除现有数据
- $res = Db::table('remote_cost')
- ->where('hospital_id', $hospital_id)
- ->where('super_hospital_id', $super_hospital_id)
- ->delete();
- if($res === false){
- return false;
- }
- // 提交最新数据
- $doctor_ids = explode(',', $remote_doctor);
- $doctors = model('DoctorModel','model\doctors')
- ->whereIn('id', $doctor_ids)
- ->column('id,realname,exam_class,is_admin');
- $insert = [];
- $class_arr = [
- 'DX',
- 'CR',
- 'CT',
- 'MR'
- ];
- foreach ($doctors as $val){
- foreach ($class_arr as $v){
- $insert[] = [
- 'hospital_id' => $hospital_id,
- 'super_hospital_id' => $super_hospital_id,
- 'exam_class' => $v,
- 'super_doctor_id' => $val['id'],
- 'timestamp' => time(),
- 'super_doctor_name' => $val['realname'],
- 'is_admin' => $val['is_admin'],
- ];
- }
- }
- $res = Db::table('remote_cost')->insertAll($insert);
- if($res === false){
- return false;
- }
- return true;
- }
- }
|