Engineers.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\admin\controller\workorder;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 工程师管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Engineers extends Backend
  14. {
  15. /**
  16. * Engineers模型对象
  17. * @var \app\admin\model\workorder\Engineers
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\workorder\Engineers;
  24. $this->view->assign("classList", $this->model->getClassList());
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $total = $this->model->with(['user'])->where($where)->order($sort, $order)->count();
  47. $list = $this->model->with(['user'])->where($where)->order($sort, $order)->limit($offset, $limit)->select();
  48. foreach ($list as $row) {
  49. $row->getRelation('user')->visible(['username', 'nickname', 'email', 'mobile', 'avatar']);
  50. }
  51. $list = collection($list)->toArray();
  52. $result = ["total" => $total, "rows" => $list];
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 添加
  59. */
  60. public function add()
  61. {
  62. if ($this->request->isPost()) {
  63. $params = $this->request->post("row/a");
  64. if ($params) {
  65. $params = $this->preExcludeFields($params);
  66. if ($this->model->get(['user_id' => $params['user_id']])) {
  67. $this->error(__('The user is already an engineer!'));
  68. }
  69. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  70. $params[$this->dataLimitField] = $this->auth->id;
  71. }
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. //是否采用模型验证
  76. if ($this->modelValidate) {
  77. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  78. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  79. $this->model->validateFailException(true)->validate($validate);
  80. }
  81. $result = $this->model->allowField(true)->save($params);
  82. Db::commit();
  83. } catch (ValidateException $e) {
  84. Db::rollback();
  85. $this->error($e->getMessage());
  86. } catch (PDOException $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. } catch (Exception $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. }
  93. if ($result !== false) {
  94. $this->success();
  95. } else {
  96. $this->error(__('No rows were inserted'));
  97. }
  98. }
  99. $this->error(__('Parameter %s can not be empty', ''));
  100. }
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 编辑
  105. */
  106. public function edit($ids = null)
  107. {
  108. $row = $this->model->get($ids);
  109. if (!$row) {
  110. $this->error(__('No Results were found'));
  111. }
  112. $adminIds = $this->getDataLimitAdminIds();
  113. if (is_array($adminIds)) {
  114. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  115. $this->error(__('You have no permission'));
  116. }
  117. }
  118. if ($this->request->isPost()) {
  119. $params = $this->request->post("row/a");
  120. if ($params) {
  121. if ($params['user_id'] != $row['user_id'] && $this->model->get(['user_id' => $params['user_id']])) {
  122. $this->error(__('The user is already an engineer!'));
  123. }
  124. $params = $this->preExcludeFields($params);
  125. $result = false;
  126. Db::startTrans();
  127. try {
  128. //是否采用模型验证
  129. if ($this->modelValidate) {
  130. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  131. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  132. $row->validateFailException(true)->validate($validate);
  133. }
  134. $result = $row->allowField(true)->save($params);
  135. Db::commit();
  136. } catch (ValidateException $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. } catch (PDOException $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. } catch (Exception $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. }
  146. if ($result !== false) {
  147. $this->success();
  148. } else {
  149. $this->error(__('No rows were updated'));
  150. }
  151. }
  152. $this->error(__('Parameter %s can not be empty', ''));
  153. }
  154. $this->view->assign("row", $row);
  155. return $this->view->fetch();
  156. }
  157. }