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; } }