Department.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\controller\institution;
  3. use app\admin\controller\syslog\SysLog;
  4. use app\common\controller\Backend;
  5. use app\common\library\SysLogs;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\DbException;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. use think\response\Json as JsonAlias;
  12. /**
  13. * 科室部门
  14. *
  15. * @icon fa fa-institution
  16. */
  17. class Department extends Backend
  18. {
  19. /**
  20. * Institution模型对象
  21. * @var \app\admin\model\institution\Institution
  22. */
  23. protected $model = null;
  24. /**
  25. * 无需鉴权的方法,但需要登录
  26. * @var array
  27. */
  28. protected $noNeedRight = ['institutionindex','departmentindex'];
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = new \app\admin\model\institution\Depart();
  33. }
  34. /**
  35. * 模板列表
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. return $this->view->fetch();
  42. }
  43. /**
  44. * 机构列表
  45. * @return string|\think\response\Json
  46. * @throws \think\Exception
  47. * @author matielong
  48. */
  49. public function institutionIndex()
  50. {
  51. //设置过滤方法
  52. $this->request->filter(['strip_tags']);
  53. if ($this->request->isAjax()) {
  54. list($where, $sort, $order, $offset, $limit) = $this->buildparams(['name']);
  55. // 过滤机构
  56. $childInsIds = $this->auth->getMyInsId();
  57. if($childInsIds == false){
  58. $more = false;
  59. } else {
  60. $more = ['id' => ['in', $childInsIds]];
  61. }
  62. $field = ['id', 'name', 'createdAt'];
  63. $myWhere = ['status' => '1'];
  64. $total = model('Institution','model\institution')
  65. ->where($more)
  66. ->where($where)
  67. ->where($myWhere)
  68. ->order($sort, $order)
  69. ->count();
  70. $list = model('Institution','model\institution')
  71. ->where($more)
  72. ->where($where)
  73. ->where($myWhere)
  74. ->order($sort, $order)
  75. ->limit($offset, $limit)
  76. ->field($field)
  77. ->select();
  78. $list = collection($list)->toArray();
  79. $result = array("total" => $total, "rows" => $list);
  80. return json($result);
  81. }
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 科室列表
  86. * @return string|\think\response\Json
  87. * @throws Exception
  88. * @author matielong
  89. */
  90. public function departmentIndex()
  91. {
  92. $ins_id = $this->request->param('ins_id');
  93. //设置过滤方法
  94. $this->request->filter(['strip_tags']);
  95. if ($this->request->isAjax()) {
  96. list($where, $sort, $order, $offset, $limit) = $this->buildparams($this->searchFields, true);
  97. $myWhere = ['institution_id' => $ins_id,];
  98. $total = $this->model
  99. ->where($where)
  100. ->where($myWhere)
  101. ->order($sort, $order)
  102. ->count();
  103. $list = $this->model
  104. ->where($where)
  105. ->where($myWhere)
  106. ->order($sort, $order)
  107. ->limit($offset, $limit)
  108. ->select();
  109. $list = collection($list)->toArray();
  110. $result = array("total" => $total, "rows" => $list);
  111. return json($result);
  112. }
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 添加
  117. * @return string
  118. * @throws Exception
  119. * @author matielong
  120. */
  121. public function add()
  122. {
  123. $ins_id = $this->request->param('ids');
  124. if ($this->request->isPost()) {
  125. $params = $this->request->post("row/a");
  126. if ($params) {
  127. $params = $this->preExcludeFields($params);
  128. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  129. $params[$this->dataLimitField] = $this->auth->id;
  130. }
  131. $result = false;
  132. Db::startTrans();
  133. try {
  134. //是否采用模型验证
  135. if ($this->modelValidate) {
  136. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  137. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  138. $this->model->validateFailException(true)->validate($validate);
  139. }
  140. $params['id'] = makeNew16Uid();
  141. $params['institution_id'] = $ins_id;
  142. $params['parent_id'] = 'root';
  143. $result = $this->model->allowField(true)->save($params);
  144. Db::commit();
  145. } catch (ValidateException $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. } catch (PDOException $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. } catch (Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. if ($result !== false) {
  156. $this->success();
  157. } else {
  158. $this->error(__('No rows were inserted'));
  159. }
  160. }
  161. $this->error(__('Parameter %s can not be empty', ''));
  162. }
  163. return $this->view->fetch();
  164. }
  165. }