Contact.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\admin\controller\institution;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 远程诊断关系管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Contact extends Backend
  14. {
  15. /**
  16. * Contact模型对象
  17. * @var \app\admin\model\institution\Contact
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\institution\Contact;
  24. }
  25. /**
  26. * 添加
  27. */
  28. public function add()
  29. {
  30. if ($this->request->isPost()) {
  31. $params = $this->request->post("row/a");
  32. if ($params) {
  33. $params = $this->preExcludeFields($params);
  34. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  35. $params[$this->dataLimitField] = $this->auth->id;
  36. }
  37. $result = false;
  38. // 判断关系是否已存在
  39. $exist = $this->model
  40. ->where('hospital_id', $params['hospital_id'])
  41. ->where('super_hospital_id', $params['super_hospital_id'])
  42. ->value('id');
  43. if($exist){
  44. $this->error('此关系已维护,请直接修改');
  45. }
  46. $this->institution_status($params['hospital_id'],$params['super_hospital_id']);
  47. Db::startTrans();
  48. try {
  49. //是否采用模型验证
  50. if ($this->modelValidate) {
  51. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  52. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  53. $this->model->validateFailException(true)->validate($validate);
  54. }
  55. if($params['remote_doctor']){
  56. $res = $this->saveRemoteDoctor($params['hospital_id'], $params['super_hospital_id'], $params['remote_doctor']);
  57. if(!$res){
  58. Db::rollback();
  59. $this->error('提交远程医生数据失败');
  60. }
  61. }
  62. $result = $this->model->allowField(true)->save($params);
  63. Db::commit();
  64. } catch (ValidateException $e) {
  65. Db::rollback();
  66. $this->error($e->getMessage());
  67. } catch (PDOException $e) {
  68. Db::rollback();
  69. $this->error($e->getMessage());
  70. } catch (Exception $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. }
  74. if ($result !== false) {
  75. $this->success();
  76. } else {
  77. $this->error(__('No rows were inserted'));
  78. }
  79. }
  80. $this->error(__('Parameter %s can not be empty', ''));
  81. }
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 添加上下级医院关系
  86. */
  87. public function institution_status($local,$remote)
  88. {
  89. $ins = DB::table('institution')->where('id',$local)->value('parent_institution');
  90. if(empty($ins))
  91. {
  92. DB::table('institution')->where('id',$local)->update(['parent_institution'=>$remote]);
  93. return;
  94. }
  95. $search = stripos($ins,$remote);
  96. if($search === false)
  97. {
  98. //不存在父级id
  99. $str = $ins.','.$remote;
  100. DB::table('institution')->where('id',$local)->update(['parent_institution'=>$str]);
  101. }
  102. return;
  103. }
  104. /**
  105. * 编辑
  106. */
  107. public function edit($ids = null)
  108. {
  109. $row = $this->model->get($ids);
  110. if (!$row) {
  111. $this->error(__('No Results were found'));
  112. }
  113. $adminIds = $this->getDataLimitAdminIds();
  114. if (is_array($adminIds)) {
  115. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. }
  119. $this->institution_status($row['hospital_id'],$row['super_hospital_id']);
  120. if ($this->request->isPost()) {
  121. $params = $this->request->post("row/a");
  122. if ($params) {
  123. $params = $this->preExcludeFields($params);
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException(true)->validate($validate);
  132. }
  133. if($params['remote_doctor']){
  134. $res = $this->saveRemoteDoctor($row['hospital_id'], $row['super_hospital_id'], $params['remote_doctor']);
  135. if(!$res){
  136. Db::rollback();
  137. $this->error('提交远程医生数据失败');
  138. }
  139. }
  140. // $result = $row->allowField(true)->save($params);
  141. Db::commit();
  142. } catch (ValidateException $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. } catch (PDOException $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. } catch (Exception $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. }
  152. if ($result !== false) {
  153. $this->success();
  154. } else {
  155. $this->error(__('No rows were updated'));
  156. }
  157. }
  158. $this->error(__('Parameter %s can not be empty', ''));
  159. }
  160. $remote_doctor = Db::table('remote_cost')
  161. ->where('hospital_id', $row['hospital_id'])
  162. ->where('super_hospital_id', $row['super_hospital_id'])
  163. ->column('super_doctor_id');
  164. $remote_doctor = implode(',', array_unique($remote_doctor));
  165. $this->view->assign("remote_doctor", $remote_doctor);
  166. $this->view->assign("row", $row);
  167. return $this->view->fetch();
  168. }
  169. /**
  170. * 删除
  171. */
  172. public function del($ids = "")
  173. {
  174. if ($ids) {
  175. $pk = $this->model->getPk();
  176. $adminIds = $this->getDataLimitAdminIds();
  177. if (is_array($adminIds)) {
  178. $this->model->where($this->dataLimitField, 'in', $adminIds);
  179. }
  180. $list = $this->model->where($pk, 'in', $ids)->select();
  181. $count = 0;
  182. Db::startTrans();
  183. try {
  184. $row = $this->model->get($ids);
  185. $res = Db::table('remote_cost')
  186. ->where('hospital_id', $row['hospital_id'])
  187. ->where('super_hospital_id', $row['super_hospital_id'])
  188. ->delete();
  189. if($res === false){
  190. Db::rollback();
  191. $this->error('删除远程医生关系数据失败');
  192. }
  193. foreach ($list as $k => $v) {
  194. $count += $v->delete();
  195. }
  196. Db::commit();
  197. } catch (PDOException $e) {
  198. Db::rollback();
  199. $this->error($e->getMessage());
  200. } catch (Exception $e) {
  201. Db::rollback();
  202. $this->error($e->getMessage());
  203. }
  204. if ($count) {
  205. $this->success();
  206. } else {
  207. $this->error(__('No rows were deleted'));
  208. }
  209. }
  210. $this->error(__('Parameter %s can not be empty', 'ids'));
  211. }
  212. /**
  213. * 提交远程医生关系表数据
  214. * @param $hospital_id
  215. * @param $super_hospital_id
  216. * @param $remote_doctor
  217. * @return bool
  218. * @throws Exception
  219. * @throws PDOException
  220. */
  221. public function saveRemoteDoctor($hospital_id, $super_hospital_id, $remote_doctor)
  222. {
  223. // 删除现有数据
  224. $res = Db::table('remote_cost')
  225. ->where('hospital_id', $hospital_id)
  226. ->where('super_hospital_id', $super_hospital_id)
  227. ->delete();
  228. if($res === false){
  229. return false;
  230. }
  231. // 提交最新数据
  232. $doctor_ids = explode(',', $remote_doctor);
  233. $doctors = model('DoctorModel','model\doctors')
  234. ->whereIn('id', $doctor_ids)
  235. ->column('id,realname,exam_class,is_admin');
  236. $insert = [];
  237. $class_arr = [
  238. 'DX',
  239. 'CR',
  240. 'CT',
  241. 'MR'
  242. ];
  243. foreach ($doctors as $val){
  244. foreach ($class_arr as $v){
  245. $insert[] = [
  246. 'hospital_id' => $hospital_id,
  247. 'super_hospital_id' => $super_hospital_id,
  248. 'exam_class' => $v,
  249. 'super_doctor_id' => $val['id'],
  250. 'timestamp' => time(),
  251. 'super_doctor_name' => $val['realname'],
  252. 'is_admin' => $val['is_admin'],
  253. ];
  254. }
  255. }
  256. $res = Db::table('remote_cost')->insertAll($insert);
  257. if($res === false){
  258. return false;
  259. }
  260. return true;
  261. }
  262. }