Rota.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\admin\controller\institution;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 医生值班管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Rota extends Backend
  11. {
  12. /**
  13. * Rota模型对象
  14. * @var \app\admin\model\institution\Rota
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\institution\Rota;
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  26. */
  27. /**
  28. * 添加
  29. */
  30. public function add()
  31. {
  32. if ($this->request->isPost()) {
  33. $params = $this->request->post("row/a");
  34. if ($params) {
  35. $params = $this->preExcludeFields($params);
  36. if($params['rota_datetime'] < date('Ymd'))
  37. {
  38. $this->error('不可设置今天之前的日期');
  39. }
  40. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  41. $params[$this->dataLimitField] = $this->auth->id;
  42. }
  43. $result = false;
  44. Db::startTrans();
  45. try {
  46. //是否采用模型验证
  47. if ($this->modelValidate) {
  48. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  49. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  50. $this->model->validateFailException(true)->validate($validate);
  51. }
  52. $ins_name = DB::table('institution')->where('id',$params['institution'])->value('name');
  53. $data = [];
  54. foreach (explode(',',$params['doctor_id']) as $k=>$v)
  55. {
  56. if(empty($v))
  57. {
  58. continue;
  59. }
  60. $array = [];
  61. $array['institution_id'] = $params['institution'];
  62. $array['institution_name'] = $ins_name;
  63. $array['rota_datetime'] = $params['rota_datetime'];
  64. $array['doctor_id'] = $v;
  65. $array['doctor_name'] = DB::table('doctors')->where('id',$v)->value('realname');
  66. $data[] = $array;
  67. }
  68. $result = $this->model->allowField(true)->saveAll($data);
  69. Db::commit();
  70. } catch (ValidateException $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. } catch (PDOException $e) {
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. } catch (Exception $e) {
  77. Db::rollback();
  78. $this->error($e->getMessage());
  79. }
  80. if ($result !== false) {
  81. $this->success();
  82. } else {
  83. $this->error(__('No rows were inserted'));
  84. }
  85. }
  86. $this->error(__('Parameter %s can not be empty', ''));
  87. }
  88. return $this->view->fetch();
  89. }
  90. /**
  91. * 编辑
  92. */
  93. public function edit($ids = null)
  94. {
  95. $row = $this->model->get($ids);
  96. if (!$row) {
  97. $this->error(__('No Results were found'));
  98. }
  99. $adminIds = $this->getDataLimitAdminIds();
  100. if (is_array($adminIds)) {
  101. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  102. $this->error(__('You have no permission'));
  103. }
  104. }
  105. if ($this->request->isPost()) {
  106. $params = $this->request->post("row/a");
  107. if ($params) {
  108. $params = $this->preExcludeFields($params);
  109. if($params['rota_datetime'] < date('Ymd') && $params['rota_datetime'] !== $row['rota_datetime'])
  110. {
  111. $this->error('不可修改为设置今天之前的日期');
  112. }
  113. $result = false;
  114. Db::startTrans();
  115. try {
  116. //是否采用模型验证
  117. if ($this->modelValidate) {
  118. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  119. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  120. $row->validateFailException(true)->validate($validate);
  121. }
  122. $params['doctor_name'] = DB::table('doctors')->where('id',$params['doctor_id'])->value('realname');
  123. $result = $row->allowField(true)->save($params);
  124. Db::commit();
  125. } catch (ValidateException $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. } catch (PDOException $e) {
  129. Db::rollback();
  130. $this->error($e->getMessage());
  131. } catch (Exception $e) {
  132. Db::rollback();
  133. $this->error($e->getMessage());
  134. }
  135. if ($result !== false) {
  136. $this->success();
  137. } else {
  138. $this->error(__('No rows were updated'));
  139. }
  140. }
  141. $this->error(__('Parameter %s can not be empty', ''));
  142. }
  143. $this->view->assign("row", $row);
  144. return $this->view->fetch();
  145. }
  146. /**
  147. * 删除
  148. */
  149. public function del($ids = "")
  150. {
  151. if ($ids) {
  152. $pk = $this->model->getPk();
  153. $adminIds = $this->getDataLimitAdminIds();
  154. if (is_array($adminIds)) {
  155. $this->model->where($this->dataLimitField, 'in', $adminIds);
  156. }
  157. $list = $this->model->where($pk, 'in', $ids)->select();
  158. $count = 0;
  159. Db::startTrans();
  160. try {
  161. foreach ($list as $k => $v) {
  162. $count += $v->delete();
  163. }
  164. Db::commit();
  165. } catch (PDOException $e) {
  166. Db::rollback();
  167. $this->error($e->getMessage());
  168. } catch (Exception $e) {
  169. Db::rollback();
  170. $this->error($e->getMessage());
  171. }
  172. if ($count) {
  173. $this->success();
  174. } else {
  175. $this->error(__('No rows were deleted'));
  176. }
  177. }
  178. $this->error(__('Parameter %s can not be empty', 'ids'));
  179. }
  180. }